get rid of direct references to Database's engine
This commit is contained in:
parent
d313cea8a7
commit
38f7555d98
@ -269,31 +269,15 @@ class Database {
|
||||
/**
|
||||
* The currently active cache engine
|
||||
*/
|
||||
private $cache = null;
|
||||
public $cache = null;
|
||||
|
||||
/**
|
||||
* Don't do anything yet; DB and cache connections will be created as
|
||||
* and when they're needed
|
||||
* For now, only connect to the cache, as we will pretty much certainly
|
||||
* need it. There are some pages where all the data is in cache, so the
|
||||
* DB connection is on-demand.
|
||||
*/
|
||||
public function Database() {
|
||||
}
|
||||
|
||||
public function __get($name) {
|
||||
if($name == "engine") {
|
||||
if(is_null($this->engine)) {
|
||||
$this->connect_engine();
|
||||
}
|
||||
return $this->engine;
|
||||
}
|
||||
else if($name == "cache") {
|
||||
if(is_null($this->cache)) {
|
||||
$this->connect_cache();
|
||||
}
|
||||
return $this->cache;
|
||||
}
|
||||
else {
|
||||
return $this->$name;
|
||||
}
|
||||
$this->connect_cache();
|
||||
}
|
||||
|
||||
private function connect_cache() {
|
||||
@ -356,16 +340,26 @@ class Database {
|
||||
}
|
||||
|
||||
public function commit() {
|
||||
if(!is_null($this->db)) $this->db->commit();
|
||||
if(!is_null($this->db)) return $this->db->commit();
|
||||
}
|
||||
|
||||
public function rollback() {
|
||||
if(!is_null($this->db)) $this->db->rollback();
|
||||
if(!is_null($this->db)) return $this->db->rollback();
|
||||
}
|
||||
|
||||
public function escape($input) {
|
||||
if(is_null($this->db)) $this->connect_db();
|
||||
$this->db->Quote($input);
|
||||
return $this->db->Quote($input);
|
||||
}
|
||||
|
||||
public function scoreql_to_sql($input) {
|
||||
if(is_null($this->engine)) $this->connect_engine();
|
||||
return $this->engine->scoreql_to_sql($input);
|
||||
}
|
||||
|
||||
public function get_driver_name() {
|
||||
if(is_null($this->engine)) $this->connect_engine();
|
||||
return $this->engine->name;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -160,7 +160,7 @@ class Image {
|
||||
else if(count($tags) == 1 && !preg_match("/[:=><]/", $tags[0])) {
|
||||
$term = Tag::resolve_alias($tags[0]);
|
||||
return $database->get_one(
|
||||
$database->engine->scoreql_to_sql("SELECT count FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"),
|
||||
$database->scoreql_to_sql("SELECT count FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"),
|
||||
array("tag"=>$term));
|
||||
}
|
||||
else {
|
||||
@ -432,7 +432,7 @@ class Image {
|
||||
public function set_locked($tf) {
|
||||
global $database;
|
||||
$ln = $tf ? "Y" : "N";
|
||||
$sln = $database->engine->scoreql_to_sql('SCORE_BOOL_'.$ln);
|
||||
$sln = $database->scoreql_to_sql('SCORE_BOOL_'.$ln);
|
||||
$sln = str_replace("'", "", $sln);
|
||||
$sln = str_replace('"', "", $sln);
|
||||
if(bool_escape($sln) !== $this->locked) {
|
||||
@ -472,7 +472,7 @@ class Image {
|
||||
// insert each new tags
|
||||
foreach($tags as $tag) {
|
||||
$id = $database->get_one(
|
||||
$database->engine->scoreql_to_sql(
|
||||
$database->scoreql_to_sql(
|
||||
"SELECT id FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"
|
||||
),
|
||||
array("tag"=>$tag));
|
||||
@ -493,7 +493,7 @@ class Image {
|
||||
array("iid"=>$this->id, "tid"=>$id));
|
||||
}
|
||||
$database->execute(
|
||||
$database->engine->scoreql_to_sql(
|
||||
$database->scoreql_to_sql(
|
||||
"UPDATE tags SET count = count + 1 WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"
|
||||
),
|
||||
array("tag"=>$tag));
|
||||
@ -602,7 +602,7 @@ class Image {
|
||||
private static function build_search_querylet($terms) {
|
||||
assert(is_array($terms));
|
||||
global $database;
|
||||
if($database->engine->name === "mysql")
|
||||
if($database->get_driver_name() === "mysql")
|
||||
return Image::build_ugly_search_querylet($terms);
|
||||
else
|
||||
return Image::build_accurate_search_querylet($terms);
|
||||
@ -704,7 +704,7 @@ class Image {
|
||||
|
||||
// one positive tag (a common case), do an optimised search
|
||||
else if(count($tag_querylets) == 1 && $tag_querylets[0]->positive) {
|
||||
$query = new Querylet($database->engine->scoreql_to_sql("
|
||||
$query = new Querylet($database->scoreql_to_sql("
|
||||
SELECT images.* FROM images
|
||||
JOIN image_tags ON images.id=image_tags.image_id
|
||||
JOIN tags ON image_tags.tag_id=tags.id
|
||||
@ -725,7 +725,7 @@ class Image {
|
||||
|
||||
foreach($tag_querylets as $tq) {
|
||||
$tag_ids = $database->get_col(
|
||||
$database->engine->scoreql_to_sql(
|
||||
$database->scoreql_to_sql(
|
||||
"SELECT id FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"
|
||||
),
|
||||
array("tag"=>$tq->tag));
|
||||
@ -1015,7 +1015,7 @@ class Tag {
|
||||
|
||||
global $database;
|
||||
$newtag = $database->get_one(
|
||||
$database->engine->scoreql_to_sql("SELECT newtag FROM aliases WHERE SCORE_STRNORM(oldtag)=SCORE_STRNORM(:tag)"),
|
||||
$database->scoreql_to_sql("SELECT newtag FROM aliases WHERE SCORE_STRNORM(oldtag)=SCORE_STRNORM(:tag)"),
|
||||
array("tag"=>$tag));
|
||||
if(!empty($newtag)) {
|
||||
return $newtag;
|
||||
|
@ -45,7 +45,7 @@ class User {
|
||||
global $config, $database;
|
||||
$row = $database->cache->get("user-session-$name-$session");
|
||||
if(!$row) {
|
||||
if($database->engine->name === "mysql") {
|
||||
if($database->get_driver_name() === "mysql") {
|
||||
$query = "SELECT * FROM users WHERE name = :name AND md5(concat(pass, :ip)) = :sess";
|
||||
}
|
||||
else {
|
||||
|
@ -91,7 +91,7 @@ class CommentList extends Extension {
|
||||
// the whole history
|
||||
if($config->get_int("ext_comments_version") < 1) {
|
||||
$database->create_table("comments", "
|
||||
id {$database->engine->auto_increment},
|
||||
id SCORE_AIPK,
|
||||
image_id INTEGER NOT NULL,
|
||||
owner_id INTEGER NOT NULL,
|
||||
owner_ip CHAR(16) NOT NULL,
|
||||
@ -390,12 +390,12 @@ class CommentList extends Extension {
|
||||
global $database;
|
||||
|
||||
// sqlite fails at intervals
|
||||
if($database->engine->name === "sqlite") return false;
|
||||
if($database->get_driver_name() === "sqlite") return false;
|
||||
|
||||
$window = int_escape($config->get_int('comment_window'));
|
||||
$max = int_escape($config->get_int('comment_limit'));
|
||||
|
||||
if($database->engine->name == "mysql") $window_sql = "interval $window minute";
|
||||
if($database->get_driver_name() == "mysql") $window_sql = "interval $window minute";
|
||||
else $window_sql = "interval '$window minute'";
|
||||
|
||||
// window doesn't work as an SQL param because it's inside quotes >_<
|
||||
|
@ -44,7 +44,7 @@ class ET extends Extension {
|
||||
$info['sys_shimmie'] = VERSION;
|
||||
$info['sys_schema'] = $config->get_string("db_version");
|
||||
$info['sys_php'] = phpversion();
|
||||
$info['sys_db'] = $database->engine->name;
|
||||
$info['sys_db'] = $database->get_driver_name();
|
||||
$info['sys_os'] = php_uname();
|
||||
$info['sys_disk'] = to_shorthand_int(disk_total_space("./") - disk_free_space("./")) . " / " .
|
||||
to_shorthand_int(disk_total_space("./"));
|
||||
|
@ -155,7 +155,7 @@ class ImageBan extends Extension {
|
||||
$args[] = "%".$_GET['reason']."%";
|
||||
}
|
||||
$where = implode(" AND ", $where);
|
||||
$bans = $database->get_all($database->engine->scoreql_to_sql("
|
||||
$bans = $database->get_all($database->scoreql_to_sql("
|
||||
SELECT *
|
||||
FROM image_bans
|
||||
WHERE $where
|
||||
|
@ -197,7 +197,7 @@ class IPBan extends Extension {
|
||||
private function block(/*string*/ $remote) {
|
||||
global $config, $database;
|
||||
|
||||
$prefix = ($database->engine->name == "sqlite" ? "bans." : "");
|
||||
$prefix = ($database->get_driver_name() == "sqlite" ? "bans." : "");
|
||||
|
||||
$bans = $this->get_active_bans();
|
||||
|
||||
|
@ -15,8 +15,8 @@ class IPBanTheme extends Themelet {
|
||||
global $database, $user;
|
||||
$h_bans = "";
|
||||
$n = 0;
|
||||
$prefix = ($database->engine->name == "sqlite" ? "bans." : "");
|
||||
$prefix2 = ($database->engine->name == "sqlite" ? "users." : "");
|
||||
$prefix = ($database->get_driver_name() == "sqlite" ? "bans." : "");
|
||||
$prefix2 = ($database->get_driver_name() == "sqlite" ? "users." : "");
|
||||
foreach($bans as $ban) {
|
||||
$end_human = date('Y-m-d', $ban[$prefix.'end_timestamp']);
|
||||
$h_bans .= "
|
||||
|
@ -58,7 +58,7 @@ class LogDatabase extends Extension {
|
||||
$args["module"] = $_GET["module"];
|
||||
}
|
||||
if(!empty($_GET["user"])) {
|
||||
if($database->engine->name == "pgsql") {
|
||||
if($database->get_driver_name() == "pgsql") {
|
||||
if(preg_match("#\d+\.\d+\.\d+\.\d+(/\d+)?#", $_GET["user"])) {
|
||||
$wheres[] = "(username = :user1 OR text(address) = :user2)";
|
||||
$args["user1"] = $_GET["user"];
|
||||
|
@ -153,7 +153,7 @@ class TagList extends Extension {
|
||||
|
||||
$tags_min = $this->get_tags_min();
|
||||
|
||||
$tag_data = $database->get_col($database->engine->scoreql_to_sql("
|
||||
$tag_data = $database->get_col($database->scoreql_to_sql("
|
||||
SELECT DISTINCT
|
||||
SCORE_STRNORM(substr(tag, 1, 1))
|
||||
FROM tags
|
||||
@ -192,7 +192,7 @@ class TagList extends Extension {
|
||||
if(file_exists($cache_key)) {return file_get_contents($cache_key);}
|
||||
|
||||
// SHIT: PDO/pgsql has problems using the same named param twice -_-;;
|
||||
$tag_data = $database->get_all($database->engine->scoreql_to_sql("
|
||||
$tag_data = $database->get_all($database->scoreql_to_sql("
|
||||
SELECT
|
||||
tag,
|
||||
FLOOR(LOG(2.7, LOG(2.7, count - :tags_min2 + 1)+1)*1.5*100)/100 AS scaled
|
||||
@ -228,7 +228,7 @@ class TagList extends Extension {
|
||||
$cache_key = data_path("cache/tag_alpha-" . md5("ta" . $tags_min . $starts_with) . ".html");
|
||||
if(file_exists($cache_key)) {return file_get_contents($cache_key);}
|
||||
|
||||
$tag_data = $database->get_all($database->engine->scoreql_to_sql("
|
||||
$tag_data = $database->get_all($database->scoreql_to_sql("
|
||||
SELECT tag, count
|
||||
FROM tags
|
||||
WHERE count >= :tags_min
|
||||
|
@ -28,7 +28,7 @@ class Upgrade extends Extension {
|
||||
$config->set_bool("in_upgrade", true);
|
||||
$config->set_int("db_version", 8);
|
||||
|
||||
$database->execute($database->engine->scoreql_to_sql(
|
||||
$database->execute($database->scoreql_to_sql(
|
||||
"ALTER TABLE images ADD COLUMN locked SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N"
|
||||
));
|
||||
|
||||
@ -40,7 +40,7 @@ class Upgrade extends Extension {
|
||||
$config->set_bool("in_upgrade", true);
|
||||
$config->set_int("db_version", 9);
|
||||
|
||||
if($database->engine->name == 'mysql') {
|
||||
if($database->get_driver_name() == 'mysql') {
|
||||
$tables = $database->get_col("SHOW TABLES");
|
||||
foreach($tables as $table) {
|
||||
log_info("upgrade", "converting $table to innodb");
|
||||
|
Loading…
x
Reference in New Issue
Block a user