JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix db_get_value after mysql->mysqli upgrade
[wfpl.git] / db.php
diff --git a/db.php b/db.php
index d24d8b6..428c1bf 100644 (file)
--- a/db.php
+++ b/db.php
@@ -111,6 +111,7 @@ function db_send_query($sql) {
 #
 # %%  put a % in the output
 # %i  put an integer in the output (strips non-numeric digits, and puts in 0 if blank)
+# %I  a list of integers (as %i) separated by commas
 # %f  put a floating point value in the output (strips non-numeric digits, puts in 0.0 if not valid)
 # %"  output double quotes, surrounding the variable which is encoded to be in there.
 # %s  output encoded to be in double quotes, but don't output the quotes
@@ -147,9 +148,18 @@ function _db_printf($str, $args) {
                } elseif($chr == 's') {
                        $out .= db_enc_sql(array_shift($args));
                } elseif($chr == 'i') {
-                       $int = format_int(array_shift($args));
-                       if($int == '') $int = '0';
-                       $out .= $int;
+                       $out .= format_int_0(array_shift($args));
+               } elseif($chr == 'I') {
+                       $arg = array_shift($args);
+                       $first = true;
+                       foreach ($arg as $int) {
+                               if ($first) {
+                                       $first = false;
+                               } else {
+                                       $out .= ',';
+                               }
+                               $out .= format_int_0($int);
+                       }
                } elseif($chr == 'f') {
                        $arg = array_shift($args);
                        if(is_numeric($arg)) {
@@ -266,7 +276,7 @@ function db_get_value($table, $column, $where = '') {
        $result = db_send_get($table, $column, $where, $args);
 
        $value = mysqli_fetch_row($result);
-       if($value !== false) {
+       if($value !== NULL) {
                $value = $value[0];
        }
 
@@ -456,10 +466,22 @@ function db_reposition_respace($table, $field, $where = '') {
                return;
        }
        $inc = floor(DB_ORD_MAX / ($c + 1));
-       $cur = $inc;
-       foreach($ids as $id) {
-               db_update($table, $field, $cur, 'where id=%i', $id);
-               $cur += $inc;
+       $ord = $inc;
+       $count = count($ids);
+       for ($i = 0; $i < $count; $i += 1000) {
+               $values = [];
+               $j_max = min($count, $i + 1000);
+               for ($j = $i; $j < $j_max; ++$j) {
+                       $id = $ids[$j];
+                       $values[] = "($id,$ord)";
+                       $ord += $inc;
+               }
+               $sql =
+                       "insert into $table (id,$field) values "
+                       . implode(',', $values)
+                       . " on duplicate key update $field=VALUES($field)"
+               ;
+               db_send_query($sql);
        }
 }
 
@@ -581,7 +603,7 @@ function db_upgrade() {
        }
 
        if ($version === -1) {
-               db_send_query('create table if not exists wfpl_mutexes (id int unique auto_increment, name varchar(30) binary, expires int(11)) CHARSET=utf8;');
+               db_send_query('create table if not exists wfpl_mutexes (id int unique auto_increment, name varchar(255) binary, expires int(11)) CHARSET=utf8;');
                $version = 0;
                # don't save version now in case another thread is doing this too
        }