-
Notifications
You must be signed in to change notification settings - Fork 3
/
datadestination_oracle.go
175 lines (159 loc) · 5.41 KB
/
datadestination_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
package push
import (
"fmt"
"strings"
"time"
"github.com/cgi-fr/lino/pkg/push"
// import Oracle connector
_ "github.com/godror/godror"
)
// OracleDataDestinationFactory exposes methods to create new Oracle extractors.
type OracleDataDestinationFactory struct{}
// NewOracleDataDestinationFactory creates a new Oracle datadestination factory.
func NewOracleDataDestinationFactory() *OracleDataDestinationFactory {
return &OracleDataDestinationFactory{}
}
// New return a Oracle pusher
func (e *OracleDataDestinationFactory) New(url string, schema string) push.DataDestination {
return NewSQLDataDestination(url, schema, OracleDialect{})
}
// OracleDialect inject oracle variations
type OracleDialect struct{}
// Placeholde return the variable format for postgres
func (d OracleDialect) Placeholder(position int) string {
return fmt.Sprintf(":v%d", position)
}
// EnableConstraintsStatement generate statments to activate constraintes
func (d OracleDialect) EnableConstraintsStatement(tableName string) string {
tableComposants := strings.Split(tableName, ".")
switch len(tableComposants) {
case 1:
return fmt.Sprintf(`BEGIN
FOR c IN
(SELECT c.owner, c.table_name, c.constraint_name
FROM user_constraints c, user_tables t
WHERE c.table_name = t.table_name
AND c.owner = sys_context( 'userenv', 'current_schema' )
AND c.table_name = '%s'
AND c.status = 'DISABLED'
ORDER BY c.constraint_type)
LOOP
dbms_utility.exec_ddl_statement('alter table "' || c.owner || '"."' || c.table_name || '" enable constraint ' || c.constraint_name);
END LOOP;
END;
`, tableName)
case 2:
return fmt.Sprintf(`BEGIN
FOR c IN
(SELECT c.owner, c.table_name, c.constraint_name
FROM user_constraints c, user_tables t
WHERE c.table_name = t.table_name
AND c.owner = '%s'
AND c.table_name = '%s'
AND c.status = 'DISABLED'
ORDER BY c.constraint_type)
LOOP
dbms_utility.exec_ddl_statement('alter table "' || c.owner || '"."' || c.table_name || '" enable constraint ' || c.constraint_name);
END LOOP;
END;
`, tableComposants[0], tableComposants[0])
default:
return ""
}
}
// DisableConstraintsStatement generate statments to deactivate constraintes
func (d OracleDialect) DisableConstraintsStatement(tableName string) string {
tableComposants := strings.Split(tableName, ".")
switch len(tableComposants) {
case 1:
return fmt.Sprintf(`BEGIN
FOR c IN
(SELECT c.owner, c.table_name, c.constraint_name
FROM user_constraints c, user_tables t
WHERE c.table_name = t.table_name
AND c.owner = sys_context( 'userenv', 'current_schema' )
AND c.table_name = '%s'
AND c.status = 'ENABLED'
AND NOT (t.iot_type IS NOT NULL AND c.constraint_type = 'P')
ORDER BY c.constraint_type DESC)
LOOP
dbms_utility.exec_ddl_statement('alter table "' || c.owner || '"."' || c.table_name || '" disable constraint ' || c.constraint_name);
END LOOP;
END;
`, tableName)
case 2:
return fmt.Sprintf(`BEGIN
FOR c IN
(SELECT c.owner, c.table_name, c.constraint_name
FROM user_constraints c, user_tables t
WHERE c.table_name = t.table_name
AND c.owner = '%s'
AND c.table_name = '%s'
AND c.status = 'ENABLED'
AND NOT (t.iot_type IS NOT NULL AND c.constraint_type = 'P')
ORDER BY c.constraint_type DESC)
LOOP
dbms_utility.exec_ddl_statement('alter table "' || c.owner || '"."' || c.table_name || '" disable constraint ' || c.constraint_name);
END LOOP;
END;
`, tableComposants[0], tableComposants[1])
default:
return ""
}
}
// TruncateStatement generate statement to truncat table content
func (d OracleDialect) TruncateStatement(tableName string) string {
return fmt.Sprintf("TRUNCATE TABLE %s", tableName)
}
// InsertStatement generate insert statement
func (d OracleDialect) InsertStatement(tableName string, columns []string, values []string, primaryKeys []string) string {
protectedColumns := []string{}
for _, c := range columns {
protectedColumns = append(protectedColumns, fmt.Sprintf("\"%s\"", c))
}
return fmt.Sprintf("INSERT INTO %s(%s) VALUES(%s)", tableName, strings.Join(protectedColumns, ","), strings.Join(values, ","))
}
// UpdateStatement
func (d OracleDialect) UpdateStatement(tableName string, columns []string, uValues []string, primaryKeys []string, pValues []string) (string, []string, *push.Error) {
sql := &strings.Builder{}
sql.Write([]byte("UPDATE "))
sql.Write([]byte(tableName))
sql.Write([]byte(" SET "))
for index, column := range columns {
sql.Write([]byte(column))
fmt.Fprint(sql, "=")
fmt.Fprint(sql, uValues[index])
if index+1 < len(columns) {
sql.Write([]byte(", "))
}
}
if len(primaryKeys) > 0 {
sql.Write([]byte(" WHERE "))
} else {
return "", []string{}, &push.Error{Description: fmt.Sprintf("can't update table [%s] because no primary key is defined", tableName)}
}
for index, pk := range primaryKeys {
sql.Write([]byte(pk))
fmt.Fprint(sql, "=")
fmt.Fprint(sql, pValues[index])
if index+1 < len(primaryKeys) {
sql.Write([]byte(" AND "))
}
}
return sql.String(), append(columns, primaryKeys...), nil
}
// IsDuplicateError check if error is a duplicate error
func (d OracleDialect) IsDuplicateError(err error) bool {
// ORA-00001
return strings.Contains(err.Error(), "ORA-00001")
}
// ConvertValue before load
func (d OracleDialect) ConvertValue(from push.Value) push.Value {
// FIXME: Workaround to parse time from json
aTime, err := time.Parse("2006-01-02T15:04:05.999Z07:00", fmt.Sprintf("%v", from))
if err != nil {
return from
} else {
return aTime
}
}