import{a as v,b as B,c as A,d as q,f as _,g as z,i as l,j as g}from"./chunk-3WWIVTCY.js";import{d as R}from"./chunk-F4GETNPB.js";import{e as P,f as w,g as d,h as a,j as f}from"./chunk-QY3QWFKW.js";f();f();function E(m){let s=m.e;return s.query=m.query,s.params=m.params,s.queryOptions=m.options,s}var T,p,t,y,x,h,O,k=class{constructor(){w(this,t);this.serializers={...B};this.parsers={...v};w(this,T,!1);w(this,p,!1)}async _initArrayTypes({force:s=!1}={}){if(P(this,T)&&!s)return;d(this,T,!0);let e=await this.query(` SELECT b.oid, b.typarray FROM pg_catalog.pg_type a LEFT JOIN pg_catalog.pg_type b ON b.oid = a.typelem WHERE a.typcategory = 'A' GROUP BY b.oid, b.typarray ORDER BY b.oid `);for(let r of e.rows)this.serializers[r.typarray]=o=>A(o,this.serializers[r.oid],r.typarray),this.parsers[r.typarray]=o=>q(o,this.parsers[r.oid],r.typarray)}async refreshArrayTypes(){await this._initArrayTypes({force:!0})}async query(s,e,r){return await this._checkReady(),await this._runExclusiveTransaction(async()=>await a(this,t,x).call(this,s,e,r))}async sql(s,...e){let{query:r,params:o}=R(s,...e);return await this.query(r,o)}async exec(s,e){return await this._checkReady(),await this._runExclusiveTransaction(async()=>await a(this,t,h).call(this,s,e))}async describeQuery(s,e){let r=[];try{await a(this,t,y).call(this,l.parse({text:s,types:e?.paramTypes}),e),r=await a(this,t,y).call(this,l.describe({type:"S"}),e)}catch(n){throw n instanceof g?E({e:n,options:e,params:void 0,query:s}):n}finally{r.push(...await a(this,t,y).call(this,l.sync(),e))}let o=r.find(n=>n.name==="parameterDescription"),i=r.find(n=>n.name==="rowDescription"),c=o?.dataTypeIDs.map(n=>({dataTypeID:n,serializer:this.serializers[n]}))??[],u=i?.fields.map(n=>({name:n.name,dataTypeID:n.dataTypeID,parser:this.parsers[n.dataTypeID]}))??[];return{queryParams:c,resultFields:u}}async transaction(s){return await this._checkReady(),await this._runExclusiveTransaction(async()=>{await a(this,t,h).call(this,"BEGIN"),d(this,p,!0);let e=!1,r=()=>{if(e)throw new Error("Transaction is closed")},o={query:async(i,c,u)=>(r(),await a(this,t,x).call(this,i,c,u)),sql:async(i,...c)=>{let{query:u,params:n}=R(i,...c);return await a(this,t,x).call(this,u,n)},exec:async(i,c)=>(r(),await a(this,t,h).call(this,i,c)),rollback:async()=>{r(),await a(this,t,h).call(this,"ROLLBACK"),e=!0},listen:async(i,c)=>(r(),await this.listen(i,c,o)),get closed(){return e}};try{let i=await s(o);return e||(e=!0,await a(this,t,h).call(this,"COMMIT")),d(this,p,!1),i}catch(i){throw e||await a(this,t,h).call(this,"ROLLBACK"),d(this,p,!1),i}})}async runExclusive(s){return await this._runExclusiveQuery(s)}};T=new WeakMap,p=new WeakMap,t=new WeakSet,y=async function(s,e={}){return await this.execProtocolStream(s,{...e,syncToFs:!1})},x=async function(s,e=[],r){return await this._runExclusiveQuery(async()=>{a(this,t,O).call(this,"runQuery",s,e,r),await this._handleBlob(r?.blob);let o=[];try{let c=await a(this,t,y).call(this,l.parse({text:s,types:r?.paramTypes}),r),u=z(await a(this,t,y).call(this,l.describe({type:"S"}),r)),n=e.map((b,S)=>{let D=u[S];if(b==null)return null;let Q=r?.serializers?.[D]??this.serializers[D];return Q?Q(b):b.toString()});o=[...c,...await a(this,t,y).call(this,l.bind({values:n}),r),...await a(this,t,y).call(this,l.describe({type:"P"}),r),...await a(this,t,y).call(this,l.execute({}),r)]}catch(c){throw c instanceof g?E({e:c,options:r,params:e,query:s}):c}finally{o.push(...await a(this,t,y).call(this,l.sync(),r))}await this._cleanupBlob(),P(this,p)||await this.syncToFs();let i=await this._getWrittenBlob();return _(o,this.parsers,r,i)[0]})},h=async function(s,e){return await this._runExclusiveQuery(async()=>{a(this,t,O).call(this,"runExec",s,e),await this._handleBlob(e?.blob);let r=[];try{r=await a(this,t,y).call(this,l.query(s),e)}catch(i){throw i instanceof g?E({e:i,options:e,params:void 0,query:s}):i}finally{r.push(...await a(this,t,y).call(this,l.sync(),e))}this._cleanupBlob(),P(this,p)||await this.syncToFs();let o=await this._getWrittenBlob();return _(r,this.parsers,e,o)})},O=function(...s){this.debug>0&&console.log(...s)};export{k as a}; //# sourceMappingURL=chunk-F2DQ4FIK.js.map