+ public static function select($table, array $fields = [], array $condition = [], array $params = [])
+ {
+ if ($table == '') {
+ return false;
+ }
+
+ if (count($fields) > 0) {
+ $select_fields = "`".implode("`, `", array_values($fields))."`";
+ } else {
+ $select_fields = "*";
+ }
+
+ $condition_string = self::buildCondition($condition);
+
+ $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 ? "DESC" : "ASC").", ";
+ } else {
+ $param_string .= "`".$order."`, ";
+ }
+ }
+ $param_string = substr($param_string, 0, -2);
+ }
+
+ if (isset($params['limit']) && is_int($params['limit'])) {
+ $param_string .= " LIMIT ".$params['limit'];
+ $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'];
+ }
+
+ $sql = "SELECT ".$select_fields." FROM `".$table."`".$condition_string.$param_string;
+
+ $result = self::p($sql, $condition);
+
+ if (is_bool($result) || !$single_row) {
+ return $result;
+ } else {
+ $row = self::fetch($result);
+ self::close($result);
+ return $row;
+ }
+ }
+
+ /**
+ * @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
+ *
+ * @param object $stmt statement object
+ * @return array Data array
+ */
+ public static function inArray($stmt, $do_close = true) {
+ if (is_bool($stmt)) {
+ return $stmt;
+ }
+
+ $data = array();
+ while ($row = self::fetch($stmt)) {
+ $data[] = $row;
+ }
+ if ($do_close) {
+ self::close($stmt);
+ }
+ return $data;
+ }
+
+ /**
+ * @brief Returns the error number of the last query
+ *
+ * @return string Error number (0 if no error)
+ */
+ public static function errorNo() {
+ return self::$errorno;
+ }
+
+ /**
+ * @brief Returns the error message of the last query
+ *
+ * @return string Error message ('' if no error)
+ */
+ public static function errorMessage() {
+ return self::$error;
+ }
+
+ /**
+ * @brief Closes the current statement
+ *
+ * @param object $stmt statement object
+ * @return boolean was the close successfull?
+ */
+ public static function close($stmt) {
+ if (!is_object($stmt)) {
+ return false;
+ }
+
+ switch (self::$driver) {
+ case 'pdo':
+ return $stmt->closeCursor();
+ case 'mysqli':
+ $stmt->free_result();
+ return $stmt->close();
+ }
+ }
+}
+
+function dbesc($str) {
+ if (dba::$connected) {
+ return(dba::escape($str));
+ } else {
+ return(str_replace("'","\\'",$str));
+ }