File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ });
You can’t perform that action at this time.
0 commit comments