}
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) {
}
*/
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;
}
*/
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;
}
* @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;
* @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;
}
/**
$condition_string .= " AND ";
}
if (is_array($value)) {
- // Check if there are integer values in the parameters
+ /* 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.
+ */
$is_int = false;
- $is_float = false;
$is_alpha = false;
foreach ($value as $single_value) {
if (is_int($single_value)) {
$is_int = true;
- }
-
- // To prevent to round floats we look for them
- if (round($single_value) != (float)$single_value) {
- $is_float = true;
- }
-
- // Is any non numeric value present?
- if (!is_numeric($single_value)) {
+ } else {
$is_alpha = true;
}
}
-
- // Cast them all in an unique method
- if ($is_int) {
- $casted = [];
- foreach ($value as $single_value) {
- if (!$is_alpha && !$is_float) {
- $casted[] = (int)$single_value;
- } else {
- $casted[] = (string)$single_value;
+
+ if ($is_int && $is_alpha) {
+ foreach ($value as &$ref) {
+ if (is_int($ref)) {
+ $ref = (string)$ref;
}
}
- $value = $casted;
+ unset($ref); //Prevent accidental re-use.
}
$new_values = array_merge($new_values, array_values($value));