/
oracle.go
193 lines (164 loc) · 4.13 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
183
184
185
186
187
188
189
190
191
192
193
// +build oracle
package loaders
import (
"fmt"
"os"
"regexp"
"strings"
_ "github.com/godror/godror"
"github.com/xo/xo/internal"
"github.com/xo/xo/models"
)
func init() {
internal.SchemaLoaders["godror"] = internal.TypeLoader{
ParamN: func(i int) string { return fmt.Sprintf(":%d", i+1) },
MaskFunc: func() string { return ":%d" },
ProcessRelkind: OrRelkind,
Schema: OrSchema,
ParseType: OrParseType,
// EnumList: models.OrEnums,
// EnumValueList: OrEnumValues,
// ProcList: models.OrProcs,
// ProcParamList: models.OrProcParams,
TableList: models.OrTables,
ColumnList: models.OrTableColumns,
ForeignKeyList: models.OrTableForeignKeys,
IndexList: models.OrTableIndexes,
IndexColumnList: models.OrIndexColumns,
QueryColumnList: OrQueryColumns,
}
}
// OrRelkind returns the oracle string representation for RelType.
func OrRelkind(relType internal.RelType) string {
var s string
switch relType {
case internal.Table:
s = "TABLE"
case internal.View:
s = "VIEW"
default:
panic("unsupported RelType")
}
return s
}
// OrSchema retrieves the name of the current schema.
func OrSchema(args *internal.ArgType) (string, error) {
var err error
// sql query
const sqlstr = `SELECT LOWER(SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')) FROM dual`
var schema string
// run query
models.XOLog(sqlstr)
err = args.DB.QueryRow(sqlstr).Scan(&schema)
if err != nil {
return "", err
}
return schema, nil
}
// OrLenRE is a regexp that matches lengths.
var OrLenRE = regexp.MustCompile(`\([0-9]+\)`)
// OrParseType parse a oracle type into a Go type based on the column
// definition.
func OrParseType(args *internal.ArgType, dt string, nullable bool) (int, string, string) {
nilVal := "nil"
dt = strings.ToLower(dt)
// extract precision
dt, precision, scale := args.ParsePrecision(dt)
var typ string
// strip remaining length (on things like timestamp)
switch OrLenRE.ReplaceAllString(dt, "") {
case "char", "nchar", "varchar", "varchar2", "nvarchar2",
"long",
"clob", "nclob",
"rowid":
nilVal = `""`
typ = "string"
case "shortint":
nilVal = "0"
typ = "int16"
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "integer":
nilVal = "0"
typ = args.Int32Type
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "longinteger":
nilVal = "0"
typ = "int64"
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
case "float", "shortdecimal":
nilVal = "0.0"
typ = "float32"
if nullable {
nilVal = "sql.NullFloat64{}"
typ = "sql.NullFloat64"
}
case "number", "decimal":
nilVal = "0.0"
if 0 < precision && precision < 18 && scale > 0 {
typ = "float64"
if nullable {
nilVal = "sql.NullFloat64{}"
typ = "sql.NullFloat64"
}
} else if 0 < precision && precision <= 19 && scale == 0 {
typ = "int64"
if nullable {
nilVal = "sql.NullInt64{}"
typ = "sql.NullInt64"
}
} else {
nilVal = `""`
typ = "string"
}
case "blob", "long raw", "raw":
typ = "[]byte"
case "date", "timestamp", "timestamp with time zone":
typ = "time.Time"
nilVal = "time.Time{}"
default:
// bail
fmt.Fprintf(os.Stderr, "error: unknown type %q\n", dt)
os.Exit(1)
}
// special case for bool
if typ == "int" && precision == 1 {
nilVal = "false"
typ = "bool"
if nullable {
nilVal = "sql.NullBool{}"
typ = "sql.NullBool"
}
}
return precision, nilVal, typ
}
// OrQueryColumns parses the query and generates a type for it.
func OrQueryColumns(args *internal.ArgType, inspect []string) ([]*models.Column, error) {
var err error
// create temporary view xoid
xoid := "XO$" + internal.GenRandomID()
viewq := `CREATE GLOBAL TEMPORARY TABLE ` + xoid + ` ` +
`ON COMMIT PRESERVE ROWS ` +
`AS ` + strings.Join(inspect, "\n")
models.XOLog(viewq)
_, err = args.DB.Exec(viewq)
if err != nil {
return nil, err
}
// load columns
cols, err := models.OrTableColumns(args.DB, args.Schema, xoid)
// drop inspect view
dropq := `DROP TABLE ` + xoid
models.XOLog(dropq)
_, _ = args.DB.Exec(dropq)
// load column information
return cols, err
}