|
| 1 | +// Copyright 2024 Huan Du. All rights reserved. |
| 2 | +// Licensed under the MIT license that can be found in the LICENSE file. |
| 3 | + |
| 4 | +package sqlbuilder |
| 5 | + |
| 6 | +const ( |
| 7 | + cteMarkerInit injectionMarker = iota |
| 8 | + cteMarkerAfterWith |
| 9 | +) |
| 10 | + |
| 11 | +// With creates a new CTE builder with default flavor. |
| 12 | +func With(tables ...*CTETableBuilder) *CTEBuilder { |
| 13 | + return DefaultFlavor.NewCTEBuilder().With(tables...) |
| 14 | +} |
| 15 | + |
| 16 | +func newCTEBuilder() *CTEBuilder { |
| 17 | + return &CTEBuilder{ |
| 18 | + args: &Args{}, |
| 19 | + injection: newInjection(), |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// CTEBuilder is a CTE (Common Table Expression) builder. |
| 24 | +type CTEBuilder struct { |
| 25 | + tableNames []string |
| 26 | + tableBuilderVars []string |
| 27 | + |
| 28 | + args *Args |
| 29 | + |
| 30 | + injection *injection |
| 31 | + marker injectionMarker |
| 32 | +} |
| 33 | + |
| 34 | +var _ Builder = new(CTEBuilder) |
| 35 | + |
| 36 | +// With sets the CTE name and columns. |
| 37 | +func (cteb *CTEBuilder) With(tables ...*CTETableBuilder) *CTEBuilder { |
| 38 | + tableNames := make([]string, 0, len(tables)) |
| 39 | + tableBuilderVars := make([]string, 0, len(tables)) |
| 40 | + |
| 41 | + for _, table := range tables { |
| 42 | + tableNames = append(tableNames, table.TableName()) |
| 43 | + tableBuilderVars = append(tableBuilderVars, cteb.args.Add(table)) |
| 44 | + } |
| 45 | + |
| 46 | + cteb.tableNames = tableNames |
| 47 | + cteb.tableBuilderVars = tableBuilderVars |
| 48 | + cteb.marker = cteMarkerAfterWith |
| 49 | + return cteb |
| 50 | +} |
| 51 | + |
| 52 | +// Select creates a new SelectBuilder to build a SELECT statement using this CTE. |
| 53 | +func (cteb *CTEBuilder) Select(col ...string) *SelectBuilder { |
| 54 | + sb := cteb.args.Flavor.NewSelectBuilder() |
| 55 | + return sb.With(cteb).Select(col...) |
| 56 | +} |
| 57 | + |
| 58 | +// String returns the compiled CTE string. |
| 59 | +func (cteb *CTEBuilder) String() string { |
| 60 | + sql, _ := cteb.Build() |
| 61 | + return sql |
| 62 | +} |
| 63 | + |
| 64 | +// Build returns compiled CTE string and args. |
| 65 | +func (cteb *CTEBuilder) Build() (sql string, args []interface{}) { |
| 66 | + return cteb.BuildWithFlavor(cteb.args.Flavor) |
| 67 | +} |
| 68 | + |
| 69 | +// BuildWithFlavor builds a CTE with the specified flavor and initial arguments. |
| 70 | +func (cteb *CTEBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{}) { |
| 71 | + buf := newStringBuilder() |
| 72 | + cteb.injection.WriteTo(buf, cteMarkerInit) |
| 73 | + |
| 74 | + if len(cteb.tableBuilderVars) > 0 { |
| 75 | + buf.WriteLeadingString("WITH ") |
| 76 | + buf.WriteStrings(cteb.tableBuilderVars, ", ") |
| 77 | + } |
| 78 | + |
| 79 | + cteb.injection.WriteTo(buf, cteMarkerAfterWith) |
| 80 | + return cteb.args.CompileWithFlavor(buf.String(), flavor, initialArg...) |
| 81 | +} |
| 82 | + |
| 83 | +// SetFlavor sets the flavor of compiled sql. |
| 84 | +func (cteb *CTEBuilder) SetFlavor(flavor Flavor) (old Flavor) { |
| 85 | + old = cteb.args.Flavor |
| 86 | + cteb.args.Flavor = flavor |
| 87 | + return |
| 88 | +} |
| 89 | + |
| 90 | +// SQL adds an arbitrary sql to current position. |
| 91 | +func (cteb *CTEBuilder) SQL(sql string) *CTEBuilder { |
| 92 | + cteb.injection.SQL(cteb.marker, sql) |
| 93 | + return cteb |
| 94 | +} |
| 95 | + |
| 96 | +// TableNames returns all table names in a CTE. |
| 97 | +func (cteb *CTEBuilder) TableNames() []string { |
| 98 | + return cteb.tableNames |
| 99 | +} |
0 commit comments