+ (* Check if something with the same name already exists. If replace=1
+ * then we can replace it, otherwise we must present an error message.
+ *)
+ let replace = q#param_true "replace" in
+ let sth = dbh#prepare_cached "select 1 from images
+ where hostid = ? and name = ?" in
+ sth#execute [`Int hostid; `String name];
+
+ let exists = try sth#fetch1int () = 1 with Not_found -> false in
+
+ if exists then (
+ if not replace then (
+ error ~title:"Image already exists" ~back_button:true
+ q ("An image with the same name already exists.");
+ return ()
+ ) else (
+ let sth = dbh#prepare_cached "update images
+ set name_deleted = name, name = null
+ where hostid = ? and name = ?" in
+ sth#execute [`Int hostid; `String name];
+ )
+ );
+