Index: trunk/phase3/includes/db/DatabaseSqlite.php |
— | — | @@ -20,7 +20,7 @@ |
21 | 21 | /** |
22 | 22 | * Constructor |
23 | 23 | */ |
24 | | - function __construct($server = false, $user = false, $password = false, $dbName = false, $failFunction = false, $flags = 0) { |
| 24 | + function __construct( $server = false, $user = false, $password = false, $dbName = false, $failFunction = false, $flags = 0 ) { |
25 | 25 | global $wgSQLiteDataDir; |
26 | 26 | $this->mFailFunction = $failFunction; |
27 | 27 | $this->mFlags = $flags; |
— | — | @@ -28,7 +28,7 @@ |
29 | 29 | if( !is_readable( $this->mDatabaseFile ) ) |
30 | 30 | throw new DBConnectionError( $this, "SQLite database not accessible" ); |
31 | 31 | $this->mName = $dbName; |
32 | | - $this->open($server, $user, $password, $dbName); |
| 32 | + $this->open( $server, $user, $password, $dbName ); |
33 | 33 | } |
34 | 34 | |
35 | 35 | /** |
— | — | @@ -37,20 +37,20 @@ |
38 | 38 | function implicitGroupby() { return false; } |
39 | 39 | function implicitOrderby() { return false; } |
40 | 40 | |
41 | | - static function newFromParams($server, $user, $password, $dbName, $failFunction = false, $flags = 0) { |
42 | | - return new DatabaseSqlite($server, $user, $password, $dbName, $failFunction, $flags); |
| 41 | + static function newFromParams( $server, $user, $password, $dbName, $failFunction = false, $flags = 0 ) { |
| 42 | + return new DatabaseSqlite( $server, $user, $password, $dbName, $failFunction, $flags ); |
43 | 43 | } |
44 | 44 | |
45 | 45 | /** Open an SQLite database and return a resource handle to it |
46 | 46 | * NOTE: only $dbName is used, the other parameters are irrelevant for SQLite databases |
47 | 47 | */ |
48 | | - function open($server,$user,$pass,$dbName) { |
| 48 | + function open( $server, $user, $pass, $dbName ) { |
49 | 49 | $this->mConn = false; |
50 | | - if ($dbName) { |
| 50 | + if ( $dbName ) { |
51 | 51 | $file = $this->mDatabaseFile; |
52 | 52 | try { |
53 | 53 | if ( $this->mFlags & DBO_PERSISTENT ) { |
54 | | - $this->mConn = new PDO( "sqlite:$file", $user, $pass, |
| 54 | + $this->mConn = new PDO( "sqlite:$file", $user, $pass, |
55 | 55 | array( PDO::ATTR_PERSISTENT => true ) ); |
56 | 56 | } else { |
57 | 57 | $this->mConn = new PDO( "sqlite:$file", $user, $pass ); |
— | — | @@ -69,7 +69,7 @@ |
70 | 70 | } |
71 | 71 | $this->mOpened = $this->mConn; |
72 | 72 | # set error codes only, don't raise exceptions |
73 | | - $this->mConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT ); |
| 73 | + $this->mConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT ); |
74 | 74 | } |
75 | 75 | return $this->mConn; |
76 | 76 | } |
— | — | @@ -79,8 +79,8 @@ |
80 | 80 | */ |
81 | 81 | function close() { |
82 | 82 | $this->mOpened = false; |
83 | | - if (is_object($this->mConn)) { |
84 | | - if ($this->trxLevel()) $this->immediateCommit(); |
| 83 | + if ( is_object( $this->mConn ) ) { |
| 84 | + if ( $this->trxLevel() ) $this->immediateCommit(); |
85 | 85 | $this->mConn = null; |
86 | 86 | } |
87 | 87 | return true; |
— | — | @@ -89,36 +89,50 @@ |
90 | 90 | /** |
91 | 91 | * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result |
92 | 92 | */ |
93 | | - function doQuery($sql) { |
94 | | - $res = $this->mConn->query($sql); |
95 | | - if ($res === false) { |
| 93 | + function doQuery( $sql ) { |
| 94 | + $res = $this->mConn->query( $sql ); |
| 95 | + if ( $res === false ) { |
96 | 96 | return false; |
97 | 97 | } else { |
98 | 98 | $r = $res instanceof ResultWrapper ? $res->result : $res; |
99 | 99 | $this->mAffectedRows = $r->rowCount(); |
100 | | - $res = new ResultWrapper($this,$r->fetchAll()); |
| 100 | + $res = new ResultWrapper( $this, $r->fetchAll() ); |
101 | 101 | } |
102 | 102 | return $res; |
103 | 103 | } |
104 | 104 | |
105 | | - function freeResult($res) { |
106 | | - if ($res instanceof ResultWrapper) $res->result = NULL; else $res = NULL; |
| 105 | + function freeResult( $res ) { |
| 106 | + if ( $res instanceof ResultWrapper ) |
| 107 | + $res->result = NULL; |
| 108 | + else |
| 109 | + $res = NULL; |
107 | 110 | } |
108 | 111 | |
109 | 112 | function fetchObject($res) { |
110 | | - if ($res instanceof ResultWrapper) $r =& $res->result; else $r =& $res; |
111 | | - $cur = current($r); |
112 | | - if (is_array($cur)) { |
113 | | - next($r); |
| 113 | + if ($res instanceof ResultWrapper) |
| 114 | + $r =& $res->result; |
| 115 | + else |
| 116 | + $r =& $res; |
| 117 | + |
| 118 | + $cur = current( $r ); |
| 119 | + if ( is_array( $cur ) ) { |
| 120 | + next( $r ); |
114 | 121 | $obj = new stdClass; |
115 | | - foreach ($cur as $k => $v) if (!is_numeric($k)) $obj->$k = $v; |
| 122 | + foreach ( $cur as $k => $v ) |
| 123 | + if ( !is_numeric( $k ) ) |
| 124 | + $obj->$k = $v; |
| 125 | + |
116 | 126 | return $obj; |
117 | 127 | } |
118 | 128 | return false; |
119 | 129 | } |
120 | 130 | |
121 | 131 | function fetchRow($res) { |
122 | | - if ($res instanceof ResultWrapper) $r =& $res->result; else $r =& $res; |
| 132 | + if ( $res instanceof ResultWrapper ) |
| 133 | + $r =& $res->result; |
| 134 | + else |
| 135 | + $r =& $res; |
| 136 | + |
123 | 137 | $cur = current($r); |
124 | 138 | if (is_array($cur)) { |
125 | 139 | next($r); |
— | — | @@ -130,20 +144,20 @@ |
131 | 145 | /** |
132 | 146 | * The PDO::Statement class implements the array interface so count() will work |
133 | 147 | */ |
134 | | - function numRows($res) { |
| 148 | + function numRows( $res ) { |
135 | 149 | $r = $res instanceof ResultWrapper ? $res->result : $res; |
136 | | - return count($r); |
| 150 | + return count( $r ); |
137 | 151 | } |
138 | 152 | |
139 | | - function numFields($res) { |
| 153 | + function numFields( $res ) { |
140 | 154 | $r = $res instanceof ResultWrapper ? $res->result : $res; |
141 | | - return is_array($r) ? count($r[0]) : 0; |
| 155 | + return is_array( $r ) ? count( $r[0] ) : 0; |
142 | 156 | } |
143 | 157 | |
144 | | - function fieldName($res,$n) { |
| 158 | + function fieldName( $res, $n ) { |
145 | 159 | $r = $res instanceof ResultWrapper ? $res->result : $res; |
146 | | - if (is_array($r)) { |
147 | | - $keys = array_keys($r[0]); |
| 160 | + if ( is_array( $r ) ) { |
| 161 | + $keys = array_keys( $r[0] ); |
148 | 162 | return $keys[$n]; |
149 | 163 | } |
150 | 164 | return false; |
— | — | @@ -152,8 +166,8 @@ |
153 | 167 | /** |
154 | 168 | * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks |
155 | 169 | */ |
156 | | - function tableName($name) { |
157 | | - return str_replace('`','',parent::tableName($name)); |
| 170 | + function tableName( $name ) { |
| 171 | + return str_replace( '`', '', parent::tableName( $name ) ); |
158 | 172 | } |
159 | 173 | |
160 | 174 | /** |
— | — | @@ -170,20 +184,26 @@ |
171 | 185 | return $this->mConn->lastInsertId(); |
172 | 186 | } |
173 | 187 | |
174 | | - function dataSeek($res,$row) { |
175 | | - if ($res instanceof ResultWrapper) $r =& $res->result; else $r =& $res; |
176 | | - reset($r); |
177 | | - if ($row > 0) for ($i = 0; $i < $row; $i++) next($r); |
| 188 | + function dataSeek( $res, $row ) { |
| 189 | + if ( $res instanceof ResultWrapper ) |
| 190 | + $r =& $res->result; |
| 191 | + else |
| 192 | + $r =& $res; |
| 193 | + reset( $r ); |
| 194 | + if ( $row > 0 ) |
| 195 | + for ( $i = 0; $i < $row; $i++ ) |
| 196 | + next( $r ); |
178 | 197 | } |
179 | 198 | |
180 | 199 | function lastError() { |
181 | | - if (!is_object($this->mConn)) return "Cannot return last error, no db connection"; |
| 200 | + if ( !is_object( $this->mConn ) ) |
| 201 | + return "Cannot return last error, no db connection"; |
182 | 202 | $e = $this->mConn->errorInfo(); |
183 | | - return isset($e[2]) ? $e[2] : ''; |
| 203 | + return isset( $e[2] ) ? $e[2] : ''; |
184 | 204 | } |
185 | 205 | |
186 | 206 | function lastErrno() { |
187 | | - if (!is_object($this->mConn)) { |
| 207 | + if ( !is_object( $this->mConn ) ) { |
188 | 208 | return "Cannot return last error, no db connection"; |
189 | 209 | } else { |
190 | 210 | $info = $this->mConn->errorInfo(); |
— | — | @@ -200,7 +220,7 @@ |
201 | 221 | * Returns false if the index does not exist |
202 | 222 | * - if errors are explicitly ignored, returns NULL on failure |
203 | 223 | */ |
204 | | - function indexInfo($table, $index, $fname = 'Database::indexExists') { |
| 224 | + function indexInfo( $table, $index, $fname = 'DatabaseSqlite::indexExists' ) { |
205 | 225 | $sql = 'PRAGMA index_info(' . $this->addQuotes( $this->indexName( $index ) ) . ')'; |
206 | 226 | $res = $this->query( $sql, $fname ); |
207 | 227 | if ( !$res ) { |
— | — | @@ -216,8 +236,8 @@ |
217 | 237 | return $info; |
218 | 238 | } |
219 | 239 | |
220 | | - function indexUnique($table, $index, $fname = 'Database::indexUnique') { |
221 | | - $row = $this->selectRow( 'sqlite_master', '*', |
| 240 | + function indexUnique( $table, $index, $fname = 'DatabaseSqlite::indexUnique' ) { |
| 241 | + $row = $this->selectRow( 'sqlite_master', '*', |
222 | 242 | array( |
223 | 243 | 'type' => 'index', |
224 | 244 | 'name' => $this->indexName( $index ), |
— | — | @@ -239,27 +259,34 @@ |
240 | 260 | /** |
241 | 261 | * Filter the options used in SELECT statements |
242 | 262 | */ |
243 | | - function makeSelectOptions($options) { |
244 | | - foreach ($options as $k => $v) if (is_numeric($k) && $v == 'FOR UPDATE') $options[$k] = ''; |
245 | | - return parent::makeSelectOptions($options); |
| 263 | + function makeSelectOptions( $options ) { |
| 264 | + foreach ( $options as $k => $v ) |
| 265 | + if ( is_numeric( $k ) && $v == 'FOR UPDATE' ) |
| 266 | + $options[$k] = ''; |
| 267 | + return parent::makeSelectOptions( $options ); |
246 | 268 | } |
247 | 269 | |
248 | 270 | /** |
249 | 271 | * Based on MySQL method (parent) with some prior SQLite-sepcific adjustments |
250 | 272 | */ |
251 | | - function insert($table, $a, $fname = 'DatabaseSqlite::insert', $options = array()) { |
252 | | - if (!count($a)) return true; |
253 | | - if (!is_array($options)) $options = array($options); |
| 273 | + function insert( $table, $a, $fname = 'DatabaseSqlite::insert', $options = array() ) { |
| 274 | + if ( !count( $a ) ) return true; |
| 275 | + if ( !is_array( $options ) ) $options = array( $options ); |
254 | 276 | |
255 | 277 | # SQLite uses OR IGNORE not just IGNORE |
256 | | - foreach ($options as $k => $v) if ($v == 'IGNORE') $options[$k] = 'OR IGNORE'; |
| 278 | + foreach ( $options as $k => $v ) |
| 279 | + if ( $v == 'IGNORE' ) |
| 280 | + $options[$k] = 'OR IGNORE'; |
257 | 281 | |
258 | 282 | # SQLite can't handle multi-row inserts, so divide up into multiple single-row inserts |
259 | | - if (isset($a[0]) && is_array($a[0])) { |
| 283 | + if ( isset( $a[0] ) && is_array( $a[0] ) ) { |
260 | 284 | $ret = true; |
261 | | - foreach ($a as $k => $v) if (!parent::insert($table,$v,"$fname/multi-row",$options)) $ret = false; |
| 285 | + foreach ( $a as $k => $v ) |
| 286 | + if ( !parent::insert( $table, $v, "$fname/multi-row", $options ) ) |
| 287 | + $ret = false; |
| 288 | + } else { |
| 289 | + $ret = parent::insert( $table, $a, "$fname/single-row", $options ); |
262 | 290 | } |
263 | | - else $ret = parent::insert($table,$a,"$fname/single-row",$options); |
264 | 291 | |
265 | 292 | return $ret; |
266 | 293 | } |
— | — | @@ -268,8 +295,8 @@ |
269 | 296 | * Returns the size of a text field, or -1 for "unlimited" |
270 | 297 | * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though. |
271 | 298 | */ |
272 | | - function textFieldSize($table, $field) { |
273 | | - return -1; |
| 299 | + function textFieldSize( $table, $field ) { |
| 300 | + return - 1; |
274 | 301 | } |
275 | 302 | |
276 | 303 | function wasDeadlock() { |
— | — | @@ -295,15 +322,14 @@ |
296 | 323 | * @return string Version information from the database |
297 | 324 | */ |
298 | 325 | function getServerVersion() { |
299 | | - global $wgContLang; |
300 | | - $ver = $this->mConn->getAttribute(PDO::ATTR_SERVER_VERSION); |
| 326 | + $ver = $this->mConn->getAttribute( PDO::ATTR_SERVER_VERSION ); |
301 | 327 | return $ver; |
302 | 328 | } |
303 | 329 | |
304 | 330 | /** |
305 | 331 | * Query whether a given column exists in the mediawiki schema |
306 | 332 | */ |
307 | | - function fieldExists($table, $field, $fname = '') { |
| 333 | + function fieldExists( $table, $field, $fname = '' ) { |
308 | 334 | $info = $this->fieldInfo( $table, $field ); |
309 | 335 | return (bool)$info; |
310 | 336 | } |
— | — | @@ -312,7 +338,7 @@ |
313 | 339 | * Get information about a given field |
314 | 340 | * Returns false if the field does not exist. |
315 | 341 | */ |
316 | | - function fieldInfo($table, $field) { |
| 342 | + function fieldInfo( $table, $field ) { |
317 | 343 | $tableName = $this->tableName( $table ); |
318 | 344 | $sql = 'PRAGMA table_info(' . $this->addQuotes( $tableName ) . ')'; |
319 | 345 | $res = $this->query( $sql, __METHOD__ ); |
— | — | @@ -325,51 +351,53 @@ |
326 | 352 | } |
327 | 353 | |
328 | 354 | function begin( $fname = '' ) { |
329 | | - if ($this->mTrxLevel == 1) $this->commit(); |
| 355 | + if ( $this->mTrxLevel == 1 ) $this->commit(); |
330 | 356 | $this->mConn->beginTransaction(); |
331 | 357 | $this->mTrxLevel = 1; |
332 | 358 | } |
333 | 359 | |
334 | 360 | function commit( $fname = '' ) { |
335 | | - if ($this->mTrxLevel == 0) return; |
| 361 | + if ( $this->mTrxLevel == 0 ) return; |
336 | 362 | $this->mConn->commit(); |
337 | 363 | $this->mTrxLevel = 0; |
338 | 364 | } |
339 | 365 | |
340 | 366 | function rollback( $fname = '' ) { |
341 | | - if ($this->mTrxLevel == 0) return; |
| 367 | + if ( $this->mTrxLevel == 0 ) return; |
342 | 368 | $this->mConn->rollBack(); |
343 | 369 | $this->mTrxLevel = 0; |
344 | 370 | } |
345 | 371 | |
346 | | - function limitResultForUpdate($sql, $num) { |
| 372 | + function limitResultForUpdate( $sql, $num ) { |
347 | 373 | return $this->limitResult( $sql, $num ); |
348 | 374 | } |
349 | 375 | |
350 | | - function strencode($s) { |
351 | | - return substr($this->addQuotes($s),1,-1); |
| 376 | + function strencode( $s ) { |
| 377 | + return substr( $this->addQuotes( $s ), 1, - 1 ); |
352 | 378 | } |
353 | 379 | |
354 | | - function encodeBlob($b) { |
| 380 | + function encodeBlob( $b ) { |
355 | 381 | return new Blob( $b ); |
356 | 382 | } |
357 | 383 | |
358 | | - function decodeBlob($b) { |
359 | | - if ($b instanceof Blob) { |
| 384 | + function decodeBlob( $b ) { |
| 385 | + if ( $b instanceof Blob ) { |
360 | 386 | $b = $b->fetch(); |
361 | 387 | } |
362 | 388 | return $b; |
363 | 389 | } |
364 | 390 | |
365 | | - function addQuotes($s) { |
| 391 | + function addQuotes( $s ) { |
366 | 392 | if ( $s instanceof Blob ) { |
367 | 393 | return "x'" . bin2hex( $s->fetch() ) . "'"; |
368 | 394 | } else { |
369 | | - return $this->mConn->quote($s); |
| 395 | + return $this->mConn->quote( $s ); |
370 | 396 | } |
371 | 397 | } |
372 | 398 | |
373 | | - function quote_ident($s) { return $s; } |
| 399 | + function quote_ident( $s ) { |
| 400 | + return $s; |
| 401 | + } |
374 | 402 | |
375 | 403 | /** |
376 | 404 | * How lagged is this slave? |
— | — | @@ -383,25 +411,25 @@ |
384 | 412 | * - this is the same way PostgreSQL works, MySQL reads in tables.sql and interwiki.sql using dbsource (which calls db->sourceFile) |
385 | 413 | */ |
386 | 414 | public function setup_database() { |
387 | | - global $IP,$wgSQLiteDataDir,$wgDBTableOptions; |
| 415 | + global $IP, $wgSQLiteDataDir, $wgDBTableOptions; |
388 | 416 | $wgDBTableOptions = ''; |
389 | 417 | |
390 | 418 | # Process common MySQL/SQLite table definitions |
391 | 419 | $err = $this->sourceFile( "$IP/maintenance/tables.sql" ); |
392 | | - if ($err !== true) { |
393 | | - $this->reportQueryError($err,0,$sql,__FUNCTION__); |
| 420 | + if ( $err !== true ) { |
| 421 | + $this->reportQueryError( $err, 0, $sql, __FUNCTION__ ); |
394 | 422 | exit( 1 ); |
395 | 423 | } |
396 | 424 | |
397 | 425 | # Use DatabasePostgres's code to populate interwiki from MySQL template |
398 | | - $f = fopen("$IP/maintenance/interwiki.sql",'r'); |
399 | | - if ($f == false) dieout("<li>Could not find the interwiki.sql file"); |
| 426 | + $f = fopen( "$IP/maintenance/interwiki.sql", 'r' ); |
| 427 | + if ( $f == false ) dieout( "<li>Could not find the interwiki.sql file" ); |
400 | 428 | $sql = "INSERT INTO interwiki(iw_prefix,iw_url,iw_local) VALUES "; |
401 | | - while (!feof($f)) { |
402 | | - $line = fgets($f,1024); |
| 429 | + while ( !feof( $f ) ) { |
| 430 | + $line = fgets( $f, 1024 ); |
403 | 431 | $matches = array(); |
404 | | - if (!preg_match('/^\s*(\(.+?),(\d)\)/', $line, $matches)) continue; |
405 | | - $this->query("$sql $matches[1],$matches[2])"); |
| 432 | + if ( !preg_match( '/^\s*(\(.+?),(\d)\)/', $line, $matches ) ) continue; |
| 433 | + $this->query( "$sql $matches[1],$matches[2])" ); |
406 | 434 | } |
407 | 435 | } |
408 | 436 | |
— | — | @@ -422,7 +450,7 @@ |
423 | 451 | $s = parent::replaceVars( $s ); |
424 | 452 | if ( preg_match( '/^\s*CREATE TABLE/i', $s ) ) { |
425 | 453 | // CREATE TABLE hacks to allow schema file sharing with MySQL |
426 | | - |
| 454 | + |
427 | 455 | // binary/varbinary column type -> blob |
428 | 456 | $s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'blob\1', $s ); |
429 | 457 | // no such thing as unsigned |
— | — | @@ -494,10 +522,9 @@ |
495 | 523 | |
496 | 524 | # isKey(), isMultipleKey() not implemented, MySQL-specific concept. |
497 | 525 | # Suggest removal from base class [TS] |
498 | | - |
| 526 | + |
499 | 527 | function type() { |
500 | 528 | return $this->info->type; |
501 | 529 | } |
502 | 530 | |
503 | 531 | } // end SQLiteField |
504 | | - |