We now are using the native pdo mode

This commit is contained in:
Michael 2018-06-22 19:35:42 +00:00
parent 47ab72352e
commit 6e64091fc7

View file

@ -76,9 +76,7 @@ class dba {
} }
try { try {
self::$db = @new PDO($connect, $user, $pass); self::$db = @new PDO($connect, $user, $pass);
// Needs more testing self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
//self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
//self::$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
self::$connected = true; self::$connected = true;
} catch (PDOException $e) { } catch (PDOException $e) {
} }
@ -843,10 +841,25 @@ class dba {
*/ */
public static function lock($table) { public static function lock($table) {
// See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
if (self::$driver == 'pdo') {
self::e("SET autocommit=0"); 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"); $success = self::e("LOCK TABLES `".self::escape($table)."` WRITE");
if (self::$driver == 'pdo') {
self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
if (!$success) { if (!$success) {
if (self::$driver == 'pdo') {
self::e("SET autocommit=1"); self::e("SET autocommit=1");
} else {
self::$db->autocommit(true);
}
} else { } else {
self::$in_transaction = true; self::$in_transaction = true;
} }
@ -860,9 +873,21 @@ class dba {
*/ */
public static function unlock() { public static function unlock() {
// See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html // 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"); $success = self::e("UNLOCK TABLES");
if (self::$driver == 'pdo') {
self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
self::e("SET autocommit=1"); self::e("SET autocommit=1");
} else {
self::$db->autocommit(true);
}
self::$in_transaction = false; self::$in_transaction = false;
return $success; return $success;
} }
@ -873,23 +898,51 @@ class dba {
* @return boolean Was the command executed successfully? * @return boolean Was the command executed successfully?
*/ */
public static function transaction() { public static function transaction() {
if (!self::e('COMMIT')) { if (!self::performCommit()) {
return false; return false;
} }
if (!self::e('START TRANSACTION')) {
switch (self::$driver) {
case 'pdo':
if (self::$db->inTransaction()) {
break;
}
if (!self::$db->beginTransaction()) {
return false; return false;
} }
break;
case 'mysqli':
if (!self::$db->begin_transaction()) {
return false;
}
break;
}
self::$in_transaction = true; self::$in_transaction = true;
return 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 * @brief Does a commit
* *
* @return boolean Was the command executed successfully? * @return boolean Was the command executed successfully?
*/ */
public static function commit() { public static function commit() {
if (!self::e('COMMIT')) { if (!self::performCommit()) {
return false; return false;
} }
self::$in_transaction = false; self::$in_transaction = false;
@ -902,11 +955,20 @@ class dba {
* @return boolean Was the command executed successfully? * @return boolean Was the command executed successfully?
*/ */
public static function rollback() { public static function rollback() {
if (!self::e('ROLLBACK')) { switch (self::$driver) {
return false; 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; self::$in_transaction = false;
return true; return $ret;
} }
/** /**