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 getSdlFromTable($table) { $meta = $this->getTableMeta($table); $root = new SdlTag("table", [$table]); foreach ($meta as $row => $md) { $col = new SdlTag("column", [$row]); $col->type = $md->type; if (!$md->null) { $col->null = false; } $col->default = $md->default; $col->setComment($md->comment); $root->addChild($col); } return $root->encode(); }
<?php require_once "../../share/include/cherryphp"; use Cherry\Data\Ddl\SdlTag; use Cherry\Util\Timer; // Create a new root node, we need this. $root = new SdlTag("cyanogenmod"); $root->setComment("These are the latest three CyanogenMod versions with some info"); // You can create nodes and set attributes manually $cm9 = new SdlTag("version", "9"); $cm9->multiuser = false; $cm9->android = "4.0.x"; $cm9->setComment("Based on AOSP 4.0.x"); $root->addChild($cm9); // Or provide them to the constructor $cm10 = new SdlTag("version", "10", ['multiuser' => false, 'android' => "4.1.x"]); $cm10->setComment("Based on AOSP 4.1.x"); $root->addChild($cm10); // Comments can also go as the 5th argument $cm101 = new SdlTag("version", "10.1", ['multiuser' => true, 'android' => "4.2.x", 'note' => "New and hot!"], null, "Based on AOSP 4.2.x"); $root->addChild($cm101); // And the data can be serialized to SDL $sdl = $root->encode(); echo $sdl; // Read it back out again $test = new SdlTag("root"); $test->loadString($sdl); // Print some info about the various versions. foreach ($test->getChild("cyanogenmod")->getChildren() as $child) { echo "CM{$child[0]}: Based on Android AOSP {$child->android}."; if ($child->multiuser) {
public function testComments() { $test = new SdlTag("commented"); $comment = "This is a comment"; $test->setComment($comment); $this->assertEquals($comment, $test->getComment()); }