2013-07-07 06:49:38 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2014-01-29 09:35:06 +00:00
|
|
|
* This file defines the make_db_sqlite() function.
|
|
|
|
*
|
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
|
|
|
*/
|
|
|
|
|
2013-12-17 02:46:42 +00:00
|
|
|
if (!defined('ABSPATH')) {
|
|
|
|
echo 'Thank you, but you are not allowed to access this file.';
|
|
|
|
die();
|
|
|
|
}
|
2014-01-29 09:35:06 +00:00
|
|
|
/**
|
|
|
|
* Function to create tables according to the schemas of WordPress.
|
|
|
|
*
|
|
|
|
* This is executed only once while installation.
|
2014-02-05 19:29:23 +00:00
|
|
|
*
|
2014-01-29 09:35:06 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-07-07 06:49:38 +00:00
|
|
|
function make_db_sqlite() {
|
|
|
|
include_once PDODIR . 'query_create.class.php';
|
|
|
|
include_once ABSPATH . 'wp-admin/includes/schema.php';
|
2014-02-05 19:29:23 +00:00
|
|
|
$index_array = array();
|
2013-07-07 06:49:38 +00:00
|
|
|
|
2013-11-06 19:23:32 +00:00
|
|
|
// ob_end_clean();
|
2013-07-07 06:49:38 +00:00
|
|
|
$table_schemas = wp_get_db_schema();
|
2014-02-05 19:29:23 +00:00
|
|
|
$queries = explode (";", $table_schemas);
|
|
|
|
$query_parser = new CreateQuery();
|
2013-07-07 06:49:38 +00:00
|
|
|
try {
|
|
|
|
$pdo = new PDO('sqlite:'.FQDB, null, null, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
|
|
|
|
} catch (PDOException $err) {
|
|
|
|
$err_data = $err->errorInfo;
|
2014-02-05 19:29:23 +00:00
|
|
|
$message = 'Database connection error!<br />';
|
2013-08-13 00:34:40 +00:00
|
|
|
$message .= sprintf("Error message is: %s", $err_data[2]);
|
2013-07-07 06:49:38 +00:00
|
|
|
echo $message;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$pdo->beginTransaction();
|
|
|
|
foreach ($queries as $query) {
|
|
|
|
$query = trim($query);
|
|
|
|
if (empty($query))
|
|
|
|
continue;
|
|
|
|
$rewritten_query = $query_parser->rewrite_query($query);
|
|
|
|
if (is_array($rewritten_query)) {
|
2014-02-05 19:29:23 +00:00
|
|
|
$table_query = array_shift($rewritten_query);
|
2013-07-07 06:49:38 +00:00
|
|
|
$index_queries = $rewritten_query;
|
2014-02-05 19:29:23 +00:00
|
|
|
$table_query = trim($table_query);
|
2013-07-07 06:49:38 +00:00
|
|
|
$pdo->exec($table_query);
|
|
|
|
// foreach($rewritten_query as $single_query) {
|
|
|
|
// $single_query = trim($single_query);
|
|
|
|
// $pdo->exec($single_query);
|
|
|
|
// }
|
|
|
|
} else {
|
|
|
|
$rewritten_query = trim($rewritten_query);
|
|
|
|
$pdo->exec($rewritten_query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$pdo->commit();
|
|
|
|
if ($index_queries) {
|
|
|
|
// $query_parser rewrites KEY to INDEX, so we don't need KEY pattern
|
|
|
|
$pattern = '/CREATE\\s*(UNIQUE\\s*INDEX|INDEX)\\s*IF\\s*NOT\\s*EXISTS\\s*(\\w+)?\\s*.*/im';
|
|
|
|
$pdo->beginTransaction();
|
|
|
|
foreach ($index_queries as $index_query) {
|
|
|
|
preg_match($pattern, $index_query, $match);
|
|
|
|
$index_name = trim($match[2]);
|
|
|
|
if (in_array($index_name, $index_array)) {
|
|
|
|
$r = rand(0, 50);
|
|
|
|
$replacement = $index_name . "_$r";
|
|
|
|
$index_query = str_ireplace('EXISTS '.$index_name, 'EXISTS '.$replacement, $index_query);
|
|
|
|
} else {
|
|
|
|
$index_array[] = $index_name;
|
|
|
|
}
|
|
|
|
$pdo->exec($index_query);
|
|
|
|
}
|
|
|
|
$pdo->commit();
|
|
|
|
}
|
|
|
|
} catch (PDOException $err) {
|
|
|
|
$err_data = $err->errorInfo;
|
|
|
|
$err_code = $err_data[1];
|
|
|
|
if (5 == $err_code || 6 == $err_code) {
|
|
|
|
// if the database is locked, commit again
|
|
|
|
$pdo->commit();
|
|
|
|
} else {
|
|
|
|
$pdo->rollBack();
|
2014-02-05 19:29:23 +00:00
|
|
|
$message = sprintf("Error occured while creating tables or indexes...<br />Query was: %s<br />", var_export($rewritten_query, true));
|
2013-08-13 00:34:40 +00:00
|
|
|
$message .= sprintf("Error message is: %s", $err_data[2]);
|
2013-07-07 06:49:38 +00:00
|
|
|
echo $message;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$query_parser = null;
|
|
|
|
$pdo = null;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
?>
|