X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fdba.php;h=9d828f8b440e40ac8147662eefc32acd328bd4ff;hb=cb7176ee7051cc90e3fb6d5c9816e875112a90d1;hp=cfe31af51d89bfaf1f8be59d5a5cd07ca23965b9;hpb=150989ba998ba4da6a824b1d05d66b782043579d;p=friendica.git diff --git a/include/dba.php b/include/dba.php index cfe31af51d..9d828f8b44 100644 --- a/include/dba.php +++ b/include/dba.php @@ -76,9 +76,7 @@ class dba { } try { self::$db = @new PDO($connect, $user, $pass); - // Needs more testing - //self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); - //self::$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); + self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); self::$connected = true; } catch (PDOException $e) { } @@ -843,10 +841,25 @@ class dba { */ public static function lock($table) { // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html - self::e("SET autocommit=0"); + if (self::$driver == 'pdo') { + self::e("SET autocommit=0"); + self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); + } else { + self::$db->autocommit(false); + } + $success = self::e("LOCK TABLES `".self::escape($table)."` WRITE"); + + if (self::$driver == 'pdo') { + self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + } + if (!$success) { - self::e("SET autocommit=1"); + if (self::$driver == 'pdo') { + self::e("SET autocommit=1"); + } else { + self::$db->autocommit(true); + } } else { self::$in_transaction = true; } @@ -860,9 +873,21 @@ class dba { */ public static function unlock() { // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html - self::e("COMMIT"); + self::performCommit(); + + if (self::$driver == 'pdo') { + self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); + } + $success = self::e("UNLOCK TABLES"); - self::e("SET autocommit=1"); + + if (self::$driver == 'pdo') { + self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + self::e("SET autocommit=1"); + } else { + self::$db->autocommit(true); + } + self::$in_transaction = false; return $success; } @@ -873,23 +898,51 @@ class dba { * @return boolean Was the command executed successfully? */ public static function transaction() { - if (!self::e('COMMIT')) { + if (!self::performCommit()) { return false; } - if (!self::e('START TRANSACTION')) { - return false; + + switch (self::$driver) { + case 'pdo': + if (self::$db->inTransaction()) { + break; + } + if (!self::$db->beginTransaction()) { + return false; + } + break; + case 'mysqli': + if (!self::$db->begin_transaction()) { + return false; + } + break; } + self::$in_transaction = true; return true; } + private static function performCommit() + { + switch (self::$driver) { + case 'pdo': + if (!self::$db->inTransaction()) { + return true; + } + return self::$db->commit(); + case 'mysqli': + return self::$db->commit(); + } + return true; + } + /** * @brief Does a commit * * @return boolean Was the command executed successfully? */ public static function commit() { - if (!self::e('COMMIT')) { + if (!self::performCommit()) { return false; } self::$in_transaction = false; @@ -902,11 +955,20 @@ class dba { * @return boolean Was the command executed successfully? */ public static function rollback() { - if (!self::e('ROLLBACK')) { - return false; + switch (self::$driver) { + case 'pdo': + if (!self::$db->inTransaction()) { + $ret = true; + break; + } + $ret = self::$db->rollBack(); + break; + case 'mysqli': + $ret = self::$db->rollback(); + break; } self::$in_transaction = false; - return true; + return $ret; } /** @@ -953,7 +1015,7 @@ class dba { $commands = []; // Create a key for the loop prevention - $key = $table . ':' . implode(':', array_keys($conditions)) . ':' . implode(':', $conditions); + $key = $table . ':' . json_encode($conditions); // We quit when this key already exists in the callstack. if (isset($callstack[$key])) { @@ -980,7 +1042,7 @@ class dba { $rel_def = array_values(self::$relation[$table])[0]; // Create a key for preventing double queries - $qkey = $field . '-' . $table . ':' . implode(':', array_keys($conditions)) . ':' . implode(':', $conditions); + $qkey = $field . '-' . $table . ':' . json_encode($conditions); // When the search field is the relation field, we don't need to fetch the rows // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend @@ -1299,8 +1361,7 @@ class dba { $condition_string .= " AND "; } if (is_array($value)) { - /** - * Workaround for MySQL Bug #64791. + /* 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.