Skip to content

Commit 3e23e12

Browse files
committed
added SQLitePDO DB connector
1 parent 8a8ce20 commit 3e23e12

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed

core/DB/SQLitePDO.class.php

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
* @var PDO
24+
*/
25+
protected $link = null;
26+
27+
/**
28+
* @return LiteDialect
29+
**/
30+
public static function getDialect()
31+
{
32+
return LiteDialect::me();
33+
}
34+
35+
/**
36+
* @return SQLitePDO
37+
**/
38+
public function connect()
39+
{
40+
try {
41+
$this->link = new PDO(
42+
"sqlite:{$this->basename}",
43+
'',
44+
'',
45+
array(PDO::ATTR_PERSISTENT => $this->persistent)
46+
);
47+
} catch (PDOException $e) {
48+
throw new DatabaseException(
49+
'can not open SQLitePDO base: '
50+
.$e->getMessage()
51+
);
52+
}
53+
54+
return $this;
55+
}
56+
57+
/**
58+
* @return SQLitePDO
59+
**/
60+
public function disconnect()
61+
{
62+
if ($this->link) {
63+
$this->link = null;
64+
}
65+
66+
return $this;
67+
}
68+
69+
public function isConnected()
70+
{
71+
return $this->link !== null;
72+
}
73+
74+
/**
75+
* misc
76+
**/
77+
public function setDbEncoding()
78+
{
79+
throw new UnsupportedMethodException();
80+
}
81+
82+
/**
83+
* query methods
84+
**/
85+
public function queryRaw($queryString)
86+
{
87+
try {
88+
$res = $this->link->query($queryString);
89+
return $res;
90+
} catch (PDOException $e) {
91+
$code = $e->getCode();
92+
93+
if ($code == 19)
94+
$e = 'DuplicateObjectException';
95+
else
96+
$e = 'DatabaseException';
97+
98+
throw new $e(
99+
$e->getMessage(),
100+
$code
101+
);
102+
}
103+
}
104+
105+
/**
106+
* Same as query, but returns number of affected rows
107+
* Returns number of affected rows in insert/update queries
108+
**/
109+
public function queryCount(Query $query)
110+
{
111+
$res = $this->queryNull($query);
112+
/* @var $res PDOStatement */
113+
114+
return $res->rowCount();
115+
}
116+
117+
public function queryRow(Query $query)
118+
{
119+
$res = $this->query($query);
120+
/* @var $res PDOStatement */
121+
122+
$array = $res->fetchAll(PDO::FETCH_COLUMN);
123+
if (count($array) > 1)
124+
throw new TooManyRowsException(
125+
'query returned too many rows (we need only one)'
126+
);
127+
elseif (count($array) == 1)
128+
return reset($array);
129+
else
130+
return null;
131+
}
132+
133+
public function queryColumn(Query $query)
134+
{
135+
$res = $this->query($query);
136+
/* @var $res PDOStatement */
137+
138+
if ($res->rowCount()) {
139+
$array = array();
140+
foreach ($res->fetchAll(PDO::FETCH_COLUMN) as $row) {
141+
$array[] = reset($row);
142+
}
143+
144+
return $array;
145+
} else
146+
return null;
147+
}
148+
149+
public function querySet(Query $query)
150+
{
151+
$res = $this->query($query);
152+
/* @var $res PDOStatement */
153+
154+
return $res->fetchAll(PDO::FETCH_ASSOC) ?: null;
155+
}
156+
157+
public function hasQueue()
158+
{
159+
return false;
160+
}
161+
162+
public function getTableInfo($table)
163+
{
164+
throw new UnimplementedFeatureException();
165+
}
166+
167+
protected function getInsertId()
168+
{
169+
return $this->link->lastInsertId();
170+
}
171+
}
172+
?>

0 commit comments

Comments
 (0)