3 require_once 'DBSchema/Generic.php';
5 class MTrackDBSchema_SQLite extends MTrackDBSchema_Generic {
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 */
12 $q = $this->db->query('select version from mtrack_schema');
14 foreach ($q as $row) {
18 } catch (Exception $e) {
21 /* do we have any tables at all? if we do, we treat that as schema
23 foreach ($this->db->query('select count(*) from sqlite_master') as $row) {
26 'create table mtrack_schema (version integer not null)');
34 'autoinc' => 'INTEGER PRIMARY KEY AUTOINCREMENT',
38 function createTable(MTrackDBSchema_Table $table)
40 parent::createTable($table);
43 function alterTable(MTrackDBSchema_Table $from, MTrackDBSchema_Table $to)
45 $tname = $from->name . '_' . uniqid();
48 foreach ($to->fields as $f) {
49 if ($f->type == 'autoinc') {
52 $str = $this->computeFieldCreate($f);
56 $sql = "CREATE TEMPORARY TABLE $tname (\n" .
60 $this->db->exec($sql);
62 /* copy old data into this table */
63 $sql = "INSERT INTO $tname (";
66 foreach ($from->fields as $f) {
68 if (!isset($to->fields[$f->name])) {
69 // to_field does not exist in new schema.
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;
80 $new_names[] = $f->name;
81 $old_names[] = $f->name;
83 $sql .= join(', ', $new_names);
84 $sql .= ") SELECT " . join(', ', $names) . " from $from->name";
87 $this->db->exec($sql);
89 $this->db->exec("DROP TABLE $from->name");
90 $this->createTable($to);
91 $sql = "INSERT INTO $from->name (";
93 foreach ($from->fields as $f) {
94 if (!isset($to->fields[$f->name])) continue;
97 $sql .= join(', ', $names);
98 $sql .= ") SELECT " . join(', ', $names) . " from $tname";
100 $this->db->exec($sql);
101 $this->db->exec("DROP TABLE $tname");