123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647 |
- <?php
- require_once 'phing/tasks/ext/pdo/PDOTask.php';
- include_once 'phing/system/io/StringReader.php';
- include_once 'phing/tasks/ext/pdo/PDOSQLExecFormatterElement.php';
- class PDOSQLExecTask extends PDOTask {
-
- private $goodSql = 0;
-
- private $totalSql = 0;
- const DELIM_ROW = "row";
- const DELIM_NORMAL = "normal";
-
- private $conn = null;
-
- private $filesets = array();
-
- private $filelists = array();
-
-
- private $formatters = array();
-
- private $statement;
-
- private $srcFile;
-
- private $sqlCommand = "";
-
- private $transactions = array();
-
- private $delimiter = ";";
-
- private $delimiterType = "normal";
-
- private $onError = "abort";
-
- private $encoding = null;
-
- private $fetchMode;
-
- public function setSrc(PhingFile $srcFile) {
- $this->srcFile = $srcFile;
- }
-
- public function addText($sql) {
- $this->sqlCommand .= $sql;
- }
-
- public function addFileset(FileSet $set) {
- $this->filesets[] = $set;
- }
-
- public function addFilelist(FileList $list) {
- $this->filelists[] = $list;
- }
-
-
- public function createFormatter()
- {
- $fe = new PDOSQLExecFormatterElement($this);
- $this->formatters[] = $fe;
- return $fe;
- }
-
- public function createTransaction() {
- $t = new PDOSQLExecTransaction($this);
- $this->transactions[] = $t;
- return $t;
- }
-
- public function setEncoding($encoding) {
- $this->encoding = $encoding;
- }
-
- public function setDelimiter($delimiter)
- {
- $this->delimiter = $delimiter;
- }
-
- public function setDelimiterType($delimiterType)
- {
- $this->delimiterType = $delimiterType;
- }
-
- public function setOnerror($action) {
- $this->onError = $action;
- }
-
- public function setFetchmode($mode) {
- if (is_numeric($mode)) {
- $this->fetchMode = (int) $mode;
- } else {
- if (defined($mode)) {
- $this->fetchMode = constant($mode);
- } else {
- throw new BuildException("Invalid PDO fetch mode specified: " . $mode, $this->getLocation());
- }
- }
- }
-
- private function getDefaultOutput()
- {
- return new LogWriter($this);
- }
-
- public function main() {
-
-
- if ($this->fetchMode === null) {
- $this->fetchMode = PDO::FETCH_BOTH;
- }
-
-
- foreach($this->formatters as $fe) {
- $fe->prepare();
- }
- $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) && empty($this->filelists)
- && count($this->transactions) === 0) {
- throw new BuildException("Source file or fileset/filelist, "
- . "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);
- }
-
- foreach($this->filesets as $fs) {
- $ds = $fs->getDirectoryScanner($this->project);
- $srcDir = $fs->getDir($this->project);
- $srcFiles = $ds->getIncludedFiles();
-
- foreach($srcFiles as $srcFile) {
- $t = $this->createTransaction();
- $t->setSrc(new PhingFile($srcDir, $srcFile));
- }
- }
-
-
- foreach($this->filelists as $fl) {
- $srcDir = $fl->getDir($this->project);
- $srcFiles = $fl->getFiles($this->project);
-
- foreach($srcFiles as $srcFile) {
- $t = $this->createTransaction();
- $t->setSrc(new PhingFile($srcDir, $srcFile));
- }
- }
-
- $t = $this->createTransaction();
- if ($this->srcFile) $t->setSrc($this->srcFile);
- $t->addText($this->sqlCommand);
- $this->conn = $this->getConnection();
- try {
- $this->statement = null;
-
- $this->initFormatters();
- try {
-
- for ($i=0,$size=count($this->transactions); $i < $size; $i++) {
- if (!$this->isAutocommit()) {
- $this->log("Beginning transaction", Project::MSG_VERBOSE);
- $this->conn->beginTransaction();
- }
- $this->transactions[$i]->runTransaction();
- if (!$this->isAutocommit()) {
- $this->log("Commiting transaction", Project::MSG_VERBOSE);
- $this->conn->commit();
- }
- }
- } catch (Exception $e) {
- throw $e;
- }
- } catch (IOException $e) {
- if (!$this->isAutocommit() && $this->conn !== null && $this->onError == "abort") {
- try {
- $this->conn->rollback();
- } catch (PDOException $ex) {}
- }
- throw new BuildException($e->getMessage(), $this->location);
- } catch (PDOException $e){
- if (!$this->isAutocommit() && $this->conn !== null && $this->onError == "abort") {
- try {
- $this->conn->rollback();
- } catch (PDOException $ex) {}
- }
- throw new BuildException($e->getMessage(), $this->location);
- }
-
-
- $this->closeFormatters();
- $this->log($this->goodSql . " of " . $this->totalSql .
- " SQL statements executed successfully");
- } catch (Exception $e) {
- $this->transactions = $savedTransaction;
- $this->sqlCommand = $savedSqlCommand;
- throw $e;
- }
-
- $this->transactions = $savedTransaction;
- $this->sqlCommand = $savedSqlCommand;
- }
-
- public function runStatements(Reader $reader) {
- $sql = "";
- $line = "";
- $sqlBacklog = "";
- $hasQuery = false;
- $in = new BufferedReader($reader);
- try {
- while (($line = $in->readLine()) !== null) {
- $line = trim($line);
- $line = ProjectConfigurator::replaceProperties($this->project, $line,
- $this->project->getProperties());
- if (($line != $this->delimiter) && (
- StringHelper::startsWith("//", $line) ||
- StringHelper::startsWith("--", $line) ||
- StringHelper::startsWith("#", $line))) {
- continue;
- }
- if (strlen($line) > 4
- && strtoupper(substr($line,0, 4)) == "REM ") {
- continue;
- }
-
- if (preg_match('/DELIMITER [\'"]?([^\'" $]+)[\'"]?/i', $line, $matches)) {
- $this->setDelimiter($matches[1]);
- continue;
- }
- if ($sqlBacklog !== "") {
- $sql = $sqlBacklog;
- $sqlBacklog = "";
- }
- $sql .= " " . $line . "\n";
-
-
-
- if (strpos($line, "--") !== false) {
- $sql .= "\n";
- }
-
- if ($this->delimiterType == self::DELIM_NORMAL) {
- $reg = "#((?:\"(?:\\\\.|[^\"])*\"?)+|'(?:\\\\.|[^'])*'?|" . preg_quote($this->delimiter) . ")#";
- $sqlParts = preg_split($reg, $sql, 0, PREG_SPLIT_DELIM_CAPTURE);
- $sqlBacklog = "";
- foreach ($sqlParts as $sqlPart) {
-
- $sqlBacklog .= $sqlPart;
-
- if ($sqlPart === $this->delimiter) {
- $sql = $sqlBacklog;
- $sqlBacklog = "";
- $hasQuery = true;
- }
- }
- }
- if ($hasQuery || ($this->delimiterType == self::DELIM_ROW && $line == $this->delimiter)) {
-
- $sql = StringHelper::substring($sql, 0, strlen($sql) - 1 - strlen($this->delimiter));
- $this->log("SQL: " . $sql, Project::MSG_VERBOSE);
- $this->execSQL($sql);
- $sql = "";
- $hasQuery = false;
- }
- }
-
- if ($sql !== "") {
- $this->execSQL($sql);
- }
- } catch (PDOException $e) {
- throw $e;
- }
- }
-
- protected function isSelectSql($sql)
- {
- $sql = trim($sql);
- return (stripos($sql, 'select') === 0 && stripos($sql, 'select into ') !== 0);
- }
-
- protected function execSQL($sql) {
-
- if (trim($sql) == "") {
- return;
- }
- try {
- $this->totalSql++;
- $this->statement = $this->conn->prepare($sql);
- $this->statement->execute();
- $this->log($this->statement->rowCount() . " rows affected", Project::MSG_VERBOSE);
-
- if ($this->statement->columnCount() > 0)
- {
- $this->processResults();
- }
- $this->statement->closeCursor();
- $this->statement = null;
- $this->goodSql++;
- } catch (PDOException $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);
- }
- }
-
- protected function getConfiguredFormatters()
- {
- $formatters = array();
- foreach ($this->formatters as $fe) {
- $formatters[] = $fe->getFormatter();
- }
- return $formatters;
- }
-
- protected function initFormatters() {
- $formatters = $this->getConfiguredFormatters();
- foreach ($formatters as $formatter) {
- $formatter->initialize();
- }
- }
-
- protected function closeFormatters() {
- $formatters = $this->getConfiguredFormatters();
- foreach ($formatters as $formatter) {
- $formatter->close();
- }
- }
-
- protected function processResults() {
- try {
- $this->log("Processing new result set.", Project::MSG_VERBOSE);
- $formatters = $this->getConfiguredFormatters();
- while ($row = $this->statement->fetch($this->fetchMode)) {
- foreach ($formatters as $formatter) {
- $formatter->processRow($row);
- }
- }
- } catch (Exception $x) {
- $this->log("Error processing reults: " . $x->getMessage(), Project::MSG_ERR);
- foreach ($formatters as $formatter) {
- $formatter->close();
- }
- throw $x;
- }
- }
- }
- class PDOSQLExecTransaction {
- private $tSrcFile = null;
- private $tSqlCommand = "";
- private $parent;
- function __construct($parent)
- {
-
- $this->parent = $parent;
- }
- public function setSrc(PhingFile $src)
- {
- $this->tSrcFile = $src;
- }
- public function addText($sql)
- {
- $this->tSqlCommand .= $sql;
- }
-
- public function runTransaction()
- {
- if (!empty($this->tSqlCommand)) {
- $this->parent->log("Executing commands", Project::MSG_INFO);
- $this->parent->runStatements(new StringReader($this->tSqlCommand));
- }
- if ($this->tSrcFile !== null) {
- $this->parent->log("Executing file: " . $this->tSrcFile->getAbsolutePath(),
- Project::MSG_INFO);
- $reader = new FileReader($this->tSrcFile);
- $this->parent->runStatements($reader);
- $reader->close();
- }
- }
- }
|