Пример #1
0
 /**
  * Get XML tree
  *
  * @return  xml.Tree
  */
 public function getTree()
 {
     $indexes = [];
     // Attributes
     with($t = $this->doc->root()->nodeAt(0));
     if ($attr = $this->table->getFirstAttribute()) {
         do {
             $a = $t->addChild(new \xml\Node('attribute', null, ['name' => trim($attr->getName()), 'type' => $attr->getTypeString(), 'identity' => $attr->isIdentity() ? 'true' : 'false', 'typename' => $attr->typeName(), 'nullable' => $attr->isNullable() ? 'true' : 'false']));
             // Only add length attribute if length is set - "bool" does not
             // have a length, whereas varchar(255) does.
             $attr->getLength() && $a->setAttribute('length', $attr->getLength());
         } while ($attr = $this->table->getNextAttribute());
     }
     // Indexes
     if ($index = $this->table->getFirstIndex()) {
         do {
             $n = $t->addChild(new \xml\Node('index', null, ['name' => trim($index->getName()), 'unique' => $index->isUnique() ? 'true' : 'false', 'primary' => $index->isPrimaryKey() ? 'true' : 'false']));
             foreach ($index->getKeys() as $key) {
                 $n->addChild(new \xml\Node('key', $key));
             }
             if (isset($indexes[implode('|', $index->getKeys())]) && $this->cat) {
                 $this->cat->warn('(' . implode('|', $index->getKeys()) . ')', 'has been indexed twice');
             }
             $indexes[implode('|', $index->getKeys())] = true;
         } while ($index = $this->table->getNextIndex());
     }
     // constraints
     $constKeyList = [];
     if ($constraint = $this->table->getFirstForeignKeyConstraint()) {
         do {
             if (isset($constKeyList[$this->constraintKey($constraint)])) {
                 $this->cat && $this->cat->warn($this->table->name, 'has a double constraint' . "\n" . \xp::stringOf($constraint));
                 continue;
             }
             $constKeyList[$this->constraintKey($constraint)] = true;
             $cn = $t->addChild(new \xml\Node('constraint', null, ['name' => trim($constraint->getName())]));
             $fgn = $cn->addChild(new \xml\Node('reference', null, ['table' => $constraint->getSource(), 'role' => DBXMLNamingContext::foreignKeyConstraintName($this->table, $constraint)]));
             foreach ($constraint->getKeys() as $attribute => $sourceattribute) {
                 $fgn->addChild(new \xml\Node('key', null, ['attribute' => $attribute, 'sourceattribute' => $sourceattribute]));
             }
         } while ($constraint = $this->table->getNextForeignKeyConstraint());
     }
     return $this->doc;
 }