2015-01-12 05:33:41 +01:00
< ? php
/**
* @ dataProvider ../ databases . ini
*/
2017-06-09 22:20:47 +02:00
declare ( strict_types = 1 );
2015-10-08 02:13:22 +02:00
use Dibi\DateTime ;
2017-07-11 12:27:26 +02:00
use Tester\Assert ;
2015-01-12 05:33:41 +01:00
require __DIR__ . '/bootstrap.php' ;
2017-02-06 10:39:34 +01:00
$conn = new Dibi\Connection ( $config + [ 'formatDateTime' => " 'Y-m-d H:i:s.u' " , 'formatDate' => " 'Y-m-d' " ]);
2015-01-12 05:33:41 +01:00
2018-05-10 21:45:11 +02:00
// Dibi detects INSERT or REPLACE command & booleans
2015-01-12 05:33:41 +01:00
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " REPLACE INTO [products] ([title], [price]) VALUES (N'Drticka', 318) " ,
" REPLACE INTO [products] ([title], [price]) VALUES ('Drticka', 318) " ,
]),
2015-10-06 01:39:01 +02:00
$conn -> translate ( 'REPLACE INTO [products]' , [
2017-09-21 14:17:14 +02:00
'title' => 'Drticka' ,
'price' => 318 ,
])
);
2015-01-12 05:33:41 +01:00
// multiple INSERT command
2015-10-06 01:39:01 +02:00
$array = [
2015-06-19 03:11:36 +02:00
'title' => 'Super Product' ,
'price' => 12 ,
2017-07-11 12:28:13 +02:00
'brand' => null ,
2015-10-06 01:39:01 +02:00
];
2015-01-12 05:33:41 +01:00
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " INSERT INTO [products] ([title], [price], [brand]) VALUES (N'Super Product', 12, NULL) , (N'Super Product', 12, NULL) , (N'Super Product', 12, NULL) " ,
" INSERT INTO [products] ([title], [price], [brand]) VALUES ('Super Product', 12, NULL) , ('Super Product', 12, NULL) , ('Super Product', 12, NULL) " ,
]),
2015-06-19 03:11:36 +02:00
$conn -> translate ( 'INSERT INTO [products]' , $array , $array , $array )
2015-01-12 05:33:41 +01:00
);
// multiple INSERT command II
2015-10-06 01:39:01 +02:00
$array = [
[ 'pole' => 'hodnota1' , 'bit' => 1 ],
[ 'pole' => 'hodnota2' , 'bit' => 1 ],
[ 'pole' => 'hodnota3' , 'bit' => 1 ],
];
2015-01-12 05:33:41 +01:00
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " INSERT INTO [products] ([pole], [bit]) VALUES (N'hodnota1', 1) , (N'hodnota2', 1) , (N'hodnota3', 1) " ,
" INSERT INTO [products] ([pole], [bit]) VALUES ('hodnota1', 1) , ('hodnota2', 1) , ('hodnota3', 1) " ,
]),
2015-06-19 03:11:36 +02:00
$conn -> translate ( 'INSERT INTO [products] %ex' , $array )
2015-01-12 05:33:41 +01:00
);
2018-05-10 21:45:11 +02:00
// Dibi detects UPDATE command
2015-01-12 05:33:41 +01:00
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " UPDATE [colors] SET [color]=N'blue', [order]=12 WHERE [id]=123 " ,
" UPDATE [colors] SET [color]='blue', [order]=12 WHERE [id]=123 " ,
]),
2015-10-06 01:39:01 +02:00
$conn -> translate ( 'UPDATE [colors] SET' , [
2017-09-21 14:17:14 +02:00
'color' => 'blue' ,
'order' => 12 ,
], 'WHERE [id]=%i' , 123 )
);
2015-01-12 05:33:41 +01:00
// IN array
2015-10-06 01:39:01 +02:00
$array = [ 1 , 2 , 3 ];
2015-01-12 05:33:41 +01:00
Assert :: same (
reformat ( 'SELECT * FROM [people] WHERE [id] IN ( 1, 2, 3 )' ),
2015-06-19 03:11:36 +02:00
$conn -> translate ( 'SELECT * FROM [people] WHERE [id] IN (' , $array , ')' )
2015-01-12 05:33:41 +01:00
);
// long numbers
Assert :: same (
reformat ( 'SELECT -123456789123456789123456789' ),
2015-06-19 03:11:36 +02:00
$conn -> translate ( 'SELECT %i' , '-123456789123456789123456789' )
2015-01-12 05:33:41 +01:00
);
// long float numbers
Assert :: same (
reformat ( 'SELECT -.12345678912345678912345678e10' ),
2015-06-19 03:11:36 +02:00
$conn -> translate ( 'SELECT %f' , '-.12345678912345678912345678e10' )
2015-01-12 05:33:41 +01:00
);
// invalid input
2015-06-19 03:11:36 +02:00
$e = Assert :: exception ( function () use ( $conn ) {
2015-10-06 01:39:01 +02:00
$conn -> translate ( 'SELECT %s' , ( object ) [ 123 ], ', %m' , 123 );
2017-06-09 12:21:23 +02:00
}, Dibi\Exception :: class , 'SQL translate error: Invalid combination of type stdClass and modifier %s' );
2015-11-02 16:47:35 +01:00
Assert :: same ( 'SELECT **Invalid combination of type stdClass and modifier %s** , **Unknown or unexpected modifier %m**' , $e -> getSql ());
2015-01-12 05:33:41 +01:00
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " SELECT * FROM [table] WHERE id=10 AND name=N'ahoj' " ,
" SELECT * FROM [table] WHERE id=10 AND name='ahoj' " ,
]),
2015-01-12 05:33:41 +01:00
$conn -> translate ( 'SELECT * FROM [table] WHERE id=%i AND name=%s' , 10 , 'ahoj' )
);
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " TEST ([cond] > 2) OR ([cond2] = N'3') OR (cond3 < RAND()) " ,
" TEST ([cond] > 2) OR ([cond2] = '3') OR (cond3 < RAND()) " ,
]),
2015-10-06 01:39:01 +02:00
$conn -> translate ( 'TEST %or' , [ '[cond] > 2' , '[cond2] = "3"' , 'cond3 < RAND()' ])
2015-01-12 05:33:41 +01:00
);
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " TEST ([cond] > 2) AND ([cond2] = N'3') AND (cond3 < RAND()) " ,
" TEST ([cond] > 2) AND ([cond2] = '3') AND (cond3 < RAND()) " ,
]),
2015-10-06 01:39:01 +02:00
$conn -> translate ( 'TEST %and' , [ '[cond] > 2' , '[cond2] = "3"' , 'cond3 < RAND()' ])
2015-01-12 05:33:41 +01:00
);
2017-07-11 12:29:13 +02:00
2015-10-06 01:39:01 +02:00
$where = [];
2015-01-12 05:33:41 +01:00
$where [] = '[age] > 20' ;
$where [] = '[email] IS NOT NULL' ;
Assert :: same (
reformat ( 'SELECT * FROM [table] WHERE ([age] > 20) AND ([email] IS NOT NULL)' ),
$conn -> translate ( 'SELECT * FROM [table] WHERE %and' , $where )
);
2015-10-06 01:39:01 +02:00
$where = [];
2017-07-11 12:28:13 +02:00
$where [ 'age' ] = null ;
2015-01-12 05:33:41 +01:00
$where [ 'email' ] = 'ahoj' ;
2015-10-06 01:39:01 +02:00
$where [ 'id%l' ] = [ 10 , 20 , 30 ];
2015-01-12 05:33:41 +01:00
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " SELECT * FROM [table] WHERE ([age] IS NULL) AND ([email] = N'ahoj') AND ([id] IN (10, 20, 30)) " ,
" SELECT * FROM [table] WHERE ([age] IS NULL) AND ([email] = 'ahoj') AND ([id] IN (10, 20, 30)) " ,
]),
2015-01-12 05:33:41 +01:00
$conn -> translate ( 'SELECT * FROM [table] WHERE %and' , $where )
);
2015-10-06 01:39:01 +02:00
$where = [];
2015-01-12 05:33:41 +01:00
Assert :: same (
reformat ( 'SELECT * FROM [table] WHERE 1=1' ),
$conn -> translate ( 'SELECT * FROM [table] WHERE %and' , $where )
);
// ORDER BY array
2015-10-06 01:39:01 +02:00
$order = [
2015-01-12 05:33:41 +01:00
'field1' => 'asc' ,
'field2' => 'desc' ,
'field3' => 1 ,
'field4' => - 1 ,
2017-07-11 12:28:13 +02:00
'field5' => true ,
'field6' => false ,
2015-10-06 01:39:01 +02:00
];
2015-01-12 05:33:41 +01:00
Assert :: same (
2015-06-19 03:11:36 +02:00
reformat ( 'SELECT * FROM [people] ORDER BY [field1] ASC, [field2] DESC, [field3] ASC, [field4] DESC, [field5] ASC, [field6] DESC' ),
$conn -> translate ( 'SELECT * FROM [people] ORDER BY %by' , $order )
2015-01-12 05:33:41 +01:00
);
// with limit = 2
Assert :: same (
2015-10-06 01:39:01 +02:00
reformat ([
2019-10-22 19:26:24 +02:00
'odbc' => 'SELECT TOP 2 * FROM (SELECT * FROM [products]) t' ,
2016-01-26 09:39:34 +01:00
'sqlsrv' => 'SELECT * FROM [products] OFFSET 0 ROWS FETCH NEXT 2 ROWS ONLY' ,
2019-10-22 19:26:24 +02:00
'SELECT * FROM [products] LIMIT 2' ,
2015-10-06 01:39:01 +02:00
]),
2015-01-12 05:33:41 +01:00
$conn -> translate ( 'SELECT * FROM [products] %lmt' , 2 )
);
if ( $config [ 'system' ] === 'odbc' ) {
2015-06-19 03:11:36 +02:00
Assert :: exception ( function () use ( $conn ) {
2015-01-12 05:33:41 +01:00
$conn -> translate ( 'SELECT * FROM [products] %lmt %ofs' , 2 , 1 );
2017-06-09 12:21:23 +02:00
}, Dibi\Exception :: class );
2015-01-12 05:33:41 +01:00
} else {
// with limit = 2, offset = 1
Assert :: same (
2016-01-26 09:39:34 +01:00
reformat ([
'sqlsrv' => 'SELECT * FROM [products] OFFSET 1 ROWS FETCH NEXT 2 ROWS ONLY' ,
2019-10-22 19:26:24 +02:00
'SELECT * FROM [products] LIMIT 2 OFFSET 1' ,
2016-01-26 09:39:34 +01:00
]),
2015-01-12 05:33:41 +01:00
$conn -> translate ( 'SELECT * FROM [products] %lmt %ofs' , 2 , 1 )
);
// with offset = 50
Assert :: same (
2015-10-06 01:39:01 +02:00
reformat ([
2019-10-22 19:26:24 +02:00
'mysql' => 'SELECT * FROM `products` LIMIT 18446744073709551615 OFFSET 50' ,
'postgre' => 'SELECT * FROM "products" OFFSET 50' ,
2016-01-26 09:39:34 +01:00
'sqlsrv' => 'SELECT * FROM [products] OFFSET 50 ROWS' ,
2019-10-22 19:26:24 +02:00
'SELECT * FROM [products] LIMIT -1 OFFSET 50' ,
2015-10-06 01:39:01 +02:00
]),
2015-01-12 05:33:41 +01:00
$conn -> translate ( 'SELECT * FROM [products] %ofs' , 50 )
);
}
Assert :: same (
2015-10-06 01:39:01 +02:00
reformat ([
2017-02-06 10:39:34 +01:00
'odbc' => 'INSERT INTO test ([a2], [a4], [b1], [b2], [b3], [b4], [b5], [b6], [b7], [b8], [b9], [c1]) VALUES (#09/26/1212 00:00:00.000000#, #12/31/1969 22:13:20.000000#, #09/26/1212#, #09/26/1212 00:00:00.000000#, #12/31/1969#, #12/31/1969 22:13:20.000000#, #09/26/1212 00:00:00.000000#, #09/26/1212#, #09/26/1212 00:00:00.000000#, NULL, NULL, #09/26/1212 16:51:34.012400#)' ,
2017-07-21 23:03:07 +02:00
'sqlsrv' => " INSERT INTO test ([a2], [a4], [b1], [b2], [b3], [b4], [b5], [b6], [b7], [b8], [b9], [c1]) VALUES (CONVERT(DATETIME2(7), '1212-09-26 00:00:00.000000'), CONVERT(DATETIME2(7), '1969-12-31 22:13:20.000000'), '1212-09-26', CONVERT(DATETIME2(7), '1212-09-26 00:00:00.000000'), '1969-12-31', CONVERT(DATETIME2(7), '1969-12-31 22:13:20.000000'), CONVERT(DATETIME2(7), '1212-09-26 00:00:00.000000'), '1212-09-26', CONVERT(DATETIME2(7), '1212-09-26 00:00:00.000000'), NULL, NULL, CONVERT(DATETIME2(7), '1212-09-26 16:51:34.012400')) " ,
2017-02-06 10:39:34 +01:00
" INSERT INTO test ([a2], [a4], [b1], [b2], [b3], [b4], [b5], [b6], [b7], [b8], [b9], [c1]) VALUES ('1212-09-26 00:00:00.000000', '1969-12-31 22:13:20.000000', '1212-09-26', '1212-09-26 00:00:00.000000', '1969-12-31', '1969-12-31 22:13:20.000000', '1212-09-26 00:00:00.000000', '1212-09-26', '1212-09-26 00:00:00.000000', NULL, NULL, '1212-09-26 16:51:34.012400') " ,
2015-10-06 01:39:01 +02:00
]),
$conn -> translate ( 'INSERT INTO test' , [
2017-09-21 14:17:14 +02:00
'a2' => new DateTime ( '1212-09-26' ),
'a4' => new DateTime ( - 10000 ),
'b1%d' => '1212-09-26' ,
'b2%t' => '1212-09-26' ,
'b3%d' => - 10000 ,
'b4%t' => - 10000 ,
'b5' => new DateTime ( '1212-09-26' ),
'b6%d' => new DateTime ( '1212-09-26' ),
'b7%t' => new DateTime ( '1212-09-26' ),
'b8%d' => null ,
'b9%t' => null ,
'c1%t' => new DateTime ( '1212-09-26 16:51:34.0124' ),
])
);
2015-01-12 05:33:41 +01:00
2015-11-02 16:57:02 +01:00
Assert :: exception ( function () use ( $conn ) {
$conn -> translate ( 'SELECT %s' , new DateTime ( '1212-09-26' ));
2017-06-09 12:21:23 +02:00
}, Dibi\Exception :: class , 'SQL translate error: Invalid combination of type Dibi\DateTime and modifier %s' );
2015-11-02 16:57:02 +01:00
2015-01-12 05:33:41 +01:00
// like
2015-10-06 01:39:01 +02:00
$args = [
2015-06-19 03:11:36 +02:00
'SELECT * FROM products WHERE (title LIKE %like~ AND title LIKE %~like) OR title LIKE %~like~' ,
2015-01-22 17:21:44 +01:00
'C' ,
'r' ,
2015-06-19 03:11:36 +02:00
" a \n %_ \\ ' \" " ,
2015-10-06 01:39:01 +02:00
];
2015-01-22 17:21:44 +01:00
2015-10-13 17:37:14 +02:00
if ( $config [ 'system' ] === 'postgre' ) {
2015-01-22 17:21:44 +01:00
$conn -> query ( 'SET escape_string_warning = off' ); // do not log warnings
$conn -> query ( 'SET standard_conforming_strings = off' );
Assert :: same (
" SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a \n \\ \\ % \\ \\ _ \\ \\ \\ \\ '' \" %' " ,
$conn -> translate ( $args [ 0 ], $args [ 1 ], $args [ 2 ], $args [ 3 ])
);
$conn -> query ( 'SET standard_conforming_strings = on' );
Assert :: same (
" SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a \n \\ % \\ _ \\ \\ '' \" %' " ,
$conn -> translate ( $args [ 0 ], $args [ 1 ], $args [ 2 ], $args [ 3 ])
);
} elseif ( $config [ 'driver' ] !== 'sqlite' ) { // sqlite2
2015-01-12 05:33:41 +01:00
Assert :: same (
2015-10-06 01:39:01 +02:00
reformat ([
2015-01-12 05:33:41 +01:00
'sqlite' => " SELECT * FROM products WHERE (title LIKE 'C%' ESCAPE ' \\ ' AND title LIKE '%r' ESCAPE ' \\ ') OR title LIKE '%a \n \\ % \\ _ \\ \\ '' \" %' ESCAPE ' \\ ' " ,
'odbc' => " SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a \n [%][_] \\ '' \" %' " ,
2016-01-26 09:39:34 +01:00
'sqlsrv' => " SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a \n [%][_] \\ '' \" %' " ,
2020-10-28 16:38:16 +01:00
" SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a \\ n \\ % \\ _ \\ \\ \\ \\ \\ ' \" %' " ,
2015-10-06 01:39:01 +02:00
]),
2015-01-22 17:21:44 +01:00
$conn -> translate ( $args [ 0 ], $args [ 1 ], $args [ 2 ], $args [ 3 ])
2015-01-12 05:33:41 +01:00
);
}
2015-06-19 03:11:36 +02:00
$e = Assert :: exception ( function () use ( $conn ) {
2015-01-12 05:33:41 +01:00
$conn -> translate ( " SELECT ' " );
2017-06-09 12:21:23 +02:00
}, Dibi\Exception :: class , 'SQL translate error: Alone quote' );
2015-01-12 05:33:41 +01:00
Assert :: same ( 'SELECT **Alone quote**' , $e -> getSql ());
Assert :: match (
2015-10-06 01:39:01 +02:00
reformat ([
2015-01-12 05:33:41 +01:00
'mysql' => " SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT
CONCAT ( last_name , ', ' , first_name ) AS full_name
GROUP BY `user`
HAVING MAX ( salary ) > % i 123
2020-10-28 16:38:16 +01:00
INTO OUTFILE '/tmp/result\\' . txt '
2015-01-12 05:33:41 +01:00
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\\\"'
LINES TERMINATED BY '\\\\n'
" ,
2019-07-12 14:33:38 +02:00
'sqlsrv' => " SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT
CONCAT ( last_name , N ', ' , first_name ) AS full_name
GROUP BY [ user ]
HAVING MAX ( salary ) > % i 123
INTO OUTFILE N '/tmp/result' '.txt'
FIELDS TERMINATED BY N ',' OPTIONALLY ENCLOSED BY N '\"'
LINES TERMINATED BY N '\\n' " , " SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT
2015-01-12 05:33:41 +01:00
CONCAT ( last_name , ', ' , first_name ) AS full_name
GROUP BY [ user ]
HAVING MAX ( salary ) > % i 123
INTO OUTFILE '/tmp/result' '.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\\n'
2015-06-19 03:11:36 +02:00
" ,
2015-10-06 01:39:01 +02:00
]),
2015-06-19 03:11:36 +02:00
$conn -> translate ( '%sql' , ' SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT
CONCAT ( last_name , " , " , first_name ) AS full_name
2015-01-12 05:33:41 +01:00
GROUP BY [ user ]
2015-06-19 03:11:36 +02:00
HAVING MAX ( salary ) > % i ' , 123 , "
2015-01-12 05:33:41 +01:00
INTO OUTFILE '/tmp/result' '.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\\n'
" )
);
2015-10-06 01:39:01 +02:00
$array1 = [ 1 , 2 , 3 ];
$array2 = [ 'one' , 'two' , 'three' ];
$array3 = [
2015-01-12 05:33:41 +01:00
'col1' => 'one' ,
'col2' => 'two' ,
2016-07-20 15:33:04 +02:00
'col3' => 'thr.ee' ,
2015-10-06 01:39:01 +02:00
];
$array4 = [
2015-06-19 03:11:36 +02:00
'a' => 12 ,
2017-07-11 12:28:13 +02:00
'b' => null ,
2015-10-08 02:13:22 +02:00
'c' => new DateTime ( '12.3.2007' ),
2015-06-19 03:11:36 +02:00
'd' => 'any string' ,
2015-10-06 01:39:01 +02:00
];
2015-01-12 05:33:41 +01:00
2015-10-06 01:39:01 +02:00
$array5 = [ 'RAND()' , '[col1] > [col2]' ];
2015-01-12 05:33:41 +01:00
Assert :: match (
2015-10-06 01:39:01 +02:00
reformat ([
2015-01-12 05:33:41 +01:00
'mysql' => " SELECT *
FROM `db` . `table`
WHERE ( `test` . `a` LIKE '1995-03-01'
OR `b1` IN ( 1 , 2 , 3 )
OR `b2` IN ( '1' , '2' , '3' )
OR `b3` IN ( )
OR `b4` IN ( 'one' , 'two' , 'three' )
2016-07-20 15:33:04 +02:00
OR `b5` IN ( `col1` AS `one` , `col2` AS `two` , `col3` AS `thr.ee` )
OR `b6` IN ( 'one' , 'two' , 'thr.ee' )
2015-01-12 05:33:41 +01:00
OR `b7` IN ( NULL )
OR `b8` IN ( RAND () `col1` > `col2` )
2015-11-02 15:10:13 +01:00
OR `b9` IN ( RAND (), [ col1 ] > [ col2 ] )
OR `b10` IN ( )
2020-10-28 16:38:16 +01:00
AND `c` = 'embedded \\' string '
2015-01-12 05:33:41 +01:00
OR `d` = 10
OR `e` = NULL
OR `true` = 1
OR `false` = 0
OR `str_null` = NULL
OR `str_not_null` = 'hello'
2019-07-12 14:33:38 +02:00
LIMIT 10 " ,
'sqlsrv' => " SELECT *
FROM [ db ] . [ table ]
WHERE ([ test ] . [ a ] LIKE '1995-03-01'
OR [ b1 ] IN ( 1 , 2 , 3 )
OR [ b2 ] IN ( N '1' , N '2' , N '3' )
OR [ b3 ] IN ( )
OR [ b4 ] IN ( N 'one' , N 'two' , N 'three' )
OR [ b5 ] IN ([ col1 ] AS [ one ], [ col2 ] AS [ two ], [ col3 ] AS [ thr . ee ] )
OR [ b6 ] IN ( N 'one' , N 'two' , N 'thr.ee' )
OR [ b7 ] IN ( NULL )
OR [ b8 ] IN ( RAND () [ col1 ] > [ col2 ] )
OR [ b9 ] IN ( RAND (), [ col1 ] > [ col2 ] )
OR [ b10 ] IN ( )
AND [ c ] = N 'embedded ' ' string'
OR [ d ] = 10
OR [ e ] = NULL
OR [ true ] = 1
OR [ false ] = 0
OR [ str_null ] = NULL
OR [ str_not_null ] = N 'hello'
2015-01-12 05:33:41 +01:00
LIMIT 10 " ,
2015-10-13 17:37:14 +02:00
'postgre' => ' SELECT *
2015-01-12 05:33:41 +01:00
FROM " db " . " table "
WHERE ( " test " . " a " LIKE \ ' 1995 - 03 - 01 \ '
OR " b1 " IN ( 1 , 2 , 3 )
OR " b2 " IN ( \ ' 1 \ ' , \ ' 2 \ ' , \ ' 3 \ ' )
OR " b3 " IN ( )
OR " b4 " IN ( \ ' one\ ' , \ ' two\ ' , \ ' three\ ' )
2016-07-20 15:33:04 +02:00
OR " b5 " IN ( " col1 " AS " one " , " col2 " AS " two " , " col3 " AS " thr.ee " )
OR " b6 " IN ( \ ' one\ ' , \ ' two\ ' , \ ' thr . ee\ ' )
2015-01-12 05:33:41 +01:00
OR " b7 " IN ( NULL )
OR " b8 " IN ( RAND () " col1 " > " col2 " )
2015-11-02 15:10:13 +01:00
OR " b9 " IN ( RAND (), [ col1 ] > [ col2 ] )
OR " b10 " IN ( )
2015-01-12 05:33:41 +01:00
AND " c " = \ ' embedded \ ' \ ' string\ '
OR " d " = 10
OR " e " = NULL
OR " true " = TRUE
OR " false " = FALSE
OR " str_null " = NULL
OR " str_not_null " = \ ' hello\ '
LIMIT 10 ' ,
'odbc' => " SELECT *
FROM [ db ] . [ table ]
WHERE ([ test ] . [ a ] LIKE #03/01/1995#
OR [ b1 ] IN ( 1 , 2 , 3 )
OR [ b2 ] IN ( '1' , '2' , '3' )
OR [ b3 ] IN ( )
OR [ b4 ] IN ( 'one' , 'two' , 'three' )
2016-07-20 15:33:04 +02:00
OR [ b5 ] IN ([ col1 ] AS [ one ], [ col2 ] AS [ two ], [ col3 ] AS [ thr . ee ] )
OR [ b6 ] IN ( 'one' , 'two' , 'thr.ee' )
2015-01-12 05:33:41 +01:00
OR [ b7 ] IN ( NULL )
OR [ b8 ] IN ( RAND () [ col1 ] > [ col2 ] )
2015-11-02 15:10:13 +01:00
OR [ b9 ] IN ( RAND (), [ col1 ] > [ col2 ] )
OR [ b10 ] IN ( )
2015-01-12 05:33:41 +01:00
AND [ c ] = 'embedded ' ' string'
OR [ d ] = 10
OR [ e ] = NULL
OR [ true ] = 1
OR [ false ] = 0
OR [ str_null ] = NULL
OR [ str_not_null ] = 'hello'
LIMIT 10 " ,
" SELECT *
FROM [ db ] . [ table ]
WHERE ([ test ] . [ a ] LIKE '1995-03-01'
OR [ b1 ] IN ( 1 , 2 , 3 )
OR [ b2 ] IN ( '1' , '2' , '3' )
OR [ b3 ] IN ( )
OR [ b4 ] IN ( 'one' , 'two' , 'three' )
2016-07-20 15:33:04 +02:00
OR [ b5 ] IN ([ col1 ] AS [ one ], [ col2 ] AS [ two ], [ col3 ] AS [ thr . ee ] )
OR [ b6 ] IN ( 'one' , 'two' , 'thr.ee' )
2015-01-12 05:33:41 +01:00
OR [ b7 ] IN ( NULL )
OR [ b8 ] IN ( RAND () [ col1 ] > [ col2 ] )
2015-11-02 15:10:13 +01:00
OR [ b9 ] IN ( RAND (), [ col1 ] > [ col2 ] )
OR [ b10 ] IN ( )
2015-01-12 05:33:41 +01:00
AND [ c ] = 'embedded ' ' string'
OR [ d ] = 10
OR [ e ] = NULL
OR [ true ] = 1
OR [ false ] = 0
OR [ str_null ] = NULL
OR [ str_not_null ] = 'hello'
LIMIT 10 " ,
2015-10-06 01:39:01 +02:00
]),
2015-06-19 03:11:36 +02:00
$conn -> translate ( ' SELECT *
2015-01-12 05:33:41 +01:00
FROM [ db . table ]
2015-06-19 03:11:36 +02:00
WHERE ([ test . a ] LIKE % d ', ' 1995 - 03 - 01 ', '
OR [ b1 ] IN ( ', $array1, ' )
OR [ b2 ] IN ( % s ', $array1, ' )
2015-10-06 01:39:01 +02:00
OR [ b3 ] IN ( % s ', [], ' )
2015-06-19 03:11:36 +02:00
OR [ b4 ] IN ( ', $array2, ' )
OR [ b5 ] IN ( % n ', $array3, ' )
OR [ b6 ] IN % l ', $array3, '
2015-10-06 01:39:01 +02:00
OR [ b7 ] IN % in ', [], '
2015-06-19 03:11:36 +02:00
OR [ b8 ] IN ( % sql ', $array5, ' )
2015-11-02 15:10:13 +01:00
OR [ b9 ] IN ( % SQL ', $array5, ' )
OR [ b10 ] IN ( ' , [], " )
2015-01-12 05:33:41 +01:00
AND [ c ] = 'embedded ' ' string'
2015-06-19 03:11:36 +02:00
OR [ d ] =% i " , 10.3, '
2017-07-11 12:28:13 +02:00
OR [ e ] =% i ', null, '
OR [ true ] = ', true, '
OR [ false ] = ', false, '
2015-06-19 03:11:36 +02:00
OR [ str_null ] =% sn ', ' ', '
OR [ str_not_null ] =% sn ', ' hello ', '
LIMIT 10 ' )
2015-01-12 05:33:41 +01:00
);
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " TEST [cond] > 2 [cond2] = N'3' cond3 < RAND() 123 " ,
" TEST [cond] > 2 [cond2] = '3' cond3 < RAND() 123 " ,
]),
2015-10-06 01:39:01 +02:00
$conn -> translate ( 'TEST %ex' , [ '[cond] > 2' , '[cond2] = "3"' , 'cond3 < RAND()' ], 123 )
2015-01-12 05:33:41 +01:00
);
Assert :: same (
reformat ( 'TEST ([cond] > 2) OR ([cond2] > 3) OR ([cond3] = 10 + 1)' ),
2015-10-06 01:39:01 +02:00
$conn -> translate ( 'TEST %or' , [ '`cond` > 2' , [ '[cond2] > %i' , '3' ], 'cond3%sql' => [ '10 + 1' ]])
2015-01-12 05:33:41 +01:00
);
Assert :: same (
reformat ( 'TEST ([cond] = 2) OR ([cond3] = RAND())' ),
2015-10-06 01:39:01 +02:00
$conn -> translate ( 'TEST %or' , [ 'cond' => 2 , 'cond3%sql' => 'RAND()' ])
2015-01-12 05:33:41 +01:00
);
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " TEST ([cond1] 3) OR ([cond2] RAND()) OR ([cond3] LIKE N'string') " ,
" TEST ([cond1] 3) OR ([cond2] RAND()) OR ([cond3] LIKE 'string') " ,
]),
2015-10-06 01:39:01 +02:00
$conn -> translate ( 'TEST %or' , [ 'cond1%ex' => 3 , 'cond2%ex' => 'RAND()' , 'cond3%ex' => [ 'LIKE %s' , 'string' ]])
2015-01-12 05:33:41 +01:00
);
Assert :: same (
2015-10-06 01:39:01 +02:00
reformat ([
2019-10-22 19:26:24 +02:00
'odbc' => 'SELECT TOP 10 * FROM (SELECT * FROM [test] WHERE [id] LIKE \'%d%t\') t' ,
2019-07-12 14:33:38 +02:00
'sqlsrv' => 'SELECT * FROM [test] WHERE [id] LIKE N\'%d%t\' OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY' ,
2019-10-22 19:26:24 +02:00
'SELECT * FROM [test] WHERE [id] LIKE \'%d%t\' LIMIT 10' ,
2015-10-06 01:39:01 +02:00
]),
2015-01-12 05:33:41 +01:00
$conn -> translate ( " SELECT * FROM [test] WHERE %n LIKE '%d%t' %lmt " , 'id' , 10 )
);
2015-10-06 01:39:01 +02:00
$where = [
2017-09-21 14:17:14 +02:00
'tablename.column' => 1 ,
2015-10-06 01:39:01 +02:00
];
2015-01-12 05:33:41 +01:00
Assert :: same (
reformat ( 'SELECT * FROM [tablename] WHERE ([tablename].[column] = 1)' ),
$conn -> translate ( 'SELECT * FROM [tablename] WHERE %and' , $where )
);
Assert :: same (
2019-10-22 19:26:24 +02:00
reformat ( 'SELECT FROM ...' ),
2017-07-11 12:28:13 +02:00
$conn -> translate ( 'SELECT FROM ... %lmt' , null )
2015-01-12 05:33:41 +01:00
);
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " SELECT N'%i' " ,
" SELECT '%i' " ,
]),
2015-01-12 05:33:41 +01:00
$conn -> translate ( " SELECT '%i' " )
);
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " SELECT N'%i' " ,
" SELECT '%i' " ,
]),
2015-01-12 05:33:41 +01:00
$conn -> translate ( 'SELECT "%i"' )
);
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " INSERT INTO [products] ([product_id], [title]) VALUES (1, SHA1(N'Test product')) , (1, SHA1(N'Test product')) " ,
" INSERT INTO [products] ([product_id], [title]) VALUES (1, SHA1('Test product')) , (1, SHA1('Test product')) " ,
]),
2015-10-06 01:39:01 +02:00
$conn -> translate ( 'INSERT INTO [products]' , [
2017-09-21 14:17:14 +02:00
'product_id' => 1 ,
2017-09-21 15:04:53 +02:00
'title' => new Dibi\Expression ( 'SHA1(%s)' , 'Test product' ),
2017-09-21 14:17:14 +02:00
], [
'product_id' => 1 ,
2017-09-21 14:51:31 +02:00
'title' => new Dibi\Expression ( 'SHA1(%s)' , 'Test product' ),
2017-09-21 14:17:14 +02:00
])
2015-01-12 05:33:41 +01:00
);
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " UPDATE [products] [product_id]=1, [title]=SHA1(N'Test product') " ,
" UPDATE [products] [product_id]=1, [title]=SHA1('Test product') " ,
]),
2015-10-06 01:39:01 +02:00
$conn -> translate ( 'UPDATE [products]' , [
2017-09-21 14:17:14 +02:00
'product_id' => 1 ,
2017-09-21 15:04:53 +02:00
'title' => new Dibi\Expression ( 'SHA1(%s)' , 'Test product' ),
2017-09-21 14:17:14 +02:00
])
2015-01-12 05:33:41 +01:00
);
2017-09-21 14:51:31 +02:00
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " UPDATE [products] [product_id]=1, [title]=SHA1(N'Test product') " ,
" UPDATE [products] [product_id]=1, [title]=SHA1('Test product') " ,
]),
2017-09-21 14:51:31 +02:00
$conn -> translate ( 'UPDATE [products]' , [
'product_id' => 1 ,
'title' => new Dibi\Expression ( 'SHA1(%s)' , 'Test product' ),
])
);
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " SELECT * FROM [products] WHERE [product_id]=1, [title]=SHA1(N'Test product') " ,
" SELECT * FROM [products] WHERE [product_id]=1, [title]=SHA1('Test product') " ,
]),
2017-09-21 14:51:31 +02:00
$conn -> translate ( 'SELECT * FROM [products] WHERE' , [
'product_id' => 1 ,
'title' => new Dibi\Expression ( 'SHA1(%s)' , 'Test product' ),
])
);
2015-01-12 05:33:41 +01:00
2018-04-06 02:56:22 +02:00
Assert :: same (
reformat ( 'SELECT * FROM [table] WHERE (([left] = 1) OR ([top] = 2)) AND (number < 100)' ),
$conn -> translate ( 'SELECT * FROM `table` WHERE %and' , [
new Dibi\Expression ( '%or' , [
'left' => 1 ,
'top' => 2 ,
]),
new Dibi\Expression ( 'number < %i' , 100 ),
])
);
2015-06-19 03:11:36 +02:00
$e = Assert :: exception ( function () use ( $conn ) {
2015-10-06 01:39:01 +02:00
$array6 = [
'id' => [ 1 , 2 , 3 , 4 ],
2017-09-21 15:04:53 +02:00
'text' => [ 'ahoj' , 'jak' , 'se' , new Dibi\Expression ( 'SUM(%i)' , '5' )],
2015-10-06 01:39:01 +02:00
'num%i' => [ '1' , '' ],
];
2015-01-12 05:33:41 +01:00
$conn -> translate ( 'INSERT INTO test %m' , $array6 );
2017-06-09 12:21:23 +02:00
}, Dibi\Exception :: class , 'SQL translate error: Multi-insert array "num%i" is different' );
2015-11-02 16:47:35 +01:00
Assert :: same ( 'INSERT INTO test **Multi-insert array "num%i" is different**' , $e -> getSql ());
2015-01-12 05:33:41 +01:00
2015-10-06 01:39:01 +02:00
$array6 = [
'id' => [ 1 , 2 , 3 , 4 ],
2017-09-21 15:04:53 +02:00
'text' => [ 'ahoj' , 'jak' , 'se' , new Dibi\Expression ( 'SUM(%i)' , '5' )],
2017-07-22 00:11:23 +02:00
'num%i' => [ '1' , '-1' , 10.3 , 1 ],
2015-10-06 01:39:01 +02:00
];
2015-01-12 05:33:41 +01:00
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " INSERT INTO test ([id], [text], [num]) VALUES (1, N'ahoj', 1), (2, N'jak', -1), (3, N'se', 10), (4, SUM(5), 1) " ,
" INSERT INTO test ([id], [text], [num]) VALUES (1, 'ahoj', 1), (2, 'jak', -1), (3, 'se', 10), (4, SUM(5), 1) " ,
]),
2015-01-12 05:33:41 +01:00
$conn -> translate ( 'INSERT INTO test %m' , $array6 )
);
2015-10-06 01:39:01 +02:00
$by = [
[ 'funkce(nazev_pole) ASC' ],
2015-06-19 03:11:36 +02:00
'jine_pole' => 'DESC' ,
2015-10-06 01:39:01 +02:00
];
2015-01-12 05:33:41 +01:00
Assert :: same (
reformat ( 'SELECT * FROM table ORDER BY funkce(nazev_pole) ASC, [jine_pole] DESC' ),
2015-06-19 03:11:36 +02:00
$conn -> translate ( 'SELECT * FROM table ORDER BY %by' , $by )
2015-01-12 05:33:41 +01:00
);
Assert :: same (
reformat ( 'INSERT INTO [test].*' ),
$conn -> translate ( 'INSERT INTO [test.*]' )
);
2017-07-22 00:11:23 +02:00
Assert :: exception ( function () use ( $conn ) {
$conn -> translate ( 'INSERT INTO %i' , 'ahoj' );
}, Dibi\Exception :: class , " Expected number, 'ahoj' given. " );
Assert :: exception ( function () use ( $conn ) {
$conn -> translate ( 'INSERT INTO %f' , 'ahoj' );
}, Dibi\Exception :: class , " Expected number, 'ahoj' given. " );
2015-01-12 05:33:41 +01:00
2015-11-02 15:10:13 +01:00
Assert :: same (
reformat ( 'SELECT * FROM table' ),
$conn -> translate ( 'SELECT' , new Dibi\Literal ( '* FROM table' ))
);
2015-11-02 16:55:14 +01:00
Assert :: same (
reformat ( 'SELECT * FROM table' ),
$conn -> translate ( 'SELECT %SQL' , new Dibi\Literal ( '* FROM table' ))
);
2015-11-02 15:10:13 +01:00
Assert :: same (
reformat ( 'SELECT * FROM table' ),
$conn -> translate ( new Dibi\Literal ( 'SELECT * FROM table' ))
);
2016-07-20 14:49:58 +02:00
Assert :: same (
reformat ( 'SELECT [a].[b] AS [c.d]' ),
$conn -> translate ( 'SELECT %n AS %N' , 'a.b' , 'c.d' )
);
2017-07-11 12:29:13 +02:00
setlocale ( LC_ALL , 'czech' );
2015-01-12 05:33:41 +01:00
Assert :: same (
2019-07-12 14:33:38 +02:00
reformat ([
'sqlsrv' => " UPDATE [colors] SET [color]=N'blue', [price]=-12.4, [spec]=-9E-005, [spec2]=1000, [spec3]=10000, [spec4]=10000 WHERE [price]=123.5 " ,
" UPDATE [colors] SET [color]='blue', [price]=-12.4, [spec]=-9E-005, [spec2]=1000, [spec3]=10000, [spec4]=10000 WHERE [price]=123.5 " ,
]),
2015-10-06 01:39:01 +02:00
$conn -> translate ( 'UPDATE [colors] SET' , [
2017-09-21 14:17:14 +02:00
'color' => 'blue' ,
'price' => - 12.4 ,
'spec%f' => '-9E-005' ,
'spec2%f' => 1000.00 ,
'spec3%i' => 10000 ,
'spec4' => 10000 ,
], 'WHERE [price]=%f' , 123.5 )
2015-01-12 05:33:41 +01:00
);