final move of files
[web.mtrack] / MTrack / DBSchema / SQLite.php
1 <?php
2
3 require_once 'DBSchema/Generic.php';
4
5 class MTrackDBSchema_SQLite extends MTrackDBSchema_Generic {
6
7   function determineVersion() {
8     /* older versions did not have a schema version table, so we dance
9      * around a little bit, but only for sqlite, as those older versions
10      * didn't support other databases */
11     try {
12       $q = $this->db->query('select version from mtrack_schema');
13       if ($q) {
14         foreach ($q as $row) {
15           return $row[0];
16         }
17       }
18     } catch (Exception $e) {
19     }
20
21     /* do we have any tables at all? if we do, we treat that as schema
22      * version 0 */
23     foreach ($this->db->query('select count(*) from sqlite_master') as $row) {
24       if ($row[0] > 0) {
25         $this->db->exec(
26           'create table mtrack_schema (version integer not null)');
27         return 0;
28       }
29     }
30     return null;
31   }
32
33   var $typemap = array(
34     'autoinc' => 'INTEGER PRIMARY KEY AUTOINCREMENT',
35     'longtext' => 'text'
36   );
37
38   function createTable(MTrackDBSchema_Table $table)
39   {
40     parent::createTable($table);
41   }
42
43   function alterTable(MTrackDBSchema_Table $from, MTrackDBSchema_Table $to)
44   {
45     $tname = $from->name . '_' . uniqid();
46
47     $sql = array();
48     foreach ($to->fields as $f) {
49       if ($f->type == 'autoinc') {
50         $pri_key = $f->name;
51       }
52       $str = $this->computeFieldCreate($f);
53       $sql[] = $str;
54     }
55
56     $sql = "CREATE TEMPORARY TABLE $tname (\n" .
57       join(",\n", $sql) .
58       ")\n";
59
60     $this->db->exec($sql);
61
62     /* copy old data into this table */
63     $sql = "INSERT INTO $tname (";
64     $new_names = array();
65     $old_names = array();
66     foreach ($from->fields as $f) {
67         
68         if (!isset($to->fields[$f->name])) {
69             // to_field does not exist in new schema.
70             // look for it.
71             foreach( $to->fields as $tf) {
72                 if (!empty($tf->oldname) && $tf->oldname == $f->name) {
73                     $new_names[] = $f->name;
74                     $old_names[] = $f->oldname;
75                     break;
76                 }
77             }
78             continue;
79         }
80         $new_names[] = $f->name;
81         $old_names[] = $f->name;
82     }
83     $sql .= join(', ', $new_names);
84     $sql .= ") SELECT " . join(', ', $names) . " from $from->name";
85
86     #echo "$sql\n";
87     $this->db->exec($sql);
88
89     $this->db->exec("DROP TABLE $from->name");
90     $this->createTable($to);
91     $sql = "INSERT INTO $from->name (";
92     $names = array();
93     foreach ($from->fields as $f) {
94       if (!isset($to->fields[$f->name])) continue;
95       $names[] = $f->name;
96     }
97     $sql .= join(', ', $names);
98     $sql .= ") SELECT " . join(', ', $names) . " from $tname";
99     #echo "$sql\n";
100     $this->db->exec($sql);
101     $this->db->exec("DROP TABLE $tname");
102   }
103
104
105 }