29
29
30
30
$ defaults = [
31
31
'format ' => 'xml ' ,
32
- 'limit ' => 100
32
+ 'limit ' => 100 ,
33
+ 'page ' => 1
33
34
];
34
35
35
- $ options = getopt ('' , ['user: ' , 'password: ' , 'service: ' , 'format: ' , 'query: ' , 'limit: ' ]);
36
+ $ options = getopt ('' , ['user: ' , 'password: ' , 'service: ' , 'format: ' , 'query: ' , 'limit: ' , ' page: ' ]);
36
37
37
38
$ requiredOptions = ['user ' , 'password ' , 'service ' , 'query ' ];
38
39
39
40
foreach ($ requiredOptions as $ option ) {
40
41
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 );
42
44
}
43
45
}
44
46
45
47
$ options = array_merge ($ defaults , $ options );
46
48
47
49
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 );
49
57
}
50
58
51
59
$ exportformats = [
55
63
];
56
64
57
65
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 );
59
68
}
60
69
61
70
// Initialize database connection parameters
62
71
63
72
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 );
66
76
}
67
77
68
78
if ($ options ['service ' ] !== substr (preg_replace ('|[^a-zA-Z0-9:.() =/_-]| ' , '' ,
69
79
$ options ['service ' ]), 0 , 2000 )) {
70
- die ("ERROR: --service contains invalid characters. \n" );
80
+ fwrite (STDERR , "ERROR: --service contains invalid characters. \n" );
81
+ exit (1 );
71
82
}
72
83
73
84
$ charset = 'UTF-8 ' ;
93
104
if ($ cursor ) {
94
105
if (ocistatementtype ($ cursor ) !== 'SELECT ' ) {
95
106
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 );
97
109
}
98
110
}
99
111
109
121
}
110
122
}
111
123
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
+
112
137
// Header
113
138
114
139
if ($ options ['format ' ] == 'xml ' ) {
118
143
$ userstr = $ options ['user ' ];
119
144
$ userstr .= '@ ' . $ options ['service ' ];
120
145
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 ' ]));
124
150
125
151
// Column aliases: We can use column names as tag names only if
126
152
// they're valid XML names - <count(MYFIELD)> won't work.
211
237
}
212
238
213
239
if ($ options ['format ' ] == 'xml ' ) {
214
- echo sprintf ("\t<row%s> \n" ,
240
+ echo sprintf ("\t<row num= \"%d \"%s> \n" ,
241
+ ($ i + $ rowOffset ),
215
242
(isset ($ row ['ROWID_ ' ]) ? (' id=" ' . htmlspecialchars ($ row ['ROWID_ ' ]) . '" ' ) : '' ));
216
243
217
244
foreach ($ row as $ fieldname => $ value ) {
257
284
echo "</tr> \n" ;
258
285
}
259
286
260
- if (($ exportlimit > 0 ) && ($ exportlimit <= ++$ i )) {
287
+ if (($ exportlimit > 0 ) && ($ exportlimit < ++$ i )) {
261
288
break ;
262
289
}
263
290
}
@@ -297,7 +324,8 @@ function pof_connect()
297
324
$ err = ocierror ();
298
325
299
326
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 );
301
329
}
302
330
}
303
331
@@ -321,7 +349,8 @@ function pof_opencursor($sql, $bind = false)
321
349
if (!$ cursor ) {
322
350
$ err = ocierror ($ conn );
323
351
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 );
325
354
}
326
355
} else { // This might improve performance?
327
356
ocisetprefetch ($ cursor , $ options ['limit ' ]);
@@ -338,7 +367,8 @@ function pof_opencursor($sql, $bind = false)
338
367
$ err = ocierror ($ cursor );
339
368
340
369
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 );
342
372
}
343
373
344
374
pof_closecursor ($ cursor );
0 commit comments