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;
}

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.
*

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.
*
@ -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

@ -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

@ -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'));