Skip to content

Commit 29b4593

Browse files
committed
Added SqlDocument::comment()
1 parent 19d281b commit 29b4593

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

src/Helpers.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,14 @@ public static function formatValue($value, IDriver $driver)
4040

4141
throw new InvalidArgumentException("Unsupported value type.");
4242
}
43+
44+
45+
/**
46+
* @param string
47+
* @return string
48+
*/
49+
public static function normalizeNewLines($s)
50+
{
51+
return str_replace(array("\r\n", "\r"), "\n", $s);
52+
}
4353
}

src/SqlDocument.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,16 @@ public function command($command)
132132
$this->addStatement($statement);
133133
return $statement;
134134
}
135+
136+
137+
/**
138+
* @param string
139+
* @return Statements\Comment
140+
*/
141+
public function comment($comment)
142+
{
143+
$statement = new Statements\Comment($comment);
144+
$this->addStatement($statement);
145+
return $statement;
146+
}
135147
}

src/Statements/Comment.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace CzProject\SqlGenerator\Statements;
4+
5+
use CzProject\SqlGenerator\Helpers;
6+
use CzProject\SqlGenerator\IDriver;
7+
use CzProject\SqlGenerator\IStatement;
8+
9+
10+
class Comment implements IStatement
11+
{
12+
/** @var string */
13+
private $comment;
14+
15+
16+
/**
17+
* @param string
18+
*/
19+
public function __construct($comment)
20+
{
21+
$this->comment = $comment;
22+
}
23+
24+
25+
/**
26+
* @return string
27+
*/
28+
public function toSql(IDriver $driver)
29+
{
30+
return '-- ' . str_replace("\n", "\n-- ", Helpers::normalizeNewLines(trim($this->comment)));
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use CzProject\SqlGenerator\Drivers;
4+
use CzProject\SqlGenerator\SqlDocument;
5+
use CzProject\SqlGenerator\Statements\IndexDefinition;
6+
use Tester\Assert;
7+
8+
require __DIR__ . '/../bootstrap.php';
9+
10+
11+
test(function () {
12+
13+
$sql = new SqlDocument;
14+
$driver = new Drivers\MysqlDriver;
15+
16+
$sql->comment('inline comment');
17+
$sql->comment("\tblock comment #1\nblock comment #2\n\nblock comment #3\n");
18+
19+
Assert::same(implode("\n", array(
20+
'',
21+
'-- inline comment',
22+
'',
23+
'-- block comment #1',
24+
'-- block comment #2',
25+
'-- ',
26+
'-- block comment #3',
27+
'',
28+
)), $sql->toSql($driver));
29+
30+
});

0 commit comments

Comments
 (0)