Example #1
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;
}
Example #2
0
 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;
 }