Skip to content

Commit 27d43da

Browse files
committed
Merge pull request #80 from AlexeyDsov/sqlitePDO
SQLitePDO
2 parents 80b82d6 + 77aef96 commit 27d43da

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

core/DB/SQLitePDO.class.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
/***************************************************************************
3+
* Copyright (C) 2012 by Aleksey S. Denisov *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify *
6+
* it under the terms of the GNU Lesser General Public License as *
7+
* published by the Free Software Foundation; either version 3 of the *
8+
* License, or (at your option) any later version. *
9+
* *
10+
***************************************************************************/
11+
12+
/**
13+
* SQLitePDO DB connector.
14+
*
15+
* @see http://www.sqlite.org/
16+
* @see http://www.php.net/manual/en/ref.pdo-sqlite.php
17+
*
18+
* @ingroup DB
19+
**/
20+
final class SQLitePDO extends Sequenceless
21+
{
22+
23+
const ERROR_CONSTRAINT = 19;
24+
/**
25+
* @var PDO
26+
*/
27+
protected $link = null;
28+
29+
/**
30+
* @return LiteDialect
31+
**/
32+
public static function getDialect()
33+
{
34+
return LiteDialect::me();
35+
}
36+
37+
/**
38+
* @return SQLitePDO
39+
**/
40+
public function connect()
41+
{
42+
try {
43+
$this->link = new PDO(
44+
"sqlite:{$this->basename}",
45+
'',
46+
'',
47+
array(PDO::ATTR_PERSISTENT => $this->persistent)
48+
);
49+
} catch (PDOException $e) {
50+
throw new DatabaseException(
51+
'can not open SQLitePDO base: '
52+
.$e->getMessage()
53+
);
54+
}
55+
56+
return $this;
57+
}
58+
59+
/**
60+
* @return SQLitePDO
61+
**/
62+
public function disconnect()
63+
{
64+
if ($this->link) {
65+
$this->link = null;
66+
}
67+
68+
return $this;
69+
}
70+
71+
public function isConnected()
72+
{
73+
return $this->link !== null;
74+
}
75+
76+
/**
77+
* misc
78+
**/
79+
public function setDbEncoding()
80+
{
81+
throw new UnsupportedMethodException();
82+
}
83+
84+
/**
85+
* query methods
86+
**/
87+
public function queryRaw($queryString)
88+
{
89+
try {
90+
$res = $this->link->query($queryString);
91+
return $res;
92+
} catch (PDOException $e) {
93+
$code = $e->getCode();
94+
95+
if ($code == self::ERROR_CONSTRAINT)
96+
$e = 'DuplicateObjectException';
97+
else
98+
$e = 'DatabaseException';
99+
100+
throw new $e(
101+
$e->getMessage(),
102+
$code
103+
);
104+
}
105+
}
106+
107+
/**
108+
* Same as query, but returns number of affected rows
109+
* Returns number of affected rows in insert/update queries
110+
**/
111+
public function queryCount(Query $query)
112+
{
113+
$res = $this->queryNull($query);
114+
/* @var $res PDOStatement */
115+
116+
return $res->rowCount();
117+
}
118+
119+
public function queryRow(Query $query)
120+
{
121+
$res = $this->query($query);
122+
/* @var $res PDOStatement */
123+
124+
$array = $res->fetchAll(PDO::FETCH_ASSOC);
125+
if (count($array) > 1)
126+
throw new TooManyRowsException(
127+
'query returned too many rows (we need only one)'
128+
);
129+
elseif (count($array) == 1)
130+
return reset($array);
131+
else
132+
return null;
133+
}
134+
135+
public function queryColumn(Query $query)
136+
{
137+
$res = $this->query($query);
138+
/* @var $res PDOStatement */
139+
140+
$resArray = $res->fetchAll(PDO::FETCH_ASSOC);
141+
if ($resArray) {
142+
$array = array();
143+
foreach ($resArray as $row) {
144+
$array[] = reset($row);
145+
}
146+
147+
return $array;
148+
} else
149+
return null;
150+
}
151+
152+
public function querySet(Query $query)
153+
{
154+
$res = $this->query($query);
155+
/* @var $res PDOStatement */
156+
157+
return $res->fetchAll(PDO::FETCH_ASSOC) ?: null;
158+
}
159+
160+
public function hasQueue()
161+
{
162+
return false;
163+
}
164+
165+
public function getTableInfo($table)
166+
{
167+
throw new UnimplementedFeatureException();
168+
}
169+
170+
protected function getInsertId()
171+
{
172+
return $this->link->lastInsertId();
173+
}
174+
}
175+
?>

0 commit comments

Comments
 (0)