public function getTables() { $tables = array(); foreach ($this->XMLContent->database->children() as $t) { $table = new Nextgen_Core_Table($t['name']); foreach ($t->children() as $tp) { $typeOfProperty = $tp->getName(); switch ($typeOfProperty) { case 'field': $tableField = new Nextgen_Core_TableField($tp['Field']); $tableField->setType($tp['Type']); $tableField->isNull($tp['Null'] == 'YES'); $tableField->setDefault($tp['Default']); $tableField->isAutoIncrement($tp['Extra'] == 'auto_increment'); $table->addField($tableField); break; case 'key': break; case 'options': $table->setEngine($tp['Engine']); $table->setCollation($tp['Collation']); break; } /* if ($tp->field) { echo "\nOK\n"; } else { echo "\nNO\n"; } */ } $tables[] = $table; } }
private function getTables() { $tables = array(); $createTableRegExp = '!^[ |\\t]*CREATE TABLE `([^`]+)`$!'; $tableFieldRegExp = '!^[ |\\t]*`([^`]+)` (.+)(,)?$!'; $primaryKeysRegExp = '!^[ |\\t]*PRIMARY KEY \\(([^)]+)\\)(,)?$!'; $databaseEngineRegExp = '!^\\)Type=([^)]+);$!'; $uniqueKeyRegExp = '!^[ |\\t]*UNIQUE KEY `([^`]+)` \\((.+)\\).*(,)?$!'; $keyRegExp = '!^[ |\\t]*KEY `([^`]+)` *\\((.+)\\).*(,)?$!'; $indexRegExp = '!^[ |\\t]*INDEX `([^`]+)` \\((.+)\\).*(,)?$!'; $constraintRegExp1 = '!^[ |\\t]*CONSTRAINT (`([^`]+)`)(,)?$!'; $constraintRegExp2 = '!^[ |\\t]*(FOREIGN KEY \\(`([^`]+)`\\))(,)?$!'; $constraintRegExp3 = '!^[ |\\t]*(REFERENCES `([^)]+)` \\(`([^`]+)`\\))(,)?$!'; $constraintRegExp4 = '!^[ |\\t]*(ON .+)(,)?$!'; $fileIterator = new Nextgen_Core_FileIterator($this->resource); $rawTableContentArray = array(); foreach ($fileIterator as $line) { if (preg_match($createTableRegExp, $line, $matches)) { // start of the table declaration $tableName = $matches[1]; $table = new Nextgen_Core_Table($tableName); $rawTableContentArray = array(); //$rawTableContentArray = array(); $this->logger->insertBreak(); $this->logger->log("Creating table --{$tableName}--"); } else { if (preg_match($tableFieldRegExp, $line, $matches)) { // new table field $fieldName = $matches[1]; $fieldContent = self::prepareLine($matches[2]); $tableField = new Nextgen_Core_TableField($fieldName); $tableField->setContent($fieldContent); $table->addField($tableField); $this->logger->log("Adding field --{$fieldName}-- (--{$fieldContent}--)"); } else { if (preg_match($primaryKeysRegExp, $line, $matches)) { $primaryKeyContent = self::prepareLine($matches[1]); $primaryKey = new Nextgen_Core_TableKey('primary_key', $primaryKeyContent); $table->setPrimaryKey($primaryKey); $this->logger->log("Adding primary key --{$primaryKeyContent}--"); } else { if (preg_match($uniqueKeyRegExp, $line, $matches)) { $uniqueKeyName = $matches[1]; $uniqueKeyValue = $matches[2]; $uniqueKey = new Nextgen_Core_TableKey($uniqueKeyName, $uniqueKeyValue); $table->addUniqueKey($uniqueKey); $this->logger->log("Adding unique key --{$uniqueKeyName}-- whose content is --{$uniqueKeyValue}--"); } else { if (preg_match($keyRegExp, $line, $matches)) { $keyName = $matches[1]; $keyValue = $matches[2]; $key = new Nextgen_Core_TableKey($keyName, $keyValue); $table->addKey($key); $this->logger->log("Adding key --{$keyName}-- whose content is --{$keyValue}--"); } else { if (preg_match($indexRegExp, $line, $matches)) { $indexName = $matches[1]; $indexValue = $matches[2]; $index = new Nextgen_Core_TableKey($indexName, $indexValue); $table->addIndex($index); $this->logger->log("Adding index --{$indexName}-- whose content is --{$indexValue}--"); } else { if (preg_match($constraintRegExp1, $line, $matches)) { $constraintName = $matches[2]; $constraintContent = $matches[1]; $fileIterator->next(); $line2 = $fileIterator->current(); preg_match($constraintRegExp2, $line2, $matches); $constraintContent .= ' ' . $matches[1]; $constraintForeignKey = $matches[2]; $line .= "\n" . $line2; $fileIterator->next(); $line3 = $fileIterator->current(); preg_match($constraintRegExp3, $line3, $matches); $constraintContent .= ' ' . $matches[1]; $constraintReferencedTable = $matches[2]; $constraintReferencedField = $matches[3]; $line .= "\n" . $line3; do { $foundExtraClause = false; $fileIterator->next(); $line4 = $fileIterator->current(); if (preg_match($constraintRegExp4, $line4, $matches)) { $constraintContent .= ' ' . $matches[1]; $line .= "\n" . $line4; $foundExtraClause = true; } else { $fileIterator->previous(); } } while ($foundExtraClause); //$constraint = new Nextgen_Core_TableConstraint($constraintName, $constraintForeignKey, $constraintContent, $constraintReferencedTable, $constraintReferencedField); $constraint = new Nextgen_Core_TableKey($constraintName, $constraintContent); $table->addConstraint($constraint); $this->logger->log("Adding constraint --{$constraintName}-- whose foreign key is --{$constraintForeignKey}--\n and referenced table is --{$constraintReferencedTable}--\n and referenced field is --{$constraintReferencedField}--\n and content is --{$constraintContent}--"); } else { if (preg_match($databaseEngineRegExp, $line, $matches)) { // end of the table declaration $engine = $matches[1]; $table->setEngine($engine); $rawTableContentArray[] = $line; $rawContent = implode("\n", $rawTableContentArray); $table->setContent($rawContent); $tables[] = $table; $this->logger->log("Setting engine --{$engine}--"); $this->logger->log("Closing table"); } } } } } } } } $rawTableContentArray[] = $line; } return $tables; }