if (!empty($mapTargetTableNames)) { if (!empty($allowedTableNames)) { $allowedTableNames = array_intersect($allowedTableNames, $mapTargetTableNames); } else { $allowedTableNames = $mapTargetTableNames; } } // Load the DDL from the database, filtering out all non-allowed tables. $db = MyConnectionFactory::getConnection($connectionName); $dialect = $db->getDialect(); $loader = new ConnectionDDLLoader(); $databaseDDL = $loader->loadDDL($db, false, $databaseAllowedTableNames); $db->close(); // Load the current DDL from the DDL files. $currentDDL = new DDL(array()); if (($res = YAMLDDLParser::loadAllDDLFiles($ddlDir, $currentDDL, $allowedTableNames, $dbmap)) != 0) { exit($res); } // If we have map-target tables, remove all foreign keys from the current DDL which // reference tables which are not map targets (because those tables should not exist // in this database). if (!empty($mapTargetTableNames)) { $anyDeleted = false; for ($i = 0, $n = count($currentDDL->topLevelEntities); $i < $n; $i++) { if ($currentDDL->topLevelEntities[$i] instanceof DDLForeignKey && !in_array($currentDDL->topLevelEntities[$i]->foreignTableName, $mapTargetTableNames)) { unset($currentDDL->topLevelEntities[$i]); $anyDeleted = true; } } if ($anyDeleted) { $currentDDL->topLevelEntities = array_slice($currentDDL->topLevelEntities, 0);
function getTableDDL($tableName, $_ddlDir = null) { global $ddlFromDB, $dbClass, $dbHost, $dbUser, $dbPassword, $dbDatabase, $ddlDir; $ddl = new DDL(); if ($ddlFromDB) { // Load DDL from database. if (!class_exists($dbClass, false)) { include dirname(dirname(__FILE__)) . '/phpdaogen/' . $dbClass . '.class.php'; } $db = new $dbClass($dbHost, $dbUser, $dbPassword, $dbDatabase); $loader = new ConnectionDDLLoader(); $tmpddl = $loader->loadDDL($db, false, array($tableName)); $db->close(); mergeDDLForTable($ddl, $tmpddl, $tableName); unset($tmpddl); } else { // Load DDL from DDL file(s). if ($_ddlDir === null) { $_ddlDir = $ddlDir; } if (($dp = @opendir($_ddlDir)) !== false) { while (($fn = readdir($dp)) !== false) { if ($fn == '.' || $fn == '..') { continue; } $fn = $_ddlDir . '/' . $fn; if (is_dir($fn)) { if (($tmpddl = getTableDDL($tableName, $fn)) !== false) { mergeDDLForTable($ddl, $tmpddl, $tableName); unset($tmpddl); } continue; } if (is_file($fn) && strtolower(substr($fn, -9)) == '.ddl.yaml' || strtolower(substr($fn, -8)) == '.ddl.yml') { try { $parser = new YAMLDDLParser(); if (($tmpddl = $parser->parseFromYAML(file_get_contents($fn))) !== false) { mergeDDLForTable($ddl, $tmpddl, $tableName); unset($tmpddl); } unset($tmpddl); continue; } catch (Exception $ex) { fprintf(STDERR, "%s\n%s\n", $ex->getMessage(), $ex->getTrace()); if ($exitval == 0) { $exitval = 2; } } } } closedir($dp); } } return $ddl; }
// parseini_multi.php // Copyright (c) 2011-2016 Ronald B. Cemer // All rights reserved. // This software is released under the BSD license. // Please see the accompanying LICENSE.txt for details. include dirname(dirname(__FILE__)) . '/phpdaogen/AbstractINIMultiDatabaseConnectionFactory.class.php'; include dirname(dirname(__FILE__)) . '/phpdaogen/DDL.class.php'; if ($argc < 3 || $argc > 4) { fprintf(STDERR, "Please specify a single INI filename, the path to the ddl directory where the YAML schema files exist, and an optional secondary database name.\n"); exit(1); } $connectionParamsByName = AbstractINIMultiDatabaseConnectionFactory::loadDatabaseIniFile($argv[1]); $ddldir = $argv[2]; $connectionName = $argc >= 4 ? trim($argv[3]) : ''; $aggregateDDL = new DDL(); if (($res = YAMLDDLParser::loadAllDDLFiles(realpath($ddldir), $aggregateDDL)) != 0) { return $res; } $errorMsgs = AbstractINIMultiDatabaseConnectionFactory::validateDatabaseIniConfiguration($connectionParamsByName, $aggregateDDL); if (!empty($errorMsgs)) { foreach ($errorMsgs as $errorMsg) { fputs(STDERR, $errorMsg); fputs(STDERR, "\n"); } exit(20); } $params = AbstractINIMultiDatabaseConnectionFactory::getConnectionParams($connectionName, null, null, $connectionParamsByName); foreach ($params as $key => $val) { echo "ini_{$key}=" . escapeshellarg($val) . "\n"; } echo 'ini_allConnectionNames=' . implode(',', array_keys($connectionParamsByName)) . "\n";
break; case 'pgsql': $db = new PostgreSQLConnection($dbServer, $dbUsername, $dbPassword, $dbDatabase); break; default: fprintf(STDERR, "Unsupported dialect: %s\n", $dialect); exit(1); } $loader = new ConnectionDDLLoader(); $ddl = $loader->loadDDL($db, false, $allowedTableNames); $db->close(); unset($db); } else { // Load new DDL from DDL file(s). $aggregateDDL = new DDL(); $res = YAMLDDLParser::loadAllDDLFiles($ddlDir, $aggregateDDL, $allowedTableNames); if ($res != 0) { exit($res); } $ddl = $aggregateDDL; unset($aggregateDDL); } ///print_r($ddl); $generator = new DAOClassGenerator(); function file_put_contents_if_changed($filename, $contents) { if (!file_exists($filename) || file_get_contents($filename) != $contents) { return file_put_contents($filename, $contents); } return strlen($contents); }
public static function loadAndAggregateDDLFile($ddlFile, &$aggregateDDL) { if (!file_exists($ddlFile)) { fprintf(STDERR, "Cannot find DDL file: %s\n", $ddlFile); return 11; } $result = 12; $errorMsg = ''; try { if (strtolower(substr($ddlFile, -9)) == '.ddl.yaml' || strtolower(substr($ddlFile, -8)) == '.ddl.yml') { $group = str_replace(array('.ddl.yaml', '.ddl.yml'), '', basename($ddlFile)); $parser = new YAMLDDLParser(); $ddl = $parser->parseFromYAML(file_get_contents($ddlFile), array(), $group); $aggregateDDL->topLevelEntities = array_merge($aggregateDDL->topLevelEntities, $ddl->topLevelEntities); $result = 0; } else { throw new Exception("Unrecognized file extension (must be .ddl.yaml or .ddl.yml)"); } } catch (Exception $ex) { $errorMsg = $ex->getMessage(); $result = 11; } if ($result != 0) { fprintf(STDERR, "\n\nError processing DDL file: %s\n", $ddlFile); if ($errorMsg != '') { fprintf(STDERR, "Error message: %s\n", $errorMsg); } } return $result; }