Skip to content

Commit

Permalink
changed for Mac environment and some others
Browse files Browse the repository at this point in the history
git-svn-id: https://plugins.svn.wordpress.org/sqlite-integration/trunk@1000379 b8457f37-d9ea-0310-8a92-e5e31aec5664
  • Loading branch information
kjmtsh committed Oct 2, 2014
1 parent b0c959b commit 25cb914
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 56 deletions.
22 changes: 22 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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

* version 1.7 release.
Expand Down
3 changes: 3 additions & 0 deletions functions-5-2.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class PDOSQLiteUDFS {
* @param reference to PDO object $pdo
*/
public function __construct(&$pdo){
if (!$pdo) {
wp_die('Database is not initialized.', 'Database Error');
}
foreach ($this->functions as $f=>$t) {
$pdo->sqliteCreateFunction($f, array($this, $t));
}
Expand Down
3 changes: 3 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class PDOSQLiteUDFS {
* @param reference to PDO object $pdo
*/
public function __construct(&$pdo){
if (!$pdo) {
wp_die('Database is not initialized.', 'Database Error');
}
foreach ($this->functions as $f=>$t) {
$pdo->sqliteCreateFunction($f, array($this, $t));
}
Expand Down
2 changes: 1 addition & 1 deletion install.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function wp_install($blog_title, $user_name, $user_email, $public, $deprecated =
;// Your server is Apache. Nothing to do more.
} 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);
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 '</p></div>';
}
Expand Down
2 changes: 0 additions & 2 deletions pdodb.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,6 @@ public function db_connect($allow_bail=true) {
$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;
}
$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->ready = true;
}
Expand Down
104 changes: 52 additions & 52 deletions pdoengine.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,44 @@ class PDOEngine extends PDO {
/**
* 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
*/
function __construct() {
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();
}
/**
Expand All @@ -172,6 +206,8 @@ function __construct() {
* If SQLITE_MEM_DEBUG constant is defined, append information about
* memory usage into database/mem_debug.txt.
*
* This definition is changed since version 1.7.
*
* @return boolean
*/
function __destruct() {
Expand Down Expand Up @@ -199,63 +235,27 @@ function __destruct() {
/**
* Method to initialize database, executed in the contructor.
*
* It checks if there's a database directory and database file, creates the tables,
* and binds the user defined function to the pdo object.
* It checks if WordPress is in the installing process and does the required
* 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
*/
private function init() {
$dsn = 'sqlite:' . FQDB;
$result = $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'");
$number_of_tables = $statement->fetchColumn(0);
$statement = null;
if ($number_of_tables == 0) {
$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', '>=')) {
$this->can_insert_multiple_rows = true;
}
} else { // database file is not found, so we make it and create tables...
try {
$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();
if (defined('WP_INSTALLING') && WP_INSTALLING) {
$this->prepare_directory();
$statement = $this->pdo->query("SELECT COUNT(*) FROM sqlite_master WHERE type='table'");
$number_of_tables = $statement->fetchColumn(0);
$statement = null;
if ($number_of_tables == 0) $this->make_sqlite_tables();
}
if (version_compare($this->get_sqlite_version(), '3.7.11', '>=')) {
$this->can_insert_multiple_rows = true;
}
$statement = $this->pdo->query('PRAGMA foreign_keys');
if ($statement->fetchColumn(0) == '0') $this->pdo->query('PRAGMA foreign_keys = ON');
}

/**
Expand Down
21 changes: 20 additions & 1 deletion query_create.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function rewrite_query($query){
return $this->_query;
}
$this->strip_backticks();
$this->quote_illegal_field();
$this->get_table_name();
$this->rewrite_comments();
$this->rewrite_field_types();
Expand Down Expand Up @@ -106,6 +107,10 @@ private function get_table_name(){
/**
* 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.
*
* @access private
Expand All @@ -129,7 +134,13 @@ private function rewrite_field_types(){
'mediumblob' => 'blob', 'mediumtext' => '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";
if (preg_match("/^\\s*.*?\\s*\(.*?$o.*?\)/im", $this->_query)) {
;
Expand Down Expand Up @@ -393,5 +404,13 @@ private function rewrite_character_set(){
$patterns = array($pattern_charset, $pattern_collate1, $pattern_collate2);
$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);
}
}
?>

0 comments on commit 25cb914

Please sign in to comment.