From 30cf772c4daccf02f5fdf7706715cd9b5c0d3ff5 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Thu, 13 Oct 2016 17:54:58 +0200 Subject: [PATCH] move ADOdb row_lock and rollback_lock methods to Db class, use Db::$tablealiases and use them for backup, also added a "backup_(starts|finished)" hook for apps --- api/src/Db.php | 64 +++++++++++++++++++++++++++++++++++++++++++ api/src/Db/Backup.php | 11 ++++++-- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/api/src/Db.php b/api/src/Db.php index a47c0eb393..c016a7edb5 100644 --- a/api/src/Db.php +++ b/api/src/Db.php @@ -852,6 +852,70 @@ class Db return $this->Link_ID->FailTrans(); } + /** + * Lock a rows in table + * + * Will escalate and lock the table if row locking not supported. + * Will normally free the lock at the end of the transaction. + * + * @param string $table name of table to lock + * @param string $where ='true' where clause to use, eg: "WHERE row=12". Defaults to lock whole table. + * @param string $col ='1 as adodbignore' + */ + function row_lock($table, $where='true', $col='1 as adodbignore') + { + if (!$this->Link_ID && !$this->connect()) + { + return False; + } + if (self::$tablealiases && isset(self::$tablealiases[$table])) + { + $table = self::$tablealiases[$table]; + } + + return $this->Link_ID->RowLock($table, $where, $col); + } + + /** + * Commit changed rows in table + * + * @param string $table + * @return boolean + */ + function commit_lock($table) + { + if (!$this->Link_ID && !$this->connect()) + { + return False; + } + if (self::$tablealiases && isset(self::$tablealiases[$table])) + { + $table = self::$tablealiases[$table]; + } + + return $this->Link_ID->CommitLock($table); + } + + /** + * Unlock rows in table + * + * @param string $table + * @return boolean + */ + function rollback_lock($table) + { + if (!$this->Link_ID && !$this->connect()) + { + return False; + } + if (self::$tablealiases && isset(self::$tablealiases[$table])) + { + $table = self::$tablealiases[$table]; + } + + return $this->Link_ID->RollbackLock($table); + } + /** * Find the primary key of the last insertion on the current db connection * diff --git a/api/src/Db/Backup.php b/api/src/Db/Backup.php index c686933d55..bfd5855bf6 100644 --- a/api/src/Db/Backup.php +++ b/api/src/Db/Backup.php @@ -920,6 +920,9 @@ class Backup fwrite($f,"\nschema: ".json_encode($this->schemas)."\n"); + // let apps know that backup is about to start + Api\Hooks::process('backup_starts', array(), true); + foreach($this->schemas as $table => $schema) { if (in_array($table,$this->exclude_tables)) continue; // dont backup @@ -932,7 +935,7 @@ class Backup if ($lock_table || empty($pk) && is_null($lock_table)) { - $this->db->Link_ID->RowLock($table, 'true'); // PostgreSQL drive from ADOdb requires 2. param $where! + $this->db->Link_ID->row_lock($table); } $total = $max = 0; do { @@ -959,8 +962,12 @@ class Backup } while((!empty($pk) || $lock_table !== false) && !($total % self::ROW_CHUNK) && $num_rows); - if (!$pk) $this->db->Link_ID->RollbackLock($table); + if (!$pk) $this->db->rollback_lock($table); } + + // let apps know that backup is finished + Api\Hooks::process('backup_finished', array(), true); + if(!$zippresent) // save without files { if ($this->backup_files)