Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of INSERT ... RETURNING #923

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions core/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,6 @@ public function exists(string $query, array $args = []): bool
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->get_db()->lastInsertId($seq);
} else {
$id = $this->get_db()->lastInsertId();
}
assert(is_numeric($id));
return (int)$id;
}

/**
* Create a table from pseudo-SQL.
*/
Expand Down
6 changes: 3 additions & 3 deletions core/imageboard/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public function save_to_db()
}

if (is_null($this->id)) {
$database->execute(
$this->id = $database->get_one(
"INSERT INTO images(
owner_id, owner_ip,
filename, filesize,
Expand All @@ -242,7 +242,8 @@ public function save_to_db()
:hash, :mime, :ext,
0, 0,
:posted, :source
)",
)
RETURNING id",
[
"owner_id" => $user->id, "owner_ip" => get_real_ip(),
"filename" => $cut_name, "filesize" => $this->filesize,
Expand All @@ -251,7 +252,6 @@ public function save_to_db()
"posted" => $this->posted, "source" => $this->source
]
);
$this->id = $database->get_last_insert_id('images_id_seq');
} else {
$database->execute(
"UPDATE images SET ".
Expand Down
6 changes: 1 addition & 5 deletions core/imageboard/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,8 @@ public static function get_or_create_id(string $tag): int
);
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]
);
}
Expand Down
15 changes: 12 additions & 3 deletions core/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,20 @@ class VARCHAR(32) NOT NULL DEFAULT 'user',
");
$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", "
Expand Down
4 changes: 2 additions & 2 deletions ext/artists/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -750,11 +750,11 @@ private function add_artist(): int
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
Expand Down
11 changes: 6 additions & 5 deletions ext/blocks/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ public function onPageRequest(PageRequestEvent $event)
if ($event->page_matches("blocks") && $user->can(Permissions::MANAGE_BLOCKS)) {
if ($event->get_arg(0) == "add") {
if ($user->check_auth_token()) {
$database->execute("
INSERT INTO blocks (pages, title, area, priority, content, userclass)
VALUES (:pages, :title, :area, :priority, :content, :userclass)
", ['pages' => $_POST['pages'], 'title' => $_POST['title'], 'area' => $_POST['area'], 'priority' => (int)$_POST['priority'], 'content' => $_POST['content'], 'userclass' => $_POST['userclass']]);
log_info("blocks", "Added Block #".($database->get_last_insert_id('blocks_id_seq'))." (".$_POST['title'].")");
$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 #$blockID (".$_POST['title'].")");
$cache->delete("blocks");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("blocks/list"));
Expand Down
8 changes: 4 additions & 4 deletions ext/comment/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,12 +616,12 @@ private function add_comment_wrapper(int $image_id, User $user, string $comment)
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);
Expand Down
13 changes: 6 additions & 7 deletions ext/forum/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,17 @@ private function save_new_thread(User $user): int
$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;
Expand All @@ -342,13 +342,12 @@ private function save_new_post(int $threadID, User $user): void
$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]);
Expand Down
16 changes: 8 additions & 8 deletions ext/notes/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,15 @@ private function add_new_note()
$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]);
Expand All @@ -278,15 +278,15 @@ private function add_note_request()
$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}");
}

Expand Down
8 changes: 4 additions & 4 deletions ext/pools/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,14 +675,14 @@ public function onPoolCreation(PoolCreationEvent $event)
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;
Expand Down
7 changes: 4 additions & 3 deletions ext/user/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,12 @@ private function create_user(UserCreationEvent $event): User
$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);

Expand Down
Loading