X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fdba.php;h=fe191a9e21b3bf46f2043119b0d669a19b75c634;hb=755f6e8cc1ceb5fab0c0f63deb3ca7048d1ea48d;hp=79c15c85bdc599292c361a3932e89bfbadb020d9;hpb=d99a7efd90999c4b6adf8dbdec3b8f02ff0c0b3e;p=friendica.git diff --git a/include/dba.php b/include/dba.php index 79c15c85bd..fe191a9e21 100644 --- a/include/dba.php +++ b/include/dba.php @@ -713,6 +713,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).")"; @@ -852,6 +858,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 +1026,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 +1104,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 +1116,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 +1138,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 +1160,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