changed for Mac environment and some others

git-svn-id: https://plugins.svn.wordpress.org/sqlite-integration/trunk@1000379 b8457f37-d9ea-0310-8a92-e5e31aec5664
This commit is contained in:
kjmtsh 2014-10-02 07:18:40 +00:00
parent b0c959ba4f
commit 25cb914d6e
7 changed files with 101 additions and 56 deletions

View File

@ -1,3 +1,25 @@
2014-10-01 KOJIMA Toshiyasu
* pdoengine.class.php: revised pdoengine class constructor definition
in order to reuse the PDO instance.
* pdodb.class.php, pdoengine.class.php: moved the foreign key check
from PDODB class to PDOEngine class init() method.
* functions.php, functions-5-2.php: check if PDO instance is not null
to avoid displaying Zend debugger/Xdebug messages. If null, constructor
executes wp_die() function.
* query_create.class.php: changed the data type rewriting method.
When a field name is the same as data type, e.g. "date" or "timestamp",
add a single quote to the field name and avoid to be rewritten.
This works only if the field name is on the top of the line, according
to the rule of dbDelta() function.
Added the new function to quote illegal field name, e.g. "default" or
"values", for SQLite. This is for the plugins that use those names to
create tables without errors.
2014-09-05 KOJIMA Toshiyasu 2014-09-05 KOJIMA Toshiyasu
* version 1.7 release. * version 1.7 release.

View File

@ -34,6 +34,9 @@ class PDOSQLiteUDFS {
* @param reference to PDO object $pdo * @param reference to PDO object $pdo
*/ */
public function __construct(&$pdo){ public function __construct(&$pdo){
if (!$pdo) {
wp_die('Database is not initialized.', 'Database Error');
}
foreach ($this->functions as $f=>$t) { foreach ($this->functions as $f=>$t) {
$pdo->sqliteCreateFunction($f, array($this, $t)); $pdo->sqliteCreateFunction($f, array($this, $t));
} }

View File

@ -34,6 +34,9 @@ class PDOSQLiteUDFS {
* @param reference to PDO object $pdo * @param reference to PDO object $pdo
*/ */
public function __construct(&$pdo){ public function __construct(&$pdo){
if (!$pdo) {
wp_die('Database is not initialized.', 'Database Error');
}
foreach ($this->functions as $f=>$t) { foreach ($this->functions as $f=>$t) {
$pdo->sqliteCreateFunction($f, array($this, $t)); $pdo->sqliteCreateFunction($f, array($this, $t));
} }

View File

@ -68,7 +68,7 @@ function wp_install($blog_title, $user_name, $user_email, $public, $deprecated =
;// Your server is Apache. Nothing to do more. ;// Your server is Apache. Nothing to do more.
} else { } else {
$server_message = sprintf('Your webserver doesn\'t seem to be Apache. So the database directory access restriction by the .htaccess file may not function. We strongly recommend that you should restrict the access to the directory %s in some other way.', FQDBDIR); $server_message = sprintf('Your webserver doesn\'t seem to be Apache. So the database directory access restriction by the .htaccess file may not function. We strongly recommend that you should restrict the access to the directory %s in some other way.', FQDBDIR);
echo '<div style="position: absolute; margin-top: 250px; width: 700px; border: .5px dashed rgb(0, 0, 0);"><p style="margin: 10px;">'; echo '<div style="position: absolute; margin-top: 350px; width: 700px; border: .5px dashed rgb(0, 0, 0);"><p style="margin: 10px;">';
echo $server_message; echo $server_message;
echo '</p></div>'; echo '</p></div>';
} }

View File

@ -211,8 +211,6 @@ class PDODB extends wpdb {
$this->bail(sprintf(__("<h1>Error establlishing a database connection</h1><p>We have been unable to connect to the specified database. <br />The error message received was %s"), $this->dbh->errorInfo())); $this->bail(sprintf(__("<h1>Error establlishing a database connection</h1><p>We have been unable to connect to the specified database. <br />The error message received was %s"), $this->dbh->errorInfo()));
return; return;
} }
$is_enabled_foreign_keys = @$this->get_var('PRAGMA foreign_keys');
if ($is_enabled_foreign_keys == '0') @$this->query('PRAGMA foreign_keys = ON');
$this->has_connected = true; $this->has_connected = true;
$this->ready = true; $this->ready = true;
} }

View File

@ -160,10 +160,44 @@ class PDOEngine extends PDO {
/** /**
* Constructor * Constructor
* *
* Create PDO object, set user defined functions and initialize other settings.
* Don't use parent::__construct() because this class does not only returns
* PDO instance but many others jobs.
*
* Constructor definition is changed since version 1.7.1.
*
* @param none * @param none
*/ */
function __construct() { function __construct() {
register_shutdown_function(array($this, '__destruct')); register_shutdown_function(array($this, '__destruct'));
$dsn = 'sqlite' . FQDB;
if (isset($GLOBALS['@pdo'])) {
$this->pdo = $GLOBALS['@pdo'];
} else {
$locked = false;
$status = 0;
do {
try {
$this->pdo = new PDO($dsn, null, null, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
require_once UDF_FILE;
new PDOSQLiteUDFS($this->pdo);
$GLOBALS['@pdo'] = $this->pdo;
} catch (PDOException $ex) {
$status = $ex->getCode();
if ($status == 5 || $status == 6) {
$locked = true;
} else {
$err_message = $ex->getMessage();
}
}
} while ($locked);
if ($status > 0) {
$message = 'Database initialization error!<br />' .
'Code: ' . $status . '<br />Error Message: ' . $err_message;
$this->set_error(__LINE__, __FILE__, $message);
return false;
}
}
$this->init(); $this->init();
} }
/** /**
@ -172,6 +206,8 @@ class PDOEngine extends PDO {
* If SQLITE_MEM_DEBUG constant is defined, append information about * If SQLITE_MEM_DEBUG constant is defined, append information about
* memory usage into database/mem_debug.txt. * memory usage into database/mem_debug.txt.
* *
* This definition is changed since version 1.7.
*
* @return boolean * @return boolean
*/ */
function __destruct() { function __destruct() {
@ -199,63 +235,27 @@ class PDOEngine extends PDO {
/** /**
* Method to initialize database, executed in the contructor. * Method to initialize database, executed in the contructor.
* *
* It checks if there's a database directory and database file, creates the tables, * It checks if WordPress is in the installing process and does the required
* and binds the user defined function to the pdo object. * jobs. SQLite library version specific settings are also in this function.
*
* Some developers use WP_INSTALLING constant for other purposes, if so, this
* function will do no harms.
* *
* @return boolean * @return boolean
*/ */
private function init() { private function init() {
$dsn = 'sqlite:' . FQDB; if (defined('WP_INSTALLING') && WP_INSTALLING) {
$result = $this->prepare_directory(); $this->prepare_directory();
if (!$result) return false;
if (is_file(FQDB)) {
$locked = false;
do {
try {
if ($locked) $locked = false;
$this->pdo = new PDO(
$dsn, // data source name
null, // user name
null, // user password
array( // PDO options
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$statement = $this->pdo->query("SELECT COUNT(*) FROM sqlite_master WHERE type='table'"); $statement = $this->pdo->query("SELECT COUNT(*) FROM sqlite_master WHERE type='table'");
$number_of_tables = $statement->fetchColumn(0); $number_of_tables = $statement->fetchColumn(0);
$statement = null; $statement = null;
if ($number_of_tables == 0) { if ($number_of_tables == 0) $this->make_sqlite_tables();
$this->make_sqlite_tables();
} }
} catch (PDOException $err) {
$status = $err->getCode();
// code 5 => The database file is locked
// code 6 => A table in the database is locked
if ($status == 5 || $status == 6) {
$locked = true;
} else {
$message = 'Database connection error!<br />';
$message .= sprintf("Error message is: %s", $err->getMessage());
$this->set_error(__LINE__, __FUNCTION__, $message);
return false;
}
}
} while ($locked);
require_once UDF_FILE;
new PDOSQLiteUDFS($this->pdo);
if (version_compare($this->get_sqlite_version(), '3.7.11', '>=')) { if (version_compare($this->get_sqlite_version(), '3.7.11', '>=')) {
$this->can_insert_multiple_rows = true; $this->can_insert_multiple_rows = true;
} }
} else { // database file is not found, so we make it and create tables... $statement = $this->pdo->query('PRAGMA foreign_keys');
try { if ($statement->fetchColumn(0) == '0') $this->pdo->query('PRAGMA foreign_keys = ON');
$this->pdo = new PDO($dsn, null, null, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch (PDOException $err) {
$message = 'Database initialization error!<br />';
$message .= sprintf("Error message is: %s", $err->getMessage());
$this->set_error(__LINE__, __FUNCTION__, $message);
return false;
}
$this->make_sqlite_tables();
}
} }
/** /**

View File

@ -71,6 +71,7 @@ class CreateQuery{
return $this->_query; return $this->_query;
} }
$this->strip_backticks(); $this->strip_backticks();
$this->quote_illegal_field();
$this->get_table_name(); $this->get_table_name();
$this->rewrite_comments(); $this->rewrite_comments();
$this->rewrite_field_types(); $this->rewrite_field_types();
@ -106,6 +107,10 @@ class CreateQuery{
/** /**
* Method to change the MySQL field types to SQLite compatible types. * Method to change the MySQL field types to SQLite compatible types.
* *
* If column name is the same as the key value, e.g. "date" or "timestamp",
* and the column is on the top of the line, we add a single quote and avoid
* to be replaced. But this doesn't work if that column name is in the middle
* of the line.
* Order of the key value is important. Don't change it. * Order of the key value is important. Don't change it.
* *
* @access private * @access private
@ -130,6 +135,12 @@ class CreateQuery{
'longblob' => 'blob', 'longtext' => 'text' 'longblob' => 'blob', 'longtext' => 'text'
); );
foreach ($array_types as $o => $r){ foreach ($array_types as $o => $r){
if (preg_match("/^\\s*(?<!')$o\\s+(.+$)/im", $this->_query, $match)) {
$ptrn = "/$match[1]/im";
$replaced = str_ireplace($ptrn, '#placeholder#', $this->_query);
$replaced = str_ireplace($o, "'{$o}'", $replaced);
$this->_query = str_replace('#placeholder#', $ptrn, $replaced);
}
$pattern = "/\\b(?<!')$o\\b\\s*(\([^\)]*\)*)?\\s*/ims"; $pattern = "/\\b(?<!')$o\\b\\s*(\([^\)]*\)*)?\\s*/ims";
if (preg_match("/^\\s*.*?\\s*\(.*?$o.*?\)/im", $this->_query)) { if (preg_match("/^\\s*.*?\\s*\(.*?$o.*?\)/im", $this->_query)) {
; ;
@ -393,5 +404,13 @@ class CreateQuery{
$patterns = array($pattern_charset, $pattern_collate1, $pattern_collate2); $patterns = array($pattern_charset, $pattern_collate1, $pattern_collate2);
$this->_query = preg_replace($patterns, '', $this->_query); $this->_query = preg_replace($patterns, '', $this->_query);
} }
/**
* Method to quote illegal field name for SQLite
*
* @access private
*/
private function quote_illegal_field() {
$this->_query = preg_replace("/^\\s*(?<!')(default|values)/im", "'\\1'", $this->_query);
}
} }
?> ?>