forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dialect_oracle.go
186 lines (151 loc) · 4.83 KB
/
dialect_oracle.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
package gorm
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
type SequenceStore struct {
TableName string `gorm:"primary_key"`
Sequence float64 `gorm:"not null;`
}
var sequenceStoreExists bool
func buildFakeSequenceGenerator(sequenceName string, scope *Scope) (float64, bool) {
if sequenceName != "AUTO_INCREMENT" {
return 0, false
}
scope.Log("buildFakeSequenceGenerator: Do not use this feature in a production environment. You must create your own sequence!")
var nextVal float64 = 0
if !sequenceStoreExists {
scope.NewDB().CreateTable(&SequenceStore{})
sequenceStoreExists = true
}
var r SequenceStore
scope.NewDB().Model(&SequenceStore{}).Where("TABLE_NAME = ?", scope.QuotedTableName()).Scan(&r)
if len(r.TableName) == 0 {
r.TableName = scope.QuotedTableName()
r.Sequence = 0
}
r.Sequence += 1
scope.NewDB().Save(&r)
nextVal = r.Sequence
return nextVal, true
}
func setIdentityInsert(scope *Scope) {
if scope.Dialect().GetName() == "goracle" {
for _, field := range scope.PrimaryFields() {
if seq, ok := field.TagSettings["AUTO_INCREMENT"]; ok && field.IsBlank {
var nextVal float64
if nextVal, ok = buildFakeSequenceGenerator(seq, scope); !ok {
stmt := fmt.Sprintf("SELECT %s.nextval %s", seq, scope.Dialect().SelectFromDummyTable())
row := scope.NewDB().Raw(stmt).Row()
row.Scan(&nextVal)
}
scope.SetColumn(field.Name, nextVal)
scope.InstanceSet("oracle:sequence_insert_on", true)
}
}
}
}
type oracle struct {
db SQLCommon
DefaultForeignKeyNamer
}
func init() {
DefaultCallback.Create().After("gorm:begin_transaction").Register("oracle:set_identity_insert", setIdentityInsert)
RegisterDialect("goracle", &oracle{})
}
func (oracle) GetName() string {
return "goracle"
}
func (d *oracle) SetDB(db SQLCommon) {
d.db = db
}
func (oracle) BindVar(i int) string {
return fmt.Sprintf(":%d", i)
}
func (oracle) Quote(key string) string {
return fmt.Sprintf("%s", strings.ToUpper(key))
}
func (d *oracle) DataTypeOf(field *StructField) string {
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field, d)
if sqlType == "" {
switch dataValue.Kind() {
case reflect.Bool:
sqlType = "CHAR(1)"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
sqlType = "INTEGER"
case reflect.Int64, reflect.Uint64:
sqlType = "NUMBER"
case reflect.Float32, reflect.Float64:
sqlType = "FLOAT"
case reflect.String:
if size > 0 && size < 4000 {
sqlType = fmt.Sprintf("VARCHAR(%d)", size)
} else {
sqlType = "CLOB"
}
case reflect.Struct:
if _, ok := dataValue.Interface().(time.Time); ok {
sqlType = "DATE"
}
default:
if IsByteArrayOrSlice(dataValue) {
sqlType = "BLOB"
}
}
}
if sqlType == "" {
panic(fmt.Sprintf("invalid sql type %s (%s) for ora", dataValue.Type().Name(), dataValue.Kind().String()))
}
if strings.TrimSpace(additionalType) == "" {
return sqlType
}
return fmt.Sprintf("%v %v", sqlType, additionalType)
}
func (d oracle) HasIndex(tableName string, indexName string) bool {
var count int
d.db.QueryRow("SELECT COUNT(1) FROM USER_INDEXES WHERE TABLE_NAME = :1 AND INDEX_NAME = :2", strings.ToUpper(tableName), strings.ToUpper(indexName)).Scan(&count)
return count > 0
}
func (d oracle) RemoveIndex(tableName string, indexName string) error {
_, err := d.db.Exec(fmt.Sprintf("DROP INDEX %v", indexName))
return err
}
func (d oracle) HasForeignKey(tableName string, foreignKeyName string) bool {
var count int
d.db.QueryRow("SELECT COUNT(1) FROM USER_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'R' AND TABLE_NAME = :1 AND CONSTRAINT_NAME = :2", strings.ToUpper(tableName), strings.ToUpper(foreignKeyName)).Scan(&count)
return count > 0
}
func (d oracle) HasTable(tableName string) bool {
var count int
d.db.QueryRow("SELECT COUNT(1) FROM USER_TABLES WHERE TABLE_NAME = :1", strings.ToUpper(tableName)).Scan(&count)
return count > 0
}
func (d oracle) HasColumn(tableName string, columnName string) bool {
var count int
d.db.QueryRow("SELECT COUNT(1) FROM USER_TAB_COLUMNS WHERE TABLE_NAME = :1 AND COLUMN_NAME = :2", strings.ToUpper(tableName), strings.ToUpper(columnName)).Scan(&count)
return count > 0
}
func (d oracle) CurrentDatabase() (name string) {
d.db.QueryRow("SELECT GLOBAL_NAME FROM GLOBAL_NAME").Scan(&name)
return
}
func (oracle) LimitWhereSQL(limit interface{}) (sql string) {
if limit != nil {
if parsedLimit, err := strconv.ParseInt(fmt.Sprint(limit), 0, 0); err == nil && parsedLimit >= 0 {
sql = fmt.Sprintf(" AND (ROWNUM <= %d)", parsedLimit)
}
}
return
}
func (oracle) LimitAndOffsetSQL(limit, offset interface{}) (sql string) {
return
}
func (oracle) SelectFromDummyTable() string {
return "FROM dual"
}
func (oracle) LastInsertIDReturningSuffix(tableName, key string) string {
return ""
}