Skip to content

Commit

Permalink
[pdosqlexec] Added more attributes. (#1631)
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 authored Aug 23, 2021
1 parent c100f9d commit 14c5bbe
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 15 deletions.
15 changes: 15 additions & 0 deletions etc/phing-grammar.rng
Original file line number Diff line number Diff line change
Expand Up @@ -4908,6 +4908,16 @@
<data type="boolean"/>
</attribute>
</optional>
<optional>
<attribute name="expandproperties" a:defaultValue="true">
<data type="boolean"/>
</attribute>
</optional>
<optional>
<attribute name="keepformat" a:defaultValue="false">
<data type="boolean"/>
</attribute>
</optional>
<optional>
<attribute name="errorproperty"/>
</optional>
Expand Down Expand Up @@ -4958,6 +4968,11 @@
<data type="boolean"/>
</attribute>
</optional>
<optional>
<attribute name="showtrailers">
<data type="boolean"/>
</attribute>
</optional>
<optional>
<attribute name="coldelim"/>
</optional>
Expand Down
22 changes: 14 additions & 8 deletions src/Phing/Task/System/Pdo/DefaultPDOQuerySplitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,16 @@ public function nextQuery(): ?string
while (($line = $this->sqlReader->readLine()) !== null) {
$delimiter = $this->parent->getDelimiter();
$project = $this->parent->getOwningTarget()->getProject();
$line = $project->replaceProperties(trim($line));
if (!$this->keepformat) {
$line = trim($line);
}
if ($this->expandProperties) {
$line = $project->replaceProperties($line);
}

if (
($line != $delimiter)
!$this->keepformat
&& ($line !== $delimiter)
&& (StringHelper::startsWith('//', $line)
|| StringHelper::startsWith('--', $line)
|| StringHelper::startsWith('#', $line))
Expand All @@ -88,7 +94,7 @@ public function nextQuery(): ?string

if (
strlen($line) > 4
&& 'REM ' === strtoupper(substr($line, 0, 4))
&& stripos($line, 'REM ') === 0
) {
continue;
}
Expand All @@ -110,13 +116,13 @@ public function nextQuery(): ?string
// 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 (false !== strpos((string) $line, '--')) {
if (!$this->keepformat && false !== strpos((string) $line, '--')) {
$sql .= "\n";
}

// DELIM_ROW doesn't need this (as far as i can tell)
if (PDOSQLExecTask::DELIM_NORMAL == $this->delimiterType) {
$reg = "#((?:\"(?:\\\\.|[^\"])*\"?)+|'(?:\\\\.|[^'])*'?|" . preg_quote($delimiter) . ')#';
if (PDOSQLExecTask::DELIM_NORMAL === $this->delimiterType) {
$reg = "#((?:\"(?:\\\\.|[^\"])*\"?)+|'(?:\\\\.|[^'])*'?|" . preg_quote($delimiter, null) . ')#';

$sqlParts = preg_split($reg, $sql, 0, PREG_SPLIT_DELIM_CAPTURE);
$this->sqlBacklog = '';
Expand All @@ -133,13 +139,13 @@ public function nextQuery(): ?string
}
}

if ($hasQuery || (PDOSQLExecTask::DELIM_ROW == $this->delimiterType && $line == $delimiter)) {
if ($hasQuery || (PDOSQLExecTask::DELIM_ROW === $this->delimiterType && $line === $delimiter)) {
// this assumes there is always a delimter on the end of the SQL statement.
return StringHelper::substring(
$sql,
0,
strlen($sql) - strlen($delimiter)
- (PDOSQLExecTask::DELIM_ROW == $this->delimiterType ? 2 : 1)
- (PDOSQLExecTask::DELIM_ROW === $this->delimiterType ? 2 : 1)
);
}
}
Expand Down
19 changes: 16 additions & 3 deletions src/Phing/Task/System/Pdo/DummyPDOQuerySplitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ public function nextQuery(): ?string
while (($line = $this->sqlReader->readLine()) !== null) {
$delimiter = $this->parent->getDelimiter();
$project = $this->parent->getOwningTarget()->getProject();
$line = $project->replaceProperties(trim($line));
if (!$this->keepformat) {
$line = trim($line);
}
if ($this->expandProperties) {
$line = $project->replaceProperties($line);
}

if (
($line != $delimiter)
!$this->keepformat
&& ($line !== $delimiter)
&& (StringHelper::startsWith('//', $line)
|| StringHelper::startsWith('--', $line)
|| StringHelper::startsWith('#', $line))
Expand All @@ -55,8 +61,15 @@ public function nextQuery(): ?string

$sql .= ' ' . $line . "\n";

// 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 (!$this->keepformat && strpos($line, '--') !== false) {
$sql .= "\n";
}

/*
* fix issue with PDO and wrong formated multistatements
* fix issue with PDO and wrong formatted multi statements
*
* @issue 1108
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Phing/Task/System/Pdo/PDOQuerySplitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ abstract class PDOQuerySplitter
*/
protected $sqlReader;

protected $keepformat = false;
protected $expandProperties = true;

/**
* Constructor, sets the parent task and reader with SQL source.
*/
Expand All @@ -53,6 +56,16 @@ public function __construct(PDOSQLExecTask $parent, Reader $reader)
$this->sqlReader = new BufferedReader($reader);
}

public function setKeepformat(bool $keepformat): void
{
$this->keepformat = $keepformat;
}

public function setExpandProperties(bool $expandProps): void
{
$this->expandProperties = $expandProps;
}

/**
* Returns next query from SQL source, null if no more queries left.
*
Expand Down
18 changes: 17 additions & 1 deletion src/Phing/Task/System/Pdo/PDOSQLExecFormatterElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class PDOSQLExecFormatterElement
*/
private $showheaders = true;

/**
* Print trailer.
*
* @var bool
*/
private $showtrailers = true;

/**
* Whether to format XML output.
*
Expand Down Expand Up @@ -173,6 +180,10 @@ public function prepare(Location $location): void
if ($this->formatter instanceof PlainPDOResultFormatter) {
// set any options that apply to the plain formatter
$this->formatter->setShowheaders($this->showheaders);
if ($this->showtrailers) {
$this->formatter->setStatementCounter($this->parentTask->getGoodSQL());
$this->formatter->setShowtrailers($this->showtrailers);
}
$this->formatter->setRowdelim($this->rowdelimiter);
$this->formatter->setColdelim($this->coldelimiter);
} elseif ($this->formatter instanceof XMLPDOResultFormatter) {
Expand Down Expand Up @@ -274,7 +285,7 @@ public function getOutfile(): ?File
*/
public function setAppend(bool $append): void
{
$this->append = (bool) $append;
$this->append = $append;
}

/**
Expand All @@ -298,6 +309,11 @@ public function setShowheaders($showheaders): void
$this->showheaders = (bool) $showheaders;
}

public function setShowtrailers($showtrailers): void
{
$this->showtrailers = (bool) $showtrailers;
}

/**
* Sets the column delimiter.
*
Expand Down
38 changes: 35 additions & 3 deletions src/Phing/Task/System/Pdo/PDOSQLExecTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ class PDOSQLExecTask extends PDOTask implements Condition
*/
private $statementCountProperty;

/**
* @var bool
*/
private $keepformat = false;

/**
* @var bool
*/
private $expandProperties = true;

/**
* Set the name of the SQL file to be run.
* Required unless statements are enclosed in the build file.
Expand Down Expand Up @@ -297,6 +307,11 @@ public function setFetchmode($mode): void
}
}

public function getGoodSQL()
{
return $this->goodSql;
}

/**
* Property to set to "true" if a statement throws an error.
*
Expand All @@ -317,6 +332,16 @@ public function setStatementCountProperty(string $statementCountProperty): void
$this->statementCountProperty = $statementCountProperty;
}

public function setKeepformat(bool $keepformat): void
{
$this->keepformat = $keepformat;
}

public function setExpandProperties(bool $expandProps): void
{
$this->expandProperties = $expandProps;
}

/**
* Load the sql file and then execute it.
*
Expand Down Expand Up @@ -462,6 +487,9 @@ public function runStatements(Reader $reader): void
$splitter = new DefaultPDOQuerySplitter($this, $reader, $this->delimiterType);
}

$splitter->setExpandProperties($this->expandProperties);
$splitter->setKeepformat($this->keepformat);

try {
while (null !== ($query = $splitter->nextQuery())) {
$this->log('SQL: ' . $query, Project::MSG_VERBOSE);
Expand Down Expand Up @@ -553,7 +581,7 @@ protected function execSQL($sql): void
$this->log('Failed to execute: ' . $sql, Project::MSG_ERR);
$this->setErrorProp();
if ('abort' !== $this->onError) {
$this->log((string)$e, Project::MSG_ERR);
$this->log((string) $e, Project::MSG_ERR);
}
if ('continue' !== $this->onError) {
throw new BuildException('Failed to execute SQL', $e);
Expand All @@ -572,7 +600,11 @@ protected function getConfiguredFormatters(): array
{
$formatters = [];
foreach ($this->formatters as $fe) {
$formatters[] = $fe->getFormatter();
$formatter = $fe->getFormatter();
if ($formatter instanceof PlainPDOResultFormatter) {
$formatter->setStatementCounter($this->goodSql);
}
$formatters[] = $formatter;
}

return $formatters;
Expand Down Expand Up @@ -665,7 +697,7 @@ private function setProperty(?string $name, string $value): void
*/
private function closeQuietly(): void
{
if (!$this->isAutocommit() && null !== $this->conn && 'abort' === $this->onError) {
if (null !== $this->conn && 'abort' === $this->onError && !$this->isAutocommit()) {
try {
$this->conn->rollback();
} catch (PDOException $ex) {
Expand Down
21 changes: 21 additions & 0 deletions src/Phing/Task/System/Pdo/PlainPDOResultFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class PlainPDOResultFormatter extends PDOResultFormatter
* @var bool
*/
private $showheaders = true;
private $showtrailers = false;

/**
* Column delimiter.
Expand All @@ -61,6 +62,7 @@ class PlainPDOResultFormatter extends PDOResultFormatter
* @var string
*/
private $rowdelimiter = PHP_EOL;
private $statementcounter = 0;

/**
* Set the showheaders attribute.
Expand All @@ -72,6 +74,21 @@ public function setShowheaders($v)
$this->showheaders = StringHelper::booleanValue($v);
}

/**
* Set the showtrailers attribute.
*
* @param bool $v
*/
public function setShowtrailers($v)
{
$this->showtrailers = StringHelper::booleanValue($v);
}

public function setStatementCounter($count)
{
$this->statementcounter = $count;
}

/**
* Sets the column delimiter.
*
Expand Down Expand Up @@ -101,6 +118,10 @@ public function processRow($row)
{
$line = '';

if ($this->showtrailers) {
$this->out->write('# ' . $this->statementcounter . ' statement(s) successful executed.' . PHP_EOL);
}

if (!$this->colsprinted && $this->showheaders) {
$first = true;
foreach ($row as $fieldName => $ignore) {
Expand Down
8 changes: 8 additions & 0 deletions tests/Phing/Task/Optional/PDOTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,12 @@ public function testStatementCountProp(): void
$this->executeTarget(__FUNCTION__);
$this->assertPropertyEquals('statement.count', 2);
}

public function testOptionalAttributes(): void
{
$this->executeTarget(__FUNCTION__);
$this->assertFileExists('result.txt');
$this->assertStringContainsString('# 3 statement(s) successful executed.', file_get_contents('result.txt'));
@unlink('result.txt');
}
}
22 changes: 22 additions & 0 deletions tests/etc/tasks/ext/pdo/test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,26 @@
)]]></transaction>
</pdosqlexec>
</target>
<target name="testOptionalAttributes">
<pdosqlexec url="sqlite:test.db">
<transaction>DROP TABLE IF EXISTS xxxxx</transaction>
<transaction><![CDATA[
CREATE TABLE xxxxx (
test_id INTEGER PRIMARY KEY AUTOINCREMENT,
test_field TEXT
)]]></transaction>
<transaction><![CDATA[
INSERT INTO xxxxx (test_field)
VALUES('test field 1')
]]></transaction>
<transaction><![CDATA[
SELECT * from xxxxx
]]></transaction>
<formatter type="plain"
showheaders="true"
showtrailers="true"
outfile="result.txt"
/>
</pdosqlexec>
</target>
</project>

0 comments on commit 14c5bbe

Please sign in to comment.