github.com/dolthub/go-mysql-server@v0.18.0/optgen/cmd/support/frame_factory_gen.go (about) 1 package support 2 3 import ( 4 "fmt" 5 "io" 6 "math" 7 "strings" 8 ) 9 10 type FrameFactoryGen struct { 11 w io.Writer 12 defs []frameDef 13 limit int 14 } 15 16 func (g *FrameFactoryGen) Generate(defines GenDefs, w io.Writer) { 17 g.w = w 18 if g.limit == 0 { 19 g.limit = math.MaxInt32 20 } 21 g.defs = getDefs(g.limit) 22 g.generate() 23 } 24 25 func (g *FrameFactoryGen) generate() { 26 g.genImports() 27 g.genNewFrameFactory() 28 } 29 30 func (g *FrameFactoryGen) genImports() { 31 fmt.Fprintf(g.w, "import (\n") 32 fmt.Fprintf(g.w, " \"fmt\"\n") 33 fmt.Fprintf(g.w, " \"github.com/dolthub/go-mysql-server/sql\"\n") 34 fmt.Fprintf(g.w, " \"github.com/dolthub/go-mysql-server/sql/plan\"\n") 35 fmt.Fprintf(g.w, " ast \"github.com/dolthub/vitess/go/vt/sqlparser\"\n") 36 fmt.Fprintf(g.w, ")\n\n") 37 } 38 39 func (g *FrameFactoryGen) genNewFrameFactory() { 40 fmt.Fprintf(g.w, "func (b *Builder) NewFrame(inScope *scope, f *ast.Frame) sql.WindowFrame {\n") 41 fmt.Fprintf(g.w, " if f == nil {\n") 42 fmt.Fprintf(g.w, " return nil\n") 43 fmt.Fprintf(g.w, " }\n") 44 // use manual accessors to init input args 45 fmt.Fprintf(g.w, " isRange := f.Unit == ast.RangeUnit\n") 46 fmt.Fprintf(g.w, " isRows := f.Unit == ast.RowsUnit\n") 47 for _, arg := range frameExtents { 48 fmt.Fprintf(g.w, " %s := b.getFrame%s(inScope, f)\n", arg.String(), strings.Title(arg.String())) 49 } 50 51 // switch on frame conditionals to select appropriate constructor 52 fmt.Fprintf(g.w, " switch {\n") 53 for _, def := range g.defs { 54 fmt.Fprintf(g.w, " case %s:\n", def.CondArgs()) 55 constructArgs := strings.Builder{} 56 i := 0 57 for _, a := range def.Args() { 58 if a.argType() == "bool" { 59 continue 60 } 61 if i > 0 { 62 constructArgs.WriteString(", ") 63 } 64 constructArgs.WriteString(a.String()) 65 i++ 66 } 67 fmt.Fprintf(g.w, " return plan.New%sFrame(%s)\n", def.Name(), constructArgs.String()) 68 } 69 fmt.Fprintf(g.w, " default:\n") 70 fmt.Fprintf(g.w, " err := fmt.Errorf(\"no matching constructor found for frame: %%v\", f)\n") 71 fmt.Fprintf(g.w, " b.handleErr(err)\n") 72 fmt.Fprintf(g.w, " return nil\n") 73 fmt.Fprintf(g.w, " }\n") 74 fmt.Fprintf(g.w, "}\n\n") 75 }