@barishnamazov/gsql 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/cli.js +61 -61
  2. package/dist/index.js +13 -13
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,25 +1,25 @@
1
- import{CstParser as Ce}from"chevrotain";import{createToken as o,Lexer as w}from"chevrotain";var me=o({name:"WhiteSpace",pattern:/\s+/,group:w.SKIPPED}),ue=o({name:"LineComment",pattern:/\/\/[^\n]*/,group:w.SKIPPED}),Ee=o({name:"BlockComment",pattern:/\/\*[\s\S]*?\*\//,group:w.SKIPPED}),r=o({name:"Identifier",pattern:/[a-zA-Z_][a-zA-Z0-9_]*/}),x=o({name:"TemplateIdentifier",pattern:/\{[a-zA-Z_][a-zA-Z0-9_]*\}/}),F=o({name:"Concept",pattern:/concept/,longer_alt:r}),j=o({name:"Schema",pattern:/schema/,longer_alt:r}),z=o({name:"Mixin",pattern:/mixin/,longer_alt:r}),Y=o({name:"Enum",pattern:/enum/,longer_alt:r}),Q=o({name:"Extension",pattern:/extension/,longer_alt:r}),H=o({name:"Function",pattern:/function/,longer_alt:r}),K=o({name:"Func",pattern:/func/,longer_alt:r}),Z=o({name:"Trigger",pattern:/trigger/,longer_alt:r}),D=o({name:"Index",pattern:/index/,longer_alt:r}),R=o({name:"Check",pattern:/check/,longer_alt:r}),J=o({name:"Before",pattern:/before/,longer_alt:r}),V=o({name:"After",pattern:/after/,longer_alt:r}),W=o({name:"Ondelete",pattern:/ondelete/,longer_alt:r}),X=o({name:"On",pattern:/on/,longer_alt:r}),tt=o({name:"Each",pattern:/each/,longer_alt:r}),et=o({name:"Row",pattern:/row/,longer_alt:r}),nt=o({name:"Statement",pattern:/statement/,longer_alt:r}),it=o({name:"Execute",pattern:/execute/,longer_alt:r}),d=o({name:"Unique",pattern:/unique/,longer_alt:r}),I=o({name:"Gin",pattern:/gin/,longer_alt:r}),y=o({name:"Gist",pattern:/gist/,longer_alt:r}),v=o({name:"Btree",pattern:/btree/,longer_alt:r}),b=o({name:"Hash",pattern:/hash/,longer_alt:r}),st=o({name:"Pkey",pattern:/pkey/,longer_alt:r}),rt=o({name:"Nonull",pattern:/nonull/,longer_alt:r}),ot=o({name:"Default",pattern:/default/,longer_alt:r}),at=o({name:"Ref",pattern:/ref/,longer_alt:r}),ct=o({name:"Cascade",pattern:/cascade/,longer_alt:r}),lt=o({name:"Restrict",pattern:/restrict/,longer_alt:r}),ht=o({name:"SetNull",pattern:/setnull/,longer_alt:r}),pt=o({name:"SetDefault",pattern:/setdefault/,longer_alt:r}),mt=o({name:"NoAction",pattern:/noaction/,longer_alt:r}),ut=o({name:"Update",pattern:/update/,longer_alt:r}),Et=o({name:"Insert",pattern:/insert/,longer_alt:r}),St=o({name:"Delete",pattern:/delete/,longer_alt:r}),Ct=o({name:"Return",pattern:/return/,longer_alt:r}),k=o({name:"New",pattern:/NEW/,longer_alt:r}),B=o({name:"Old",pattern:/OLD/,longer_alt:r}),ft=o({name:"Serial",pattern:/serial/i,longer_alt:r}),Nt=o({name:"BigSerial",pattern:/bigserial/i,longer_alt:r}),Ut=o({name:"Integer",pattern:/integer/i,longer_alt:r}),Lt=o({name:"Bigint",pattern:/bigint/i,longer_alt:r}),gt=o({name:"SmallInt",pattern:/smallint/i,longer_alt:r}),Ot=o({name:"Text",pattern:/text/i,longer_alt:r}),Tt=o({name:"Varchar",pattern:/varchar/i,longer_alt:r}),dt=o({name:"Char",pattern:/char/i,longer_alt:r}),Mt=o({name:"Boolean",pattern:/boolean/i,longer_alt:r}),At=o({name:"Timestamptz",pattern:/timestamptz/i,longer_alt:r}),xt=o({name:"Timestamp",pattern:/timestamp/i,longer_alt:r}),Dt=o({name:"Date",pattern:/date/i,longer_alt:r}),Rt=o({name:"Time",pattern:/time/i,longer_alt:r}),It=o({name:"Jsonb",pattern:/jsonb/i,longer_alt:r}),yt=o({name:"Json",pattern:/json/i,longer_alt:r}),vt=o({name:"Uuid",pattern:/uuid/i,longer_alt:r}),bt=o({name:"Inet",pattern:/inet/i,longer_alt:r}),kt=o({name:"Citext",pattern:/citext/i,longer_alt:r}),Bt=o({name:"Decimal",pattern:/decimal/i,longer_alt:r}),_t=o({name:"Numeric",pattern:/numeric/i,longer_alt:r}),$t=o({name:"Real",pattern:/real/i,longer_alt:r}),qt=o({name:"DoublePrecision",pattern:/double\s+precision/i,longer_alt:r}),Pt=o({name:"Bytea",pattern:/bytea/i,longer_alt:r}),Gt=o({name:"StringLiteral",pattern:/'(?:[^'\\]|\\.)*'/}),_=o({name:"NumberLiteral",pattern:/-?\d+(?:\.\d+)?/}),wt=o({name:"BooleanLiteral",pattern:/true|false/,longer_alt:r}),Ft=o({name:"NullLiteral",pattern:/null/i,longer_alt:r}),Ht=o({name:"Arrow",pattern:/->/}),jt=o({name:"DoubleColon",pattern:/::/}),M=o({name:"LBrace",pattern:/\{/}),A=o({name:"RBrace",pattern:/\}/}),S=o({name:"LParen",pattern:/\(/}),C=o({name:"RParen",pattern:/\)/}),zt=o({name:"LBracket",pattern:/\[/}),Yt=o({name:"RBracket",pattern:/\]/}),$=o({name:"LAngle",pattern:/</}),q=o({name:"RAngle",pattern:/>/}),N=o({name:"Semicolon",pattern:/;/}),O=o({name:"Comma",pattern:/,/}),P=o({name:"Dot",pattern:/\./}),G=o({name:"Equals",pattern:/=/}),Kt=[me,ue,Ee,Ht,jt,F,j,z,Y,Q,H,K,Z,D,R,J,V,W,X,tt,et,nt,it,d,I,y,v,b,st,rt,ot,at,pt,ht,ct,lt,mt,ut,Et,St,Ct,k,B,qt,At,xt,Nt,ft,gt,Lt,Ut,Tt,dt,Ot,Mt,Dt,Rt,It,yt,vt,bt,kt,Bt,_t,$t,Pt,Gt,_,wt,Ft,x,r,M,A,S,C,zt,Yt,$,q,N,O,P,G],Se=new w(Kt,{ensureOptimizations:!0});function ie(i){return Se.tokenize(i)}var Vt=class extends Ce{constructor(){super(Kt,{recoveryEnabled:!0,maxLookahead:3}),this.performSelfAnalysis()}anyIdentifier=this.RULE("anyIdentifier",()=>{this.OR([{ALT:()=>this.CONSUME(r)},{ALT:()=>this.CONSUME(F)},{ALT:()=>this.CONSUME(j)},{ALT:()=>this.CONSUME(z)},{ALT:()=>this.CONSUME(Y)},{ALT:()=>this.CONSUME(Q)},{ALT:()=>this.CONSUME(K)},{ALT:()=>this.CONSUME(Z)},{ALT:()=>this.CONSUME(D)},{ALT:()=>this.CONSUME(R)},{ALT:()=>this.CONSUME(J)},{ALT:()=>this.CONSUME(V)},{ALT:()=>this.CONSUME(X)},{ALT:()=>this.CONSUME(tt)},{ALT:()=>this.CONSUME(et)},{ALT:()=>this.CONSUME(nt)},{ALT:()=>this.CONSUME(it)},{ALT:()=>this.CONSUME(H)},{ALT:()=>this.CONSUME(Ct)},{ALT:()=>this.CONSUME(k)},{ALT:()=>this.CONSUME(B)},{ALT:()=>this.CONSUME(ut)},{ALT:()=>this.CONSUME(Et)},{ALT:()=>this.CONSUME(St)},{ALT:()=>this.CONSUME(d)},{ALT:()=>this.CONSUME(st)},{ALT:()=>this.CONSUME(rt)},{ALT:()=>this.CONSUME(ot)},{ALT:()=>this.CONSUME(at)},{ALT:()=>this.CONSUME(W)},{ALT:()=>this.CONSUME(ct)},{ALT:()=>this.CONSUME(lt)},{ALT:()=>this.CONSUME(ht)},{ALT:()=>this.CONSUME(pt)},{ALT:()=>this.CONSUME(mt)},{ALT:()=>this.CONSUME(I)},{ALT:()=>this.CONSUME(y)},{ALT:()=>this.CONSUME(v)},{ALT:()=>this.CONSUME(b)},{ALT:()=>this.CONSUME(ft)},{ALT:()=>this.CONSUME(Nt)},{ALT:()=>this.CONSUME(Ut)},{ALT:()=>this.CONSUME(Lt)},{ALT:()=>this.CONSUME(gt)},{ALT:()=>this.CONSUME(Ot)},{ALT:()=>this.CONSUME(Tt)},{ALT:()=>this.CONSUME(dt)},{ALT:()=>this.CONSUME(Mt)},{ALT:()=>this.CONSUME(At)},{ALT:()=>this.CONSUME(xt)},{ALT:()=>this.CONSUME(Dt)},{ALT:()=>this.CONSUME(Rt)},{ALT:()=>this.CONSUME(It)},{ALT:()=>this.CONSUME(yt)},{ALT:()=>this.CONSUME(vt)},{ALT:()=>this.CONSUME(bt)},{ALT:()=>this.CONSUME(kt)},{ALT:()=>this.CONSUME(Bt)},{ALT:()=>this.CONSUME(_t)},{ALT:()=>this.CONSUME($t)},{ALT:()=>this.CONSUME(qt)},{ALT:()=>this.CONSUME(Pt)}])});program=this.RULE("program",()=>{this.MANY(()=>{this.SUBRULE(this.topLevelDeclaration)})});topLevelDeclaration=this.RULE("topLevelDeclaration",()=>{this.OR([{ALT:()=>this.SUBRULE(this.extensionDecl)},{ALT:()=>this.SUBRULE(this.functionDecl)},{ALT:()=>this.SUBRULE(this.conceptDecl)},{ALT:()=>this.SUBRULE(this.enumDecl)},{ALT:()=>this.SUBRULE(this.schemaDecl)},{ALT:()=>this.SUBRULE(this.instantiation)},{ALT:()=>this.SUBRULE(this.perInstanceIndex)}])});extensionDecl=this.RULE("extensionDecl",()=>{this.CONSUME(Q),this.SUBRULE(this.anyIdentifier),this.CONSUME(N)});functionDecl=this.RULE("functionDecl",()=>{this.CONSUME(K),this.SUBRULE(this.anyIdentifier),this.CONSUME(S),this.CONSUME(C),this.CONSUME(Ht),this.SUBRULE2(this.anyIdentifier),this.SUBRULE(this.functionBody)});functionBody=this.RULE("functionBody",()=>{this.CONSUME(M),this.MANY(()=>{this.SUBRULE(this.functionStatement)}),this.CONSUME(A)});functionStatement=this.RULE("functionStatement",()=>{this.OR([{ALT:()=>this.SUBRULE(this.returnStatement)},{ALT:()=>this.SUBRULE(this.assignmentStatement)}])});returnStatement=this.RULE("returnStatement",()=>{this.CONSUME(Ct),this.OR([{ALT:()=>this.CONSUME(k)},{ALT:()=>this.CONSUME(B)},{ALT:()=>this.CONSUME(r)}]),this.CONSUME(N)});assignmentStatement=this.RULE("assignmentStatement",()=>{this.OR([{ALT:()=>this.CONSUME(k)},{ALT:()=>this.CONSUME(B)},{ALT:()=>this.CONSUME(r)}]),this.CONSUME(P),this.SUBRULE(this.anyIdentifier),this.CONSUME(G),this.SUBRULE(this.functionCallExpr),this.CONSUME(N)});functionCallExpr=this.RULE("functionCallExpr",()=>{this.CONSUME(r),this.CONSUME(S),this.CONSUME(C)});conceptDecl=this.RULE("conceptDecl",()=>{this.CONSUME(F),this.CONSUME(r),this.OPTION(()=>{this.SUBRULE(this.typeParamList)}),this.CONSUME(M),this.MANY(()=>{this.OR([{ALT:()=>this.SUBRULE(this.enumDecl)},{ALT:()=>this.SUBRULE(this.schemaDecl)}])}),this.CONSUME(A)});typeParamList=this.RULE("typeParamList",()=>{this.CONSUME($),this.CONSUME(r),this.MANY(()=>{this.CONSUME(O),this.CONSUME2(r)}),this.CONSUME(q)});enumDecl=this.RULE("enumDecl",()=>{this.CONSUME(Y),this.SUBRULE(this.anyIdentifier),this.CONSUME(M),this.MANY(()=>{this.SUBRULE2(this.anyIdentifier),this.CONSUME(N)}),this.CONSUME(A)});schemaDecl=this.RULE("schemaDecl",()=>{this.CONSUME(j),this.CONSUME(r),this.OPTION(()=>{this.SUBRULE(this.mixinList)}),this.CONSUME(M),this.MANY(()=>{this.SUBRULE(this.schemaBodyItem)}),this.CONSUME(A)});mixinList=this.RULE("mixinList",()=>{this.CONSUME(z),this.CONSUME(r),this.MANY(()=>{this.CONSUME(O),this.CONSUME2(r)})});schemaBodyItem=this.RULE("schemaBodyItem",()=>{this.OR([{ALT:()=>this.SUBRULE(this.indexDef)},{ALT:()=>this.SUBRULE(this.checkDef)},{ALT:()=>this.SUBRULE(this.triggerDef)},{ALT:()=>this.SUBRULE(this.columnDef)}])});columnDef=this.RULE("columnDef",()=>{this.SUBRULE(this.columnName),this.SUBRULE(this.dataType),this.MANY(()=>{this.SUBRULE(this.columnConstraint)}),this.CONSUME(N)});columnName=this.RULE("columnName",()=>{this.OR([{ALT:()=>{this.CONSUME(x),this.OPTION(()=>{this.CONSUME(r)})}},{ALT:()=>this.CONSUME2(r)}])});dataType=this.RULE("dataType",()=>{this.OR([{ALT:()=>this.CONSUME(ft)},{ALT:()=>this.CONSUME(Nt)},{ALT:()=>this.CONSUME(Ut)},{ALT:()=>this.CONSUME(Lt)},{ALT:()=>this.CONSUME(gt)},{ALT:()=>this.CONSUME(Ot)},{ALT:()=>this.CONSUME(Tt)},{ALT:()=>this.CONSUME(dt)},{ALT:()=>this.CONSUME(Mt)},{ALT:()=>this.CONSUME(At)},{ALT:()=>this.CONSUME(xt)},{ALT:()=>this.CONSUME(Dt)},{ALT:()=>this.CONSUME(Rt)},{ALT:()=>this.CONSUME(It)},{ALT:()=>this.CONSUME(yt)},{ALT:()=>this.CONSUME(vt)},{ALT:()=>this.CONSUME(bt)},{ALT:()=>this.CONSUME(kt)},{ALT:()=>this.CONSUME(Bt)},{ALT:()=>this.CONSUME(_t)},{ALT:()=>this.CONSUME($t)},{ALT:()=>this.CONSUME(qt)},{ALT:()=>this.CONSUME(Pt)},{ALT:()=>this.CONSUME(r)}]),this.OPTION(()=>{this.CONSUME(S),this.CONSUME(_),this.CONSUME(C)})});columnConstraint=this.RULE("columnConstraint",()=>{this.OR([{ALT:()=>this.CONSUME(st)},{ALT:()=>this.CONSUME(rt)},{ALT:()=>this.CONSUME(d)},{ALT:()=>this.SUBRULE(this.defaultConstraint)},{ALT:()=>this.SUBRULE(this.refConstraint)},{ALT:()=>this.SUBRULE(this.checkConstraint)},{ALT:()=>this.SUBRULE(this.onDeleteConstraint)}])});defaultConstraint=this.RULE("defaultConstraint",()=>{this.CONSUME(ot),this.CONSUME(S),this.SUBRULE(this.defaultValue),this.CONSUME(C)});defaultValue=this.RULE("defaultValue",()=>{this.OR([{ALT:()=>this.CONSUME(Gt)},{ALT:()=>this.CONSUME(_)},{ALT:()=>this.CONSUME(wt)},{ALT:()=>this.CONSUME(Ft)},{ALT:()=>this.SUBRULE(this.functionCallExpr)},{ALT:()=>{this.SUBRULE(this.anyIdentifier),this.CONSUME(jt),this.SUBRULE2(this.anyIdentifier)}},{ALT:()=>this.SUBRULE3(this.anyIdentifier)}])});refConstraint=this.RULE("refConstraint",()=>{this.CONSUME(at),this.CONSUME(S),this.CONSUME(r),this.CONSUME(P),this.CONSUME2(r),this.CONSUME(C)});checkConstraint=this.RULE("checkConstraint",()=>{this.CONSUME(R),this.CONSUME(S),this.SUBRULE(this.checkExpression),this.CONSUME(C)});checkExpression=this.RULE("checkExpression",()=>{this.MANY(()=>{this.OR([{ALT:()=>{this.CONSUME(S),this.SUBRULE(this.checkExpression),this.CONSUME(C)}},{ALT:()=>this.CONSUME(r)},{ALT:()=>this.CONSUME(x)},{ALT:()=>this.CONSUME(_)},{ALT:()=>this.CONSUME(Gt)},{ALT:()=>this.CONSUME(wt)},{ALT:()=>this.CONSUME(Ft)},{ALT:()=>this.CONSUME(jt)},{ALT:()=>this.CONSUME(G)},{ALT:()=>this.CONSUME(O)},{ALT:()=>this.CONSUME(P)},{ALT:()=>this.CONSUME($)},{ALT:()=>this.CONSUME(q)}])})});onDeleteConstraint=this.RULE("onDeleteConstraint",()=>{this.CONSUME(W),this.CONSUME(S),this.OR([{ALT:()=>this.CONSUME(ct)},{ALT:()=>this.CONSUME(lt)},{ALT:()=>this.CONSUME(ht)},{ALT:()=>this.CONSUME(pt)},{ALT:()=>this.CONSUME(mt)}]),this.CONSUME(C)});indexDef=this.RULE("indexDef",()=>{this.CONSUME(D),this.CONSUME(S),this.SUBRULE(this.indexColumnList),this.CONSUME(C),this.MANY(()=>{this.OR([{ALT:()=>this.CONSUME(d)},{ALT:()=>this.CONSUME(I)},{ALT:()=>this.CONSUME(y)},{ALT:()=>this.CONSUME(v)},{ALT:()=>this.CONSUME(b)}])}),this.CONSUME(N)});indexColumnList=this.RULE("indexColumnList",()=>{this.SUBRULE(this.indexColumn),this.MANY(()=>{this.CONSUME(O),this.SUBRULE2(this.indexColumn)})});indexColumn=this.RULE("indexColumn",()=>{this.OR([{ALT:()=>this.CONSUME(x)},{ALT:()=>this.CONSUME(r)}]),this.OPTION(()=>{this.CONSUME2(r)})});checkDef=this.RULE("checkDef",()=>{this.CONSUME(R),this.CONSUME(S),this.SUBRULE(this.checkExpression),this.CONSUME(C),this.CONSUME(N)});triggerDef=this.RULE("triggerDef",()=>{this.CONSUME(Z),this.CONSUME(r),this.OR([{ALT:()=>this.CONSUME(J)},{ALT:()=>this.CONSUME(V)}]),this.OR2([{ALT:()=>this.CONSUME(ut)},{ALT:()=>this.CONSUME(Et)},{ALT:()=>this.CONSUME(St)}]),this.CONSUME(X),this.CONSUME(tt),this.OR3([{ALT:()=>this.CONSUME(et)},{ALT:()=>this.CONSUME(nt)}]),this.CONSUME(it),this.CONSUME(H),this.CONSUME2(r),this.CONSUME(S),this.CONSUME(C),this.CONSUME(N)});instantiation=this.RULE("instantiation",()=>{this.SUBRULE(this.instantiationTargetList),this.CONSUME(G),this.SUBRULE(this.conceptReference),this.CONSUME(N)});instantiationTargetList=this.RULE("instantiationTargetList",()=>{this.SUBRULE(this.instantiationTarget),this.MANY(()=>{this.CONSUME(O),this.SUBRULE2(this.instantiationTarget)})});instantiationTarget=this.RULE("instantiationTarget",()=>{this.CONSUME(r),this.OPTION(()=>{this.CONSUME(zt),this.CONSUME2(r),this.CONSUME(Yt)})});conceptReference=this.RULE("conceptReference",()=>{this.CONSUME(r),this.OPTION(()=>{this.CONSUME($),this.SUBRULE(this.typeArgList),this.CONSUME(q)})});typeArgList=this.RULE("typeArgList",()=>{this.SUBRULE(this.typeArg),this.MANY(()=>{this.CONSUME(O),this.SUBRULE2(this.typeArg)})});typeArg=this.RULE("typeArg",()=>{this.CONSUME(r),this.OPTION(()=>{this.CONSUME(zt),this.CONSUME2(r),this.CONSUME(Yt)})});perInstanceIndex=this.RULE("perInstanceIndex",()=>{this.CONSUME(D),this.CONSUME(S),this.CONSUME(r),this.CONSUME(O),this.CONSUME2(r),this.MANY(()=>{this.CONSUME2(O),this.CONSUME3(r)}),this.CONSUME(C),this.MANY2(()=>{this.OR([{ALT:()=>this.CONSUME(d)},{ALT:()=>this.CONSUME(I)},{ALT:()=>this.CONSUME(y)},{ALT:()=>this.CONSUME(v)},{ALT:()=>this.CONSUME(b)}])}),this.CONSUME(N)})},Zt=new Vt;function u(i,t){let e=i.children[t];if(e&&e.length>0){let s=e[0];if(s&&"image"in s)return s.image}return""}function T(i,t){let e=i.children[t];return e?e.map(s=>"image"in s?s.image:"").filter(s=>s!==""):[]}function m(i,t){let s=i.children[t]?.[0];if(s&&"children"in s)return s}function L(i,t){let e=i.children[t];return e?e.filter(s=>"children"in s):[]}function se(i){let e=L(i,"anyIdentifier")[0];if(e)for(let s of Object.keys(e.children)){let l=e.children[s]?.[0];if(l&&"image"in l)return l.image}return""}function Jt(i){let t=L(i,"anyIdentifier"),e=[];for(let s of t)for(let a of Object.keys(s.children)){let n=s.children[a]?.[0];if(n&&"image"in n){e.push(n.image);break}}return e}var Wt=class{visit(t){let e=[],s=L(t,"topLevelDeclaration");for(let a of s){let l=this.visitTopLevelDeclaration(a);l&&e.push(l)}return{type:"Program",declarations:e}}visitTopLevelDeclaration(t){let e=m(t,"extensionDecl");if(e)return this.visitExtensionDecl(e);let s=m(t,"functionDecl");if(s)return this.visitFunctionDecl(s);let a=m(t,"conceptDecl");if(a)return this.visitConceptDecl(a);let l=m(t,"enumDecl");if(l)return this.visitEnumDecl(l);let n=m(t,"schemaDecl");if(n)return this.visitSchemaDecl(n);let c=m(t,"instantiation");if(c)return this.visitInstantiation(c);let h=m(t,"perInstanceIndex");return h?this.visitPerInstanceIndex(h):null}visitExtensionDecl(t){return{type:"ExtensionDecl",name:se(t)}}visitFunctionDecl(t){let e=Jt(t),s=e[0]??"",a=e[1]??"",l=m(t,"functionBody"),n="";if(l){let c=L(l,"functionStatement"),h=[];for(let p of c){let f=m(p,"returnStatement");if(f){let E=u(f,"Identifier");E||(E=u(f,"New")),E||(E=u(f,"Old")),h.push(`RETURN ${E};`)}let g=m(p,"assignmentStatement");if(g){let E=u(g,"Identifier");E||(E=u(g,"New")),E||(E=u(g,"Old"));let Qt=se(g),U=m(g,"functionCallExpr"),pe=U?u(U,"Identifier"):"";h.push(`${E}.${Qt} := ${pe}();`)}}n=h.join(`
2
- `)}return{type:"FunctionDecl",name:s,returnType:a,body:n}}visitConceptDecl(t){let e=u(t,"Identifier"),s=[],a=m(t,"typeParamList");a&&s.push(...T(a,"Identifier"));let l=[],n=L(t,"schemaDecl");for(let h of n){let p=this.visitSchemaDecl(h);p.conceptScope=e,l.push(p)}let c=L(t,"enumDecl");for(let h of c){let p=this.visitEnumDecl(h);p.conceptScope=e,l.push(p)}return{type:"ConceptDecl",name:e,typeParams:s,members:l}}visitEnumDecl(t){let e=Jt(t),s=e[0]??"",a=e.slice(1);return{type:"EnumDecl",name:s,values:a}}visitSchemaDecl(t){let e=u(t,"Identifier"),s=[],a=m(t,"mixinList");a&&s.push(...T(a,"Identifier"));let l=[],n=L(t,"schemaBodyItem");for(let c of n){let h=this.visitSchemaBodyItem(c);h&&l.push(h)}return{type:"SchemaDecl",name:e,mixins:s,members:l}}visitSchemaBodyItem(t){let e=m(t,"columnDef");if(e)return this.visitColumnDef(e);let s=m(t,"indexDef");if(s)return this.visitIndexDef(s);let a=m(t,"checkDef");if(a)return this.visitCheckDef(a);let l=m(t,"triggerDef");return l?this.visitTriggerDef(l):null}visitColumnDef(t){let e=m(t,"columnName"),s="";if(e){let h=u(e,"TemplateIdentifier"),p=u(e,"Identifier");s=h?h+p:p}let a=m(t,"dataType"),l="";if(a){for(let f of Object.keys(a.children)){let E=a.children[f]?.[0];if(E&&"image"in E){l=E.image;break}}let p=a.children.NumberLiteral?.[0];p&&"image"in p&&(l+=`(${p.image})`)}let n=[],c=L(t,"columnConstraint");for(let h of c){let p=this.visitColumnConstraint(h);p&&n.push(p)}return{type:"ColumnDef",name:s,dataType:l,constraints:n}}visitColumnConstraint(t){if(t.children.Pkey)return{type:"PrimaryKey"};if(t.children.Nonull)return{type:"NotNull"};if(t.children.Unique)return{type:"Unique"};let e=m(t,"defaultConstraint");if(e){let n=m(e,"defaultValue"),c="";if(n){let h=u(n,"StringLiteral"),p=u(n,"NumberLiteral"),f=u(n,"BooleanLiteral"),g=u(n,"NullLiteral"),E=m(n,"functionCallExpr"),Qt=u(n,"DoubleColon"),U=Jt(n);h?c=h:p?c=p:f?c=f:g?c=g:E?c=u(E,"Identifier")+"()":Qt&&U.length===2&&U[0]&&U[1]?c=`${U[0]}::${U[1]}`:U.length>0&&U[0]&&(c=U[0])}return{type:"Default",value:c}}let s=m(t,"refConstraint");if(s){let n=T(s,"Identifier");return{type:"Reference",table:n[0]??"",column:n[1]??""}}let a=m(t,"checkConstraint");if(a){let n=m(a,"checkExpression");return{type:"Check",value:n?this.reconstructCheckExpression(n):""}}let l=m(t,"onDeleteConstraint");if(l){let n={Cascade:"CASCADE",Restrict:"RESTRICT",SetNull:"SET NULL",SetDefault:"SET DEFAULT",NoAction:"NO ACTION"};for(let[c,h]of Object.entries(n))if(l.children[c])return{type:"OnDelete",action:h}}return null}reconstructCheckExpression(t){let e=[];for(let s of Object.keys(t.children)){let a=t.children[s];if(a){for(let l of a)if("image"in l)e.push(l.image);else if("children"in l&&s==="checkExpression"){let n=this.reconstructCheckExpression(l);n&&e.push(`(${n})`)}}}return e.join(" ")}visitIndexDef(t){let e=[],s=m(t,"indexColumnList");if(s){let n=L(s,"indexColumn");for(let c of n){let h=u(c,"TemplateIdentifier"),p=u(c,"Identifier");e.push(h?h+p:p)}}let a=t.children.Unique!==void 0,l;return t.children.Gin&&(l="gin"),t.children.Gist&&(l="gist"),t.children.Btree&&(l="btree"),t.children.Hash&&(l="hash"),{type:"IndexDef",columns:e,unique:a,using:l}}visitCheckDef(t){let e=m(t,"checkExpression");return{type:"CheckDef",expression:e?this.reconstructCheckExpression(e):""}}visitTriggerDef(t){let e=T(t,"Identifier"),s=e[0]??"",a=e[1]??"",l=t.children.Before?"before":"after",n="update";t.children.Insert&&(n="insert"),t.children.Delete&&(n="delete");let c=t.children.Row?"row":"statement";return{type:"TriggerDef",name:s,timing:l,event:n,forEach:c,executeFunction:a}}visitInstantiation(t){let e=[],s=m(t,"instantiationTargetList");if(s){let c=L(s,"instantiationTarget");for(let h of c){let p=T(h,"Identifier");e.push({tableName:p[0]??"",alias:p[1]})}}let a=m(t,"conceptReference"),l="",n=[];if(a){l=u(a,"Identifier");let c=m(a,"typeArgList");if(c){let h=L(c,"typeArg");for(let p of h){let f=T(p,"Identifier");n.push({tableName:f[0]??"",alias:f[1]})}}}return{type:"Instantiation",targets:e,conceptName:l,typeArgs:n}}visitPerInstanceIndex(t){let e=T(t,"Identifier"),s=e[0]??"",a=e.slice(1),l=t.children.Unique!==void 0,n;return t.children.Gin&&(n="gin"),t.children.Gist&&(n="gist"),t.children.Btree&&(n="btree"),t.children.Hash&&(n="hash"),{type:"PerInstanceIndex",tableName:s,columns:a,unique:l,using:n}}};function Xt(i){let t=ie(i),e=[];for(let n of t.errors)e.push({message:n.message,location:{start:{line:n.line??0,column:n.column??0,offset:n.offset},end:{line:n.line??0,column:n.column??0,offset:n.offset}},severity:"error"});if(e.length>0)return{ast:null,errors:e};Zt.input=t.tokens;let s=Zt.program();for(let n of Zt.errors){let c=n.token;e.push({message:n.message,location:{start:{line:c.startLine??0,column:c.startColumn??0,offset:c.startOffset},end:{line:c.endLine??0,column:c.endColumn??0,offset:c.endOffset??c.startOffset}},severity:"error"})}return e.length>0?{ast:null,errors:e}:{ast:new Wt().visit(s),errors:e}}function te(i){return i.replace(/([A-Z]+)([A-Z][a-z])/g,"$1_$2").replace(/([a-z\d])([A-Z])/g,"$1_$2").toLowerCase()}function fe(){return{concepts:new Map,schemas:new Map,enums:new Map,tableToSchema:new Map,templateSubs:new Map,enumSql:[],tableSql:[],indexSql:[],triggerSql:[],extensionSql:[],functionSql:[],perInstanceIndexSql:[],generatedEnums:new Set,generatedTables:new Set}}function Ne(i,t){for(let e of i.declarations)switch(e.type){case"ConceptDecl":t.concepts.set(e.name,e);break;case"SchemaDecl":t.schemas.set(e.name,e);break;case"EnumDecl":t.enums.set(e.name,e);break}}function ee(i,t){let e=i.match(/^\{([^}]+)\}(.*)$/);if(e){let[,s,a]=e,l;for(let[n,c]of t.templateSubs)if(n===s){l=c;break}return(l??te(s??""))+(a??"")}return i}function Ue(i){return i.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function ae(i,t){let e=i;for(let[s,a]of t.templateSubs){let l=Ue(s),n=new RegExp(`\\{${l}\\}`,"g");e=e.replace(n,a)}return e=e.replace(/(\w+)::(\w+)/g,"'$2'::$1"),e}function ce(i,t){let e=i.name;if(t.generatedEnums.has(e))return;t.generatedEnums.add(e);let s=i.values.map(a=>`'${a}'`).join(", ");t.enumSql.push(`DO $$ BEGIN
3
- CREATE TYPE ${e} AS ENUM (${s});
1
+ import{CstParser as Ue}from"chevrotain";import{createToken as c,Lexer as F}from"chevrotain";var fe=c({name:"WhiteSpace",pattern:/\s+/,group:F.SKIPPED}),Ee=c({name:"LineComment",pattern:/\/\/[^\n]*/,group:F.SKIPPED}),Se=c({name:"BlockComment",pattern:/\/\*[\s\S]*?\*\//,group:F.SKIPPED}),r=c({name:"Identifier",pattern:/[a-zA-Z_][a-zA-Z0-9_]*/}),D=c({name:"TemplateIdentifier",pattern:/\{[a-zA-Z_][a-zA-Z0-9_]*\}/}),j=c({name:"Concept",pattern:/concept/,longer_alt:r}),z=c({name:"Schema",pattern:/schema/,longer_alt:r}),Y=c({name:"Mixin",pattern:/mixin/,longer_alt:r}),Q=c({name:"Enum",pattern:/enum/,longer_alt:r}),H=c({name:"Extension",pattern:/extension/,longer_alt:r}),K=c({name:"Function",pattern:/function/,longer_alt:r}),V=c({name:"Func",pattern:/func/,longer_alt:r}),Z=c({name:"Trigger",pattern:/trigger/,longer_alt:r}),R=c({name:"Index",pattern:/index/,longer_alt:r}),I=c({name:"Check",pattern:/check/,longer_alt:r}),J=c({name:"Before",pattern:/before/,longer_alt:r}),W=c({name:"After",pattern:/after/,longer_alt:r}),X=c({name:"Ondelete",pattern:/ondelete/,longer_alt:r}),tt=c({name:"On",pattern:/on/,longer_alt:r}),et=c({name:"Each",pattern:/each/,longer_alt:r}),nt=c({name:"Row",pattern:/row/,longer_alt:r}),st=c({name:"Statement",pattern:/statement/,longer_alt:r}),it=c({name:"Execute",pattern:/execute/,longer_alt:r}),T=c({name:"Unique",pattern:/unique/,longer_alt:r}),y=c({name:"Gin",pattern:/gin/,longer_alt:r}),v=c({name:"Gist",pattern:/gist/,longer_alt:r}),b=c({name:"Btree",pattern:/btree/,longer_alt:r}),k=c({name:"Hash",pattern:/hash/,longer_alt:r}),rt=c({name:"Pkey",pattern:/pkey/,longer_alt:r}),ot=c({name:"Nonull",pattern:/nonull/,longer_alt:r}),at=c({name:"Default",pattern:/default/,longer_alt:r}),ct=c({name:"Ref",pattern:/ref/,longer_alt:r}),lt=c({name:"Cascade",pattern:/cascade/,longer_alt:r}),ht=c({name:"Restrict",pattern:/restrict/,longer_alt:r}),pt=c({name:"SetNull",pattern:/setnull/,longer_alt:r}),mt=c({name:"SetDefault",pattern:/setdefault/,longer_alt:r}),ut=c({name:"NoAction",pattern:/noaction/,longer_alt:r}),ft=c({name:"Update",pattern:/update/,longer_alt:r}),Et=c({name:"Insert",pattern:/insert/,longer_alt:r}),St=c({name:"Delete",pattern:/delete/,longer_alt:r}),Ct=c({name:"Return",pattern:/return/,longer_alt:r}),B=c({name:"New",pattern:/NEW/,longer_alt:r}),_=c({name:"Old",pattern:/OLD/,longer_alt:r}),Ut=c({name:"Serial",pattern:/serial/i,longer_alt:r}),Nt=c({name:"BigSerial",pattern:/bigserial/i,longer_alt:r}),Lt=c({name:"Integer",pattern:/integer/i,longer_alt:r}),gt=c({name:"Bigint",pattern:/bigint/i,longer_alt:r}),Ot=c({name:"SmallInt",pattern:/smallint/i,longer_alt:r}),dt=c({name:"Text",pattern:/text/i,longer_alt:r}),Tt=c({name:"Varchar",pattern:/varchar/i,longer_alt:r}),Mt=c({name:"Char",pattern:/char/i,longer_alt:r}),xt=c({name:"Boolean",pattern:/boolean/i,longer_alt:r}),At=c({name:"Timestamptz",pattern:/timestamptz/i,longer_alt:r}),Dt=c({name:"Timestamp",pattern:/timestamp/i,longer_alt:r}),Rt=c({name:"Date",pattern:/date/i,longer_alt:r}),It=c({name:"Time",pattern:/time/i,longer_alt:r}),yt=c({name:"Jsonb",pattern:/jsonb/i,longer_alt:r}),vt=c({name:"Json",pattern:/json/i,longer_alt:r}),bt=c({name:"Uuid",pattern:/uuid/i,longer_alt:r}),kt=c({name:"Inet",pattern:/inet/i,longer_alt:r}),Bt=c({name:"Citext",pattern:/citext/i,longer_alt:r}),_t=c({name:"Decimal",pattern:/decimal/i,longer_alt:r}),Pt=c({name:"Numeric",pattern:/numeric/i,longer_alt:r}),qt=c({name:"Real",pattern:/real/i,longer_alt:r}),$t=c({name:"DoublePrecision",pattern:/double\s+precision/i,longer_alt:r}),Gt=c({name:"Bytea",pattern:/bytea/i,longer_alt:r}),wt=c({name:"StringLiteral",pattern:/'(?:[^'\\]|\\.)*'/}),P=c({name:"NumberLiteral",pattern:/-?\d+(?:\.\d+)?/}),Ft=c({name:"BooleanLiteral",pattern:/true|false/,longer_alt:r}),jt=c({name:"NullLiteral",pattern:/null/i,longer_alt:r}),Ht=c({name:"Arrow",pattern:/->/}),zt=c({name:"DoubleColon",pattern:/::/}),M=c({name:"LBrace",pattern:/\{/}),x=c({name:"RBrace",pattern:/\}/}),C=c({name:"LParen",pattern:/\(/}),U=c({name:"RParen",pattern:/\)/}),Yt=c({name:"LBracket",pattern:/\[/}),Qt=c({name:"RBracket",pattern:/\]/}),q=c({name:"LAngle",pattern:/</}),$=c({name:"RAngle",pattern:/>/}),L=c({name:"Semicolon",pattern:/;/}),O=c({name:"Comma",pattern:/,/}),G=c({name:"Dot",pattern:/\./}),w=c({name:"Equals",pattern:/=/}),Kt=[fe,Ee,Se,Ht,zt,j,z,Y,Q,H,K,V,Z,R,I,J,W,X,tt,et,nt,st,it,T,y,v,b,k,rt,ot,at,ct,mt,pt,lt,ht,ut,ft,Et,St,Ct,B,_,$t,At,Dt,Nt,Ut,Ot,gt,Lt,Tt,Mt,dt,xt,Rt,It,yt,vt,bt,kt,Bt,_t,Pt,qt,Gt,wt,P,Ft,jt,D,r,M,x,C,U,Yt,Qt,q,$,L,O,G,w],Ce=new F(Kt,{ensureOptimizations:!0});function ne(s){return Ce.tokenize(s)}var Jt=class extends Ue{constructor(){super(Kt,{recoveryEnabled:!0,maxLookahead:3}),this.performSelfAnalysis()}anyIdentifier=this.RULE("anyIdentifier",()=>{this.OR([{ALT:()=>this.CONSUME(r)},{ALT:()=>this.CONSUME(j)},{ALT:()=>this.CONSUME(z)},{ALT:()=>this.CONSUME(Y)},{ALT:()=>this.CONSUME(Q)},{ALT:()=>this.CONSUME(H)},{ALT:()=>this.CONSUME(V)},{ALT:()=>this.CONSUME(Z)},{ALT:()=>this.CONSUME(R)},{ALT:()=>this.CONSUME(I)},{ALT:()=>this.CONSUME(J)},{ALT:()=>this.CONSUME(W)},{ALT:()=>this.CONSUME(tt)},{ALT:()=>this.CONSUME(et)},{ALT:()=>this.CONSUME(nt)},{ALT:()=>this.CONSUME(st)},{ALT:()=>this.CONSUME(it)},{ALT:()=>this.CONSUME(K)},{ALT:()=>this.CONSUME(Ct)},{ALT:()=>this.CONSUME(B)},{ALT:()=>this.CONSUME(_)},{ALT:()=>this.CONSUME(ft)},{ALT:()=>this.CONSUME(Et)},{ALT:()=>this.CONSUME(St)},{ALT:()=>this.CONSUME(T)},{ALT:()=>this.CONSUME(rt)},{ALT:()=>this.CONSUME(ot)},{ALT:()=>this.CONSUME(at)},{ALT:()=>this.CONSUME(ct)},{ALT:()=>this.CONSUME(X)},{ALT:()=>this.CONSUME(lt)},{ALT:()=>this.CONSUME(ht)},{ALT:()=>this.CONSUME(pt)},{ALT:()=>this.CONSUME(mt)},{ALT:()=>this.CONSUME(ut)},{ALT:()=>this.CONSUME(y)},{ALT:()=>this.CONSUME(v)},{ALT:()=>this.CONSUME(b)},{ALT:()=>this.CONSUME(k)},{ALT:()=>this.CONSUME(Ut)},{ALT:()=>this.CONSUME(Nt)},{ALT:()=>this.CONSUME(Lt)},{ALT:()=>this.CONSUME(gt)},{ALT:()=>this.CONSUME(Ot)},{ALT:()=>this.CONSUME(dt)},{ALT:()=>this.CONSUME(Tt)},{ALT:()=>this.CONSUME(Mt)},{ALT:()=>this.CONSUME(xt)},{ALT:()=>this.CONSUME(At)},{ALT:()=>this.CONSUME(Dt)},{ALT:()=>this.CONSUME(Rt)},{ALT:()=>this.CONSUME(It)},{ALT:()=>this.CONSUME(yt)},{ALT:()=>this.CONSUME(vt)},{ALT:()=>this.CONSUME(bt)},{ALT:()=>this.CONSUME(kt)},{ALT:()=>this.CONSUME(Bt)},{ALT:()=>this.CONSUME(_t)},{ALT:()=>this.CONSUME(Pt)},{ALT:()=>this.CONSUME(qt)},{ALT:()=>this.CONSUME($t)},{ALT:()=>this.CONSUME(Gt)}])});program=this.RULE("program",()=>{this.MANY(()=>{this.SUBRULE(this.topLevelDeclaration)})});topLevelDeclaration=this.RULE("topLevelDeclaration",()=>{this.OR([{ALT:()=>this.SUBRULE(this.extensionDecl)},{ALT:()=>this.SUBRULE(this.functionDecl)},{ALT:()=>this.SUBRULE(this.conceptDecl)},{ALT:()=>this.SUBRULE(this.enumDecl)},{ALT:()=>this.SUBRULE(this.schemaDecl)},{ALT:()=>this.SUBRULE(this.instantiation)},{ALT:()=>this.SUBRULE(this.perInstanceIndex)}])});extensionDecl=this.RULE("extensionDecl",()=>{this.CONSUME(H),this.SUBRULE(this.anyIdentifier),this.CONSUME(L)});functionDecl=this.RULE("functionDecl",()=>{this.CONSUME(V),this.SUBRULE(this.anyIdentifier),this.CONSUME(C),this.CONSUME(U),this.CONSUME(Ht),this.SUBRULE2(this.anyIdentifier),this.SUBRULE(this.functionBody)});functionBody=this.RULE("functionBody",()=>{this.CONSUME(M),this.MANY(()=>{this.SUBRULE(this.functionStatement)}),this.CONSUME(x)});functionStatement=this.RULE("functionStatement",()=>{this.OR([{ALT:()=>this.SUBRULE(this.returnStatement)},{ALT:()=>this.SUBRULE(this.assignmentStatement)}])});returnStatement=this.RULE("returnStatement",()=>{this.CONSUME(Ct),this.OR([{ALT:()=>this.CONSUME(B)},{ALT:()=>this.CONSUME(_)},{ALT:()=>this.CONSUME(r)}]),this.CONSUME(L)});assignmentStatement=this.RULE("assignmentStatement",()=>{this.OR([{ALT:()=>this.CONSUME(B)},{ALT:()=>this.CONSUME(_)},{ALT:()=>this.CONSUME(r)}]),this.CONSUME(G),this.SUBRULE(this.anyIdentifier),this.CONSUME(w),this.SUBRULE(this.functionCallExpr),this.CONSUME(L)});functionCallExpr=this.RULE("functionCallExpr",()=>{this.CONSUME(r),this.CONSUME(C),this.CONSUME(U)});conceptDecl=this.RULE("conceptDecl",()=>{this.CONSUME(j),this.CONSUME(r),this.OPTION(()=>{this.SUBRULE(this.typeParamList)}),this.CONSUME(M),this.MANY(()=>{this.OR([{ALT:()=>this.SUBRULE(this.enumDecl)},{ALT:()=>this.SUBRULE(this.schemaDecl)}])}),this.CONSUME(x)});typeParamList=this.RULE("typeParamList",()=>{this.CONSUME(q),this.CONSUME(r),this.MANY(()=>{this.CONSUME(O),this.CONSUME2(r)}),this.CONSUME($)});enumDecl=this.RULE("enumDecl",()=>{this.CONSUME(Q),this.SUBRULE(this.anyIdentifier),this.CONSUME(M),this.MANY(()=>{this.SUBRULE2(this.anyIdentifier),this.CONSUME(L)}),this.CONSUME(x)});schemaDecl=this.RULE("schemaDecl",()=>{this.CONSUME(z),this.CONSUME(r),this.OPTION(()=>{this.SUBRULE(this.mixinList)}),this.CONSUME(M),this.MANY(()=>{this.SUBRULE(this.schemaBodyItem)}),this.CONSUME(x)});mixinList=this.RULE("mixinList",()=>{this.CONSUME(Y),this.CONSUME(r),this.MANY(()=>{this.CONSUME(O),this.CONSUME2(r)})});schemaBodyItem=this.RULE("schemaBodyItem",()=>{this.OR([{ALT:()=>this.SUBRULE(this.indexDef)},{ALT:()=>this.SUBRULE(this.checkDef)},{ALT:()=>this.SUBRULE(this.triggerDef)},{ALT:()=>this.SUBRULE(this.columnDef)}])});columnDef=this.RULE("columnDef",()=>{this.SUBRULE(this.columnName),this.SUBRULE(this.dataType),this.MANY(()=>{this.SUBRULE(this.columnConstraint)}),this.CONSUME(L)});columnName=this.RULE("columnName",()=>{this.OR([{ALT:()=>{this.CONSUME(D),this.OPTION(()=>{this.CONSUME(r)})}},{ALT:()=>this.CONSUME2(r)}])});dataType=this.RULE("dataType",()=>{this.OR([{ALT:()=>this.CONSUME(Ut)},{ALT:()=>this.CONSUME(Nt)},{ALT:()=>this.CONSUME(Lt)},{ALT:()=>this.CONSUME(gt)},{ALT:()=>this.CONSUME(Ot)},{ALT:()=>this.CONSUME(dt)},{ALT:()=>this.CONSUME(Tt)},{ALT:()=>this.CONSUME(Mt)},{ALT:()=>this.CONSUME(xt)},{ALT:()=>this.CONSUME(At)},{ALT:()=>this.CONSUME(Dt)},{ALT:()=>this.CONSUME(Rt)},{ALT:()=>this.CONSUME(It)},{ALT:()=>this.CONSUME(yt)},{ALT:()=>this.CONSUME(vt)},{ALT:()=>this.CONSUME(bt)},{ALT:()=>this.CONSUME(kt)},{ALT:()=>this.CONSUME(Bt)},{ALT:()=>this.CONSUME(_t)},{ALT:()=>this.CONSUME(Pt)},{ALT:()=>this.CONSUME(qt)},{ALT:()=>this.CONSUME($t)},{ALT:()=>this.CONSUME(Gt)},{ALT:()=>this.CONSUME(r)}]),this.OPTION(()=>{this.CONSUME(C),this.CONSUME(P),this.CONSUME(U)})});columnConstraint=this.RULE("columnConstraint",()=>{this.OR([{ALT:()=>this.CONSUME(rt)},{ALT:()=>this.CONSUME(ot)},{ALT:()=>this.CONSUME(T)},{ALT:()=>this.SUBRULE(this.defaultConstraint)},{ALT:()=>this.SUBRULE(this.refConstraint)},{ALT:()=>this.SUBRULE(this.checkConstraint)},{ALT:()=>this.SUBRULE(this.onDeleteConstraint)}])});defaultConstraint=this.RULE("defaultConstraint",()=>{this.CONSUME(at),this.CONSUME(C),this.SUBRULE(this.defaultValue),this.CONSUME(U)});defaultValue=this.RULE("defaultValue",()=>{this.OR([{ALT:()=>this.CONSUME(wt)},{ALT:()=>this.CONSUME(P)},{ALT:()=>this.CONSUME(Ft)},{ALT:()=>this.CONSUME(jt)},{ALT:()=>this.SUBRULE(this.functionCallExpr)},{ALT:()=>{this.SUBRULE(this.anyIdentifier),this.CONSUME(zt),this.SUBRULE2(this.anyIdentifier)}},{ALT:()=>this.SUBRULE3(this.anyIdentifier)}])});refConstraint=this.RULE("refConstraint",()=>{this.CONSUME(ct),this.CONSUME(C),this.CONSUME(r),this.CONSUME(G),this.CONSUME2(r),this.CONSUME(U)});checkConstraint=this.RULE("checkConstraint",()=>{this.CONSUME(I),this.CONSUME(C),this.SUBRULE(this.checkExpression),this.CONSUME(U)});checkExpression=this.RULE("checkExpression",()=>{this.MANY(()=>{this.OR([{ALT:()=>{this.CONSUME(C),this.SUBRULE(this.checkExpression),this.CONSUME(U)}},{ALT:()=>this.CONSUME(r)},{ALT:()=>this.CONSUME(D)},{ALT:()=>this.CONSUME(P)},{ALT:()=>this.CONSUME(wt)},{ALT:()=>this.CONSUME(Ft)},{ALT:()=>this.CONSUME(jt)},{ALT:()=>this.CONSUME(zt)},{ALT:()=>this.CONSUME(w)},{ALT:()=>this.CONSUME(O)},{ALT:()=>this.CONSUME(G)},{ALT:()=>this.CONSUME(q)},{ALT:()=>this.CONSUME($)}])})});onDeleteConstraint=this.RULE("onDeleteConstraint",()=>{this.CONSUME(X),this.CONSUME(C),this.OR([{ALT:()=>this.CONSUME(lt)},{ALT:()=>this.CONSUME(ht)},{ALT:()=>this.CONSUME(pt)},{ALT:()=>this.CONSUME(mt)},{ALT:()=>this.CONSUME(ut)}]),this.CONSUME(U)});indexDef=this.RULE("indexDef",()=>{this.CONSUME(R),this.CONSUME(C),this.SUBRULE(this.indexColumnList),this.CONSUME(U),this.MANY(()=>{this.OR([{ALT:()=>this.CONSUME(T)},{ALT:()=>this.CONSUME(y)},{ALT:()=>this.CONSUME(v)},{ALT:()=>this.CONSUME(b)},{ALT:()=>this.CONSUME(k)}])}),this.CONSUME(L)});indexColumnList=this.RULE("indexColumnList",()=>{this.SUBRULE(this.indexColumn),this.MANY(()=>{this.CONSUME(O),this.SUBRULE2(this.indexColumn)})});indexColumn=this.RULE("indexColumn",()=>{this.OR([{ALT:()=>this.CONSUME(D)},{ALT:()=>this.CONSUME(r)}]),this.OPTION(()=>{this.CONSUME2(r)})});checkDef=this.RULE("checkDef",()=>{this.CONSUME(I),this.CONSUME(C),this.SUBRULE(this.checkExpression),this.CONSUME(U),this.CONSUME(L)});triggerDef=this.RULE("triggerDef",()=>{this.CONSUME(Z),this.CONSUME(r),this.OR([{ALT:()=>this.CONSUME(J)},{ALT:()=>this.CONSUME(W)}]),this.OR2([{ALT:()=>this.CONSUME(ft)},{ALT:()=>this.CONSUME(Et)},{ALT:()=>this.CONSUME(St)}]),this.CONSUME(tt),this.CONSUME(et),this.OR3([{ALT:()=>this.CONSUME(nt)},{ALT:()=>this.CONSUME(st)}]),this.CONSUME(it),this.CONSUME(K),this.CONSUME2(r),this.CONSUME(C),this.CONSUME(U),this.CONSUME(L)});instantiation=this.RULE("instantiation",()=>{this.SUBRULE(this.instantiationTargetList),this.CONSUME(w),this.SUBRULE(this.conceptReference),this.CONSUME(L)});instantiationTargetList=this.RULE("instantiationTargetList",()=>{this.SUBRULE(this.instantiationTarget),this.MANY(()=>{this.CONSUME(O),this.SUBRULE2(this.instantiationTarget)})});instantiationTarget=this.RULE("instantiationTarget",()=>{this.CONSUME(r),this.OPTION(()=>{this.CONSUME(Yt),this.CONSUME2(r),this.CONSUME(Qt)})});conceptReference=this.RULE("conceptReference",()=>{this.CONSUME(r),this.OPTION(()=>{this.CONSUME(q),this.SUBRULE(this.typeArgList),this.CONSUME($)})});typeArgList=this.RULE("typeArgList",()=>{this.SUBRULE(this.typeArg),this.MANY(()=>{this.CONSUME(O),this.SUBRULE2(this.typeArg)})});typeArg=this.RULE("typeArg",()=>{this.CONSUME(r),this.OPTION(()=>{this.CONSUME(Yt),this.CONSUME2(r),this.CONSUME(Qt)})});perInstanceIndex=this.RULE("perInstanceIndex",()=>{this.CONSUME(R),this.CONSUME(C),this.CONSUME(r),this.CONSUME(O),this.CONSUME2(r),this.MANY(()=>{this.CONSUME2(O),this.CONSUME3(r)}),this.CONSUME(U),this.MANY2(()=>{this.OR([{ALT:()=>this.CONSUME(T)},{ALT:()=>this.CONSUME(y)},{ALT:()=>this.CONSUME(v)},{ALT:()=>this.CONSUME(b)},{ALT:()=>this.CONSUME(k)}])}),this.CONSUME(L)})},Vt=new Jt;function E(s,t){let e=s.children[t];if(e&&e.length>0){let i=e[0];if(i&&"image"in i)return i.image}return""}function d(s,t){let e=s.children[t];return e?e.map(i=>"image"in i?i.image:"").filter(i=>i!==""):[]}function m(s,t){let i=s.children[t]?.[0];if(i&&"children"in i)return i}function g(s,t){let e=s.children[t];return e?e.filter(i=>"children"in i):[]}function se(s){let e=g(s,"anyIdentifier")[0];if(e)for(let i of Object.keys(e.children)){let l=e.children[i]?.[0];if(l&&"image"in l)return l.image}return""}function Zt(s){let t=g(s,"anyIdentifier"),e=[];for(let i of t)for(let a of Object.keys(i.children)){let n=i.children[a]?.[0];if(n&&"image"in n){e.push(n.image);break}}return e}var Wt=class{visit(t){let e=[],i=g(t,"topLevelDeclaration");for(let a of i){let l=this.visitTopLevelDeclaration(a);l&&e.push(l)}return{type:"Program",declarations:e}}visitTopLevelDeclaration(t){let e=m(t,"extensionDecl");if(e)return this.visitExtensionDecl(e);let i=m(t,"functionDecl");if(i)return this.visitFunctionDecl(i);let a=m(t,"conceptDecl");if(a)return this.visitConceptDecl(a);let l=m(t,"enumDecl");if(l)return this.visitEnumDecl(l);let n=m(t,"schemaDecl");if(n)return this.visitSchemaDecl(n);let o=m(t,"instantiation");if(o)return this.visitInstantiation(o);let h=m(t,"perInstanceIndex");return h?this.visitPerInstanceIndex(h):null}visitExtensionDecl(t){return{type:"ExtensionDecl",name:se(t)}}visitFunctionDecl(t){let e=Zt(t),i=e[0]??"",a=e[1]??"",l=m(t,"functionBody"),n="";if(l){let o=g(l,"functionStatement"),h=[];for(let p of o){let u=m(p,"returnStatement");if(u){let f=E(u,"Identifier");f||(f=E(u,"New")),f||(f=E(u,"Old")),h.push(`RETURN ${f};`)}let S=m(p,"assignmentStatement");if(S){let f=E(S,"Identifier");f||(f=E(S,"New")),f||(f=E(S,"Old"));let A=se(S),N=m(S,"functionCallExpr"),ue=N?E(N,"Identifier"):"";h.push(`${f}.${A} := ${ue}();`)}}n=h.join(`
2
+ `)}return{type:"FunctionDecl",name:i,returnType:a,body:n}}visitConceptDecl(t){let e=E(t,"Identifier"),i=[],a=m(t,"typeParamList");a&&i.push(...d(a,"Identifier"));let l=[],n=g(t,"schemaDecl");for(let h of n){let p=this.visitSchemaDecl(h);p.conceptScope=e,l.push(p)}let o=g(t,"enumDecl");for(let h of o){let p=this.visitEnumDecl(h);p.conceptScope=e,l.push(p)}return{type:"ConceptDecl",name:e,typeParams:i,members:l}}visitEnumDecl(t){let e=Zt(t),i=e[0]??"",a=e.slice(1);return{type:"EnumDecl",name:i,values:a}}visitSchemaDecl(t){let e=E(t,"Identifier"),i=[],a=m(t,"mixinList");a&&i.push(...d(a,"Identifier"));let l=[],n=g(t,"schemaBodyItem");for(let o of n){let h=this.visitSchemaBodyItem(o);h&&l.push(h)}return{type:"SchemaDecl",name:e,mixins:i,members:l}}visitSchemaBodyItem(t){let e=m(t,"columnDef");if(e)return this.visitColumnDef(e);let i=m(t,"indexDef");if(i)return this.visitIndexDef(i);let a=m(t,"checkDef");if(a)return this.visitCheckDef(a);let l=m(t,"triggerDef");return l?this.visitTriggerDef(l):null}visitColumnDef(t){let e=m(t,"columnName"),i="";if(e){let h=E(e,"TemplateIdentifier"),p=E(e,"Identifier");i=h?h+p:p}let a=m(t,"dataType"),l="";if(a){for(let u of Object.keys(a.children)){let f=a.children[u]?.[0];if(f&&"image"in f){l=f.image;break}}let p=a.children.NumberLiteral?.[0];p&&"image"in p&&(l+=`(${p.image})`)}let n=[],o=g(t,"columnConstraint");for(let h of o){let p=this.visitColumnConstraint(h);p&&n.push(p)}return{type:"ColumnDef",name:i,dataType:l,constraints:n}}visitColumnConstraint(t){if(t.children.Pkey)return{type:"PrimaryKey"};if(t.children.Nonull)return{type:"NotNull"};if(t.children.Unique)return{type:"Unique"};let e=m(t,"defaultConstraint");if(e){let n=m(e,"defaultValue"),o="";if(n){let h=E(n,"StringLiteral"),p=E(n,"NumberLiteral"),u=E(n,"BooleanLiteral"),S=E(n,"NullLiteral"),f=m(n,"functionCallExpr"),A=E(n,"DoubleColon"),N=Zt(n);h?o=h:p?o=p:u?o=u:S?o=S:f?o=E(f,"Identifier")+"()":A&&N.length===2&&N[0]&&N[1]?o=`${N[0]}::${N[1]}`:N.length>0&&N[0]&&(o=N[0])}return{type:"Default",value:o}}let i=m(t,"refConstraint");if(i){let n=d(i,"Identifier");return{type:"Reference",table:n[0]??"",column:n[1]??""}}let a=m(t,"checkConstraint");if(a){let n=m(a,"checkExpression");return{type:"Check",value:n?this.reconstructCheckExpression(n):""}}let l=m(t,"onDeleteConstraint");if(l){let n={Cascade:"CASCADE",Restrict:"RESTRICT",SetNull:"SET NULL",SetDefault:"SET DEFAULT",NoAction:"NO ACTION"};for(let[o,h]of Object.entries(n))if(l.children[o])return{type:"OnDelete",action:h}}return null}reconstructCheckExpression(t){let e=[];for(let a of Object.keys(t.children)){let l=t.children[a];if(l){for(let n of l)if("image"in n&&"startOffset"in n){let o=typeof n.startOffset=="number"?n.startOffset:0;e.push({image:n.image,startOffset:o,isParenExpr:!1})}else if("children"in n&&a==="checkExpression"){let o=this.reconstructCheckExpression(n);if(o){let h=1/0;for(let p of Object.keys(n.children)){let u=n.children[p];if(u)for(let S of u)"startOffset"in S&&typeof S.startOffset=="number"&&(h=Math.min(h,S.startOffset))}e.push({image:o,startOffset:h===1/0?0:h,isParenExpr:!1})}}}}e.sort((a,l)=>a.startOffset-l.startOffset);let i="";for(let a=0;a<e.length;a++){let l=e[a],n=a>0?e[a-1]:null;if(!l)continue;let o=l.image,h=[")",",",".","::",">","<"],p=["(",".","::"];if(n&&!l.isParenExpr){let u=n.image,S=u[u.length-1]??"",f=(u===">"||u==="<")&&o==="=",A=(o===">"||o==="<")&&!p.includes(u)&&S!=="(";(!h.includes(o)&&!p.includes(u)&&S!=="("&&!f||A)&&(i+=" ")}else if(n&&l.isParenExpr){let u=n.image;(u[u.length-1]??"")!=="("&&(i+=" ")}i+=o}return i}visitIndexDef(t){let e=[],i=m(t,"indexColumnList");if(i){let n=g(i,"indexColumn");for(let o of n){let h=E(o,"TemplateIdentifier"),p=E(o,"Identifier");e.push(h?h+p:p)}}let a=t.children.Unique!==void 0,l;return t.children.Gin&&(l="gin"),t.children.Gist&&(l="gist"),t.children.Btree&&(l="btree"),t.children.Hash&&(l="hash"),{type:"IndexDef",columns:e,unique:a,using:l}}visitCheckDef(t){let e=m(t,"checkExpression");return{type:"CheckDef",expression:e?this.reconstructCheckExpression(e):""}}visitTriggerDef(t){let e=d(t,"Identifier"),i=e[0]??"",a=e[1]??"",l=t.children.Before?"before":"after",n="update";t.children.Insert&&(n="insert"),t.children.Delete&&(n="delete");let o=t.children.Row?"row":"statement";return{type:"TriggerDef",name:i,timing:l,event:n,forEach:o,executeFunction:a}}visitInstantiation(t){let e=[],i=m(t,"instantiationTargetList");if(i){let o=g(i,"instantiationTarget");for(let h of o){let p=d(h,"Identifier");e.push({tableName:p[0]??"",alias:p[1]})}}let a=m(t,"conceptReference"),l="",n=[];if(a){l=E(a,"Identifier");let o=m(a,"typeArgList");if(o){let h=g(o,"typeArg");for(let p of h){let u=d(p,"Identifier");n.push({tableName:u[0]??"",alias:u[1]})}}}return{type:"Instantiation",targets:e,conceptName:l,typeArgs:n}}visitPerInstanceIndex(t){let e=d(t,"Identifier"),i=e[0]??"",a=e.slice(1),l=t.children.Unique!==void 0,n;return t.children.Gin&&(n="gin"),t.children.Gist&&(n="gist"),t.children.Btree&&(n="btree"),t.children.Hash&&(n="hash"),{type:"PerInstanceIndex",tableName:i,columns:a,unique:l,using:n}}};function Xt(s){let t=ne(s),e=[];for(let n of t.errors)e.push({message:n.message,location:{start:{line:n.line??0,column:n.column??0,offset:n.offset},end:{line:n.line??0,column:n.column??0,offset:n.offset}},severity:"error"});if(e.length>0)return{ast:null,errors:e};Vt.input=t.tokens;let i=Vt.program();for(let n of Vt.errors){let o=n.token;e.push({message:n.message,location:{start:{line:o.startLine??0,column:o.startColumn??0,offset:o.startOffset},end:{line:o.endLine??0,column:o.endColumn??0,offset:o.endOffset??o.startOffset}},severity:"error"})}return e.length>0?{ast:null,errors:e}:{ast:new Wt().visit(i),errors:e}}function te(s){return s.replace(/([A-Z]+)([A-Z][a-z])/g,"$1_$2").replace(/([a-z\d])([A-Z])/g,"$1_$2").toLowerCase()}function oe(s){return s.includes("::")?s.replace(/(\w+)::(\w+)/g,"'$2'::$1"):s}function Ne(s,t){return s==="NULL"||/^'[^']*'::\w+$/.test(s)?s:s.includes("::")?oe(s):`'${s}'::${t}`}function Le(){return{concepts:new Map,schemas:new Map,enums:new Map,tableToSchema:new Map,templateSubs:new Map,enumSql:[],tableSql:[],indexSql:[],triggerSql:[],extensionSql:[],functionSql:[],perInstanceIndexSql:[],generatedEnums:new Set,generatedTables:new Set}}function ge(s,t){for(let e of s.declarations)switch(e.type){case"ConceptDecl":t.concepts.set(e.name,e);break;case"SchemaDecl":t.schemas.set(e.name,e);break;case"EnumDecl":t.enums.set(e.name,e);break}}function ae(s,t){let e=s.match(/^\{([^}]+)\}(.*)$/);if(e){let[,i,a]=e,l;for(let[n,o]of t.templateSubs)if(n===i){l=o;break}return(l??te(i??""))+(a??"")}return s}function Oe(s){return s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function ce(s,t){let e=s;for(let[i,a]of t.templateSubs){let l=Oe(i),n=new RegExp(`\\{${l}\\}`,"g");e=e.replace(n,a)}return e=oe(e),e}function le(s,t){let e=s.name;if(t.generatedEnums.has(e))return;t.generatedEnums.add(e),t.enums.set(e,s);let i=s.values.map(a=>`'${a}'`).join(", ");t.enumSql.push(`DO $$ BEGIN
3
+ CREATE TYPE ${e} AS ENUM (${i});
4
4
  EXCEPTION
5
5
  WHEN duplicate_object THEN null;
6
- END $$;`)}function Le(i,t){let e=ee(i.name,t),s=i.dataType.toUpperCase(),a=[` ${e} ${s}`],l=[];for(let n of i.constraints)switch(n.type){case"PrimaryKey":a.push("PRIMARY KEY");break;case"NotNull":a.push("NOT NULL");break;case"Unique":a.push("UNIQUE");break;case"Default":a.push(`DEFAULT ${n.value??"NULL"}`);break;case"Reference":{let c=n.table??"";t.tableToSchema.has(c)&&(c=t.tableToSchema.get(c)??c),l.push(`REFERENCES ${c}(${n.column??"id"})`);break}case"Check":a.push(`CHECK (${ae(n.value??"",t)})`);break;case"OnDelete":l.push(`ON DELETE ${n.action??"NO ACTION"}`);break}return[...a,...l].join(" ")}function ge(i,t,e){let s=t.columns.map(h=>ee(h,e)).join(", "),a=t.columns.map(h=>ee(h,e)).join("_"),l=`idx_${i}_${a}`,n=t.unique===!0?"UNIQUE ":"",c=t.using?`USING ${t.using} `:"";return`CREATE ${n}INDEX ${l} ON ${i} ${c}(${s});`}function Oe(i,t,e){return` CHECK (${ae(t.expression,e)})`}function Te(i,t,e){let s=t.timing.toUpperCase(),a=t.event.toUpperCase(),l=t.forEach==="row"?"FOR EACH ROW":"FOR EACH STATEMENT";return`CREATE TRIGGER ${t.name}_${i}
7
- ${s} ${a} ON ${i}
8
- ${l} EXECUTE FUNCTION ${t.executeFunction}();`}function le(i,t){let e=[];for(let s of i.mixins){let a=t.schemas.get(s);a&&e.push(...le(a,t))}return e.push(...i.members),e}function re(i,t,e){let s=le(i,e),a=[],l=[],n=[],c=[];for(let h of s)switch(h.type){case"ColumnDef":a.push(h);break;case"IndexDef":l.push(h);break;case"CheckDef":n.push(h);break;case"TriggerDef":c.push(h);break}return{tableName:t,columns:a,indexes:l,checks:n,triggers:c}}function oe(i,t){if(t.generatedTables.has(i.tableName))return;t.generatedTables.add(i.tableName);let e=i.columns.map(n=>Le(n,t)),s=i.checks.map(n=>Oe(i.tableName,n,t)),l=[...e,...s].join(`,
9
- `);t.tableSql.push(`CREATE TABLE ${i.tableName} (
6
+ END $$;`)}function de(s,t){let e=ae(s.name,t),i=s.dataType.toUpperCase(),a=[` ${e} ${i}`],l=[];for(let n of s.constraints)switch(n.type){case"PrimaryKey":a.push("PRIMARY KEY");break;case"NotNull":a.push("NOT NULL");break;case"Unique":a.push("UNIQUE");break;case"Default":{let o=n.value??"NULL",h=s.dataType.toLowerCase();t.enums.has(h)&&(o=Ne(o,h)),a.push(`DEFAULT ${o}`);break}case"Reference":{let o=n.table??"";t.tableToSchema.has(o)&&(o=t.tableToSchema.get(o)??o),l.push(`REFERENCES ${o}(${n.column??"id"})`);break}case"Check":a.push(`CHECK (${ce(n.value??"",t)})`);break;case"OnDelete":l.push(`ON DELETE ${n.action??"NO ACTION"}`);break}return[...a,...l].join(" ")}function he(s,t,e){let i=t.columns.map(p=>ae(p,e)),a=i.join(", "),l=i.join("_"),n=`idx_${s}_${l}`,o=t.unique===!0?"UNIQUE ":"",h=t.using?`USING ${t.using} `:"";return`CREATE ${o}INDEX ${n} ON ${s} ${h}(${a});`}function Te(s,t,e){return` CHECK (${ce(t.expression,e)})`}function Me(s,t,e){let i=t.timing.toUpperCase(),a=t.event.toUpperCase(),l=t.forEach==="row"?"FOR EACH ROW":"FOR EACH STATEMENT";return`CREATE TRIGGER ${t.name}_${s}
7
+ ${i} ${a} ON ${s}
8
+ ${l} EXECUTE FUNCTION ${t.executeFunction}();`}function pe(s,t){let e=[];for(let i of s.mixins){let a=t.schemas.get(i);a&&e.push(...pe(a,t))}return e.push(...s.members),e}function ie(s,t,e){let i=pe(s,e),a=[],l=[],n=[],o=[];for(let h of i)switch(h.type){case"ColumnDef":a.push(h);break;case"IndexDef":l.push(h);break;case"CheckDef":n.push(h);break;case"TriggerDef":o.push(h);break}return{tableName:t,columns:a,indexes:l,checks:n,triggers:o}}function re(s,t){if(t.generatedTables.has(s.tableName))return;t.generatedTables.add(s.tableName);let e=s.columns.map(n=>de(n,t)),i=s.checks.map(n=>Te(s.tableName,n,t)),l=[...e,...i].join(`,
9
+ `);t.tableSql.push(`CREATE TABLE ${s.tableName} (
10
10
  ${l}
11
- );`);for(let n of i.indexes)t.indexSql.push(ge(i.tableName,n,t));for(let n of i.triggers)t.triggerSql.push(Te(i.tableName,n,t))}function de(i,t){t.extensionSql.push(`CREATE EXTENSION IF NOT EXISTS "${i.name}";`)}function Me(i,t){t.functionSql.push(`CREATE OR REPLACE FUNCTION ${i.name}()
11
+ );`);for(let n of s.indexes)t.indexSql.push(he(s.tableName,n,t));for(let n of s.triggers)t.triggerSql.push(Me(s.tableName,n,t))}function xe(s,t){t.extensionSql.push(`CREATE EXTENSION IF NOT EXISTS "${s.name}";`)}function Ae(s,t){t.functionSql.push(`CREATE OR REPLACE FUNCTION ${s.name}()
12
12
  RETURNS TRIGGER AS $$
13
13
  BEGIN
14
- ${i.body}
14
+ ${s.body}
15
15
  END;
16
- $$ LANGUAGE plpgsql;`)}function Ae(i,t){ce(i,t)}function xe(i,t){t.schemas.set(i.name,i)}function De(i,t){let e=t.concepts.get(i.conceptName);if(!e){let n=t.schemas.get(i.conceptName);if(n){let c=i.targets[0];if(c){let h=c.tableName;t.tableToSchema.set(n.name,h);let p=re(n,h,t);oe(p,t)}}return}t.templateSubs.clear();for(let n=0;n<e.typeParams.length;n++){let c=e.typeParams[n],h=i.typeArgs[n];c&&h&&(t.templateSubs.set(c,h.alias??te(c)),t.tableToSchema.set(c,h.tableName))}let s=e.members.filter(n=>n.type==="SchemaDecl"),a=new Map;for(let n=0;n<i.targets.length&&n<s.length;n++){let c=i.targets[n],h=s[n];c&&h&&(a.set(h.name,{name:c.tableName,alias:c.alias}),t.tableToSchema.set(h.name,c.tableName),c.alias?t.templateSubs.set(h.name,c.alias):t.templateSubs.set(h.name,te(h.name)))}let l=e.members.filter(n=>n.type==="EnumDecl");for(let n of l)ce(n,t);for(let n of s){let c=a.get(n.name);if(c){let h=re(n,c.name,t);oe(h,t)}}}function Re(i,t){let e=i.columns.join(", "),s=`idx_${i.tableName}_${i.columns.join("_")}`,a=i.unique===!0?"UNIQUE ":"",l=i.using?`USING ${i.using} `:"";t.perInstanceIndexSql.push(`CREATE ${a}INDEX ${s} ON ${i.tableName} ${l}(${e});`)}function ne(i){let t=fe();Ne(i,t);for(let a of i.declarations)Ie(a,t);let e=[];t.extensionSql.length>0&&e.push(t.extensionSql.join(`
16
+ $$ LANGUAGE plpgsql;`)}function De(s,t){le(s,t)}function Re(s,t){t.schemas.set(s.name,s)}function Ie(s,t){let e=t.concepts.get(s.conceptName);if(!e){let n=t.schemas.get(s.conceptName);if(n){let o=s.targets[0];if(o){let h=o.tableName;t.tableToSchema.set(n.name,h);let p=ie(n,h,t);re(p,t)}}return}t.templateSubs.clear();for(let n=0;n<e.typeParams.length;n++){let o=e.typeParams[n],h=s.typeArgs[n];o&&h&&(t.templateSubs.set(o,h.alias??te(o)),t.tableToSchema.set(o,h.tableName))}let i=e.members.filter(n=>n.type==="SchemaDecl"),a=new Map;for(let n=0;n<s.targets.length&&n<i.length;n++){let o=s.targets[n],h=i[n];o&&h&&(a.set(h.name,{name:o.tableName,alias:o.alias}),t.tableToSchema.set(h.name,o.tableName),o.alias?t.templateSubs.set(h.name,o.alias):t.templateSubs.set(h.name,te(h.name)))}let l=e.members.filter(n=>n.type==="EnumDecl");for(let n of l)le(n,t);for(let n of i){let o=a.get(n.name);if(o){let h=ie(n,o.name,t);re(h,t)}}}function ye(s,t){let e={type:"IndexDef",columns:s.columns,unique:s.unique,using:s.using};t.perInstanceIndexSql.push(he(s.tableName,e,t))}function ee(s){let t=Le();ge(s,t);for(let a of s.declarations)ve(a,t);let e=[];t.extensionSql.length>0&&e.push(t.extensionSql.join(`
17
17
 
18
18
  `)),t.functionSql.length>0&&e.push(t.functionSql.join(`
19
19
 
20
20
  `)),t.enumSql.length>0&&e.push(t.enumSql.join(`
21
21
 
22
- `));let s=[];for(let a of t.tableSql){a&&s.push(a);let l=a.match(/CREATE TABLE (\w+)/);if(l){let n=l[1]??"";for(let c of t.indexSql)c.includes(` ON ${n} `)&&s.push(c);for(let c of t.triggerSql)c.includes(` ON ${n}`)&&s.push(c)}}return s.length>0&&e.push(s.join(`
22
+ `));let i=[];for(let a of t.tableSql){a&&i.push(a);let l=a.match(/CREATE TABLE (\w+)/);if(l){let n=l[1]??"";for(let o of t.indexSql)o.includes(` ON ${n} `)&&i.push(o);for(let o of t.triggerSql)o.includes(` ON ${n}`)&&i.push(o)}}return i.length>0&&e.push(i.join(`
23
23
 
24
24
  `)),t.perInstanceIndexSql.length>0&&e.push(t.perInstanceIndexSql.join(`
25
25
 
@@ -28,6 +28,6 @@ $$ LANGUAGE plpgsql;`)}function Ae(i,t){ce(i,t)}function xe(i,t){t.schemas.set(i
28
28
  `+e.join(`
29
29
 
30
30
  `)+`
31
- `}function Ie(i,t){switch(i.type){case"ExtensionDecl":de(i,t);break;case"FunctionDecl":Me(i,t);break;case"EnumDecl":Ae(i,t);break;case"SchemaDecl":xe(i,t);break;case"ConceptDecl":break;case"Instantiation":De(i,t);break;case"PerInstanceIndex":Re(i,t);break}}function he(i){let t=[],e=Xt(i);if(e.errors.length>0)return{success:!1,errors:e.errors,ast:e.ast??void 0};if(!e.ast)return t.push({message:"Failed to parse source code",severity:"error"}),{success:!1,errors:t};try{return{success:!0,sql:ne(e.ast),errors:[],ast:e.ast}}catch(s){let a=s instanceof Error?s.message:String(s);return t.push({message:`Code generation failed: ${a}`,severity:"error"}),{success:!1,errors:t,ast:e.ast}}}function ye(i){let t=he(i);if(!t.success||!t.sql){let e=t.errors.map(s=>s.message).join(`
31
+ `}function ve(s,t){switch(s.type){case"ExtensionDecl":xe(s,t);break;case"FunctionDecl":Ae(s,t);break;case"EnumDecl":De(s,t);break;case"SchemaDecl":Re(s,t);break;case"ConceptDecl":break;case"Instantiation":Ie(s,t);break;case"PerInstanceIndex":ye(s,t);break}}function me(s){let t=[],e=Xt(s);if(e.errors.length>0)return{success:!1,errors:e.errors,ast:e.ast??void 0};if(!e.ast)return t.push({message:"Failed to parse source code",severity:"error"}),{success:!1,errors:t};try{return{success:!0,sql:ee(e.ast),errors:[],ast:e.ast}}catch(i){let a=i instanceof Error?i.message:String(i);return t.push({message:`Code generation failed: ${a}`,severity:"error"}),{success:!1,errors:t,ast:e.ast}}}function be(s){let t=me(s);if(!t.success||!t.sql){let e=t.errors.map(i=>i.message).join(`
32
32
  `);throw new Error(`Compilation failed:
33
- ${e}`)}return t.sql}export{he as compile,ye as compileToSQL,ne as generate,Xt as parse};
33
+ ${e}`)}return t.sql}export{me as compile,be as compileToSQL,ee as generate,Xt as parse};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barishnamazov/gsql",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Generic SQL - Parametric Polymorphism for SQL Schemas",
5
5
  "module": "src/index.ts",
6
6
  "type": "module",