Revert "INSERT ... RETURNING is well-supported now" - mysql doesn't...

This reverts commit 633d5c5348.
This commit is contained in:
Shish 2023-06-25 20:31:11 +00:00
parent 1f908bdd05
commit 33f32f7b22
11 changed files with 55 additions and 47 deletions

View file

@ -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.
*/

View file

@ -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 ".

View file

@ -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]
);
}

View file

@ -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", "

View file

@ -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

View file

@ -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"));

View file

@ -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);

View file

@ -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]);

View file

@ -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}");
}

View file

@ -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;

View file

@ -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);