From f0a5b97f3c7b754f3058a56ae8be0883a69b5c29 Mon Sep 17 00:00:00 2001 From: kjmtsh Date: Tue, 18 Mar 2014 10:06:36 +0000 Subject: [PATCH] bug fixes git-svn-id: https://plugins.svn.wordpress.org/sqlite-integration/trunk@877068 b8457f37-d9ea-0310-8a92-e5e31aec5664 --- pdoengine.class.php | 28 +++++++++++++++++++++++----- query.class.php | 41 +++++++++++++++++++++++++++++++---------- readme-ja.txt | 5 +++-- readme.txt | 5 +++-- utilities/utility.php | 3 ++- 5 files changed, 62 insertions(+), 20 deletions(-) diff --git a/pdoengine.class.php b/pdoengine.class.php index f636b4f..95fb346 100644 --- a/pdoengine.class.php +++ b/pdoengine.class.php @@ -278,6 +278,23 @@ class PDOEngine extends PDO { /** * Function to execute query(). * + * Divide the query types into seven different ones. That is to say: + * + * 1. SELECT SQL_CALC_FOUND_ROWS + * 2. INSERT + * 3. CREATE TABLE(INDEX) + * 4. ALTER TABLE + * 5. SHOW VARIABLES + * 6. DROP INDEX + * 7. THE OTHERS + * + * #1 is just a tricky play. See the private function handle_sql_count() in query.class.php. + * From #2 through #5 call different functions respectively. + * #6 call the ALTER TABLE query. + * #7 is a normal process: sequentially call prepare_query() and execute_query(). + * + * #1 process has been changed since version 1.5.1. + * * @param $query full SQL statement string * @return mixed according to the query type * @see PDO::query() @@ -287,7 +304,7 @@ class PDOEngine extends PDO { $this->queries[] = "Raw query:\n$query"; $res = $this->determine_query_type($query); - if (!$res) { + if (!$res && defined(PDO_DEBUG) && PDO_DEBUG) { $bailoutString = sprintf(__("

Unknown query type

Sorry, we cannot determine the type of query that is requested.

The query is %s

", 'sqlite-integration'), $query); $this->set_error(__LINE__, __FUNCTION__, $bailoutString); } @@ -296,10 +313,11 @@ class PDOEngine extends PDO { $_column = array('FOUND_ROWS()' => ''); $column = array(); if (!is_null($this->found_rows_result)) { - $this->num_rows = count($this->found_rows_result); - foreach ($this->found_rows_result[0] as $key => $value) { - $_column['FOUND_ROWS()'] = $value; - } + $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; +// } $column[] = new ObjectArray($_column); $this->results = $column; $this->found_rows_result = null; diff --git a/query.class.php b/query.class.php index c5d81ab..0982f5e 100644 --- a/query.class.php +++ b/query.class.php @@ -176,14 +176,12 @@ class PDOSQLiteDriver { * * This is a kind of tricky play. * 1. remove SQL_CALC_FOUND_ROWS option, and give it to the pdo engine - * 2. make another $wpdb instance, and execute SELECT COUNT(*) query - * 3. give the returned value to the original instance variable without LIMIT + * 2. make another $wpdb instance, and execute the rewritten query + * 3. give the returned value (integer: number of the rows) to the original instance variable without LIMIT * - * When SQL statement contains GROUP BY option, SELECT COUNT query doesn't - * go well. So we remove the GROUP BY, which means the returned value may - * be a approximate one. + * We no longer use SELECT COUNT query, because it returns the inexact values when used with WP_Meta_Query(). * - * This kind of statement is required for WordPress to calculate the paging. + * This kind of statement is required for WordPress to calculate the paging information. * see also WP_Query class in wp-includes/query.php * * @access private @@ -196,17 +194,18 @@ class PDOSQLiteDriver { // we make the data for next SELECE FOUND_ROWS() statement $unlimited_query = preg_replace('/\\bLIMIT\\s*.*/imsx', '', $this->_query); $unlimited_query = preg_replace('/\\bGROUP\\s*BY\\s*.*/imsx', '', $unlimited_query); - $unlimited_query = $this->_transform_to_count($unlimited_query); + // we no longer use SELECT COUNT query +// $unlimited_query = $this->_transform_to_count($unlimited_query); $_wpdb = new PDODB(); $result = $_wpdb->query($unlimited_query); - $wpdb->dbh->found_rows_result = $_wpdb->last_result; + $wpdb->dbh->found_rows_result = $result; $_wpdb = null; } } /** * Call back method to change SELECT query to SELECT COUNT(). * - * revised for version 1.5.1 + * obsolite since version 1.5.1 * * @param string $query the query to be transformed * @return string the transformed query @@ -488,17 +487,39 @@ class PDOSQLiteDriver { * Method to handle ON DUPLICATE KEY UPDATE statement. * * First we use SELECT query and check if INSERT is allowed or not. - * Rewriting procedure looks like a detour, but I've got no other way. + * Rewriting procedure looks like a detour, but I've got no other ways. + * + * Added the literal check since the version 1.5.1. * * @return void * @access private */ private function execute_duplicate_key_update() { + $rewrite_flag = true; $update = false; $unique_keys_for_cond = array(); $unique_keys_for_check = array(); $pattern = '/^\\s*INSERT\\s*INTO\\s*(\\w+)?\\s*(.*)\\s*ON\\s*DUPLICATE\\s*KEY\\s*UPDATE\\s*(.*)$/ims'; if (preg_match($pattern, $this->_query, $match_0)) { + $tokens = preg_split("/(''|')/s", $this->_query, -1, PREG_SPLIT_DELIM_CAPTURE); + $literal = false; + foreach ($tokens as $token) { + if (strpos($token, "'") !== false) { + if ($literal) { + $literal = false; + } else { + $literal = true; + } + } else { + if ($literal === false && stripos($token, 'ON DUPLICATE KEY UPDATE') !== false) { + $rewrite_flag = true; + break; + } else { + $rewrite_flag = false; + } + } + } + if (!$rewrite_flag) return; $table_name = trim($match_0[1]); $insert_data = trim($match_0[2]); $update_data = trim($match_0[3]); diff --git a/readme-ja.txt b/readme-ja.txt index acb36f0..b4aebbc 100644 --- a/readme-ja.txt +++ b/readme-ja.txt @@ -160,7 +160,8 @@ query_posts() や WP_Query() を使うときに、オプションの一部が機 == Changelog == = 1.5.1 (2014-02-06) = -* SQL_CALC_FOUND_ROW ステートメントのバグを修正しました。メインクエリと WP_Query のページング情報に関連したものです。 +* 未対応のクエリに対するエラーメッセージのコントロールができていないのを修正しました。 +* SQL_CALC_FOUND_ROW ステートメントのバグを修正しました。メインクエリと WP_Query、WP_Meta_Query などのページング情報に関連したものです。 * コメント本文からバッククォートが削除されるバグを修正しました。 * バックアップファイルをローカルにダウンロードできるようにしました。 * PHP documentor で使えるように、ソースコードのドキュメントを書き直しました。 @@ -168,7 +169,7 @@ query_posts() や WP_Query() を使うときに、オプションの一部が機 * マイナーな変更、修正がいくつかありました。 * WordPress 3.8.1 と 3.9 alpha でインストールテストをしました。 * プラグイン互換リストを増補しました。 -* これまで使えなかった WordPress の関数が使えるようになりました。 +* これまで使えなくしていた wp-db.php の関数を使えるようにしました。 * いくつかのユーザ定義関数を追加しました。 = 1.5 (2013-12-17) = diff --git a/readme.txt b/readme.txt index 6a0390c..3022274 100644 --- a/readme.txt +++ b/readme.txt @@ -158,7 +158,8 @@ When query_posts() or WP_Query() is used, some options didn't work properly. Whe == Changelog == = 1.5.1 (2014-02-06) = -* Fixed the bug for 'SQL_CALC_FOUND_ROW' statement. This is for the main query and WP_Query concerning paging information. +* Fixed the bug of error messaging control for the unknown query. +* Fixed the bug for 'SQL_CALC_FOUND_ROW' statement. This is for the main query, WP_Query class and WP_Meta_Query concerning paging information. * Fixed the bug that the back quote in the comments was removed. * Added the feature to download a backup file to a local machine. * Revised all the doc strings in the sourcse code for PHP documentor. @@ -166,7 +167,7 @@ When query_posts() or WP_Query() is used, some options didn't work properly. Whe * Fixed minor bugs and typos. * Tested to install WordPress 3.8.1 and 3.9 beta. * Augumented the plugin compatibility list. -* Some functions in WordPress that can't be used are made to work. +* Some functions in wp-db.php, which was disabled by the plugin, is enabled and can be used. * Some more user defined functions are added. = 1.5 (2013-12-17) = diff --git a/utilities/utility.php b/utilities/utility.php index ff3088d..a99fd82 100644 --- a/utilities/utility.php +++ b/utilities/utility.php @@ -506,7 +506,8 @@ class SQLiteIntegrationUtils { if (count($file_names) != 1) return 2; $file_name = $file_names[0]; $file_path = FQDBDIR . $file_name; - $download_file_name = get_bloginfo('name') . '_' . $file_name; + $blog_name = str_replace(array(' ', ' ', ';'), array('_', '_', '_'), get_bloginfo('name')); + $download_file_name = $blog_name . '_' . $file_name; header('Pragma: public'); header('Cache-Control: must-revalidate,post-check=0,pre-check=0'); header('Content-Type: application/force-download');