X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fdba.php;h=dd4272c994536c43f73028ac802b96713799f1c2;hb=008c97c570ea62749a3f486b42c424365863d627;hp=79c15c85bdc599292c361a3932e89bfbadb020d9;hpb=723b40eea45c2b610558e0e566ee74ec0221b691;p=friendica.git diff --git a/include/dba.php b/include/dba.php index 79c15c85bd..dd4272c994 100644 --- a/include/dba.php +++ b/include/dba.php @@ -1,6 +1,7 @@ save_timestamp($stamp1, "network"); @@ -713,6 +714,12 @@ class dba { * @return boolean was the insert successfull? */ public static function insert($table, $param, $on_duplicate_update = false) { + + if (empty($table) || empty($param)) { + logger('Table and fields have to be set'); + return false; + } + $sql = "INSERT INTO `".self::escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (". substr(str_repeat("?, ", count($param)), 0, -2).")"; @@ -823,12 +830,12 @@ class dba { /** * @brief Build the array with the table relations * - * The array is build from the database definitions in dbstructure.php + * The array is build from the database definitions in DBStructure.php * * This process must only be started once, since the value is cached. */ private static function build_relation_data() { - $definition = db_definition(); + $definition = DBStructure::definition(); foreach ($definition AS $table => $structure) { foreach ($structure['fields'] AS $field => $field_struct) { @@ -852,6 +859,12 @@ class dba { * @return boolean|array was the delete successfull? When $in_process is set: deletion data */ public static function delete($table, $param, $in_process = false, &$callstack = array()) { + + if (empty($table) || empty($param)) { + logger('Table and condition have to be set'); + return false; + } + $commands = array(); // Create a key for the loop prevention @@ -1014,18 +1027,20 @@ class dba { * @return boolean was the update successfull? */ public static function update($table, $fields, $condition, $old_fields = array()) { + + if (empty($table) || empty($fields) || empty($condition)) { + logger('Table, fields and condition have to be set'); + return false; + } + $table = self::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))."` = ?"; - } + $array_element = each($condition); + $array_key = $array_element['key']; + if (is_int($array_key)) { + $condition_string = " WHERE ".array_shift($condition); } else { - $condition_string = ""; + $condition_string = " WHERE `".implode("` = ? AND `", array_keys($condition))."` = ?"; } if (is_bool($old_fields)) { @@ -1090,7 +1105,8 @@ class dba { * * $data = dba::select($table, $fields, $condition, $params); */ - public static function select($table, $fields = array(), $condition = array(), $params = array()) { + public static function select($table, array $fields = [], array $condition = [], array $params = []) + { if ($table == '') { return false; } @@ -1101,17 +1117,7 @@ class dba { $select_fields = "*"; } - 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 = ""; - } + $condition_string = self::buildCondition($condition); $param_string = ''; $single_row = false; @@ -1133,6 +1139,11 @@ class dba { $single_row = ($params['limit'] == 1); } + if (isset($params['limit']) && is_array($params['limit'])) { + $param_string .= " LIMIT ".intval($params['limit'][0]).", ".intval($params['limit'][1]); + $single_row = ($params['limit'][1] == 1); + } + if (isset($params['only_query']) && $params['only_query']) { $single_row = !$params['only_query']; } @@ -1150,6 +1161,71 @@ class dba { } } + /** + * @brief Counts the rows from a table satisfying the provided condition + * + * @param string $table Table name + * @param array $condition array of fields for condition + * + * @return int + * + * Example: + * $table = "item"; + * + * $condition = ["uid" => 1, "network" => 'dspr']; + * or: + * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr']; + * + * $count = dba::count($table, $condition); + */ + public static function count($table, array $condition = []) + { + if ($table == '') { + return false; + } + + $condition_string = self::buildCondition($condition); + + $sql = "SELECT COUNT(*) AS `count` FROM `".$table."`".$condition_string; + + $row = self::fetch_first($sql, $condition); + + return $row['count']; + } + + /** + * @brief Returns the SQL condition string built from the provided condition array + * + * This function operates with two modes. + * - Supplied with a filed/value associative array, it builds simple strict + * equality conditions linked by AND. + * - Supplied with a flat list, the first element is the condition string and + * the following arguments are the values to be interpolated + * + * $condition = ["uid" => 1, "network" => 'dspr']; + * or: + * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr']; + * + * In either case, the provided array is left with the parameters only + * + * @param array $condition + * @return string + */ + private static function buildCondition(array &$condition = []) + { + $condition_string = ''; + 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))."` = ?"; + } + } + + return $condition_string; + } /** * @brief Fills an array with data from a query