Debian Codegraph @

builder_pgsql.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 "sort"
10 "strings"
11 )
12
13 // PgsqlBuilder is the builder for PostgreSQL databases.
14 type PgsqlBuilder struct {
15 *BaseBuilder
16 qb *BaseQueryBuilder
17 }
18
19 var _ Builder = &PgsqlBuilder{}
20
21 // NewPgsqlBuilder creates a new PgsqlBuilder instance.
22 func NewPgsqlBuilder(db *DB, executor Executor) Builder {
23 return &PgsqlBuilder{
24 NewBaseBuilder(db, executor),
25 NewBaseQueryBuilder(db),
26 }
27 }
28
29 // Select returns a new SelectQuery object that can be used to build a SELECT statement.
30 // The parameters to this method should be the list column names to be selected.
31 // A column name may have an optional alias name. For example, Select("id", "my_name AS name").
32 func (b *PgsqlBuilder) Select(cols ...string) *SelectQuery {
33 return NewSelectQuery(b, b.db).Select(cols...)
34 }
35
36 // Model returns a new ModelQuery object that can be used to perform model-based DB operations.
37 // The model passed to this method should be a pointer to a model struct.
38 func (b *PgsqlBuilder) Model(model interface{}) *ModelQuery {
39 return NewModelQuery(model, b.db.FieldMapper, b.db, b)
40 }
41
42 // GeneratePlaceholder generates an anonymous parameter placeholder with the given parameter ID.
43 func (b *PgsqlBuilder) GeneratePlaceholder(i int) string {
44 return fmt.Sprintf("$%v", i)
45 }
46
47 // QueryBuilder returns the query builder supporting the current DB.
48 func (b *PgsqlBuilder) QueryBuilder() QueryBuilder {
49 return b.qb
50 }
51
52 // Upsert creates a Query that represents an UPSERT SQL statement.
53 // Upsert inserts a row into the table if the primary key or unique index is not found.
54 // Otherwise it will update the row with the new values.
55 // The keys of cols are the column names, while the values of cols are the corresponding column
56 // values to be inserted.
57 func (b *PgsqlBuilder) Upsert(table string, cols Params, constraints ...string) *Query {
58 q := b.Insert(table, cols)
59
60 names := []string{}
61 for name := range cols {
62 names = append(names, name)
63 }
64 sort.Strings(names)
65
66 lines := []string{}
67 for _, name := range names {
68 value := cols[name]
69 name = b.db.QuoteColumnName(name)
70 if e, ok := value.(Expression); ok {
71 lines = append(lines, name+"="+e.Build(b.db, q.params))
72 } else {
73 lines = append(lines, fmt.Sprintf("%v={:p%v}", name, len(q.params)))
74 q.params[fmt.Sprintf("p%v", len(q.params))] = value
75 }
76 }
77
78 if len(constraints) > 0 {
79 c := b.quoteColumns(constraints)
80 q.sql += " ON CONFLICT (" + c + ") DO UPDATE SET " + strings.Join(lines, ", ")
81 } else {
82 q.sql += " ON CONFLICT DO UPDATE SET " + strings.Join(lines, ", ")
83 }
84
85 return b.NewQuery(q.sql).Bind(q.params)
86 }
87
88 // DropIndex creates a Query that can be used to remove the named index from a table.
89 func (b *PgsqlBuilder) DropIndex(table, name string) *Query {
90 sql := fmt.Sprintf("DROP INDEX %v", b.db.QuoteColumnName(name))
91 return b.NewQuery(sql)
92 }
93
94 // RenameTable creates a Query that can be used to rename a table.
95 func (b *PgsqlBuilder) RenameTable(oldName, newName string) *Query {
96 sql := fmt.Sprintf("ALTER TABLE %v RENAME TO %v", b.db.QuoteTableName(oldName), b.db.QuoteTableName(newName))
97 return b.NewQuery(sql)
98 }
99
100 // AlterColumn creates a Query that can be used to change the definition of a table column.
101 func (b *PgsqlBuilder) AlterColumn(table, col, typ string) *Query {
102 col = b.db.QuoteColumnName(col)
103 sql := fmt.Sprintf("ALTER TABLE %v ALTER COLUMN %v TYPE %v", b.db.QuoteTableName(table), col, typ)
104 return b.NewQuery(sql)
105 }