[core] insert or update image in a single statement
This commit is contained in:
parent
107485d991
commit
b872c021e1
1 changed files with 46 additions and 71 deletions
|
@ -293,54 +293,48 @@ class Image implements \ArrayAccess
|
||||||
public function save_to_db(): void
|
public function save_to_db(): void
|
||||||
{
|
{
|
||||||
global $database, $user;
|
global $database, $user;
|
||||||
$cut_name = substr($this->filename, 0, 255);
|
|
||||||
|
|
||||||
if (is_null($this->posted) || $this->posted == "") {
|
if (is_null($this->posted) || $this->posted == "") {
|
||||||
$this->posted = date('Y-m-d H:i:s', time());
|
$this->posted = date('Y-m-d H:i:s', time());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_null($this->id)) {
|
$props_to_save = [
|
||||||
$database->execute(
|
"filename" => substr($this->filename, 0, 255),
|
||||||
"INSERT INTO images(
|
|
||||||
owner_id, owner_ip,
|
|
||||||
filename, filesize,
|
|
||||||
hash, mime, ext,
|
|
||||||
width, height,
|
|
||||||
posted, source
|
|
||||||
)
|
|
||||||
VALUES (
|
|
||||||
:owner_id, :owner_ip,
|
|
||||||
:filename, :filesize,
|
|
||||||
:hash, :mime, :ext,
|
|
||||||
0, 0,
|
|
||||||
:posted, :source
|
|
||||||
)",
|
|
||||||
[
|
|
||||||
"owner_id" => $user->id, "owner_ip" => get_real_ip(),
|
|
||||||
"filename" => $cut_name, "filesize" => $this->filesize,
|
|
||||||
"hash" => $this->hash, "mime" => strtolower($this->mime),
|
|
||||||
"ext" => strtolower($this->ext),
|
|
||||||
"posted" => $this->posted, "source" => $this->source
|
|
||||||
]
|
|
||||||
);
|
|
||||||
$this->id = $database->get_last_insert_id('images_id_seq');
|
|
||||||
} else {
|
|
||||||
$database->execute(
|
|
||||||
"UPDATE images SET ".
|
|
||||||
"filename = :filename, filesize = :filesize, hash = :hash, ".
|
|
||||||
"mime = :mime, ext = :ext, width = 0, height = 0, ".
|
|
||||||
"posted = :posted, source = :source ".
|
|
||||||
"WHERE id = :id",
|
|
||||||
[
|
|
||||||
"filename" => $cut_name,
|
|
||||||
"filesize" => $this->filesize,
|
"filesize" => $this->filesize,
|
||||||
"hash" => $this->hash,
|
"hash" => $this->hash,
|
||||||
"mime" => strtolower($this->mime),
|
"mime" => strtolower($this->mime),
|
||||||
"ext" => strtolower($this->ext),
|
"ext" => strtolower($this->ext),
|
||||||
"posted" => $this->posted,
|
"posted" => $this->posted,
|
||||||
"source" => $this->source,
|
"source" => $this->source,
|
||||||
"id" => $this->id,
|
"width" => $this->width,
|
||||||
]
|
"height" => $this->height,
|
||||||
|
"lossless" => $this->lossless,
|
||||||
|
"video" => $this->video,
|
||||||
|
"video_codec" => $this->video_codec,
|
||||||
|
"image" => $this->image,
|
||||||
|
"audio" => $this->audio,
|
||||||
|
"length" => $this->length
|
||||||
|
];
|
||||||
|
if (is_null($this->id)) {
|
||||||
|
$props_to_save["owner_id"] = $user->id;
|
||||||
|
$props_to_save["owner_ip"] = get_real_ip();
|
||||||
|
|
||||||
|
$props_sql = implode(", ", array_keys($props_to_save));
|
||||||
|
$vals_sql = implode(", ", array_map(fn ($prop) => ":$prop", array_keys($props_to_save)));
|
||||||
|
|
||||||
|
$database->execute(
|
||||||
|
"INSERT INTO images($props_sql) VALUES ($vals_sql)",
|
||||||
|
$props_to_save,
|
||||||
|
);
|
||||||
|
$this->id = $database->get_last_insert_id('images_id_seq');
|
||||||
|
} else {
|
||||||
|
$props_sql = implode(", ", array_map(fn ($prop) => "$prop = :$prop", array_keys($props_to_save)));
|
||||||
|
$database->execute(
|
||||||
|
"UPDATE images SET $props_sql WHERE id = :id",
|
||||||
|
array_merge(
|
||||||
|
$props_to_save,
|
||||||
|
["id" => $this->id]
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -352,25 +346,6 @@ class Image implements \ArrayAccess
|
||||||
$props_sql .= " WHERE id = :id";
|
$props_sql .= " WHERE id = :id";
|
||||||
$database->execute($props_sql, array_merge($this->dynamic_props, ["id" => $this->id]));
|
$database->execute($props_sql, array_merge($this->dynamic_props, ["id" => $this->id]));
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$database->execute(
|
|
||||||
"UPDATE images SET ".
|
|
||||||
"lossless = :lossless, ".
|
|
||||||
"video = :video, video_codec = :video_codec, audio = :audio,image = :image, ".
|
|
||||||
"height = :height, width = :width, ".
|
|
||||||
"length = :length WHERE id = :id",
|
|
||||||
[
|
|
||||||
"id" => $this->id,
|
|
||||||
"width" => $this->width,
|
|
||||||
"height" => $this->height,
|
|
||||||
"lossless" => $this->lossless,
|
|
||||||
"video" => $this->video,
|
|
||||||
"video_codec" => $this->video_codec,
|
|
||||||
"image" => $this->image,
|
|
||||||
"audio" => $this->audio,
|
|
||||||
"length" => $this->length
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Reference in a new issue