import{a as e,c as t,l as n,o as r,u as i}from"./chunk-BTBUZ646-DM3JdZYw.js";import{C as a,c as o,g as s,h as c,n as l,o as u,y as d}from"./chunk-EADU5A67-CVVwbce8.js";n();var f={part:`part`,container:`container`};function p(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 m(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===f.part){p(n,e[o],a.str),p(n.raw,e.raw[o],a.str);continue}if(a?._templateType===f.container){p(n,e[o],...a.strings),p(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 h(e,...t){let{strings:n,values:r}=m(e,...t);return{query:[n[0],...r.flatMap((e,t)=>[`$${t+1}`,n[t+1]])].join(``),params:r}}n();var g,_,v,y,b,x,S,C=class{constructor(){e(this,v),this.serializers={...s},this.parsers={...d},e(this,g,!1),e(this,_,!1)}async _initArrayTypes({force:e=!1}={}){if(t(this,g)&&!e)return;i(this,g,!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=>u(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,v,b).call(this,e,t,n))}async sql(e,...t){let{query:n,params:r}=h(e,...t);return await this.query(n,r)}async exec(e,t){return await this._checkReady(),await this._runExclusiveTransaction(async()=>await r(this,v,x).call(this,e,t))}async describeQuery(e,t){try{await r(this,v,y).call(this,o.parse({text:e,types:t?.paramTypes}),t);let n=await r(this,v,y).call(this,o.describe({type:`S`}),t),i=n.messages.find(e=>e.name===`parameterDescription`),a=n.messages.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]}))??[]}}finally{await r(this,v,y).call(this,o.sync(),t)}}async transaction(e){return await this._checkReady(),await this._runExclusiveTransaction(async()=>{await r(this,v,x).call(this,`BEGIN`),i(this,_,!0);let t=!1,n=()=>{if(t)throw Error(`Transaction is closed`)},a={query:async(e,t,i)=>(n(),await r(this,v,b).call(this,e,t,i)),sql:async(e,...t)=>{let{query:n,params:i}=h(e,...t);return await r(this,v,b).call(this,n,i)},exec:async(e,t)=>(n(),await r(this,v,x).call(this,e,t)),rollback:async()=>{n(),await r(this,v,x).call(this,`ROLLBACK`),t=!0},get closed(){return t}};try{let n=await e(a);return t||(t=!0,await r(this,v,x).call(this,`COMMIT`)),i(this,_,!1),n}catch(e){throw t||await r(this,v,x).call(this,`ROLLBACK`),i(this,_,!1),e}})}async runExclusive(e){return await this._runExclusiveQuery(e)}};g=new WeakMap,_=new WeakMap,v=new WeakSet,y=async function(e,t={}){return await this.execProtocol(e,{...t,syncToFs:!1})},b=async function(e,n=[],i){return await this._runExclusiveQuery(async()=>{r(this,v,S).call(this,`runQuery`,e,n,i),await this._handleBlob(i?.blob);let a;try{let{messages:t}=await r(this,v,y).call(this,o.parse({text:e,types:i?.paramTypes}),i),s=l((await r(this,v,y).call(this,o.describe({type:`S`}),i)).messages),c=n.map((e,t)=>{let n=s[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,v,y).call(this,o.bind({values:c}),i)).messages,...(await r(this,v,y).call(this,o.describe({type:`P`}),i)).messages,...(await r(this,v,y).call(this,o.execute({}),i)).messages]}finally{await r(this,v,y).call(this,o.sync(),i)}await this._cleanupBlob(),t(this,_)||await this.syncToFs();let s=await this._getWrittenBlob();return c(a,this.parsers,i,s)[0]})},x=async function(e,n){return await this._runExclusiveQuery(async()=>{r(this,v,S).call(this,`runExec`,e,n),await this._handleBlob(n?.blob);let i;try{i=(await r(this,v,y).call(this,o.query(e),n)).messages}finally{await r(this,v,y).call(this,o.sync(),n)}this._cleanupBlob(),t(this,_)||await this.syncToFs();let a=await this._getWrittenBlob();return c(i,this.parsers,n,a)})},S=function(...e){this.debug>0&&console.log(...e)};export{C as t};
//# sourceMappingURL=chunk-A7RFOIQ7-jlNKEM6a.js.map