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
235
236
237
238
239
240
241
242
243
244
245
|
<?php # $Id: sqlite.inc.php 565 2005-10-17 13:04:11Z garvinhicking $ # Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team) # All rights reserved. See LICENSE file for licensing details
if (!function_exists('sqlite_open')) { @dl('sqlite.so'); @dl('sqlite.dll'); }
function serendipity_db_begin_transaction(){ serendipity_db_query('begin transaction'); }
function serendipity_db_end_transaction($commit){ if ($commit){ serendipity_db_query('commit transaction'); }else{ serendipity_db_query('rollback transaction'); } }
function serendipity_db_connect() { global $serendipity;
if (isset($serendipity['dbConn'])) { return $serendipity['dbConn']; }
if (isset($serendipity['dbPersistent']) && $serendipity['dbPersistent']) { $function = 'sqlite_popen'; } else { $function = 'sqlite_open'; }
$serendipity['dbConn'] = $function($serendipity['serendipityPath'] . $serendipity['dbName'] . '.db');
return $serendipity['dbConn']; }
function serendipity_db_escape_string($string) { static $search = array("\x00", '%', "'", '\"'); static $replace = array('%00', '%25', "''", '\\\"');
return str_replace($search, $replace, $string); }
function serendipity_db_affected_rows() { global $serendipity;
return sqlite_changes($serendipity['dbConn']); }
function serendipity_db_updated_rows() { global $serendipity; // It is unknown whether sqllite returns rows MATCHED or rows UPDATED return sqlite_changes($serendipity['dbConn']); }
function serendipity_db_matched_rows() { global $serendipity; // It is unknown whether sqllite returns rows MATCHED or rows UPDATED return sqlite_changes($serendipity['dbConn']); }
function serendipity_db_insert_id() { global $serendipity;
return sqlite_last_insert_rowid($serendipity['dbConn']); }
function serendipity_db_sqlite_fetch_array($res, $type = SQLITE_BOTH) { static $search = array('%00', '%25'); static $replace = array("\x00", '%');
$row = sqlite_fetch_array($res, $type); if (!is_array($row)) { return $row; }
/* strip any slashes, correct fieldname */ foreach ($row as $i => $v) { // TODO: If a query of the format 'SELECT a.id, b.text FROM table' is used, // the sqlite extension will give us key indizes 'a.id' and 'b.text' // instead of just 'id' and 'text' like in mysql/postgresql extension. // To fix that, we use a preg-regex; but that is quite performance costy. // Either we always need to use 'SELECT a.id AS id, b.text AS text' in query, // or the sqlite extension may get fixed. :-) $row[preg_replace('@^.+\.(.*)@', '\1', $i)] = str_replace($search, $replace, $v); }
return $row; }
function serendipity_db_in_sql($col, &$search_ids, $type = ' OR ') { $sql = array(); if (!is_array($search_ids)) { return false; } foreach($search_ids AS $id) { $sql[] = $col . ' = ' . $id; } $cond = '(' . implode($type, $sql) . ')'; return $cond; }
function &serendipity_db_query($sql, $single = false, $result_type = "both", $reportErr = true, $assocKey = false, $assocVal = false, $expectError = false) { global $serendipity; static $type_map = array( 'assoc' => SQLITE_ASSOC, 'num' => SQLITE_NUM, 'both' => SQLITE_BOTH, 'true' => true, 'false' => false );
static $debug = false;
if ($debug) { // Open file and write directly. In case of crashes, the pointer needs to be killed. $fp = @fopen('sqlite.log', 'a'); fwrite($fp, '[' . date('d.m.Y H:i') . '] SQLITE QUERY: ' . $sql . "\n\n"); fclose($fp); } if ($reportErr && !$expectError) { $res = sqlite_query($sql, $serendipity['dbConn']); } else { $res = @sqlite_query($sql, $serendipity['dbConn']); }
if (!$res) { if (!$expectError && !$serendipity['production']) { var_dump($res); var_dump($sql); $msg = "problem with query"; return $msg; } if ($debug) { $fp = @fopen('sqlite.log', 'a'); fwrite($fp, '[' . date('d.m.Y H:i') . '] [ERROR] ' . "\n\n"); fclose($fp); }
return $type_map['false']; }
if ($res === true) { return $type_map['true']; }
if (sqlite_num_rows($res) == 0) { if ($single) { return $type_map['false']; } return $type_map['true']; } else { $rows = array();
while (($row = serendipity_db_sqlite_fetch_array($res, $type_map[$result_type]))) { if (!empty($assocKey) && !empty($assocVal)) { // You can fetch a key-associated array via the two function parameters assocKey and assocVal $rows[$row[$assocKey]] = $row[$assocVal]; } else { $rows[] = $row; } }
if ($debug) { $fp = @fopen('sqlite.log', 'a'); fwrite($fp, '[' . date('d.m.Y H:i') . '] SQLITE RESULT: ' . print_r($rows, true). "\n\n"); fclose($fp); }
if ($single && count($rows) == 1) { return $rows[0]; }
return $rows; } }
function serendipity_db_probe($hash, &$errs) { global $serendipity;
$dbName = (isset($hash['sqlitedbName']) ? $hash['sqlitedbName'] : $hash['dbName']);
if (!function_exists('sqlite_open')) { $errs[] = 'SQLite extension not installed. Run "pear install sqlite" on your webserver or contact your systems administrator regarding this problem.'; return false; }
$serendipity['dbConn'] = sqlite_open($serendipity['serendipityPath'] . $dbName . '.db');
if ($serendipity['dbConn']) { return true; }
$errs[] = "Unable to open \"{$serendipity['serendipityPath']}$dbName.db\" - check permissions (directory needs to be writeable for webserver)!"; return false; }
function serendipity_db_schema_import($query) { static $search = array('{AUTOINCREMENT}', '{PRIMARY}', '{UNSIGNED}', '{FULLTEXT}', '{BOOLEAN}', '{UTF_8}'); static $replace = array('INTEGER', 'PRIMARY KEY', '', '', 'BOOLEAN NOT NULL', '');
if (stristr($query, '{FULLTEXT_MYSQL}')) { return true; }
$query = trim(str_replace($search, $replace, $query)); if ($query{0} == '@') { // Errors are expected to happen (like duplicate index creation) return serendipity_db_query(substr($query, 1), false, 'both', false, false, false, true); } else { return serendipity_db_query($query); } }
function serendipity_db_limit($start, $offset) { return $start . ', ' . $offset; }
function serendipity_db_limit_sql($limitstring) { return ' LIMIT ' . $limitstring; }
function serendipity_db_concat($string) { return 'concat(' . $string . ')'; }
/* vim: set sts=4 ts=4 expandtab : */ ?>
|