public function getSdlTag() { $table = $this; // Print out the table nodes $sdl = new SdlTag("table", $table->name); $sdl->setComment("Table {$table->name} from database {$table->database->name}"); // Convert the columns to SDL foreach ($table->getColumns() as $co) { $col = new SdlTag("column", $co->name, ['type' => $co->type]); if (!empty($co->default)) { $col->setAttribute('default', $co->default); } if ($co->auto) { $col->setAttribute('auto', 1); } $col->setAttribute('null', $co->null); if (!empty($co->comment)) { $col->setAttribute('comment', $co->comment); } $sdl->addChild($col); } // Convert the indexes to SDL $idx = new SdlTag("indexes"); foreach ($table->getIndexes() as $ix) { $cols = $ix->columns; $ii = new SdlTag($ix->type, $ix->name); $ii->setComment("Index {$ix->name}"); $ii->addChild(new SdlTag(NULL, $cols)); $idx->addChild($ii); } $sdl->addChild($idx); return $sdl; }
public function dbtosdl() { $args = func_get_args(); $opts = $this->parseOpts($args, array('database' => 'database:', 'table' => 'table:', 'with-data' => '+withdata')); var_dump($opts); // Get the connection instance if (empty($opts['database'])) { $conn = DatabaseConnection::getInstance("default"); } else { $conn = DatabaseConnection::getInstance($opts['database']); } // Enumerate all the tables foreach ($conn->tables->getAll() as $table) { if (empty($opts['table']) || $table->name == $opts['table']) { // Print out the table nodes $sdl = new SdlTag("table", $table->name); $sdl->setComment("Table {$table->name} from database {$table->database->name}"); // Convert the columns to SDL foreach ($table->getColumns() as $co) { $col = new SdlTag("column", $co->name, ['type' => $co->type]); if ($co->default !== NULL) { $col->setAttribute('default', $co->default); } if ($co->auto) { $col->setAttribute('auto', 1); } $col->setAttribute('null', $co->null); $sdl->addChild($col); } // Convert the indexes to SDL $idx = new SdlTag("indexes"); foreach ($table->getIndexes() as $ix) { $cols = $ix->columns; $ii = new SdlTag($ix->type, $ix->name); $ii->setComment("Index {$ix->name}"); $ii->addChild(new SdlTag(NULL, $cols)); $idx->addChild($ii); } $sdl->addChild($idx); $rs = $conn->query("SELECT * FROM {$table->name}"); $data = new SdlTag("data"); foreach ($rs as $row) { $rdata = new SdlTag("row"); foreach ($row as $k => $v) { if (!is_integer($k)) { $rdata->addChild(new SdlTag($k, $v)); } } $data->addChild($rdata); } $sdl->addChild($data); echo $sdl->encode() . "\n"; } } }
public function testAttributes() { $test = new SdlTag("root"); $test->setAttribute("name", "bob"); $this->assertEquals("bob", $test->name); $test->name = "joe"; $this->assertEquals("joe", $test->name); unset($test->name); $this->assertEquals(null, $test->name); $test->loadString("pet name=\"bobo\" type=\"dog\" alive=true age=14"); $this->assertEquals("dog", $test->getChild("pet")->type, "String ttribute don't match parsed value"); $this->assertEquals(true, $test->getChild("pet")->alive, "Bool attribute don't match parsed value"); $this->assertEquals(14, $test->getChild("pet")->age, "Numeric attribute don't match parsed value"); $this->assertEquals("bobo", $test->getChild("pet")->name, "String attribute don't match parsed value"); $attr = $test->getChild("pet")->getAttributes(); $match = ['name' => 'bobo', 'type' => 'dog', 'age' => 14, 'alive' => true]; $this->assertEquals($match, $attr, "Unexpected attribute set returned"); }