summaryrefslogtreecommitdiff
path: root/it_dbi.class
diff options
context:
space:
mode:
Diffstat (limited to 'it_dbi.class')
-rw-r--r--it_dbi.class361
1 files changed, 244 insertions, 117 deletions
diff --git a/it_dbi.class b/it_dbi.class
index 0fa890a..e51b23e 100644
--- a/it_dbi.class
+++ b/it_dbi.class
@@ -21,6 +21,8 @@
class it_dbi
{
+ static $_global_key = 'it_dbi'; # $GLOBAL key to use for singleton
+
var $_found_rows; # public: number of found rows if CALC_FOUND_ROWS was set
var $_data; # semi-public: current result as assoc array
@@ -69,7 +71,7 @@ function __construct($p = array(), $query = null)
$p['db'] = strtr(it::match('/www/([^/]*)', $p['home']), '.-', '__');
# If the global singleton defaults are for this db/server/server_update then use them.
- $dp = (array)$GLOBALS['it_dbi']->_p;
+ $dp = (array)$GLOBALS[static::$_global_key]->_p;
if ((!isset($p['db']) || ($p['db'] == $dp['db'])) && (!isset($p['server']) || ($p['server'] == $dp['server'])) && (!isset($p['server_update']) || ($p['server_update'] == $dp['server_update'])))
$p += $dp;
@@ -81,8 +83,8 @@ function __construct($p = array(), $query = null)
if ($p['table']) # Standard use: create a table object
{
- if (!isset($GLOBALS['it_dbi']))
- new it_dbi;
+ if (!isset($GLOBALS[static::$_global_key]))
+ new static::$_global_key;
if ($p['getfieldinfo'])
$this->_p += $this->_get_field_info(); # Get $this->_fields and p[keyfield, autoincrement, randomid]
@@ -98,7 +100,7 @@ function __construct($p = array(), $query = null)
$this->read($query);
}
else
- $GLOBALS['it_dbi'] =& $this;
+ $GLOBALS[static::$_global_key] =& $this;
}
/**
@@ -115,26 +117,26 @@ function __construct($p = array(), $query = null)
static function createclasses($p = array())
{
# Make sure singleton exists
- $dbi = $GLOBALS['it_dbi'] ? $GLOBALS['it_dbi'] : new it_dbi($p);
+ $dbi = $GLOBALS[static::$_global_key] ?: new static::$_global_key($p);
$p += $dbi->_p;
$dbid = "{$p['user']}@{$p['server']}:{$p['db']}";
- $state = it_dbi::_state_get($dbid);
+ $state = static::_state_get($dbid);
if (!$tables = $state['tables'])
{
$tables = $dbi->tables($p);
- $state = it_dbi::_state_get($dbid); # State could have been modified by $db->tables() call
+ $state = static::_state_get($dbid); # State could have been modified by $db->tables() call
$state['tables'] = $tables;
- it_dbi::_state_put($dbid, $state);
+ static::_state_put($dbid, $state);
}
foreach ($tables as $table)
{
# Either create class in autoloader or manually just below
if (!class_exists($p['classprefix'] . $table))
- it_dbi::createclass(array('table' => $table) + $p);
+ static::createclass(array('table' => $table) + $p);
}
}
@@ -149,23 +151,22 @@ static function createclass($p)
$p = array('table' => $p);
# Make sure singleton exists
- $dbi = $GLOBALS['it_dbi'] ? $GLOBALS['it_dbi'] : new it_dbi(array('table' => null) + $p);
+ $dbi = $GLOBALS[static::$_global_key] ? $GLOBALS[static::$_global_key] : new static::$_global_key(['table' => null] + $p);
$p += $dbi->_p;
$dbid = $dbi->_dbid = "{$p['user']}@{$p['server']}:{$p['db']}";
if (!isset($dbi->_tables[$dbid]))
{
- $state = it_dbi::_state_get($dbid);
+ $state = static::_state_get($dbid);
$dbi->_tables[$dbid] = array();
if (!($tables = $state['tables']))
{
- for ($tables = array(), $res = $dbi->query('SHOW TABLES', $p); $row = mysqli_fetch_row($res);)
- $tables[] = $row[0];
+ $tables = $dbi->tables($p);
- $state = it_dbi::_state_get($dbid); # State could have been modified by query above
+ $state = static::_state_get($dbid); # State could have been modified by query above
$state['tables'] = $tables;
- it_dbi::_state_put($dbid, $state);
+ static::_state_put($dbid, $state);
}
foreach ($tables as $table)
@@ -179,7 +180,8 @@ static function createclass($p)
if (substr($classname, 0, 4) != 'PMA_') # It is designed behaviour that an error is generated if this class already exists!
{
$interface = function_exists("interface_exists") && interface_exists("Iterator", false) ? "implements Iterator" : "";
- $code = "class $classname extends it_dbi $interface
+ $parentname = get_called_class();
+ $code = "class $classname extends $parentname $interface
{
function __construct(/* \$query ... */)
{
@@ -207,47 +209,39 @@ function _connect($p = array())
{
$p += $this->_p;
$dbid = "{$p['user']}@{$p['server']}:{$p['db']}";
- $state = it_dbi::_state_get($dbid);
+ $state = static::_state_get($dbid);
if ($p['reconnect'] || !($this->_link = $state['link']))
{
# Force new link if same server/user was seen before (mysql ignores selected db)
- if ($GLOBALS['it_dbi']->_connected["{$p['server']}/{$p['user']}"]++)
- $this->_link = @mysqli_connect($p['server'], $p['user'], $p['pw']);
+ if ($GLOBALS[static::$_global_key]->_connected["{$p['server']}/{$p['user']}"]++)
+ list($this->_link, $error) = $this->_connect_db($p);
else
- $this->_link = @mysqli_connect($p['server'], $p['user'], $p['pw']);
+ list($this->_link, $error) = $this->_connect_db($p);
if (!$this->_link)
{
# One retry after a short delay
- it::log('sqllog', "it_dbi(): retrying DB link (mysqli_connect {$p['server']}, {$p['db']}): " . mysqli_connect_error());
+ it::log('sqllog', "it_dbi(): retrying DB link (_connect_db {$p['server']}, {$p['db']}): $error");
sleep(1);
- $this->_link = @mysqli_connect($p['server'], $p['user'], $p['pw']);
+ list($this->_link, $error) = $this->_connect_db($p);
}
if (!$this->_link)
- $this->_fatal("_connect(): can't create DB link (mysqli_connect {$p['user']}@{$p['server']}, {$p['db']}): " . mysqli_connect_error());
-
- if (!(@mysqli_select_db($this->_link, $p['db'])))
- $this->_fatal("_connect(): can't select database \"{$p['db']}\"");
-
- # set charset used for this connection
- if ($p['charset'])
- if (!mysqli_set_charset($this->_link, $p['charset']))
- $this->_fatal("_connect(): can't set charset \"{$p['charset']}\"");
+ $this->_fatal("_connect(): can't create DB link (_connect_db {$p['user']}@{$p['server']}, {$p['db']}): $error");
# NOTE: This overwrites old state but that is on purpose. New link means we should refetch all info about connection
$state['link'] = $this->_link;
- it_dbi::_state_put($dbid, $state, false); # Store only locally as link is not shared anyway
+ static::_state_put($dbid, $state, false); # Store only locally as link is not shared anyway
}
}
/**
- * INTERNAL: construct SQL SET clause of changed values from tags array.
+ * INTERNAL: construct SQL expressions of changed values from tags array.
* $force = must write all fields, dont try to optimize
*/
-function _set($tags, $force = false)
+function _expressions($tags, $force = false)
{
$result = array();
$dyndata = $this->_dyndata;
@@ -264,44 +258,72 @@ function _set($tags, $force = false)
if (!$this->_fields[$f] && $this->_fields['dyncols'])
{
if (substr($field, 0, 1) === "-")
- $newdyns[] = "'\$.$f', " . $value;
+ $newdyns[$f] = $value;
else if ($force || isset($value) && isset($dyndata[$f]) ? strval($value) !== strval($dyndata[$f]) : $value !== $dyndata[$f] || !array_key_exists($f, $dyndata))
{
if (is_null($value))
- $deldyns[] = "'\$.$f'";
- if (is_int($value))
- $newdyns[] = "'\$.$f', $value";
+ $deldyns[] = $f;
+ else if (is_int($value))
+ $newdyns[$f] = $value;
else
- $newdyns[] = "'\$.$f', " . $this->escape_string($value);
+ $newdyns[$f] = $this->escape_string($value);
}
- $alldyns[] = $this->escape_string($f) . ", " . (substr($field, 0, 1) === "-" || is_int($value) ? $value : $this->escape_string($value));
+ $alldyns[$f] = (substr($field, 0, 1) === "-" || is_int($value) ? $value : $this->escape_string($value));
$dyndata[$f] = $value;
}
else if (substr($field, 0, 1) === '-') # Unquoted value (always added)
- $result[] = substr($field, 1) . "=$value";
+ $result[substr($field, 1)] = $value;
else if ($force || (isset($value) && isset($this->_data[$field]) ? strval($value) !== strval($this->_data[$field]) : $value !== $this->_data[$field] || !array_key_exists($field, $this->_data)))
- $result[] = "`$field`=" . (isset($value) ? $this->escape_string($value) : 'NULL');
+ $result[$field] = isset($value) ? $this->escape_string($value) : 'NULL';
}
if ($alldyns)
{
if ($force == "insert") # INSERT/REPLACE
- $result[] = "dyncols = JSON_OBJECT(" . join(", ", (array)$alldyns) . ")";
+ $result['dyncols'] = $this->_json_object($alldyns);
else if ($newdyns || $deldyns)
{
- $source = $this->_dyndata ? 'dyncols' : '"{}"';
+ $source = $this->_dyndata ? 'dyncols' : $this->escape_string('{}');
if ($newdyns)
- $source = "JSON_SET($source, " . join(', ', $newdyns) . ')';
+ $source = $this->_json_set($source, $newdyns);
if ($deldyns)
- $source = "JSON_REMOVE($source, " . join(', ', $deldyns) . ')';
- $result[] = "dyncols = $source";
+ $source = $this->_json_remove($source, $deldyns);
+ $result['dyncols'] = $source;
}
}
$this->_writes += $result ? 1 : 0;
- return $result ? 'SET ' . implode(', ', $result) : '';
+ return $result;
+}
+
+/**
+ * INTERNAL: construct SQL SET clause of changed values from tags array.
+ * $force = must write all fields, dont try to optimize
+ */
+function _set($tags, $force = false)
+{
+ $expressions = $this->_expressions($tags, $force);
+ foreach ((array)$expressions as $k => $v)
+ $strings[] = $this->escape_name($k) . "=$v";
+
+ return $strings ? 'SET ' . implode(', ', $strings) : '';
+}
+
+/**
+ * INTERNAL: construct SQL VALUES clause of changed values from tags array.
+ * $force = must write all fields, dont try to optimize
+ */
+function _values($tags, $force = false)
+{
+ $expressions = $this->_expressions($tags, $force);
+
+ foreach ((array)$expressions as $k => $v) {
+ $keys[] = $this->escape_name($k);
+ $vals[] = $v;
+ }
+ return $expressions ? '(' . implode(', ', $keys) . ') VALUES (' . implode(', ', $vals) . ')' : '';
}
/**
@@ -392,7 +414,9 @@ function _where($params)
}
if ($dyncols_enabled && $this->_fields['dyncols'] && !$this->_fields[$field] && strpos($field, '(') === false)
- $field = "JSON_EXTRACT(dyncols, " . $this->escape_string('$.' . $field) . ")";
+ $field = $this->_json_extract('dyncols', $field);
+ else if (it::match('^\w*[A-Z]\w+$', $field, ['casesensitive' => 1]))
+ $field = $this->escape_name($field);
switch ($op)
{
@@ -406,7 +430,7 @@ function _where($params)
$query .= $sep . "$field IS NOT NULL AND (" . join(" OR ", $parts) . ")"; # Check for IS NOT NULL to take advantage of index
}
else
- $query .= $sep . "1";
+ $query .= $sep . "TRUE";
break;
case 'MATCH':
@@ -428,7 +452,7 @@ function _where($params)
$query .= "$sep$field $op (" . join(",", $qvals) . ")"; # null is mapped to ''
}
else
- $query .= $sep . (($op == 'IN') ? "0" : "1");
+ $query .= $sep . (($op == 'IN') ? "FALSE" : "TRUE");
break;
}
@@ -454,25 +478,15 @@ function _where($params)
return $query;
}
-
/**
* Internal: Output class name::error message and terminate execution.
*/
function _fatal($text, $body = null)
{
- $text = get_class($this).'::'.$text;
-
- if ($this->_link && ($errstr = mysqli_error($this->_link)))
- $text = "\"$errstr\" in $text [errno " . mysqli_errno($this->_link) . "]";
-
- if ($this->_link && ($res = @mysqli_fetch_row(mysqli_query($this->_link, 'select database()')))) # dont create extra errs
- $text .= ", DB: " . $res[0];
-
- it::fatal(['title' => $text . ", Server: " . $this->_p['server'], 'body' => $body]);
+ it::fatal(['title' => $this->_error($text) . ", DB: " . $this->_p['db'] . ", Server: " . $this->_p['server'], 'body' => $body]);
/* NOT REACHED */
}
-
/**
* Hook to postprocess data after reading a record.
* This is a stub-function that can be overloaded.
@@ -502,10 +516,7 @@ static function _write_preprocess($data)
*/
function tables($p = array())
{
- for ($qr = $this->query('SHOW TABLES', $p); $row = mysqli_fetch_row($qr);)
- $result[] = $row[0];
-
- return (array)$result;
+ return $this->_tables($p);
}
@@ -547,40 +558,17 @@ function query($query, $p = array())
debug("{$p['user']}@{$p['server']}:{$p['db']}" . '.' . get_class($this) . "::query(\"$query\")", 4);
- if (!($result = mysqli_query($this->_link, $query, $p['unbuffered'] ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT)) && $p['safety'])
+ if (!($result = $this->_query($query, $p)))
{
- $errno = mysqli_errno($this->_link);
- if (($p['safety'] < 2) && ($errno == 1062)) # Duplicate entry
- return false;
-
- if ($errno == 2006) # mysql server has gone away: retry
- {
- it::log('sqllog', "it_dbi(): reconnecting mysqli_connect {$p['server']}, {$p['db']}");
- $this->_connect(array('reconnect' => true));
- $result = mysqli_query($this->_link, $query, $p['unbuffered'] ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT);
- }
-
- if (!$result)
- $this->_fatal("query(\"$query\") failed");
+ if ($result === false)
+ return $result;
+ $this->_fatal("query(\"$query\") failed");
}
else if (it::match('^(CREATE|ALTER|DROP) ', $query, array('utf8' => false)))
{
# Purge cache for schema changes (after modifying table)
$dbid = "{$p['user']}@{$p['server']}:{$p['db']}";
- it_dbi::_state_purgeshared($dbid);
- }
-
- $this->_affectedrows = $this->_link->affected_rows; # get_warnings() clobbers this
- $this->_insertid = mysqli_insert_id($this->_link); # get_warnings() clobbers this
- if (($warning = $this->_link->get_warnings()))
- {
- do {
- if (!it::match(trim($this->_p['ignored_warnings'] . "|1364|1261|1051|1062", "|"), $warning->errno))
- $messages[] = $warning->message . " [error $warning->errno]";
- } while ($warning->next() && ++$checked < 20);
-
- if ($messages)
- it::error(['title' => "Mysql warning: " . $messages[0], 'body' => "$query\n\n" . join("\n", $messages) . "\n"]);
+ static::_state_purgeshared($dbid);
}
if ($writing && $this->_p['throttle_writes'])
@@ -674,11 +662,11 @@ function select(/* $query = array|string, ... */)
$this->clear();
if ($this->_result = $this->query($sql = "SELECT $what " . $this->_from($query) . " " . $this->_where($query)))
{
- $result = $this->_p['unbuffered'] ? true : mysqli_num_rows($this->_result);
+ $result = $this->_p['unbuffered'] ? true : $this->_num_rows($this->_result);
if ($calc_found_rows)
{
- list($count) = mysqli_fetch_row($this->query('SELECT FOUND_ROWS()'));
+ $count = $this->_fetch_assoc($this->query('SELECT FOUND_ROWS() AS count'))['count'];
$this->_found_rows = intval($count);
}
@@ -701,7 +689,7 @@ function iterate()
{
if (!$this->_nofetch)
{
- if ($this->_data = mysqli_fetch_assoc($this->_result))
+ if ($this->_data = $this->_fetch_assoc($this->_result))
{
if ($localizedfields = $this->_localizedfields)
foreach ($localizedfields as $field => $dummy)
@@ -768,7 +756,7 @@ function iterate()
* @param $tags key => value pairs to set
* @return true for success, false for failure (e.g. duplicate entry for key)
*/
-function insert($tags = array(), $command = "INSERT")
+function insert($tags = array(), $command = "INSERT", $suffix = "")
{
$this->_connect();
@@ -778,9 +766,9 @@ function insert($tags = array(), $command = "INSERT")
if ($this->_p['randomid'] && !isset($tags[$this->_p['keyfield']]))
$tags[$this->_p['keyfield']] = bin2hex(random_bytes(16));
- $set = $this->_set($tags, "insert");
+ $values = $this->_values($tags, "insert");
- if ($result = $this->query($query = "$command INTO {$this->_p['table']} " . $set))
+ if ($result = $this->query($query = "$command INTO {$this->_p['table']} " . $values . $suffix))
{
$id = ($this->_p['autoincrement'] && !isset($tags[$this->_p['keyfield']])) ? $this->_insertid : $tags[$this->_p['keyfield']];
if ($this->_p['keyfield'] && !$this->read($id) && $this->_p['safety'])
@@ -885,7 +873,7 @@ function delete($query = null)
*/
function delete_untouched($query = null)
{
- if ($this->select(['SELECT' => $this->_p['keyfield']] + (array)$query))
+ if ($this->select(['SELECT' => $this->escape_name($this->_p['keyfield'])] + (array)$query))
while ($this->iterate())
if (($id = $this->_key) && !$this->_touchedids[$id] && $this->delete())
$result[] = $id;
@@ -902,9 +890,19 @@ function delete_untouched($query = null)
function escape_string($str)
{
$this->_connect();
- return "'" . mysqli_real_escape_string($this->_link, $str) . "'";
+ return $this->_escape_string($str);
}
+/**
+ * Escapes a name/identifier for use in a DB query
+ * @param The identifier to be quoted
+ * @return The quoted value
+ */
+function escape_name($str)
+{
+ $this->_connect();
+ return $this->_escape_name($str);
+}
/**
* INTERNAL: Store information about a table's fields in $this->_fields, possibly from cache.
@@ -914,16 +912,15 @@ function _get_field_info()
{
$result = array();
$dbid = "{$this->_p['user']}@{$this->_p['server']}:{$this->_p['db']}";
- $state = it_dbi::_state_get($dbid);
+ $state = static::_state_get($dbid);
if (!($this->_fields = $state['fields'][$this->_p['table']]))
{
debug("it_dbi(): no fields for {$dbid}.{$this->_p['table']}, calculating.", 5);
- for ($res = $this->query('SHOW COLUMNS FROM ' . $this->_p['table']); $res && ($field = mysqli_fetch_assoc($res)); )
- {
- $this->_fields[$field['Field']] = $field + array('Length' => preg_match('/date|time/', $field['Type']) ? 20 : intval(it::match('\d+', $field['Type'])));
+ foreach ($this->_get_field_defs() as $name => $field) {
+ $this->_fields[$name] = $field + array('Length' => preg_match('/date|time/', $field['Type']) ? 20 : intval(it::match('\d+', $field['Type'])));
if (preg_match('/^(tiny|small|medium|)int|^float|^double/', $field['Type']))
- $this->_convertfunc[$field['Field']] = it::match('int', $field['Type']) ? "intval" : "floatval";
+ $this->_convertfunc[$name] = it::match('int', $field['Type']) ? "intval" : "floatval";
}
$this->_fieldnames = "," . join(",", array_keys((array)$this->_fields)) . ",";
@@ -931,11 +928,11 @@ function _get_field_info()
foreach (preg_grep('/_' . $this->_p['localized_defaultlanguage'] . '$/', array_keys((array)$this->_fields)) as $field)
$this->_localizedfields[substr($field, 0, -1 - strlen($this->_p['localized_defaultlanguage']))] = true;
- $state = it_dbi::_state_get($dbid); # State could have been modified by query above
+ $state = static::_state_get($dbid); # State could have been modified by query above
$state['fields'][$this->_p['table']] = $this->_fields;
$state['convertfunc'][$this->_p['table']] = $this->_convertfunc;
$state['localizedfields'][$this->_p['table']] = $this->_localizedfields;
- it_dbi::_state_put($dbid, $state);
+ static::_state_put($dbid, $state);
}
else # Existing _fields, copy other info too
{
@@ -953,14 +950,14 @@ function _get_field_info()
}
}
- unset($GLOBALS['it_dbi']->_p['table'], $GLOBALS['it_dbi']->_p['keyfield']); # Remove cruft
+ unset($GLOBALS[static::$_global_key]->_p['table'], $GLOBALS[static::$_global_key]->_p['keyfield']); # Remove cruft
return $result;
}
static function _state_get($dbid)
{
- if (!($result = $GLOBALS['it_dbi']->_state[$dbid]))
- $result = $GLOBALS['it_dbi']->_state[$dbid] = (array)it_cache::get("dbi:$dbid");
+ if (!($result = $GLOBALS[static::$_global_key]->_state[$dbid]))
+ $result = $GLOBALS[static::$_global_key]->_state[$dbid] = (array)it_cache::get(get_called_class() . ":$dbid");
#var_dump("get", $dbid, $result);
return $result;
@@ -969,15 +966,15 @@ static function _state_get($dbid)
static function _state_put($dbid, $state, $shared = true)
{
#var_dump("put", $dbid, $state);
- $GLOBALS['it_dbi']->_state[$dbid] = $state;
+ $GLOBALS[static::$_global_key]->_state[$dbid] = $state;
if ($shared)
- it_cache::put("dbi:$dbid", array('link' => null) + (array)$state); # link is not transferable
+ it_cache::put(get_called_class() . ":$dbid", array('link' => null) + (array)$state); # link is not transferable
}
static function _state_purgeshared($dbid = null)
{
#var_dump("purgeshared", $dbid);
- it_cache::put("dbi:" . ($dbid ? $dbid : $GLOBALS['it_dbi']->_dbid), array()); # Nuke shared cache
+ it_cache::put(get_called_class(). ":" . ($dbid ? $dbid : $GLOBALS[static::$_global_key]->_dbid), array()); # Nuke shared cache
}
#
@@ -1005,8 +1002,8 @@ function rewind()
$this->select();
# Only rewind if not already at start and results present
- if (!$this->_nofetch && mysqli_num_rows($this->_result))
- mysqli_data_seek($this->_result, 0);
+ if (!$this->_nofetch && $this->_num_rows($this->_result))
+ $this->_seek($this->_result, 0);
$this->_iteratorkey = 0;
$this->iterate();
@@ -1037,4 +1034,134 @@ static function get($id)
return isset($id) && ($rec = new static) && $rec->read($id) ? $rec : null;
}
+
+/**
+ * Start of mysqli specific default implmementation
+ */
+
+function _escape_string($str)
+{
+ return "'" . mysqli_real_escape_string($this->_link, $str) . "'";
+}
+
+function _escape_name($str)
+{
+ return "`" . $str . "`";
+}
+
+function _connect_db($p) {
+ $result = @mysqli_connect($p['server'], $p['user'], $p['pw']);
+
+ if ($result)
+ {
+ if (!(@mysqli_select_db($result, $p['db'])))
+ $this->_fatal("_connect(): can't select database \"{$p['db']}\"");
+
+ # set charset used for this connection
+ if ($p['charset'])
+ if (!mysqli_set_charset($result, $p['charset']))
+ $this->_fatal("_connect(): can't set charset \"{$p['charset']}\"");
+ }
+
+ return [$result, mysqli_connect_error()];
+}
+
+function _get_field_defs()
+{
+ for ($res = $this->query('SHOW COLUMNS FROM ' . $this->_p['table']); $res && ($field = $this->_fetch_assoc($res)); )
+ $result[$field['Field']] = it::filter_keys($field, ['Field', 'Type', 'Key', 'Extra']);
+ return $result;
+}
+
+function _tables($p) {
+ for ($qr = $this->query('SHOW TABLES', $p); $row = mysqli_fetch_row($qr);)
+ $result[] = $row[0];
+
+ return (array)$result;
+}
+
+function _query($query, $p)
+{
+ if (!($result = mysqli_query($this->_link, $query, $p['unbuffered'] ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT)) && $p['safety'])
+ {
+ $errno = mysqli_errno($this->_link);
+ if (($p['safety'] < 2) && ($errno == 1062)) # Duplicate entry
+ return false;
+
+ if ($errno == 2006) # mysql server has gone away: retry
+ {
+ it::log('sqllog', "it_dbi(): reconnecting mysqli_connect {$p['server']}, {$p['db']}");
+ $this->_connect(array('reconnect' => true));
+ $result = mysqli_query($this->_link, $query, $p['unbuffered'] ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT);
+ }
+ }
+
+ $this->_affectedrows = $this->_link->affected_rows; # get_warnings() clobbers this
+ $this->_insertid = mysqli_insert_id($this->_link); # get_warnings() clobbers this
+ if (($warning = $this->_link->get_warnings()))
+ {
+ do {
+ if (!it::match(trim($this->_p['ignored_warnings'] . "|1364|1261|1051|1062", "|"), $warning->errno))
+ $messages[] = $warning->message . " [error $warning->errno]";
+ } while ($warning->next() && ++$checked < 20);
+
+ if ($messages)
+ it::error(['title' => "Mysql warning: " . $messages[0], 'body' => "$query\n\n" . join("\n", $messages) . "\n"]);
+ }
+
+ return $result;
+}
+
+function _json_extract($col, $field)
+{
+ return "JSON_EXTRACT($col, " . $this->escape_string('$.' . $field) . ")";
+}
+
+function _json_object($tags)
+{
+ foreach ((array)$tags as $f => $v)
+ $strings[] = $this->escape_string($f) . ', ' . $v;
+ return "JSON_OBJECT(" . implode(', ', $strings) . ")";
+}
+
+function _json_set($source, $tags)
+{
+ foreach ((array)$tags as $f => $v)
+ $strings[] = $this->escape_string('$.' . $f) . ', ' . $v;
+ return "JSON_SET($source, " . implode(", ", $strings) . ')';
+}
+
+function _json_remove($source, $fields)
+{
+ foreach ((array)$fields as $f)
+ $strings[] = $this->escape_string('$.' . $f);
+ return "JSON_REMOVE($source, " . implode(", ", $strings) . ')';
+}
+
+function _fetch_assoc($res)
+{
+ return mysqli_fetch_assoc($res);
+}
+
+function _num_rows($res)
+{
+ return mysqli_num_rows($res);
+}
+
+
+function _seek($res, $offset)
+{
+ return mysqli_data_seek($res, $offset);
+}
+
+function _error($text)
+{
+ $text = get_class($this).'::'.$text;
+
+ if ($this->_link && ($errstr = mysqli_error($this->_link)))
+ $text = "\"$errstr\" in $text [errno " . mysqli_errno($this->_link) . "]";
+
+ return $text;
+}
+
} /* End class it_dbi */