github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/databases/orm/qb_mysql.go (about) 1 // The original package is migrated from beego and modified, you can find orignal from following link: 2 // "github.com/beego/beego/" 3 // 4 // Copyright 2023 IAC. All Rights Reserved. 5 // 6 // Licensed under the Apache License, Version 2.0 (the "License"); 7 // you may not use this file except in compliance with the License. 8 // You may obtain a copy of the License at 9 // 10 // http://www.apache.org/licenses/LICENSE-2.0 11 // 12 // Unless required by applicable law or agreed to in writing, software 13 // distributed under the License is distributed on an "AS IS" BASIS, 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 18 package orm 19 20 import ( 21 "fmt" 22 "strconv" 23 "strings" 24 ) 25 26 // CommaSpace is the separation 27 const CommaSpace = ", " 28 29 // MySQLQueryBuilder is the SQL build 30 type MySQLQueryBuilder struct { 31 tokens []string 32 } 33 34 // Select will join the fields 35 func (qb *MySQLQueryBuilder) Select(fields ...string) QueryBuilder { 36 qb.tokens = append(qb.tokens, "SELECT", strings.Join(fields, CommaSpace)) 37 return qb 38 } 39 40 // ForUpdate add the FOR UPDATE clause 41 func (qb *MySQLQueryBuilder) ForUpdate() QueryBuilder { 42 qb.tokens = append(qb.tokens, "FOR UPDATE") 43 return qb 44 } 45 46 // From join the tables 47 func (qb *MySQLQueryBuilder) From(tables ...string) QueryBuilder { 48 qb.tokens = append(qb.tokens, "FROM", strings.Join(tables, CommaSpace)) 49 return qb 50 } 51 52 // InnerJoin INNER JOIN the table 53 func (qb *MySQLQueryBuilder) InnerJoin(table string) QueryBuilder { 54 qb.tokens = append(qb.tokens, "INNER JOIN", table) 55 return qb 56 } 57 58 // LeftJoin LEFT JOIN the table 59 func (qb *MySQLQueryBuilder) LeftJoin(table string) QueryBuilder { 60 qb.tokens = append(qb.tokens, "LEFT JOIN", table) 61 return qb 62 } 63 64 // RightJoin RIGHT JOIN the table 65 func (qb *MySQLQueryBuilder) RightJoin(table string) QueryBuilder { 66 qb.tokens = append(qb.tokens, "RIGHT JOIN", table) 67 return qb 68 } 69 70 // On join with on cond 71 func (qb *MySQLQueryBuilder) On(cond string) QueryBuilder { 72 qb.tokens = append(qb.tokens, "ON", cond) 73 return qb 74 } 75 76 // Where join the Where cond 77 func (qb *MySQLQueryBuilder) Where(cond string) QueryBuilder { 78 qb.tokens = append(qb.tokens, "WHERE", cond) 79 return qb 80 } 81 82 // And join the and cond 83 func (qb *MySQLQueryBuilder) And(cond string) QueryBuilder { 84 qb.tokens = append(qb.tokens, "AND", cond) 85 return qb 86 } 87 88 // Or join the or cond 89 func (qb *MySQLQueryBuilder) Or(cond string) QueryBuilder { 90 qb.tokens = append(qb.tokens, "OR", cond) 91 return qb 92 } 93 94 // In join the IN (vals) 95 func (qb *MySQLQueryBuilder) In(vals ...string) QueryBuilder { 96 qb.tokens = append(qb.tokens, "IN", "(", strings.Join(vals, CommaSpace), ")") 97 return qb 98 } 99 100 // OrderBy join the Order by fields 101 func (qb *MySQLQueryBuilder) OrderBy(fields ...string) QueryBuilder { 102 qb.tokens = append(qb.tokens, "ORDER BY", strings.Join(fields, CommaSpace)) 103 return qb 104 } 105 106 // Asc join the asc 107 func (qb *MySQLQueryBuilder) Asc() QueryBuilder { 108 qb.tokens = append(qb.tokens, "ASC") 109 return qb 110 } 111 112 // Desc join the desc 113 func (qb *MySQLQueryBuilder) Desc() QueryBuilder { 114 qb.tokens = append(qb.tokens, "DESC") 115 return qb 116 } 117 118 // Limit join the limit num 119 func (qb *MySQLQueryBuilder) Limit(limit int) QueryBuilder { 120 qb.tokens = append(qb.tokens, "LIMIT", strconv.Itoa(limit)) 121 return qb 122 } 123 124 // Offset join the offset num 125 func (qb *MySQLQueryBuilder) Offset(offset int) QueryBuilder { 126 qb.tokens = append(qb.tokens, "OFFSET", strconv.Itoa(offset)) 127 return qb 128 } 129 130 // GroupBy join the Group by fields 131 func (qb *MySQLQueryBuilder) GroupBy(fields ...string) QueryBuilder { 132 qb.tokens = append(qb.tokens, "GROUP BY", strings.Join(fields, CommaSpace)) 133 return qb 134 } 135 136 // Having join the Having cond 137 func (qb *MySQLQueryBuilder) Having(cond string) QueryBuilder { 138 qb.tokens = append(qb.tokens, "HAVING", cond) 139 return qb 140 } 141 142 // Update join the update table 143 func (qb *MySQLQueryBuilder) Update(tables ...string) QueryBuilder { 144 qb.tokens = append(qb.tokens, "UPDATE", strings.Join(tables, CommaSpace)) 145 return qb 146 } 147 148 // Set join the set kv 149 func (qb *MySQLQueryBuilder) Set(kv ...string) QueryBuilder { 150 qb.tokens = append(qb.tokens, "SET", strings.Join(kv, CommaSpace)) 151 return qb 152 } 153 154 // Delete join the Delete tables 155 func (qb *MySQLQueryBuilder) Delete(tables ...string) QueryBuilder { 156 qb.tokens = append(qb.tokens, "DELETE") 157 if len(tables) != 0 { 158 qb.tokens = append(qb.tokens, strings.Join(tables, CommaSpace)) 159 } 160 return qb 161 } 162 163 // InsertInto join the insert SQL 164 func (qb *MySQLQueryBuilder) InsertInto(table string, fields ...string) QueryBuilder { 165 qb.tokens = append(qb.tokens, "INSERT INTO", table) 166 if len(fields) != 0 { 167 fieldsStr := strings.Join(fields, CommaSpace) 168 qb.tokens = append(qb.tokens, "(", fieldsStr, ")") 169 } 170 return qb 171 } 172 173 // Values join the Values(vals) 174 func (qb *MySQLQueryBuilder) Values(vals ...string) QueryBuilder { 175 valsStr := strings.Join(vals, CommaSpace) 176 qb.tokens = append(qb.tokens, "VALUES", "(", valsStr, ")") 177 return qb 178 } 179 180 // Subquery join the sub as alias 181 func (qb *MySQLQueryBuilder) Subquery(sub string, alias string) string { 182 return fmt.Sprintf("(%s) AS %s", sub, alias) 183 } 184 185 // String join all tokens 186 func (qb *MySQLQueryBuilder) String() string { 187 s := strings.Join(qb.tokens, " ") 188 qb.tokens = qb.tokens[:0] 189 return s 190 }