INSERT ... RETURNING is well-supported now
This commit is contained in:
parent
a94ead8c04
commit
633d5c5348
11 changed files with 47 additions and 55 deletions
|
@ -280,20 +280,6 @@ class Database
|
||||||
return true;
|
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.
|
* Create a table from pseudo-SQL.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -360,7 +360,7 @@ class Image
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_null($this->id)) {
|
if (is_null($this->id)) {
|
||||||
$database->execute(
|
$this->id = $database->get_one(
|
||||||
"INSERT INTO images(
|
"INSERT INTO images(
|
||||||
owner_id, owner_ip,
|
owner_id, owner_ip,
|
||||||
filename, filesize,
|
filename, filesize,
|
||||||
|
@ -374,7 +374,8 @@ class Image
|
||||||
:hash, :mime, :ext,
|
:hash, :mime, :ext,
|
||||||
0, 0,
|
0, 0,
|
||||||
:posted, :source
|
:posted, :source
|
||||||
)",
|
)
|
||||||
|
RETURNING id",
|
||||||
[
|
[
|
||||||
"owner_id" => $user->id, "owner_ip" => get_real_ip(),
|
"owner_id" => $user->id, "owner_ip" => get_real_ip(),
|
||||||
"filename" => $cut_name, "filesize" => $this->filesize,
|
"filename" => $cut_name, "filesize" => $this->filesize,
|
||||||
|
@ -383,7 +384,6 @@ class Image
|
||||||
"posted" => $this->posted, "source" => $this->source
|
"posted" => $this->posted, "source" => $this->source
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
$this->id = $database->get_last_insert_id('images_id_seq');
|
|
||||||
} else {
|
} else {
|
||||||
$database->execute(
|
$database->execute(
|
||||||
"UPDATE images SET ".
|
"UPDATE images SET ".
|
||||||
|
|
|
@ -105,12 +105,8 @@ class Tag
|
||||||
);
|
);
|
||||||
if (empty($id)) {
|
if (empty($id)) {
|
||||||
// a new tag
|
// a new tag
|
||||||
$database->execute(
|
|
||||||
"INSERT INTO tags(tag) VALUES (:tag)",
|
|
||||||
["tag"=>$tag]
|
|
||||||
);
|
|
||||||
$id = $database->get_one(
|
$id = $database->get_one(
|
||||||
"SELECT id FROM tags WHERE LOWER(tag) = LOWER(:tag)",
|
"INSERT INTO tags(tag) VALUES (:tag) RETURNING id",
|
||||||
["tag"=>$tag]
|
["tag"=>$tag]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -241,11 +241,20 @@ function create_tables(Database $db)
|
||||||
");
|
");
|
||||||
$db->execute("CREATE INDEX users_name_idx ON users(name)", []);
|
$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']);
|
$anon_id = $db->get_one(
|
||||||
$db->execute("INSERT INTO config(name, value) VALUES(:name, :value)", ["name" => 'anon_id', "value" => $db->get_last_insert_id('users_id_seq')]);
|
"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) {
|
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", "
|
$db->create_table("images", "
|
||||||
|
|
|
@ -753,11 +753,11 @@ class Artists extends Extension
|
||||||
private function save_new_artist(string $name, string $notes): int
|
private function save_new_artist(string $name, string $notes): int
|
||||||
{
|
{
|
||||||
global $database, $user;
|
global $database, $user;
|
||||||
$database->execute("
|
return $database->get_one("
|
||||||
INSERT INTO artists (user_id, name, notes, created, updated)
|
INSERT INTO artists (user_id, name, notes, created, updated)
|
||||||
VALUES (:user_id, :name, :notes, now(), now())
|
VALUES (:user_id, :name, :notes, now(), now())
|
||||||
|
RETURNING id
|
||||||
", ['user_id'=>$user->id, 'name'=>$name, 'notes'=>$notes]);
|
", ['user_id'=>$user->id, 'name'=>$name, 'notes'=>$notes]);
|
||||||
return $database->get_last_insert_id('artists_id_seq');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function artist_exists(string $name): bool
|
private function artist_exists(string $name): bool
|
||||||
|
|
|
@ -65,11 +65,12 @@ class Blocks extends Extension
|
||||||
if ($event->page_matches("blocks") && $user->can(Permissions::MANAGE_BLOCKS)) {
|
if ($event->page_matches("blocks") && $user->can(Permissions::MANAGE_BLOCKS)) {
|
||||||
if ($event->get_arg(0) == "add") {
|
if ($event->get_arg(0) == "add") {
|
||||||
if ($user->check_auth_token()) {
|
if ($user->check_auth_token()) {
|
||||||
$database->execute("
|
$blockID = $database->get_one("
|
||||||
INSERT INTO blocks (pages, title, area, priority, content)
|
INSERT INTO blocks (pages, title, area, priority, content)
|
||||||
VALUES (: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']]);
|
", ['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");
|
$cache->delete("blocks");
|
||||||
$page->set_mode(PageMode::REDIRECT);
|
$page->set_mode(PageMode::REDIRECT);
|
||||||
$page->set_redirect(make_link("blocks/list"));
|
$page->set_redirect(make_link("blocks/list"));
|
||||||
|
|
|
@ -626,12 +626,12 @@ class CommentList extends Extension
|
||||||
if ($user->is_anonymous()) {
|
if ($user->is_anonymous()) {
|
||||||
$page->add_cookie("nocache", "Anonymous Commenter", time()+60*60*24, "/");
|
$page->add_cookie("nocache", "Anonymous Commenter", time()+60*60*24, "/");
|
||||||
}
|
}
|
||||||
$database->execute(
|
$cid = $database->get_one(
|
||||||
"INSERT INTO comments(image_id, owner_id, owner_ip, posted, comment) ".
|
"INSERT INTO comments(image_id, owner_id, owner_ip, posted, comment)
|
||||||
"VALUES(:image_id, :user_id, :remote_addr, now(), :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]
|
["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 = substr($comment, 0, 100);
|
||||||
$snippet = str_replace("\n", " ", $snippet);
|
$snippet = str_replace("\n", " ", $snippet);
|
||||||
$snippet = str_replace("\r", " ", $snippet);
|
$snippet = str_replace("\r", " ", $snippet);
|
||||||
|
|
|
@ -316,17 +316,17 @@ class Forum extends Extension
|
||||||
$sticky = !empty($_POST["sticky"]);
|
$sticky = !empty($_POST["sticky"]);
|
||||||
|
|
||||||
global $database;
|
global $database;
|
||||||
$database->execute(
|
$threadID = $database->get_one(
|
||||||
"
|
"
|
||||||
INSERT INTO forum_threads
|
INSERT INTO forum_threads
|
||||||
(title, sticky, user_id, date, uptodate)
|
(title, sticky, user_id, date, uptodate)
|
||||||
VALUES
|
VALUES
|
||||||
(:title, :sticky, :user_id, now(), now())",
|
(:title, :sticky, :user_id, now(), now())
|
||||||
|
RETURNING id
|
||||||
|
",
|
||||||
['title'=>$title, 'sticky'=>$sticky, 'user_id'=>$user->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}");
|
log_info("forum", "Thread {$threadID} created by {$user->name}");
|
||||||
|
|
||||||
return $threadID;
|
return $threadID;
|
||||||
|
@ -342,13 +342,12 @@ class Forum extends Extension
|
||||||
$message = substr($message, 0, $max_characters);
|
$message = substr($message, 0, $max_characters);
|
||||||
|
|
||||||
global $database;
|
global $database;
|
||||||
$database->execute("
|
$postID = $database->get_one("
|
||||||
INSERT INTO forum_posts (thread_id, user_id, date, message)
|
INSERT INTO forum_posts (thread_id, user_id, date, message)
|
||||||
VALUES (:thread_id, :user_id, now(), :message)
|
VALUES (:thread_id, :user_id, now(), :message)
|
||||||
|
RETURNING id
|
||||||
", ['thread_id'=>$threadID, 'user_id'=>$userID, 'message'=>$message]);
|
", ['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}");
|
log_info("forum", "Post {$postID} created by {$user->name}");
|
||||||
|
|
||||||
$database->execute("UPDATE forum_threads SET uptodate=now() WHERE id=:id", ['id'=>$threadID]);
|
$database->execute("UPDATE forum_threads SET uptodate=now() WHERE id=:id", ['id'=>$threadID]);
|
||||||
|
|
|
@ -255,15 +255,15 @@ class Notes extends Extension
|
||||||
$noteWidth = int_escape($_POST["note_width"]);
|
$noteWidth = int_escape($_POST["note_width"]);
|
||||||
$noteText = html_escape($_POST["note_text"]);
|
$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)
|
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]
|
['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}");
|
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]);
|
$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"]);
|
$image_id = int_escape($_POST["image_id"]);
|
||||||
$user_id = $user->id;
|
$user_id = $user->id;
|
||||||
|
|
||||||
$database->execute(
|
$resultID = $database->get_one(
|
||||||
"
|
"
|
||||||
INSERT INTO note_request (image_id, user_id, date)
|
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]
|
['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}");
|
log_info("notes", "Note requested {$resultID} by {$user->name}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -659,14 +659,14 @@ class Pools extends Extension
|
||||||
throw new PoolCreationException("A pool using this title already exists.");
|
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)
|
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]
|
["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}");
|
log_info("pools", "Pool {$poolID} created by {$user->name}");
|
||||||
|
|
||||||
$event->new_id = $poolID;
|
$event->new_id = $poolID;
|
||||||
|
|
|
@ -614,11 +614,12 @@ class UserPage extends Extension
|
||||||
$need_admin = ($database->get_one("SELECT COUNT(*) FROM users WHERE class='admin'") == 0);
|
$need_admin = ($database->get_one("SELECT COUNT(*) FROM users WHERE class='admin'") == 0);
|
||||||
$class = $need_admin ? 'admin' : 'user';
|
$class = $need_admin ? 'admin' : 'user';
|
||||||
|
|
||||||
$database->execute(
|
$uid = $database->get_one(
|
||||||
"INSERT INTO users (name, pass, joindate, email, class) VALUES (:username, :hash, now(), :email, :class)",
|
"INSERT INTO users (name, pass, joindate, email, class)
|
||||||
|
VALUES (:username, :hash, now(), :email, :class)
|
||||||
|
RETURNING id",
|
||||||
["username"=>$event->username, "hash"=>'', "email"=>$email, "class"=>$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 = User::by_name($event->username);
|
||||||
$new_user->set_password($event->password);
|
$new_user->set_password($event->password);
|
||||||
|
|
||||||
|
|
Reference in a new issue