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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
<?php
/*
* $Id: CreoleSQLExecTask.php 59 2006-04-28 14:49:47Z mrook $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
require_once 'phing/tasks/ext/CreoleTask.php';
include_once 'phing/system/io/StringReader.php';
/**
* Executes a series of SQL statements on a database using Creole.
*
* <p>Statements can
* either be read in from a text file using the <i>src</i> attribute or from
* between the enclosing SQL tags.</p>
*
* <p>Multiple statements can be provided, separated by semicolons (or the
* defined <i>delimiter</i>). Individual lines within the statements can be
* commented using either --, // or REM at the start of the line.</p>
*
* <p>The <i>autocommit</i> attribute specifies whether auto-commit should be
* turned on or off whilst executing the statements. If auto-commit is turned
* on each statement will be executed and committed. If it is turned off the
* statements will all be executed as one transaction.</p>
*
* <p>The <i>onerror</i> attribute specifies how to proceed when an error occurs
* during the execution of one of the statements.
* The possible values are: <b>continue</b> execution, only show the error;
* <b>stop</b> execution and commit transaction;
* and <b>abort</b> execution and transaction and fail task.</p>
*
* @author Hans Lellelid <hans@xmpl.org> (Phing)
* @author Jeff Martin <jeff@custommonkey.org> (Ant)
* @author Michael McCallum <gholam@xtra.co.nz> (Ant)
* @author Tim Stephenson <tim.stephenson@sybase.com> (Ant)
* @package phing.tasks.ext
* @version $Revision: 1.21 $
*/
class CreoleSQLExecTask extends CreoleTask {
private $goodSql = 0;
private $totalSql = 0;
const DELIM_ROW = "row";
const DELIM_NORMAL = "normal";
/**
* Database connection
*/
private $conn = null;
/**
* files to load
*/
private $filesets = array();
/**
* SQL statement
*/
private $statement = null;
/**
* SQL input file
*/
private $srcFile = null;
/**
* SQL input command
*/
private $sqlCommand = "";
/**
* SQL transactions to perform
*/
private $transactions = array();
/**
* SQL Statement delimiter
*/
private $delimiter = ";";
/**
* The delimiter type indicating whether the delimiter will
* only be recognized on a line by itself
*/
private $delimiterType = "normal"; // can't use constant just defined
/**
* Print SQL results.
*/
private $print = false;
/**
* Print header columns.
*/
private $showheaders = true;
/**
* Results Output file.
*/
private $output = null;
/**
* Action to perform if an error is found
**/
private $onError = "abort";
/**
* Encoding to use when reading SQL statements from a file
*/
private $encoding = null;
/**
* Append to an existing file or overwrite it?
*/
private $append = false;
/**
* Set the name of the SQL file to be run.
* Required unless statements are enclosed in the build file
*/
public function setSrc(PhingFile $srcFile) {
$this->srcFile = $srcFile;
}
/**
* Set an inline SQL command to execute.
* NB: Properties are not expanded in this text.
*/
public function addText($sql) {
$this->sqlCommand .= $sql;
}
/**
* Adds a set of files (nested fileset attribute).
*/
public function addFileset(FileSet $set) {
$this->filesets[] = $set;
}
/**
* Add a SQL transaction to execute
*/
public function createTransaction() {
$t = new SQLExecTransaction($this);
$this->transactions[] = $t;
return $t;
}
/**
* Set the file encoding to use on the SQL files read in
*
* @param encoding the encoding to use on the files
*/
public function setEncoding($encoding) {
$this->encoding = $encoding;
}
/**
* Set the statement delimiter.
*
* <p>For example, set this to "go" and delimitertype to "ROW" for
* Sybase ASE or MS SQL Server.</p>
*
* @param delimiter
*/
public function setDelimiter($delimiter)
{
$this->delimiter = $delimiter;
}
/**
* Set the Delimiter type for this sql task. The delimiter type takes two
* values - normal and row. Normal means that any occurence of the delimiter
* terminate the SQL command whereas with row, only a line containing just
* the delimiter is recognized as the end of the command.
*
* @param string $delimiterType
*/
public function setDelimiterType($delimiterType)
{
$this->delimiterType = $delimiterType;
}
/**
* Set the print flag.
*
* @param boolean $print
*/
public function setPrint($print)
{
$this->print = (boolean) $print;
}
/**
* Print headers for result sets from the
* statements; optional, default true.
* @param boolean $showheaders
*/
public function setShowheaders($showheaders) {
$this->showheaders = (boolean) $showheaders;
}
/**
* Set the output file;
* optional, defaults to the console.
* @param PhingFile $output
*/
public function setOutput(PhingFile $output) {
$this->output = $output;
}
/**
* whether output should be appended to or overwrite
* an existing file. Defaults to false.
* @param $append
*/
public function setAppend($append) {
$this->append = (boolean) $append;
}
/**
* Action to perform when statement fails: continue, stop, or abort
* optional; default "abort"
*/
public function setOnerror($action) {
$this->onError = $action;
}
/**
* Load the sql file and then execute it
* @throws BuildException
*/
public function main() {
$savedTransaction = array();
for($i=0,$size=count($this->transactions); $i < $size; $i++) {
$savedTransaction[] = clone $this->transactions[$i];
}
$savedSqlCommand = $this->sqlCommand;
$this->sqlCommand = trim($this->sqlCommand);
try {
if ($this->srcFile === null && $this->sqlCommand === ""
&& empty($this->filesets)) {
if (count($this->transactions) === 0) {
throw new BuildException("Source file or fileset, "
. "transactions or sql statement "
. "must be set!", $this->location);
}
}
if ($this->srcFile !== null && !$this->srcFile->exists()) {
throw new BuildException("Source file does not exist!", $this->location);
}
// deal with the filesets
for ($i = 0,$size=count($this->filesets); $i < $size; $i++) {
$fs = $this->filesets[$i];
$ds = $fs->getDirectoryScanner($this->project);
$srcDir = $fs->getDir($this->project);
$srcFiles = $ds->getIncludedFiles();
// Make a transaction for each file
for ($j=0, $size=count($srcFiles); $j < $size; $j++) {
$t = $this->createTransaction();
$t->setSrc(new PhingFile($srcDir, $srcFiles[$j]));
}
}
// Make a transaction group for the outer command
$t = $this->createTransaction();
if ($this->srcFile) $t->setSrc($this->srcFile);
$t->addText($this->sqlCommand);
$this->conn = $this->getConnection();
try {
$this->statement = $this->conn->createStatement();
$out = null;
try {
if ($this->output !== null) {
$this->log("Opening output file " . $this->output, PROJECT_MSG_VERBOSE);
$out = new BufferedWriter(new FileWriter($this->output->getAbsolutePath(), $this->append));
}
// Process all transactions
for ($i=0,$size=count($this->transactions); $i < $size; $i++) {
$this->transactions[$i]->runTransaction($out);
if (!$this->isAutocommit()) {
$this->log("Commiting transaction", PROJECT_MSG_VERBOSE);
$this->conn->commit();
}
}
if ($out) $out->close();
} catch (Exception $e) {
if ($out) $out->close();
throw $e;
}
} catch (IOException $e) {
if (!$this->isAutocommit() && $this->conn !== null && $this->onError == "abort") {
try {
$this->conn->rollback();
} catch (SQLException $ex) {}
}
throw new BuildException($e->getMessage(), $this->location);
} catch (SQLException $e){
if (!$this->isAutocommit() && $this->conn !== null && $this->onError == "abort") {
try {
$this->conn->rollback();
} catch (SQLException $ex) {}
}
throw new BuildException($e->getMessage(), $this->location);
}
$this->log($this->goodSql . " of " . $this->totalSql .
" SQL statements executed successfully");
} catch (Exception $e) {
$this->transactions = $savedTransaction;
$this->sqlCommand = $savedSqlCommand;
throw $e;
}
// finally {
$this->transactions = $savedTransaction;
$this->sqlCommand = $savedSqlCommand;
}
/**
* read in lines and execute them
* @throws SQLException, IOException
*/
public function runStatements(Reader $reader, $out = null) {
$sql = "";
$line = "";
$in = new BufferedReader($reader);
try {
while (($line = $in->readLine()) !== null) {
$line = trim($line);
$line = ProjectConfigurator::replaceProperties($this->project, $line,
$this->project->getProperties());
if (StringHelper::startsWith("//", $line) ||
StringHelper::startsWith("--", $line) ||
StringHelper::startsWith("#", $line)) {
continue;
}
if (strlen($line) > 4
&& strtoupper(substr($line,0, 4)) == "REM ") {
continue;
}
$sql .= " " . $line;
$sql = trim($sql);
// SQL defines "--" as a comment to EOL
// and in Oracle it may contain a hint
// so we cannot just remove it, instead we must end it
if (strpos($line, "--") !== false) {
$sql .= "\n";
}
if ($this->delimiterType == self::DELIM_NORMAL
&& StringHelper::endsWith($this->delimiter, $sql)
|| $this->delimiterType == self::DELIM_ROW
&& $line == $this->delimiter) {
$this->log("SQL: " . $sql, PROJECT_MSG_VERBOSE);
$this->execSQL(StringHelper::substring($sql, 0, strlen($sql) - strlen($this->delimiter)) - 1, $out);
$sql = "";
}
}
// Catch any statements not followed by ;
if ($sql !== "") {
$this->execSQL($sql, $out);
}
} catch (SQLException $e) {
throw new BuildException("Error running statements", $e);
}
}
/**
* Exec the sql statement.
* @throws SQLException
*/
protected function execSQL($sql, $out = null) {
// Check and ignore empty statements
if (trim($sql) == "") {
return;
}
try {
$this->totalSql++;
if (!$this->statement->execute($sql)) {
$this->log($this->statement->getUpdateCount() . " rows affected", PROJECT_MSG_VERBOSE);
} else {
if ($this->print) {
$this->printResults($out);
}
}
$this->goodSql++;
} catch (SQLException $e) {
$this->log("Failed to execute: " . $sql, PROJECT_MSG_ERR);
if ($this->onError != "continue") {
throw new BuildException("Failed to execute SQL", $e);
}
$this->log($e->getMessage(), PROJECT_MSG_ERR);
}
}
/**
* print any results in the statement.
* @throw SQLException
*/
protected function printResults($out = null) {
$lSep = Phing::getProperty('line.separator');
$rs = null;
do {
$rs = $this->statement->getResultSet();
if ($rs !== null) {
$this->log("Processing new result set.", PROJECT_MSG_VERBOSE);
$line = "";
$colsprinted = false;
while ($rs->next()) {
$fields = $rs->getRow();
if (!$colsprinted && $this->showheaders) {
$first = true;
foreach($fields as $fieldName => $ignore) {
if ($first) $first = false; else $line .= ",";
$line .= $fieldName;
}
if ($out !== null) {
$out->write($line);
$out->newLine();
} else {
print($line.$lSep);
}
$line = "";
$colsprinted = true;
} // if show headers
$first = true;
foreach($fields as $columnValue) {
if ($columnValue != null) {
$columnValue = trim($columnValue);
}
if ($first) {
$first = false;
} else {
$line .= ",";
}
$line .= $columnValue;
}
if ($out !== null) {
$out->write($line);
$out->newLine();
} else {
print($line . $lSep);
}
$line = "";
} // while rs->next()
}
} while ($this->statement->getMoreResults());
print($lSep);
if ($out !== null) $out->newLine();
}
}
/**
* "Inner" class that contains the definition of a new transaction element.
* Transactions allow several files or blocks of statements
* to be executed using the same JDBC connection and commit
* operation in between.
*/
class SQLExecTransaction {
private $tSrcFile = null;
private $tSqlCommand = "";
private $parent;
function __construct($parent)
{
// Parent is required so that we can log things ...
$this->parent = $parent;
}
public function setSrc(PhingFile $src)
{
$this->tSrcFile = $src;
}
public function addText($sql)
{
$this->tSqlCommand .= $sql;
}
/**
* @throws IOException, SQLException
*/
public function runTransaction($out = null)
{
if (!empty($this->tSqlCommand)) {
$this->parent->log("Executing commands", PROJECT_MSG_INFO);
$this->parent->runStatements(new StringReader($this->tSqlCommand), $out);
}
if ($this->tSrcFile !== null) {
$this->parent->log("Executing file: " . $this->tSrcFile->getAbsolutePath(),
PROJECT_MSG_INFO);
$reader = new FileReader($this->tSrcFile);
$this->parent->runStatements($reader, $out);
$reader->close();
}
}
}
|