[core] insert or update image in a single statement

This commit is contained in:
Shish 2024-01-15 18:48:56 +00:00
parent 107485d991
commit b872c021e1

View file

@ -212,7 +212,7 @@ class Image implements \ArrayAccess
$max = Search::count_images($tags); $max = Search::count_images($tags);
if ($max < 1) { if ($max < 1) {
return null; return null;
} // From Issue #22 - opened by HungryFeline on May 30, 2011. } // From Issue #22 - opened by HungryFeline on May 30, 2011.
if ($limit_range > 0 && $max > $limit_range) { if ($limit_range > 0 && $max > $limit_range) {
$max = $limit_range; $max = $limit_range;
} }
@ -282,10 +282,10 @@ class Image implements \ArrayAccess
global $database; global $database;
if ($owner->id != $this->owner_id) { if ($owner->id != $this->owner_id) {
$database->execute(" $database->execute("
UPDATE images UPDATE images
SET owner_id=:owner_id SET owner_id=:owner_id
WHERE id=:id WHERE id=:id
", ["owner_id" => $owner->id, "id" => $this->id]); ", ["owner_id" => $owner->id, "id" => $this->id]);
log_info("core_image", "Owner for Post #{$this->id} set to {$owner->name}"); log_info("core_image", "Owner for Post #{$this->id} set to {$owner->name}");
} }
} }
@ -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());
} }
$props_to_save = [
"filename" => substr($this->filename, 0, 255),
"filesize" => $this->filesize,
"hash" => $this->hash,
"mime" => strtolower($this->mime),
"ext" => strtolower($this->ext),
"posted" => $this->posted,
"source" => $this->source,
"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)) { 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( $database->execute(
"INSERT INTO images( "INSERT INTO images($props_sql) VALUES ($vals_sql)",
owner_id, owner_ip, $props_to_save,
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'); $this->id = $database->get_last_insert_id('images_id_seq');
} else { } else {
$props_sql = implode(", ", array_map(fn ($prop) => "$prop = :$prop", array_keys($props_to_save)));
$database->execute( $database->execute(
"UPDATE images SET ". "UPDATE images SET $props_sql WHERE id = :id",
"filename = :filename, filesize = :filesize, hash = :hash, ". array_merge(
"mime = :mime, ext = :ext, width = 0, height = 0, ". $props_to_save,
"posted = :posted, source = :source ". ["id" => $this->id]
"WHERE id = :id", )
[
"filename" => $cut_name,
"filesize" => $this->filesize,
"hash" => $this->hash,
"mime" => strtolower($this->mime),
"ext" => strtolower($this->ext),
"posted" => $this->posted,
"source" => $this->source,
"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
]
);
} }
/** /**
@ -384,12 +359,12 @@ class Image implements \ArrayAccess
global $database; global $database;
if (!isset($this->tag_array)) { if (!isset($this->tag_array)) {
$this->tag_array = $database->get_col(" $this->tag_array = $database->get_col("
SELECT tag SELECT tag
FROM image_tags FROM image_tags
JOIN tags ON image_tags.tag_id = tags.id JOIN tags ON image_tags.tag_id = tags.id
WHERE image_id=:id WHERE image_id=:id
ORDER BY tag ORDER BY tag
", ["id" => $this->id]); ", ["id" => $this->id]);
sort($this->tag_array); sort($this->tag_array);
} }
return $this->tag_array; return $this->tag_array;
@ -598,10 +573,10 @@ class Image implements \ArrayAccess
) )
", ["id" => $this->id]); ", ["id" => $this->id]);
$database->execute(" $database->execute("
DELETE DELETE
FROM image_tags FROM image_tags
WHERE image_id=:id WHERE image_id=:id
", ["id" => $this->id]); ", ["id" => $this->id]);
} }
/** /**