From 3e23e129a27c65e378580eaf5f8e116b5047831a Mon Sep 17 00:00:00 2001 From: AlexeyDsov Date: Fri, 16 Mar 2012 02:17:29 +0400 Subject: [PATCH 1/3] added SQLitePDO DB connector --- core/DB/SQLitePDO.class.php | 172 ++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 core/DB/SQLitePDO.class.php diff --git a/core/DB/SQLitePDO.class.php b/core/DB/SQLitePDO.class.php new file mode 100644 index 0000000000..934db996e5 --- /dev/null +++ b/core/DB/SQLitePDO.class.php @@ -0,0 +1,172 @@ +link = new PDO( + "sqlite:{$this->basename}", + '', + '', + array(PDO::ATTR_PERSISTENT => $this->persistent) + ); + } catch (PDOException $e) { + throw new DatabaseException( + 'can not open SQLitePDO base: ' + .$e->getMessage() + ); + } + + return $this; + } + + /** + * @return SQLitePDO + **/ + public function disconnect() + { + if ($this->link) { + $this->link = null; + } + + return $this; + } + + public function isConnected() + { + return $this->link !== null; + } + + /** + * misc + **/ + public function setDbEncoding() + { + throw new UnsupportedMethodException(); + } + + /** + * query methods + **/ + public function queryRaw($queryString) + { + try { + $res = $this->link->query($queryString); + return $res; + } catch (PDOException $e) { + $code = $e->getCode(); + + if ($code == 19) + $e = 'DuplicateObjectException'; + else + $e = 'DatabaseException'; + + throw new $e( + $e->getMessage(), + $code + ); + } + } + + /** + * Same as query, but returns number of affected rows + * Returns number of affected rows in insert/update queries + **/ + public function queryCount(Query $query) + { + $res = $this->queryNull($query); + /* @var $res PDOStatement */ + + return $res->rowCount(); + } + + public function queryRow(Query $query) + { + $res = $this->query($query); + /* @var $res PDOStatement */ + + $array = $res->fetchAll(PDO::FETCH_COLUMN); + if (count($array) > 1) + throw new TooManyRowsException( + 'query returned too many rows (we need only one)' + ); + elseif (count($array) == 1) + return reset($array); + else + return null; + } + + public function queryColumn(Query $query) + { + $res = $this->query($query); + /* @var $res PDOStatement */ + + if ($res->rowCount()) { + $array = array(); + foreach ($res->fetchAll(PDO::FETCH_COLUMN) as $row) { + $array[] = reset($row); + } + + return $array; + } else + return null; + } + + public function querySet(Query $query) + { + $res = $this->query($query); + /* @var $res PDOStatement */ + + return $res->fetchAll(PDO::FETCH_ASSOC) ?: null; + } + + public function hasQueue() + { + return false; + } + + public function getTableInfo($table) + { + throw new UnimplementedFeatureException(); + } + + protected function getInsertId() + { + return $this->link->lastInsertId(); + } + } +?> \ No newline at end of file From a134c8057a4750a329d2b90ad2aba7f61a2ade43 Mon Sep 17 00:00:00 2001 From: AlexeyDsov Date: Sat, 17 Mar 2012 10:44:21 +0400 Subject: [PATCH 2/3] bugfix SQLitePDO --- core/DB/SQLitePDO.class.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core/DB/SQLitePDO.class.php b/core/DB/SQLitePDO.class.php index 934db996e5..c0efc6bf76 100644 --- a/core/DB/SQLitePDO.class.php +++ b/core/DB/SQLitePDO.class.php @@ -119,7 +119,7 @@ public function queryRow(Query $query) $res = $this->query($query); /* @var $res PDOStatement */ - $array = $res->fetchAll(PDO::FETCH_COLUMN); + $array = $res->fetchAll(PDO::FETCH_ASSOC); if (count($array) > 1) throw new TooManyRowsException( 'query returned too many rows (we need only one)' @@ -135,9 +135,10 @@ public function queryColumn(Query $query) $res = $this->query($query); /* @var $res PDOStatement */ - if ($res->rowCount()) { + $resArray = $res->fetchAll(PDO::FETCH_ASSOC); + if ($resArray) { $array = array(); - foreach ($res->fetchAll(PDO::FETCH_COLUMN) as $row) { + foreach ($resArray as $row) { $array[] = reset($row); } From 77aef96db6b43503d2fe8768bd758a3778ce58fa Mon Sep 17 00:00:00 2001 From: AlexeyDsov Date: Sat, 17 Mar 2012 13:37:39 +0400 Subject: [PATCH 3/3] SQLitePDO update --- core/DB/SQLitePDO.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/DB/SQLitePDO.class.php b/core/DB/SQLitePDO.class.php index c0efc6bf76..3f62865a27 100644 --- a/core/DB/SQLitePDO.class.php +++ b/core/DB/SQLitePDO.class.php @@ -19,6 +19,8 @@ **/ final class SQLitePDO extends Sequenceless { + + const ERROR_CONSTRAINT = 19; /** * @var PDO */ @@ -90,7 +92,7 @@ public function queryRaw($queryString) } catch (PDOException $e) { $code = $e->getCode(); - if ($code == 19) + if ($code == self::ERROR_CONSTRAINT) $e = 'DuplicateObjectException'; else $e = 'DatabaseException';