Dep: update

주로 PHAN
This commit is contained in:
2021-08-06 22:39:09 +09:00
parent 5310c2e7f6
commit b306601c72
1098 changed files with 92137 additions and 33228 deletions
+591 -469
View File
@@ -1,6 +1,6 @@
<?php
/*
Copyright (C) 2008-2012 Sergey Tsalkov (stsalkov@gmail.com)
Copyright (C) 2008 Sergey Tsalkov (stsalkov@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -23,24 +23,21 @@ class DB {
public static $user = '';
public static $password = '';
public static $host = 'localhost';
public static $port = null;
public static $port = 3306; //hhvm complains if this is null
public static $socket = null;
public static $encoding = 'latin1';
// configure workings
public static $param_char = '%';
public static $named_param_seperator = '_';
public static $success_handler = false;
public static $error_handler = true;
public static $throw_exception_on_error = false;
public static $nonsql_error_handler = null;
public static $throw_exception_on_nonsql_error = false;
public static $nested_transactions = false;
public static $usenull = true;
public static $ssl = array('key' => '', 'cert' => '', 'ca_cert' => '', 'ca_path' => '', 'cipher' => '');
public static $connect_options = array(MYSQLI_OPT_CONNECT_TIMEOUT => 30);
public static $logfile;
// internal
protected static $mdb = null;
public static $variables_to_sync = array('param_char', 'named_param_seperator', 'nested_transactions', 'ssl', 'connect_options', 'logfile');
public static function getMDB() {
$mdb = DB::$mdb;
@@ -49,66 +46,26 @@ class DB {
$mdb = DB::$mdb = new MeekroDB();
}
static $variables_to_sync = array('param_char', 'named_param_seperator', 'success_handler', 'error_handler', 'throw_exception_on_error',
'nonsql_error_handler', 'throw_exception_on_nonsql_error', 'nested_transactions', 'usenull', 'ssl', 'connect_options');
$db_class_vars = get_class_vars('DB'); // the DB::$$var syntax only works in 5.3+
foreach ($variables_to_sync as $variable) {
if ($mdb->$variable !== $db_class_vars[$variable]) {
$mdb->$variable = $db_class_vars[$variable];
}
}
// Sync everytime because settings might have changed. It's fast.
$mdb->sync_config();
return $mdb;
}
// yes, this is ugly. __callStatic() only works in 5.3+
public static function get() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'get'), $args); }
public static function disconnect() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'disconnect'), $args); }
public static function query() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'query'), $args); }
public static function queryFirstRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstRow'), $args); }
public static function queryOneRow() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneRow'), $args); }
public static function queryAllLists() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryAllLists'), $args); }
public static function queryFullColumns() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFullColumns'), $args); }
public static function queryFirstList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstList'), $args); }
public static function queryOneList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneList'), $args); }
public static function queryFirstColumn() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstColumn'), $args); }
public static function queryOneColumn() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneColumn'), $args); }
public static function queryFirstField() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryFirstField'), $args); }
public static function queryOneField() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryOneField'), $args); }
public static function queryRaw() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryRaw'), $args); }
public static function queryRawUnbuf() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'queryRawUnbuf'), $args); }
public static function insert() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insert'), $args); }
public static function insertIgnore() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertIgnore'), $args); }
public static function insertUpdate() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertUpdate'), $args); }
public static function replace() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'replace'), $args); }
public static function update() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'update'), $args); }
public static function delete() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'delete'), $args); }
public static function insertId() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'insertId'), $args); }
public static function count() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'count'), $args); }
public static function affectedRows() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'affectedRows'), $args); }
public static function useDB() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'useDB'), $args); }
public static function startTransaction() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'startTransaction'), $args); }
public static function commit() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'commit'), $args); }
public static function rollback() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'rollback'), $args); }
public static function tableList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'tableList'), $args); }
public static function columnList() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'columnList'), $args); }
public static function sqlEval() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'sqlEval'), $args); }
public static function nonSQLError() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'nonSQLError'), $args); }
public static function serverVersion() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'serverVersion'), $args); }
public static function transactionDepth() { $args = func_get_args(); return call_user_func_array(array(DB::getMDB(), 'transactionDepth'), $args); }
public static function debugMode($handler = true) {
DB::$success_handler = $handler;
public static function __callStatic($name, $args) {
$fn = array(DB::getMDB(), $name);
if (! is_callable($fn)) {
throw new MeekroDBException("MeekroDB does not have a method called $name");
}
return call_user_func_array($fn, $args);
}
// --- begin deprecated methods (kept for backwards compatability)
static function debugMode($enable=true) {
if ($enable) self::$logfile = fopen('php://output', 'w');
else self::$logfile = null;
}
}
@@ -118,24 +75,17 @@ class MeekroDB {
public $user = '';
public $password = '';
public $host = 'localhost';
public $port = null;
public $port = 3306;
public $socket = null;
public $encoding = 'latin1';
// configure workings
public $param_char = '%';
public $named_param_seperator = '_';
public $success_handler = false;
/** @var callable|bool */
public $error_handler = true;
public $throw_exception_on_error = false;
/** @var callable|null */
public $nonsql_error_handler = null;
public $throw_exception_on_nonsql_error = false;
public $nested_transactions = false;
public $usenull = true;
public $ssl = array('key' => '', 'cert' => '', 'ca_cert' => '', 'ca_path' => '', 'cipher' => '');
/** @var array<int,int|float|string|bool> */
public $connect_options = array(MYSQLI_OPT_CONNECT_TIMEOUT => 30);
public $logfile;
// internal
public $internal_mysql = null;
@@ -145,14 +95,23 @@ class MeekroDB {
public $affected_rows = 0;
public $current_db = null;
public $nested_transactions_count = 0;
public function __construct($host=null, $user=null, $password=null, $dbName=null, $port=null, $encoding=null) {
public $last_query;
protected $hooks = array(
'pre_parse' => array(),
'pre_run' => array(),
'post_run' => array(),
'run_success' => array(),
'run_failed' => array(),
);
public function __construct($host=null, $user=null, $password=null, $dbName=null, $port=null, $encoding=null, $socket=null) {
if ($host === null) $host = DB::$host;
if ($user === null) $user = DB::$user;
if ($password === null) $password = DB::$password;
if ($dbName === null) $dbName = DB::$dbName;
if ($port === null) $port = DB::$port;
if ($socket === null) $socket = DB::$socket;
if ($encoding === null) $encoding = DB::$encoding;
$this->host = $host;
@@ -160,7 +119,19 @@ class MeekroDB {
$this->password = $password;
$this->dbName = $dbName;
$this->port = $port;
$this->socket = $socket;
$this->encoding = $encoding;
$this->sync_config();
}
// suck in config settings from static class
public function sync_config() {
foreach (DB::$variables_to_sync as $variable) {
if ($this->$variable !== DB::$$variable) {
$this->$variable = DB::$$variable;
}
}
}
public function get() {
@@ -181,10 +152,10 @@ class MeekroDB {
}
// suppress warnings, since we will check connect_error anyway
@$mysql->real_connect($this->host, $this->user, $this->password, $this->dbName, $this->port, null, $connect_flags);
@$mysql->real_connect($this->host, $this->user, $this->password, $this->dbName, $this->port, $this->socket, $connect_flags);
if ($mysql->connect_error) {
$this->nonSQLError('Unable to connect to MySQL server! Error: ' . $mysql->connect_error);
throw new MeekroDBException("Unable to connect to MySQL server! Error: {$mysql->connect_error}");
}
$mysql->set_charset($this->encoding);
@@ -203,43 +174,157 @@ class MeekroDB {
}
$this->internal_mysql = null;
}
public function nonSQLError($message) {
if ($this->throw_exception_on_nonsql_error) {
$e = new MeekroDBException($message);
throw $e;
function addHook($type, $fn) {
if (! array_key_exists($type, $this->hooks)) {
throw new MeekroDBException("Hook type $type is not recognized");
}
if (! is_callable($fn)) {
throw new MeekroDBException("Second arg to addHook() must be callable");
}
$this->hooks[$type][] = $fn;
end($this->hooks[$type]);
return key($this->hooks[$type]);
}
function removeHook($type, $index) {
if (! array_key_exists($type, $this->hooks)) {
throw new MeekroDBException("Hook type $type is not recognized");
}
if (! array_key_exists($index, $this->hooks[$type])) {
throw new MeekroDBException("That hook does not exist");
}
unset($this->hooks[$type][$index]);
}
function removeHooks($type) {
if (! array_key_exists($type, $this->hooks)) {
throw new MeekroDBException("Hook type $type is not recognized");
}
$this->hooks[$type] = array();
}
function runHook($type, $args=array()) {
if (! array_key_exists($type, $this->hooks)) {
throw new MeekroDBException("Hook type $type is not recognized");
}
if ($type == 'pre_parse') {
$query = $args['query'];
$args = $args['args'];
foreach ($this->hooks[$type] as $hook) {
$result = call_user_func($hook, array('query' => $query, 'args' => $args));
if (is_null($result)) {
$result = array($query, $args);
}
if (!is_array($result) || count($result) != 2) {
throw new MeekroDBException("pre_parse hook must return an array of 2 items");
}
if (!is_string($result[0])) {
throw new MeekroDBException("pre_parse hook must return a string as its first item");
}
if (!is_array($result[1])) {
throw new MeekroDBException("pre_parse hook must return an array as its second item");
}
$query = $result[0];
$args = $result[1];
}
return array($query, $args);
}
else if ($type == 'pre_run') {
$query = $args['query'];
foreach ($this->hooks[$type] as $hook) {
$result = call_user_func($hook, array('query' => $query));
if (is_null($result)) $result = $query;
if (!is_string($result)) throw new MeekroDBException("pre_run hook must return a string");
$query = $result;
}
return $query;
}
else if ($type == 'post_run') {
foreach ($this->hooks[$type] as $hook) {
call_user_func($hook, $args);
}
}
else if ($type == 'run_success') {
foreach ($this->hooks[$type] as $hook) {
call_user_func($hook, $args);
}
}
else if ($type == 'run_failed') {
foreach ($this->hooks[$type] as $hook) {
$result = call_user_func($hook, $args);
if ($result === false) return false;
}
}
else {
throw new MeekroDBException("runHook() type $type not recognized");
}
}
protected function defaultRunHook($args) {
if (! $this->logfile) return;
$query = $args['query'];
$query = preg_replace('/\s+/', ' ', $query);
$query = preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $query);
$results[] = sprintf('[%s]', date('Y-m-d H:i:s'));
$results[] = sprintf('QUERY: %s', $query);
$results[] = sprintf('RUNTIME: %s ms', $args['runtime']);
if (isset($args['affected']) && $args['affected']) {
$results[] = sprintf('AFFECTED ROWS: %s', $args['affected']);
}
if (isset($args['rows']) && $args['rows']) {
$results[] = sprintf('RETURNED ROWS: %s', $args['rows']);
}
if (isset($args['error'])) {
$results[] = 'ERROR: ' . $args['error'];
}
$error_handler = is_callable($this->nonsql_error_handler) ? $this->nonsql_error_handler : 'meekrodb_error_handler';
call_user_func($error_handler, array(
'type' => 'nonsql',
'error' => $message
));
}
public function debugMode($handler = true) {
$this->success_handler = $handler;
$results = implode("\n", $results) . "\n\n";
if (is_resource($this->logfile)) {
fwrite($this->logfile, $results);
} else {
file_put_contents($this->logfile, $results, FILE_APPEND);
}
}
public function serverVersion() { $this->get(); return $this->server_info; }
public function transactionDepth() { return $this->nested_transactions_count; }
public function insertId() { return $this->insert_id; }
public function affectedRows() { return $this->affected_rows; }
public function count() { $args = func_get_args(); return call_user_func_array(array($this, 'numRows'), $args); }
public function count() { return call_user_func_array(array($this, 'numRows'), func_get_args()); }
public function numRows() { return $this->num_rows; }
public function lastQuery() { return $this->last_query; }
public function useDB() { $args = func_get_args(); return call_user_func_array(array($this, 'setDB'), $args); }
public function useDB() { return call_user_func_array(array($this, 'setDB'), func_get_args()); }
public function setDB($dbName) {
$db = $this->get();
if (! $db->select_db($dbName)) $this->nonSQLError("Unable to set database to $dbName");
if (! $db->select_db($dbName)) throw new MeekroDBException("Unable to set database to $dbName");
$this->current_db = $dbName;
}
public function startTransaction() {
if ($this->nested_transactions && $this->serverVersion() < '5.5') {
return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion());
throw new MeekroDBException("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion());
}
if (!$this->nested_transactions || $this->nested_transactions_count == 0) {
@@ -255,7 +340,7 @@ class MeekroDB {
public function commit($all=false) {
if ($this->nested_transactions && $this->serverVersion() < '5.5') {
return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion());
throw new MeekroDBException("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion());
}
if ($this->nested_transactions && $this->nested_transactions_count > 0)
@@ -273,7 +358,7 @@ class MeekroDB {
public function rollback($all=false) {
if ($this->nested_transactions && $this->serverVersion() < '5.5') {
return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion());
throw new MeekroDBException("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion());
}
if ($this->nested_transactions && $this->nested_transactions_count > 0)
@@ -289,7 +374,7 @@ class MeekroDB {
return $this->nested_transactions_count;
}
protected function formatTableName($table) {
function formatTableName($table) {
$table = trim($table, '`');
if (strpos($table, '.')) return implode('.', array_map(array($this, 'formatTableName'), explode('.', $table)));
@@ -300,14 +385,17 @@ class MeekroDB {
$args = func_get_args();
$table = array_shift($args);
$params = array_shift($args);
$where = array_shift($args);
$query = str_replace('%', $this->param_char, "UPDATE %b SET %? WHERE ") . $where;
array_unshift($args, $params);
array_unshift($args, $table);
array_unshift($args, $query);
return call_user_func_array(array($this, 'query'), $args);
$update_part = $this->parse(
str_replace('%', $this->param_char, "UPDATE %b SET %hc"),
$table, $params
);
// we don't know if they used named or numbered args, so the where clause
// must be run through the parser separately
$where_part = call_user_func_array(array($this, 'parse'), $args);
$query = $update_part . ' WHERE ' . $where_part;
return $this->query($query);
}
public function insertOrReplace($which, $table, $datas, $options=array()) {
@@ -315,6 +403,7 @@ class MeekroDB {
$keys = $values = array();
if (isset($datas[0]) && is_array($datas[0])) {
$var = '%ll?';
foreach ($datas as $datum) {
ksort($datum);
if (! $keys) $keys = array_keys($datum);
@@ -322,21 +411,24 @@ class MeekroDB {
}
} else {
$var = '%l?';
$keys = array_keys($datas);
$values = array_values($datas);
}
if ($which != 'INSERT' && $which != 'INSERT IGNORE' && $which != 'REPLACE') {
throw new MeekroDBException('insertOrReplace() must be called with one of: INSERT, INSERT IGNORE, REPLACE');
}
if (isset($options['ignore']) && $options['ignore']) $which = 'INSERT IGNORE';
if (isset($options['update']) && is_array($options['update']) && $options['update'] && strtolower($which) == 'insert') {
if (isset($options['update']) && is_array($options['update']) && $options['update'] && $which == 'INSERT') {
if (array_values($options['update']) !== $options['update']) {
return $this->query(
str_replace('%', $this->param_char, "INSERT INTO %b %lb VALUES %? ON DUPLICATE KEY UPDATE %?"),
str_replace('%', $this->param_char, "INSERT INTO %b %lb VALUES $var ON DUPLICATE KEY UPDATE %hc"),
$table, $keys, $values, $options['update']);
} else {
$update_str = array_shift($options['update']);
$query_param = array(
str_replace('%', $this->param_char, "INSERT INTO %b %lb VALUES %? ON DUPLICATE KEY UPDATE ") . $update_str,
str_replace('%', $this->param_char, "INSERT INTO %b %lb VALUES $var ON DUPLICATE KEY UPDATE ") . $update_str,
$table, $keys, $values);
$query_param = array_merge($query_param, $options['update']);
return call_user_func_array(array($this, 'query'), $query_param);
@@ -345,12 +437,12 @@ class MeekroDB {
}
return $this->query(
str_replace('%', $this->param_char, "%l INTO %b %lb VALUES %?"),
str_replace('%', $this->param_char, "%l INTO %b %lb VALUES $var"),
$which, $table, $keys, $values);
}
public function insert($table, $data) { return $this->insertOrReplace('INSERT', $table, $data); }
public function insertIgnore($table, $data) { return $this->insertOrReplace('INSERT', $table, $data, array('ignore' => true)); }
public function insertIgnore($table, $data) { return $this->insertOrReplace('INSERT IGNORE', $table, $data); }
public function replace($table, $data) { return $this->insertOrReplace('REPLACE', $table, $data); }
public function insertUpdate() {
@@ -360,7 +452,7 @@ class MeekroDB {
if (! isset($args[0])) { // update will have all the data of the insert
if (isset($data[0]) && is_array($data[0])) { //multiple insert rows specified -- failing!
$this->nonSQLError("Badly formatted insertUpdate() query -- you didn't specify the update component!");
throw new MeekroDBException("Badly formatted insertUpdate() query -- you didn't specify the update component!");
}
$args[0] = $data;
@@ -375,20 +467,32 @@ class MeekroDB {
public function delete() {
$args = func_get_args();
$table = $this->formatTableName(array_shift($args));
$where = array_shift($args);
$buildquery = "DELETE FROM $table WHERE $where";
array_unshift($args, $buildquery);
return call_user_func_array(array($this, 'query'), $args);
$where = call_user_func_array(array($this, 'parse'), $args);
$query = "DELETE FROM {$table} WHERE {$where}";
return $this->query($query);
}
public function sqleval() {
$args = func_get_args();
$text = call_user_func_array(array($this, 'parseQueryParams'), $args);
$text = call_user_func_array(array($this, 'parse'), $args);
return new MeekroDBEval($text);
}
public function columnList($table) {
return $this->queryOneColumn('Field', "SHOW COLUMNS FROM %b", $table);
$data = $this->query("SHOW COLUMNS FROM %b", $table);
$columns = array();
foreach ($data as $row) {
$columns[$row['Field']] = array(
'type' => $row['Type'],
'null' => $row['Null'],
'key' => $row['Type'],
'default' => $row['Default'],
'extra' => $row['Extra']
);
}
return $columns;
}
public function tableList($db = null) {
@@ -401,269 +505,293 @@ class MeekroDB {
if (isset($olddb)) $this->useDB($olddb);
return $result;
}
protected function preparseQueryParams() {
$args = func_get_args();
$sql = trim(strval(array_shift($args)));
$args_all = $args;
if (count($args_all) == 0) return array($sql);
$param_char_length = strlen($this->param_char);
$named_seperator_length = strlen($this->named_param_seperator);
$types = array(
$this->param_char . 'll', // list of literals
$this->param_char . 'ls', // list of strings
$this->param_char . 'l', // literal
$this->param_char . 'li', // list of integers
$this->param_char . 'ld', // list of decimals
$this->param_char . 'lb', // list of backticks
$this->param_char . 'lt', // list of timestamps
$this->param_char . 's', // string
$this->param_char . 'i', // integer
$this->param_char . 'd', // double / decimal
$this->param_char . 'b', // backtick
$this->param_char . 't', // timestamp
$this->param_char . '?', // infer type
$this->param_char . 'ss' // search string (like string, surrounded with %'s)
protected function paramsMap() {
$t = $this;
return array(
's' => function($arg) use ($t) { return $t->escape($arg); },
'i' => function($arg) use ($t) { return $t->intval($arg); },
'd' => function($arg) use ($t) { return doubleval($arg); },
'b' => function($arg) use ($t) { return $t->formatTableName($arg); },
'l' => function($arg) use ($t) { return strval($arg); },
't' => function($arg) use ($t) { return $t->escapeTS($arg); },
'ss' => function($arg) use ($t) { return $t->escape("%" . str_replace(array('%', '_'), array('\%', '\_'), $arg) . "%"); },
'ls' => function($arg) use ($t) { return array_map(array($t, 'escape'), $arg); },
'li' => function($arg) use ($t) { return array_map(array($t, 'intval'), $arg); },
'ld' => function($arg) use ($t) { return array_map('doubleval', $arg); },
'lb' => function($arg) use ($t) { return array_map(array($t, 'formatTableName'), $arg); },
'll' => function($arg) use ($t) { return array_map('strval', $arg); },
'lt' => function($arg) use ($t) { return array_map(array($t, 'escapeTS'), $arg); },
'?' => function($arg) use ($t) { return $t->sanitize($arg); },
'l?' => function($arg) use ($t) { return $t->sanitize($arg, 'list'); },
'll?' => function($arg) use ($t) { return $t->sanitize($arg, 'doublelist'); },
'hc' => function($arg) use ($t) { return $t->sanitize($arg, 'hash'); },
'ha' => function($arg) use ($t) { return $t->sanitize($arg, 'hash', ' AND '); },
'ho' => function($arg) use ($t) { return $t->sanitize($arg, 'hash', ' OR '); },
$this->param_char => function($arg) use ($t) { return $t->param_char; },
);
}
protected function nextQueryParam($query) {
$keys = array_keys($this->paramsMap());
$first_position = PHP_INT_MAX;
$first_param = null;
$first_type = null;
$arg = null;
$named_arg = null;
foreach ($keys as $key) {
$fullkey = $this->param_char . $key;
$pos = strpos($query, $fullkey);
if ($pos === false) continue;
if ($pos <= $first_position) {
$first_position = $pos;
$first_param = $fullkey;
$first_type = $key;
}
}
if (is_null($first_param)) return;
$first_position_end = $first_position + strlen($first_param);
$named_seperator_length = strlen($this->named_param_seperator);
$arg_mask = '0123456789';
$named_arg_mask = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
// generate list of all MeekroDB variables in our query, and their position
// in the form "offset => variable", sorted by offsets
$posList = array();
foreach ($types as $type) {
$lastPos = 0;
while (($pos = strpos($sql, $type, $lastPos)) !== false) {
$lastPos = $pos + 1;
if (isset($posList[$pos]) && strlen($posList[$pos]) > strlen($type)) continue;
$posList[$pos] = $type;
if ($arg_number_length = strspn($query, $arg_mask, $first_position_end)) {
$arg = intval(substr($query, $first_position_end, $arg_number_length));
$first_param = substr($query, $first_position, strlen($first_param) + $arg_number_length);
}
else if (substr($query, $first_position_end, $named_seperator_length) == $this->named_param_seperator) {
$named_arg_length = strspn($query, $named_arg_mask, $first_position_end + $named_seperator_length);
if ($named_arg_length > 0) {
$named_arg = substr($query, $first_position_end + $named_seperator_length, $named_arg_length);
$first_param = substr($query, $first_position, strlen($first_param) + $named_seperator_length + $named_arg_length);
}
}
return array(
'param' => $first_param,
'type' => $first_type,
'pos' => $first_position,
'arg' => $arg,
'named_arg' => $named_arg,
);
}
protected function preParse($query, $args) {
$arg_ct = 0;
$max_numbered_arg = 0;
$use_numbered_args = false;
$use_named_args = false;
$queryParts = array();
while ($Param = $this->nextQueryParam($query)) {
if ($Param['pos'] > 0) {
$queryParts[] = substr($query, 0, $Param['pos']);
}
if ($Param['type'] != $this->param_char && is_null($Param['arg']) && is_null($Param['named_arg'])) {
$Param['arg'] = $arg_ct++;
}
if (! is_null($Param['arg'])) {
$use_numbered_args = true;
$max_numbered_arg = max($max_numbered_arg, $Param['arg']);
}
if (! is_null($Param['named_arg'])) {
$use_named_args = true;
}
$queryParts[] = $Param;
$query = substr($query, $Param['pos'] + strlen($Param['param']));
}
if (strlen($query) > 0) {
$queryParts[] = $query;
}
if ($use_named_args) {
if ($use_numbered_args) {
throw new MeekroDBException("You can't mix named and numbered args!");
}
if (count($args) != 1 || !is_array($args[0])) {
throw new MeekroDBException("If you use named args, you must pass an assoc array of args!");
}
}
if ($use_numbered_args) {
if ($max_numbered_arg+1 > count($args)) {
throw new MeekroDBException(sprintf('Expected %d args, but only got %d!', $max_numbered_arg+1, count($args)));
}
}
ksort($posList);
// for each MeekroDB variable, substitute it with array(type: i, value: 53) or whatever
$chunkyQuery = array(); // preparsed query
$pos_adj = 0; // how much we've added or removed from the original sql string
foreach ($posList as $pos => $type) {
$type = substr($type, $param_char_length); // variable, without % in front of it
$length_type = strlen($type) + $param_char_length; // length of variable w/o %
$new_pos = $pos + $pos_adj; // position of start of variable
$new_pos_back = $new_pos + $length_type; // position of end of variable
$arg_number_length = 0; // length of any named or numbered parameter addition
// handle numbered parameters
if ($arg_number_length = strspn($sql, '0123456789', $new_pos_back)) {
$arg_number = substr($sql, $new_pos_back, $arg_number_length);
if (! array_key_exists($arg_number, $args_all)) $this->nonSQLError("Non existent argument reference (arg $arg_number): $sql");
$arg = $args_all[$arg_number];
// handle named parameters
} else if (substr($sql, $new_pos_back, $named_seperator_length) == $this->named_param_seperator) {
$arg_number_length = strspn($sql, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_',
$new_pos_back + $named_seperator_length) + $named_seperator_length;
$arg_number = substr($sql, $new_pos_back + $named_seperator_length, $arg_number_length - $named_seperator_length);
if (count($args_all) != 1 || !is_array($args_all[0])) $this->nonSQLError("If you use named parameters, the second argument must be an array of parameters");
if (! array_key_exists($arg_number, $args_all[0])) $this->nonSQLError("Non existent argument reference (arg $arg_number): $sql");
$arg = $args_all[0][$arg_number];
} else {
$arg_number = 0;
$arg = array_shift($args);
return $queryParts;
}
function parse($query) {
$args = func_get_args();
array_shift($args);
$query = trim($query);
if (! $args) return $query;
$queryParts = $this->preParse($query, $args);
$array_types = array('ls', 'li', 'ld', 'lb', 'll', 'lt', 'l?', 'll?', 'hc', 'ha', 'ho');
$Map = $this->paramsMap();
$query = '';
foreach ($queryParts as $Part) {
if (is_string($Part)) {
$query .= $Part;
continue;
}
if ($new_pos > 0) $chunkyQuery[] = substr($sql, 0, $new_pos);
if (is_object($arg) && ($arg instanceof WhereClause)) {
list($clause_sql, $clause_args) = $arg->textAndArgs();
$fn = $Map[$Part['type']];
$is_array_type = in_array($Part['type'], $array_types, true);
$val = null;
if (!is_null($Part['named_arg'])) {
$key = $Part['named_arg'];
if (! array_key_exists($key, $args[0])) {
throw new MeekroDBException("Couldn't find named arg {$key}!");
}
$val = $args[0][$key];
}
else if (!is_null($Part['arg'])) {
$key = $Part['arg'];
$val = $args[$key];
}
if ($is_array_type && !is_array($val)) {
throw new MeekroDBException("Expected an array for arg $key but didn't get one!");
}
if ($is_array_type && count($val) == 0) {
throw new MeekroDBException("Arg {$key} array can't be empty!");
}
if (!$is_array_type && is_array($val)) {
$val = '';
}
if (is_object($val) && ($val instanceof WhereClause)) {
if ($Part['type'] != 'l') {
throw new MeekroDBException("WhereClause must be used with l arg, you used {$Part['type']} instead!");
}
list($clause_sql, $clause_args) = $val->textAndArgs();
array_unshift($clause_args, $clause_sql);
$preparsed_sql = call_user_func_array(array($this, 'preparseQueryParams'), $clause_args);
$chunkyQuery = array_merge($chunkyQuery, $preparsed_sql);
$result = call_user_func_array(array($this, 'parse'), $clause_args);
}
else {
$result = $fn($val);
if (is_array($result)) $result = '(' . implode(',', $result) . ')';
}
$query .= $result;
}
return $query;
}
public function escape($str) { return "'" . $this->get()->real_escape_string(strval($str)) . "'"; }
public function sanitize($value, $type='basic', $hashjoin=', ') {
if ($type == 'basic') {
if (is_object($value)) {
if ($value instanceof MeekroDBEval) return $value->text;
else if ($value instanceof DateTime) return $this->escape($value->format('Y-m-d H:i:s'));
else return $this->escape($value); // use __toString() value for objects, when possible
}
if (is_null($value)) return 'NULL';
else if (is_bool($value)) return ($value ? 1 : 0);
else if (is_int($value)) return $value;
else if (is_float($value)) return $value;
else if (is_array($value)) return "''";
else return $this->escape($value);
} else if ($type == 'list') {
if (is_array($value)) {
$value = array_values($value);
return '(' . implode(', ', array_map(array($this, 'sanitize'), $value)) . ')';
} else {
$chunkyQuery[] = array('type' => $type, 'value' => $arg);
throw new MeekroDBException("Expected array parameter, got something different!");
}
$sql = substr($sql, $new_pos_back + $arg_number_length);
$pos_adj -= $new_pos_back + $arg_number_length;
} else if ($type == 'doublelist') {
if (is_array($value) && array_values($value) === $value && is_array($value[0])) {
$cleanvalues = array();
foreach ($value as $subvalue) {
$cleanvalues[] = $this->sanitize($subvalue, 'list');
}
return implode(', ', $cleanvalues);
} else {
throw new MeekroDBException("Expected double array parameter, got something different!");
}
} else if ($type == 'hash') {
if (is_array($value)) {
$pairs = array();
foreach ($value as $k => $v) {
$pairs[] = $this->formatTableName($k) . '=' . $this->sanitize($v);
}
return implode($hashjoin, $pairs);
} else {
throw new MeekroDBException("Expected hash (associative array) parameter, got something different!");
}
} else {
throw new MeekroDBException("Invalid type passed to sanitize()!");
}
if (strlen($sql) > 0) $chunkyQuery[] = $sql;
return $chunkyQuery;
}
protected function escape($str) { return "'" . $this->get()->real_escape_string(strval($str)) . "'"; }
protected function sanitize($value) {
if (is_object($value)) {
if ($value instanceof MeekroDBEval) return $value->text;
else if ($value instanceof DateTime) return $this->escape($value->format('Y-m-d H:i:s'));
else return '';
function escapeTS($ts) {
if (is_string($ts)) {
$str = date('Y-m-d H:i:s', strtotime($ts));
}
if (is_null($value)) return $this->usenull ? 'NULL' : "''";
else if (is_bool($value)) return ($value ? 1 : 0);
else if (is_int($value)) return $value;
else if (is_float($value)) return $value;
else if (is_array($value)) {
// non-assoc array?
if (array_values($value) === $value) {
if (is_array($value[0])) return implode(', ', array_map(array($this, 'sanitize'), $value));
else return '(' . implode(', ', array_map(array($this, 'sanitize'), $value)) . ')';
}
$pairs = array();
foreach ($value as $k => $v) {
$pairs[] = $this->formatTableName($k) . '=' . $this->sanitize($v);
}
return implode(', ', $pairs);
else if (is_object($ts) && ($ts instanceof DateTime)) {
$str = $ts->format('Y-m-d H:i:s');
}
else return $this->escape($value);
return $this->escape($str);
}
protected function parseTS($ts) {
if (is_string($ts)) return date('Y-m-d H:i:s', strtotime($ts));
else if (is_object($ts) && ($ts instanceof DateTime)) return $ts->format('Y-m-d H:i:s');
}
protected function intval($var) {
function intval($var) {
if (PHP_INT_SIZE == 8) return intval($var);
return floor(doubleval($var));
}
protected function parseQueryParams() {
$args = func_get_args();
$chunkyQuery = call_user_func_array(array($this, 'preparseQueryParams'), $args);
$query = '';
$array_types = array('ls', 'li', 'ld', 'lb', 'll', 'lt');
foreach ($chunkyQuery as $chunk) {
if (is_string($chunk)) {
$query .= $chunk;
continue;
}
$type = $chunk['type'];
$arg = $chunk['value'];
$result = '';
if ($type != '?') {
$is_array_type = in_array($type, $array_types, true);
if ($is_array_type && !is_array($arg)) $this->nonSQLError("Badly formatted SQL query: Expected array, got scalar instead!");
else if (!$is_array_type && is_array($arg)) $this->nonSQLError("Badly formatted SQL query: Expected scalar, got array instead!");
}
if ($type == 's') $result = $this->escape($arg);
else if ($type == 'i') $result = $this->intval($arg);
else if ($type == 'd') $result = doubleval($arg);
else if ($type == 'b') $result = $this->formatTableName($arg);
else if ($type == 'l') $result = $arg;
else if ($type == 'ss') $result = $this->escape("%" . str_replace(array('%', '_'), array('\%', '\_'), $arg) . "%");
else if ($type == 't') $result = $this->escape($this->parseTS($arg));
else if ($type == 'ls') $result = array_map(array($this, 'escape'), $arg);
else if ($type == 'li') $result = array_map(array($this, 'intval'), $arg);
else if ($type == 'ld') $result = array_map('doubleval', $arg);
else if ($type == 'lb') $result = array_map(array($this, 'formatTableName'), $arg);
else if ($type == 'll') $result = $arg;
else if ($type == 'lt') $result = array_map(array($this, 'escape'), array_map(array($this, 'parseTS'), $arg));
else if ($type == '?') $result = $this->sanitize($arg);
else $this->nonSQLError("Badly formatted SQL query: Invalid MeekroDB param $type");
if (is_array($result)) $result = '(' . implode(',', $result) . ')';
$query .= $result;
}
return $query;
}
public function query() { return $this->queryHelper(array('assoc' => true), func_get_args()); }
public function queryAllLists() { return $this->queryHelper(array(), func_get_args()); }
public function queryFullColumns() { return $this->queryHelper(array('fullcols' => true), func_get_args()); }
public function queryWalk() { return $this->queryHelper(array('walk' => true), func_get_args()); }
protected function prependCall($function, $args, $prepend) { array_unshift($args, $prepend); return call_user_func_array($function, $args); }
public function query() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'assoc'); }
public function queryAllLists() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'list'); }
public function queryFullColumns() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'full'); }
protected function queryHelper($opts, $args) {
$query = array_shift($args);
public function queryRaw() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'raw_buf'); }
public function queryRawUnbuf() { $args = func_get_args(); return $this->prependCall(array($this, 'queryHelper'), $args, 'raw_unbuf'); }
protected function queryHelper() {
$args = func_get_args();
$type = array_shift($args);
$opts_fullcols = (isset($opts['fullcols']) && $opts['fullcols']);
$opts_raw = (isset($opts['raw']) && $opts['raw']);
$opts_assoc = (isset($opts['assoc']) && $opts['assoc']);
$opts_walk = (isset($opts['walk']) && $opts['walk']);
$is_buffered = !($opts_raw || $opts_walk);
list($query, $args) = $this->runHook('pre_parse', array('query' => $query, 'args' => $args));
$sql = call_user_func_array(array($this, 'parse'), array_merge(array($query), $args));
$sql = $this->runHook('pre_run', array('query' => $sql));
$this->last_query = $sql;
$db = $this->get();
$is_buffered = true;
$row_type = 'assoc'; // assoc, list, raw
$full_names = false;
switch ($type) {
case 'assoc':
break;
case 'list':
$row_type = 'list';
break;
case 'full':
$row_type = 'list';
$full_names = true;
break;
case 'raw_buf':
$row_type = 'raw';
break;
case 'raw_unbuf':
$is_buffered = false;
$row_type = 'raw';
break;
default:
$this->nonSQLError('Error -- invalid argument to queryHelper!');
}
$sql = call_user_func_array(array($this, 'parseQueryParams'), $args);
if ($this->success_handler) $starttime = microtime(true);
$starttime = microtime(true);
$result = $db->query($sql, $is_buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT);
if ($this->success_handler) $runtime = microtime(true) - $starttime;
else $runtime = 0;
// ----- BEGIN ERROR HANDLING
if (!$sql || $db->error) {
if ($this->error_handler) {
$error_handler = is_callable($this->error_handler) ? $this->error_handler : 'meekrodb_error_handler';
call_user_func($error_handler, array(
'type' => 'sql',
'query' => $sql,
'error' => $db->error,
'code' => $db->errno
));
}
echo $sql."<br>\n";
if ($this->throw_exception_on_error) {
$e = new MeekroDBException($db->error, $sql, $db->errno);
throw $e;
}
} else if ($this->success_handler) {
$runtime = sprintf('%f', $runtime * 1000);
$success_handler = is_callable($this->success_handler) ? $this->success_handler : 'meekrodb_debugmode_handler';
call_user_func($success_handler, array(
'query' => $sql,
'runtime' => $runtime,
'affected' => $db->affected_rows
));
}
// ----- END ERROR HANDLING
$runtime = microtime(true) - $starttime;
$runtime = sprintf('%f', $runtime * 1000);
$this->insert_id = $db->insert_id;
$this->affected_rows = $db->affected_rows;
@@ -672,11 +800,38 @@ class MeekroDB {
if ($is_buffered && ($result instanceof MySQLi_Result)) $this->num_rows = $result->num_rows;
else $this->num_rows = null;
if ($row_type == 'raw' || !($result instanceof MySQLi_Result)) return $result;
$Exception = null;
if ($db->error) {
$Exception = new MeekroDBException($db->error, $sql, $db->errno);
}
$hookHash = array('query' => $sql, 'runtime' => $runtime);
if ($Exception) {
$hookHash['exception'] = $Exception;
$hookHash['error'] = $Exception->getMessage();
} else if ($this->num_rows) {
$hookHash['rows'] = $this->num_rows;
} else {
$hookHash['affected'] = $db->affected_rows;
}
$this->defaultRunHook($hookHash);
$this->runHook('post_run', $hookHash);
if ($Exception) {
$result = $this->runHook('run_failed', $hookHash);
if ($result !== false) throw $Exception;
}
else {
$this->runHook('run_success', $hookHash);
}
if ($opts_walk) return new MeekroDBWalk($db, $result);
if (!($result instanceof MySQLi_Result)) return $result; // query was not a SELECT?
if ($opts_raw) return $result;
$return = array();
if ($full_names) {
if ($opts_fullcols) {
$infos = array();
foreach ($result->fetch_fields() as $info) {
if (strlen($info->table)) $infos[] = $info->table . '.' . $info->name;
@@ -684,8 +839,8 @@ class MeekroDB {
}
}
while ($row = ($row_type == 'assoc' ? $result->fetch_assoc() : $result->fetch_row())) {
if ($full_names) $row = array_combine($infos, $row);
while ($row = ($opts_assoc ? $result->fetch_assoc() : $result->fetch_row())) {
if ($opts_fullcols) $row = array_combine($infos, $row);
$return[] = $row;
}
@@ -699,7 +854,7 @@ class MeekroDB {
return $return;
}
public function queryOneRow() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstRow'), $args); }
public function queryFirstRow() {
$args = func_get_args();
$result = call_user_func_array(array($this, 'query'), $args);
@@ -707,7 +862,7 @@ class MeekroDB {
return reset($result);
}
public function queryOneList() { $args = func_get_args(); return call_user_func_array(array($this, 'queryFirstList'), $args); }
public function queryFirstList() {
$args = func_get_args();
$result = call_user_func_array(array($this, 'queryAllLists'), $args);
@@ -729,6 +884,38 @@ class MeekroDB {
return $ret;
}
public function queryFirstField() {
$args = func_get_args();
$row = call_user_func_array(array($this, 'queryFirstList'), $args);
if ($row == null) return null;
return $row[0];
}
// --- begin deprecated methods (kept for backwards compatability)
public function debugMode($enable=true) {
if ($enable) $this->logfile = fopen('php://output', 'w');
else $this->logfile = null;
}
public function queryRaw() { return $this->queryHelper(array('raw' => true), func_get_args()); }
public function queryOneList() { return call_user_func_array(array($this, 'queryFirstList'), func_get_args()); }
public function queryOneRow() { return call_user_func_array(array($this, 'queryFirstRow'), func_get_args()); }
public function queryOneField() {
$args = func_get_args();
$column = array_shift($args);
$row = call_user_func_array(array($this, 'queryOneRow'), $args);
if ($row == null) {
return null;
} else if ($column === null) {
$keys = array_keys($row);
$column = $keys[0];
}
return $row[$column];
}
public function queryOneColumn() {
$args = func_get_args();
$column = array_shift($args);
@@ -747,27 +934,39 @@ class MeekroDB {
return $ret;
}
public function queryFirstField() {
$args = func_get_args();
$row = call_user_func_array(array($this, 'queryFirstList'), $args);
if ($row == null) return null;
return $row[0];
}
class MeekroDBWalk {
protected $mysqli;
protected $result;
function __construct(MySQLi $mysqli, $result) {
$this->mysqli = $mysqli;
$this->result = $result;
}
public function queryOneField() {
$args = func_get_args();
$column = array_shift($args);
$row = call_user_func_array(array($this, 'queryOneRow'), $args);
if ($row == null) {
return null;
} else if ($column === null) {
$keys = array_keys($row);
$column = $keys[0];
}
return $row[$column];
function next() {
// $result can be non-object if the query was not a SELECT
if (! ($this->result instanceof MySQLi_Result)) return;
if ($row = $this->result->fetch_assoc()) return $row;
else $this->free();
}
function free() {
if (! ($this->result instanceof MySQLi_Result)) return;
$this->result->free();
while ($this->mysqli->more_results()) {
$this->mysqli->next_result();
if ($result = $this->mysqli->use_result()) $result->free();
}
$this->result = null;
}
function __destruct() {
$this->free();
}
}
@@ -778,7 +977,7 @@ class WhereClause {
function __construct($type) {
$type = strtolower($type);
if ($type !== 'or' && $type !== 'and') DB::nonSQLError('you must use either WhereClause(and) or WhereClause(or)');
if ($type !== 'or' && $type !== 'and') throw new MeekroDBException('you must use either WhereClause(and) or WhereClause(or)');
$this->type = $type;
}
@@ -836,16 +1035,12 @@ class WhereClause {
$args = array_merge($args, $clause_args);
}
if ($this->type == 'and') $sql = implode(' AND ', $sql);
else $sql = implode(' OR ', $sql);
if ($this->type == 'and') $sql = sprintf('(%s)', implode(' AND ', $sql));
else $sql = sprintf('(%s)', implode(' OR ', $sql));
if ($this->negate) $sql = '(NOT ' . $sql . ')';
return array($sql, $args);
}
// backwards compatability
// we now return full WhereClause object here and evaluate it in preparseQueryParams
function text() { return $this; }
}
class DBTransaction {
@@ -871,85 +1066,12 @@ class MeekroDBException extends Exception {
function __construct($message='', $query='', $code = 0) {
parent::__construct($message);
$this->query = $query;
$this->code = $code;
$this->code = $code;
}
public function getQuery() { return $this->query; }
}
class DBHelper {
/*
verticalSlice
1. For an array of assoc rays, return an array of values for a particular key
2. if $keyfield is given, same as above but use that hash key as the key in new array
*/
public static function verticalSlice($array, $field, $keyfield = null) {
$array = (array) $array;
$R = array();
foreach ($array as $obj) {
if (! array_key_exists($field, $obj)) die("verticalSlice: array doesn't have requested field\n");
if ($keyfield) {
if (! array_key_exists($keyfield, $obj)) die("verticalSlice: array doesn't have requested field\n");
$R[$obj[$keyfield]] = $obj[$field];
} else {
$R[] = $obj[$field];
}
}
return $R;
}
/*
reIndex
For an array of assoc rays, return a new array of assoc rays using a certain field for keys
*/
public static function reIndex() {
$fields = func_get_args();
$array = array_shift($fields);
$array = (array) $array;
$R = array();
foreach ($array as $obj) {
$target =& $R;
foreach ($fields as $field) {
if (! array_key_exists($field, $obj)) die("reIndex: array doesn't have requested field\n");
$nextkey = $obj[$field];
$target =& $target[$nextkey];
}
$target = $obj;
}
return $R;
}
}
function meekrodb_error_handler($params) {
if (isset($params['query'])) $out[] = "QUERY: " . $params['query'];
if (isset($params['error'])) $out[] = "ERROR: " . $params['error'];
$out[] = "";
if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
echo implode("\n", $out);
} else {
echo implode("<br>\n", $out);
}
die;
}
function meekrodb_debugmode_handler($params) {
echo "QUERY: " . $params['query'] . " [" . $params['runtime'] . " ms]";
if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
echo "\n";
} else {
echo "<br>\n";
}
}
class MeekroDBEval {
public $text = '';