"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__tsfTypeAliases = exports.PostgresDriver = void 0;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-server-foundation/type-metadata-runtime"));
const pg_1 = require("pg");
const __tsf_metadata_type = _a.createCompactMetadataRegistryV1("[1,[{\"kind\":2},{\"kind\":22,\"type\":{\"kind\":3}},{\"kind\":22,\"type\":{\"kind\":18,\"typeName\":\"DriverConnection\",\"types\":[]}},{\"kind\":18,\"typeName\":\"RenderedSql\",\"types\":[{\"kind\":20,\"name\":\"sql\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"bindings\",\"type\":{\"kind\":14,\"type\":{\"kind\":2}},\"optional\":false}]},{\"kind\":22,\"type\":{\"kind\":18,\"typeName\":\"ExecuteResult\",\"types\":[{\"kind\":20,\"name\":\"affectedRows\",\"type\":{\"kind\":7},\"optional\":false},{\"kind\":20,\"name\":\"insertId\",\"type\":{\"kind\":12,\"types\":[{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":6}]},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"warningStatus\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"rowCount\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true}]}},{\"kind\":6},{\"kind\":18,\"typeName\":\"PgClientLike\",\"types\":[]}]]", []);
let _PostgresDriverMetadata_1;
class PostgresDriver {
    dialect = 'postgres';
    pool;
    constructor(configOrPool) {
        this.pool = isPgPoolLike(configOrPool) ? configOrPool : new pg_1.Pool(configOrPool);
    }
    async connect() {
        const connection = await this.acquire();
        await connection.release();
    }
    async close() {
        await this.pool.end();
    }
    async acquire() {
        return new PostgresConnection(await this.pool.connect());
    }
    static __tsfType = _PostgresDriverMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"PostgresDriver\",\"typeName\":\"PostgresDriver\",\"classType\":{\"$tsf\":0},\"properties\":[{\"name\":\"dialect\",\"type\":{\"$tsfType\":0},\"optional\":false}],\"methods\":[{\"name\":\"connect\",\"parameters\":[],\"returnType\":{\"$tsfType\":1}},{\"name\":\"close\",\"parameters\":[],\"returnType\":{\"$tsfType\":1}},{\"name\":\"acquire\",\"parameters\":[],\"returnType\":{\"$tsfType\":2}}],\"hasConstructor\":true,\"constructorParameters\":[{\"name\":\"configOrPool\",\"type\":{\"kind\":12,\"types\":[{\"$tsfAlias\":[1,\"pg\",\"PoolConfig\",\"PoolConfig\"]},{\"kind\":18,\"typeName\":\"PgPoolLike\",\"types\":[]}]},\"optional\":false,\"default\":false}]}]", [() => PostgresDriver, require], __tsf_metadata_type);
}
exports.PostgresDriver = PostgresDriver;
_PostgresDriverMetadata_1.classType = PostgresDriver;
PostgresDriver.__tsfType = _PostgresDriverMetadata_1;
let _PostgresConnectionMetadata_1;
class PostgresConnection {
    client;
    constructor(client) {
        this.client = client;
    }
    async query(query) {
        const result = await this.client.query(query.sql, query.bindings);
        return { rows: result.rows };
    }
    async execute(query) {
        const result = await this.client.query(query.sql, query.bindings);
        return {
            affectedRows: result.rowCount ?? 0,
            rowCount: result.rowCount ?? 0
        };
    }
    async begin() {
        await this.client.query('BEGIN');
    }
    async commit() {
        await this.client.query('COMMIT');
    }
    async rollback() {
        await this.client.query('ROLLBACK');
    }
    async savepoint(name) {
        await this.client.query(`SAVEPOINT ${quoteSavepoint(name)}`);
    }
    async rollbackToSavepoint(name) {
        await this.client.query(`ROLLBACK TO SAVEPOINT ${quoteSavepoint(name)}`);
    }
    async release() {
        this.client.release();
    }
    static __tsfType = _PostgresConnectionMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"PostgresConnection\",\"typeName\":\"PostgresConnection\",\"classType\":{\"$tsf\":0},\"properties\":[],\"methods\":[{\"name\":\"query\",\"parameters\":[{\"name\":\"query\",\"type\":{\"$tsfType\":3},\"optional\":false,\"default\":false}],\"returnType\":{\"kind\":22,\"type\":{\"kind\":16,\"typeName\":\"QueryResult\",\"classType\":{\"$tsfImport\":[1,\"../driver\",\"QueryResult\"]},\"typeArguments\":[{\"kind\":2,\"typeName\":\"T\"}]}}},{\"name\":\"execute\",\"parameters\":[{\"name\":\"query\",\"type\":{\"$tsfType\":3},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":4}},{\"name\":\"begin\",\"parameters\":[],\"returnType\":{\"$tsfType\":1}},{\"name\":\"commit\",\"parameters\":[],\"returnType\":{\"$tsfType\":1}},{\"name\":\"rollback\",\"parameters\":[],\"returnType\":{\"$tsfType\":1}},{\"name\":\"savepoint\",\"parameters\":[{\"name\":\"name\",\"type\":{\"$tsfType\":5},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":1}},{\"name\":\"rollbackToSavepoint\",\"parameters\":[{\"name\":\"name\",\"type\":{\"$tsfType\":5},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":1}},{\"name\":\"release\",\"parameters\":[],\"returnType\":{\"$tsfType\":1}}],\"hasConstructor\":true,\"constructorParameters\":[{\"name\":\"client\",\"type\":{\"$tsfType\":6},\"optional\":false,\"default\":false}]}]", [() => PostgresConnection, require], __tsf_metadata_type);
}
_PostgresConnectionMetadata_1.classType = PostgresConnection;
PostgresConnection.__tsfType = _PostgresConnectionMetadata_1;
function isPgPoolLike(value) {
    return typeof value.connect === 'function' && typeof value.end === 'function';
}
function quoteSavepoint(name) {
    return `"${name.replace(/"/g, '""')}"`;
}
exports.__tsfTypeAliases = _a.decodeCompactMetadataV1("[1,{\"PgClientLike\":{\"kind\":18,\"typeName\":\"PgClientLike\",\"types\":[]},\"PgPoolLike\":{\"kind\":18,\"typeName\":\"PgPoolLike\",\"types\":[]}}]", [], __tsf_metadata_type);
//# sourceMappingURL=postgres.js.map