1
1
Fork 0

New function "dba::select" to fetch table rows

This commit is contained in:
Michael 2017-05-06 11:32:18 +00:00
parent a849a25470
commit 3ca70e6362

View file

@ -498,18 +498,28 @@ class dba {
unset($args[0]); unset($args[0]);
// When the second function parameter is an array then use this as the parameter array // When the second function parameter is an array then use this as the parameter array
if ((count($args) == 1) AND (is_array($args[1]))) { if ((count($args) > 0) AND (is_array($args[1]))) {
$params = $args[1]; $params = $args[1];
$i = 0; } else {
foreach ($params AS $param) { $params = $args;
$args[++$i] = $param; }
}
// Renumber the array keys to be sure that they fit
$i = 0;
$args = array();
foreach ($params AS $param) {
$args[++$i] = $param;
} }
if (!self::$dbo OR !self::$dbo->connected) { if (!self::$dbo OR !self::$dbo->connected) {
return false; return false;
} }
if (substr_count($sql, '?') != count($args)) {
// Question: Should we continue or stop the query here?
logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
}
$sql = self::$dbo->any_value_fallback($sql); $sql = self::$dbo->any_value_fallback($sql);
if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) { if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) {
@ -1006,6 +1016,76 @@ class dba {
return self::e($sql, $params); return self::e($sql, $params);
} }
/**
* @brief Select rows from a table
*
* @param string $table Table name
* @param array $fields array of selected fields
* @param array $condition array of fields for condition
* @param array $params array of several parameters
*
* @return boolean|object If "limit" is equal "1" only a single row is returned, else a query object is returned
*
* Example:
* $table = "item";
* $fields = array("id", "uri", "uid", "network");
* $condition = array("uid" => 1, "network" => 'dspr');
* $params = array("order" => array("id", "received" => true), "limit" => 1);
*
* $data = dba::select($table, $fields, $condition, $params);
*/
static public function select($table, $fields = array(), $condition = array(), $params = array()) {
if ($table == '') {
return false;
}
if (count($fields) > 0) {
$select_fields = "`".implode("`, `", array_values($fields))."`";
} else {
$select_fields = "*";
}
if (count($condition) > 0) {
$condition_string = " WHERE `".implode("` = ? AND `", array_keys($condition))."` = ?";
} else {
$condition_string = "";
}
$param_string = '';
$single_row = false;
if (isset($params['order'])) {
$param_string .= " ORDER BY ";
foreach ($params['order'] AS $fields => $order) {
if (!is_int($fields)) {
$param_string .= "`".$fields."` ".($order ? "ASC" : "DESC").", ";
} else {
$param_string .= "`".$order."`, ";
}
}
$param_string = substr($param_string, 0, -2);
}
if (isset($params['limit'])) {
if (is_int($params['limit'])) {
$param_string .= " LIMIT ".$params['limit'];
$single_row =($params['limit'] == 1);
}
}
$sql = "SELECT ".$select_fields." FROM `".$table."`".$condition_string.$param_string;
$result = self::p($sql, $condition);
if (is_bool($result) OR !$single_row) {
return $result;
} else {
$row = self::fetch($result);
self::close($result);
return $row;
}
}
/** /**
* @brief Closes the current statement * @brief Closes the current statement
* *