public function run() { clearstatcache(); $this->checkInputs(); $outputDir = RUN_BASE . DS . LIB_DIR_NAME . DS . "schema"; $this->defineEnvironment($this->arguments[0]); Sabel_Db_Config::initialize(new Config_Database()); $isAll = false; $tables = $this->getOutputTables(); if (isset($tables[0]) && strtolower($tables[0]) === "all") { $isAll = count($tables) === 1; } $tList = new TableListWriter($outputDir); foreach (Sabel_Db_Config::get() as $connectionName => $params) { Sabel_Db_Config::add($connectionName, $params); $db = Sabel_Db::createMetadata($connectionName); foreach ($db->getTableList() as $tblName) { if ($isAll || in_array($tblName, $tables, true)) { $writer = new Sabel_Db_Metadata_FileWriter($outputDir); $writer->write($db->getTable($tblName)); $this->success("generate Schema 'Schema_" . convert_to_modelname($tblName) . "'"); } $tList->add($connectionName, $tblName); } if (Sabel_Console::hasOption("l", $this->arguments)) { $tList->write($connectionName); } } }
public function testClean() { $tables = self::$tables; $driver = Sabel_Db::createDriver("default"); foreach ($tables as $table) { $driver->execute("DELETE FROM {$table}"); } }
public function testBuildSelectWhereQuery() { $stmt = Sabel_Db::createStatement("default"); $stmt->type(Sabel_Db_Statement::SELECT); $stmt->setMetadata(Sabel_Db_Metadata::getTableInfo("student")); $stmt->where('WHERE "ID" = 1'); $expected = 'SELECT "ID", "NAME" FROM "STUDENT" WHERE "ID" = 1'; $this->assertEquals($expected, $stmt->getQuery()); }
public function testBuildSelectOrderByQuery2() { $stmt = Sabel_Db::createStatement("default"); $stmt->type(Sabel_Db_Statement::SELECT); $stmt->setMetadata(Sabel_Db_Metadata::getTableInfo("student")); $stmt->constraints(array("order" => array("id" => "DESC", "name" => "ASC"))); $expected = 'SELECT "ID", "NAME" FROM "STUDENT" ORDER BY "ID" DESC, "NAME" ASC'; $this->assertEquals($expected, $stmt->getQuery()); }
/** * @param string $connectionName * * @return void */ public static function close($connectionName) { if (!isset(self::$connections[$connectionName])) { return; } $conn = self::$connections[$connectionName]; Sabel_Db::createDriver($connectionName)->close($conn); unset(self::$connections[$connectionName]); }
public static function getTableList($connectionName = "default") { $className = "Schema_" . ucfirst($connectionName) . "TableList"; if (Sabel::using($className)) { $sc = new $className(); return $sc->get(); } else { return Sabel_Db::createMetadata($connectionName)->getTableList(); } }
private static function initTable() { if (extension_loaded("mysql")) { $params = array("package" => "sabel.db.mysql", "host" => "127.0.0.1", "user" => "root", "password" => "", "database" => "sdb_test"); } elseif (extension_loaded("pgsql")) { $params = array("package" => "sabel.db.pgsql", "host" => "127.0.0.1", "user" => "root", "password" => "", "database" => "sdb_test"); } elseif (extension_loaded("pdo_sqlite")) { $params = array("package" => "sabel.db.pdo.sqlite", "database" => SABEL_BASE . "/Test/data/sdb_test.sq3"); } else { Sabel_Console::message("skipped 'Test_Session_Database'."); return false; } Sabel_Db_Config::add("default", $params); Sabel_Db::createDriver("default")->execute("DELETE FROM session"); return true; }
private static function initTable() { if (extension_loaded("mysql")) { $params = array("package" => "sabel.db.mysql", "host" => "127.0.0.1", "user" => "root", "password" => "", "database" => "sdb_test"); } elseif (extension_loaded("pgsql")) { $params = array("package" => "sabel.db.pgsql", "host" => "127.0.0.1", "user" => "root", "password" => "", "database" => "sdb_test"); } elseif (extension_loaded("pdo_sqlite")) { $params = array("package" => "sabel.db.pdo.sqlite", "database" => SABEL_BASE . "/Test/data/sdb_test.sq3"); } else { Sabel_Console::message("skipped 'TemplateDb'."); return false; } Sabel_Db_Config::add("default", $params); $stmt = Sabel_Db::createStatement(); $tblName = $stmt->quoteIdentifier("sbl_template"); $nCol = $stmt->quoteIdentifier("name"); $nsCol = $stmt->quoteIdentifier("namespace"); $cCol = $stmt->quoteIdentifier("contents"); $stmt->setQuery("DELETE FROM {$tblName}")->execute(); $data = array(); $data[0]["path"] = "views" . DS . "serverError" . TPL_SUFFIX; $data[0]["cont"] = ""; $data[1]["path"] = "index" . DS . "views" . DS . "error" . TPL_SUFFIX; $data[1]["cont"] = ""; $data[2]["path"] = "index" . DS . "views" . DS . "hoge" . DS . "index" . TPL_SUFFIX; $data[2]["cont"] = "hoge/index.tpl"; $data[3]["path"] = "index" . DS . "views" . DS . "hoge" . DS . "hoge" . TPL_SUFFIX; $data[3]["cont"] = "hoge/hoge.tpl"; $data[4]["path"] = "index" . DS . "views" . DS . "fuga" . DS . "index" . TPL_SUFFIX; $data[4]["cont"] = "fuga/index.tpl"; $data[5]["path"] = "index" . DS . "views" . DS . "fuga" . DS . "fuga" . TPL_SUFFIX; $data[5]["cont"] = "fuga/fuga.tpl"; foreach ($data as $d) { $query = "INSERT INTO {$tblName}({$nCol}, {$nsCol}, {$cCol}) VALUES('{$d["path"]}', '', '{$d["cont"]}')"; $stmt->setQuery($query)->execute(); } return true; }
public function testEscapeString() { $stmt = Sabel_Db::createStatement("default"); $this->assertEquals(array("'a\\'b\\\\z'"), $stmt->escape(array("a'b\\z"))); }
public function destruct() { if ($this->newSession && empty($this->attributes)) { return; } $stmt = Sabel_Db::createStatement($this->connectionName); $value = str_replace("", "\\000", serialize($this->attributes)); $timeout = time() + $this->maxLifetime; $table = $stmt->quoteIdentifier($this->tableName); $idCol = $stmt->quoteIdentifier("id"); $dataCol = $stmt->quoteIdentifier("data"); $toutCol = $stmt->quoteIdentifier("timeout"); if ($this->sessionIdExists($this->sessionId)) { $query = "UPDATE {$table} SET {$dataCol} = @data@, {$toutCol} = {$timeout} " . "WHERE {$idCol} = '{$this->sessionId}'"; } else { $query = "INSERT INTO {$table} ({$idCol}, {$dataCol}, {$toutCol}) " . "VALUES ('{$this->sessionId}', @data@, {$timeout})"; } $stmt->setQuery($query)->setBindValue("data", $value)->execute(); }
private function createStatement() { $stmt = Sabel_Db::createStatement($this->connectionName); $stmt->setMetadata(Sabel_Db_Metadata::getTableInfo($this->tableName, $this->connectionName)); return $stmt; }
equals($name->default, "default name"); isTrue($name->nullable); equals($name->max, 128); isTrue($test->isString()); isFalse($test->primary); isFalse($test->increment); isNull($test->default); isTrue($test->nullable); equals($test->max, 255); isTrue($body->isText()); isFalse($body->primary); isFalse($body->increment); isNull($body->default); isFalse($body->nullable); isTrue($bool->isBool()); isFalse($bool->primary); isFalse($bool->increment); isFalse($bool->default); isTrue($bool->nullable); isNull($hoge->ft); isNull($hoge->dbl); isNull($hoge->sint); isNull($hoge->bint); Sabel_Db_Connection::closeAll(); $path = RUN_BASE . "/migration/tmp/1_Hoge_create.php"; system("php exec.php {$path} " . CONNAME . " downgrade"); $accessor = Sabel_Db::createMetadata(CONNAME); $tables = $accessor->getTableList(); isFalse(in_array("hoge", $tables)); Sabel_Db_Connection::closeAll(); echo "\n";
/** * @param const $type Sabel_Db_Statement * * @return Sabel_Db_Statement */ public function prepareStatement($type = Sabel_Db_Statement::QUERY) { $stmt = Sabel_Db::createStatement($this->connectionName); return $stmt->setMetadata($this->metadata)->type($type); }
protected function _execMigration() { $version = $this->currentVersion; $to = (int) $this->migrateTo; if ((int) $version === $to) { $this->message("NO CHANGES FROM {$to}"); exit; } $doNext = false; if ($version < $to) { $next = $version + 1; $num = $next; $mode = "upgrade"; $doNext = $next < $to; } else { $next = $version - 1; $num = $version; $mode = "downgrade"; $doNext = $next > $to; } Sabel_Db_Migration_Manager::setApplyMode($mode); $instance = Sabel_Db::createMigrator($this->connectionName); $directory = Sabel_Db_Migration_Manager::getDirectory(); $instance->execute($directory . DS . $this->files[$num]); $this->updateVersionNumber($next); return $doNext; }
<?php define("RUN_BASE", getcwd()); require_once "/usr/local/lib/php/Sabel/Sabel.php"; require_once RUN_BASE . "/config/INIT.php"; require_once RUN_BASE . "/config/environment.php"; $configs = array("sqlite" => array("package" => "sabel.db.pdo.sqlite", "database" => "/home/ebine/test.sq3"), "mysql" => array("package" => "sabel.db.mysql", "host" => "127.0.0.1", "database" => "sdb_test", "port" => "3306", "user" => "root", "password" => ""), "pgsql" => array("package" => "sabel.db.pgsql", "host" => "127.0.0.1", "database" => "sdb_test", "user" => "pgsql", "password" => "pgsql"), "oci" => array("package" => "sabel.db.oci", "host" => "127.0.0.1", "database" => "XE", "schema" => "DEVELOP", "user" => "DEVELOP", "password" => "DEVELOP")); foreach ($configs as $key => $param) { Sabel_Db_Config::add($key, $param); } $args = $_SERVER["argv"]; $path = $args[1]; $conName = $args[2]; $type = $args[3]; $schema = Sabel_Db::createMetadata($conName); $stmt = Sabel_Db::createStatement($conName); Sabel_Db_Migration_Manager::setSchema($schema); Sabel_Db_Migration_Manager::setStatement($stmt); Sabel_Db_Migration_Manager::setDirectory(RUN_BASE . "/migration/tmp"); Sabel_Db_Migration_Manager::setApplyMode($type); $dirs = explode(".", Sabel_Db_Config::getPackage($conName)); $className = implode("_", array_map("ucfirst", $dirs)) . "_Migration"; $mig = new $className(); $mig->execute($path);