-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdialect_oracle.go
93 lines (80 loc) · 2.01 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
package schema
import (
"database/sql"
)
// TODO(js) Are we querying the correct tables? See https://dba.stackexchange.com/questions/153436/i-want-to-see-all-tables-of-db-but-no-system-tables
const oracleAllColumns = `SELECT * FROM %s WHERE 1=0`
const oracleTableNamesWithSchema = `
SELECT
owner,
table_name
FROM
all_tables
WHERE
owner IN (SELECT sys_context('userenv', 'current_schema') from dual)
ORDER BY
owner,
table_name
`
const oracleViewNamesWithSchema = `
SELECT
owner,
view_name
FROM
all_views
WHERE
owner IN (SELECT sys_context('userenv', 'current_schema') from dual)
ORDER BY
owner,
view_name
`
const oraclePrimaryKey = `
SELECT
cc.column_name
FROM
all_constraints c,
all_cons_columns cc
WHERE
c.constraint_type = 'P' AND
c.constraint_name = cc.constraint_name AND
c.owner = cc.owner AND
cc.owner IN (SELECT sys_context('userenv', 'current_schema') from dual) AND
cc.table_name = :1
ORDER BY
cc.position
`
const oraclePrimaryKeyWithSchema = `
SELECT
cc.column_name
FROM
all_constraints c,
all_cons_columns cc
WHERE
c.constraint_type = 'P' AND
c.constraint_name = cc.constraint_name AND
c.owner = cc.owner AND
cc.owner = :1 AND
cc.table_name = :2
ORDER BY
cc.position
`
type oracleDialect struct{}
func (oracleDialect) escapeIdent(ident string) string {
// "tablename"
return escapeWithDoubleQuotes(ident)
}
func (d oracleDialect) ColumnTypes(db *sql.DB, schema, name string) ([]*sql.ColumnType, error) {
return fetchColumnTypes(db, oracleAllColumns, schema, name, d.escapeIdent)
}
func (oracleDialect) PrimaryKey(db *sql.DB, schema, name string) ([]string, error) {
if schema == "" {
return fetchNames(db, oraclePrimaryKey, "", name)
}
return fetchNames(db, oraclePrimaryKeyWithSchema, schema, name)
}
func (oracleDialect) TableNames(db *sql.DB) ([][2]string, error) {
return fetchObjectNames(db, oracleTableNamesWithSchema)
}
func (oracleDialect) ViewNames(db *sql.DB) ([][2]string, error) {
return fetchObjectNames(db, oracleViewNamesWithSchema)
}