inc/DBSchema/mysql.php
authorAlan Knowles <alan@akbkhome.com>
Thu, 27 Jan 2011 15:25:30 +0000 (23:25 +0800)
committerAlan Knowles <alan@akbkhome.com>
Thu, 27 Jan 2011 15:25:30 +0000 (23:25 +0800)
inc/DBSchema/mysql.php

index 7f16e84..f944cfd 100644 (file)
@@ -9,6 +9,7 @@ class MTrackDBSchema_mysql extends MTrackDBSchema_Generic
         'autoinc' =>  'INT(11) NOT NULL AUTO_INCREMENT',
         'timestamp' => 'datetime',
         'blob' => 'longtext', // eak what blob is stored?
+        'integer'
     );
 
 
@@ -30,7 +31,52 @@ class MTrackDBSchema_mysql extends MTrackDBSchema_Generic
         // assume we need to create database..
         return null;
     }
-  
+    // may as well rewrite this.. as we have too loop throug hit anyway..
+    function createTable(MTrackDBSchema_Table $table)
+    {
+        echo "Create $table->name\n";
+
+        $pri_key = null;
+
+        $sql = array();
+        foreach ($table->fields as $f) {
+          if ($f->type == 'autoinc') {
+            $pri_key = $f->name;
+            if (!$table->keys) {
+                $table->keys = array(   
+                    (object)array(  
+                        'type' => 'primary',
+                        'fields' => array($f->name)
+                    )
+                );
+               }
+          }
+          $str = $this->computeFieldCreate($f);
+          $sql[] = $str;
+        }
+
+        if (is_array($table->keys)) {
+            
+            foreach ($table->keys as $k) {
+                if ($k->type != 'primary') continue;
+                if ($pri_key !== null) continue;
+                $sql[] = "\tprimary key (" . join(', ', $k->fields) . ")";
+            }
+        }
+
+        $sql = "CREATE TABLE $table->name (\n" .
+          join(",\n", $sql) .
+          ")\n";
+
+        echo $sql;
+
+        $this->db->exec($sql);
+
+        if (is_array($table->keys)) foreach ($table->keys as $k) {
+          if ($k->type == 'primary') continue;
+          $this->db->exec($this->computeIndexCreate($table, $k));
+        }
+    }
   
   function alterTable(MTrackDBSchema_Table $from, MTrackDBSchema_Table $to)
   {