-
Notifications
You must be signed in to change notification settings - Fork 1
/
oJobSQL.yaml
206 lines (186 loc) · 7.54 KB
/
oJobSQL.yaml
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
194
195
196
197
198
199
200
201
202
203
204
205
# Copyright 2023 Nuno Aguiar
jobs:
# ----------------
- name: SQL Result
help:
Use internally by oJobSQL
exec: |
if (isUnDef(args.sql)) throw "Please specify a query to run with sql=select\ user\ from\ dual";
args.format = _$(args.format, "format").default("pm");
var _res, _met = { }
if (isDef(args.metric)) {
_met = _$(ow.oJob.getMetric(args.metric)).isMap().default({})
}
if (args.execute) {
args._db.rollback();
try {
var t = now()
_res = args._db.u(args.sql);
args._db.commit();
_met.updateAccTimeMs = _$(_met.updateAccTimeMs).isNumber().default(0) + 1
_met.updateRows = _$(_met.updateRows).isNumber().default(0) + Number(_res)
_met.updateSQL = _$(_met.updateSQL).isNumber().default(0) + 1
_met.successful = _$(_met.successful).isNumber().default(0) + 1
} catch(e) {
args._db.rollback();
_met.error = _$(_met.error).isNumber().default(0) + 1
_met.exception = String(e)
throw e;
} finally {
if (isDef(args.metric)) {
_met.id = args.metric
_met.type = "sql"
ow.oJob.setMetric(args.metric, _met)
}
}
} else {
args._db.rollback();
try {
var t = now()
_res = args._db.q(args.sql).results;
args._db.commit();
_met.queryAccTimeMs = _$(_met.queryAccTimeMs).isNumber().default(0) + 1
_met.queryRows = _$(_met.queryRows).isNumber().default(0) + (isArray(_res) ? _res.length : 0)
_met.querySQL = _$(_met.querySQL).isNumber().default(0) + 1
_met.successful = _$(_met.successful).isNumber().default(0) + 1
} catch(e) {
args._db.rollback();
_met.error = _$(_met.error).isNumber().default(0) + 1
_met.exception = String(e)
throw e;
} finally {
if (isDef(args.metric)) {
_met.id = args.metric
_met.type = "sql"
ow.oJob.setMetric(args.metric, _met)
}
}
}
args.output = _res;
if (isUnDef(args.__format) && isDef(args.format)) args.__format = args.format;
ow.oJob.output(args.output, args);
# ---------
- name: SQL
to : SQL Result
help:
text: Executes a DB SQL query.
expects:
- name: DBDriver
desc: (String) The JDBC driver to use (defaults to Oracle)
- name: DBURL
desc: (String) A JDBC URL to access the database
- name: DBUser
desc: (String) The database user login
- name: DBPass
desc: (String) The database user password
- name: sql
desc: (String) The SQL to execute/query
- name: execute
desc: (Boolean) If true if will execute the sql statement instead of querying (e.g. update, truncate, ...)
- name: format
desc: "(String) The format on which to output data (default: json)"
returns:
- name: output
desc: (Object) In the default format it will be the resulting array. In tabular format it will return a string.
exec: |
args.DBDriver = _$(args.DBDriver, "DBDriver").isString().default(void 0);
_$(args.DBURL, "DBURL").isString().$_();
_$(args.DBUser, "DBUser").isString().$_();
_$(args.DBPass, "DBPass").isString().$_();
args.metric = _$(args.metric).isString().default(__);
if (isUnDef(args.sql)) throw "Please specify a query to run with sql=select\ user\ from\ dual";
if (isUnDef(args.format)) args.format = "json";
var t = now(), _met = { }
if (isDef(args.metric)) {
_met = _$(ow.oJob.getMetric(args.metric)).isMap().default({})
}
try {
if (isDef(args.DBDriver))
args._db = new DB(args.DBDriver, args.DBURL, args.DBUser, args.DBPass);
else
args._db = new DB(args.DBURL, args.DBUser, args.DBPass);
_met.connTime = now() - t
_met.successful = _$(_met.successful).isNumber().default(0) + 1
} catch(e) {
_met.error = _$(_met.error).isNumber().default(0) + 1
_met.exception = String(e)
} finally {
if (isDef(args.metric)) {
_met.id = args.metric
_met.type = "sql"
ow.oJob.setMetric(args.metric, _met)
}
}
if (isDef(_met.exception)) throw _met.exception
# ---------------
- name: SQL Batch
help:
text : Executes a series of DB SQLs.
expects:
- name: DBDriver
desc: (String) The JDBC driver to use (defaults to Oracle)
- name: DBURL
desc: (String) A JDBC URL to access the database
- name: DBUser
desc: (String) The database user login
- name: DBPass
desc: (String) The database user password
- name: SQLs
desc: (Array) The SQLs to execute/query
- name: continueOnError
desc: (Boolean) If continueOnError = true it won't stop on exceptions
returns:
- name: output
desc: (Array) An array of maps with each SQL statement and the corresponding number of affected lines
exec: |
args.DBDriver = _$(args.DBDriver, "DBDriver").isString().default(void 0);
_$(args.DBURL, "DBURL").isString().$_();
_$(args.DBUser, "DBUser").isString().$_();
_$(args.DBPass, "DBPass").isString().$_();
args.continueOnError = _$(args.continueOnError, "continueOnError").isBoolean().default(false);
if (isUnDef(args.sqls)) throw "Please provide a sqls string or array of strings.";
if (isString(args.sqls)) args.sqls = [ args.sqls ];
if (isDef(args.DBDriver))
args._db = new DB(args.DBDriver, args.DBURL, args.DBUser, args.DBPass);
else
args._db = new DB(args.DBURL, args.DBUser, args.DBPass);
args._db.getConnect().setAutoCommit(true);
var res = args.sqls.map(sql => {
var res = { sql: sql, result: void 0 };
try {
res.result = args._db.u(sql);
} catch(e) {
res.error = String(e);
if (!args.continueOnError) throw(String(e));
}
return res;
});
args.output = res;
# --------------
- name: SQL RAID
to : SQL Result
help:
text: Executes a RAID SQL query.
expects:
- name: raidURL
desc: (String) A RAID AF connection URL
- name: raidDB
desc: (String) A RAID connection name (e.g. Adm, App, Dat)
- name: raidDBURL
desc: (String) A JDBC URL for customization of the access to a raid database
- name: raidDBPass
desc: (String) Using a specific RAID database password if one can't be determined
- name: raidUseCIR
desc: (Boolean) Determines if the use of CIR should be forced for database parameters detection
- name: sql
desc: (String) The SQL to execute/query
- name: execute
desc: (Boolean) If true if will execute the sql statement instead of querying (e.g. update, truncate, ...)
returns:
- name: output
desc: (Object) In the default format it will be the resulting array. In tabular format it will return a string.
exec: |
if (isUnDef(args.raidURL)) throw "Please specify a raidURL=http://user:[email protected]:8080";
if (isUnDef(args.raidDB)) throw "Please specify a raidDB=Adm";
var _s = new AF(args.raidURL);
args._db = getRAIDDB(_s, args.raidDB, args.raidDBURL, args.raidDBPass, args.raidUseCIR);