@@ -24,6 +24,66 @@ public function __construct(
2424 }
2525
2626
27+ public function escapeIdentifier (string $ value ): string
28+ {
29+ return '` ' . str_replace ('` ' , '`` ' , $ value ) . '` ' ;
30+ }
31+
32+
33+ public function escapeBool (bool $ value ): string
34+ {
35+ return $ value ? '1 ' : '0 ' ;
36+ }
37+
38+
39+ public function escapeDate (\DateTimeInterface $ value ): string
40+ {
41+ return $ value ->format ("'Y-m-d' " );
42+ }
43+
44+
45+ public function escapeDateTime (\DateTimeInterface $ value ): string
46+ {
47+ return $ value ->format ("'Y-m-d H:i:s.u' " );
48+ }
49+
50+
51+ public function escapeDateInterval (\DateInterval $ value ): string
52+ {
53+ if ($ value ->y || $ value ->m || $ value ->d ) {
54+ throw new Dibi \NotSupportedException ('Only time interval is supported. ' );
55+ }
56+
57+ return $ value ->format ("'%r%H:%I:%S.%f' " );
58+ }
59+
60+
61+ /**
62+ * Encodes string for use in a LIKE statement.
63+ */
64+ public function escapeLike (string $ value , int $ pos ): string
65+ {
66+ $ value = addcslashes (str_replace ('\\' , '\\\\' , $ value ), "\x00\n\r\\'%_ " );
67+ return ($ pos & 1 ? "'% " : "' " ) . $ value . ($ pos & 2 ? "%' " : "' " );
68+ }
69+
70+
71+ /**
72+ * Injects LIMIT/OFFSET to the SQL query.
73+ */
74+ public function applyLimit (string &$ sql , ?int $ limit , ?int $ offset ): void
75+ {
76+ if ($ limit < 0 || $ offset < 0 ) {
77+ throw new Dibi \NotSupportedException ('Negative offset or limit. ' );
78+
79+ } elseif ($ limit !== null || $ offset ) {
80+ // see http://dev.mysql.com/doc/refman/5.0/en/select.html
81+ $ sql .= ' LIMIT ' . ($ limit ?? '18446744073709551615 ' )
82+ . ($ offset ? ' OFFSET ' . $ offset : '' );
83+ }
84+ }
85+
86+
2787 /**
2888 * Returns list of tables.
2989 */
@@ -47,7 +107,7 @@ public function getTables(): array
47107 */
48108 public function getColumns (string $ table ): array
49109 {
50- $ res = $ this ->driver ->query ("SHOW FULL COLUMNS FROM {$ this ->driver -> escapeIdentifier ($ table )}" )
110+ $ res = $ this ->driver ->query ("SHOW FULL COLUMNS FROM {$ this ->escapeIdentifier ($ table )}" )
51111 ?? throw new \LogicException ('Unexpected null result. ' );
52112 $ columns = [];
53113 while ($ row = $ res ->fetch (true )) {
@@ -73,7 +133,7 @@ public function getColumns(string $table): array
73133 */
74134 public function getIndexes (string $ table ): array
75135 {
76- $ res = $ this ->driver ->query ("SHOW INDEX FROM {$ this ->driver -> escapeIdentifier ($ table )}" )
136+ $ res = $ this ->driver ->query ("SHOW INDEX FROM {$ this ->escapeIdentifier ($ table )}" )
77137 ?? throw new \LogicException ('Unexpected null result. ' );
78138 $ indexes = [];
79139 while ($ row = $ res ->fetch (true )) {
0 commit comments