-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialector.go
170 lines (144 loc) · 4.51 KB
/
dialector.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
package oracle
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/pkg/errors"
"gorm.io/gorm"
"gorm.io/gorm/callbacks"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
"gorm.io/gorm/migrator"
"gorm.io/gorm/schema"
"gorm.io/gorm/utils"
)
/*
type Dialector interface {
Name() string
Initialize(*DB) error
Migrator(db *DB) Migrator
DataTypeOf(*schema.Field) string
DefaultValueOf(*schema.Field) clause.Expression
BindVarTo(writer clause.Writer, stmt *Statement, v interface{})
QuoteTo(clause.Writer, string)
Explain(sql string, vars ...interface{}) string
}
*/
// https://gorm.io/docs/write_driver.html
const (
dialectorName string = "oracle"
ctxKeyIsBatchInsert string = "is_batch_insert"
ctxKeyNextFieldIndex string = "next_field_index"
)
var (
// CreateClauses create clauses
CreateClauses = []string{"INSERT", "VALUES", "ON CONFLICT", "RETURNING"}
// defaultDatetimePrecision = 3
)
type Dialector struct {
*Config
}
// Name implements gorm.Dialector interface
func (dialector Dialector) Name() string {
return dialectorName
}
// Initialize implements gorm.Dialector interface
func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
ctx := context.Background()
// register callbacks
callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{
CreateClauses: CreateClauses,
})
if dialector.DriverName == "" {
dialector.DriverName = dialectorName
}
if dialector.Conn != nil {
db.ConnPool = dialector.Conn
} else {
db.ConnPool, err = sql.Open(dialector.DriverName, dialector.DSN)
if err != nil {
return errors.Wrapf(err, "sql.Open failed")
}
}
if !dialector.Config.SkipInitializeWithVersion {
err = db.ConnPool.QueryRowContext(ctx, "SELECT * FROM v$version WHERE banner LIKE 'Oracle%'").Scan(&dialector.ServerVersion)
if err != nil {
return errors.Wrapf(err, "db.ConnPool.QueryRowContext failed")
}
// https://en.wikipedia.org/wiki/Oracle_Database
// TEST:tested: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
if strings.Contains(dialector.ServerVersion, "12c") ||
strings.Contains(dialector.ServerVersion, "18c") ||
strings.Contains(dialector.ServerVersion, "19c") ||
strings.Contains(dialector.ServerVersion, "21c") {
dialector.Config.supportIdentity = true
dialector.Config.supportOffsetFetch = true
}
}
for k, v := range dialector.ClauseBuilders() {
db.ClauseBuilders[k] = v
}
return
}
// Migrator implements gorm.Dialector interface
func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
return Migrator{
Migrator: migrator.Migrator{
Config: migrator.Config{
DB: db,
Dialector: dialector,
CreateIndexAfterCreateTable: true,
},
},
Dialector: dialector,
}
}
// DataTypeOf implements gorm.Dialector interface
func (dialector Dialector) DataTypeOf(field *schema.Field) string {
switch field.DataType {
case schema.Bool:
return "boolean"
case schema.Int, schema.Uint:
return dialector.getSchemaIntAndUnitType(field)
case schema.Float:
return dialector.getSchemaFloatType(field)
case schema.String:
return dialector.getSchemaStringType(field)
case schema.Time:
return dialector.getSchemaTimeType(field)
case schema.Bytes:
return dialector.getSchemaBytesType(field)
default:
return dialector.getSchemaCustomType(field)
}
}
// DefaultValueOf implements gorm.Dialector interface
func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression {
return clause.Expr{SQL: "NULL"}
}
// BindVarTo implements gorm.Dialector interface
func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
isInsert := utils.Contains(stmt.BuildClauses, "INSERT")
if !isInsert {
writer.WriteString(fmt.Sprintf(":p%d", len(stmt.Vars)))
return
}
writer.WriteString(dialector.bindVarParameter(stmt))
}
// QuoteTo implements gorm.Dialector interface
func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {
writer.WriteString(str)
}
// Explain implements gorm.Dialector interface
func (dialector Dialector) Explain(sql string, vars ...interface{}) string {
return logger.ExplainSQL(sql, nil, `'`, vars...)
}
// SavePoint implements gorm.SavePointerDialectorInterface interface
func (dialector Dialector) SavePoint(tx *gorm.DB, name string) error {
return tx.Exec("SAVEPOINT " + name).Error
}
// RollbackTo implements gorm.SavePointerDialectorInterface interface
func (dialector Dialector) RollbackTo(tx *gorm.DB, name string) error {
return tx.Exec("ROLLBACK TO SAVEPOINT " + name).Error
}