use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Core\System;
+use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Profiler;
use mysqli;
{
// Use environment variables for mysql if they are set beforehand
if (!empty($server['MYSQL_HOST'])
- && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
+ && (!empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER'])))
&& $server['MYSQL_PASSWORD'] !== false
&& !empty($server['MYSQL_DATABASE']))
{
public function connect()
{
if (!is_null($this->connection) && $this->connected()) {
- return true;
+ return $this->connected;
}
+ // Reset connected state
+ $this->connected = false;
+
$port = 0;
$serveraddr = trim($this->configCache->get('database', 'hostname'));
$serverdata = explode(':', $serveraddr);
$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->connected = true;
} catch (PDOException $e) {
- /// @TODO At least log exception, don't ignore it!
+ $this->connected = false;
}
}
*/
public function disconnect()
{
- if (is_null($this->connection)) {
- return;
+ if (!is_null($this->connection)) {
+ switch ($this->driver) {
+ case 'pdo':
+ $this->connection = null;
+ break;
+ case 'mysqli':
+ $this->connection->close();
+ $this->connection = null;
+ break;
+ }
}
- switch ($this->driver) {
- case 'pdo':
- $this->connection = null;
- break;
- case 'mysqli':
- $this->connection->close();
- $this->connection = null;
- break;
- }
+ $this->driver = null;
+ $this->connected = false;
}
/**
$connected = $this->connection->ping();
break;
}
+
return $connected;
}
// We are having an own error logging in the function "e"
$called_from_e = ($called_from['function'] == 'e');
+ if (!isset($this->connection)) {
+ throw new InternalServerErrorException('The Connection is empty, although connected is set true.');
+ }
+
switch ($this->driver) {
case 'pdo':
// If there are no arguments we use "query"
return false;
}
- $table_string = DBA::buildTableString($table);
-
- $condition_string = DBA::buildCondition($condition);
-
if (is_bool($old_fields)) {
$do_insert = $old_fields;
return true;
}
+ $table_string = DBA::buildTableString($table);
+
+ $condition_string = DBA::buildCondition($condition);
+
$sql = "UPDATE " . $table_string . " SET "
. implode(" = ?, ", array_map([DBA::class, 'quoteIdentifier'], array_keys($fields))) . " = ?"
. $condition_string;
- $params1 = array_values($fields);
- $params2 = array_values($condition);
- $params = array_merge_recursive($params1, $params2);
+ // Combines the updated fields parameter values with the condition parameter values
+ $params = array_merge(array_values($fields), $condition);
return $this->e($sql, $params);
}
*
* @brief Retrieve a single record from a table
*
- * @param string $table
- * @param array $fields
- * @param array $condition
- * @param array $params
+ * @param string|array $table
+ * @param array $fields
+ * @param array $condition
+ * @param array $params
*
* @return bool|array
* @throws \Exception
* @throws \Exception
* @see self::select
*/
- public function selectToArray(string $table, array $fields = [], array $condition = [], array $params = [])
+ public function selectToArray($table, array $fields = [], array $condition = [], array $params = [])
{
return $this->toArray($this->select($table, $fields, $condition, $params));
}
*
* @param string|array $table Table name or array [schema => table]
* @param array $condition Array of fields for condition
+ * @param array $params Array of several parameters
*
* @return int
*
* $count = DBA::count($table, $condition);
* @throws \Exception
*/
- public function count($table, array $condition = [])
+ public function count($table, array $condition = [], array $params = [])
{
if (empty($table)) {
return false;
$condition_string = DBA::buildCondition($condition);
- $sql = "SELECT COUNT(*) AS `count` FROM " . $table_string . $condition_string;
+ if (empty($params['expression'])) {
+ $expression = '*';
+ } elseif (!empty($params['distinct'])) {
+ $expression = "DISTINCT " . DBA::quoteIdentifier($params['expression']);
+ } else {
+ $expression = DBA::quoteIdentifier($params['expression']);
+ }
+
+ $sql = "SELECT COUNT(" . $expression . ") AS `count` FROM " . $table_string . $condition_string;
$row = $this->fetchFirst($sql, $condition);