protected function parseTables(Database $database, $filterTable = null) { $sql = 'SHOW FULL TABLES'; if ($filterTable) { if ($schema = $filterTable->getSchema()) { $sql .= ' FROM ' . $database->getPlatform()->doQuoting($schema); } $sql .= sprintf(" LIKE '%s'", $filterTable->getCommonName()); } else { if ($schema = $database->getSchema()) { $sql .= ' FROM ' . $database->getPlatform()->doQuoting($schema); } } $dataFetcher = $this->dbh->query($sql); // First load the tables (important that this happen before filling out details of tables) $tables = array(); foreach ($dataFetcher as $row) { $name = $row[0]; $type = $row[1]; if ($name == $this->getMigrationTable() || $type !== 'BASE TABLE') { continue; } $table = new Table($name); $table->setIdMethod($database->getDefaultIdMethod()); if ($filterTable && $filterTable->getSchema()) { $table->setSchema($filterTable->getSchema()); } $database->addTable($table); $tables[] = $table; } }
/** * */ public function parse(Database $database) { $this->addVendorInfo = $this->getGeneratorConfig()->getBuildProperty('addVendorInfo'); $sql = 'SHOW FULL TABLES'; if ($schema = $database->getSchema()) { $sql .= ' FROM ' . $database->getPlatform()->quoteIdentifier($schema); } $dataFetcher = $this->dbh->query($sql); // First load the tables (important that this happen before filling out details of tables) $tables = array(); foreach ($dataFetcher as $row) { $name = $row[0]; $type = $row[1]; if ($name == $this->getMigrationTable() || $type !== 'BASE TABLE') { continue; } $table = new Table($name); $table->setIdMethod($database->getDefaultIdMethod()); $database->addTable($table); $tables[] = $table; } // Now populate only columns. foreach ($tables as $table) { $this->addColumns($table); } // Now add indices and constraints. foreach ($tables as $table) { $this->addForeignKeys($table); $this->addIndexes($table); $this->addPrimaryKey($table); $this->addTableVendorInfo($table); } return count($tables); }
/** * */ public function parse(Database $database) { $dataFetcher = $this->dbh->query("\n SELECT name\n FROM sqlite_master\n WHERE type='table'\n UNION ALL\n SELECT name\n FROM sqlite_temp_master\n WHERE type='table'\n ORDER BY name;"); // First load the tables (important that this happen before filling out details of tables) $tables = array(); foreach ($dataFetcher as $row) { $name = $row[0]; $commonName = ''; if ('sqlite_' == substr($name, 0, 7)) { continue; } if ($database->getSchema()) { if (false !== ($pos = strpos($name, '§'))) { if ($database->getSchema()) { if ($database->getSchema() !== substr($name, 0, $pos)) { continue; } else { $commonName = substr($name, $pos + 2); //2 because the delimiter § uses in UTF8 one byte more. } } } else { continue; } } if ($name === $this->getMigrationTable()) { continue; } $table = new Table($commonName ?: $name); $table->setIdMethod($database->getDefaultIdMethod()); $database->addTable($table); $tables[] = $table; } // Now populate only columns. foreach ($tables as $table) { $this->addColumns($table); } // Now add indexes and constraints. foreach ($tables as $table) { $this->addIndexes($table); $this->addForeignKeys($table); } return count($tables); }
/** * @return Database */ public function readConnectedDatabase() { $this->getDatabase(); $database = new Database(); $database->setSchema($this->database->getSchema()); $database->setName($this->database->getName()); $database->setPlatform($this->getPlatform()); $this->getParser()->parse($database); return $database; }
protected function parseTables(Database $database, Table $filterTable = null) { $sql = "\n SELECT name\n FROM sqlite_master\n WHERE type='table'\n %filter%\n UNION ALL\n SELECT name\n FROM sqlite_temp_master\n WHERE type='table'\n %filter%\n ORDER BY name;"; $filter = ''; if ($filterTable) { if ($schema = $filterTable->getSchema()) { $filter = sprintf(" AND name LIKE '%s§%%'", $schema); } $filter .= sprintf(" AND (name = '%s' OR name LIKE '%%§%1\$s')", $filterTable->getCommonName()); } else { if ($schema = $database->getSchema()) { $filter = sprintf(" AND name LIKE '%s§%%'", $schema); } } $sql = str_replace('%filter%', $filter, $sql); $dataFetcher = $this->dbh->query($sql); // First load the tables (important that this happen before filling out details of tables) foreach ($dataFetcher as $row) { $tableName = $row[0]; $tableSchema = ''; if ('sqlite_' == substr($tableName, 0, 7)) { continue; } if (false !== ($pos = strpos($tableName, '§'))) { $tableSchema = substr($tableName, 0, $pos); $tableName = substr($tableName, $pos + 2); } $table = new Table($tableName); if ($filterTable && $filterTable->getSchema()) { $table->setSchema($filterTable->getSchema()); } else { if (!$database->getSchema() && $tableSchema) { //we have no schema to filter, but this belongs to one, so next continue; } } if ($tableName === $this->getMigrationTable()) { continue; } $table->setIdMethod($database->getDefaultIdMethod()); $database->addTable($table); } }
/** * Returns whether or not this table is linked to a schema. * * @return boolean */ private function hasSchema() { return $this->database && ($this->schema ?: $this->database->getSchema()) && ($platform = $this->getPlatform()) && $platform->supportsSchemas(); }
/** * Adds the sequences for this database. * * @param Database $database */ protected function addSequences(Database $database) { $searchPath = '?'; $params = [$database->getSchema()]; if (!$database->getSchema()) { $stmt = $this->dbh->query('SHOW search_path'); $searchPathString = $stmt->fetchColumn(); $params = []; $searchPath = explode(',', $searchPathString); foreach ($searchPath as &$path) { $params[] = $path; $path = '?'; } $searchPath = implode(', ', $searchPath); } $stmt = $this->dbh->prepare("\n SELECT c.relname, n.nspname\n FROM pg_class c, pg_namespace n\n WHERE\n n.oid = c.relnamespace\n AND c.relkind = 'S'\n AND n.nspname IN ({$searchPath});\n "); $stmt->execute($params); while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $name = $row['nspname'] . '.' . $row['relname']; $database->addSequence($name); } }
/** * Appends the generated <database> XML node to its parent node. * * @param Database $database The Database model instance * @param \DOMNode $parentNode The parent DOMNode object */ private function appendDatabaseNode(Database $database, \DOMNode $parentNode) { $databaseNode = $parentNode->appendChild($this->document->createElement('database')); $databaseNode->setAttribute('name', $database->getName()); $databaseNode->setAttribute('defaultIdMethod', $database->getDefaultIdMethod()); if ($package = $database->getPackage()) { $databaseNode->setAttribute('package', $package); } if ($schema = $database->getSchema()) { $databaseNode->setAttribute('schema', $schema); } if ($namespace = $database->getNamespace()) { $databaseNode->setAttribute('namespace', $namespace); } if ($baseClass = $database->getBaseClass()) { $databaseNode->setAttribute('baseClass', $baseClass); } if ($baseQueryClass = $database->getBaseQueryClass()) { $databaseNode->setAttribute('baseQueryClass', $baseQueryClass); } if ($defaultNamingMethod = $database->getDefaultPhpNamingMethod()) { $databaseNode->setAttribute('defaultPhpNamingMethod', $defaultNamingMethod); } $defaultAccessorVisibility = $database->getDefaultAccessorVisibility(); if ($defaultAccessorVisibility !== Database::VISIBILITY_PUBLIC) { $databaseNode->setAttribute('defaultAccessorVisibility', $defaultAccessorVisibility); } $defaultMutatorVisibility = $database->getDefaultMutatorVisibility(); if ($defaultMutatorVisibility !== Database::VISIBILITY_PUBLIC) { $databaseNode->setAttribute('defaultMutatorVisibility', $defaultMutatorVisibility); } $defaultStringFormat = $database->getDefaultStringFormat(); if (Database::DEFAULT_STRING_FORMAT !== $defaultStringFormat) { $databaseNode->setAttribute('defaultStringFormat', $defaultStringFormat); } if ($database->isHeavyIndexing()) { $databaseNode->setAttribute('heavyIndexing', 'true'); } if ($tablePrefix = $database->getTablePrefix()) { $databaseNode->setAttribute('tablePrefix', $tablePrefix); } if ($database->isIdentifierQuotingEnabled()) { $databaseNode->setAttribute('identifierQuoting', 'true'); } /* FIXME - Before we can add support for domains in the schema, we need to have a method of the Column that indicates whether the column was mapped to a SPECIFIC domain (since Column->getDomain() will always return a Domain object) foreach ($this->domainMap as $domain) { $this->appendDomainNode($databaseNode); } */ foreach ($database->getVendorInformation() as $vendorInformation) { $this->appendVendorInformationNode($vendorInformation, $databaseNode); } foreach ($database->getTables() as $table) { $this->appendTableNode($table, $databaseNode); } }
public function startElement($parser, $name, $attributes) { $parentTag = $this->peekCurrentSchemaTag(); if (false === $parentTag) { switch ($name) { case 'database': if ($this->isExternalSchema()) { $this->currentPackage = isset($attributes['package']) ? $attributes['package'] : null; if (null === $this->currentPackage) { $this->currentPackage = $this->defaultPackage; } } else { $this->currDB = $this->schema->addDatabase($attributes); } break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ('database' === $parentTag) { switch ($name) { case 'external-schema': $xmlFile = isset($attributes['filename']) ? $attributes['filename'] : null; // 'referenceOnly' attribute is valid in the main schema XML file only, // and it's ignored in the nested external-schemas if (!$this->isExternalSchema()) { $isForRefOnly = isset($attributes['referenceOnly']) ? $attributes['referenceOnly'] : null; $this->isForReferenceOnly = null !== $isForRefOnly ? 'true' === strtolower($isForRefOnly) : true; // defaults to TRUE } if ('/' !== $xmlFile[0]) { $xmlFile = realpath(dirname($this->currentXmlFile) . DIRECTORY_SEPARATOR . $xmlFile); if (!file_exists($xmlFile)) { throw new SchemaException(sprintf('Unknown include external "%s"', $xmlFile)); } } $this->parseFile($xmlFile); break; case 'domain': $this->currDB->addDomain($attributes); break; case 'table': if (!isset($attributes['schema']) && $this->currDB->getSchema() && $this->currDB->getPlatform()->supportsSchemas() && false === strpos($attributes['name'], $this->currDB->getPlatform()->getSchemaDelimiter())) { $attributes['schema'] = $this->currDB->getSchema(); } $this->currTable = $this->currDB->addTable($attributes); if ($this->isExternalSchema()) { $this->currTable->setForReferenceOnly($this->isForReferenceOnly); $this->currTable->setPackage($this->currentPackage); } break; case 'vendor': $this->currVendorObject = $this->currDB->addVendorInfo($attributes); break; case 'behavior': $this->currBehavior = $this->currDB->addBehavior($attributes); break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ('table' === $parentTag) { switch ($name) { case 'column': $this->currColumn = $this->currTable->addColumn($attributes); break; case 'foreign-key': $this->currFK = $this->currTable->addForeignKey($attributes); break; case 'index': $this->currIndex = new Index(); $this->currIndex->setTable($this->currTable); $this->currIndex->loadMapping($attributes); break; case 'unique': $this->currUnique = new Unique(); $this->currUnique->setTable($this->currTable); $this->currUnique->loadMapping($attributes); break; case 'vendor': $this->currVendorObject = $this->currTable->addVendorInfo($attributes); break; case 'id-method-parameter': $this->currTable->addIdMethodParameter($attributes); break; case 'behavior': $this->currBehavior = $this->currTable->addBehavior($attributes); break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ('column' === $parentTag) { switch ($name) { case 'inheritance': $this->currColumn->addInheritance($attributes); break; case 'vendor': $this->currVendorObject = $this->currColumn->addVendorInfo($attributes); break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ('foreign-key' === $parentTag) { switch ($name) { case 'reference': $this->currFK->addReference($attributes); break; case 'vendor': $this->currVendorObject = $this->currUnique->addVendorInfo($attributes); break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ('index' === $parentTag) { switch ($name) { case 'index-column': $this->currIndex->addColumn($attributes); break; case 'vendor': $this->currVendorObject = $this->currIndex->addVendorInfo($attributes); break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ('unique' === $parentTag) { switch ($name) { case 'unique-column': $this->currUnique->addColumn($attributes); break; case 'vendor': $this->currVendorObject = $this->currUnique->addVendorInfo($attributes); break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ($parentTag == 'behavior') { switch ($name) { case 'parameter': $this->currBehavior->addParameter($attributes); break; default: $this->_throwInvalidTagException($parser, $name); } } elseif ('vendor' === $parentTag) { switch ($name) { case 'parameter': $this->currVendorObject->setParameter($attributes['name'], $attributes['value']); break; default: $this->_throwInvalidTagException($parser, $name); } } else { // it must be an invalid tag $this->_throwInvalidTagException($parser, $name); } $this->pushCurrentSchemaTag($name); }