summaryrefslogtreecommitdiffstats
path: root/lib/SQL
diff options
context:
space:
mode:
authorAleksander Machniak <machniak@kolabsys.com>2014-02-24 14:18:59 +0100
committerAleksander Machniak <machniak@kolabsys.com>2014-02-24 14:18:59 +0100
commit21b6f160fdb3afec0906082767de317af43d4737 (patch)
treec4c150dcbb4729d558d777d34c525112e917a671 /lib/SQL
parent301f909fb4c65e1ceb34b224de30938436c6a884 (diff)
downloadwebadmin-21b6f160fdb3afec0906082767de317af43d4737.tar.gz
Implemented DB layer based on PDO (Bug #1041)
Diffstat (limited to 'lib/SQL')
-rw-r--r--lib/SQL/mysql.php133
1 files changed, 133 insertions, 0 deletions
diff --git a/lib/SQL/mysql.php b/lib/SQL/mysql.php
new file mode 100644
index 0000000..9eb7543
--- /dev/null
+++ b/lib/SQL/mysql.php
@@ -0,0 +1,133 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------------+
+ | This file is part of the Kolab Web Admin Panel |
+ | |
+ | Copyright (C) 2011-2014, Kolab Systems AG |
+ | |
+ | This program is free software: you can redistribute it and/or modify |
+ | it under the terms of the GNU Affero General Public License as published |
+ | by the Free Software Foundation, either version 3 of the License, or |
+ | (at your option) any later version. |
+ | |
+ | This program is distributed in the hope that it will be useful, |
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ | GNU Affero General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU Affero General Public License |
+ | along with this program. If not, see <http://www.gnu.org/licenses/> |
+ +--------------------------------------------------------------------------+
+ | Author: Aleksander Machniak <machniak@kolabsys.com> |
+ +--------------------------------------------------------------------------+
+*/
+
+/**
+ * Database independent query interface
+ *
+ * This is a wrapper for the PHP PDO
+ */
+class SQL_mysql extends SQL
+{
+ public $db_provider = 'mysql';
+
+ /**
+ * Object constructor
+ */
+ public function __construct($conn_name, $conn_dsn)
+ {
+ parent::__construct($conn_name, $conn_dsn);
+
+ // SQL identifiers quoting
+ $this->options['identifier_start'] = '`';
+ $this->options['identifier_end'] = '`';
+ }
+
+ /**
+ * Driver-specific configuration of database connection
+ */
+ protected function conn_configure()
+ {
+ if ($this->conn) {
+ $this->conn->query("SET NAMES 'utf8'");
+ }
+ }
+
+ /**
+ * Returns PDO DSN string from DSN array
+ *
+ * @param array $dsn DSN parameters
+ *
+ * @return string Connection string
+ */
+ protected function dsn_string($dsn)
+ {
+ $params = array();
+ $result = 'mysql:';
+
+ if ($dsn['database']) {
+ $params[] = 'dbname=' . $dsn['database'];
+ }
+
+ if ($dsn['hostspec']) {
+ $params[] = 'host=' . $dsn['hostspec'];
+ }
+
+ if ($dsn['port']) {
+ $params[] = 'port=' . $dsn['port'];
+ }
+
+ if ($dsn['socket']) {
+ $params[] = 'unix_socket=' . $dsn['socket'];
+ }
+
+ $params[] = 'charset=utf8';
+
+ if (!empty($params)) {
+ $result .= implode(';', $params);
+ }
+
+ return $result;
+ }
+
+ /**
+ * Returns driver-specific connection options
+ *
+ * @param array $dsn DSN parameters
+ *
+ * @return array Connection options
+ */
+ protected function dsn_options($dsn)
+ {
+ $result = array();
+
+ if (!empty($dsn['key'])) {
+ $result[PDO::MYSQL_ATTR_SSL_KEY] = $dsn['key'];
+ }
+
+ if (!empty($dsn['cipher'])) {
+ $result[PDO::MYSQL_ATTR_SSL_CIPHER] = $dsn['cipher'];
+ }
+
+ if (!empty($dsn['cert'])) {
+ $result[PDO::MYSQL_ATTR_SSL_CERT] = $dsn['cert'];
+ }
+
+ if (!empty($dsn['capath'])) {
+ $result[PDO::MYSQL_ATTR_SSL_CAPATH] = $dsn['capath'];
+ }
+
+ if (!empty($dsn['ca'])) {
+ $result[PDO::MYSQL_ATTR_SSL_CA] = $dsn['ca'];
+ }
+
+ // Always return matching (not affected only) rows count
+ $result[PDO::MYSQL_ATTR_FOUND_ROWS] = true;
+
+ // Enable AUTOCOMMIT mode
+ $result[PDO::ATTR_AUTOCOMMIT] = true;
+
+ return $result;
+ }
+}