Index: trunk/phase3/includes/db/DatabaseMysql.php |
— | — | @@ -253,8 +253,9 @@ |
254 | 254 | public function estimateRowCount( $table, $vars='*', $conds='', $fname = 'Database::estimateRowCount', $options = array() ) { |
255 | 255 | $options['EXPLAIN'] = true; |
256 | 256 | $res = $this->select( $table, $vars, $conds, $fname, $options ); |
257 | | - if ( $res === false ) |
| 257 | + if ( $res === false ) { |
258 | 258 | return false; |
| 259 | + } |
259 | 260 | if ( !$this->numRows( $res ) ) { |
260 | 261 | return 0; |
261 | 262 | } |
— | — | @@ -350,6 +351,66 @@ |
351 | 352 | return false; |
352 | 353 | } |
353 | 354 | |
| 355 | + /** |
| 356 | + * INSERT ... ON DUPE UPDATE wrapper, inserts an array into a table, optionally updating if |
| 357 | + * duplicate primary key found |
| 358 | + * |
| 359 | + * $a may be a single associative array, or an array of these with numeric keys, for |
| 360 | + * multi-row insert. |
| 361 | + * |
| 362 | + * Usually aborts on failure |
| 363 | + * If errors are explicitly ignored, returns success |
| 364 | + * |
| 365 | + * @param $table String: table name (prefix auto-added) |
| 366 | + * @param $a Array: Array of rows to insert |
| 367 | + * @param $fname String: Calling function name (use __METHOD__) for logs/profiling |
| 368 | + * @param $options Mixed: Associative array of options |
| 369 | + * @param $onDupeUpdate Array: Associative array of fields to update on duplicate |
| 370 | + * |
| 371 | + * @return bool |
| 372 | + */ |
| 373 | + function insertOrUpdate( $table, $a, $fname = 'DatabaseBase::insertOnDupeUpdate', $options = array(), $onDupeUpdate = array() ) { |
| 374 | + # No rows to insert, easy just return now |
| 375 | + if ( !count( $a ) ) { |
| 376 | + return true; |
| 377 | + } |
| 378 | + |
| 379 | + $table = $this->tableName( $table ); |
| 380 | + if ( !is_array( $options ) ) { |
| 381 | + $options = array( $options ); |
| 382 | + } |
| 383 | + if ( isset( $a[0] ) && is_array( $a[0] ) ) { |
| 384 | + $multi = true; |
| 385 | + $keys = array_keys( $a[0] ); |
| 386 | + } else { |
| 387 | + $multi = false; |
| 388 | + $keys = array_keys( $a ); |
| 389 | + } |
| 390 | + |
| 391 | + $sql = 'INSERT ' . implode( ' ', $options ) . |
| 392 | + " INTO $table (" . implode( ',', $keys ) . ') VALUES '; |
| 393 | + |
| 394 | + if ( $multi ) { |
| 395 | + $first = true; |
| 396 | + foreach ( $a as $row ) { |
| 397 | + if ( $first ) { |
| 398 | + $first = false; |
| 399 | + } else { |
| 400 | + $sql .= ','; |
| 401 | + } |
| 402 | + $sql .= '(' . $this->makeList( $row ) . ')'; |
| 403 | + } |
| 404 | + } else { |
| 405 | + $sql .= '(' . $this->makeList( $a ) . ')'; |
| 406 | + } |
| 407 | + |
| 408 | + if ( count( $onDupeUpdate ) ) { |
| 409 | + $sql .= ' ON DUPLICATE KEY UPDATE ' . $this->makeList( $onDupeUpdate ); |
| 410 | + } |
| 411 | + |
| 412 | + return (bool)$this->query( $sql, $fname ); |
| 413 | + } |
| 414 | + |
354 | 415 | function getServerVersion() { |
355 | 416 | return mysql_get_server_info( $this->mConn ); |
356 | 417 | } |
Index: trunk/phase3/includes/db/Database.php |
— | — | @@ -1115,28 +1115,6 @@ |
1116 | 1116 | * @return bool |
1117 | 1117 | */ |
1118 | 1118 | function insert( $table, $a, $fname = 'DatabaseBase::insert', $options = array() ) { |
1119 | | - return $this->insertOnDupeUpdate( $table, $a, $fname, $options ); |
1120 | | - } |
1121 | | - |
1122 | | - /** |
1123 | | - * INSERT ... ON DUPE UPDATE wrapper, inserts an array into a table, optionally updating if |
1124 | | - * duplicate primary key found |
1125 | | - * |
1126 | | - * $a may be a single associative array, or an array of these with numeric keys, for |
1127 | | - * multi-row insert. |
1128 | | - * |
1129 | | - * Usually aborts on failure |
1130 | | - * If errors are explicitly ignored, returns success |
1131 | | - * |
1132 | | - * @param $table String: table name (prefix auto-added) |
1133 | | - * @param $a Array: Array of rows to insert |
1134 | | - * @param $fname String: Calling function name (use __METHOD__) for logs/profiling |
1135 | | - * @param $options Mixed: Associative array of options |
1136 | | - * @param $onDupeUpdate Array: Associative array of fields to update on duplicate |
1137 | | - * |
1138 | | - * @return bool |
1139 | | - */ |
1140 | | - function insertOnDupeUpdate( $table, $a, $fname = 'DatabaseBase::insertOnDupeUpdate', $options = array(), $onDupeUpdate = array() ) { |
1141 | 1119 | # No rows to insert, easy just return now |
1142 | 1120 | if ( !count( $a ) ) { |
1143 | 1121 | return true; |
— | — | @@ -1170,15 +1148,35 @@ |
1171 | 1149 | } else { |
1172 | 1150 | $sql .= '(' . $this->makeList( $a ) . ')'; |
1173 | 1151 | } |
1174 | | - |
1175 | | - if ( count( $onDupeUpdate ) ) { |
1176 | | - $sql .= ' ON DUPLICATE KEY UPDATE ' . $this->makeList( $onDupeUpdate ); |
1177 | | - } |
1178 | | - |
| 1152 | + |
1179 | 1153 | return (bool)$this->query( $sql, $fname ); |
1180 | 1154 | } |
1181 | 1155 | |
1182 | 1156 | /** |
| 1157 | + * INSERT ... ON DUPE UPDATE wrapper, inserts an array into a table, optionally updating if |
| 1158 | + * duplicate primary key found |
| 1159 | + * |
| 1160 | + * $a may be a single associative array, or an array of these with numeric keys, for |
| 1161 | + * multi-row insert. |
| 1162 | + * |
| 1163 | + * Usually aborts on failure |
| 1164 | + * If errors are explicitly ignored, returns success |
| 1165 | + * |
| 1166 | + * @param $table String: table name (prefix auto-added) |
| 1167 | + * @param $a Array: Array of rows to insert |
| 1168 | + * @param $fname String: Calling function name (use __METHOD__) for logs/profiling |
| 1169 | + * @param $options Mixed: Associative array of options |
| 1170 | + * @param $onDupeUpdate Array: Associative array of fields to update on duplicate |
| 1171 | + * |
| 1172 | + * @return bool |
| 1173 | + */ |
| 1174 | + function insertOrUpdate( $table, $a, $fname = 'DatabaseBase::insertOnDupeUpdate', $options = array(), $onDupeUpdate = array() ) { |
| 1175 | + //TODO:FIXME |
| 1176 | + //$this->select(); |
| 1177 | + //$this->replace(); |
| 1178 | + } |
| 1179 | + |
| 1180 | + /** |
1183 | 1181 | * Make UPDATE options for the DatabaseBase::update function |
1184 | 1182 | * |
1185 | 1183 | * @private |
— | — | @@ -1190,10 +1188,12 @@ |
1191 | 1189 | $options = array( $options ); |
1192 | 1190 | } |
1193 | 1191 | $opts = array(); |
1194 | | - if ( in_array( 'LOW_PRIORITY', $options ) ) |
| 1192 | + if ( in_array( 'LOW_PRIORITY', $options ) ) { |
1195 | 1193 | $opts[] = $this->lowPriorityOption(); |
1196 | | - if ( in_array( 'IGNORE', $options ) ) |
| 1194 | + } |
| 1195 | + if ( in_array( 'IGNORE', $options ) ) { |
1197 | 1196 | $opts[] = 'IGNORE'; |
| 1197 | + } |
1198 | 1198 | return implode(' ', $opts); |
1199 | 1199 | } |
1200 | 1200 | |
— | — | @@ -1369,7 +1369,9 @@ |
1370 | 1370 | # Note that we check the end so that we will still quote any use of |
1371 | 1371 | # use of `database`.table. But won't break things if someone wants |
1372 | 1372 | # to query a database table with a dot in the name. |
1373 | | - if ( $name[0] == '`' && substr( $name, -1, 1 ) == '`' ) return $name; |
| 1373 | + if ( $name[0] == '`' && substr( $name, -1, 1 ) == '`' ) { |
| 1374 | + return $name; |
| 1375 | + } |
1374 | 1376 | |
1375 | 1377 | # Lets test for any bits of text that should never show up in a table |
1376 | 1378 | # name. Basically anything like JOIN or ON which are actually part of |
— | — | @@ -1378,19 +1380,26 @@ |
1379 | 1381 | # Note that we use a whitespace test rather than a \b test to avoid |
1380 | 1382 | # any remote case where a word like on may be inside of a table name |
1381 | 1383 | # surrounded by symbols which may be considered word breaks. |
1382 | | - if( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) return $name; |
| 1384 | + if( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) { |
| 1385 | + return $name; |
| 1386 | + } |
1383 | 1387 | |
1384 | 1388 | # Split database and table into proper variables. |
1385 | 1389 | # We reverse the explode so that database.table and table both output |
1386 | 1390 | # the correct table. |
1387 | 1391 | $dbDetails = array_reverse( explode( '.', $name, 2 ) ); |
1388 | | - if( isset( $dbDetails[1] ) ) @list( $table, $database ) = $dbDetails; |
1389 | | - else @list( $table ) = $dbDetails; |
| 1392 | + if( isset( $dbDetails[1] ) ) { |
| 1393 | + @list( $table, $database ) = $dbDetails; |
| 1394 | + } else { |
| 1395 | + @list( $table ) = $dbDetails; |
| 1396 | + } |
1390 | 1397 | $prefix = $this->mTablePrefix; # Default prefix |
1391 | 1398 | |
1392 | 1399 | # A database name has been specified in input. Quote the table name |
1393 | 1400 | # because we don't want any prefixes added. |
1394 | | - if( isset($database) ) $table = ( $table[0] == '`' ? $table : "`{$table}`" ); |
| 1401 | + if( isset($database) ) { |
| 1402 | + $table = ( $table[0] == '`' ? $table : "`{$table}`" ); |
| 1403 | + } |
1395 | 1404 | |
1396 | 1405 | # Note that we use the long format because php will complain in in_array if |
1397 | 1406 | # the input is not an array, and will complain in is_array if it is not set. |
— | — | @@ -1405,7 +1414,9 @@ |
1406 | 1415 | } |
1407 | 1416 | |
1408 | 1417 | # Quote the $database and $table and apply the prefix if not quoted. |
1409 | | - if( isset($database) ) $database = ( $database[0] == '`' ? $database : "`{$database}`" ); |
| 1418 | + if( isset($database) ) { |
| 1419 | + $database = ( $database[0] == '`' ? $database : "`{$database}`" ); |
| 1420 | + } |
1410 | 1421 | $table = ( $table[0] == '`' ? $table : "`{$prefix}{$table}`" ); |
1411 | 1422 | |
1412 | 1423 | # Merge our database and table into our final table name. |