예제 #1
0
파일: db.php 프로젝트: noccy80/cherryphp
 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";
         }
     }
 }
예제 #2
0
 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;
 }
예제 #3
0
 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();
 }
예제 #4
0
<?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) {
예제 #5
0
    public function testBinaryData()
    {
        // This is a node with base64-encoded data.
        $sdl = <<<EOT
testnode [SGVsbG8gV29ybGQh];
EOT;
        $sdl3 = <<<EOT
root {
    testnode [SGVsbG8gV29ybGQh]
}
EOT;
        $sdl2 = <<<EOT
root {
    testnode [SGVsbG8gV29ybGQh]
    testnode2 [UHV0dGluZyBiaW5hcnkgZGF0YSBpbiB0aGUgU0RMIG5vZGU=]
}
EOT;
        // Read it back out again
        $test = new SdlTag("root");
        $test->loadString($sdl);
        $enc = $test->encode();
        $this->assertEquals(trim($sdl3), trim($enc), "Encoded data does not match decoded data");
        $node = new SdlTag("testnode2");
        $testvalue = "Putting binary data in the SDL node";
        $node->setValue($testvalue, 0, SdlTypedValue::LT_BINARY);
        // $node->setBinaryValue($testvalue);
        $test->addChild($node);
        $this->assertEquals($testvalue, $node[0], "Set binary string does not match retrieved binary string");
        $enc2 = $test->encode();
        $this->assertEquals(trim($sdl2), trim($enc2), "Encoded data after adding binary data does not match expected data");
    }
예제 #6
0
<?php

require_once "../../share/include/cherryphp";
use Cherry\Data\Ddl\SdlTag;
use Cherry\Util\Timer;
// This is a node with base64-encoded data.
$sdl = <<<EOT
testnode [SGVsbG8gV29ybGQh];
EOT;
// Read it back out again
$test = new SdlTag("root");
$test->loadString($sdl);
$node = new SdlTag("testnode2");
$node->setValue("Putting binary data in the SDL node", 0, SdlTag::LT_BINARY);
$test->addChild($node);
echo "Current state of SDL tree:\n";
echo $test->encode() . "\n";
echo "Node values:\n";
echo "  testnode=" . $test->getChild("testnode")[0] . "\n";
echo "  testnode2=" . $test->getChild("testnode2")[0] . "\n";
예제 #7
0
$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);
$temp = new SdlTag("root");
$temp->addChild($root);
testspath($temp, "cyanogenmod/version[@multiuser=true]");
testspath($temp, "cyanogenmod/version[10]");
testspath($temp, "cyanogenmod/*");
function testspath($root, $expr)
{
    echo "Expression: '{$expr}'\n";
    foreach ($root->query($expr) as $child) {
        echo "  CM{$child[0]}: Based on Android AOSP {$child->android}.";
        if ($child->multiuser) {
            echo " Multiple user-profiles supported.";
        }
        echo "\n";
    }
}