db = $db; } function determineVersion() { //echo "RUNNING QUERY"; try { $q = $this->db->query('select version from mtrack_schema'); if ($q) { foreach ($q as $row) { return $row[0]; } } } catch (Exception $e) { // print_r($e->toString()); } return null; } function computeFieldCreate($f) { $str = "\t$f->name "; $str .= isset($this->typemap[$f->type]) ? $this->typemap[$f->type] : $f->type; if (isset($f->nullable) && $f->nullable == '0') { $str .= ' NOT NULL '; } if (isset($f->default)) { if (!strlen($f->default)) { $str .= " DEFAULT ''"; } else { $str .= " DEFAULT $f->default"; } } return $str; } function computeIndexCreate($table, $k) { switch ($k->type) { case 'unique': $kt = ' UNIQUE '; break; case 'multiple': default: $kt = ''; } return "CREATE $kt INDEX $k->name on $table->name (" . join(', ', $k->fields) . ")"; } 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; } $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(); } } function alterTable(MTrackDBSchema_Table $from, MTrackDBSchema_Table $to) { /* if keys have changed, we drop the old key definitions before changing the columns */ echo "Need to alter $from->name\n"; throw new Exception("bang!"); } function dropTable(MTrackDBSchema_Table $table) { echo "Drop $table->name\n"; $this->db->exec("drop table $table->name"); } }