| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package dbx |
| 6 |
|
| 7 |
import ( |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"sort" |
| 11 |
"strings" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
type Builder interface { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
NewQuery(string) *Query |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
Select(...string) *SelectQuery |
| 26 |
|
| 27 |
|
| 28 |
Model(interface{}) *ModelQuery |
| 29 |
|
| 30 |
|
| 31 |
GeneratePlaceholder(int) string |
| 32 |
|
| 33 |
|
| 34 |
Quote(string) string |
| 35 |
|
| 36 |
|
| 37 |
QuoteSimpleTableName(string) string |
| 38 |
|
| 39 |
|
| 40 |
QuoteSimpleColumnName(string) string |
| 41 |
|
| 42 |
|
| 43 |
QueryBuilder() QueryBuilder |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
Insert(table string, cols Params) *Query |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
Upsert(table string, cols Params, constraints ...string) *Query |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
Update(table string, cols Params, where Expression) *Query |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
Delete(table string, where Expression) *Query |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
CreateTable(table string, cols map[string]string, options ...string) *Query |
| 69 |
|
| 70 |
RenameTable(oldName, newName string) *Query |
| 71 |
|
| 72 |
DropTable(table string) *Query |
| 73 |
|
| 74 |
TruncateTable(table string) *Query |
| 75 |
|
| 76 |
|
| 77 |
AddColumn(table, col, typ string) *Query |
| 78 |
|
| 79 |
DropColumn(table, col string) *Query |
| 80 |
|
| 81 |
RenameColumn(table, oldName, newName string) *Query |
| 82 |
|
| 83 |
AlterColumn(table, col, typ string) *Query |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
AddPrimaryKey(table, name string, cols ...string) *Query |
| 88 |
|
| 89 |
DropPrimaryKey(table, name string) *Query |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
AddForeignKey(table, name string, cols, refCols []string, refTable string, options ...string) *Query |
| 96 |
|
| 97 |
DropForeignKey(table, name string) *Query |
| 98 |
|
| 99 |
|
| 100 |
CreateIndex(table, name string, cols ...string) *Query |
| 101 |
|
| 102 |
CreateUniqueIndex(table, name string, cols ...string) *Query |
| 103 |
|
| 104 |
DropIndex(table, name string) *Query |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
type BaseBuilder struct { |
| 109 |
db *DB |
| 110 |
executor Executor |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
func NewBaseBuilder(db *DB, executor Executor) *BaseBuilder { |
| 115 |
return &BaseBuilder{db, executor} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
func (b *BaseBuilder) DB() *DB { |
| 120 |
return b.db |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
func (b *BaseBuilder) Executor() Executor { |
| 125 |
return b.executor |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
func (b *BaseBuilder) NewQuery(sql string) *Query { |
| 132 |
return NewQuery(b.db, b.executor, sql) |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
func (b *BaseBuilder) GeneratePlaceholder(int) string { |
| 137 |
return "?" |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
func (b *BaseBuilder) Quote(s string) string { |
| 142 |
return "'" + strings.Replace(s, "'", "''", -1) + "'" |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
func (b *BaseBuilder) QuoteSimpleTableName(s string) string { |
| 148 |
if strings.Contains(s, `"`) { |
| 149 |
return s |
| 150 |
} |
| 151 |
return `"` + s + `"` |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
func (b *BaseBuilder) QuoteSimpleColumnName(s string) string { |
| 157 |
if strings.Contains(s, `"`) || s == "*" { |
| 158 |
return s |
| 159 |
} |
| 160 |
return `"` + s + `"` |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
func (b *BaseBuilder) Insert(table string, cols Params) *Query { |
| 167 |
names := make([]string, 0, len(cols)) |
| 168 |
for name := range cols { |
| 169 |
names = append(names, name) |
| 170 |
} |
| 171 |
sort.Strings(names) |
| 172 |
|
| 173 |
params := Params{} |
| 174 |
columns := make([]string, 0, len(names)) |
| 175 |
values := make([]string, 0, len(names)) |
| 176 |
for _, name := range names { |
| 177 |
columns = append(columns, b.db.QuoteColumnName(name)) |
| 178 |
value := cols[name] |
| 179 |
if e, ok := value.(Expression); ok { |
| 180 |
values = append(values, e.Build(b.db, params)) |
| 181 |
} else { |
| 182 |
values = append(values, fmt.Sprintf("{:p%v}", len(params))) |
| 183 |
params[fmt.Sprintf("p%v", len(params))] = value |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
var sql string |
| 188 |
if len(names) == 0 { |
| 189 |
sql = fmt.Sprintf("INSERT INTO %v DEFAULT VALUES", b.db.QuoteTableName(table)) |
| 190 |
} else { |
| 191 |
sql = fmt.Sprintf("INSERT INTO %v (%v) VALUES (%v)", |
| 192 |
b.db.QuoteTableName(table), |
| 193 |
strings.Join(columns, ", "), |
| 194 |
strings.Join(values, ", "), |
| 195 |
) |
| 196 |
} |
| 197 |
|
| 198 |
return b.NewQuery(sql).Bind(params) |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
func (b *BaseBuilder) Upsert(table string, cols Params, constraints ...string) *Query { |
| 207 |
q := b.NewQuery("") |
| 208 |
q.LastError = errors.New("Upsert is not supported") |
| 209 |
return q |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
func (b *BaseBuilder) Update(table string, cols Params, where Expression) *Query { |
| 217 |
names := make([]string, 0, len(cols)) |
| 218 |
for name := range cols { |
| 219 |
names = append(names, name) |
| 220 |
} |
| 221 |
sort.Strings(names) |
| 222 |
|
| 223 |
params := Params{} |
| 224 |
lines := make([]string, 0, len(names)) |
| 225 |
for _, name := range names { |
| 226 |
value := cols[name] |
| 227 |
name = b.db.QuoteColumnName(name) |
| 228 |
if e, ok := value.(Expression); ok { |
| 229 |
lines = append(lines, name+"="+e.Build(b.db, params)) |
| 230 |
} else { |
| 231 |
lines = append(lines, fmt.Sprintf("%v={:p%v}", name, len(params))) |
| 232 |
params[fmt.Sprintf("p%v", len(params))] = value |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
sql := fmt.Sprintf("UPDATE %v SET %v", b.db.QuoteTableName(table), strings.Join(lines, ", ")) |
| 237 |
if where != nil { |
| 238 |
w := where.Build(b.db, params) |
| 239 |
if w != "" { |
| 240 |
sql += " WHERE " + w |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return b.NewQuery(sql).Bind(params) |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
func (b *BaseBuilder) Delete(table string, where Expression) *Query { |
| 251 |
sql := "DELETE FROM " + b.db.QuoteTableName(table) |
| 252 |
params := Params{} |
| 253 |
if where != nil { |
| 254 |
w := where.Build(b.db, params) |
| 255 |
if w != "" { |
| 256 |
sql += " WHERE " + w |
| 257 |
} |
| 258 |
} |
| 259 |
return b.NewQuery(sql).Bind(params) |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
func (b *BaseBuilder) CreateTable(table string, cols map[string]string, options ...string) *Query { |
| 266 |
names := []string{} |
| 267 |
for name := range cols { |
| 268 |
names = append(names, name) |
| 269 |
} |
| 270 |
sort.Strings(names) |
| 271 |
|
| 272 |
columns := []string{} |
| 273 |
for _, name := range names { |
| 274 |
columns = append(columns, b.db.QuoteColumnName(name)+" "+cols[name]) |
| 275 |
} |
| 276 |
|
| 277 |
sql := fmt.Sprintf("CREATE TABLE %v (%v)", b.db.QuoteTableName(table), strings.Join(columns, ", ")) |
| 278 |
for _, opt := range options { |
| 279 |
sql += " " + opt |
| 280 |
} |
| 281 |
|
| 282 |
return b.NewQuery(sql) |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
func (b *BaseBuilder) RenameTable(oldName, newName string) *Query { |
| 287 |
sql := fmt.Sprintf("RENAME TABLE %v TO %v", b.db.QuoteTableName(oldName), b.db.QuoteTableName(newName)) |
| 288 |
return b.NewQuery(sql) |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
func (b *BaseBuilder) DropTable(table string) *Query { |
| 293 |
sql := "DROP TABLE " + b.db.QuoteTableName(table) |
| 294 |
return b.NewQuery(sql) |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
func (b *BaseBuilder) TruncateTable(table string) *Query { |
| 299 |
sql := "TRUNCATE TABLE " + b.db.QuoteTableName(table) |
| 300 |
return b.NewQuery(sql) |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
func (b *BaseBuilder) AddColumn(table, col, typ string) *Query { |
| 305 |
sql := fmt.Sprintf("ALTER TABLE %v ADD %v %v", b.db.QuoteTableName(table), b.db.QuoteColumnName(col), typ) |
| 306 |
return b.NewQuery(sql) |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
func (b *BaseBuilder) DropColumn(table, col string) *Query { |
| 311 |
sql := fmt.Sprintf("ALTER TABLE %v DROP COLUMN %v", b.db.QuoteTableName(table), b.db.QuoteColumnName(col)) |
| 312 |
return b.NewQuery(sql) |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
func (b *BaseBuilder) RenameColumn(table, oldName, newName string) *Query { |
| 317 |
sql := fmt.Sprintf("ALTER TABLE %v RENAME COLUMN %v TO %v", b.db.QuoteTableName(table), b.db.QuoteColumnName(oldName), b.db.QuoteColumnName(newName)) |
| 318 |
return b.NewQuery(sql) |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
func (b *BaseBuilder) AlterColumn(table, col, typ string) *Query { |
| 323 |
col = b.db.QuoteColumnName(col) |
| 324 |
sql := fmt.Sprintf("ALTER TABLE %v CHANGE %v %v %v", b.db.QuoteTableName(table), col, col, typ) |
| 325 |
return b.NewQuery(sql) |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
func (b *BaseBuilder) AddPrimaryKey(table, name string, cols ...string) *Query { |
| 331 |
sql := fmt.Sprintf("ALTER TABLE %v ADD CONSTRAINT %v PRIMARY KEY (%v)", |
| 332 |
b.db.QuoteTableName(table), |
| 333 |
b.db.QuoteColumnName(name), |
| 334 |
b.quoteColumns(cols)) |
| 335 |
return b.NewQuery(sql) |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
func (b *BaseBuilder) DropPrimaryKey(table, name string) *Query { |
| 340 |
sql := fmt.Sprintf("ALTER TABLE %v DROP CONSTRAINT %v", b.db.QuoteTableName(table), b.db.QuoteColumnName(name)) |
| 341 |
return b.NewQuery(sql) |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
func (b *BaseBuilder) AddForeignKey(table, name string, cols, refCols []string, refTable string, options ...string) *Query { |
| 349 |
sql := fmt.Sprintf("ALTER TABLE %v ADD CONSTRAINT %v FOREIGN KEY (%v) REFERENCES %v (%v)", |
| 350 |
b.db.QuoteTableName(table), |
| 351 |
b.db.QuoteColumnName(name), |
| 352 |
b.quoteColumns(cols), |
| 353 |
b.db.QuoteTableName(refTable), |
| 354 |
b.quoteColumns(refCols)) |
| 355 |
for _, opt := range options { |
| 356 |
sql += " " + opt |
| 357 |
} |
| 358 |
return b.NewQuery(sql) |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
func (b *BaseBuilder) DropForeignKey(table, name string) *Query { |
| 363 |
sql := fmt.Sprintf("ALTER TABLE %v DROP CONSTRAINT %v", b.db.QuoteTableName(table), b.db.QuoteColumnName(name)) |
| 364 |
return b.NewQuery(sql) |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
func (b *BaseBuilder) CreateIndex(table, name string, cols ...string) *Query { |
| 369 |
sql := fmt.Sprintf("CREATE INDEX %v ON %v (%v)", |
| 370 |
b.db.QuoteColumnName(name), |
| 371 |
b.db.QuoteTableName(table), |
| 372 |
b.quoteColumns(cols)) |
| 373 |
return b.NewQuery(sql) |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
func (b *BaseBuilder) CreateUniqueIndex(table, name string, cols ...string) *Query { |
| 378 |
sql := fmt.Sprintf("CREATE UNIQUE INDEX %v ON %v (%v)", |
| 379 |
b.db.QuoteColumnName(name), |
| 380 |
b.db.QuoteTableName(table), |
| 381 |
b.quoteColumns(cols)) |
| 382 |
return b.NewQuery(sql) |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
func (b *BaseBuilder) DropIndex(table, name string) *Query { |
| 387 |
sql := fmt.Sprintf("DROP INDEX %v ON %v", b.db.QuoteColumnName(name), b.db.QuoteTableName(table)) |
| 388 |
return b.NewQuery(sql) |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
func (b *BaseBuilder) quoteColumns(cols []string) string { |
| 393 |
s := "" |
| 394 |
for i, col := range cols { |
| 395 |
if i == 0 { |
| 396 |
s = b.db.QuoteColumnName(col) |
| 397 |
} else { |
| 398 |
s += ", " + b.db.QuoteColumnName(col) |
| 399 |
} |
| 400 |
} |
| 401 |
return s |
| 402 |
} |