From 633d5c5348b3d7a6b9b746a110802e758f968834 Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 25 Jun 2023 17:22:32 +0000 Subject: [PATCH] INSERT ... RETURNING is well-supported now --- 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, 47 insertions(+), 55 deletions(-) diff --git a/core/database.php b/core/database.php index 78f8cf80..44cec07d 100644 --- a/core/database.php +++ b/core/database.php @@ -280,20 +280,6 @@ 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 87afbdb5..7997414f 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -360,7 +360,7 @@ class Image } if (is_null($this->id)) { - $database->execute( + $this->id = $database->get_one( "INSERT INTO images( owner_id, owner_ip, filename, filesize, @@ -374,7 +374,8 @@ 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, @@ -383,7 +384,6 @@ 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 5592c0f0..1c875bfa 100644 --- a/core/imageboard/tag.php +++ b/core/imageboard/tag.php @@ -105,12 +105,8 @@ class Tag ); if (empty($id)) { // a new tag - $database->execute( - "INSERT INTO tags(tag) VALUES (:tag)", - ["tag"=>$tag] - ); $id = $database->get_one( - "SELECT id FROM tags WHERE LOWER(tag) = LOWER(:tag)", + "INSERT INTO tags(tag) VALUES (:tag) RETURNING id", ["tag"=>$tag] ); } diff --git a/core/install.php b/core/install.php index b18c14f8..a79530a3 100644 --- a/core/install.php +++ b/core/install.php @@ -241,11 +241,20 @@ function create_tables(Database $db) "); $db->execute("CREATE INDEX users_name_idx ON users(name)", []); - $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')]); + $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] + ); 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 7f03941c..e7b6fd75 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; - $database->execute(" + return $database->get_one(" 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 ccf71069..8373a442 100644 --- a/ext/blocks/main.php +++ b/ext/blocks/main.php @@ -65,11 +65,12 @@ 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()) { - $database->execute(" + $blockID = $database->get_one(" 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 #".($database->get_last_insert_id('blocks_id_seq'))." (".$_POST['title'].")"); + log_info("blocks", "Added Block #$blockID (".$_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 bf588318..6d3ab50e 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, "/"); } - $database->execute( - "INSERT INTO comments(image_id, owner_id, owner_ip, posted, comment) ". - "VALUES(:image_id, :user_id, :remote_addr, now(), :comment)", + $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", ["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 443af4ac..05619839 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; - $database->execute( + $threadID = $database->get_one( " INSERT INTO forum_threads (title, sticky, user_id, date, uptodate) VALUES - (:title, :sticky, :user_id, now(), now())", + (:title, :sticky, :user_id, now(), now()) + RETURNING id + ", ['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,13 +342,12 @@ class Forum extends Extension $message = substr($message, 0, $max_characters); global $database; - $database->execute(" + $postID = $database->get_one(" 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 67df6f02..d414de09 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"]); - $database->execute( + $noteID = $database->get_one( " 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)", + VALUES (:enable, :image_id, :user_id, :user_ip, now(), :x1, :y1, :height, :width, :note) + RETURNING id + ", ['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; - $database->execute( + $resultID = $database->get_one( " INSERT INTO note_request (image_id, user_id, date) - VALUES (:image_id, :user_id, now())", + VALUES (:image_id, :user_id, now()) + RETURNING id + ", ['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 85fff391..13507b03 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."); } - $database->execute( + $poolID = $database->get_one( " INSERT INTO pools (user_id, public, title, description, date) - VALUES (:uid, :public, :title, :desc, now())", + VALUES (:uid, :public, :title, :desc, now()) + RETURNING id + ", ["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 1b279f50..c15c85cc 100644 --- a/ext/user/main.php +++ b/ext/user/main.php @@ -614,11 +614,12 @@ class UserPage extends Extension $need_admin = ($database->get_one("SELECT COUNT(*) FROM users WHERE class='admin'") == 0); $class = $need_admin ? 'admin' : 'user'; - $database->execute( - "INSERT INTO users (name, pass, joindate, email, class) VALUES (:username, :hash, now(), :email, :class)", + $uid = $database->get_one( + "INSERT INTO users (name, pass, joindate, email, class) + VALUES (:username, :hash, now(), :email, :class) + RETURNING id", ["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);