monet.ts 6.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (C) 2018 Centro de Computacao Cientifica e Software Livre
 * Departamento de Informatica - Universidade Federal do Parana
 *
 * This file is part of blend.
 *
 * blend 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.
 *
 * blend 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 blend.  If not, see <http://www.gnu.org/licenses/>.
 */

import { SQLAdapter } from "./sql";
import { View } from "../core/view";
Rafael Dias's avatar
Rafael Dias committed
23
import { Source } from "../core/source";
24
import { FilterOperator } from "../core/filter";
25
import { DataType } from "../common/types";
26
/** @hidden */
27 28
const MDB = require("monetdb")();

29 30 31 32
/**
 * Params required to connect with a MonetDB database and
 * to create a MonetAdapter object.
 */
33
export interface MonetConfig {
34
    /** Database hostname */
35
    host: string;
36
    /** Database port */
37
    port: number;
38
    /** Database name */
39
    dbname: string;
40
    /** Database user */
41
    user: string;
42
    /** Datavase password */
43 44
    password: string;
}
45 46 47 48
/**
 * Represent the data format returned by MonetDB.
 * This interface is used to parse this format to the BlenDB Standart.
 */
49
interface MonetResult {
50
    /** Query result as a list of values */
51
    data: any[];
52
    /** Meta data of each attribute required. */
53 54 55
    structure: {type: string, column: string, index: number}[];
}

56 57 58
/**
 * Adapter which connects with a MonetDB database.
 */
59
export class MonetAdapter extends SQLAdapter {
60
    /** Information used to connect with a MonetDB database. */
61 62
    private config: MonetConfig;

63 64 65 66 67
    /**
     * Creates a new adapter with the database connection configuration.
     * @param conf - The information required to create a connection with
     * the database.
     */
68 69 70 71
    constructor (conf: MonetConfig) {
        super();
        this.config = conf;
    }
72 73 74 75 76 77 78 79 80

    /**
     * Asynchronously reads all data from given view.
     * In other words perform a SELECT query.
     * @param view - "Location" from all data should be read.
     * @param cb - Callback function which contains the data read.
     * @param cb.error - Error information when the method fails.
     * @param cb.result - Data got from view.
     */
81 82
    public getDataFromView(view: View, cb: (error: Error, result?: any[]) => void): void {
        const query = this.getQueryFromView(view);
Rafael Dias's avatar
Rafael Dias committed
83 84
        this.executeQuery(query, cb);
    }
85

86 87 88 89 90 91 92
    /**
     * Asynchronously executes a query and get its result.
     * @param query - Query (SQL format) to be executed.
     * @param cb - Callback function which contains the data read.
     * @param cb.error - Error information when the method fails.
     * @param cb.result - Query result.
     */
Rafael Dias's avatar
Rafael Dias committed
93
    private executeQuery(query: string, cb: (error: Error, result?: any[]) => void): void {
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        let pool: any = new MDB(this.config);
        pool.connect();
        pool.query(query).then((result: MonetResult) => {
            if (result) {
                let res = result.data.map((item) => {
                    let obj: any = {};
                    for (let i = 0; i < result.structure.length; ++i) {
                        let struct = result.structure[i];
                        if (struct.type === "timestamp") {
                            obj[struct.column] = new Date(item[struct.index]);
                        }
                        else {
                            obj[struct.column] = item[struct.index];
                        }
                    }
                    return obj;
                });
                cb(null, res);
            }
            else {
                cb(null, null);
            }
        }).fail((err: Error) => {
            cb(err, null);
        });
        pool.close();
    }

122 123 124 125
    /**
     * Materialize a given view.
     * @param view - View to  be materialized.
     */
126 127 128 129
    public materializeView(view: View): boolean {
        return false;
    }

130 131 132 133 134 135 136
    /**
     * Asynchronously insert one register into a given Source.
     * @param source - Insertion "location".
     * @param cb - Callback function which contains the query result.
     * @param cb.error - Error information when the method fails.
     * @param cb.result - Query result.
     */
Rafael Dias's avatar
Rafael Dias committed
137 138 139 140 141
    public insertIntoSource(source: Source, data: any[], cb: (err: Error, result: any[]) =>  void): void {
        const query = this.getQueryFromSource(source, data);
        this.executeQuery(query, cb);
    }

142 143 144 145 146
    /**
     * Cast BlenDB data types to be used in MonetDB queries.
     * @param quotedValue - SQL query attribute wrapped by quotes.
     * @param dt - Attribute data type.
     */
147
    protected typeCast(quotedValue: string, dt: DataType): string {
148
        switch (dt) {
149
            case DataType.DATE:
150
                return "CAST(" + quotedValue + " AS TIMESTAMP)";
151
            case DataType.INTEGER:
152
                return "CAST(" + quotedValue + " AS INTEGER)";
153
            case DataType.BOOLEAN:
154 155 156 157 158 159
                return "CAST(" + quotedValue + " AS BOOLEAN)";
            default:
                return quotedValue;
        }
    }

160 161 162 163 164 165
    /**
     * Translate filter operator to be used in MonetDB queries.
     * @param lSide - Operation left side operator.
     * @param rSide - Operation right side operator.
     * @param op - Operation to be performed.
     */
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    protected applyOperator(lSide: string, rSide: string, op: FilterOperator): string {
        switch (op) {
            case FilterOperator.EQUAL:
                return lSide + " = " + rSide;
            case FilterOperator.NOTEQUAL:
                return "NOT(" + lSide + " = " + rSide + ")";
            case FilterOperator.GREATER:
                return lSide + " > " + rSide;
            case FilterOperator.LOWER:
                return lSide + " < " + rSide;
            case FilterOperator.GREATEREQ:
                return lSide + " >= " + rSide;
            case FilterOperator.LOWEREQ:
                return lSide + " <= " + rSide;
            default:
                return  "";
        }
    }
}