From 33f32f7b220b92660c58e6c99e5f9f25a76d4398 Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 25 Jun 2023 20:31:11 +0000 Subject: [PATCH] Revert "INSERT ... RETURNING is well-supported now" - mysql doesn't... This reverts commit 633d5c5348b3d7a6b9b746a110802e758f968834. --- core/database.php | 14 ++++++++++++++ core/imageboard/image.php | 6 +++--- core/imageboard/tag.php | 6 +++++- core/install.php | 15 +++------------ ext/artists/main.php | 4 ++-- ext/blocks/main.php | 5 ++--- ext/comment/main.php | 8 ++++---- ext/forum/main.php | 13 +++++++------ ext/notes/main.php | 16 ++++++++-------- ext/pools/main.php | 8 ++++---- ext/user/main.php | 7 +++---- 11 files changed, 55 insertions(+), 47 deletions(-) diff --git a/core/database.php b/core/database.php index 44cec07d..78f8cf80 100644 --- a/core/database.php +++ b/core/database.php @@ -280,6 +280,20 @@ class Database return true; } + /** + * Get the ID of the last inserted row. + */ + public function get_last_insert_id(string $seq): int + { + if ($this->get_engine()->id == DatabaseDriverID::PGSQL) { + $id = $this->db->lastInsertId($seq); + } else { + $id = $this->db->lastInsertId(); + } + assert(is_numeric($id)); + return (int)$id; + } + /** * Create a table from pseudo-SQL. */ diff --git a/core/imageboard/image.php b/core/imageboard/image.php index d4f1141b..96ec8203 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -360,7 +360,7 @@ class Image } if (is_null($this->id)) { - $this->id = $database->get_one( + $database->execute( "INSERT INTO images( owner_id, owner_ip, filename, filesize, @@ -374,8 +374,7 @@ class Image :hash, :mime, :ext, 0, 0, :posted, :source - ) - RETURNING id", + )", [ "owner_id" => $user->id, "owner_ip" => get_real_ip(), "filename" => $cut_name, "filesize" => $this->filesize, @@ -384,6 +383,7 @@ class Image "posted" => $this->posted, "source" => $this->source ] ); + $this->id = $database->get_last_insert_id('images_id_seq'); } else { $database->execute( "UPDATE images SET ". diff --git a/core/imageboard/tag.php b/core/imageboard/tag.php index 1c875bfa..5592c0f0 100644 --- a/core/imageboard/tag.php +++ b/core/imageboard/tag.php @@ -105,8 +105,12 @@ class Tag ); if (empty($id)) { // a new tag + $database->execute( + "INSERT INTO tags(tag) VALUES (:tag)", + ["tag"=>$tag] + ); $id = $database->get_one( - "INSERT INTO tags(tag) VALUES (:tag) RETURNING id", + "SELECT id FROM tags WHERE LOWER(tag) = LOWER(:tag)", ["tag"=>$tag] ); } diff --git a/core/install.php b/core/install.php index a79530a3..b18c14f8 100644 --- a/core/install.php +++ b/core/install.php @@ -241,20 +241,11 @@ function create_tables(Database $db) "); $db->execute("CREATE INDEX users_name_idx ON users(name)", []); - $anon_id = $db->get_one( - "INSERT INTO users(name, pass, joindate, class) VALUES(:name, :pass, now(), :class) RETURNING id", - ["name" => 'Anonymous', "pass" => null, "class" => 'anonymous'] - ); - $db->execute( - "INSERT INTO config(name, value) VALUES(:name, :value)", - ["name" => 'anon_id', "value" => $anon_id] - ); + $db->execute("INSERT INTO users(name, pass, joindate, class) VALUES(:name, :pass, now(), :class)", ["name" => 'Anonymous', "pass" => null, "class" => 'anonymous']); + $db->execute("INSERT INTO config(name, value) VALUES(:name, :value)", ["name" => 'anon_id', "value" => $db->get_last_insert_id('users_id_seq')]); if (check_im_version() > 0) { - $db->execute( - "INSERT INTO config(name, value) VALUES(:name, :value)", - ["name" => 'thumb_engine', "value" => 'convert'] - ); + $db->execute("INSERT INTO config(name, value) VALUES(:name, :value)", ["name" => 'thumb_engine', "value" => 'convert']); } $db->create_table("images", " diff --git a/ext/artists/main.php b/ext/artists/main.php index e7b6fd75..7f03941c 100644 --- a/ext/artists/main.php +++ b/ext/artists/main.php @@ -753,11 +753,11 @@ class Artists extends Extension private function save_new_artist(string $name, string $notes): int { global $database, $user; - return $database->get_one(" + $database->execute(" INSERT INTO artists (user_id, name, notes, created, updated) VALUES (:user_id, :name, :notes, now(), now()) - RETURNING id ", ['user_id'=>$user->id, 'name'=>$name, 'notes'=>$notes]); + return $database->get_last_insert_id('artists_id_seq'); } private function artist_exists(string $name): bool diff --git a/ext/blocks/main.php b/ext/blocks/main.php index 8373a442..ccf71069 100644 --- a/ext/blocks/main.php +++ b/ext/blocks/main.php @@ -65,12 +65,11 @@ class Blocks extends Extension if ($event->page_matches("blocks") && $user->can(Permissions::MANAGE_BLOCKS)) { if ($event->get_arg(0) == "add") { if ($user->check_auth_token()) { - $blockID = $database->get_one(" + $database->execute(" INSERT INTO blocks (pages, title, area, priority, content) VALUES (:pages, :title, :area, :priority, :content) - RETURNING id ", ['pages'=>$_POST['pages'], 'title'=>$_POST['title'], 'area'=>$_POST['area'], 'priority'=>(int)$_POST['priority'], 'content'=>$_POST['content']]); - log_info("blocks", "Added Block #$blockID (".$_POST['title'].")"); + log_info("blocks", "Added Block #".($database->get_last_insert_id('blocks_id_seq'))." (".$_POST['title'].")"); $cache->delete("blocks"); $page->set_mode(PageMode::REDIRECT); $page->set_redirect(make_link("blocks/list")); diff --git a/ext/comment/main.php b/ext/comment/main.php index 6d3ab50e..bf588318 100644 --- a/ext/comment/main.php +++ b/ext/comment/main.php @@ -626,12 +626,12 @@ class CommentList extends Extension if ($user->is_anonymous()) { $page->add_cookie("nocache", "Anonymous Commenter", time()+60*60*24, "/"); } - $cid = $database->get_one( - "INSERT INTO comments(image_id, owner_id, owner_ip, posted, comment) - VALUES(:image_id, :user_id, :remote_addr, now(), :comment) - RETURNING id", + $database->execute( + "INSERT INTO comments(image_id, owner_id, owner_ip, posted, comment) ". + "VALUES(:image_id, :user_id, :remote_addr, now(), :comment)", ["image_id"=>$image_id, "user_id"=>$user->id, "remote_addr"=>get_real_ip(), "comment"=>$comment] ); + $cid = $database->get_last_insert_id('comments_id_seq'); $snippet = substr($comment, 0, 100); $snippet = str_replace("\n", " ", $snippet); $snippet = str_replace("\r", " ", $snippet); diff --git a/ext/forum/main.php b/ext/forum/main.php index 05619839..443af4ac 100644 --- a/ext/forum/main.php +++ b/ext/forum/main.php @@ -316,17 +316,17 @@ class Forum extends Extension $sticky = !empty($_POST["sticky"]); global $database; - $threadID = $database->get_one( + $database->execute( " INSERT INTO forum_threads (title, sticky, user_id, date, uptodate) VALUES - (:title, :sticky, :user_id, now(), now()) - RETURNING id - ", + (:title, :sticky, :user_id, now(), now())", ['title'=>$title, 'sticky'=>$sticky, 'user_id'=>$user->id] ); + $threadID = $database->get_last_insert_id("forum_threads_id_seq"); + log_info("forum", "Thread {$threadID} created by {$user->name}"); return $threadID; @@ -342,12 +342,13 @@ class Forum extends Extension $message = substr($message, 0, $max_characters); global $database; - $postID = $database->get_one(" + $database->execute(" INSERT INTO forum_posts (thread_id, user_id, date, message) VALUES (:thread_id, :user_id, now(), :message) - RETURNING id ", ['thread_id'=>$threadID, 'user_id'=>$userID, 'message'=>$message]); + $postID = $database->get_last_insert_id("forum_posts_id_seq"); + log_info("forum", "Post {$postID} created by {$user->name}"); $database->execute("UPDATE forum_threads SET uptodate=now() WHERE id=:id", ['id'=>$threadID]); diff --git a/ext/notes/main.php b/ext/notes/main.php index d414de09..67df6f02 100644 --- a/ext/notes/main.php +++ b/ext/notes/main.php @@ -255,15 +255,15 @@ class Notes extends Extension $noteWidth = int_escape($_POST["note_width"]); $noteText = html_escape($_POST["note_text"]); - $noteID = $database->get_one( + $database->execute( " INSERT INTO notes (enable, image_id, user_id, user_ip, date, x1, y1, height, width, note) - VALUES (:enable, :image_id, :user_id, :user_ip, now(), :x1, :y1, :height, :width, :note) - RETURNING id - ", + VALUES (:enable, :image_id, :user_id, :user_ip, now(), :x1, :y1, :height, :width, :note)", ['enable'=>1, 'image_id'=>$imageID, 'user_id'=>$user_id, 'user_ip'=>get_real_ip(), 'x1'=>$noteX1, 'y1'=>$noteY1, 'height'=>$noteHeight, 'width'=>$noteWidth, 'note'=>$noteText] ); + $noteID = $database->get_last_insert_id('notes_id_seq'); + log_info("notes", "Note added {$noteID} by {$user->name}"); $database->execute("UPDATE images SET notes=(SELECT COUNT(*) FROM notes WHERE image_id=:id1) WHERE id=:id2", ['id1'=>$imageID, 'id2'=>$imageID]); @@ -278,15 +278,15 @@ class Notes extends Extension $image_id = int_escape($_POST["image_id"]); $user_id = $user->id; - $resultID = $database->get_one( + $database->execute( " INSERT INTO note_request (image_id, user_id, date) - VALUES (:image_id, :user_id, now()) - RETURNING id - ", + VALUES (:image_id, :user_id, now())", ['image_id'=>$image_id, 'user_id'=>$user_id] ); + $resultID = $database->get_last_insert_id('note_request_id_seq'); + log_info("notes", "Note requested {$resultID} by {$user->name}"); } diff --git a/ext/pools/main.php b/ext/pools/main.php index 13507b03..85fff391 100644 --- a/ext/pools/main.php +++ b/ext/pools/main.php @@ -659,14 +659,14 @@ class Pools extends Extension throw new PoolCreationException("A pool using this title already exists."); } - $poolID = $database->get_one( + $database->execute( " INSERT INTO pools (user_id, public, title, description, date) - VALUES (:uid, :public, :title, :desc, now()) - RETURNING id - ", + VALUES (:uid, :public, :title, :desc, now())", ["uid" => $event->user->id, "public" => $event->public, "title" => $event->title, "desc" => $event->description] ); + + $poolID = $database->get_last_insert_id('pools_id_seq'); log_info("pools", "Pool {$poolID} created by {$user->name}"); $event->new_id = $poolID; diff --git a/ext/user/main.php b/ext/user/main.php index c15c85cc..1b279f50 100644 --- a/ext/user/main.php +++ b/ext/user/main.php @@ -614,12 +614,11 @@ class UserPage extends Extension $need_admin = ($database->get_one("SELECT COUNT(*) FROM users WHERE class='admin'") == 0); $class = $need_admin ? 'admin' : 'user'; - $uid = $database->get_one( - "INSERT INTO users (name, pass, joindate, email, class) - VALUES (:username, :hash, now(), :email, :class) - RETURNING id", + $database->execute( + "INSERT INTO users (name, pass, joindate, email, class) VALUES (:username, :hash, now(), :email, :class)", ["username"=>$event->username, "hash"=>'', "email"=>$email, "class"=>$class] ); + $uid = $database->get_last_insert_id('users_id_seq'); $new_user = User::by_name($event->username); $new_user->set_password($event->password);