postgres.ts 4.79 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 93
    /**
     * Cast BlenDB data types to be used in PostgreSQL queries.
     * @param quotedValue - SQL query attribute wrapped by quotes.
     * @param dt - Attribute data type.
     */
94
    protected typeCast(quotedValue: string, dt: DataType): string {
95
        switch (dt) {
96
            case DataType.DATE:
97
                return quotedValue + "::DATE";
98
            case DataType.INTEGER:
99
                return quotedValue + "::INTEGER";
100
            case DataType.BOOLEAN:
101
                return quotedValue + "::BOOLEAN";
102
            default:
103
                return quotedValue;
104 105 106
        }
    }

107 108 109 110 111 112
    /**
     * 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.
     */
113
    protected applyOperator(lSide: string, rSide: string, op: FilterOperator): string {
114 115
        switch (op) {
            case FilterOperator.EQUAL:
116
                return lSide + " = " + rSide;
117
            case FilterOperator.NOTEQUAL:
118
                return lSide + " != " + rSide;
119
            case FilterOperator.GREATER:
120
                return lSide + " > " + rSide;
121
            case FilterOperator.LOWER:
122
                return lSide + " < " + rSide;
123
            case FilterOperator.GREATEREQ:
124
                return lSide + " >= " + rSide;
125
            case FilterOperator.LOWEREQ:
126
                return lSide + " <= " + rSide;
127 128 129
            default:
                return  "";
        }
130
    }
131
}