-
Notifications
You must be signed in to change notification settings - Fork 317
/
oracle.go
78 lines (73 loc) · 2.37 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
package loader
import (
"regexp"
"github.com/xo/xo/models"
xo "github.com/xo/xo/types"
)
func init() {
Register("oracle", Loader{
Mask: ":%d",
Schema: models.OracleSchema,
Procs: models.OracleProcs,
ProcParams: models.OracleProcParams,
Tables: models.OracleTables,
TableColumns: models.OracleTableColumns,
TableSequences: models.OracleTableSequences,
TableForeignKeys: models.OracleTableForeignKeys,
TableIndexes: models.OracleTableIndexes,
IndexColumns: models.OracleIndexColumns,
ViewCreate: models.OracleViewCreate,
ViewTruncate: models.OracleViewTruncate,
ViewDrop: models.OracleViewDrop,
})
}
// OracleGoType parse a oracle type into a Go type based on the column
// definition.
func OracleGoType(d xo.Type, schema, itype, utype string) (string, string, error) {
var goType, zero string
// strip remaining length (on things like timestamp)
switch orLenRE.ReplaceAllString(d.Type, "") {
case "char", "nchar", "varchar", "varchar2", "nvarchar2", "clob", "nclob", "rowid":
goType, zero = "string", `""`
if d.Nullable {
goType, zero = "sql.NullString", "sql.NullString{}"
}
case "number":
switch {
case d.Prec == 0 && d.Scale == 0 && !d.Nullable:
goType, zero = "int", "0"
case d.Scale != 0 && !d.Nullable:
goType, zero = "float64", "0.0"
case d.Scale != 0 && d.Nullable:
goType, zero = "sql.NullFloat64", "sql.NullFloat64{}"
case !d.Nullable:
goType, zero = "int64", "0"
default:
goType, zero = "sql.NullInt64", "sql.NullInt64{}"
}
case "float":
goType, zero = "float64", "0.0"
if d.Nullable {
goType, zero = "sql.NullFloat64", "sql.NullFloat64{}"
}
case "date", "timestamp", "timestamp with time zone", "timestamp with local time zone":
goType, zero = "time.Time", "time.Time{}"
if d.Nullable {
goType, zero = "sql.NullTime", "sql.NullTime{}"
}
case "blob", "long raw", "raw", "xmltype":
goType, zero = "[]byte", "nil"
default:
goType, zero = schemaType(d.Type, d.Nullable, schema)
}
// handle bools
switch {
case goType == "int64" && d.Prec == 1 && !d.Nullable:
goType, zero = "bool", "false"
case goType == "sql.NullInt64" && d.Prec == 1 && d.Nullable:
goType, zero = "sql.NullBool", "sql.NullBool{}"
}
return goType, zero, nil
}
// orLenRE is a regexp that matches lengths.
var orLenRE = regexp.MustCompile(`\([0-9]+\)`)