Exemplo n.º 1
0
 /**
  * @covers Model\Cluster\Schema\Table::addIndex
  */
 public function testAddIndex()
 {
     $primaryIndex = new PrimaryIndex('PRIMARY', array(new Column(array(), $this->_table)));
     $this->_table->addIndex($primaryIndex);
     $primaryIndex = $this->_table->getIndex('PRIMARY');
     $this->assertInstanceOf('Model\\Cluster\\Schema\\Table\\Index\\AbstractIndex', $primaryIndex);
 }
Exemplo n.º 2
0
 /**
  */
 public function testToArray()
 {
     $column = $this->_table->getColumn('id');
     $columnArray = $column->toArray();
     $this->assertInternalType('array', $columnArray);
     $arrayFields = array('table_catalog', 'table_schema', 'table_name', 'column_name', 'ordinal_position', 'column_default', 'is_nullable', 'data_type', 'character_maximum_length', 'character_octet_length', 'numeric_precision', 'numeric_scale', 'character_set_name', 'collation_name', 'column_type', 'column_comment', 'is_unique');
     foreach ($arrayFields as $field) {
         $this->assertArrayHasKey($field, $columnArray);
     }
 }
Exemplo n.º 3
0
 public function testToArray()
 {
     $idKey = new \Model\Cluster\Schema\Table\Index\Key('id', array($this->_table->getColumn('id')));
     $array = $idKey->toArray(false);
     $this->assertArrayNotHasKey('columns', $array);
     $this->assertArrayHasKey('type', $array);
     $this->assertEquals('Model\\Cluster\\Schema\\Table\\Index\\Key', $array['type']);
     $this->assertArrayHasKey('name', $array);
     $this->assertEquals('id', $array['name']);
     $array = $idKey->toArray();
     $this->assertArrayHasKey('columns', $array);
     $this->assertEquals('id', $array['columns'][0]['column_name']);
 }
Exemplo n.º 4
0
 public function __construct(Table $table, Schema $cluster, $outputFilename = null)
 {
     Log::info('Generate part entity ' . $table->getName());
     $this->_table = $table;
     $file = new \Model\Code\Generator\FileGenerator();
     $class = new \Zend\Code\Generator\ClassGenerator();
     $file->setClass($class);
     $this->setFile($file);
     $this->_runPlugins(self::PART_ENTITY_ABSTRACT, self::RUNTIME_PRE);
     $class->setName($table->getNameAsCamelCase() . 'EntityAbstract');
     $class->setExtendedClass('\\Model\\Entity');
     $this->_runPlugins(self::PART_ENTITY_ABSTRACT, self::RUNTIME_POST);
     if ($outputFilename) {
         file_put_contents($outputFilename, $file->generate());
     }
 }
Exemplo n.º 5
0
 public function __construct(Table $table, Cluster $cluster, $outputFilename = null)
 {
     Log::info('Generate part front collection ' . $table->getName());
     $this->_table = $table;
     $file = new \Model\Code\Generator\FileGenerator();
     $this->setFile($file);
     $class = new \Zend\Code\Generator\ClassGenerator();
     $file->setClass($class);
     $this->_runPlugins(self::PART_FRONT_COLLECTION, self::RUNTIME_PRE);
     $class->setNamespaceName('Model\\Collection');
     $class->setName($table->getNameAsCamelCase() . 'Collection');
     $class->setExtendedClass('Abstract' . $table->getNameAsCamelCase() . 'Collection');
     $this->_runPlugins(self::PART_FRONT_COLLECTION, self::RUNTIME_POST);
     if ($outputFilename) {
         file_put_contents($outputFilename, $file->generate());
     }
 }
Exemplo n.º 6
0
 public function __construct(Table $table, Cluster $cluster, $outputFilename = null, array $options = array())
 {
     if (!empty($options)) {
         $this->setOptions($options);
     }
     Log::info('Generate part mode ' . $table->getName());
     $this->_table = $table;
     $file = new FileGenerator();
     $this->setFile($file);
     $file->setNamespace('Model');
     $class = new ClassGenerator();
     $file->setClass($class);
     $file->addUse('Model\\Result\\Result');
     $file->addUse('Model\\Entity\\' . $table->getNameAsCamelCase() . 'Entity');
     $file->addUse('Model\\Cond\\' . $table->getNameAsCamelCase() . 'Cond', 'Cond');
     $file->addUse('Model\\Cond\\AbstractCond');
     $file->addUse('Model\\Collection\\' . $table->getNameAsCamelCase() . 'Collection');
     $this->_runPlugins(self::PART_MODEL, self::RUNTIME_PRE);
     $class->setName('Abstract' . $table->getNameAsCamelCase() . 'Model');
     if ($table->isTree() && $this->hasPlugin('Tree', AbstractPart::PART_MODEL)) {
         $class->setExtendedClass('\\Model\\Mysql\\TreeModel');
     } else {
         $class->setExtendedClass('\\Model\\Mysql\\AbstractModel');
     }
     $class->setAbstract(true);
     $this->_runPlugins(self::PART_MODEL, self::RUNTIME_POST);
     if ($outputFilename) {
         file_put_contents($outputFilename, $file->generate());
     }
 }
Exemplo n.º 7
0
 public function __construct(Table $table, Cluster $cluster, $outputFilename = null)
 {
     Log::debug('Generate part list ' . $table->getName());
     $this->_table = $table;
     $file = new FileGenerator();
     $this->setFile($file);
     $class = new ClassGenerator();
     $file->setClass($class);
     $file->setNamespace('Model\\Collection');
     //$file->setUse('Model\ResultList');
     $this->_runPlugins(self::PART_COLLECTION, self::RUNTIME_PRE);
     $class->setName('Abstract' . $table->getNameAsCamelCase() . 'Collection');
     $class->setExtendedClass('AbstractCollection');
     $class->setAbstract(true);
     $this->_runPlugins(self::PART_COLLECTION, self::RUNTIME_POST);
     if ($outputFilename) {
         file_put_contents($outputFilename, $file->generate());
     }
 }
Exemplo n.º 8
0
 /**
  * @param Table|string $table
  * @return array
  */
 public function getForeignKeyArray($table)
 {
     $schemaName = $this->getName();
     $tableName = $table instanceof Table ? $table->getName() : (string) $table;
     $cacheId = 'generator_schema_getForeignKeyArray_' . $tableName;
     $cacheAdapter = $this->getCacheAdapter();
     $result = $cacheAdapter ? $cacheAdapter->getItem($cacheId) : null;
     if (!$cacheAdapter || is_null($result)) {
         $sql = "SELECT k.CONSTRAINT_NAME as key_name,\n                           k.TABLE_SCHEMA as table_schema,\n                           k.TABLE_NAME as table_name,\n                           k.COLUMN_NAME as column_name,\n                           k.REFERENCED_TABLE_SCHEMA as referenced_table_schema,\n                           k.REFERENCED_TABLE_NAME as referenced_table_name,\n                           k.REFERENCED_COLUMN_NAME as referenced_column_name,\n                           r.UPDATE_RULE as update_rule,\n                           r.DELETE_RULE as delete_rule\n                    FROM `information_schema`.`KEY_COLUMN_USAGE` as k\n                    JOIN `information_schema`.TABLE_CONSTRAINTS as c ON\n                        k.`CONSTRAINT_SCHEMA` = c.`CONSTRAINT_SCHEMA`\n                        AND k.`TABLE_NAME` = c.`TABLE_NAME`\n                        AND k.`CONSTRAINT_NAME` = c.`CONSTRAINT_NAME`\n                    JOIN `information_schema`.`REFERENTIAL_CONSTRAINTS` as r\n                        ON (k.`CONSTRAINT_SCHEMA` = r.`CONSTRAINT_SCHEMA`\n                            AND k.`TABLE_NAME` = r.`TABLE_NAME`\n                            AND k.`CONSTRAINT_NAME` = r.`CONSTRAINT_NAME`)\n                    WHERE ((k.`CONSTRAINT_SCHEMA` = '{$schemaName}'\n                        AND k.`TABLE_NAME` = '{$tableName}')\n                        OR  (k.`REFERENCED_TABLE_SCHEMA` = '{$schemaName}'\n                            AND k.`REFERENCED_TABLE_NAME` = '{$tableName}'))\n                        AND c.CONSTRAINT_TYPE = 'FOREIGN KEY'\n                    ORDER BY k.CONSTRAINT_NAME";
         $result = $this->getDb()->fetchAll($sql);
         if ($cacheAdapter) {
             $cacheAdapter->setItem($cacheId, $result);
         }
     }
     return $result;
 }
Exemplo n.º 9
0
 /**
  * Создать экземпляр из XMl
  *
  * @param                             $xml
  * @param \Model\Cluster\Schema\Table $table
  * @throws \Model\Exception\ErrorException
  * @return AbstractLink
  */
 public static function fromXml($xml, Table $table)
 {
     if (is_array($xml)) {
         $data = $xml;
     } else {
         $xml = simplexml_load_string($xml);
         $data = json_decode(json_encode((array) $xml), 1);
     }
     $data = Column::prepareXmlArray($data);
     switch (strtolower($data['type'])) {
         case 'onetoone':
             $type = self::LINK_TYPE_ONE_TO_ONE;
             break;
         case 'onetomany':
             $type = self::LINK_TYPE_ONE_TO_MANY;
             break;
         case 'manytoone':
             $type = self::LINK_TYPE_MANY_TO_ONE;
             break;
         case 'manytomany':
             $type = self::LINK_TYPE_MANY_TO_MANY;
             break;
     }
     $type = '\\' . $type;
     $schema = $table->getSchema();
     $localColumn = $schema->getTable($data['local_table'])->getColumn($data['local_column']);
     $foreignColumn = $schema->getTable($data['foreign_table'])->getColumn($data['foreign_column']);
     if (!$localColumn) {
         throw new ErrorException('Local column not found');
     }
     if (!$foreignColumn) {
         throw new ErrorException('Foreign column not found');
     }
     if (isset($data['rule_update'])) {
         if (empty($data['rule_update'])) {
             $data['rule_update'] = null;
         }
     }
     if (isset($data['rule_delete'])) {
         if (empty($data['rule_delete'])) {
             $data['rule_delete'] = null;
         }
     }
     if (isset($data['link_table'])) {
         $linkTableLocalColumn = $schema->getTable($data['link_table'])->getColumn($data['link_table_local_column']);
         $linkTableForeignColumn = $schema->getTable($data['link_table'])->getColumn($data['link_table_foreign_column']);
         if (!$linkTableLocalColumn) {
             throw new ErrorException('Link table Local column not found');
         }
         if (!$linkTableForeignColumn) {
             throw new ErrorException('Link table Foreign column not found');
         }
         $link = new $type($localColumn, $foreignColumn, $data['rule_delete'], $data['rule_update'], $linkTableLocalColumn, $linkTableForeignColumn);
     } else {
         $link = new $type($localColumn, $foreignColumn, $data['rule_delete'], $data['rule_update']);
     }
     return $link;
 }
Exemplo n.º 10
0
 /**
  * @param                             $xml
  * @param \Model\Cluster\Schema\Table $table
  * @return mixed
  * @throws \Model\Exception\ErrorException
  */
 public static function fromXml($xml, Table $table)
 {
     if (is_array($xml)) {
         $data = $xml;
     } else {
         $xml = simplexml_load_string($xml);
         $data = json_decode(json_encode((array) $xml), 1);
     }
     $data = Column::prepareXmlArray($data);
     $columnArray = array_map('trim', explode(',', $data['@attributes']['columns']));
     $columns = array();
     foreach ($columnArray as $column) {
         $col = $table->getColumn($column);
         if (!$col) {
             throw new \Model\Exception\ErrorException('Column ' . $column . ' not found');
         }
         $columns[] = $col;
     }
     switch (strtolower($data['@attributes']['type'])) {
         case 'primary':
             $type = self::TYPE_PRIMARY;
             break;
         case 'unique':
             $type = self::TYPE_UNIQUE;
             break;
         case 'key':
             $type = self::TYPE_KEY;
             break;
     }
     return new $type($data['@attributes']['name'], $columns);
 }
Exemplo n.º 11
0
 /**
  * @param Table $table
  * @param       $part
  * @param null  $aliasName
  */
 public function buildPart(Table $table, $part, $aliasName = null)
 {
     if ($part == 'front_cond') {
         $aliasList = $table->getAliasLinkList();
         if (!empty($aliasList) && !$aliasName) {
             foreach ($aliasList as $_aliasName => $tableName) {
                 $this->buildPart($table, $part, $_aliasName);
             }
         }
     }
     $partConst = constant('\\Model\\Generator\\Part\\AbstractPart::PART_' . strtoupper($part));
     $partAsCameCase = implode('', array_map('ucfirst', explode('_', $part)));
     if ($aliasName) {
         $aliasNameAsCamelCase = implode('', array_map('ucfirst', explode('_', $aliasName)));
     } else {
         $aliasNameAsCamelCase = $table->getNameAsCamelCase();
     }
     $partClass = '\\Model\\Generator\\Part\\' . $partAsCameCase;
     $outputFile = $this->outDirArray[$partConst];
     $outputFile = rtrim($outputFile, '/') . '/';
     if (preg_match('#abstract/?$#', $outputFile)) {
         $outputFile .= 'Abstract';
     }
     $outputFile .= str_replace('Front', '', $aliasNameAsCamelCase . $partAsCameCase) . '.php';
     $options = $aliasName ? array('alias' => $aliasName, 'config' => $this->getGeneratorConfig()) : array('config' => $this->getGeneratorConfig());
     new $partClass($table, $this->getCluster(), $outputFile, $options);
 }