self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
self::$connected = true;
} catch (PDOException $e) {
+ /// @TODO At least log exception, don't ignore it!
}
}
}
$r = self::p("EXPLAIN ".$query);
- if (!self::is_result($r)) {
+ if (!self::isResult($r)) {
return;
}
}
public static function escape($str) {
- switch (self::$driver) {
- case 'pdo':
- return substr(@self::$connection->quote($str, PDO::PARAM_STR), 1, -1);
- case 'mysqli':
- return @self::$connection->real_escape_string($str);
+ if (self::$connected) {
+ switch (self::$driver) {
+ case 'pdo':
+ return substr(@self::$connection->quote($str, PDO::PARAM_STR), 1, -1);
+
+ case 'mysqli':
+ return @self::$connection->real_escape_string($str);
+ }
+ } else {
+ return str_replace("'", "\\'", $str);
}
}
switch (self::$driver) {
case 'pdo':
$r = self::p("SELECT 1");
- if (self::is_result($r)) {
+ if (self::isResult($r)) {
$row = self::toArray($r);
$connected = ($row[0]['1'] == '1');
}
* @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
*
* Please only use it with complicated queries.
- * For all regular queries please use dba::select or dba::exists
+ * For all regular queries please use DBA::select or DBA::exists
*
* @param string $sql SQL statement
* @return bool|object statement object or result object
/**
* @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
*
- * Please use dba::delete, dba::insert, dba::update, ... instead
+ * Please use DBA::delete, DBA::insert, DBA::update, ... instead
*
* @param string $sql SQL statement
* @return boolean Was the query successfull? False is returned only if an error occurred
/**
* Fetches the first row
*
- * Please use dba::selectFirst or dba::exists whenever this is possible.
+ * Please use DBA::selectFirst or DBA::exists whenever this is possible.
*
* @brief Fetches the first row
* @param string $sql SQL statement
switch (self::$driver) {
case 'pdo':
- if (self::$connection->inTransaction()) {
- break;
- }
- if (!self::$connection->beginTransaction()) {
+ if (!self::$connection->inTransaction() && !self::$connection->beginTransaction()) {
return false;
}
break;
+
case 'mysqli':
if (!self::$connection->begin_transaction()) {
return false;
if (!self::$connection->inTransaction()) {
return true;
}
+
return self::$connection->commit();
+
case 'mysqli':
return self::$connection->commit();
}
+
return true;
}
}
$ret = self::$connection->rollBack();
break;
+
case 'mysqli':
$ret = self::$connection->rollback();
break;
$commands[$key] = ['table' => $table, 'conditions' => $conditions];
- $cascade = defaults($options, 'cascade', true);
+ // Don't use "defaults" here, since it would set "false" to "true"
+ if (isset($options['cascade'])) {
+ $cascade = $options['cascade'];
+ } else {
+ $cascade = true;
+ }
// To speed up the whole process we cache the table relations
if ($cascade && count(self::$relation) == 0) {
*
* $params = array("order" => array("id", "received" => true), "limit" => 10);
*
- * $data = dba::select($table, $fields, $condition, $params);
+ * $data = DBA::select($table, $fields, $condition, $params);
*/
public static function select($table, array $fields = [], array $condition = [], array $params = [])
{
* or:
* $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
*
- * $count = dba::count($table, $condition);
+ * $count = DBA::count($table, $condition);
*/
public static function count($table, array $condition = [])
{
/* Workaround for MySQL Bug #64791.
* Never mix data types inside any IN() condition.
* In case of mixed types, cast all as string.
- * Logic needs to be consistent with dba::p() data types.
+ * Logic needs to be consistent with DBA::p() data types.
*/
$is_int = false;
$is_alpha = false;
$limit_string = '';
if (isset($params['limit']) && is_int($params['limit'])) {
- $limit_string = " LIMIT " . $params['limit'];
+ $limit_string = " LIMIT " . intval($params['limit']);
}
if (isset($params['limit']) && is_array($params['limit'])) {
case 'mysqli':
// MySQLi offers both a mysqli_stmt and a mysqli_result class.
// We should be careful not to assume the object type of $stmt
- // because dba::p() has been able to return both types.
+ // because DBA::p() has been able to return both types.
if ($stmt instanceof mysqli_stmt) {
$stmt->free_result();
$ret = $stmt->close();
*
* @return boolean Whether $array is a filled array or an object with rows
*/
- public static function is_result($array)
+ public static function isResult($array)
{
// It could be a return value from an update statement
if (is_bool($array)) {
* @param boolean $add_quotation add quotation marks for string values
* @return void
*/
- private static function esc_array_callback(&$value, $key, $add_quotation)
+ private static function escapeArrayCallback(&$value, $key, $add_quotation)
{
if (!$add_quotation) {
if (is_bool($value)) {
$value = ($value ? '1' : '0');
} else {
- $value = dbesc($value);
+ $value = self::escape($value);
}
return;
}
} elseif (is_float($value) || is_integer($value)) {
$value = (string) $value;
} else {
- $value = "'" . dbesc($value) . "'";
+ $value = "'" . self::escape($value) . "'";
}
}
* @param boolean $add_quotation add quotation marks for string values
* @return void
*/
- public static function esc_array(&$arr, $add_quotation = false)
+ public static function escapeArray(&$arr, $add_quotation = false)
{
- array_walk($arr, 'self::esc_array_callback', $add_quotation);
+ array_walk($arr, 'self::escapeArrayCallback', $add_quotation);
}
}