Ejemplo n.º 1
0
 public static function getTableInfo($tblName, $connectionName = "default")
 {
     if (isset(self::$metadata[$tblName])) {
         return self::$metadata[$tblName];
     }
     $className = "Schema_" . convert_to_modelname($tblName);
     if (Sabel::using($className)) {
         $cols = array();
         $schemaClass = new $className();
         foreach ($schemaClass->get() as $colName => $info) {
             $co = new Sabel_Db_Metadata_Column();
             $co->name = $colName;
             foreach ($info as $key => $val) {
                 $co->{$key} = $val;
             }
             $cols[$colName] = $co;
         }
         $tblSchema = new Sabel_Db_Metadata_Table($tblName, $cols);
         $properties = $schemaClass->getProperty();
         $tblSchema->setTableEngine($properties["tableEngine"]);
         $tblSchema->setUniques($properties["uniques"]);
         $tblSchema->setForeignKeys($properties["fkeys"]);
     } else {
         $schemaObj = Sabel_Db::createMetadata($connectionName);
         $tblSchema = $schemaObj->getTable($tblName);
     }
     return self::$metadata[$tblName] = $tblSchema;
 }
Ejemplo n.º 2
0
 public static function build(Sabel_Db_Model $source, Sabel_Db_Join_Structure $structure, $rows)
 {
     $objects = $structure->getJoinObjects();
     $structure = $structure->getStructure();
     $tables = array();
     foreach ($structure as $joinTables) {
         $tables = array_merge($tables, $joinTables);
     }
     $results = array();
     $selfObj = MODEL($source->getName());
     foreach ($rows as $row) {
         $models = self::createModels($row, $tables, $objects);
         foreach ($tables as $tblName) {
             if (!isset($structure[$tblName])) {
                 continue;
             }
             foreach ($structure[$tblName] as $parent) {
                 $name = convert_to_modelname($parent);
                 $models[$tblName]->__set($name, $models[$parent]);
             }
         }
         $self = clone $selfObj;
         $self->setProperties($row);
         $tblName = $source->getTableName();
         foreach ($structure[$tblName] as $parent) {
             $name = convert_to_modelname($parent);
             $self->__set($name, $models[$parent]);
         }
         $results[] = $self;
     }
     return $results;
 }
Ejemplo n.º 3
0
 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);
         }
     }
 }
Ejemplo n.º 4
0
 public function createModel(&$row)
 {
     $name = $this->tblName;
     static $models = array();
     if (isset($models[$name])) {
         $model = clone $models[$name];
     } else {
         $model = MODEL(convert_to_modelname($name));
         $models[$name] = clone $model;
     }
     if ($this->hasAlias()) {
         $name = strtolower($this->aliasName);
     }
     $props = array();
     foreach ($this->columns as $column) {
         $hash = Sabel_Db_Join_ColumnHash::getHash("{$name}.{$column}");
         if (array_key_exists($hash, $row)) {
             $props[$column] = $row[$hash];
             unset($row[$hash]);
         }
     }
     $model->setProperties($props);
     return $model;
 }
Ejemplo n.º 5
0
 public function write(Sabel_Db_Metadata_Table $metadata)
 {
     $mdlName = convert_to_modelname($metadata->getTableName());
     $className = "Schema_" . $mdlName;
     $target = $this->schemaDir . DS . $mdlName . ".php";
     if (file_exists($target)) {
         unlink($target);
     }
     $lines = array();
     $lines[] = "<?php" . PHP_EOL;
     $lines[] = "class {$className}";
     $lines[] = "{";
     $lines[] = "  public static function get()";
     $lines[] = "  {";
     $lines[] = '    $cols = array();' . PHP_EOL;
     $colLines = $this->createColumnLines($metadata);
     foreach ($colLines as $line) {
         $lines[] = "    " . $line;
     }
     $lines[] = PHP_EOL;
     $lines[] = '    return $cols;';
     $lines[] = "  }" . PHP_EOL;
     $lines[] = "  public function getProperty()";
     $lines[] = "  {";
     $lines[] = '    $property = array();' . PHP_EOL;
     $this->writeEngine($lines, $metadata);
     $this->writeUniques($lines, $metadata);
     $this->writeForeignKeys($lines, $metadata);
     $lines[] = PHP_EOL;
     $lines[] = "    return \$property;";
     $lines[] = "  }";
     $lines[] = "}";
     $fp = fopen($target, "w");
     fwrite($fp, implode(PHP_EOL, $lines));
     fclose($fp);
 }
Ejemplo n.º 6
0
 protected function createSelectSql()
 {
     $tblName = $this->quoteIdentifier($this->table);
     $projection = $this->getProjection();
     $c = $this->constraints;
     $limit = null;
     $offset = null;
     if (isset($c["offset"]) && isset($c["limit"])) {
         $limit = $c["limit"];
         $offset = $c["offset"];
     } elseif (isset($c["offset"]) && !isset($c["limit"])) {
         $limit = 100;
         $offset = $c["offset"];
     } elseif (isset($c["limit"]) && !isset($c["offset"])) {
         $limit = $c["limit"];
         $offset = 0;
     }
     if ($limit !== null) {
         if (isset($c["order"])) {
             $order = $c["order"];
         } else {
             $order = convert_to_modelname($this->metadata->getTableName()) . "." . $this->metadata->getPrimaryKey() . " ASC";
         }
         $orderBy = " ORDER BY " . $this->quoteIdentifierForOrderBy($order);
         $sql = "SELECT * FROM (SELECT ROW_NUMBER() OVER({$orderBy}) AS [SBL_RN], {$projection} " . "FROM {$tblName}" . $this->join . $this->where . ") AS [SBL_TMP] " . "WHERE [SBL_RN] BETWEEN " . ($offset + 1) . " AND " . ($offset + $limit) . " " . $orderBy;
     } else {
         $sql = "SELECT {$projection} FROM {$tblName}" . $this->join . $this->where;
         if (isset($c["order"])) {
             $sql .= " ORDER BY " . $this->quoteIdentifierForOrderBy($c["order"]);
         }
     }
     return $sql;
 }
Ejemplo n.º 7
0
 public static function schemaClassExists($tblName)
 {
     $className = "Schema_" . convert_to_modelname($tblName);
     return Sabel::using($className);
 }