/*
 * 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";
import { Source } from "../core/source";
import { FilterOperator } from "../core/filter";
import { Pool, PoolConfig } from "pg";
import { DataType } from "../common/types";

export class PostgresAdapter extends SQLAdapter {
    private pool: Pool;

    constructor (config: PoolConfig) {
        super();
        this.pool = new Pool(config);
    }
    public getDataFromView(view: View, cb: (error: Error, result?: any[]) => void): void {
        const query = this.getQueryFromView(view);
        this.executeQuery(query, cb);
    }

    private executeQuery(query: string, cb: (err: Error, result?: any[]) =>  void): void{
        this.pool.connect((err, client, done) => {
            if (err) {
                cb (err);
                return;
            }
            client.query(query, [], (error, result) => {
                // call 'done()' to release client back to pool
                done();
                cb(error, (result) ? result.rows : null);
            });
        });
    }
    public insertIntoSource(source: Source, data: any[], cb: (err: Error, result?: any[]) =>  void): void {
        const query = this.getQueryFromSource(source, data);
        this.executeQuery(query, cb);
    }

    public materializeView(view: View): boolean {
        return false;
    }

    protected typeCast(quotedValue: string, dt: DataType): string {
        switch (dt) {
            case DataType.DATE:
                return quotedValue + "::DATE";
            case DataType.INTEGER:
                return quotedValue + "::INTEGER";
            case DataType.BOOLEAN:
                return quotedValue + "::BOOLEAN";
            default:
                return quotedValue;
        }
    }

    protected applyOperator(lSide: string, rSide: string, op: FilterOperator): string {
        switch (op) {
            case FilterOperator.EQUAL:
                return lSide + " = " + rSide;
            case FilterOperator.NOTEQUAL:
                return 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  "";
        }
    }
}