mirror of
https://github.com/dg/dibi.git
synced 2025-08-29 08:49:50 +02:00
used PHP 5.4 syntax
This commit is contained in:
@@ -8,24 +8,24 @@ use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
$conn = new DibiConnection($config + array('formatDateTime' => "'Y-m-d H:i:s'", 'formatDate' => "'Y-m-d'"));
|
||||
$conn = new DibiConnection($config + ['formatDateTime' => "'Y-m-d H:i:s'", 'formatDate' => "'Y-m-d'"]);
|
||||
|
||||
|
||||
// dibi detects INSERT or REPLACE command & booleans
|
||||
Assert::same(
|
||||
reformat("REPLACE INTO [products] ([title], [price]) VALUES ('Drticka', 318)"),
|
||||
$conn->translate('REPLACE INTO [products]', array(
|
||||
$conn->translate('REPLACE INTO [products]', [
|
||||
'title' => 'Drticka',
|
||||
'price' => 318,
|
||||
)));
|
||||
]));
|
||||
|
||||
|
||||
// multiple INSERT command
|
||||
$array = array(
|
||||
$array = [
|
||||
'title' => 'Super Product',
|
||||
'price' => 12,
|
||||
'brand' => NULL,
|
||||
);
|
||||
];
|
||||
Assert::same(
|
||||
reformat('INSERT INTO [products] ([title], [price], [brand]) VALUES (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL)'),
|
||||
$conn->translate('INSERT INTO [products]', $array, $array, $array)
|
||||
@@ -33,11 +33,11 @@ Assert::same(
|
||||
|
||||
|
||||
// multiple INSERT command II
|
||||
$array = array(
|
||||
array('pole' => 'hodnota1', 'bit' => 1),
|
||||
array('pole' => 'hodnota2', 'bit' => 1),
|
||||
array('pole' => 'hodnota3', 'bit' => 1),
|
||||
);
|
||||
$array = [
|
||||
['pole' => 'hodnota1', 'bit' => 1],
|
||||
['pole' => 'hodnota2', 'bit' => 1],
|
||||
['pole' => 'hodnota3', 'bit' => 1],
|
||||
];
|
||||
Assert::same(
|
||||
reformat('INSERT INTO [products] ([pole], [bit]) VALUES (\'hodnota1\', 1) , (\'hodnota2\', 1) , (\'hodnota3\', 1)'),
|
||||
$conn->translate('INSERT INTO [products] %ex', $array)
|
||||
@@ -47,14 +47,14 @@ Assert::same(
|
||||
// dibi detects UPDATE command
|
||||
Assert::same(
|
||||
reformat("UPDATE [colors] SET [color]='blue', [order]=12 WHERE [id]=123"),
|
||||
$conn->translate('UPDATE [colors] SET', array(
|
||||
$conn->translate('UPDATE [colors] SET', [
|
||||
'color' => 'blue',
|
||||
'order' => 12,
|
||||
), 'WHERE [id]=%i', 123));
|
||||
], 'WHERE [id]=%i', 123));
|
||||
|
||||
|
||||
// IN array
|
||||
$array = array(1, 2, 3);
|
||||
$array = [1, 2, 3];
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [people] WHERE [id] IN ( 1, 2, 3 )'),
|
||||
$conn->translate('SELECT * FROM [people] WHERE [id] IN (', $array, ')')
|
||||
@@ -75,7 +75,7 @@ Assert::same(
|
||||
|
||||
// invalid input
|
||||
$e = Assert::exception(function () use ($conn) {
|
||||
$conn->translate('SELECT %s', (object) array(123), ', %m', 123);
|
||||
$conn->translate('SELECT %s', (object) [123], ', %m', 123);
|
||||
}, 'DibiException', 'SQL translate error');
|
||||
Assert::same('SELECT **Unexpected type object** , **Unknown or invalid modifier %m**', $e->getSql());
|
||||
|
||||
@@ -86,16 +86,16 @@ Assert::same(
|
||||
|
||||
Assert::same(
|
||||
reformat('TEST ([cond] > 2) OR ([cond2] = \'3\') OR (cond3 < RAND())'),
|
||||
$conn->translate('TEST %or', array('[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()'))
|
||||
$conn->translate('TEST %or', ['[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()'])
|
||||
);
|
||||
|
||||
Assert::same(
|
||||
reformat('TEST ([cond] > 2) AND ([cond2] = \'3\') AND (cond3 < RAND())'),
|
||||
$conn->translate('TEST %and', array('[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()'))
|
||||
$conn->translate('TEST %and', ['[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()'])
|
||||
);
|
||||
|
||||
//
|
||||
$where = array();
|
||||
$where = [];
|
||||
$where[] = '[age] > 20';
|
||||
$where[] = '[email] IS NOT NULL';
|
||||
Assert::same(
|
||||
@@ -104,17 +104,17 @@ Assert::same(
|
||||
);
|
||||
|
||||
|
||||
$where = array();
|
||||
$where = [];
|
||||
$where['age'] = NULL;
|
||||
$where['email'] = 'ahoj';
|
||||
$where['id%l'] = array(10, 20, 30);
|
||||
$where['id%l'] = [10, 20, 30];
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [table] WHERE ([age] IS NULL) AND ([email] = \'ahoj\') AND ([id] IN (10, 20, 30))'),
|
||||
$conn->translate('SELECT * FROM [table] WHERE %and', $where)
|
||||
);
|
||||
|
||||
|
||||
$where = array();
|
||||
$where = [];
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [table] WHERE 1=1'),
|
||||
$conn->translate('SELECT * FROM [table] WHERE %and', $where)
|
||||
@@ -122,14 +122,14 @@ Assert::same(
|
||||
|
||||
|
||||
// ORDER BY array
|
||||
$order = array(
|
||||
$order = [
|
||||
'field1' => 'asc',
|
||||
'field2' => 'desc',
|
||||
'field3' => 1,
|
||||
'field4' => -1,
|
||||
'field5' => TRUE,
|
||||
'field6' => FALSE,
|
||||
);
|
||||
];
|
||||
Assert::same(
|
||||
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)
|
||||
@@ -138,10 +138,10 @@ Assert::same(
|
||||
|
||||
// with limit = 2
|
||||
Assert::same(
|
||||
reformat(array(
|
||||
reformat([
|
||||
'odbc' => 'SELECT TOP 2 * FROM (SELECT * FROM [products] ) t',
|
||||
'SELECT * FROM [products] LIMIT 2',
|
||||
)),
|
||||
]),
|
||||
$conn->translate('SELECT * FROM [products] %lmt', 2)
|
||||
);
|
||||
|
||||
@@ -158,11 +158,11 @@ if ($config['system'] === 'odbc') {
|
||||
|
||||
// with offset = 50
|
||||
Assert::same(
|
||||
reformat(array(
|
||||
reformat([
|
||||
'mysql' => 'SELECT * FROM `products` LIMIT 18446744073709551615 OFFSET 50',
|
||||
'pgsql' => 'SELECT * FROM "products" OFFSET 50',
|
||||
'SELECT * FROM [products] LIMIT -1 OFFSET 50',
|
||||
)),
|
||||
]),
|
||||
$conn->translate('SELECT * FROM [products] %ofs', 50)
|
||||
);
|
||||
}
|
||||
@@ -171,11 +171,11 @@ if ($config['system'] === 'odbc') {
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat(array(
|
||||
reformat([
|
||||
'odbc' => 'INSERT INTO test ([a2], [a4], [b1], [b2], [b3], [b4], [b5], [b6], [b7], [b8], [b9]) VALUES (#09/26/1212 00:00:00#, #12/31/1969 22:13:20#, #09/26/1212#, #09/26/1212 00:00:00#, #12/31/1969#, #12/31/1969 22:13:20#, #09/26/1212 00:00:00#, #09/26/1212#, #09/26/1212 00:00:00#, NULL, NULL)',
|
||||
"INSERT INTO test ([a2], [a4], [b1], [b2], [b3], [b4], [b5], [b6], [b7], [b8], [b9]) VALUES ('1212-09-26 00:00:00', '1969-12-31 22:13:20', '1212-09-26', '1212-09-26 00:00:00', '1969-12-31', '1969-12-31 22:13:20', '1212-09-26 00:00:00', '1212-09-26', '1212-09-26 00:00:00', NULL, NULL)",
|
||||
)),
|
||||
$conn->translate('INSERT INTO test', array(
|
||||
]),
|
||||
$conn->translate('INSERT INTO test', [
|
||||
'a2' => new DibiDateTime('1212-09-26'),
|
||||
'a4' => new DibiDateTime(-10000),
|
||||
'b1%d' => '1212-09-26',
|
||||
@@ -187,17 +187,17 @@ Assert::same(
|
||||
'b7%t' => new DateTime('1212-09-26'),
|
||||
'b8%d' => NULL,
|
||||
'b9%t' => NULL,
|
||||
)));
|
||||
]));
|
||||
|
||||
|
||||
|
||||
// like
|
||||
$args = array(
|
||||
$args = [
|
||||
'SELECT * FROM products WHERE (title LIKE %like~ AND title LIKE %~like) OR title LIKE %~like~',
|
||||
'C',
|
||||
'r',
|
||||
"a\n%_\\'\"",
|
||||
);
|
||||
];
|
||||
|
||||
if ($config['system'] === 'pgsql') {
|
||||
$conn->query('SET escape_string_warning = off'); // do not log warnings
|
||||
@@ -215,11 +215,11 @@ if ($config['system'] === 'pgsql') {
|
||||
);
|
||||
} elseif ($config['driver'] !== 'sqlite') { // sqlite2
|
||||
Assert::same(
|
||||
reformat(array(
|
||||
reformat([
|
||||
'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[%][_]\\''\"%'",
|
||||
"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])
|
||||
);
|
||||
}
|
||||
@@ -231,7 +231,7 @@ $e = Assert::exception(function () use ($conn) {
|
||||
Assert::same('SELECT **Alone quote**', $e->getSql());
|
||||
|
||||
Assert::match(
|
||||
reformat(array(
|
||||
reformat([
|
||||
'mysql' => "SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT
|
||||
CONCAT(last_name, ', ', first_name) AS full_name
|
||||
GROUP BY `user`
|
||||
@@ -248,7 +248,7 @@ INTO OUTFILE '/tmp/result''.txt'
|
||||
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
|
||||
LINES TERMINATED BY '\\n'
|
||||
",
|
||||
)),
|
||||
]),
|
||||
$conn->translate('%sql', 'SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT
|
||||
CONCAT(last_name, ", ", first_name) AS full_name
|
||||
GROUP BY [user]
|
||||
@@ -263,25 +263,25 @@ LINES TERMINATED BY '\\n'
|
||||
|
||||
|
||||
|
||||
$array1 = array(1, 2, 3);
|
||||
$array2 = array('one', 'two', 'three');
|
||||
$array3 = array(
|
||||
$array1 = [1, 2, 3];
|
||||
$array2 = ['one', 'two', 'three'];
|
||||
$array3 = [
|
||||
'col1' => 'one',
|
||||
'col2' => 'two',
|
||||
'col3' => 'three',
|
||||
);
|
||||
$array4 = array(
|
||||
];
|
||||
$array4 = [
|
||||
'a' => 12,
|
||||
'b' => NULL,
|
||||
'c' => new DibiDateTime('12.3.2007'),
|
||||
'd' => 'any string',
|
||||
);
|
||||
];
|
||||
|
||||
$array5 = array('RAND()', '[col1] > [col2]');
|
||||
$array5 = ['RAND()', '[col1] > [col2]'];
|
||||
|
||||
|
||||
Assert::match(
|
||||
reformat(array(
|
||||
reformat([
|
||||
'mysql' => "SELECT *
|
||||
FROM `db`.`table`
|
||||
WHERE (`test`.`a` LIKE '1995-03-01'
|
||||
@@ -362,20 +362,20 @@ WHERE ([test].[a] LIKE '1995-03-01'
|
||||
OR [str_null]=NULL
|
||||
OR [str_not_null]='hello'
|
||||
LIMIT 10",
|
||||
)),
|
||||
]),
|
||||
|
||||
$conn->translate('SELECT *
|
||||
FROM [db.table]
|
||||
WHERE ([test.a] LIKE %d', '1995-03-01', '
|
||||
OR [b1] IN (', $array1, ')
|
||||
OR [b2] IN (%s', $array1, ')
|
||||
OR [b3] IN (%s', array(), ')
|
||||
OR [b3] IN (%s', [], ')
|
||||
OR [b4] IN (', $array2, ')
|
||||
OR [b5] IN (%n', $array3, ')
|
||||
OR [b6] IN %l', $array3, '
|
||||
OR [b7] IN %in', array(), '
|
||||
OR [b7] IN %in', [], '
|
||||
OR [b8] IN (%sql', $array5, ')
|
||||
OR [b9] IN (', array(), ")
|
||||
OR [b9] IN (', [], ")
|
||||
AND [c] = 'embedded '' string'
|
||||
OR [d]=%i", 10.3, '
|
||||
OR [e]=%i', NULL, '
|
||||
@@ -389,40 +389,40 @@ LIMIT 10')
|
||||
|
||||
Assert::same(
|
||||
reformat('TEST [cond] > 2 [cond2] = \'3\' cond3 < RAND() 123'),
|
||||
$conn->translate('TEST %ex', array('[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()'), 123)
|
||||
$conn->translate('TEST %ex', ['[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()'], 123)
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('TEST ([cond] > 2) OR ([cond2] > 3) OR ([cond3] = 10 + 1)'),
|
||||
$conn->translate('TEST %or', array('`cond` > 2', array('[cond2] > %i', '3'), 'cond3%sql' => array('10 + 1')))
|
||||
$conn->translate('TEST %or', ['`cond` > 2', ['[cond2] > %i', '3'], 'cond3%sql' => ['10 + 1']])
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('TEST ([cond] = 2) OR ([cond3] = RAND())'),
|
||||
$conn->translate('TEST %or', array('cond' => 2, 'cond3%sql' => 'RAND()'))
|
||||
$conn->translate('TEST %or', ['cond' => 2, 'cond3%sql' => 'RAND()'])
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('TEST ([cond1] 3) OR ([cond2] RAND()) OR ([cond3] LIKE \'string\')'),
|
||||
$conn->translate('TEST %or', array('cond1%ex' => 3, 'cond2%ex' => 'RAND()', 'cond3%ex' => array('LIKE %s', 'string')))
|
||||
$conn->translate('TEST %or', ['cond1%ex' => 3, 'cond2%ex' => 'RAND()', 'cond3%ex' => ['LIKE %s', 'string']])
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat(array(
|
||||
reformat([
|
||||
'odbc' => 'SELECT TOP 10 * FROM (SELECT * FROM [test] WHERE [id] LIKE \'%d%t\' ) t',
|
||||
'SELECT * FROM [test] WHERE [id] LIKE \'%d%t\' LIMIT 10',
|
||||
)),
|
||||
]),
|
||||
$conn->translate("SELECT * FROM [test] WHERE %n LIKE '%d%t' %lmt", 'id', 10)
|
||||
);
|
||||
|
||||
|
||||
$where = array(
|
||||
$where = [
|
||||
'tablename.column' => 1,
|
||||
);
|
||||
];
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [tablename] WHERE ([tablename].[column] = 1)'),
|
||||
$conn->translate('SELECT * FROM [tablename] WHERE %and', $where)
|
||||
@@ -447,39 +447,39 @@ Assert::same(
|
||||
|
||||
Assert::same(
|
||||
reformat('INSERT INTO [products] ([product_id], [title]) VALUES (1, SHA1(\'Test product\')) , (1, SHA1(\'Test product\'))'),
|
||||
$conn->translate('INSERT INTO [products]', array(
|
||||
$conn->translate('INSERT INTO [products]', [
|
||||
'product_id' => 1,
|
||||
'title' => array('SHA1(%s)', 'Test product'),
|
||||
), array(
|
||||
'title' => ['SHA1(%s)', 'Test product'],
|
||||
], [
|
||||
'product_id' => 1,
|
||||
'title' => array('SHA1(%s)', 'Test product'),
|
||||
))
|
||||
'title' => ['SHA1(%s)', 'Test product'],
|
||||
])
|
||||
);
|
||||
|
||||
Assert::same(
|
||||
reformat('UPDATE [products] [product_id]=1, [title]=SHA1(\'Test product\')'),
|
||||
$conn->translate('UPDATE [products]', array(
|
||||
$conn->translate('UPDATE [products]', [
|
||||
'product_id' => 1,
|
||||
'title' => array('SHA1(%s)', 'Test product'),
|
||||
))
|
||||
'title' => ['SHA1(%s)', 'Test product'],
|
||||
])
|
||||
);
|
||||
|
||||
|
||||
$e = Assert::exception(function () use ($conn) {
|
||||
$array6 = array(
|
||||
'id' => array(1, 2, 3, 4),
|
||||
'text' => array('ahoj', 'jak', 'se', array('SUM(%i)', '5')),
|
||||
'num%i' => array('1', ''),
|
||||
);
|
||||
$array6 = [
|
||||
'id' => [1, 2, 3, 4],
|
||||
'text' => ['ahoj', 'jak', 'se', ['SUM(%i)', '5']],
|
||||
'num%i' => ['1', ''],
|
||||
];
|
||||
$conn->translate('INSERT INTO test %m', $array6);
|
||||
}, 'DibiException', 'SQL translate error');
|
||||
Assert::same('INSERT INTO test **Multi-insert array "num%i" is different.**', $e->getSql());
|
||||
|
||||
$array6 = array(
|
||||
'id' => array(1, 2, 3, 4),
|
||||
'text' => array('ahoj', 'jak', 'se', array('SUM(%i)', '5')),
|
||||
'num%i' => array('1', '', 10.3, 1),
|
||||
);
|
||||
$array6 = [
|
||||
'id' => [1, 2, 3, 4],
|
||||
'text' => ['ahoj', 'jak', 'se', ['SUM(%i)', '5']],
|
||||
'num%i' => ['1', '', 10.3, 1],
|
||||
];
|
||||
|
||||
Assert::same(
|
||||
reformat('INSERT INTO test ([id], [text], [num]) VALUES (1, \'ahoj\', 1), (2, \'jak\', 0), (3, \'se\', 10), (4, SUM(5), 1)'),
|
||||
@@ -487,10 +487,10 @@ Assert::same(
|
||||
);
|
||||
|
||||
|
||||
$by = array(
|
||||
array('funkce(nazev_pole) ASC'),
|
||||
$by = [
|
||||
['funkce(nazev_pole) ASC'],
|
||||
'jine_pole' => 'DESC',
|
||||
);
|
||||
];
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM table ORDER BY funkce(nazev_pole) ASC, [jine_pole] DESC'),
|
||||
@@ -513,12 +513,12 @@ setLocale(LC_ALL, 'czech');
|
||||
Assert::same(
|
||||
reformat("UPDATE [colors] SET [color]='blue', [price]=-12.4, [spec]=-9E-005, [spec2]=1000, [spec3]=10000, [spec4]=10000 WHERE [price]=123.5"),
|
||||
|
||||
$conn->translate('UPDATE [colors] SET', array(
|
||||
$conn->translate('UPDATE [colors] SET', [
|
||||
'color' => 'blue',
|
||||
'price' => -12.4,
|
||||
'spec%f' => '-9E-005',
|
||||
'spec2%f' => 1000.00,
|
||||
'spec3%i' => 10000,
|
||||
'spec4' => 10000,
|
||||
), 'WHERE [price]=%f', 123.5)
|
||||
], 'WHERE [price]=%f', 123.5)
|
||||
);
|
||||
|
Reference in New Issue
Block a user