"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__tsfTypeAliases = exports.MySQLDriver = void 0;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-reflection/type-metadata-runtime"));
const promise_1 = require("mysql2/promise");
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\":\"QueryResult\",\"types\":[{\"kind\":20,\"name\":\"rows\",\"type\":{\"kind\":14,\"type\":{\"kind\":2,\"typeName\":\"T\"}},\"optional\":false},{\"kind\":20,\"name\":\"executeResult\",\"type\":{\"kind\":12,\"types\":[{\"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\":4}]},\"optional\":true}]}},{\"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\":\"MySQLConnectionLike\",\"types\":[]}]]", []);
let _MySQLDriverMetadata_1;
class MySQLDriver {
    dialect = 'mysql';
    pool;
    constructor(configOrPool) {
        this.pool = isMySQLPoolLike(configOrPool)
            ? configOrPool
            : (0, promise_1.createPool)({
                decimalNumbers: true,
                ...configOrPool,
                timezone: configOrPool.timezone ?? 'Z'
            });
    }
    async connect() {
        const connection = await this.acquire();
        await connection.release();
    }
    async close() {
        await this.pool.end();
    }
    async acquire() {
        return new MySQLConnection(await this.pool.getConnection());
    }
    static __tsfType = _MySQLDriverMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"MySQLDriver\",\"typeName\":\"MySQLDriver\",\"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,\"mysql2/promise\",\"PoolOptions\",\"PoolOptions\"]},{\"kind\":18,\"typeName\":\"MySQLPoolLike\",\"types\":[]}]},\"optional\":false,\"default\":false}]}]", [() => MySQLDriver, require], __tsf_metadata_type);
}
exports.MySQLDriver = MySQLDriver;
_MySQLDriverMetadata_1.classType = MySQLDriver;
MySQLDriver.__tsfType = _MySQLDriverMetadata_1;
let _MySQLConnectionMetadata_1;
class MySQLConnection {
    connection;
    constructor(connection) {
        this.connection = connection;
    }
    async query(query) {
        const [rows] = await this.connection.query(query.sql, query.bindings);
        return { rows: Array.isArray(rows) ? rows : [] };
    }
    async execute(query) {
        const [result] = await this.connection.execute(query.sql, query.bindings);
        return {
            affectedRows: Number(result?.affectedRows ?? 0),
            insertId: result?.insertId,
            warningStatus: result?.warningStatus,
            rowCount: Number(result?.affectedRows ?? 0)
        };
    }
    async begin() {
        await this.connection.query('START TRANSACTION');
    }
    async commit() {
        await this.connection.query('COMMIT');
    }
    async rollback() {
        await this.connection.query('ROLLBACK');
    }
    async savepoint(name) {
        await this.connection.query(`SAVEPOINT ${quoteSavepoint(name)}`);
    }
    async rollbackToSavepoint(name) {
        await this.connection.query(`ROLLBACK TO SAVEPOINT ${quoteSavepoint(name)}`);
    }
    async release() {
        await this.connection.release();
    }
    static __tsfType = _MySQLConnectionMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"MySQLConnection\",\"typeName\":\"MySQLConnection\",\"classType\":{\"$tsf\":0},\"properties\":[],\"methods\":[{\"name\":\"query\",\"parameters\":[{\"name\":\"query\",\"type\":{\"$tsfType\":3},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":4}},{\"name\":\"execute\",\"parameters\":[{\"name\":\"query\",\"type\":{\"$tsfType\":3},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":5}},{\"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\":6},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":1}},{\"name\":\"rollbackToSavepoint\",\"parameters\":[{\"name\":\"name\",\"type\":{\"$tsfType\":6},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":1}},{\"name\":\"release\",\"parameters\":[],\"returnType\":{\"$tsfType\":1}}],\"hasConstructor\":true,\"constructorParameters\":[{\"name\":\"connection\",\"type\":{\"$tsfType\":7},\"optional\":false,\"default\":false}]}]", [() => MySQLConnection], __tsf_metadata_type);
}
_MySQLConnectionMetadata_1.classType = MySQLConnection;
MySQLConnection.__tsfType = _MySQLConnectionMetadata_1;
function isMySQLPoolLike(value) {
    return typeof value.getConnection === 'function' && typeof value.end === 'function';
}
function quoteSavepoint(name) {
    return `\`${name.replace(/`/g, '``')}\``;
}
exports.__tsfTypeAliases = JSON.parse("[1,{\"MySQLConnectionLike\":{\"kind\":18,\"typeName\":\"MySQLConnectionLike\",\"types\":[]},\"MySQLPoolLike\":{\"kind\":18,\"typeName\":\"MySQLPoolLike\",\"types\":[]}}]")[1];
//# sourceMappingURL=mysql.js.map