Skip to content

Commit a76ae79

Browse files
committed
Add skipping to next result pages. Write error messages to STDERR.
1 parent efdf827 commit a76ae79

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

OracleCLI.php

+47-17
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,31 @@
2929

3030
$defaults = [
3131
'format' => 'xml',
32-
'limit' => 100
32+
'limit' => 100,
33+
'page' => 1
3334
];
3435

35-
$options = getopt('', ['user:', 'password:', 'service:', 'format:', 'query:', 'limit:']);
36+
$options = getopt('', ['user:', 'password:', 'service:', 'format:', 'query:', 'limit:', 'page:']);
3637

3738
$requiredOptions = ['user', 'password', 'service', 'query'];
3839

3940
foreach ($requiredOptions as $option) {
4041
if (!isset($options[$option])) {
41-
die(sprintf("ERROR: --%s is required.\n", $option));
42+
fwrite(STDERR, sprintf("ERROR: --%s is required.\n", $option));
43+
exit(1);
4244
}
4345
}
4446

4547
$options = array_merge($defaults, $options);
4648

4749
if (!is_numeric($options['limit'])) {
48-
die("ERROR: --limit must be a number.\n");
50+
fwrite(STDERR, "ERROR: --limit must be a number.\n");
51+
exit(1);
52+
}
53+
54+
if (!is_numeric($options['page'])) {
55+
fwrite(STDERR, "ERROR: --page must be a number.\n");
56+
exit(1);
4957
}
5058

5159
$exportformats = [
@@ -55,19 +63,22 @@
5563
];
5664

5765
if (!isset($exportformats[$options['format']])) {
58-
die(sprintf("ERROR: --format must be one of %s.\n", implode(', ', array_keys($exportformats))));
66+
fwrite(STDERR, sprintf("ERROR: --format must be one of %s.\n", implode(', ', array_keys($exportformats))));
67+
exit(1);
5968
}
6069

6170
// Initialize database connection parameters
6271

6372
if ($options['user'] !== substr(preg_replace('/[^a-zA-Z0-9$_-]/', '',
64-
$options['user']), 0, 30)) {
65-
die("ERROR: --user contains invalid characters.\n");
73+
$options['user']), 0, 30)) {
74+
fwrite(STDERR, "ERROR: --user contains invalid characters.\n");
75+
exit(1);
6676
}
6777

6878
if ($options['service'] !== substr(preg_replace('|[^a-zA-Z0-9:.() =/_-]|', '',
6979
$options['service']), 0, 2000)) {
70-
die("ERROR: --service contains invalid characters.\n");
80+
fwrite(STDERR, "ERROR: --service contains invalid characters.\n");
81+
exit(1);
7182
}
7283

7384
$charset = 'UTF-8';
@@ -93,7 +104,8 @@
93104
if ($cursor) {
94105
if (ocistatementtype($cursor) !== 'SELECT') {
95106
pof_closecursor($cursor);
96-
die("ERROR: --query must be a SELECT statement.\n");
107+
fwrite(STDERR, "ERROR: --query must be a SELECT statement.\n");
108+
exit(1);
97109
}
98110
}
99111

@@ -109,6 +121,19 @@
109121
}
110122
}
111123

124+
// Skip previous sets
125+
126+
$rowOffset = 0;
127+
128+
if ($options['page'] > 1) {
129+
$rowOffset = ($options['page'] - 1) * $options['limit'];
130+
for ($j = 1; $j <= $rowOffset; $j++) {
131+
if (!ocifetch($cursor)) {
132+
break;
133+
}
134+
}
135+
}
136+
112137
// Header
113138

114139
if ($options['format'] == 'xml') {
@@ -118,9 +143,10 @@
118143
$userstr = $options['user'];
119144
$userstr .= '@' . $options['service'];
120145

121-
echo sprintf('<rowset exported="%s" user="%s" server="%s">', date('Y-m-d\TH:i:s'), $userstr,
122-
$_SERVER['SERVER_NAME']) . "\n";
123-
echo sprintf("\t<sql>%s</sql>\n", htmlspecialchars($options['query']));
146+
echo sprintf('<rowset exported="%s" user="%s" server="%s">', date('Y-m-d\TH:i:s'), htmlspecialchars($userstr),
147+
htmlspecialchars(php_uname('n'))) . "\n";
148+
echo sprintf("\t<sql limit=\"%d\" page=\"%d\">%s</sql>\n", $options['limit'], $options['page'],
149+
htmlspecialchars($options['query']));
124150

125151
// Column aliases: We can use column names as tag names only if
126152
// they're valid XML names - <count(MYFIELD)> won't work.
@@ -211,7 +237,8 @@
211237
}
212238

213239
if ($options['format'] == 'xml') {
214-
echo sprintf("\t<row%s>\n",
240+
echo sprintf("\t<row num=\"%d\"%s>\n",
241+
($i + $rowOffset),
215242
(isset($row['ROWID_']) ? (' id="' . htmlspecialchars($row['ROWID_']) . '"') : ''));
216243

217244
foreach ($row as $fieldname => $value) {
@@ -257,7 +284,7 @@
257284
echo "</tr>\n";
258285
}
259286

260-
if (($exportlimit > 0) && ($exportlimit <= ++$i)) {
287+
if (($exportlimit > 0) && ($exportlimit < ++$i)) {
261288
break;
262289
}
263290
}
@@ -297,7 +324,8 @@ function pof_connect()
297324
$err = ocierror();
298325

299326
if (is_array($err)) {
300-
die(sprintf("ERROR: Logon failed: %s\n", $err['message']));
327+
fwrite(STDERR, sprintf("ERROR: Logon failed: %s\n", $err['message']));
328+
exit(1);
301329
}
302330
}
303331

@@ -321,7 +349,8 @@ function pof_opencursor($sql, $bind = false)
321349
if (!$cursor) {
322350
$err = ocierror($conn);
323351
if (is_array($err)) {
324-
die(sprintf("ERROR: Parse failed: %s\n", $err['message']));
352+
fwrite(STDERR, sprintf("ERROR: Parse failed: %s\n", $err['message']));
353+
exit(1);
325354
}
326355
} else { // This might improve performance?
327356
ocisetprefetch($cursor, $options['limit']);
@@ -338,7 +367,8 @@ function pof_opencursor($sql, $bind = false)
338367
$err = ocierror($cursor);
339368

340369
if (is_array($err)) {
341-
die(sprintf("ERROR: Execute failed: %s\n", $err['message']));
370+
fwrite(STDERR, sprintf("ERROR: Execute failed: %s\n", $err['message']));
371+
exit(1);
342372
}
343373

344374
pof_closecursor($cursor);

0 commit comments

Comments
 (0)