postgres.ts 4.96 KB
Newer Older
1
/*
2
 * Copyright (C) 2018 Centro de Computacao Cientifica e Software Livre
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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/>.
 */

21
import { SQLAdapter } from "./sql";
22
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 { Pool, PoolConfig } from "pg";
26
import { DataType } from "../common/types";
27

28
/** Adapter which connects with a PostgreSQL database. */
29
export class PostgresAdapter extends SQLAdapter {
30
    /** Information used to connect with a PostgreSQL database. */
31 32
    private pool: Pool;

33 34 35 36 37
    /**
     * Creates a new adapter with the database connection configuration.
     * @param config - The information required to create a connection with
     * the database.
     */
38 39 40 41
    constructor (config: PoolConfig) {
        super();
        this.pool = new Pool(config);
    }
42 43 44 45 46 47 48 49 50

    /**
     * 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.
     */
51
    public getDataFromView(view: View, cb: (error: Error, result?: any[]) => void): void {
52
        const query = this.getQueryFromView(view);
Rafael Dias's avatar
Rafael Dias committed
53 54
        this.executeQuery(query, cb);
    }
55

56 57 58 59 60 61 62
    /**
     * 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
63
    private executeQuery(query: string, cb: (err: Error, result?: any[]) =>  void): void{
64 65 66
        this.pool.connect((err, client, done) => {
            if (err) {
                cb (err);
67
                return;
68 69 70 71 72 73 74
            }
            client.query(query, [], (error, result) => {
                // call 'done()' to release client back to pool
                done();
                cb(error, (result) ? result.rows : null);
            });
        });
75
    }
76 77 78 79 80 81 82 83

    /**
     * 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
84 85 86 87
    public insertIntoSource(source: Source, data: any[], cb: (err: Error, result?: any[]) =>  void): void {
        const query = this.getQueryFromSource(source, data);
        this.executeQuery(query, cb);
    }
88

89 90 91 92
    /**
     * Materialize a given view.
     * @param view - View to  be materialized.
     */
93 94
    public materializeView(view: View): boolean {
        return false;
95 96
    }

97 98 99 100 101
    /**
     * Cast BlenDB data types to be used in PostgreSQL queries.
     * @param quotedValue - SQL query attribute wrapped by quotes.
     * @param dt - Attribute data type.
     */
102
    protected typeCast(quotedValue: string, dt: DataType): string {
103
        switch (dt) {
104
            case DataType.DATE:
105
                return quotedValue + "::DATE";
106
            case DataType.INTEGER:
107
                return quotedValue + "::INTEGER";
108
            case DataType.BOOLEAN:
109
                return quotedValue + "::BOOLEAN";
110
            default:
111
                return quotedValue;
112 113 114
        }
    }

115 116 117 118 119 120
    /**
     * Translate filter operator to be used in PostgreSQL queries.
     * @param lSide - Operation left side operator.
     * @param rSide - Operation right side operator.
     * @param op - Operation to be performed.
     */
121
    protected applyOperator(lSide: string, rSide: string, op: FilterOperator): string {
122 123
        switch (op) {
            case FilterOperator.EQUAL:
124
                return lSide + " = " + rSide;
125
            case FilterOperator.NOTEQUAL:
126
                return lSide + " != " + rSide;
127
            case FilterOperator.GREATER:
128
                return lSide + " > " + rSide;
129
            case FilterOperator.LOWER:
130
                return lSide + " < " + rSide;
131
            case FilterOperator.GREATEREQ:
132
                return lSide + " >= " + rSide;
133
            case FilterOperator.LOWEREQ:
134
                return lSide + " <= " + rSide;
135 136 137
            default:
                return  "";
        }
138
    }
139
}