From b4aec46a67f7521a0bb8933bf0fb48ae5b9c8303 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 21 Sep 2018 13:30:57 -0400 Subject: [PATCH] Add $database->supportsTransaction($table = '') method to return true or false as to whether the database (or a specific table) supports transactions --- wire/core/WireDatabasePDO.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/wire/core/WireDatabasePDO.php b/wire/core/WireDatabasePDO.php index 4597618f..7d539b92 100644 --- a/wire/core/WireDatabasePDO.php +++ b/wire/core/WireDatabasePDO.php @@ -332,6 +332,32 @@ class WireDatabasePDO extends Wire implements WireDatabase { return $this->pdo()->inTransaction(); } + /** + * Are transactions available with current DB engine (or table)? + * + * #pw-group-PDO + * + * @param string $table Optionally specify a table to specifically check to that table + * @return bool + * + */ + public function supportsTransaction($table = '') { + $engine = ''; + if($table) { + $query = $this->prepare('SHOW TABLE STATUS WHERE name=:name'); + $query->bindValue(':name', $table); + $query->execute(); + if($query->rowCount()) { + $row = $query->fetch(\PDO::FETCH_ASSOC); + $engine = empty($row['engine']) ? '' : $row['engine']; + } + $query->closeCursor(); + } else { + $engine = $this->wire('config')->dbEngine; + } + return strtoupper($engine) === 'INNODB'; + } + /** * Commits a transaction *