-
Notifications
You must be signed in to change notification settings - Fork 101
/
oracle.go
182 lines (168 loc) · 4.51 KB
/
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
package qbs
import (
"database/sql"
"fmt"
"strings"
"time"
)
type oracle struct {
base
}
func NewOracle() Dialect {
d := &oracle{}
d.base.dialect = d
return d
}
func (d oracle) quote(s string) string {
sep := "."
a := []string{}
c := strings.Split(s, sep)
for _, v := range c {
a = append(a, fmt.Sprintf(`"%s"`, v))
}
return strings.Join(a, sep)
}
func (d oracle) sqlType(field modelField) string {
f := field.value
switch f.(type) {
case time.Time:
return "DATE"
/*
case bool:
return "boolean"
*/
case int, int8, int16, int32, uint, uint8, uint16, uint32, int64, uint64:
if field.size > 0 {
return fmt.Sprintf("NUMBER(%d)", field.size)
}
return "NUMBER"
case float32, float64:
if field.size > 0 {
return fmt.Sprintf("NUMBER(%d,%d)", field.size/10, field.size%10)
}
return "NUMBER(16,2)"
case []byte, string:
if field.size > 0 && field.size < 4000 {
return fmt.Sprintf("VARCHAR2(%d)", field.size)
}
return "CLOB"
default:
if len(field.colType) != 0 {
switch field.colType {
case QBS_COLTYPE_BOOL:
panic("Qbs doesn't support column type " + field.colType + "for Oracle")
case QBS_COLTYPE_INT, QBS_COLTYPE_BIGINT:
return "NUMBER"
case QBS_COLTYPE_DOUBLE:
if field.size > 0 {
return fmt.Sprintf("NUMBER(%d,%d)", field.size/10, field.size%10)
}
return "NUMBER(16,2)"
case QBS_COLTYPE_TIME:
return "DATE"
case QBS_COLTYPE_TEXT:
if field.size > 0 && field.size < 4000 {
return fmt.Sprintf("VARCHAR2(%d)", field.size)
}
return "CLOB"
default:
panic("Qbs doesn't support column type " + field.colType + "for Oracle")
}
}
}
panic("invalid sql type for field:" + field.name)
}
func (d oracle) insert(q *Qbs) (int64, error) {
sql, args := d.dialect.insertSql(q.criteria)
row := q.QueryRow(sql, args...)
value := q.criteria.model.pk.value
var err error
var id int64
if _, ok := value.(int64); ok {
err = row.Scan(&id)
} else if _, ok := value.(string); ok {
var str string
err = row.Scan(&str)
}
return id, err
}
func (d oracle) insertSql(criteria *criteria) (string, []interface{}) {
sql, values := d.base.insertSql(criteria)
sql += " RETURNING " + d.dialect.quote(criteria.model.pk.name)
return sql, values
}
func (d oracle) indexExists(mg *Migration, tableName, indexName string) bool {
var row *sql.Row
var name string
query := "SELECT INDEX_NAME FROM USER_INDEXES "
query += "WHERE TABLE_NAME = ? AND INDEX_NAME = ?"
query = d.substituteMarkers(query)
row = mg.db.QueryRow(query, tableName, indexName)
row.Scan(&name)
return name != ""
}
func (d oracle) substituteMarkers(query string) string {
position := 1
chunks := make([]string, 0, len(query)*2)
for _, v := range query {
if v == '?' {
chunks = append(chunks, fmt.Sprintf("$%d", position))
position++
} else {
chunks = append(chunks, string(v))
}
}
return strings.Join(chunks, "")
}
func (d oracle) columnsInTable(mg *Migration, table interface{}) map[string]bool {
tn := tableName(table)
columns := make(map[string]bool)
query := "SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME = ?"
query = mg.dialect.substituteMarkers(query)
rows, err := mg.db.Query(query, tn)
defer rows.Close()
if err != nil {
panic(err)
}
for rows.Next() {
column := ""
err := rows.Scan(&column)
if err == nil {
columns[column] = true
}
}
return columns
}
func (d oracle) primaryKeySql(isString bool, size int) string {
if isString {
return fmt.Sprintf("VARCHAR2(%d) PRIMARY KEY NOT NULL", size)
}
if size == 0 {
size = 16
}
return fmt.Sprintf("NUMBER(%d) PRIMARY KEY NOT NULL", size)
}
func (d oracle) createTableSql(model *model, ifNotExists bool) string {
baseSql := d.base.createTableSql(model, false)
if _, isString := model.pk.value.(string); isString {
return baseSql
}
table_pk := model.table + "_" + model.pk.name
sequence := " CREATE SEQUENCE " + table_pk + "_seq" +
" MINVALUE 1 NOMAXVALUE START WITH 1 INCREMENT BY 1 NOCACHE CYCLE"
trigger := " CREATE TRIGGER " + table_pk + "_triger BEFORE INSERT ON " + table_pk +
" FOR EACH ROW WHEN (new.id is null)" +
" begin" +
" select " + table_pk + "_seq.nextval into: new.id from dual " +
" end "
return baseSql + ";" + sequence + ";" + trigger
}
func (d oracle) catchMigrationError(err error) bool {
errString := err.Error()
return strings.Contains(errString, "ORA-00955") || strings.Contains(errString, "ORA-00942")
}
func (d oracle) dropTableSql(table string) string {
a := []string{"DROP TABLE"}
a = append(a, d.dialect.quote(table))
return strings.Join(a, " ")
}