more pdo compat

This commit is contained in:
Shish 2011-01-01 15:58:09 +00:00
parent 9e00675900
commit 8d978aa06a
4 changed files with 39 additions and 42 deletions

View File

@ -148,7 +148,7 @@ class Image {
else { else {
$querylet = Image::build_search_querylet($tags); $querylet = Image::build_search_querylet($tags);
$result = $database->execute($querylet->sql, $querylet->variables); $result = $database->execute($querylet->sql, $querylet->variables);
return $result->RecordCount(); return $result->rowCount();
} }
} }
@ -365,7 +365,7 @@ class Image {
public function set_source($source) { public function set_source($source) {
global $database; global $database;
if(empty($source)) $source = null; if(empty($source)) $source = null;
$database->execute("UPDATE images SET source=? WHERE id=?", array($source, $this->id)); $database->execute("UPDATE images SET source=:source WHERE id=:id", array("source"=>$source, "id"=>$this->id));
} }
@ -378,7 +378,7 @@ class Image {
$sln = $database->engine->scoreql_to_sql("SCORE_BOOL_$ln"); $sln = $database->engine->scoreql_to_sql("SCORE_BOOL_$ln");
$sln = str_replace("'", "", $sln); $sln = str_replace("'", "", $sln);
$sln = str_replace('"', "", $sln); $sln = str_replace('"', "", $sln);
$database->execute("UPDATE images SET locked=? WHERE id=?", array($sln, $this->id)); $database->execute("UPDATE images SET locked=:yn WHERE id=:id", array("yn"=>$sln, "id"=>$this->id));
} }
/** /**
@ -390,8 +390,8 @@ class Image {
global $database; global $database;
$database->execute( $database->execute(
"UPDATE tags SET count = count - 1 WHERE id IN ". "UPDATE tags SET count = count - 1 WHERE id IN ".
"(SELECT tag_id FROM image_tags WHERE image_id = ?)", array($this->id)); "(SELECT tag_id FROM image_tags WHERE image_id = :id)", array("id"=>$this->id));
$database->execute("DELETE FROM image_tags WHERE image_id=?", array($this->id)); $database->execute("DELETE FROM image_tags WHERE image_id=:id", array("id"=>$this->id));
} }
/** /**
@ -411,30 +411,30 @@ class Image {
foreach($tags as $tag) { foreach($tags as $tag) {
$id = $database->db->GetOne( $id = $database->db->GetOne(
$database->engine->scoreql_to_sql( $database->engine->scoreql_to_sql(
"SELECT id FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(?)" "SELECT id FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"
), ),
array($tag)); array("tag"=>$tag));
if(empty($id)) { if(empty($id)) {
// a new tag // a new tag
$database->execute( $database->execute(
"INSERT INTO tags(tag) VALUES (?)", "INSERT INTO tags(tag) VALUES (:tag)",
array($tag)); array("tag"=>$tag));
$database->execute( $database->execute(
"INSERT INTO image_tags(image_id, tag_id) "INSERT INTO image_tags(image_id, tag_id)
VALUES(?, (SELECT id FROM tags WHERE tag = ?))", VALUES(:id, (SELECT id FROM tags WHERE tag = :tag))",
array($this->id, $tag)); array("id"=>$this->id, "tag"=>$tag));
} }
else { else {
// user of an existing tag // user of an existing tag
$database->execute( $database->execute(
"INSERT INTO image_tags(image_id, tag_id) VALUES(?, ?)", "INSERT INTO image_tags(image_id, tag_id) VALUES(:iid, :tid)",
array($this->id, $id)); array("iid"=>$this->id, "tid"=>$id));
} }
$database->execute( $database->execute(
$database->engine->scoreql_to_sql( $database->engine->scoreql_to_sql(
"UPDATE tags SET count = count + 1 WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(?)" "UPDATE tags SET count = count + 1 WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"
), ),
array($tag)); array("tag"=>$tag));
} }
log_info("core-image", "Tags for Image #{$this->id} set to: ".implode(" ", $tags)); log_info("core-image", "Tags for Image #{$this->id} set to: ".implode(" ", $tags));
@ -447,7 +447,7 @@ class Image {
public function delete() { public function delete() {
global $database; global $database;
$this->delete_tags_from_image(); $this->delete_tags_from_image();
$database->execute("DELETE FROM images WHERE id=?", array($this->id)); $database->execute("DELETE FROM images WHERE id=:id", array("id"=>$this->id));
log_info("core-image", "Deleted Image #{$this->id} ({$this->hash})"); log_info("core-image", "Deleted Image #{$this->id} ({$this->hash})");
unlink($this->get_image_filename()); unlink($this->get_image_filename());
@ -595,8 +595,8 @@ class Image {
$query = new Querylet($database->engine->scoreql_to_sql(" $query = new Querylet($database->engine->scoreql_to_sql("
SELECT images.* FROM images SELECT images.* FROM images
JOIN image_tags ON images.id = image_tags.image_id JOIN image_tags ON images.id = image_tags.image_id
WHERE tag_id = (SELECT tags.id FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(?)) WHERE tag_id = (SELECT tags.id FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag))
"), array($tag_querylets[0]->tag)); "), array("tag"=>$tag_querylets[0]->tag));
if(strlen($img_search->sql) > 0) { if(strlen($img_search->sql) > 0) {
$query->append_sql(" AND "); $query->append_sql(" AND ");
@ -613,9 +613,9 @@ class Image {
foreach($tag_querylets as $tq) { foreach($tag_querylets as $tq) {
$tag_ids = $database->db->GetCol( $tag_ids = $database->db->GetCol(
$database->engine->scoreql_to_sql( $database->engine->scoreql_to_sql(
"SELECT id FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(?)" "SELECT id FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"
), ),
array($tq->tag)); array("tag"=>$tq->tag));
if($tq->positive) { if($tq->positive) {
$positive_tag_id_array = array_merge($positive_tag_id_array, $tag_ids); $positive_tag_id_array = array_merge($positive_tag_id_array, $tag_ids);
$tags_ok = count($tag_ids) > 0; $tags_ok = count($tag_ids) > 0;
@ -728,8 +728,8 @@ class Image {
$terms = array(); $terms = array();
foreach($tag_querylets as $tq) { foreach($tag_querylets as $tq) {
$sign = $tq->positive ? "+" : "-"; $sign = $tq->positive ? "+" : "-";
$sql .= " $sign (tag LIKE ?)"; $sql .= " $sign (tag LIKE :tag)";
$terms[] = $tq->tag; $terms["tag"] = $tq->tag;
if($sign == "+") $positive_tag_count++; if($sign == "+") $positive_tag_count++;
else $negative_tag_count++; else $negative_tag_count++;
@ -768,7 +768,7 @@ class Image {
SELECT images.*, UNIX_TIMESTAMP(posted) AS posted_timestamp SELECT images.*, UNIX_TIMESTAMP(posted) AS posted_timestamp
FROM tags, image_tags, images FROM tags, image_tags, images
WHERE WHERE
tag LIKE ? tag LIKE :tag
AND tags.id = image_tags.tag_id AND tags.id = image_tags.tag_id
AND image_tags.image_id = images.id AND image_tags.image_id = images.id
", ",
@ -788,7 +788,7 @@ class Image {
$tag_id_array = array(); $tag_id_array = array();
$tags_ok = true; $tags_ok = true;
foreach($tag_search->variables as $tag) { foreach($tag_search->variables as $tag) {
$tag_ids = $database->db->GetCol("SELECT id FROM tags WHERE tag LIKE ?", array($tag)); $tag_ids = $database->get_col("SELECT id FROM tags WHERE tag LIKE :tag", array("tag"=>$tag));
$tag_id_array = array_merge($tag_id_array, $tag_ids); $tag_id_array = array_merge($tag_id_array, $tag_ids);
$tags_ok = count($tag_ids) > 0; $tags_ok = count($tag_ids) > 0;
if(!$tags_ok) break; if(!$tags_ok) break;
@ -803,10 +803,10 @@ class Image {
JOIN tags ON image_tags.tag_id = tags.id JOIN tags ON image_tags.tag_id = tags.id
WHERE tags.id IN ({$tag_id_list}) WHERE tags.id IN ({$tag_id_list})
GROUP BY images.id GROUP BY images.id
HAVING score = ?", HAVING score = :score",
array_merge( array_merge(
$tag_search->variables, $tag_search->variables,
array($positive_tag_count) array("score"=>$positive_tag_count)
) )
); );
$query = new Querylet(" $query = new Querylet("
@ -895,7 +895,7 @@ class Tag {
assert(is_string($tag)); assert(is_string($tag));
global $database; global $database;
$newtag = $database->db->GetOne("SELECT newtag FROM aliases WHERE oldtag=?", array($tag)); $newtag = $database->get_one("SELECT newtag FROM aliases WHERE oldtag=:tag", array("tag"=>$tag));
if(!empty($newtag)) { if(!empty($newtag)) {
return $newtag; return $newtag;
} else { } else {

View File

@ -53,14 +53,14 @@ class User {
public static function by_id($id) { public static function by_id($id) {
assert(is_numeric($id)); assert(is_numeric($id));
global $database; global $database;
$row = $database->get_row("SELECT * FROM users WHERE id = ?", array($id)); $row = $database->get_row("SELECT * FROM users WHERE id = :id", array("id"=>$id));
return is_null($row) ? null : new User($row); return is_null($row) ? null : new User($row);
} }
public static function by_name($name) { public static function by_name($name) {
assert(is_string($name)); assert(is_string($name));
global $database; global $database;
$row = $database->get_row("SELECT * FROM users WHERE name = ?", array($name)); $row = $database->get_row("SELECT * FROM users WHERE name = :name", array("name"=>$name));
return is_null($row) ? null : new User($row); return is_null($row) ? null : new User($row);
} }
@ -69,7 +69,7 @@ class User {
assert(is_string($hash)); assert(is_string($hash));
assert(strlen($hash) == 32); assert(strlen($hash) == 32);
global $database; global $database;
$row = $database->get_row("SELECT * FROM users WHERE name = ? AND pass = ?", array($name, $hash)); $row = $database->get_row("SELECT * FROM users WHERE name = :name AND pass = :hash", array("name"=>$name, "hash"=>$hash));
return is_null($row) ? null : new User($row); return is_null($row) ? null : new User($row);
} }
@ -77,7 +77,7 @@ class User {
assert(is_numeric($offset)); assert(is_numeric($offset));
assert(is_numeric($limit)); assert(is_numeric($limit));
global $database; global $database;
$rows = $database->get_all("SELECT * FROM users WHERE id >= ? AND id < ?", array($offset, $offset+$limit)); $rows = $database->get_all("SELECT * FROM users WHERE id >= :start AND id < :end", array("start"=>$offset, "end"=>$offset+$limit));
return array_map("_new_user", $rows); return array_map("_new_user", $rows);
} }
@ -119,20 +119,20 @@ class User {
assert(is_bool($admin)); assert(is_bool($admin));
global $database; global $database;
$yn = $admin ? 'Y' : 'N'; $yn = $admin ? 'Y' : 'N';
$database->Execute("UPDATE users SET admin=? WHERE id=?", array($yn, $this->id)); $database->Execute("UPDATE users SET admin=:yn WHERE id=:id", array("yn"=>$yn, "id"=>$this->id));
log_info("core-user", "Made {$this->name} admin=$yn"); log_info("core-user", "Made {$this->name} admin=$yn");
} }
public function set_password($password) { public function set_password($password) {
global $database; global $database;
$hash = md5(strtolower($this->name) . $password); $hash = md5(strtolower($this->name) . $password);
$database->Execute("UPDATE users SET pass=? WHERE id=?", array($hash, $this->id)); $database->Execute("UPDATE users SET pass=:hash WHERE id=:id", array("hash"=>$hash, "id"=>$this->id));
log_info("core-user", "Set password for {$this->name}"); log_info("core-user", "Set password for {$this->name}");
} }
public function set_email($address) { public function set_email($address) {
global $database; global $database;
$database->Execute("UPDATE users SET email=? WHERE id=?", array($address, $this->id)); $database->Execute("UPDATE users SET email=:email WHERE id=:id", array("email"=>$address, "id"=>$this->id));
log_info("core-user", "Set email for {$this->name}"); log_info("core-user", "Set email for {$this->name}");
} }
@ -181,6 +181,5 @@ class User {
public function check_auth_token() { public function check_auth_token() {
return ($_POST["auth_token"] == $this->get_auth_token()); return ($_POST["auth_token"] == $this->get_auth_token());
} }
} }
?> ?>

View File

@ -51,7 +51,7 @@ class Comment {
public static function count_comments_by_user($user) { public static function count_comments_by_user($user) {
global $database; global $database;
return $database->db->GetOne("SELECT COUNT(*) AS count FROM comments WHERE owner_id=:owner_id", array("owner_id"=>$user->id)); return $database->get_one("SELECT COUNT(*) AS count FROM comments WHERE owner_id=:owner_id", array("owner_id"=>$user->id));
} }
public function get_owner() { public function get_owner() {
@ -265,12 +265,12 @@ class CommentList extends SimpleExtension {
"; ";
$result = $database->Execute($get_threads, array("limit"=>$threads_per_page, "offset"=>$start)); $result = $database->Execute($get_threads, array("limit"=>$threads_per_page, "offset"=>$start));
$total_pages = (int)($database->db->GetOne("SELECT COUNT(c1) FROM (SELECT COUNT(image_id) AS c1 FROM comments GROUP BY image_id) AS s1") / 10); $total_pages = (int)($database->get_one("SELECT COUNT(c1) FROM (SELECT COUNT(image_id) AS c1 FROM comments GROUP BY image_id) AS s1") / 10);
$images = array(); $images = array();
while(!$result->EOF) { while($row = $result->fetch()) {
$image = Image::by_id($result->fields["image_id"]); $image = Image::by_id($row["image_id"]);
$comments = $this->get_comments($image->id); $comments = $this->get_comments($image->id);
if(class_exists("Ratings")) { if(class_exists("Ratings")) {
if(strpos($user_ratings, $image->rating) === FALSE) { if(strpos($user_ratings, $image->rating) === FALSE) {
@ -278,7 +278,6 @@ class CommentList extends SimpleExtension {
} }
} }
if(!is_null($image)) $images[] = array($image, $comments); if(!is_null($image)) $images[] = array($image, $comments);
$result->MoveNext();
} }
$this->theme->display_comment_list($images, $current_page, $total_pages, $this->can_comment()); $this->theme->display_comment_list($images, $current_page, $total_pages, $this->can_comment());

View File

@ -310,7 +310,7 @@ class TagList implements Extension {
foreach($wild_tags as $tag) { foreach($wild_tags as $tag) {
$tag = str_replace("*", "%", $tag); $tag = str_replace("*", "%", $tag);
$tag = str_replace("?", "_", $tag); $tag = str_replace("?", "_", $tag);
$tag_ids = $database->db->GetCol("SELECT id FROM tags WHERE tag LIKE :tag", array("tag"=>$tag)); $tag_ids = $database->get_col("SELECT id FROM tags WHERE tag LIKE :tag", array("tag"=>$tag));
// $search_tags = array_merge($search_tags, // $search_tags = array_merge($search_tags,
// $database->db->GetCol("SELECT tag FROM tags WHERE tag LIKE :tag", array("tag"=>$tag))); // $database->db->GetCol("SELECT tag FROM tags WHERE tag LIKE :tag", array("tag"=>$tag)));
$tag_id_array = array_merge($tag_id_array, $tag_ids); $tag_id_array = array_merge($tag_id_array, $tag_ids);
@ -339,7 +339,6 @@ class TagList implements Extension {
$args = array("limit"=>$config->get_int('tag_list_length')); $args = array("limit"=>$config->get_int('tag_list_length'));
$related_tags = $database->get_all($query, $args); $related_tags = $database->get_all($query, $args);
print $database->db->ErrorMsg();
if(count($related_tags) > 0) { if(count($related_tags) > 0) {
$this->theme->display_refine_block($page, $related_tags, $wild_tags); $this->theme->display_refine_block($page, $related_tags, $wild_tags);
} }