From: konrad Date: Sun, 30 May 2010 03:23:07 +0000 (+0000) Subject: provide sequence sync'ing X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=81b73160e69fc0c4af5cd9f5f5182ed872aa3a06;p=konrad%2Fpack.git provide sequence sync'ing git-svn-id: https://silmor.de/svn/softmagic/pack/trunk@484 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- diff --git a/phpbase/db.php b/phpbase/db.php index b0235b9..f86bda6 100644 --- a/phpbase/db.php +++ b/phpbase/db.php @@ -503,6 +503,18 @@ abstract class DbEngine } } + /**synchronize all sequences with current data*/ + public function syncSequences() + { + global $dbScheme; + foreach($dbScheme->tableNames() as $table) + if($dbScheme->hasSequence($table)!==false) + $this->syncSequence($table); + } + + /**synchronize the sequence of one database table; overwrite to actually do something*/ + public function syncSequence($table){} + /**helper: inserts data from backup*/ private function backupInsert($table,$data,$overwrite) { @@ -553,6 +565,7 @@ abstract class DbEngine /*currently ignored, maybe we do something with it later*/ break; case "endofbackup": + $this->syncSequences(); print("Reached End of Backup.
\n"); break 2; case "table": diff --git a/phpbase/db_pgsql.php b/phpbase/db_pgsql.php index 5b0f518..04a050e 100644 --- a/phpbase/db_pgsql.php +++ b/phpbase/db_pgsql.php @@ -9,8 +9,7 @@ class PGsqlEngine extends DbEngine private $dbhdl=false; private $charset="UTF8"; private $intrans=false; - //used for table creation to work around mysql bugs - private $tableappend; + private $do_debug=false; /**initialize driver*/ public function __construct($connstring) @@ -326,11 +325,36 @@ class PGsqlEngine extends DbEngine else return false; } + /**synchronize the sequence of one database table*/ + public function syncSequence($table) + { + $tab=$this->tableName($table); + global $dbScheme; + $col=$dbScheme->hasSequence($table); + if($col===false)return; + $seq=$tab."_".$col."_seq"; + $q="select setval('".$seq."',(select max(".$col.") from ".$tab."))"; +// print($q."
\n"); + $res=@pg_query($this->dbhdl,$q); + if($res===false){ + $this->db_debug_error("sequence sync"); + return false; + } + pg_free_result($res); + return true; + } + + /**development helper: activate debug mode*/ + public function setDebugMode($b=true) + { + $this->do_debug=$b; + } + /**internal helper: print debug message as XML comment, if activated*/ private function db_debug_error($what) { //change this to if(1) if you want debug messages - if(0) + if($this->do_debug) print("\n"); } };