github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgSql/MysqlAst/SelectCommand.go (about) 1 package MysqlAst 2 3 type SelectCommand struct { 4 selectExprList Node 5 tableReferenceList Node 6 whereCondition WhereCondition 7 groupByList Node 8 have WhereCondition 9 orderByList Node 10 limit Node 11 isForUpdate bool 12 isLockInShareMode bool 13 } 14 15 func NewSelectCommand() *SelectCommand { 16 return &SelectCommand{} 17 } 18 19 func (c *SelectCommand) GetPrepareParameter() (output string, parameterList []string) { 20 output = "SELECT " 21 if c.selectExprList == nil { 22 output += "*" 23 } else { 24 output += c.selectExprList.GetText() 25 } 26 if c.tableReferenceList != nil { 27 output += " FROM " + c.tableReferenceList.GetText() 28 } 29 if c.whereCondition != nil && !c.whereCondition.IsEmpty() { 30 text, parameter := c.whereCondition.GetPrepareParameter() 31 output += " WHERE " + text 32 parameterList = append(parameterList, parameter...) 33 } 34 if c.groupByList != nil { 35 output += " GROUP BY " + c.groupByList.GetText() 36 } 37 if c.have != nil && c.have.IsEmpty() { 38 text, parameter := c.have.GetPrepareParameter() 39 output += " HAVE " + text 40 parameterList = append(parameterList, parameter...) 41 } 42 if c.orderByList != nil { 43 output += " ORDER BY " + c.orderByList.GetText() 44 } 45 if c.limit != nil { 46 output += " LIMIT " + c.limit.GetText() 47 } 48 if c.isForUpdate { 49 output += " FOR UPDATE " 50 } 51 if c.isLockInShareMode { 52 output += " LOCK IN SHARE MODE " 53 } 54 return 55 } 56 57 func (c *SelectCommand) Copy() *SelectCommand { 58 s := &SelectCommand{} 59 if c.selectExprList != nil { 60 s.selectExprList = c.selectExprList.Copy() 61 } 62 if c.tableReferenceList != nil { 63 s.tableReferenceList = c.tableReferenceList.Copy() 64 } 65 if c.whereCondition != nil { 66 s.whereCondition = c.whereCondition.Copy().(WhereCondition) 67 } 68 if c.groupByList != nil { 69 s.groupByList = c.groupByList.Copy() 70 } 71 if c.have != nil { 72 s.have = c.have.Copy().(WhereCondition) 73 } 74 if c.orderByList != nil { 75 s.orderByList = c.orderByList.Copy() 76 } 77 if c.limit != nil { 78 s.limit = c.limit.Copy() 79 } 80 s.isForUpdate = c.isForUpdate 81 s.isLockInShareMode = c.isLockInShareMode 82 return s 83 } 84 85 func (c *SelectCommand) Select(text string) *SelectCommand { 86 c.selectExprList = Text(text) 87 return c 88 } 89 func (c *SelectCommand) From(text string) *SelectCommand { 90 c.tableReferenceList = Text(text) 91 return c 92 } 93 94 func (c *SelectCommand) Where(text string, parameterList ...string) *SelectCommand { 95 c.whereCondition = Prepare(text, parameterList...) 96 return c 97 } 98 func (c *SelectCommand) WhereObj(obj WhereCondition) *SelectCommand { 99 c.whereCondition = obj 100 return c 101 } 102 func (c *SelectCommand) GroupBy(text string) *SelectCommand { 103 c.groupByList = Text(text) 104 return c 105 } 106 func (c *SelectCommand) Have(text string, parameterList ...string) *SelectCommand { 107 c.have = Prepare(text, parameterList...) 108 return c 109 } 110 func (c *SelectCommand) OrderBy(text string) *SelectCommand { 111 c.orderByList = Text(text) 112 return c 113 } 114 func (c *SelectCommand) Limit(text string) *SelectCommand { 115 c.limit = Text(text) 116 return c 117 } 118 func (c *SelectCommand) ForUpdate() *SelectCommand { 119 c.isForUpdate = true 120 return c 121 } 122 func (c *SelectCommand) LockInShareMode() *SelectCommand { 123 c.isLockInShareMode = true 124 return c 125 } 126 func (c *SelectCommand) GetWhereCondition() WhereCondition { 127 return c.whereCondition 128 } 129 func (c *SelectCommand) AddAndWhereCondition(node PreparedNode) *SelectCommand { 130 c.whereCondition = c.whereCondition.AddAndCondition(node) 131 return c 132 }