import{b as Q,c as A,d as B,e as S,g as O,h as q,j as l,k as f}from"./chunk-2B24BK54.js";import{d as R}from"./chunk-F4GETNPB.js";import{e as T,f as w,g as d,h as a,j as g}from"./chunk-QY3QWFKW.js";g();g();function E(h){let s=h.e;return s.query=h.query,s.params=h.params,s.queryOptions=h.options,s}var P,p,t,y,x,m,_,z=class{constructor(){w(this,t);this.serializers={...A};this.parsers={...Q};w(this,P,!1);w(this,p,!1)}async _initArrayTypes({force:s=!1}={}){if(T(this,P)&&!s)return;d(this,P,!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=>B(o,this.serializers[r.oid],r.typarray),this.parsers[r.typarray]=o=>S(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,m).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 f?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,m).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,m).call(this,i,c)),rollback:async()=>{r(),await a(this,t,m).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,m).call(this,"COMMIT")),d(this,p,!1),i}catch(i){throw e||await a(this,t,m).call(this,"ROLLBACK"),d(this,p,!1),i}})}async runExclusive(s){return await this._runExclusiveQuery(s)}};P=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,_).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=q(await a(this,t,y).call(this,l.describe({type:"S"}),r)),n=e.map((b,k)=>{let D=u[k];if(b==null)return null;let v=r?.serializers?.[D]??this.serializers[D];return v?v(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 f?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(),T(this,p)||await this.syncToFs();let i=await this._getWrittenBlob();return O(o,this.parsers,r,i)[0]})},m=async function(s,e){return await this._runExclusiveQuery(async()=>{a(this,t,_).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 f?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(),T(this,p)||await this.syncToFs();let o=await this._getWrittenBlob();return O(r,this.parsers,e,o)})},_=function(...s){this.debug>0&&console.log(...s)};export{z as a};
//# sourceMappingURL=chunk-SRXPYZFS.js.map