import{a as e,c as t,l as n,o as r,u as i}from"./chunk-QY3QWFKW-D9_sMH3U.js";import{C as a,g as o,h as s,r as c,s as l,t as u,v as d,y as f}from"./chunk-3WWIVTCY-BYrk_Nvy.js";n();var p={part:`part`,container:`container`};function m(e,t,...n){let r=e.length-1,i=n.length-1;if(i!==-1){if(i===0){e[r]=e[r]+n[0]+t;return}e[r]=e[r]+n[0],e.push(...n.slice(1,i)),e.push(n[i]+t)}}function h(e,...t){let n=[e[0]];n.raw=[e.raw[0]];let r=[];for(let i=0;i<t.length;i++){let a=t[i],o=i+1;if(a?._templateType===p.part){m(n,e[o],a.str),m(n.raw,e.raw[o],a.str);continue}if(a?._templateType===p.container){m(n,e[o],...a.strings),m(n.raw,e.raw[o],...a.strings.raw),r.push(...a.values);continue}n.push(e[o]),n.raw.push(e.raw[o]),r.push(a)}return{_templateType:`container`,strings:n,values:r}}function g(e,...t){let{strings:n,values:r}=h(e,...t);return{query:[n[0],...r.flatMap((e,t)=>[`$${t+1}`,n[t+1]])].join(``),params:r}}n(),n();function _(e){let t=e.e;return t.query=e.query,t.params=e.params,t.queryOptions=e.options,t}var v,y,b,x,S,C,w,T=class{constructor(){e(this,b),this.serializers={...o},this.parsers={...f},e(this,v,!1),e(this,y,!1)}async _initArrayTypes({force:e=!1}={}){if(t(this,v)&&!e)return;i(this,v,!0);let n=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 e of n.rows)this.serializers[e.typarray]=t=>l(t,this.serializers[e.oid],e.typarray),this.parsers[e.typarray]=t=>a(t,this.parsers[e.oid],e.typarray)}async refreshArrayTypes(){await this._initArrayTypes({force:!0})}async query(e,t,n){return await this._checkReady(),await this._runExclusiveTransaction(async()=>await r(this,b,S).call(this,e,t,n))}async sql(e,...t){let{query:n,params:r}=g(e,...t);return await this.query(n,r)}async exec(e,t){return await this._checkReady(),await this._runExclusiveTransaction(async()=>await r(this,b,C).call(this,e,t))}async describeQuery(e,t){let n=[];try{await r(this,b,x).call(this,d.parse({text:e,types:t?.paramTypes}),t),n=await r(this,b,x).call(this,d.describe({type:`S`}),t)}catch(n){throw n instanceof u?_({e:n,options:t,params:void 0,query:e}):n}finally{n.push(...await r(this,b,x).call(this,d.sync(),t))}let i=n.find(e=>e.name===`parameterDescription`),a=n.find(e=>e.name===`rowDescription`);return{queryParams:i?.dataTypeIDs.map(e=>({dataTypeID:e,serializer:this.serializers[e]}))??[],resultFields:a?.fields.map(e=>({name:e.name,dataTypeID:e.dataTypeID,parser:this.parsers[e.dataTypeID]}))??[]}}async transaction(e){return await this._checkReady(),await this._runExclusiveTransaction(async()=>{await r(this,b,C).call(this,`BEGIN`),i(this,y,!0);let t=!1,n=()=>{if(t)throw Error(`Transaction is closed`)},a={query:async(e,t,i)=>(n(),await r(this,b,S).call(this,e,t,i)),sql:async(e,...t)=>{let{query:n,params:i}=g(e,...t);return await r(this,b,S).call(this,n,i)},exec:async(e,t)=>(n(),await r(this,b,C).call(this,e,t)),rollback:async()=>{n(),await r(this,b,C).call(this,`ROLLBACK`),t=!0},listen:async(e,t)=>(n(),await this.listen(e,t,a)),get closed(){return t}};try{let n=await e(a);return t||(t=!0,await r(this,b,C).call(this,`COMMIT`)),i(this,y,!1),n}catch(e){throw t||await r(this,b,C).call(this,`ROLLBACK`),i(this,y,!1),e}})}async runExclusive(e){return await this._runExclusiveQuery(e)}};v=new WeakMap,y=new WeakMap,b=new WeakSet,x=async function(e,t={}){return await this.execProtocolStream(e,{...t,syncToFs:!1})},S=async function(e,n=[],i){return await this._runExclusiveQuery(async()=>{r(this,b,w).call(this,`runQuery`,e,n,i),await this._handleBlob(i?.blob);let a=[];try{let t=await r(this,b,x).call(this,d.parse({text:e,types:i?.paramTypes}),i),o=c(await r(this,b,x).call(this,d.describe({type:`S`}),i)),s=n.map((e,t)=>{let n=o[t];if(e==null)return null;let r=i?.serializers?.[n]??this.serializers[n];return r?r(e):e.toString()});a=[...t,...await r(this,b,x).call(this,d.bind({values:s}),i),...await r(this,b,x).call(this,d.describe({type:`P`}),i),...await r(this,b,x).call(this,d.execute({}),i)]}catch(t){throw t instanceof u?_({e:t,options:i,params:n,query:e}):t}finally{a.push(...await r(this,b,x).call(this,d.sync(),i))}await this._cleanupBlob(),t(this,y)||await this.syncToFs();let o=await this._getWrittenBlob();return s(a,this.parsers,i,o)[0]})},C=async function(e,n){return await this._runExclusiveQuery(async()=>{r(this,b,w).call(this,`runExec`,e,n),await this._handleBlob(n?.blob);let i=[];try{i=await r(this,b,x).call(this,d.query(e),n)}catch(t){throw t instanceof u?_({e:t,options:n,params:void 0,query:e}):t}finally{i.push(...await r(this,b,x).call(this,d.sync(),n))}this._cleanupBlob(),t(this,y)||await this.syncToFs();let a=await this._getWrittenBlob();return s(i,this.parsers,n,a)})},w=function(...e){this.debug>0&&console.log(...e)};export{T as t};
//# sourceMappingURL=chunk-F2DQ4FIK-Hc2gUWci.js.map