-
Notifications
You must be signed in to change notification settings - Fork 9
/
adapter_pdosqlite.php
234 lines (201 loc) · 6.34 KB
/
adapter_pdosqlite.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* Implements functions for using PDO driver of PHP for sqlite
*
* Only sqlite3 is (good) supported.
* Therefore an upgrade function is supplied, when you have also the
* Sqlite Extension running.
*/
class helper_plugin_sqlite_adapter_pdosqlite extends helper_plugin_sqlite_adapter {
protected $fileextension = '.sqlite3';
/** @var $db PDO */
protected $db;
/**
* return name of adapter
*/
public function getName() {
return DOKU_EXT_PDO;
}
/**
* Registers a User Defined Function for use in SQL statements
*/
public function create_function($function_name, $callback, $num_args) {
$this->db->sqliteCreateFunction($function_name, $callback, $num_args);
}
/**
* open db
*/
public function opendb($init, $sqliteupgrade = false) {
if($init) {
$oldDbfile = substr($this->dbfile, 0, -1);
if(@file_exists($oldDbfile)) {
$notfound_msg = "SQLite: '".$this->dbname.$this->fileextension."' database not found. In the meta directory is '".$this->dbname.substr($this->fileextension, 0, -1)."' available. ";
global $ID;
if($this->isSqlite3db($oldDbfile)) {
msg($notfound_msg."PDO sqlite needs a rename of the file extension to '.sqlite3'. For admins more info via Admin > <a href=\"".wl($ID, array('do'=> 'admin', 'page'=> 'sqlite'))."\">Sqlite Interface</a>.", -1);
return false;
} else {
//don't block connecting db, when upgrading
if(!$sqliteupgrade) {
msg($notfound_msg."PDO sqlite needs a upgrade of this sqlite2 db to sqlite3 format. For admins more info via Admin > <a href=\"".wl($ID, array('do'=> 'admin', 'page'=> 'sqlite'))."\">Sqlite Interface</a>.", -1);
return false;
}
}
}
} else {
if(!$this->isSqlite3db($this->dbfile)) {
msg("SQLite: failed to open SQLite '".$this->dbname."' database (DB has not a sqlite3 format.)", -1);
return false;
}
}
$dsn = 'sqlite:'.$this->dbfile;
try {
$this->db = new PDO($dsn);
} catch(PDOException $e) {
msg("SQLite: failed to open SQLite '".$this->dbname."' database (".$e->getMessage().")", -1);
return false;
}
$this->db->sqliteCreateAggregate(
'group_concat',
array($this, '_pdo_group_concat_step'),
array($this, '_pdo_group_concat_finalize')
);
return true;
}
/**
* close current db
*/
public function closedb() {
$this->db->close();
}
/**
* Execute a query
*
* @param string $sql query
* @return bool|PDOStatement
*/
public function executeQuery($sql) {
$this->data = null;
$res = $this->db->query($sql);
if(!$res) {
$err = $this->db->errorInfo();
msg($err[0].' '.$err[1].' '.$err[2].':<br /><pre>'.hsc($sql).'</pre>', -1);
return false;
}
return $res;
}
/**
* Close the result set and it's cursors
*
* @param PDOStatement $res
* @return bool
*/
public function res_close($res) {
return $res->closeCursor();
}
/**
* Returns a complete result set as array
*/
public function res2arr($res, $assoc = true) {
if(!$res) return array();
if(!$this->data) {
$mode = $assoc ? PDO::FETCH_ASSOC : PDO::FETCH_NUM;
$this->data = $res->fetchAll($mode);
}
return $this->data;
}
/**
* Return the next row of the given result set as associative array
*/
public function res2row($res) {
if(!$res) return false;
return $res->fetch(PDO::FETCH_ASSOC);
}
/**
* Return the first value from the next row.
*/
public function res2single($res) {
if(!$res) return false;
$data = $res->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_ABS, 0);
if(!count($data)) {
return false;
}
return $data[0];
}
/**
* Run sqlite_escape_string() on the given string and surround it
* with quotes
*/
public function quote_string($string) {
return $this->db->quote($string);
}
/**
* Escape string for sql
*/
public function escape_string($str) {
return trim($this->db->quote($str), "'");
}
/**
* Aggregation function for SQLite via PDO
*
* @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine
*/
public function _pdo_group_concat_step(&$context, $rownumber, $string, $separator = ',') {
if(is_null($context)) {
$context = array(
'sep' => $separator,
'data' => array()
);
}
$context['data'][] = $string;
return $context;
}
/**
* Aggregation function for SQLite via PDO
*
* @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine
*/
public function _pdo_group_concat_finalize(&$context, $rownumber) {
if(!is_array($context)) {
return null;
}
$context['data'] = array_unique($context['data']);
return join($context['sep'], $context['data']);
}
/**
* fetch the next row as zero indexed array
*/
public function res_fetch_array($res) {
if(!$res) return false;
return $res->fetch(PDO::FETCH_NUM);
}
/**
* fetch the next row as assocative array
*/
public function res_fetch_assoc($res) {
if(!$res) return false;
return $res->fetch(PDO::FETCH_ASSOC);
}
/**
* Count the number of records in result
*
* This function is really inperformant in PDO and should be avoided!
*/
public function res2count($res) {
if(!$res) return 0;
if(!$this->data) {
$this->data = $this->res2arr($res);
}
return count($this->data);
}
/**
* Count the number of records changed last time
*
* Don't work after a SELECT statement
*/
public function countChanges($res) {
if(!$res) return 0;
return $res->rowCount();
}
}
// vim:ts=4:sw=4:et:enc=utf-8: