-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOracleCLI.php
389 lines (296 loc) · 9.4 KB
/
OracleCLI.php
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
/*
OracleEditor.php Command Line Interface
Version 1.20
by Tim Strehle <[email protected]>
https://github.com/tistre/oracleeditor
Usage:
php OracleCLI.php \
--user=me \
--password=secret \
--service='oracle.example.com:1721/DB' \
--limit=10 \
--format=xml \
--query="select * from DUAL"
*/
// Don't write PHP warnings into HTML. Watch your PHP error_log file!
ini_set('display_errors', 0);
// Format version string
$version = trim(substr('$Revision: 1.20 $', 10, -1));
// Read command line options
$defaults = [
'format' => 'xml',
'limit' => 100,
'page' => 1
];
$options = getopt('', ['user:', 'password:', 'service:', 'format:', 'query:', 'limit:', 'page:']);
$requiredOptions = ['user', 'password', 'service', 'query'];
foreach ($requiredOptions as $option) {
if (!isset($options[$option])) {
fwrite(STDERR, sprintf("ERROR: --%s is required.\n", $option));
exit(1);
}
}
$options = array_merge($defaults, $options);
if (!is_numeric($options['limit'])) {
fwrite(STDERR, "ERROR: --limit must be a number.\n");
exit(1);
}
if (!is_numeric($options['page'])) {
fwrite(STDERR, "ERROR: --page must be a number.\n");
exit(1);
}
$exportformats = [
'xml' => ['XML', 'text/xml'],
'csv' => ['CSV', 'text/comma-separated-values'],
'html' => ['HTML table', 'text/html']
];
if (!isset($exportformats[$options['format']])) {
fwrite(STDERR, sprintf("ERROR: --format must be one of %s.\n", implode(', ', array_keys($exportformats))));
exit(1);
}
// Initialize database connection parameters
if ($options['user'] !== substr(preg_replace('/[^a-zA-Z0-9$_-]/', '',
$options['user']), 0, 30)) {
fwrite(STDERR, "ERROR: --user contains invalid characters.\n");
exit(1);
}
if ($options['service'] !== substr(preg_replace('|[^a-zA-Z0-9:.() =/_-]|', '',
$options['service']), 0, 2000)) {
fwrite(STDERR, "ERROR: --service contains invalid characters.\n");
exit(1);
}
$charset = 'UTF-8';
// Initialize connection
$conn = false;
pof_connect();
// Exporting may take a while
set_time_limit(0);
// Initialize export settings
$exportlimit = abs(intval($options['limit']));
// Loop through results
$cursor = pof_opencursor($options['query']);
if ($cursor) {
if (ocistatementtype($cursor) !== 'SELECT') {
pof_closecursor($cursor);
fwrite(STDERR, "ERROR: --query must be a SELECT statement.\n");
exit(1);
}
}
$columns = [];
$numcols = ocinumcols($cursor);
for ($j = 1; $j <= $numcols; $j++) {
if (ocicolumnname($cursor, $j) != 'ROWID_') {
$columns[(ocicolumnname($cursor, $j))] = array(
'type' => ocicolumntype($cursor, $j),
'size' => ocicolumnsize($cursor, $j)
);
}
}
// Skip previous sets
$rowOffset = 0;
if ($options['page'] > 1) {
$rowOffset = ($options['page'] - 1) * $options['limit'];
for ($j = 1; $j <= $rowOffset; $j++) {
if (!ocifetch($cursor)) {
break;
}
}
}
// Header
if ($options['format'] == 'xml') {
echo sprintf('<' . '?xml version="1.0" encoding="%s"?' . '>', $charset) . "\n";
echo "<!-- Generated by OracleEditor.php (https://github.com/tistre/oracleeditor) -->\n";
$userstr = $options['user'];
$userstr .= '@' . $options['service'];
echo sprintf('<rowset exported="%s" user="%s" server="%s">', date('Y-m-d\TH:i:s'), htmlspecialchars($userstr),
htmlspecialchars(php_uname('n'))) . "\n";
echo sprintf("\t<sql limit=\"%d\" page=\"%d\">%s</sql>\n", $options['limit'], $options['page'],
htmlspecialchars($options['query']));
// Column aliases: We can use column names as tag names only if
// they're valid XML names - <count(MYFIELD)> won't work.
$i = 0;
foreach ($columns as $name => $column) {
$i++;
if (preg_match('/^[a-zA-Z][a-zA-Z0-9_-]*$/', $name) == 0) {
$columns[$name]['alias'] = 'ALIAS' . $i;
}
}
echo "\t<columns>\n";
foreach ($columns as $name => $column) {
echo sprintf("\t\t" . '<column name="%s" type="%s" size="%s"%s/>' . "\n",
htmlspecialchars($name),
$column['type'],
$column['size'],
(isset($column['alias']) ? ' alias="' . $column['alias'] . '"' : '')
);
}
echo "\t</columns>\n";
} elseif ($options['format'] == 'csv') {
$first = true;
foreach ($columns as $name => $column) {
if ($name != 'ROWID_') {
if (!$first) {
echo ', ';
}
echo sprintf('"%s"', str_replace('"', '""', $name));
$first = false;
}
}
echo "\n";
} elseif ($options['format'] == 'html') { ?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=<?php echo $charset; ?>">
<meta name="date" content="<?php echo date('Y-m-d\TH:i:s'); ?>">
<meta name="generator" content="OracleEditor.php (https://github.com/tistre/oracleeditor)">
<title>Exported Oracle data (by OracleEditor.php)</title>
</head>
<body>
<h1>Exported Oracle data</h1>
<?php
$userstr = $options['user'];
$userstr .= '@' . $options['service'];
?>
<p>The Oracle user <em><?php echo htmlspecialchars($userstr); ?></em> exported this data on
<em><?php echo date('r'); ?></em>
by running the following SQL statement in OracleCLI.php:<br/>
<pre><?php echo htmlspecialchars($options['query']); ?></pre></p>
<table border="1">
<tr>
<?php
foreach ($columns as $name => $column) {
echo sprintf('<th>%s<br />(%s, %s)</th>' . "\n",
htmlspecialchars($name),
$column['type'],
$column['size']
);
}
?>
</tr>
<?php
}
// Rows
$i = 1;
while (true) {
if (!ocifetchinto($cursor, $row, OCI_ASSOC | OCI_RETURN_LOBS)) {
break;
}
if ($options['format'] == 'xml') {
echo sprintf("\t<row num=\"%d\"%s>\n",
($i + $rowOffset),
(isset($row['ROWID_']) ? (' id="' . htmlspecialchars($row['ROWID_']) . '"') : ''));
foreach ($row as $fieldname => $value) {
if ($fieldname != 'ROWID_') {
echo sprintf("\t\t<%1\$s>%2\$s</%1\$s>\n",
(isset($columns[$fieldname]['alias']) ? $columns[$fieldname]['alias'] : $fieldname),
htmlspecialchars($value));
}
}
echo "\t</row>\n";
} elseif ($options['format'] == 'csv') {
$first = true;
foreach ($columns as $fieldname => $column) {
if ($fieldname != 'ROWID_') {
if (!$first) {
echo ', ';
}
if (isset($row[$fieldname])) {
echo sprintf('"%s"', str_replace('"', '""', $row[$fieldname]));
} else {
echo '""';
}
$first = false;
}
}
echo "\n";
} elseif ($options['format'] == 'html') {
echo "<tr>\n";
foreach ($columns as $fieldname => $column) {
if ($fieldname != 'ROWID_') {
echo "\t<td>";
if (isset($row[$fieldname])) {
echo htmlspecialchars($row[$fieldname]);
}
echo "</td>\n";
}
}
echo "</tr>\n";
}
if (($exportlimit > 0) && ($exportlimit < ++$i)) {
break;
}
}
// Footer
if ($options['format'] == 'xml') {
echo "</rowset>\n";
} elseif ($options['format'] == 'html') { ?>
</table>
<p>HTML generated by <a
href="https://github.com/tistre/oracleeditor">OracleEditor.php</a> <?php echo $version; ?>
© 2017 by <a href="https://www.strehle.de/tim/">Tim Strehle</a> <<a
href="mailto:[email protected]">[email protected]</a>></p>
</body>
</html>
<?php
}
pof_closecursor($cursor);
function pof_connect()
{
global $conn, $options;
$conn = oci_connect
(
$options['user'],
$options['password'],
$options['service'],
'AL32UTF8'
);
$err = ocierror();
if (is_array($err)) {
fwrite(STDERR, sprintf("ERROR: Logon failed: %s\n", $err['message']));
exit(1);
}
}
function pof_disconnect()
{
global $conn;
if ($conn) {
ocilogoff($conn);
}
}
function pof_opencursor($sql, $bind = false)
{
global $conn, $options;
$cursor = ociparse($conn, $sql);
if (!$cursor) {
$err = ocierror($conn);
if (is_array($err)) {
fwrite(STDERR, sprintf("ERROR: Parse failed: %s\n", $err['message']));
exit(1);
}
} else { // This might improve performance?
ocisetprefetch($cursor, $options['limit']);
if (is_array($bind)) {
foreach ($bind as $fieldname => $value) {
ocibindbyname($cursor, ':' . $fieldname, $bind[$fieldname], -1);
}
}
$ok = ociexecute($cursor);
if (!$ok) {
$err = ocierror($cursor);
if (is_array($err)) {
fwrite(STDERR, sprintf("ERROR: Execute failed: %s\n", $err['message']));
exit(1);
}
pof_closecursor($cursor);
$cursor = false;
}
}
return $cursor;
}
function pof_closecursor($cursor)
{
if ($cursor) {
ocifreestatement($cursor);
}
}