+ $old_fields = self::selectFirst($table, [], $condition);
+
+ if (is_bool($old_fields)) {
+ if ($do_insert) {
+ $values = array_merge($condition, $fields);
+ return self::insert($table, $values, $do_insert);
+ }
+ $old_fields = array();
+ }
+ }
+
+ $do_update = (count($old_fields) == 0);
+
+ foreach ($old_fields AS $fieldname => $content) {
+ if (isset($fields[$fieldname])) {
+ if ($fields[$fieldname] == $content) {
+ unset($fields[$fieldname]);
+ } else {
+ $do_update = true;
+ }
+ }
+ }
+
+ if (!$do_update || (count($fields) == 0)) {
+ return true;
+ }
+
+ $sql = "UPDATE `".$table."` SET `".
+ implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
+
+ $params1 = array_values($fields);
+ $params2 = array_values($condition);
+ $params = array_merge_recursive($params1, $params2);
+
+ return self::e($sql, $params);
+ }
+
+ /**
+ * Retrieve a single record from a table and returns it in an associative array
+ *
+ * @brief Retrieve a single record from a table
+ * @param string $table
+ * @param array $fields
+ * @param array $condition
+ * @param array $params
+ * @return bool|array
+ * @see dba::select
+ */
+ public static function selectFirst($table, array $fields = [], array $condition = [], $params = [])
+ {
+ $params['limit'] = 1;
+ $result = self::select($table, $fields, $condition, $params);
+
+ if (is_bool($result)) {
+ return $result;
+ } else {
+ $row = self::fetch($result);
+ self::close($result);
+ return $row;
+ }
+ }
+
+ /**
+ * @brief Select rows from a table
+ *
+ * @param string $table Table name
+ * @param array $fields Array of selected fields, empty for all
+ * @param array $condition Array of fields for condition
+ * @param array $params Array of several parameters
+ *
+ * @return boolean|object
+ *
+ * Example:
+ * $table = "item";
+ * $fields = array("id", "uri", "uid", "network");
+ *
+ * $condition = array("uid" => 1, "network" => 'dspr');
+ * or:
+ * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
+ *
+ * $params = array("order" => array("id", "received" => true), "limit" => 10);
+ *
+ * $data = dba::select($table, $fields, $condition, $params);
+ */
+ 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);
+
+ if (isset($params['order'])) {
+ $order_string = " ORDER BY ";
+ foreach ($params['order'] AS $fields => $order) {
+ if (!is_int($fields)) {
+ $order_string .= "`" . $fields . "` " . ($order ? "DESC" : "ASC") . ", ";
+ } else {
+ $order_string .= "`" . $order . "`, ";
+ }
+ }
+ $order_string = substr($order_string, 0, -2);
+ }
+
+ if (isset($params['limit']) && is_int($params['limit'])) {
+ $limit_string = " LIMIT " . $params['limit'];
+ }
+
+ if (isset($params['limit']) && is_array($params['limit'])) {
+ $limit_string = " LIMIT " . intval($params['limit'][0]) . ", " . intval($params['limit'][1]);
+ }
+
+ $sql = "SELECT " . $select_fields . " FROM `" . $table . "`" . $condition_string . $order_string . $limit_string;
+
+ $result = self::p($sql, $condition);
+
+ return $result;
+ }
+
+ /**
+ * @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'];