provide sequence sync'ing
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 30 May 2010 03:23:07 +0000 (03:23 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 30 May 2010 03:23:07 +0000 (03:23 +0000)
git-svn-id: https://silmor.de/svn/softmagic/pack/trunk@484 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

phpbase/db.php
phpbase/db_pgsql.php

index b0235b9..f86bda6 100644 (file)
@@ -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.<br>\n");
                                        break 2;
                                case "table":
index 5b0f518..04a050e 100644 (file)
@@ -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."<br/>\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("<!-- Error while doing '".$what."' on DB: ".xq($this->lastError())." -->\n");
        }
 };