/** * */ public function parse(Database $database) { $dataFetcher = $this->dbh->query("SELECT name FROM sqlite_master WHERE type='table' UNION ALL SELECT name FROM sqlite_temp_master WHERE type='table' 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]; if ($name === $this->getMigrationTable()) { 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 indexes and constraints. foreach ($tables as $table) { $this->addIndexes($table); } return count($tables); }
public function parse(Database $database, Task $task = null) { $dataFetcher = $this->dbh->query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME <> 'dtproperties'"); // First load the tables (important that this happen before filling out details of tables) $tables = array(); foreach ($dataFetcher as $row) { $name = $this->cleanDelimitedIdentifiers($row[0]); if ($name === $this->getMigrationTable()) { 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 indexes and constraints. foreach ($tables as $table) { $this->addForeignKeys($table); $this->addIndexes($table); $this->addPrimaryKey($table); } return count($tables); }
public function setupObject() { parent::setupObject(); $this->commonName = $this->originCommonName = $this->getAttribute('name'); // retrieves the method for converting from specified name to a PHP name. $this->phpNamingMethod = $this->getAttribute('phpNamingMethod', $this->database->getDefaultPhpNamingMethod()); $this->phpName = $this->getAttribute('phpName', $this->buildPhpName($this->getStdSeparatedName())); if ($this->database->getTablePrefix()) { $this->commonName = $this->database->getTablePrefix() . $this->commonName; } $this->idMethod = $this->getAttribute('idMethod', $this->database->getDefaultIdMethod()); $this->allowPkInsert = $this->booleanValue($this->getAttribute('allowPkInsert')); $this->skipSql = $this->booleanValue($this->getAttribute('skipSql')); $this->readOnly = $this->booleanValue($this->getAttribute('readOnly')); $this->isAbstract = $this->booleanValue($this->getAttribute('abstract')); $this->baseClass = $this->getAttribute('baseClass'); $this->alias = $this->getAttribute('alias'); $this->heavyIndexing = $this->booleanValue($this->getAttribute('heavyIndexing')) || 'false' !== $this->getAttribute('heavyIndexing') && $this->database->isHeavyIndexing(); if ($this->getAttribute('identifierQuoting')) { $this->identifierQuoting = $this->booleanValue($this->getAttribute('identifierQuoting')); } $this->description = $this->getAttribute('description'); $this->interface = $this->getAttribute('interface'); // sic ('interface' is reserved word) $this->reloadOnInsert = $this->booleanValue($this->getAttribute('reloadOnInsert')); $this->reloadOnUpdate = $this->booleanValue($this->getAttribute('reloadOnUpdate')); $this->isCrossRef = $this->booleanValue($this->getAttribute('isCrossRef', false)); $this->defaultStringFormat = $this->getAttribute('defaultStringFormat'); $this->defaultAccessorVisibility = $this->getAttribute('defaultAccessorVisibility', $this->database->getAttribute('defaultAccessorVisibility', static::VISIBILITY_PUBLIC)); $this->defaultMutatorVisibility = $this->getAttribute('defaultMutatorVisibility', $this->database->getAttribute('defaultMutatorVisibility', static::VISIBILITY_PUBLIC)); }
/** * */ 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); }
/** * Parses a database schema. * * @param Database $database * @return integer */ public function parse(Database $database) { $stmt = null; $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("SELECT c.oid,\n c.relname, n.nspname\n FROM pg_class c join pg_namespace n on (c.relnamespace=n.oid)\n WHERE c.relkind = 'r'\n AND n.nspname NOT IN ('information_schema','pg_catalog')\n AND n.nspname NOT LIKE 'pg_temp%'\n AND n.nspname NOT LIKE 'pg_toast%'\n AND n.nspname IN ({$searchPath})\n ORDER BY relname"); $stmt->execute($params); $tableWraps = array(); // First load the tables (important that this happen before filling out details of tables) while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $name = $row['relname']; $namespaceName = $row['nspname']; if ($name == $this->getMigrationTable()) { continue; } $oid = $row['oid']; $table = new Table($name); if ('public' !== $namespaceName) { $table->setSchema($namespaceName); } $table->setIdMethod($database->getDefaultIdMethod()); $database->addTable($table); // Create a wrapper to hold these tables and their associated OID $wrap = new \stdClass(); $wrap->table = $table; $wrap->oid = $oid; $tableWraps[] = $wrap; } // Now populate only columns. foreach ($tableWraps as $wrap) { $this->addColumns($wrap->table, $wrap->oid); } // Now add indexes and constraints. foreach ($tableWraps as $wrap) { $this->addForeignKeys($wrap->table, $wrap->oid); $this->addIndexes($wrap->table, $wrap->oid); $this->addPrimaryKey($wrap->table, $wrap->oid); } $this->addSequences($database); return count($tableWraps); }
/** * */ public function parse(Database $database, Task $task = null) { $this->addVendorInfo = $this->getGeneratorConfig()->getBuildProperty('addVendorInfo'); $stmt = $this->dbh->query("SHOW FULL TABLES"); // First load the tables (important that this happen before filling out details of tables) $tables = array(); if ($task) { $task->log("Reverse Engineering Tables", Project::MSG_VERBOSE); } while ($row = $stmt->fetch(PDO::FETCH_NUM)) { $name = $row[0]; $type = $row[1]; if ($name == $this->getMigrationTable() || $type != "BASE TABLE") { continue; } if ($task) { $task->log(" Adding table '" . $name . "'", Project::MSG_VERBOSE); } $table = new Table($name); $table->setIdMethod($database->getDefaultIdMethod()); $database->addTable($table); $tables[] = $table; } // Now populate only columns. if ($task) { $task->log("Reverse Engineering Columns", Project::MSG_VERBOSE); } foreach ($tables as $table) { if ($task) { $task->log(" Adding columns for table '" . $table->getName() . "'", Project::MSG_VERBOSE); } $this->addColumns($table); } // Now add indices and constraints. if ($task) { $task->log("Reverse Engineering Indices And Constraints", Project::MSG_VERBOSE); } foreach ($tables as $table) { if ($task) { $task->log(" Adding indices and constraints for table '" . $table->getName() . "'", Project::MSG_VERBOSE); } $this->addForeignKeys($table); $this->addIndexes($table); $this->addPrimaryKey($table); if ($this->addVendorInfo) { $this->addTableVendorInfo($table); } } return count($tables); }
/** * */ public function parse(Database $database) { $stmt = $this->dbh->query('SELECT version() as ver'); $nativeVersion = $stmt->fetchColumn(); if (!$nativeVersion) { throw new EngineException('Failed to get database version'); } $arrVersion = sscanf($nativeVersion, '%*s %d.%d'); $version = sprintf('%d.%d', $arrVersion[0], $arrVersion[1]); // Clean up $stmt = null; $stmt = $this->dbh->query("SELECT c.oid,\n c.relname, n.nspname\n FROM pg_class c join pg_namespace n on (c.relnamespace=n.oid)\n WHERE c.relkind = 'r'\n AND n.nspname NOT IN ('information_schema','pg_catalog')\n AND n.nspname NOT LIKE 'pg_temp%'\n AND n.nspname NOT LIKE 'pg_toast%'\n ORDER BY relname"); $tableWraps = array(); // First load the tables (important that this happen before filling out details of tables) while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $name = $row['relname']; $namespacename = $row['nspname']; if ($name == $this->getMigrationTable()) { continue; } $oid = $row['oid']; $table = new Table($name); if ($namespacename != 'public') { $table->setSchema($namespacename); } $table->setIdMethod($database->getDefaultIdMethod()); $database->addTable($table); // Create a wrapper to hold these tables and their associated OID $wrap = new \stdClass(); $wrap->table = $table; $wrap->oid = $oid; $tableWraps[] = $wrap; } // Now populate only columns. foreach ($tableWraps as $wrap) { $this->addColumns($wrap->table, $wrap->oid, $version); } // Now add indexes and constraints. foreach ($tableWraps as $wrap) { $this->addForeignKeys($wrap->table, $wrap->oid, $version); $this->addIndexes($wrap->table, $wrap->oid, $version); $this->addPrimaryKey($wrap->table, $wrap->oid, $version); } // @TODO - Handle Sequences ... return count($tableWraps); }
/** * */ 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); }
/** * Searches for tables in the database. Maybe we want to search also the views. * @param Database $database The Database model class to add tables to. * @param Table[] $additionalTables */ public function parse(Database $database, array $additionalTables = array()) { $tables = array(); $stmt = $this->dbh->query("SELECT OBJECT_NAME FROM USER_OBJECTS WHERE OBJECT_TYPE = 'TABLE'"); $seqPattern = $this->getGeneratorConfig()->get()['database']['adapters']['oracleAutoincrementSequencePattern']; // First load the tables (important that this happen before filling out details of tables) while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { if (false !== strpos($row['OBJECT_NAME'], '$')) { // this is an Oracle internal table or materialized view - prune continue; } if (strtoupper($row['OBJECT_NAME']) === strtoupper($this->getMigrationTable())) { continue; } $table = new Table($row['OBJECT_NAME']); $table->setIdMethod($database->getDefaultIdMethod()); $database->addTable($table); // Add columns, primary keys and indexes. $this->addColumns($table); $this->addPrimaryKey($table); $this->addIndexes($table); $pkColumns = $table->getPrimaryKey(); if (1 === count($pkColumns) && $seqPattern) { $seqName = str_replace('${table}', $table->getName(), $seqPattern); $seqName = strtoupper($seqName); $stmt2 = $this->dbh->query("SELECT * FROM USER_SEQUENCES WHERE SEQUENCE_NAME = '" . $seqName . "'"); $hasSeq = $stmt2->fetch(\PDO::FETCH_ASSOC); if ($hasSeq) { $pkColumns[0]->setAutoIncrement(true); $idMethodParameter = new IdMethodParameter(); $idMethodParameter->setValue($seqName); $table->addIdMethodParameter($idMethodParameter); } } $tables[] = $table; } foreach ($tables as $table) { $this->addForeignKeys($table); } return count($tables); }
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); } }
protected function parseTables(&$tableWraps, Database $database, Table $filterTable = null) { $stmt = null; $params = []; $sql = "\n SELECT c.oid, c.relname, n.nspname\n FROM pg_class c join pg_namespace n on (c.relnamespace=n.oid)\n WHERE c.relkind = 'r'\n AND n.nspname NOT IN ('information_schema','pg_catalog')\n AND n.nspname NOT LIKE 'pg_temp%'\n AND n.nspname NOT LIKE 'pg_toast%'"; if ($filterTable) { if ($schema = $filterTable->getSchema()) { $sql .= ' AND n.nspname = ?'; $params[] = $schema; } $sql .= ' AND c.relname = ?'; $params[] = $filterTable->getCommonName(); } else { if (!$database->getSchema()) { $stmt = $this->dbh->query('SELECT current_schemas(false)'); $searchPathString = substr($stmt->fetchColumn(), 1, -1); $params = []; $searchPath = explode(',', $searchPathString); foreach ($searchPath as &$path) { $params[] = $path; $path = '?'; } $searchPath = implode(', ', $searchPath); $sql .= "\n AND n.nspname IN ({$searchPath})"; } elseif ($database->getSchema()) { $sql .= "\n AND n.nspname = ?"; $params[] = $database->getSchema(); } } $sql .= "\n ORDER BY relname"; $stmt = $this->dbh->prepare($sql); $stmt->execute($params); // First load the tables (important that this happen before filling out details of tables) while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $name = $row['relname']; $namespaceName = $row['nspname']; if ($name == $this->getMigrationTable()) { continue; } $oid = $row['oid']; $table = new Table($name); if ('public' !== $namespaceName) { $table->setSchema($namespaceName); } $table->setIdMethod($database->getDefaultIdMethod()); $database->addTable($table); // Create a wrapper to hold these tables and their associated OID $wrap = new \stdClass(); $wrap->table = $table; $wrap->oid = $oid; $tableWraps[] = $wrap; } }
/** * 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); } }
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 testSetDefaultIdMethod() { $database = new Database(); $database->setDefaultIdMethod('native'); $this->assertSame('native', $database->getDefaultIdMethod()); }