Debian Codegraph @

builder_sqlite.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 "errors"
9 "fmt"
10 "strings"
11 )
12
13 var _ QueryBuilder = &SqliteQueryBuilder{}
14
15 // SqliteQueryBuilder is the query builder for SQLite databases.
16 type SqliteQueryBuilder struct {
17 *BaseQueryBuilder
18 }
19
20 // BuildUnion generates a UNION clause from the given union information.
21 //
22 // This is similar to BaseQueryBuilder.BuildUnion but without the parenthesis wrapping.
23 func (b *SqliteQueryBuilder) BuildUnion(unions []UnionInfo, params Params) string {
24 if len(unions) == 0 {
25 return ""
26 }
27
28 sql := ""
29
30 for i, union := range unions {
31 if i > 0 {
32 sql += " "
33 }
34
35 for k, v := range union.Query.params {
36 params[k] = v
37 }
38
39 u := "UNION"
40 if union.All {
41 u = "UNION ALL"
42 }
43
44 sql += fmt.Sprintf("%v %v", u, union.Query.sql)
45 }
46
47 return sql
48 }
49
50 // CombineUnion combines the nonempty unionClause with the provided sql string.
51 //
52 // This is similar to BaseQueryBuilder.CombineUnion but without the parenthesis wrapping.
53 func (q *SqliteQueryBuilder) CombineUnion(sql string, unionClause string) string {
54 if unionClause == "" {
55 return sql
56 }
57
58 return fmt.Sprintf("%v %v", sql, unionClause)
59 }
60
61 // -------------------------------------------------------------------
62
63 // SqliteBuilder is the builder for SQLite databases.
64 type SqliteBuilder struct {
65 *BaseBuilder
66 qb *SqliteQueryBuilder
67 }
68
69 var _ Builder = &SqliteBuilder{}
70
71 // NewSqliteBuilder creates a new SqliteBuilder instance.
72 func NewSqliteBuilder(db *DB, executor Executor) Builder {
73 return &SqliteBuilder{
74 NewBaseBuilder(db, executor),
75 &SqliteQueryBuilder{NewBaseQueryBuilder(db)},
76 }
77 }
78
79 // QueryBuilder returns the query builder supporting the current DB.
80 func (b *SqliteBuilder) QueryBuilder() QueryBuilder {
81 return b.qb
82 }
83
84 // Select returns a new SelectQuery object that can be used to build a SELECT statement.
85 // The parameters to this method should be the list column names to be selected.
86 // A column name may have an optional alias name. For example, Select("id", "my_name AS name").
87 func (b *SqliteBuilder) Select(cols ...string) *SelectQuery {
88 return NewSelectQuery(b, b.db).Select(cols...)
89 }
90
91 // Model returns a new ModelQuery object that can be used to perform model-based DB operations.
92 // The model passed to this method should be a pointer to a model struct.
93 func (b *SqliteBuilder) Model(model interface{}) *ModelQuery {
94 return NewModelQuery(model, b.db.FieldMapper, b.db, b)
95 }
96
97 // QuoteSimpleTableName quotes a simple table name.
98 // A simple table name does not contain any schema prefix.
99 func (b *SqliteBuilder) QuoteSimpleTableName(s string) string {
100 if strings.ContainsAny(s, "`") {
101 return s
102 }
103 return "`" + s + "`"
104 }
105
106 // QuoteSimpleColumnName quotes a simple column name.
107 // A simple column name does not contain any table prefix.
108 func (b *SqliteBuilder) QuoteSimpleColumnName(s string) string {
109 if strings.Contains(s, "`") || s == "*" {
110 return s
111 }
112 return "`" + s + "`"
113 }
114
115 // DropIndex creates a Query that can be used to remove the named index from a table.
116 func (b *SqliteBuilder) DropIndex(table, name string) *Query {
117 sql := fmt.Sprintf("DROP INDEX %v", b.db.QuoteColumnName(name))
118 return b.NewQuery(sql)
119 }
120
121 // TruncateTable creates a Query that can be used to truncate a table.
122 func (b *SqliteBuilder) TruncateTable(table string) *Query {
123 sql := "DELETE FROM " + b.db.QuoteTableName(table)
124 return b.NewQuery(sql)
125 }
126
127 // RenameTable creates a Query that can be used to rename a table.
128 func (b *SqliteBuilder) RenameTable(oldName, newName string) *Query {
129 sql := fmt.Sprintf("ALTER TABLE %v RENAME TO %v", b.db.QuoteTableName(oldName), b.db.QuoteTableName(newName))
130 return b.NewQuery(sql)
131 }
132
133 // AlterColumn creates a Query that can be used to change the definition of a table column.
134 func (b *SqliteBuilder) AlterColumn(table, col, typ string) *Query {
135 q := b.NewQuery("")
136 q.LastError = errors.New("SQLite does not support altering column")
137 return q
138 }
139
140 // AddPrimaryKey creates a Query that can be used to specify primary key(s) for a table.
141 // The "name" parameter specifies the name of the primary key constraint.
142 func (b *SqliteBuilder) AddPrimaryKey(table, name string, cols ...string) *Query {
143 q := b.NewQuery("")
144 q.LastError = errors.New("SQLite does not support adding primary key")
145 return q
146 }
147
148 // DropPrimaryKey creates a Query that can be used to remove the named primary key constraint from a table.
149 func (b *SqliteBuilder) DropPrimaryKey(table, name string) *Query {
150 q := b.NewQuery("")
151 q.LastError = errors.New("SQLite does not support dropping primary key")
152 return q
153 }
154
155 // AddForeignKey creates a Query that can be used to add a foreign key constraint to a table.
156 // The length of cols and refCols must be the same as they refer to the primary and referential columns.
157 // The optional "options" parameters will be appended to the SQL statement. They can be used to
158 // specify options such as "ON DELETE CASCADE".
159 func (b *SqliteBuilder) AddForeignKey(table, name string, cols, refCols []string, refTable string, options ...string) *Query {
160 q := b.NewQuery("")
161 q.LastError = errors.New("SQLite does not support adding foreign keys")
162 return q
163 }
164
165 // DropForeignKey creates a Query that can be used to remove the named foreign key constraint from a table.
166 func (b *SqliteBuilder) DropForeignKey(table, name string) *Query {
167 q := b.NewQuery("")
168 q.LastError = errors.New("SQLite does not support dropping foreign keys")
169 return q
170 }