diff options
author | Bart Visscher <bartv@thisnet.nl> | 2013-02-25 08:19:49 +0100 |
---|---|---|
committer | Bart Visscher <bartv@thisnet.nl> | 2013-07-21 23:17:36 +0200 |
commit | 66a215651badf8472e9922b7591cba3cfe9ce333 (patch) | |
tree | b097c9ec7f586e6632f429b59e8c1dd6e9f2a583 /lib | |
parent | 62ce3a5613a273c9208f272728ae5002d43d5096 (diff) | |
download | nextcloud-server-66a215651badf8472e9922b7591cba3cfe9ce333.tar.gz nextcloud-server-66a215651badf8472e9922b7591cba3cfe9ce333.zip |
Create DB Connection wrapper and DB\Adapter* classes
Diffstat (limited to 'lib')
-rw-r--r-- | lib/db.php | 6 | ||||
-rw-r--r-- | lib/db/adapter.php | 17 | ||||
-rw-r--r-- | lib/db/adapteroci8.php | 13 | ||||
-rw-r--r-- | lib/db/adapterpgsql.php | 13 | ||||
-rw-r--r-- | lib/db/adaptersqlite.php | 13 | ||||
-rw-r--r-- | lib/db/adaptersqlsrv.php | 13 | ||||
-rw-r--r-- | lib/db/connection.php | 88 |
7 files changed, 163 insertions, 0 deletions
diff --git a/lib/db.php b/lib/db.php index 44d77bce80e..95145fb7c50 100644 --- a/lib/db.php +++ b/lib/db.php @@ -129,6 +129,7 @@ class OC_DB { 'path' => $datadir.'/'.$name.'.db', 'driver' => 'pdo_sqlite', ); + $connectionParams['adapter'] = '\OC\DB\AdapterSqlite'; break; case 'mysql': $connectionParams = array( @@ -140,6 +141,7 @@ class OC_DB { 'charset' => 'UTF8', 'driver' => 'pdo_mysql', ); + $connectionParams['adapter'] = '\OC\DB\Adapter'; break; case 'pgsql': $connectionParams = array( @@ -150,6 +152,7 @@ class OC_DB { 'dbname' => $name, 'driver' => 'pdo_pgsql', ); + $connectionParams['adapter'] = '\OC\DB\AdapterPgSql'; break; case 'oci': $connectionParams = array( @@ -163,6 +166,7 @@ class OC_DB { if (!empty($port)) { $connectionParams['port'] = $port; } + $connectionParams['adapter'] = '\OC\DB\AdapterOCI8'; break; case 'mssql': $connectionParams = array( @@ -174,10 +178,12 @@ class OC_DB { 'charset' => 'UTF8', 'driver' => 'pdo_sqlsrv', ); + $connectionParams['adapter'] = '\OC\DB\AdapterSQLSrv'; break; default: return false; } + $connectionParams['wrapperClass'] = 'OC\DB\Connection'; try { self::$DOCTRINE = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); } catch(\Doctrine\DBAL\DBALException $e) { diff --git a/lib/db/adapter.php b/lib/db/adapter.php new file mode 100644 index 00000000000..c62e35495cd --- /dev/null +++ b/lib/db/adapter.php @@ -0,0 +1,17 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\DB; + +class Adapter { + protected $conn; + + public function __construct($conn) { + $this->conn = $conn; + } +} diff --git a/lib/db/adapteroci8.php b/lib/db/adapteroci8.php new file mode 100644 index 00000000000..7ffff4909e7 --- /dev/null +++ b/lib/db/adapteroci8.php @@ -0,0 +1,13 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + + +namespace OC\DB; + +class AdapterOCI8 extends Adapter { +} diff --git a/lib/db/adapterpgsql.php b/lib/db/adapterpgsql.php new file mode 100644 index 00000000000..d3b09357c0a --- /dev/null +++ b/lib/db/adapterpgsql.php @@ -0,0 +1,13 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + + +namespace OC\DB; + +class AdapterPgSql extends Adapter { +} diff --git a/lib/db/adaptersqlite.php b/lib/db/adaptersqlite.php new file mode 100644 index 00000000000..0b8107ac53e --- /dev/null +++ b/lib/db/adaptersqlite.php @@ -0,0 +1,13 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + + +namespace OC\DB; + +class AdapterSqlite extends Adapter { +} diff --git a/lib/db/adaptersqlsrv.php b/lib/db/adaptersqlsrv.php new file mode 100644 index 00000000000..281daf81d17 --- /dev/null +++ b/lib/db/adaptersqlsrv.php @@ -0,0 +1,13 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + + +namespace OC\DB; + +class AdapterSQLSrv extends Adapter { +} diff --git a/lib/db/connection.php b/lib/db/connection.php new file mode 100644 index 00000000000..5466609e671 --- /dev/null +++ b/lib/db/connection.php @@ -0,0 +1,88 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\DB; +use Doctrine\DBAL\Driver; +use Doctrine\DBAL\Configuration; +use Doctrine\DBAL\Cache\QueryCacheProfile; +use Doctrine\Common\EventManager; + +class Connection extends \Doctrine\DBAL\Connection { + protected $table_prefix; + protected $sequence_suffix; + + protected $adapter; + + /** + * Initializes a new instance of the Connection class. + * + * @param array $params The connection parameters. + * @param Driver $driver + * @param Configuration $config + * @param EventManager $eventManager + */ + public function __construct(array $params, Driver $driver, Configuration $config = null, + EventManager $eventManager = null) + { + if (!isset($params['adapter'])) { + throw new Exception('adapter not set'); + } + parent::__construct($params, $driver, $config, $eventManager); + $this->adapter = new $params['adapter']($this); + } + + /** + * Prepares an SQL statement. + * + * @param string $statement The SQL statement to prepare. + * @return \Doctrine\DBAL\Driver\Statement The prepared statement. + */ + public function prepare( $statement, $limit=null, $offset=null ) { + // TODO: prefix + // TODO: limit & offset + // TODO: prepared statement cache + return parent::prepare($statement); + } + + /** + * Executes an, optionally parameterized, SQL query. + * + * If the query is parameterized, a prepared statement is used. + * If an SQLLogger is configured, the execution is logged. + * + * @param string $query The SQL query to execute. + * @param array $params The parameters to bind to the query, if any. + * @param array $types The types the previous parameters are in. + * @param QueryCacheProfile $qcp + * @return \Doctrine\DBAL\Driver\Statement The executed statement. + * @internal PERF: Directly prepares a driver statement, not a wrapper. + */ + public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) + { + // TODO: prefix + return parent::executeQuery($query, $params, $types, $qcp); + } + + /** + * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters + * and returns the number of affected rows. + * + * This method supports PDO binding types as well as DBAL mapping types. + * + * @param string $query The SQL query. + * @param array $params The query parameters. + * @param array $types The parameter types. + * @return integer The number of affected rows. + * @internal PERF: Directly prepares a driver statement, not a wrapper. + */ + public function executeUpdate($query, array $params = array(), array $types = array()) + { + // TODO: prefix + return parent::executeUpdate($query, $params, $types); + } +} |