github.com/eden-framework/sqlx@v0.0.2/builder/stmt_select.go (about) 1 package builder 2 3 import ( 4 "context" 5 ) 6 7 type SelectStatement interface { 8 SqlExpr 9 selectStatement() 10 } 11 12 func Select(sqlExpr SqlExpr, modifiers ...string) *StmtSelect { 13 return &StmtSelect{ 14 sqlExpr: sqlExpr, 15 modifiers: modifiers, 16 } 17 } 18 19 type StmtSelect struct { 20 SelectStatement 21 sqlExpr SqlExpr 22 table *Table 23 modifiers []string 24 additions []Addition 25 } 26 27 func (s *StmtSelect) IsNil() bool { 28 return s == nil 29 } 30 31 func (s StmtSelect) From(table *Table, additions ...Addition) *StmtSelect { 32 s.table = table 33 s.additions = additions 34 return &s 35 } 36 37 func (s *StmtSelect) Ex(ctx context.Context) *Ex { 38 multiTable := false 39 40 for i := range s.additions { 41 addition := s.additions[i] 42 if IsNilExpr(addition) { 43 continue 44 } 45 46 if addition.AdditionType() == AdditionJoin { 47 multiTable = true 48 } 49 } 50 51 if multiTable { 52 ctx = ContextWithToggles(ctx, Toggles{ 53 ToggleMultiTable: multiTable, 54 }) 55 } 56 57 e := Expr("SELECT") 58 59 if len(s.modifiers) > 0 { 60 for i := range s.modifiers { 61 e.WriteByte(' ') 62 e.WriteString(s.modifiers[i]) 63 } 64 } 65 66 sqlExpr := s.sqlExpr 67 68 if IsNilExpr(sqlExpr) { 69 sqlExpr = Expr("*") 70 } 71 72 e.WriteByte(' ') 73 e.WriteExpr(sqlExpr) 74 75 if !IsNilExpr(s.table) { 76 e.WriteString(" FROM ") 77 e.WriteExpr(s.table) 78 } 79 80 WriteAdditions(e, s.additions...) 81 82 return e.Ex(ctx) 83 } 84 85 func ForUpdate() *OtherAddition { 86 return AsAddition(Expr("FOR UPDATE")) 87 }