From ab177947aef639099e8836793a08804905f2b27a Mon Sep 17 00:00:00 2001 From: Bruce Wells Date: Mon, 31 Oct 2022 12:12:29 -0400 Subject: [PATCH] Pre and Post SQL --- src/Mysqldump.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/Mysqldump.php b/src/Mysqldump.php index 3fa32ddb..fcd6223b 100644 --- a/src/Mysqldump.php +++ b/src/Mysqldump.php @@ -59,6 +59,10 @@ class Mysqldump private array $tableWheres = []; private array $tableLimits = []; + // pended SQL defaults to nothing + private string $prependSQL = ''; + private string $appendSQL = ''; + /** * Constructor of Mysqldump. * @@ -163,6 +167,30 @@ private function write(string $data): int return $this->io->write($data); } + /** + * Add SQL to the start of the backup + * + * @param string $sql to add at start of backup. Caller is responsible for ensuring the SQL is valid the the backup. + * */ + public function addPrependSQL(string $sql) + { + $this->prependSQL .= $sql; + + return $this; + } + + /** + * Add SQL to the end of the backup + * + * @param string $sql to add at end of backup. Caller is responsible for ensuring the SQL is valid the the backup. + */ + public function addAppendSQL(string $sql) + { + $this->appendSQL .= $sql; + + return $this; + } + /** * Primary function, triggers dumping. * @@ -192,6 +220,13 @@ public function start(?string $filename = '') $this->write($this->getDumpFileHeader()); } + // Write prepend SQL to output file + if ($this->prependSQL) { + $this->write("-- mysqldump-php prepended SQL". PHP_EOL. + $this->prependSQL. PHP_EOL. + "-- mysqldump-php end prepended SQL". PHP_EOL. PHP_EOL); + } + // Store server settings and use saner defaults to dump $this->write($this->db->backupParameters()); @@ -233,6 +268,13 @@ public function start(?string $filename = '') // Restore saved parameters. $this->write($this->db->restoreParameters()); + // Write append SQL to output file + if ($this->appendSQL) { + $this->write("-- mysqldump-php appended SQL". PHP_EOL. + $this->appendSQL. PHP_EOL. + "-- mysqldump-php end appended SQL". PHP_EOL. PHP_EOL); + } + // Write some stats to output file. if (!$this->settings->skipComments()) { $this->write($this->getDumpFileFooter());