"use strict";var __importDefault=this&&this.__importDefault||function(r){return r&&r.__esModule?r:{default:r}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.MySQL=void 0;const node_diagnostics_channel_1=__importDefault(require("node:diagnostics_channel")),faas_server_utils_1=require("@alipay/faas-server-utils"),ali_rds_1=require("ali-rds"),transaction_1=require("./transaction"),utils_1=require("../utils");class MySQL{#n;#e;#s=!0;constructor(n){this.#n=n}async query(n,s){return await this.#t().query(n,s)}async insert(n,s,o){return await this.#t().insert(n,s,o)}async update(n,s,o){return await this.#t().update(n,s,o)}async select(n,s){return await this.#t().select(n,s)}async get(n,s,o){return await this.#t().get(n,s,o)}async delete(n,s){return await this.#t().delete(n,s)}async count(n,s){return await this.#t().count(n,s)}async beginTransaction(){const n=await this.#t().beginTransaction();return new transaction_1.MySQLTransaction(n)}async beginTransactionScope(n){return await this.#t().beginTransactionScope(async s=>{const o=new transaction_1.MySQLTransaction(s);return await n(o)})}#o(n=""){return(0,utils_1.authString)(this.#n,n)}#t(){if(!this.#e){if(process.env.MYSQL_TEST_MODE==="test")this.#s=!1,this.#e=new ali_rds_1.RDSClient({host:process.env.MYSQL_HOST||"127.0.0.1",port:process.env.MYSQL_PORT?parseInt(process.env.MYSQL_PORT):3306,user:process.env.MYSQL_USER||"root",password:process.env.MYSQL_PASSWORD||"",database:process.env.MYSQL_DATABASE||"faas_unittest"});else{const[n,s]=this.#n.functionMysqlEndpoint.split(":");this.#e=new ali_rds_1.RDSClient({getConnectionConfig:()=>({host:n,port:parseInt(s),user:this.#o(),password:"",database:""})})}this.#e.beforeQuery(n=>this.#s?`/*${this.#o(n)}*/${n}`:n)}return this.#e}}exports.MySQL=MySQL;const debug=(0,faas_server_utils_1.debuglog)("faas-server-sdk:mysql");if(debug.enabled){let r=0,n=0;const s=Symbol("rds client id"),o=Symbol("rds client enqueue counter"),c=Symbol("connection id"),y=Symbol("rds client create connection counter"),a=Symbol("connection acquire counter"),u=Symbol("connection release counter"),l=Symbol("connection query start counter"),d=Symbol("connection query end counter");node_diagnostics_channel_1.default.subscribe("ali-rds:connection:new",i=>{const{client:e,connection:t}=i;e[s]||(e[s]=r++),t[c]||(t[c]=n++,t[s]=e[s]),e[y]=(e[y]||0)+1,debug("[ali-rds:connection:new] client#%s create a new connection#%s (%s#%s:%s), total created %s, stats: %o",e[s],t[c],t.threadId,t.config.host,t.config.port,e[y],e.stats)}),node_diagnostics_channel_1.default.subscribe("ali-rds:connection:acquire",i=>{const{client:e,connection:t}=i;t[a]=(t[a]||0)+1,debug("[ali-rds:connection:acquire] client#%s acquire connection#%s (%s#%s:%s, acquire/release: %s/%s), stats: %o",e[s],t[c],t.threadId,t.config.host,t.config.port,t[a],t[u]||0,e.stats)}),node_diagnostics_channel_1.default.subscribe("ali-rds:connection:release",i=>{const{client:e,connection:t}=i;t[u]=(t[u]||0)+1,debug("[ali-rds:connection:release] client#%s release connection#%s (%s#%s:%s, acquire/release: %s/%s), stats: %o",e[s],t[c],t.threadId,t.config.host,t.config.port,t[a],t[u],e.stats)}),node_diagnostics_channel_1.default.subscribe("ali-rds:connection:enqueue",i=>{const{client:e}=i;e[o]=(e[o]||0)+1,debug("[ali-rds:connection:enqueue] client#%s enqueue, times: %s, stats: %o",e[s],e[o],e.stats)}),node_diagnostics_channel_1.default.subscribe("ali-rds:query:start",i=>{const{sql:e,connection:t}=i;t[l]=(t[l]||0)+1,debug("[ali-rds:query:start] client#%s connection#%s (%s#%s:%s, query: %s/%s) start query: %j",t[s],t[c],t.threadId,t.config.host,t.config.port,t[l],t[d]||0,e)}),node_diagnostics_channel_1.default.subscribe("ali-rds:query:end",i=>{const{connection:e,duration:t,error:h}=i;e[d]=(e[d]||0)+1,debug("[ali-rds:query:end] client#%s connection#%s (%s#%s:%s, query: %s/%s) end query, duration: %sms, error: %s",e[s],e[c],e.threadId,e.config.host,e.config.port,e[l],e[d],t,h)})}