changeset 1110:a5f52e54c9da

Improve stDBGetTableSchema().
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 16 May 2019 23:25:49 +0300
parents ac3bd4e94555
children f9556e72c2d3
files msitegen.inc.php
diffstat 1 files changed, 31 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/msitegen.inc.php	Thu May 16 21:59:44 2019 +0300
+++ b/msitegen.inc.php	Thu May 16 23:25:49 2019 +0300
@@ -742,46 +742,62 @@
 }
 
 
-function stDBGetTableSchema($dbh, $data)
+function stDBGetTableSchema($dbh, $schema)
 {
-  $res = array();
+  $res = [];
   $driver = $dbh->getAttribute(PDO::ATTR_DRIVER_NAME);
 
-  foreach ($data as $col)
+  // Go through the table schema, definition by definition
+  foreach ($schema as $scol)
   {
-    $tmp = array();
+    $tmp = [];
 
+    // And each element of the one definition
+    // (like 'foo INTEGER AUTOINCREMENT')
+    foreach ($scol as $elem)
     switch ($driver)
     {
       case "pgsql":
-        foreach ($col as $elem)
+        switch ($elem)
         {
-          // For Postgres, use SERIAL for autoincrement
-          if ($elem == "AUTOINCREMENT")
+          case "AUTOINCREMENT":
+            // For Postgres, use SERIAL for autoincrement and
+            // "cleverly" replace the 2nd element with SERIAL
+            // assuming that it is INTEGER or such.
             $tmp[1] = "SERIAL";
-          else
+            break;
+
+          case "DATETIME":
+            $tmp[] = "TIMESTAMPTZ";
+            break;
+
+          default:
             $tmp[] = $elem;
+            break;
         }
         break;
 
       case "mysql":
-        foreach ($col as $elem)
+        switch ($elem)
         {
-          if ($elem == "AUTOINCREMENT")
+          case "AUTOINCREMENT":
             $tmp[] = "AUTO_INCREMENT";
-          else
+            break;
+
+          default:
             $tmp[] = $elem;
+            break;
         }
         break;
 
       case "sqlite":
-        $tmp = $col;
+        $tmp[] = $elem;
         break;
-      
+
       default:
-        die("Don't know how to handle PDO driver '".$driver."' yet.\n");
+        die("Don't know how to handle PDO driver '".$driver."'.\n");
     }
-    
+
     $res[] = implode(" ", $tmp);
   }