forked from go-gorp/gorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialect.go
205 lines (171 loc) · 6.94 KB
/
dialect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2012 James Cooper. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gorp
import (
"fmt"
"reflect"
)
// DialectType represents the type of database dialect
type DialectType string
const (
// MySQL dialect
MySQL DialectType = "mysql"
// PostgreSQL dialect
PostgreSQL DialectType = "postgres"
// SQLite dialect
SQLite DialectType = "sqlite3"
// Oracle dialect
Oracle DialectType = "oracle"
// SQLServer dialect
SQLServer DialectType = "sqlserver"
// Snowflake dialect
Snowflake DialectType = "snowflake"
)
// ColumnOptions represents options for column creation
type ColumnOptions struct {
// MaxSize is the maximum size of the column (e.g., VARCHAR(MaxSize))
MaxSize int
// IsAutoIncr indicates if the column is auto-incrementing
IsAutoIncr bool
// IsPK indicates if the column is a primary key
IsPK bool
// IsNullable indicates if the column can be NULL
IsNullable bool
// DefaultValue specifies the default value for the column
DefaultValue string
}
// IndexOptions represents options for index creation
type IndexOptions struct {
// Name is the name of the index
Name string
// Columns are the columns to include in the index
Columns []string
// Type is the type of index (e.g., BTREE, HASH, GIN, GIST)
Type IndexType
// Unique indicates if this is a unique index
Unique bool
// Using specifies the index method
Using string
// Where specifies a partial index condition
Where string
}
// The Dialect interface encapsulates behaviors that differ across
// SQL databases. At present the Dialect is only used by CreateTables()
// but this could change in the future
type Dialect interface {
// Type returns the type of dialect
Type() DialectType
// QuerySuffix adds a Suffix to any query, usually ";"
QuerySuffix() string
// ToSqlType returns the SQL column type to use when creating a
// table of the given Go Type. maxsize can be used to switch based on
// size. For example, in MySQL []byte could map to BLOB, MEDIUMBLOB,
// or LONGBLOB depending on the maxsize
ToSqlType(val reflect.Type, opts ColumnOptions) string
// AutoIncrStr returns string to append to primary key column definitions
AutoIncrStr() string
// AutoIncrBindValue returns string to bind autoincrement columns to.
// Empty string will remove reference to those columns in the INSERT statement.
AutoIncrBindValue() string
// AutoIncrInsertSuffix returns Suffix for inserting auto-increment columns
AutoIncrInsertSuffix(col *ColumnMap) string
// CreateTableSuffix returns string to append to "create table" statement
// for vendor specific table attributes
CreateTableSuffix() string
// CreateIndexSuffix returns string to append to "create index" statement
CreateIndexSuffix() string
// DropIndexSuffix returns string to append to "drop index" statement
DropIndexSuffix() string
// TruncateClause returns string to truncate tables
TruncateClause() string
// BindVar returns bind variable string to use when forming SQL statements
// in many dbs it is "?", but Postgres appears to use $1
//
// i is a zero based index of the bind variable in this statement
BindVar(i int) string
// QuoteField handles quoting of a field name to ensure that it doesn't
// raise any SQL parsing exceptions by using a reserved word as a field name.
QuoteField(field string) string
// QuotedTableForQuery handles building up of a schema.database string
// that is compatible with the given dialect
//
// schema - The schema that <table> lives in
// table - The table name
QuotedTableForQuery(schema string, table string) string
// IfSchemaNotExists returns existence clause for schema creation
IfSchemaNotExists(command, schema string) string
// IfTableExists returns existence clause for table operations
IfTableExists(command, schema, table string) string
// IfTableNotExists returns non-existence clause for table operations
IfTableNotExists(command, schema, table string) string
// Placeholder returns the placeholder for a column value
Placeholder(i int) string
// SupportsCascade returns whether the dialect supports CASCADE in DROP TABLE
SupportsCascade() bool
// SupportsMultipleSchema returns whether the dialect supports multiple schemas
SupportsMultipleSchema() bool
// SupportsLastInsertId returns whether the dialect supports LastInsertId
SupportsLastInsertId() bool
}
// TypedDialect provides type-safe dialect operations
type TypedDialect[T any] struct {
dialect Dialect
}
// NewTypedDialect creates a new TypedDialect for type T
func NewTypedDialect[T any](dialect Dialect) *TypedDialect[T] {
return &TypedDialect[T]{dialect: dialect}
}
// ToColumnType returns the SQL column type for the given field
func (d *TypedDialect[T]) ToColumnType(field string, opts ColumnOptions) (string, error) {
var zero T
t := reflect.TypeOf(zero)
f, ok := t.FieldByName(field)
if !ok {
return "", fmt.Errorf("field %s not found in type %s", field, t.Name())
}
return d.dialect.ToSqlType(f.Type, opts), nil
}
// IntegerAutoIncrInserter is implemented by dialects that can perform
// inserts with automatically incremented integer primary keys. If
// the dialect can handle automatic assignment of more than just
// integers, see TargetedAutoIncrInserter.
type IntegerAutoIncrInserter interface {
InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error)
}
// TargetedAutoIncrInserter is implemented by dialects that can
// perform automatic assignment of any primary key type (i.e. strings
// for uuids, integers for serials, etc).
type TargetedAutoIncrInserter interface {
// InsertAutoIncrToTarget runs an insert operation and assigns the
// automatically generated primary key directly to the passed in
// target. The target should be a pointer to the primary key
// field of the value being inserted.
InsertAutoIncrToTarget(exec SqlExecutor, insertSql string, target interface{}, params ...interface{}) error
}
// TargetQueryInserter is implemented by dialects that can perform
// assignment of integer primary key type by executing a query
// like "select sequence.currval from dual".
type TargetQueryInserter interface {
// InsertQueryToTarget runs an insert operation and assigns the
// automatically generated primary key retrieved by the query
// extracted from the GeneratedIdQuery field of the id column.
InsertQueryToTarget(exec SqlExecutor, insertSql, idSql string, target interface{}, params ...interface{}) error
}
// ValidateDialect checks if the given dialect type is valid
func ValidateDialect(d DialectType) error {
switch d {
case MySQL, PostgreSQL, SQLite, Oracle, SQLServer, Snowflake:
return nil
default:
return &InvalidDialectError{Dialect: string(d)}
}
}
// standardInsertAutoIncr provides a standard implementation of auto-increment insert
func standardInsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error) {
res, err := exec.Exec(insertSql, params...)
if err != nil {
return 0, err
}
return res.LastInsertId()
}