php8
[web.mtrack] / inc / DBSchema / mysql.php
1 <?php
2
3
4 require_once 'DBSchema/Generic.php';
5
6 class MTrackDBSchema_mysql extends MTrackDBSchema_Generic 
7 {
8     var $typemap = array(
9         'autoinc' =>  'INT(11) NOT NULL AUTO_INCREMENT',
10         'timestamp' => 'datetime',
11         'blob' => 'longtext', // eak what blob is stored?
12         'text' => 'VARCHAR(128)',
13     );
14
15
16     function determineVersion() {
17         $ret = parent::determineVersion();
18         if ($ret) {
19             return $ret;
20         }
21         parse_str(str_replace(array(";",":"), "&", $this->db->dsn), $dsn);
22         
23         try {
24             $this->db->query("CREATE DATABASE {$dsn['dbname']}");
25         } catch (Exception $e) {
26             // technically this should not get here..
27             // as pdo's dsn design is borked..
28         }
29          
30          
31         // assume we need to create database..
32         return null;
33     }
34     
35     
36     function computeFieldCreate($f)
37     {
38         $str = "\t`$f->name` ";
39         $str .= isset($this->typemap[$f->type]) ? $this->typemap[$f->type] : $f->type;
40         if (isset($f->nullable) && $f->nullable == '0') {
41           $str .= ' NOT NULL ';
42         }
43         if (!isset($f->default)) {
44             return $str;
45         }
46         if (!strlen($f->default)) {
47             return $str . " DEFAULT ''";
48         }
49         
50         return $str . ($f->default == 'CURRENT_TIMESTAMP' ? '' : 'DEFAULT '. $f->default);
51     }
52     
53     // may as well rewrite this.. as we have too loop throug hit anyway..
54     function createTable(MTrackDBSchema_Table $table)
55     {
56         echo "Create $table->name\n";
57
58         $pri_key = null;
59         $sql = array();
60         foreach ($table->fields as $f) {
61           if ($f->type == 'autoinc') {
62             $pri_key = $f->name;
63             
64           }
65           $str = $this->computeFieldCreate($f);
66           $sql[] = $str;
67         }
68         if (is_array($table->keys)) {
69             
70             foreach ($table->keys as $kn=>$k) {
71               if ($k->type != 'primary') continue;
72              //   if ($pri_key !== null) continue;
73                 $sql[] = "\tprimary key (`" . join('`, `', $k->fields) . "`)";
74             }
75         }
76
77         $sql = "CREATE TABLE $table->name (\n" .
78           join(",\n", $sql) .
79           ")\n";
80
81         echo $sql;
82
83         $this->db->exec($sql);
84     }
85   function alterTable(MTrackDBSchema_Table $from, MTrackDBSchema_Table $to)
86   {
87     $sql = array();
88     $actions = array();
89
90     /* if keys have changed, we drop the old key definitions before changing the columns */
91     if (is_array($from->keys)) foreach ($from->keys as $k) {
92       if (!isset($to->keys[$k->name]) || $to->keys[$k->name] != $k) {
93         if ($k->type == 'primary') {
94           $actions[] = "DROP PRIMARY KEY";
95         } else {
96           $actions[] = "DROP INDEX $k->name";
97         }
98       }
99     }
100
101     foreach ($from->fields as $f) {
102       if (!isset($to->fields[$f->name])) {
103         $actions[] = "DROP COLUMN $f->name";
104         continue;
105       }
106     }
107     foreach ($to->fields as $f) {
108       if (isset($from->fields[$f->name])) continue;
109       $actions[] = "ADD COLUMN " . $this->computeFieldCreate($f);
110     }
111
112     /* changed and new keys */
113     if (is_array($from->keys)) foreach ($from->keys as $k) {
114       if (isset($to->keys[$k->name]) && $to->keys[$k->name] != $k) {
115         if ($k->type == 'primary') {
116           $actions[] = "ADD primary key (" . join(', ', $k->fields) . ")";
117         } else {
118           $sql[] = $this->computeIndexCreate($to, $k);
119         }
120       }
121     }
122     if (is_array($to->keys)) foreach ($to->keys as $k) {
123       if (isset($from->keys[$k->name])) continue;
124       if ($k->type == 'primary') {
125         $actions[] = "ADD primary key (" . join(', ', $k->fields) . ")";
126       } else {
127         $sql[] = $this->computeIndexCreate($to, $k);
128       }
129     }
130
131     if (count($actions)) {
132       $sql[] = "ALTER TABLE $from->name " . join(",\n", $actions);
133     }
134     echo "Need to alter $from->name\n";
135     echo "SQL:\n";
136     var_dump($sql);
137     foreach ($sql as $s) {
138       $this->db->exec($s);
139     }
140   }
141 }