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