3 require_once 'Interface/DBSchema_Driver.php';
5 class MTrackDBSchema_Generic implements IMTrackDBSchema_Driver {
7 var $typemap = array();
9 function setDB(PDO $db) {
13 function determineVersion() {
15 $q = $this->db->query('select version from mtrack_schema');
17 foreach ($q as $row) {
21 } catch (Exception $e) {
22 print_r($e->toString());
27 function computeFieldCreate($f) {
29 $str .= isset($this->typemap[$f->type]) ? $this->typemap[$f->type] : $f->type;
30 if (isset($f->nullable) && $f->nullable == '0') {
33 if (isset($f->default)) {
34 if (!strlen($f->default)) {
35 $str .= " DEFAULT ''";
37 $str .= " DEFAULT $f->default";
43 function computeIndexCreate($table, $k) {
52 return "CREATE $kt INDEX $k->name on $table->name (" . join(', ', $k->fields) . ")";
55 function createTable(MTrackDBSchema_Table $table)
57 echo "Create $table->name\n";
62 foreach ($table->fields as $f) {
63 if ($f->type == 'autoinc') {
66 $str = $this->computeFieldCreate($f);
70 if (is_array($table->keys)) foreach ($table->keys as $k) {
71 if ($k->type != 'primary') continue;
72 if ($pri_key !== null) continue;
73 $sql[] = "\tprimary key (" . join(', ', $k->fields) . ")";
76 $sql = "CREATE TABLE $table->name (\n" .
82 $this->db->exec($sql);
84 if (is_array($table->keys)) foreach ($table->keys as $k) {
85 if ($k->type == 'primary') continue;
86 $this->db->exec($this->computeIndexCreate($table, $k));
90 function alterTable(MTrackDBSchema_Table $from, MTrackDBSchema_Table $to)
92 /* if keys have changed, we drop the old key definitions before changing the columns */
94 echo "Need to alter $from->name\n";
95 throw new Exception("bang!");
98 function dropTable(MTrackDBSchema_Table $table)
100 echo "Drop $table->name\n";
101 $this->db->exec("drop table $table->name");