DBA: The condition can now be a complex string

This commit is contained in:
Michael 2017-08-12 13:54:29 +00:00
parent 1d8c91c478
commit 89017d4e6c
4 changed files with 53 additions and 42 deletions

View File

@ -412,21 +412,6 @@ class dba {
return $connected;
}
function insert_id() {
switch ($this->driver) {
case 'pdo':
$id = $this->db->lastInsertId();
break;
case 'mysqli':
$id = $this->db->insert_id;
break;
case 'mysql':
$id = mysql_insert_id($this->db);
break;
}
return $id;
}
function __destruct() {
if ($this->db) {
switch ($this->driver) {
@ -777,9 +762,15 @@ class dba {
return false;
}
$fields = array_keys($condition);
$fields = array();
$stmt = self::select($table, array($fields[0]), $condition, array('limit' => 1, 'only_query' => true));
$array_element = each($condition);
$array_key = $array_element['key'];
if (!is_int($array_key)) {
$fields = array($array_key);
}
$stmt = self::select($table, $fields, $condition, array('limit' => 1, 'only_query' => true));
if (is_bool($stmt)) {
$retval = $stmt;
@ -914,6 +905,26 @@ class dba {
return self::e($sql, $param);
}
/**
* @brief Fetch the id of the last insert command
*
* @return integer Last inserted id
*/
function lastInsertId() {
switch (self::$dbo->driver) {
case 'pdo':
$id = self::$dbo->db->lastInsertId();
break;
case 'mysqli':
$id = self::$dbo->db->insert_id;
break;
case 'mysql':
$id = mysql_insert_id(self::$dbo);
break;
}
return $id;
}
/**
* @brief Locks a table for exclusive write access
*
@ -1181,15 +1192,23 @@ class dba {
$table = self::$dbo->escape($table);
if (count($condition) > 0) {
$array_element = each($condition);
$array_key = $array_element['key'];
if (is_int($array_key)) {
$condition_string = " WHERE ".array_shift($condition);
} else {
$condition_string = " WHERE `".implode("` = ? AND `", array_keys($condition))."` = ?";
}
} else {
$condition_string = "";
}
if (is_bool($old_fields)) {
$sql = "SELECT * FROM `".$table."` WHERE `".
implode("` = ? AND `", array_keys($condition))."` = ? LIMIT 1";
$params = array_values($condition);
$do_insert = $old_fields;
$old_fields = self::fetch_first($sql, $params);
$old_fields = self::select($table, array(), $condition, array('limit' => 1));
if (is_bool($old_fields)) {
if ($do_insert) {
$values = array_merge($condition, $fields);
@ -1216,8 +1235,7 @@ class dba {
}
$sql = "UPDATE `".$table."` SET `".
implode("` = ?, `", array_keys($fields))."` = ? WHERE `".
implode("` = ? AND `", array_keys($condition))."` = ?";
implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
$params1 = array_values($fields);
$params2 = array_values($condition);
@ -1256,7 +1274,13 @@ class dba {
}
if (count($condition) > 0) {
$array_element = each($condition);
$array_key = $array_element['key'];
if (is_int($array_key)) {
$condition_string = " WHERE ".array_shift($condition);
} else {
$condition_string = " WHERE `".implode("` = ? AND `", array_keys($condition))."` = ?";
}
} else {
$condition_string = "";
}

View File

@ -963,13 +963,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
// When the item was successfully stored we fetch the ID of the item.
if (dbm::is_result($r)) {
$r = q("SELECT LAST_INSERT_ID() AS `item-id`");
if (dbm::is_result($r)) {
$current_post = $r[0]['item-id'];
} else {
// This shouldn't happen
$current_post = 0;
}
$current_post = dba::lastInsertId();
} else {
// This can happen - for example - if there are locking timeouts.
dba::rollback();

View File

@ -6,13 +6,11 @@ require_once("include/Photo.php");
define("IMPORT_DEBUG", False);
function last_insert_id() {
global $db;
if (IMPORT_DEBUG) {
return 1;
}
return $db->insert_id();
return dba::lastInsertId();
}
function last_error() {

View File

@ -887,12 +887,7 @@ function item_post(App $a) {
);
if (dbm::is_result($r)) {
$r = q("SELECT LAST_INSERT_ID() AS `item-id`");
if (dbm::is_result($r)) {
$post_id = $r[0]['item-id'];
} else {
$post_id = 0;
}
$post_id = dba::lastInsertId();
} else {
logger('mod_item: unable to create post.');
$post_id = 0;