Debian Codegraph @

builder_oci.go

raw symbols SCIP
1 // Copyright 2016 Qiang Xue. All rights reserved.
2 // Use of this source code is governed by a MIT-style
3 // license that can be found in the LICENSE file.
4
5 package dbx
6
7 import (
8 "fmt"
9 )
10
11 // OciBuilder is the builder for Oracle databases.
12 type OciBuilder struct {
13 *BaseBuilder
14 qb *OciQueryBuilder
15 }
16
17 var _ Builder = &OciBuilder{}
18
19 // OciQueryBuilder is the query builder for Oracle databases.
20 type OciQueryBuilder struct {
21 *BaseQueryBuilder
22 }
23
24 // NewOciBuilder creates a new OciBuilder instance.
25 func NewOciBuilder(db *DB, executor Executor) Builder {
26 return &OciBuilder{
27 NewBaseBuilder(db, executor),
28 &OciQueryBuilder{NewBaseQueryBuilder(db)},
29 }
30 }
31
32 // Select returns a new SelectQuery object that can be used to build a SELECT statement.
33 // The parameters to this method should be the list column names to be selected.
34 // A column name may have an optional alias name. For example, Select("id", "my_name AS name").
35 func (b *OciBuilder) Select(cols ...string) *SelectQuery {
36 return NewSelectQuery(b, b.db).Select(cols...)
37 }
38
39 // Model returns a new ModelQuery object that can be used to perform model-based DB operations.
40 // The model passed to this method should be a pointer to a model struct.
41 func (b *OciBuilder) Model(model interface{}) *ModelQuery {
42 return NewModelQuery(model, b.db.FieldMapper, b.db, b)
43 }
44
45 // GeneratePlaceholder generates an anonymous parameter placeholder with the given parameter ID.
46 func (b *OciBuilder) GeneratePlaceholder(i int) string {
47 return fmt.Sprintf(":p%v", i)
48 }
49
50 // QueryBuilder returns the query builder supporting the current DB.
51 func (b *OciBuilder) QueryBuilder() QueryBuilder {
52 return b.qb
53 }
54
55 // DropIndex creates a Query that can be used to remove the named index from a table.
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 // RenameTable creates a Query that can be used to rename a table.
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 // AlterColumn creates a Query that can be used to change the definition of a table column.
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 // BuildOrderByAndLimit generates the ORDER BY and LIMIT clauses.
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 }