change the algorithm for manipulating multiple meta query

git-svn-id: https://plugins.svn.wordpress.org/sqlite-integration/trunk@948014 b8457f37-d9ea-0310-8a92-e5e31aec5664
This commit is contained in:
kjmtsh 2014-07-14 08:46:32 +00:00
parent e5f495a745
commit 61e6960b40
16 changed files with 5853 additions and 5867 deletions

1
db.php
View File

@ -34,7 +34,6 @@ if (!defined('ABSPATH')) { // Oh, you are not WordPress!
if (defined('USE_MYSQL') && USE_MYSQL) return;
function pdo_log_error($message, $data = null) {
if (strpos($_SERVER['SCRIPT_NAME'], 'wp-admin') !== false) {
$admin_dir = '';
} else {

View File

@ -172,7 +172,6 @@ class PDOSQLiteUDFS {
//convert to ISO time
$date = date("Y-m-d H:i:s", $field);
//now submit to dateformat
return is_null($format) ? $date : $self->dateformat($date, $format);
}
/**
@ -342,7 +341,6 @@ class PDOSQLiteUDFS {
$type .= 's';
}
return "$_parts[0] $_parts[1]";
break;
case "minute_second":
list($minutes, $seconds) = explode (':', $_parts[0]);
$minutes = intval($minutes);
@ -350,8 +348,6 @@ class PDOSQLiteUDFS {
$minutes = ($minutes > 1) ? "$minutes minutes" : "$minutes minute";
$seconds = ($seconds > 1) ? "$seconds seconds" : "$seconds second";
return "$minutes $seconds";
break;
case "hour_second":
list($hours, $minutes, $seconds) = explode (':', $_parts[0]);
$hours = intval($hours);
@ -361,7 +357,6 @@ class PDOSQLiteUDFS {
$minutes = ($minutes > 1) ? "$minutes minutes" : "$minutes minute";
$seconds = ($seconds > 1) ? "$seconds seconds" : "$seconds second";
return "$hours $minutes $seconds";
break;
case "hour_minute":
list($hours, $minutes) = explode (':', $_parts[0]);
$hours = intval($hours);
@ -369,7 +364,6 @@ class PDOSQLiteUDFS {
$hours = ($hours > 1) ? "$hours hours" : "$hours hour";
$minutes = ($minutes > 1) ? "$minutes minutes" : "$minutes minute";
return "$hours $minutes";
break;
case "day_second":
$days = intval($_parts[0]);
list($hours, $minutes, $seconds) = explode (':', $_parts[1]);
@ -381,7 +375,6 @@ class PDOSQLiteUDFS {
$minutes = ($minutes > 1) ? "$minutes minutes" : "$minutes minute";
$seconds = ($seconds > 1) ? "$seconds seconds" : "$seconds second";
return "$days $hours $minutes $seconds";
break;
case "day_minute":
$days = intval($_parts[0]);
list($hours, $minutes) = explode (':', $_parts[1]);
@ -391,14 +384,12 @@ class PDOSQLiteUDFS {
$hours = ($hours > 1) ? "$hours hours" : "$hours hour";
$minutes = ($minutes > 1) ? "$minutes minutes" : "$minutes minute";
return "$days $hours $minutes";
break;
case "day_hour":
$days = intval($_parts[0]);
$hours = intval($_parts[1]);
$days = $days > 1 ? "$days days" : "$days day";
$hours = ($hours > 1) ? "$hours hours" : "$hours hour";
return "$days $hours";
break;
case "year_month":
list($years, $months) = explode ('-', $_parts[0]);
$years = intval($years);
@ -406,7 +397,6 @@ class PDOSQLiteUDFS {
$years = ($years > 1) ? "$years years" : "$years year";
$months = ($months > 1) ? "$months months": "$months month";
return "$years $months";
break;
default:
return false;
}
@ -707,8 +697,8 @@ class PDOSQLiteUDFS {
* @return string representing the version number: major_version.minor_version
*/
public function version() {
// global $required_mysql_version;
// return $required_mysql_version;
//global $required_mysql_version;
//return $required_mysql_version;
return '5.5';
}
}

View File

@ -342,34 +342,27 @@ class PDOSQLiteUDFS {
case "minute_second":
list($minutes, $seconds) = explode(':', $_parts[0]);
return 'PT' . $minutes . 'M' . $seconds . 'S';
break;
case "hour_second":
list($hours, $minutes, $seconds) = explode (':', $_parts[0]);
return 'PT' . $hours . 'H' . $minutes . 'M' . $seconds . 'S';
break;
case "hour_minute":
list($hours, $minutes) = explode (':', $_parts[0]);
return 'PT' . $hours . 'H' . $minutes . 'M';
break;
case "day_second":
$days = intval($_parts[0]);
list($hours, $minutes, $seconds) = explode (':', $_parts[1]);
return 'P' . $days . 'D' . 'T' . $hours . 'H' . $minutes . 'M' . $seconds . 'S';
break;
case "day_minute":
$days = intval($_parts[0]);
list($hours, $minutes) = explode(':', $parts[1]);
return 'P' . $days . 'D' . 'T' . $hours . 'H' . $minutes . 'M';
break;
case "day_hour":
$days = intval($_parts[0]);
$hours = intval($_parts[1]);
return 'P' . $days . 'D' . 'T' . $hours . 'H';
break;
case "year_month":
list($years, $months) = explode ('-', $_parts[0]);
return 'P' . $years . 'Y' . $months . 'M';
break;
}
}
/**
@ -590,7 +583,6 @@ class PDOSQLiteUDFS {
$unsigned_int_data = sprintf('%u', $address);
return $unsigned_int_data;
}
/**
* Method to emulate MySQL DATEDIFF() function.
*
@ -680,8 +672,8 @@ class PDOSQLiteUDFS {
* @return string representing the version number: major_version.minor_version
*/
public function version() {
// global $required_mysql_version;
// return $required_mysql_version;
//global $required_mysql_version;
//return $required_mysql_version;
return '5.5';
}
}

View File

@ -251,7 +251,7 @@ class PDODB extends wpdb {
}
if (preg_match('/^\\s*(create|alter|truncate|drop|optimize)\\s*/i', $query)) {
// $return_val = $this->result;
//$return_val = $this->result;
$return_val = $this->dbh->get_return_value();
} elseif (preg_match('/^\\s*(insert|delete|update|replace)\s/i', $query)) {
$this->rows_affected = $this->dbh->get_affected_rows();
@ -323,8 +323,8 @@ class PDODB extends wpdb {
* @see wpdb::db_version()
*/
function db_version() {
// global $required_mysql_version;
// return $required_mysql_version;
//global $required_mysql_version;
//return $required_mysql_version;
return '5.5';
}
}

View File

@ -327,9 +327,9 @@ class PDOEngine extends PDO {
if (!is_null($this->found_rows_result)) {
$this->num_rows = $this->found_rows_result;
$_column['FOUND_ROWS()'] = $this->num_rows;
// foreach ($this->found_rows_result[0] as $key => $value) {
// $_column['FOUND_ROWS()'] = $value;
// }
//foreach ($this->found_rows_result[0] as $key => $value) {
//$_column['FOUND_ROWS()'] = $value;
//}
$column[] = new ObjectArray($_column);
$this->results = $column;
$this->found_rows_result = null;
@ -495,7 +495,7 @@ class PDOEngine extends PDO {
}
$output = '<div style="clear:both">&nbsp;</div>';
if ($this->is_error === false){
// return $output;
//return $output;
return '';
}
$output .= "<div class=\"queries\" style=\"clear:both; margin_bottom:2px; border: red dotted thin;\">Queries made or created this session were<br/>\r\n\t<ol>\r\n";
@ -676,7 +676,7 @@ class PDOEngine extends PDO {
case 'desc':
case 'check':
case 'analyze':
// case "foundrows":
//case "foundrows":
$this->num_rows = count($this->_results);
$this->return_value = $this->num_rows;
break;
@ -926,7 +926,7 @@ class PDOEngine extends PDO {
$rewritten_query = $engine->rewrite_query($query);
$reason = 0;
$message = '';
// $queries = explode(";", $this->rewritten_query);
//$queries = explode(";", $this->rewritten_query);
try {
$this->beginTransaction();
foreach ($rewritten_query as $single_query) {

View File

@ -48,6 +48,12 @@ class PDOSQLiteDriver {
* @var boolean
*/
private $rewrite_between = false;
/**
* Variable to check how many times rewriting BETWEEN is needed.
*
* @var integer
*/
private $num_of_rewrite_between = 0;
/**
* Method to rewrite a query string for SQLite to execute.
*
@ -102,7 +108,7 @@ class PDOSQLiteDriver {
case 'update':
//$this->safe_strip_backticks();
$this->rewrite_update_ignore();
// $this->_rewrite_date_sub();
//$this->_rewrite_date_sub();
$this->rewrite_limit_usage();
$this->rewrite_order_by_usage();
$this->rewrite_regexp();
@ -184,6 +190,7 @@ class PDOSQLiteDriver {
}
if (stripos($token, 'BETWEEN') !== false) {
$this->rewrite_between = true;
$this->num_of_rewrite_between++;
}
}
}
@ -385,7 +392,6 @@ class PDOSQLiteDriver {
* @access private
*/
private function handle_describe_query(){
// $this->_query = "select 1=1";
$pattern = '/^\\s*(DESCRIBE|DESC)\\s*(.*)/i';
if (preg_match($pattern, $this->_query, $match)) {
$tablename = preg_replace('/[\';]/', '', $match[2]);
@ -429,7 +435,7 @@ class PDOSQLiteDriver {
if (stripos($opt->compile_option, 'ENABLE_UPDATE_DELETE_LIMIT') !== false) return;
}
if (stripos($this->_query, '(select') === false) {
$this->_query = preg_replace('/\\s*ORDER\\s*BY\\s*.*$/i', '', $this->_query);
$this->_query = preg_replace('/\\s+ORDER\\s+BY\\s*.*$/i', '', $this->_query);
}
}
/**
@ -619,7 +625,7 @@ class PDOSQLiteDriver {
$val = trim(array_shift($ins_data_array));
$ins_data_assoc[trim($col)] = $val;
}
// $ins_data_assoc = array_combine($col_array, $ins_array);
$ins_data_assoc = array_combine($col_array, $ins_array);
$condition = '';
foreach ($unique_keys_for_cond as $unique_key) {
if (strpos($unique_key, ',') !== false) {
@ -635,7 +641,7 @@ class PDOSQLiteDriver {
continue;
}
}
// $condition = rtrim($condition, ' AND ');
$condition = rtrim($condition, ' AND ');
} else {
$col = trim($unique_key);
if (isset($ins_data_assoc[$col])) {
@ -653,7 +659,6 @@ class PDOSQLiteDriver {
$this->_query = 'INSERT INTO '.$table_name.' '.$insert_data;
return;
} else {
// change (col, col...) values (data, data...) to array(col=>data, col=>data...)
if (preg_match('/^\((.*)\)\\s*VALUES\\s*\((.*)\)$/im', $insert_data, $match_2)) {
$col_array = explode(',', $match_2[1]);
$ins_array = explode(',', $match_2[2]);
@ -664,8 +669,6 @@ class PDOSQLiteDriver {
$ins_array_assoc[$col] = $val;
}
}
// change col = data, col = data to array(col=>data, col=>data)
// some plugins have semi-colon at the end of the query
$update_data = rtrim($update_data, ';');
$tmp_array = explode(',', $update_data);
foreach ($tmp_array as $pair) {
@ -674,7 +677,6 @@ class PDOSQLiteDriver {
$value = trim($value);
$update_array_assoc[$col] = $value;
}
// change array(col=>values(col)) to array(col=>data)
foreach ($update_array_assoc as $key => &$value) {
if (preg_match('/^VALUES\\s*\((.*)\)$/im', $value, $match_3)) {
$col = trim($match_3[1]);
@ -697,18 +699,11 @@ class PDOSQLiteDriver {
$update_strings = rtrim($update_strings, ',');
$unique_where = array_unique($where_array, SORT_REGULAR);
$where_string = ' WHERE ' . implode(' AND ', $unique_where);
// $where_string = ' WHERE ' . rtrim($where_string, ',');
$update_query = 'UPDATE ' . $table_name . ' SET ' . $update_strings . $where_string;
$this->_query = $update_query;
}
}
}
// else {
// $pattern = '/ ON DUPLICATE KEY UPDATE.*$/im';
// $replace_query = preg_replace($pattern, '', $this->_query);
// $replace_query = str_ireplace('INSERT ', 'INSERT OR REPLACE ', $replace_query);
// $this->_query = $replace_query;
// }
}
/**
* Method to rewrite BETWEEN A AND B clause.
@ -721,17 +716,17 @@ class PDOSQLiteDriver {
private function rewrite_between() {
if (!$this->rewrite_between) return;
$pattern = '/\\s*(CAST\([^\)]+?\)|[^\\s\(]*)?\\s*BETWEEN\\s*([^\\s]*)?\\s*AND\\s*([^\\s\)]*)?\\s*/ims';
do {
if (preg_match($pattern, $this->_query, $match)) {
$column_name = trim($match[1]);
$min_value = trim($match[2]);
$max_value = trim($match[3]);
$max_value = rtrim($max_value);
$replacement = " $column_name >= $min_value AND $column_name <= $max_value";
$replacement = " ($column_name >= $min_value AND $column_name <= $max_value)";
$this->_query = str_ireplace($match[0], $replacement, $this->_query);
$this->rewrite_between = false;
}
$this->parse_query();
$this->rewrite_between();
$this->num_of_rewrite_between--;
} while ($this->num_of_rewrite_between > 0);
}
/**
* Method to avoid DELETE with JOIN statement.
@ -746,11 +741,22 @@ class PDOSQLiteDriver {
*/
private function delete_workaround() {
global $wpdb;
// $pattern = "DELETE o1 FROM $wpdb->options AS o1 JOIN $wpdb->options AS o2 USING (option_name) WHERE o2.option_id > o1.option_id";
$pattern = "DELETE o1 FROM $wpdb->options AS o1 JOIN $wpdb->options AS o2";
$pattern2 = "DELETE a, b FROM $wpdb->sitemeta AS a, $wpdb->sitemeta AS b";
$rewritten = "DELETE FROM $wpdb->options WHERE option_id IN (SELECT MIN(option_id) FROM $wpdb->options GROUP BY option_name HAVING COUNT(*) > 1)";
if (stripos($this->_query, $pattern) !== false) {
$this->_query = $rewritten;
} else if (stripos($this->_query, $pattern2) !== false) {
$time = time();
$prep_query = "SELECT a.meta_id AS aid, b.meta_id AS bid FROM $wpdb->sitemeta AS a INNER JOIN $wpdb->sitemeta AS b ON a.meta_key='_site_transient_timeout_'||substr(b.meta_key, 17) WHERE b.meta_key='_site_transient_'||substr(a.meta_key, 25) AND a.meta_value < $time";
$_wpdb = new PDODB();
$ids = $_wpdb->get_results($prep_query);
foreach ($ids as $id) {
$ids_to_delete[] = $id->aid;
$ids_to_delete[] = $id->bid;
}
$rewritten = "DELETE FROM $wpdb->sitemeta WHERE meta_id IN (".implode(',', $ids_to_delete).")";
$this->_query = $rewritten;
}
}
/**

View File

@ -33,7 +33,7 @@ class AlterQuery {
$command = str_ireplace($match[0], '', $query);
$tmp_tokens['query_type'] = trim($match[1]);
$tmp_tokens['table_name'] = trim($match[2]);
// $command_array = $this->split_multiple($command);
//$command_array = $this->split_multiple($command);
$command_array = explode(',', $command);
$single_command = array_shift($command_array);
@ -48,7 +48,6 @@ class AlterQuery {
$this->_query = 'SELECT 1=1';
return $this->_query;
}
// foreach ($tokens as $token) {
$command_name = strtolower($tokens['command']);
switch ($command_name) {
case 'add column': case 'rename to': case 'add index': case 'drop index':
@ -72,7 +71,6 @@ class AlterQuery {
default:
break;
}
// }
if (!is_array($tmp_query)) {
$this->_query[] = $tmp_query;
} else {

View File

@ -337,7 +337,7 @@ class CreateQuery{
* @return array
* @access private
*/
private function post_process(){
private function post_process() {
$mainquery = $this->_query;
do{
$count = 0;

View File

@ -22,7 +22,7 @@ function make_db_sqlite() {
include_once ABSPATH . 'wp-admin/includes/schema.php';
$index_array = array();
// ob_end_clean();
//ob_end_clean();
$table_schemas = wp_get_db_schema();
$queries = explode (";", $table_schemas);
$query_parser = new CreateQuery();
@ -47,10 +47,10 @@ function make_db_sqlite() {
$index_queries = $rewritten_query;
$table_query = trim($table_query);
$pdo->exec($table_query);
// foreach($rewritten_query as $single_query) {
// $single_query = trim($single_query);
// $pdo->exec($single_query);
// }
//foreach($rewritten_query as $single_query) {
// $single_query = trim($single_query);
// $pdo->exec($single_query);
//}
} else {
$rewritten_query = trim($rewritten_query);
$pdo->exec($rewritten_query);

View File

@ -109,6 +109,7 @@ class SQLiteIntegration {
register_activation_hook(__FILE__, array($this, 'install'));
}
if (function_exists('register_deactivation_hook')) {
;
}
if (function_exists('register_uninstall_hook')) {
register_uninstall_hook(__FILE__, array('SQLiteIntegration', 'uninstall'));
@ -268,11 +269,11 @@ class SQLiteIntegration {
*/
function textdomain_init() {
global $utils;
// $current_locale = get_locale();
// if (!empty($current_locale)) {
// $moFile = dirname(__FILE__) . "/languages/sqlite-wordpress-" . $current_locale . ".mo";
// if(@file_exists($moFile) && is_readable($moFile)) load_textdomain('sqlite-wordpress', $moFile);
// }
//$current_locale = get_locale();
//if (!empty($current_locale)) {
// $moFile = dirname(__FILE__) . "/languages/sqlite-wordpress-" . $current_locale . ".mo";
// if(@file_exists($moFile) && is_readable($moFile)) load_textdomain('sqlite-wordpress', $moFile);
//}
load_plugin_textdomain($utils->text_domain, false, SQLiteDir.'/languages/');
}