<?php
use \Friendica\Core\System;
+use Friendica\Database\DBM;
-require_once("dbm.php");
require_once('include/datetime.php');
/**
}
}
- if (!self::$connected && function_exists('mysql_connect')) {
- self::$driver = 'mysql';
- self::$db = mysql_connect($serveraddr, $user, $pass);
- if (self::$db && mysql_select_db($db, self::$db)) {
- self::$connected = true;
-
- if (isset($a->config["system"]["db_charset"])) {
- mysql_set_charset($a->config["system"]["db_charset"], self::$db);
- }
- }
- }
-
// No suitable SQL driver was found.
if (!self::$connected) {
self::$db = null;
if (!$install) {
- system_unavailable();
+ System::unavailable();
}
}
$a->save_timestamp($stamp1, "network");
case 'mysqli':
self::$_server_info = self::$db->server_info;
break;
- case 'mysql':
- self::$_server_info = mysql_get_server_info(self::$db);
- break;
}
}
return self::$_server_info;
}
$r = self::p("EXPLAIN ".$query);
- if (!dbm::is_result($r)) {
+ if (!DBM::is_result($r)) {
return;
}
return substr(@self::$db->quote($str, PDO::PARAM_STR), 1, -1);
case 'mysqli':
return @self::$db->real_escape_string($str);
- case 'mysql':
- return @mysql_real_escape_string($str,self::$db);
}
}
switch (self::$driver) {
case 'pdo':
$r = dba::p("SELECT 1");
- if (dbm::is_result($r)) {
+ if (DBM::is_result($r)) {
$row = dba::inArray($r);
$connected = ($row[0]['1'] == '1');
}
case 'mysqli':
$connected = self::$db->ping();
break;
- case 'mysql':
- $connected = mysql_ping(self::$db);
- break;
}
return $connected;
}
self::$affected_rows = $retval->affected_rows;
}
break;
- case 'mysql':
- // For the old "mysql" functions we cannot use prepared statements
- $retval = mysql_query(self::replace_parameters($sql, $args), self::$db);
- if (mysql_errno(self::$db)) {
- self::$error = mysql_error(self::$db);
- self::$errorno = mysql_errno(self::$db);
- } else {
- self::$affected_rows = mysql_affected_rows($retval);
-
- // Due to missing mysql_* support this here wasn't tested at all
- // See here: http://php.net/manual/en/function.mysql-num-rows.php
- if (self::$affected_rows <= 0) {
- self::$affected_rows = mysql_num_rows($retval);
- }
- }
- break;
}
// We are having an own error logging in the function "e"
return $stmt->columnCount();
case 'mysqli':
return $stmt->field_count;
- case 'mysql':
- return mysql_affected_rows($stmt);
}
return 0;
}
return $stmt->rowCount();
case 'mysqli':
return $stmt->num_rows;
- case 'mysql':
- return mysql_num_rows($stmt);
}
return 0;
}
$columns[$fields[$param]->name] = $col;
}
return $columns;
- case 'mysql':
- return mysql_fetch_array($stmt, MYSQL_ASSOC);
}
}
* @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).")";
case 'mysqli':
$id = self::$db->insert_id;
break;
- case 'mysql':
- $id = mysql_insert_id(self::$db);
- break;
}
return $id;
}
* @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
* @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)) {
*
* $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;
}
$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;
$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'];
}
}
}
+ /**
+ * @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
case 'pdo':
return $stmt->closeCursor();
case 'mysqli':
- return $stmt->free_result();
+ $stmt->free_result();
return $stmt->close();
- case 'mysql':
- return mysql_free_result($stmt);
}
}
}