| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package dbx |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
type OciBuilder struct { |
| 13 |
*BaseBuilder |
| 14 |
qb *OciQueryBuilder |
| 15 |
} |
| 16 |
|
| 17 |
var _ Builder = &OciBuilder{} |
| 18 |
|
| 19 |
|
| 20 |
type OciQueryBuilder struct { |
| 21 |
*BaseQueryBuilder |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
func NewOciBuilder(db *DB, executor Executor) Builder { |
| 26 |
return &OciBuilder{ |
| 27 |
NewBaseBuilder(db, executor), |
| 28 |
&OciQueryBuilder{NewBaseQueryBuilder(db)}, |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
func (b *OciBuilder) Select(cols ...string) *SelectQuery { |
| 36 |
return NewSelectQuery(b, b.db).Select(cols...) |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
func (b *OciBuilder) Model(model interface{}) *ModelQuery { |
| 42 |
return NewModelQuery(model, b.db.FieldMapper, b.db, b) |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
func (b *OciBuilder) GeneratePlaceholder(i int) string { |
| 47 |
return fmt.Sprintf(":p%v", i) |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
func (b *OciBuilder) QueryBuilder() QueryBuilder { |
| 52 |
return b.qb |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
func (b *OciBuilder) DropIndex(table, name string) *Query { |
| 57 |
sql := fmt.Sprintf("DROP INDEX %v", b.db.QuoteColumnName(name)) |
| 58 |
return b.NewQuery(sql) |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
func (b *OciBuilder) RenameTable(oldName, newName string) *Query { |
| 63 |
sql := fmt.Sprintf("ALTER TABLE %v RENAME TO %v", b.db.QuoteTableName(oldName), b.db.QuoteTableName(newName)) |
| 64 |
return b.NewQuery(sql) |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
func (b *OciBuilder) AlterColumn(table, col, typ string) *Query { |
| 69 |
col = b.db.QuoteColumnName(col) |
| 70 |
sql := fmt.Sprintf("ALTER TABLE %v MODIFY %v %v", b.db.QuoteTableName(table), col, typ) |
| 71 |
return b.NewQuery(sql) |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
func (q *OciQueryBuilder) BuildOrderByAndLimit(sql string, cols []string, limit int64, offset int64) string { |
| 76 |
if orderBy := q.BuildOrderBy(cols); orderBy != "" { |
| 77 |
sql += "\n" + orderBy |
| 78 |
} |
| 79 |
|
| 80 |
c := "" |
| 81 |
if offset > 0 { |
| 82 |
c = fmt.Sprintf("rowNumId > %v", offset) |
| 83 |
} |
| 84 |
if limit >= 0 { |
| 85 |
if c != "" { |
| 86 |
c += " AND " |
| 87 |
} |
| 88 |
c += fmt.Sprintf("rowNum <= %v", limit) |
| 89 |
} |
| 90 |
|
| 91 |
if c == "" { |
| 92 |
return sql |
| 93 |
} |
| 94 |
|
| 95 |
return `WITH USER_SQL AS (` + sql + `), |
| 96 |
PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL) |
| 97 |
SELECT * FROM PAGINATION WHERE ` + c |
| 98 |
} |