2013-07-07 06:49:38 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* This file defines CreateQuery class.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2013-07-07 06:49:38 +00:00
|
|
|
* @package SQLite Integration
|
2014-01-29 09:35:06 +00:00
|
|
|
* @author Kojima Toshiyasu
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
2014-07-14 08:46:32 +00:00
|
|
|
|
2013-07-07 06:49:38 +00:00
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* This class provides a function to rewrite CREATE query.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
|
|
|
class CreateQuery{
|
|
|
|
|
2014-02-05 19:29:23 +00:00
|
|
|
/**
|
|
|
|
* The query string to be rewritten in this class.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-02-05 19:29:23 +00:00
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private $_query = '';
|
|
|
|
/**
|
|
|
|
* The array to contain CREATE INDEX queries.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-02-05 19:29:23 +00:00
|
|
|
* @var array of strings
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private $index_queries = array();
|
|
|
|
/**
|
|
|
|
* The array to contain error messages.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-02-05 19:29:23 +00:00
|
|
|
* @var array of string
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private $_errors = array();
|
|
|
|
/**
|
|
|
|
* Variable to have the table name to be executed.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-02-05 19:29:23 +00:00
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private $table_name = '';
|
|
|
|
/**
|
|
|
|
* Variable to check if the query has the primary key.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-02-05 19:29:23 +00:00
|
|
|
* @var boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
private $has_primary_key = false;
|
2014-07-14 08:46:32 +00:00
|
|
|
|
2013-07-07 06:49:38 +00:00
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Function to rewrite query.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @param string $query the query being processed
|
|
|
|
* @return string|array the processed (rewritten) query
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
|
|
|
public function rewrite_query($query){
|
2014-02-05 19:29:23 +00:00
|
|
|
$this->_query = $query;
|
2013-07-07 06:49:38 +00:00
|
|
|
$this->_errors [] = '';
|
2013-07-24 02:31:01 +00:00
|
|
|
if (preg_match('/^CREATE\\s*(UNIQUE|FULLTEXT|)\\s*INDEX/ims', $this->_query, $match)) {
|
2014-01-29 09:35:06 +00:00
|
|
|
// we manipulate CREATE INDEX query in PDOEngine.class.php
|
|
|
|
// FULLTEXT index creation is simply ignored.
|
2013-07-24 02:31:01 +00:00
|
|
|
if (isset($match[1]) && stripos($match[1], 'fulltext') !== false) {
|
|
|
|
return 'SELECT 1=1';
|
|
|
|
} else {
|
|
|
|
return $this->_query;
|
|
|
|
}
|
2013-08-13 00:34:40 +00:00
|
|
|
} elseif (preg_match('/^CREATE\\s*(TEMP|TEMPORARY|)\\s*TRIGGER\\s*/im', $this->_query)) {
|
2014-01-29 09:35:06 +00:00
|
|
|
// if WordPress comes to use foreign key constraint, trigger will be needed.
|
|
|
|
// we don't use it for now.
|
2013-08-13 00:34:40 +00:00
|
|
|
return $this->_query;
|
2013-07-24 02:31:01 +00:00
|
|
|
}
|
2014-05-05 03:38:15 +00:00
|
|
|
$this->strip_backticks();
|
2014-10-02 07:18:40 +00:00
|
|
|
$this->quote_illegal_field();
|
2013-07-07 06:49:38 +00:00
|
|
|
$this->get_table_name();
|
|
|
|
$this->rewrite_comments();
|
|
|
|
$this->rewrite_field_types();
|
|
|
|
$this->rewrite_character_set();
|
|
|
|
$this->rewrite_engine_info();
|
|
|
|
$this->rewrite_unsigned();
|
|
|
|
$this->rewrite_autoincrement();
|
|
|
|
$this->rewrite_primary_key();
|
2014-04-17 02:45:32 +00:00
|
|
|
$this->rewrite_foreign_key();
|
2013-07-07 06:49:38 +00:00
|
|
|
$this->rewrite_unique_key();
|
|
|
|
$this->rewrite_enum();
|
|
|
|
$this->rewrite_set();
|
|
|
|
$this->rewrite_key();
|
|
|
|
$this->add_if_not_exists();
|
2014-07-14 08:46:32 +00:00
|
|
|
|
2013-07-07 06:49:38 +00:00
|
|
|
return $this->post_process();
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method to get table name from the query string.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-02-05 19:29:23 +00:00
|
|
|
* 'IF NOT EXISTS' clause is removed for the easy regular expression usage.
|
2014-01-29 09:35:06 +00:00
|
|
|
* It will be added at the end of the process.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
|
|
|
private function get_table_name(){
|
|
|
|
// $pattern = '/^\\s*CREATE\\s*(TEMP|TEMPORARY)?\\s*TABLE\\s*(IF NOT EXISTS)?\\s*([^\(]*)/imsx';
|
2014-07-14 08:46:32 +00:00
|
|
|
$pattern = '/^\\s*CREATE\\s*(?:TEMP|TEMPORARY)?\\s*TABLE\\s*(?:IF\\s*NOT\\s*EXISTS)?\\s*([^\(]*)/imsx';
|
2013-07-07 06:49:38 +00:00
|
|
|
if (preg_match($pattern, $this->_query, $matches)) {
|
2014-07-14 08:46:32 +00:00
|
|
|
$this->table_name = trim($matches[1]);
|
|
|
|
}
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method to change the MySQL field types to SQLite compatible types.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-10-02 07:18:40 +00:00
|
|
|
* 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.
|
2014-02-05 19:29:23 +00:00
|
|
|
* Order of the key value is important. Don't change it.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
|
|
|
private function rewrite_field_types(){
|
|
|
|
$array_types = array (
|
2014-07-14 08:46:32 +00:00
|
|
|
'bit' => 'integer', 'bool' => 'integer',
|
|
|
|
'boolean' => 'integer', 'tinyint' => 'integer',
|
|
|
|
'smallint' => 'integer', 'mediumint' => 'integer',
|
|
|
|
'int' => 'integer', 'integer' => 'integer',
|
|
|
|
'bigint' => 'integer', 'float' => 'real',
|
|
|
|
'double' => 'real', 'decimal' => 'real',
|
|
|
|
'dec' => 'real', 'numeric' => 'real',
|
|
|
|
'fixed' => 'real', 'date' => 'text',
|
|
|
|
'datetime' => 'text', 'timestamp' => 'text',
|
|
|
|
'time' => 'text', 'year' => 'text',
|
|
|
|
'char' => 'text', 'varchar' => 'text',
|
|
|
|
'binary' => 'integer', 'varbinary' => 'blob',
|
|
|
|
'tinyblob' => 'blob', 'tinytext' => 'text',
|
|
|
|
'blob' => 'blob', 'text' => 'text',
|
|
|
|
'mediumblob' => 'blob', 'mediumtext' => 'text',
|
|
|
|
'longblob' => 'blob', 'longtext' => 'text'
|
|
|
|
);
|
2014-10-02 07:18:40 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-07-30 23:44:12 +00:00
|
|
|
$pattern = "/\\b(?<!')$o\\b\\s*(\([^\)]*\)*)?\\s*/ims";
|
2013-08-13 00:34:40 +00:00
|
|
|
if (preg_match("/^\\s*.*?\\s*\(.*?$o.*?\)/im", $this->_query)) {
|
|
|
|
;
|
|
|
|
} else {
|
|
|
|
$this->_query = preg_replace($pattern, " $r ", $this->_query);
|
|
|
|
}
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method for stripping the comments from the SQL statement.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2014-07-14 08:46:32 +00:00
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
private function rewrite_comments(){
|
|
|
|
$this->_query = preg_replace("/# --------------------------------------------------------/","-- ******************************************************",$this->_query);
|
|
|
|
$this->_query = preg_replace("/#/","--",$this->_query);
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method for stripping the engine and other stuffs.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* TYPE, ENGINE and AUTO_INCREMENT are removed here.
|
|
|
|
* @access private
|
2014-07-14 08:46:32 +00:00
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
private function rewrite_engine_info(){
|
|
|
|
$this->_query = preg_replace("/\\s*(TYPE|ENGINE)\\s*=\\s*.*(?<!;)/ims",'',$this->_query);
|
|
|
|
$this->_query = preg_replace("/ AUTO_INCREMENT\\s*=\\s*[0-9]*/ims",'',$this->_query);
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method for stripping unsigned.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-02-05 19:29:23 +00:00
|
|
|
* SQLite doesn't have unsigned int data type. So UNSIGNED INT(EGER) is converted
|
|
|
|
* to INTEGER here.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2014-07-14 08:46:32 +00:00
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
private function rewrite_unsigned(){
|
|
|
|
$this->_query = preg_replace('/\\bunsigned\\b/ims', ' ', $this->_query);
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method for rewriting primary key auto_increment.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* If the field type is 'INTEGER PRIMARY KEY', it is automatically autoincremented
|
|
|
|
* by SQLite. There's a little difference between PRIMARY KEY and AUTOINCREMENT, so
|
|
|
|
* we may well convert to PRIMARY KEY only.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2014-07-14 08:46:32 +00:00
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
private function rewrite_autoincrement(){
|
|
|
|
$this->_query = preg_replace('/\\bauto_increment\\s*primary\\s*key\\s*(,)?/ims', ' PRIMARY KEY AUTOINCREMENT \\1', $this->_query, -1, $count);
|
|
|
|
$this->_query = preg_replace('/\\bauto_increment\\b\\s*(,)?/ims', ' PRIMARY KEY AUTOINCREMENT $1', $this->_query, -1, $count);
|
|
|
|
if ($count > 0){
|
|
|
|
$this->has_primary_key = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method for rewriting primary key.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2014-07-14 08:46:32 +00:00
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
private function rewrite_primary_key(){
|
|
|
|
if ($this->has_primary_key) {
|
2014-04-17 02:45:32 +00:00
|
|
|
$this->_query = preg_replace('/\\s*primary key\\s*.*?\([^\)]*\)\\s*(,|)/i', ' ', $this->_query);
|
2013-07-07 06:49:38 +00:00
|
|
|
} else {
|
2014-07-14 08:46:32 +00:00
|
|
|
// If primary key has an index name, we remove that name.
|
|
|
|
$this->_query = preg_replace('/\\bprimary\\s*key\\s*.*?\\s*(\(.*?\))/im', 'PRIMARY KEY \\1', $this->_query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Method for rewriting foreign key.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private function rewrite_foreign_key() {
|
|
|
|
$pattern = '/\\s*foreign\\s*key\\s*(|.*?)\([^\)]+?\)\\s*references\\s*.*/i';
|
|
|
|
if (preg_match_all($pattern, $this->_query, $match)) {
|
|
|
|
if (isset($match[1])) {
|
|
|
|
$this->_query = str_ireplace($match[1], '', $this->_query);
|
|
|
|
}
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method for rewriting unique key.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2014-07-14 08:46:32 +00:00
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
private function rewrite_unique_key(){
|
2014-04-17 02:45:32 +00:00
|
|
|
$this->_query = preg_replace_callback('/\\bunique key\\b([^\(]*)(\(.*\))/im', array($this, '_rewrite_unique_key'), $this->_query);
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Callback method for rewrite_unique_key.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2013-07-07 06:49:38 +00:00
|
|
|
* @param array $matches an array of matches from the Regex
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
|
|
|
private function _rewrite_unique_key($matches){
|
2014-07-14 08:46:32 +00:00
|
|
|
$index_name = trim($matches[1]);
|
|
|
|
$col_name = trim($matches[2]);
|
|
|
|
$tbl_name = $this->table_name;
|
|
|
|
if (preg_match('/\(\\d+?\)/', $col_name)) {
|
|
|
|
$col_name = preg_replace('/\(\\d+?\)/', '', $col_name);
|
|
|
|
}
|
|
|
|
$_wpdb = new PDODB();
|
|
|
|
$results = $_wpdb->get_results("SELECT name FROM sqlite_master WHERE type='index'");
|
|
|
|
$_wpdb = null;
|
|
|
|
if ($results) {
|
|
|
|
foreach ($results as $result) {
|
|
|
|
if ($result->name == $index_name) {
|
|
|
|
$r = rand(0, 50);
|
|
|
|
$index_name = $index_name . "_$r";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-07 06:49:38 +00:00
|
|
|
$index_name = str_replace(' ', '', $index_name);
|
|
|
|
$this->index_queries[] = "CREATE UNIQUE INDEX $index_name ON " . $tbl_name .$col_name;
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method for handling ENUM fields.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* SQLite doesn't support enum, so we change it to check constraint.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
|
|
|
private function rewrite_enum(){
|
|
|
|
$pattern = '/(,|\))([^,]*)enum\((.*?)\)([^,\)]*)/ims';
|
|
|
|
$this->_query = preg_replace_callback($pattern, array($this, '_rewrite_enum'), $this->_query);
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Call back method for rewrite_enum() and rewrite_set().
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
|
|
|
private function _rewrite_enum($matches){
|
|
|
|
$output = $matches[1] . ' ' . $matches[2]. ' TEXT '. $matches[4].' CHECK ('.$matches[2].' IN ('.$matches[3].')) ';
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method for rewriting usage of set.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* It is similar but not identical to enum. SQLite does not support either.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
|
|
|
private function rewrite_set(){
|
|
|
|
$pattern = '/\b(\w)*\bset\\s*\((.*?)\)\\s*(.*?)(,)*/ims';
|
|
|
|
$this->_query = preg_replace_callback($pattern, array($this, '_rewrite_enum'), $this->_query);
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method for rewriting usage of key to create an index.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* SQLite cannot create non-unique indices as part of the create query,
|
|
|
|
* so we need to create an index by hand and append it to the create query.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
|
|
|
private function rewrite_key(){
|
|
|
|
$this->_query = preg_replace_callback('/,\\s*(KEY|INDEX)\\s*(\\w+)?\\s*(\(.*(?<!\\d)\))/im', array($this, '_rewrite_key'), $this->_query);
|
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Callback method for rewrite_key.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2013-07-07 06:49:38 +00:00
|
|
|
* @param array $matches an array of matches from the Regex
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2014-07-14 08:46:32 +00:00
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
private function _rewrite_key($matches){
|
2014-07-14 08:46:32 +00:00
|
|
|
$index_name = trim($matches[2]);
|
|
|
|
$col_name = trim($matches[3]);
|
|
|
|
if (preg_match('/\([0-9]+?\)/', $col_name, $match)) {
|
|
|
|
$col_name = preg_replace_callback('/\([0-9]+?\)/', array($this, '_remove_length'), $col_name);
|
|
|
|
}
|
|
|
|
$tbl_name = $this->table_name;
|
|
|
|
$_wpdb = new PDODB();
|
|
|
|
$results = $_wpdb->get_results("SELECT name FROM sqlite_master WHERE type='index'");
|
|
|
|
$_wpdb = null;
|
|
|
|
if ($results) {
|
|
|
|
foreach ($results as $result) {
|
|
|
|
if ($result->name == $index_name) {
|
|
|
|
$r = rand(0, 50);
|
|
|
|
$index_name = $index_name . "_$r";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-07 06:49:38 +00:00
|
|
|
$this->index_queries[] = 'CREATE INDEX '. $index_name . ' ON ' . $tbl_name . $col_name ;
|
|
|
|
return '';
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Call back method to remove unnecessary string.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* This method is deprecated.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @param string $match
|
|
|
|
* @return string whose length is zero
|
|
|
|
* @access private
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
private function _remove_length($match) {
|
2014-07-14 08:46:32 +00:00
|
|
|
return '';
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method to assemble the main query and index queries into an array.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* It return the array of the queries to be executed separately.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @return array
|
|
|
|
* @access private
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
2014-07-14 08:46:32 +00:00
|
|
|
private function post_process() {
|
2013-07-07 06:49:38 +00:00
|
|
|
$mainquery = $this->_query;
|
|
|
|
do{
|
2014-02-05 19:29:23 +00:00
|
|
|
$count = 0;
|
2013-07-07 06:49:38 +00:00
|
|
|
$mainquery = preg_replace('/,\\s*\)/imsx',')', $mainquery, -1, $count);
|
|
|
|
} while ($count > 0);
|
|
|
|
do {
|
2014-07-14 08:46:32 +00:00
|
|
|
$count = 0;
|
|
|
|
$mainquery = preg_replace('/\(\\s*?,/imsx', '(', $mainquery, -1, $count);
|
2013-07-07 06:49:38 +00:00
|
|
|
} while ($count > 0);
|
|
|
|
$return_val[] = $mainquery;
|
2014-02-05 19:29:23 +00:00
|
|
|
$return_val = array_merge($return_val, $this->index_queries);
|
2014-07-14 08:46:32 +00:00
|
|
|
return $return_val;
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method to add IF NOT EXISTS to query string.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* This adds IF NOT EXISTS to every query string, which prevent the exception
|
|
|
|
* from being thrown.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2013-07-07 06:49:38 +00:00
|
|
|
*/
|
|
|
|
private function add_if_not_exists(){
|
|
|
|
$pattern_table = '/^\\s*CREATE\\s*(TEMP|TEMPORARY)?\\s*TABLE\\s*(IF NOT EXISTS)?\\s*/ims';
|
2014-02-05 19:29:23 +00:00
|
|
|
$this->_query = preg_replace($pattern_table, 'CREATE $1 TABLE IF NOT EXISTS ', $this->_query);
|
2013-07-07 06:49:38 +00:00
|
|
|
$pattern_index = '/^\\s*CREATE\\s*(UNIQUE)?\\s*INDEX\\s*(IF NOT EXISTS)?\\s*/ims';
|
|
|
|
for ($i = 0; $i < count($this->index_queries); $i++) {
|
2014-07-14 08:46:32 +00:00
|
|
|
$this->index_queries[$i] = preg_replace($pattern_index, 'CREATE $1 INDEX IF NOT EXISTS ', $this->index_queries[$i]);
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
2014-02-05 19:29:23 +00:00
|
|
|
* Method to strip back quotes.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2014-07-14 08:46:32 +00:00
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
private function strip_backticks(){
|
|
|
|
$this->_query = str_replace('`', '', $this->_query);
|
2014-07-14 08:46:32 +00:00
|
|
|
foreach ($this->index_queries as &$query) {
|
|
|
|
$query = str_replace('`', '', $query);
|
|
|
|
}
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* Method to remove the character set information from within mysql queries.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* This removes DEFAULT CHAR(ACTER) SET and COLLATE, which is meaningless for
|
|
|
|
* SQLite.
|
2014-07-14 08:46:32 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @access private
|
2014-07-14 08:46:32 +00:00
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
private function rewrite_character_set(){
|
2014-02-05 19:29:23 +00:00
|
|
|
$pattern_charset = '/\\b(default\\s*character\\s*set|default\\s*charset|character\\s*set)\\s*(?<!\()[^ ]*/im';
|
2013-07-07 06:49:38 +00:00
|
|
|
$pattern_collate1 = '/\\s*collate\\s*[^ ]*(?=,)/im';
|
2014-07-14 08:46:32 +00:00
|
|
|
$pattern_collate2 = '/\\s*collate\\s*[^ ]*(?<!;)/im';
|
|
|
|
$patterns = array($pattern_charset, $pattern_collate1, $pattern_collate2);
|
2014-02-05 19:29:23 +00:00
|
|
|
$this->_query = preg_replace($patterns, '', $this->_query);
|
2013-07-07 06:49:38 +00:00
|
|
|
}
|
2014-10-02 07:18:40 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2014-02-05 19:29:23 +00:00
|
|
|
}
|
|
|
|
?>
|