X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FDatabase%2FDatabase.php;h=88d8d7d0f6ef87baa78cbc7f63dcd2cceeb65c15;hb=f36d4891bcf0a0eb4f0243851375966d6e99d201;hp=f94e03f92a8b48e95ada041c7a68a6ebffc95018;hpb=e8becc153856debb927d7afac17bae5c80a0abb7;p=friendica.git diff --git a/src/Database/Database.php b/src/Database/Database.php index f94e03f92a..88d8d7d0f6 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1,6 +1,6 @@ configCache->get('database', 'password')); $db = trim($this->configCache->get('database', 'database')); $charset = trim($this->configCache->get('database', 'charset')); + $socket = trim($this->configCache->get('database', 'socket')); if (!(strlen($server) && strlen($user))) { return false; @@ -135,9 +136,14 @@ class Database $connect .= ";charset=" . $charset; } + if ($socket) { + $connect .= ";$unix_socket=" . $socket; + } + try { $this->connection = @new PDO($connect, $user, $pass, [PDO::ATTR_PERSISTENT => $persistent]); $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares); + $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); $this->connected = true; } catch (PDOException $e) { $this->connected = false; @@ -159,6 +165,11 @@ class Database if ($charset) { $this->connection->set_charset($charset); } + + if ($socket) { + $this->connection->set_socket($socket); + } + } } @@ -520,7 +531,7 @@ class Database $called_from_e = ($called_from['function'] == 'e'); if (!isset($this->connection)) { - throw new InternalServerErrorException('The Connection is empty, although connected is set true.'); + throw new ServiceUnavailableException('The Connection is empty, although connected is set true.'); } switch ($this->driver) { @@ -549,12 +560,14 @@ class Database break; } - foreach ($args as $param => $value) { + foreach (array_keys($args) as $param) { + $data_type = PDO::PARAM_STR; if (is_int($args[$param])) { $data_type = PDO::PARAM_INT; - } else { - $data_type = PDO::PARAM_STR; + } elseif ($args[$param] !== null) { + $args[$param] = (string)$args[$param]; } + $stmt->bindParam($param, $args[$param], $data_type); } @@ -605,13 +618,16 @@ class Database $param_types = ''; $values = []; - foreach ($args as $param => $value) { + foreach (array_keys($args) as $param) { if (is_int($args[$param])) { $param_types .= 'i'; } elseif (is_float($args[$param])) { $param_types .= 'd'; } elseif (is_string($args[$param])) { $param_types .= 's'; + } elseif (is_object($args[$param]) && method_exists($args[$param], '__toString')) { + $param_types .= 's'; + $args[$param] = (string)$args[$param]; } else { $param_types .= 'b'; } @@ -909,7 +925,7 @@ class Database /** * Fetch a single row * - * @param PDOStatement|mysqli_stmt $stmt statement object + * @param bool|PDOStatement|mysqli_stmt $stmt statement object * * @return array|false current row */ @@ -969,7 +985,7 @@ class Database } /** - * Insert a row into a table + * Insert a row into a table. Field value objects will be cast as string. * * @param string|array $table Table name or array [schema => table] * @param array $param parameter array @@ -1244,9 +1260,9 @@ class Database } /** - * Updates rows + * Updates rows in the database. Field value objects will be cast as string. * - * Updates rows in the database. When $old_fields is set to an array, + * When $old_fields is set to an array, * the system will only do an update if the fields in that array changed. * * Attention: @@ -1362,6 +1378,45 @@ class Database return $this->toArray($this->select($table, $fields, $condition, $params)); } + /** + * Escape fields, adding special treatment for "group by" handling + * + * @param array $fields + * @param array $options + * @return array + */ + private function escapeFields(array $fields, array $options) + { + // In the case of a "GROUP BY" we have to add all the ORDER fields to the fieldlist. + // This needs to done to apply the "ANY_VALUE(...)" treatment from below to them. + // Otherwise MySQL would report errors. + if (!empty($options['group_by']) && !empty($options['order'])) { + foreach ($options['order'] as $key => $field) { + if (!is_int($key)) { + if (!in_array($key, $fields)) { + $fields[] = $key; + } + } else { + if (!in_array($field, $fields)) { + $fields[] = $field; + } + } + } + } + + array_walk($fields, function(&$value, $key) use ($options) + { + $field = $value; + $value = '`' . str_replace('`', '``', $value) . '`'; + + if (!empty($options['group_by']) && !in_array($field, $options['group_by'])) { + $value = 'ANY_VALUE(' . $value . ') AS ' . $value; + } + }); + + return $fields; + } + /** * Select rows from a table * @@ -1398,7 +1453,8 @@ class Database } if (count($fields) > 0) { - $select_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], $fields)); + $fields = $this->escapeFields($fields, $params); + $select_string = implode(', ', $fields); } else { $select_string = '*'; } @@ -1561,9 +1617,9 @@ class Database } } - return $fields; + return $fields; } - + /** * Returns the error number of the last query *