Skip to content
Snippets Groups Projects
Select Git revision
  • develop default protected
  • simmc-based
  • drill-experiment
  • tg-felipe
  • issue/97
  • issue/63
  • icde-2019-experiments
  • issue/85
  • master protected
  • issue/20
  • refactor/engine
  • issue/6
  • feature/diagrams
  • wip-transformers
14 results

schema.ts

Blame
  • schema.ts 4.13 KiB
    #!/usr/bin/env node
    
    /*
     * Copyright (C) 2017 Centro de Computacao Cientifica e Software Livre
     * Departamento de Informatica - Universidade Federal do Parana
     *
     * This file is part of blendb.
     *
     * blendb is free software: you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     *
     * blendb is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with blendb.  If not, see <http://www.gnu.org/licenses/>.
     */
    
    import { Engine } from "../src/core/engine";
    import { PostgresAdapter } from "../src/adapter/postgres";
    import { ConfigParser } from "../src/util/configParser";
    import { QueryOpts, Query } from "../src/common/query";
    import * as path from "path";
    import * as fs from "fs";
    
    if (process.argv.length !== 4) {
        console.log("Wrong arguments number.");
        console.log("Arguments: <configFile> <outputFile>");
        process.exitCode = 1;
        process.exit();
    }
    
    const configFile = process.argv[2];
    const schemaFile = process.argv[3];
    const config = ConfigParser.parse(configFile);
    const referencePath = path.dirname(configFile);
    
    const engine = new Engine(config);
    const adapter = new PostgresAdapter(config.connections[0]);
    
    let schema = "";
    
    for (let i = 0; i < config.buildViews.length; ++i) {
        const view = config.buildViews[i].view;
        const filePath = config.buildViews[i].file;
        const alias = config.buildViews[i].alias;
        if (view.origin) {
            // Cria uma tabela
            let output = "-- View: " + alias + "\n";
            if (process.env.BLENDB_ADAPTER  === "postgres") {
                output += "DROP VIEW IF EXISTS " + view.name + " CASCADE;\n";
                output += "CREATE OR REPLACE VIEW " +  view.name + " AS\n";
            }
    
            else if (process.env.BLENDB_ADAPTER  === "monet") {
                output += "DROP VIEW " + view.name + " CASCADE;\n";
                output += "CREATE VIEW " +  view.name + " AS\n";
            }
    
            else {
                console.log("The adapter value:", process.env.BLENDB_ADAPTER, "is invalid. Abort");
                process.exit(1);
            }
            output += fs.readFileSync(referencePath + "/" + filePath, {encoding: "utf-8" });
            schema += output + "\n";
            engine.addView(view);
        }
    
    }
    
    for (let i = 0; i < config.buildViews.length; ++i) {
        const view = config.buildViews[i].view;
        const alias = config.buildViews[i].alias;
        if (!view.origin) {
            const qOpt: QueryOpts = { metrics: view.metrics,dimensions: view.dimensions,
                clauses: view.clauses };
            const materializedView = engine.query(new Query(qOpt));
    
            const table = adapter.getQueryFromView(materializedView);
            let query = "-- View: " + alias + "\n";
            if (process.env.BLENDB_ADAPTER  === "postgres") {
                query += "DROP MATERIALIZED VIEW IF EXISTS view_" + view.id + " CASCADE;\n" +
                          "CREATE MATERIALIZED VIEW view_" + materializedView.id +
                          " AS "  + table + "\n\n";
            }
    
            else if (process.env.BLENDB_ADAPTER  === "monet") {
                console.log("MonetDb does not support Materializaed views. Abort");
                console.log("All views in config.yaml must be origin form MonetDB");
                console.log("This is a known issue.");
                console.log("In the future this script will create a table and copy the data.");
                process.exit(1);
            }
    
            else {
                console.log("The adapter value:", process.env.BLENDB_ADAPTER, "is invalid. Abort");
                process.exit(1);
            }
    
            schema += query;
            engine.addView(materializedView);
    
        }
    }
    fs.writeFile(schemaFile, schema, (error) => {
        if (error) {
            console.log("The schema file could not be written. Abort");
            process.exitCode = 1;
            return;
        }
    
        else {
            console.log("File: " + schemaFile + " created");
        }
    });