1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-30 17:29:53 +02:00

Compare commits

...

6 Commits

Author SHA1 Message Date
David Grudl
8f0d0fb115 Released version 2.3.3 2015-10-22 02:44:33 +02:00
David Grudl
965570c067 removed unused code 2015-10-22 02:43:57 +02:00
David Grudl
4ae4f49c21 Result: fixed normalization of float when ends with "0" [Closes #189] 2015-10-13 15:13:59 +02:00
David Grudl
5b9ffe14ba Result: normalize converts FALSE 2015-10-13 15:13:59 +02:00
castamir
2d9358e4f7 DibiFluent::fetch(): fixed limit clause duplication [Closes #188][Closes #186][Closes #185] 2015-10-09 11:41:12 +02:00
David Grudl
e3748420f5 DibiFluent: removed keyword AS from SQL [Closes #172] 2015-10-09 00:17:10 +02:00
9 changed files with 249 additions and 51 deletions

View File

@@ -30,11 +30,6 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
*/
public function getTables()
{
/*$this->query("
SELECT TABLE_NAME as name, TABLE_TYPE = 'VIEW' as view
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE()
");*/
$res = $this->driver->query('SHOW FULL TABLES');
$tables = array();
while ($row = $res->fetch(FALSE)) {
@@ -54,12 +49,6 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
*/
public function getColumns($table)
{
/*$table = $this->escape($table, dibi::TEXT);
$this->query("
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE()
");*/
$res = $this->driver->query("SHOW FULL COLUMNS FROM {$this->driver->escape($table, dibi::IDENTIFIER)}");
$columns = array();
while ($row = $res->fetch(TRUE)) {
@@ -87,13 +76,6 @@ class DibiMySqlReflector extends DibiObject implements IDibiReflector
*/
public function getIndexes($table)
{
/*$table = $this->escape($table, dibi::TEXT);
$this->query("
SELECT *
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE()
AND REFERENCED_COLUMN_NAME IS NULL
");*/
$res = $this->driver->query("SHOW INDEX FROM {$this->driver->escape($table, dibi::IDENTIFIER)}");
$indexes = array();
while ($row = $res->fetch(TRUE)) {

View File

@@ -51,12 +51,6 @@ class DibiSqliteReflector extends DibiObject implements IDibiReflector
*/
public function getColumns($table)
{
$meta = $this->driver->query("
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = {$this->driver->escape($table, dibi::TEXT)}
UNION ALL
SELECT sql FROM sqlite_temp_master WHERE type = 'table' AND name = {$this->driver->escape($table, dibi::TEXT)}
")->fetch(TRUE);
$res = $this->driver->query("PRAGMA table_info({$this->driver->escape($table, dibi::IDENTIFIER)})");
$columns = array();
while ($row = $res->fetch(TRUE)) {

View File

@@ -38,8 +38,8 @@ class dibi
FIELD_TIME = self::TIME;
/** version */
const VERSION = '2.3.2',
REVISION = 'released on 2015-04-18';
const VERSION = '2.3.3',
REVISION = 'released on 2015-10-22';
/** sorting order */
const ASC = 'ASC',
@@ -368,18 +368,6 @@ class dibi
}
/**
* Replacement for majority of dibi::methods() in future.
*/
public static function __callStatic($name, $args)
{
//if ($name = 'select', 'update', ...') {
// return self::command()->$name($args);
//}
return call_user_func_array(array(self::getConnection(), $name), $args);
}
/********************* fluent SQL builders ****************d*g**/

View File

@@ -315,11 +315,12 @@ class DibiFluent extends DibiObject implements IDataSource
*/
public function fetch()
{
if ($this->command === 'SELECT') {
return $this->query($this->_export(NULL, array('%lmt', 1)))->fetch();
} else {
return $this->query($this->_export())->fetch();
if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) {
$result = $this->query($this->limit(1)->_export())->fetch();
$this->removeClause('LIMIT');
return $result;
}
return $this->query($this->_export())->fetch();
}
@@ -329,11 +330,12 @@ class DibiFluent extends DibiObject implements IDataSource
*/
public function fetchSingle()
{
if ($this->command === 'SELECT') {
return $this->query($this->_export(NULL, array('%lmt', 1)))->fetchSingle();
} else {
return $this->query($this->_export())->fetchSingle();
if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) {
$result = $this->query($this->limit(1)->_export())->fetchSingle();
$this->removeClause('LIMIT');
return $result;
}
return $this->query($this->_export())->fetchSingle();
}
@@ -401,7 +403,7 @@ class DibiFluent extends DibiObject implements IDataSource
public function count()
{
return (int) $this->query(array(
'SELECT COUNT(*) FROM (%ex', $this->_export(), ') AS [data]',
'SELECT COUNT(*) FROM (%ex', $this->_export(), ') [data]',
))->fetchSingle();
}

View File

@@ -488,13 +488,23 @@ class DibiResult extends DibiObject implements IDataSource
continue;
}
$value = $row[$key];
if ($value === FALSE || $type === dibi::TEXT) {
if ($type === dibi::TEXT) {
} elseif ($type === dibi::INTEGER) {
$row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;
} elseif ($type === dibi::FLOAT) {
$row[$key] = str_replace(',', '.', ltrim((string) ($tmp = (float) $value), '0')) === ltrim(rtrim(rtrim($value, '0'), '.'), '0') ? $tmp : $value;
$value = ltrim($value, '0');
$p = strpos($value, '.');
if ($p !== FALSE) {
$value = rtrim(rtrim($value, '0'), '.');
}
if ($value === '' || $value[0] === '.') {
$value = '0' . $value;
}
$row[$key] = $value === str_replace(',', '.', (string) ($float = (float) $value))
? $float
: $value;
} elseif ($type === dibi::BOOL) {
$row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';

View File

@@ -215,7 +215,7 @@ final class DibiTranslator extends DibiObject
$v = $this->formatValue($v, FALSE);
$vx[] = $k . ($v === 'NULL' ? 'IS ' : '= ') . $v;
} elseif ($pair[1] === 'ex') { // TODO: this will be removed
} elseif ($pair[1] === 'ex') {
$vx[] = $k . $this->formatValue($v, 'ex');
} else {

View File

@@ -0,0 +1,92 @@
<?php
/**
* @dataProvider ../databases.ini
*/
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
$conn = new DibiConnection($config);
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
// fetch & limit
$fluent = $conn->select('*')
->from('customers')
->limit(1)
->offset(3)
->orderBy('customer_id');
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'),
(string) $fluent
);
$fluent->fetch();
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'),
dibi::$sql
);
$fluent->fetchSingle();
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'),
dibi::$sql
);
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'),
(string) $fluent
);
$fluent->limit(0);
$fluent->fetch();
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'),
dibi::$sql
);
$fluent->fetchSingle();
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'),
dibi::$sql
);
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'),
(string) $fluent
);
$fluent->removeClause('limit');
$fluent->fetch();
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'),
dibi::$sql
);
$fluent->fetchSingle();
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'),
dibi::$sql
);
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] OFFSET 3'),
(string) $fluent
);
$fluent->removeClause('offset');
$fluent->fetch();
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'),
dibi::$sql
);
$fluent->fetchSingle();
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'),
dibi::$sql
);
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id]'),
(string) $fluent
);

View File

@@ -81,7 +81,7 @@ try {
} catch (Exception $e) {
}
Assert::same(
reformat(' SELECT * FROM [table] LIMIT 1'),
reformat('SELECT * FROM [table] LIMIT 1'),
dibi::$sql
);

View File

@@ -0,0 +1,130 @@
<?php
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
class MockResult extends DibiResult
{
function __construct()
{}
function test($row)
{
$normalize = new ReflectionMethod('DibiResult', 'normalize');
$normalize->setAccessible(TRUE);
$normalize->invokeArgs($this, array(& $row));
return $row;
}
}
test(function () {
$result = new MockResult;
$result->setType('col', dibi::BOOL);
Assert::same(array('col' => NULL), $result->test(array('col' => NULL)));
Assert::same(array('col' => TRUE), $result->test(array('col' => TRUE)));
Assert::same(array('col' => FALSE), $result->test(array('col' => FALSE)));
Assert::same(array('col' => FALSE), $result->test(array('col' => '')));
Assert::same(array('col' => FALSE), $result->test(array('col' => '0')));
Assert::same(array('col' => TRUE), $result->test(array('col' => '1')));
Assert::same(array('col' => TRUE), $result->test(array('col' => 't')));
Assert::same(array('col' => FALSE), $result->test(array('col' => 'f')));
Assert::same(array('col' => TRUE), $result->test(array('col' => 'T')));
Assert::same(array('col' => FALSE), $result->test(array('col' => 'F')));
Assert::same(array('col' => FALSE), $result->test(array('col' => 0)));
Assert::same(array('col' => FALSE), $result->test(array('col' => 0.0)));
Assert::same(array('col' => TRUE), $result->test(array('col' => 1)));
Assert::same(array('col' => TRUE), $result->test(array('col' => 1.0)));
});
test(function () {
$result = new MockResult;
$result->setType('col', dibi::TEXT); // means TEXT or UNKNOWN
Assert::same(array('col' => NULL), $result->test(array('col' => NULL)));
Assert::same(array('col' => TRUE), $result->test(array('col' => TRUE)));
Assert::same(array('col' => FALSE), $result->test(array('col' => FALSE)));
Assert::same(array('col' => ''), $result->test(array('col' => '')));
Assert::same(array('col' => '0'), $result->test(array('col' => '0')));
Assert::same(array('col' => '1'), $result->test(array('col' => '1')));
Assert::same(array('col' => 0), $result->test(array('col' => 0)));
Assert::same(array('col' => 1), $result->test(array('col' => 1)));
});
test(function () {
$result = new MockResult;
$result->setType('col', dibi::FLOAT);
Assert::same(array('col' => NULL), $result->test(array('col' => NULL)));
Assert::same(array('col' => 1.0), $result->test(array('col' => TRUE)));
Assert::same(array('col' => 0.0), $result->test(array('col' => FALSE)));
Assert::same(array('col' => 0.0), $result->test(array('col' => '')));
Assert::same(array('col' => 0.0), $result->test(array('col' => '0')));
Assert::same(array('col' => 1.0), $result->test(array('col' => '1')));
Assert::same(array('col' => 0.0), $result->test(array('col' => '.0')));
Assert::same(array('col' => 0.1), $result->test(array('col' => '.1')));
Assert::same(array('col' => 0.0), $result->test(array('col' => '0.0')));
Assert::same(array('col' => 0.1), $result->test(array('col' => '0.1')));
Assert::same(array('col' => 0.0), $result->test(array('col' => '0.000')));
Assert::same(array('col' => 0.1), $result->test(array('col' => '0.100')));
Assert::same(array('col' => 1.0), $result->test(array('col' => '1.0')));
Assert::same(array('col' => 1.1), $result->test(array('col' => '1.1')));
Assert::same(array('col' => 1.0), $result->test(array('col' => '1.000')));
Assert::same(array('col' => 1.1), $result->test(array('col' => '1.100')));
Assert::same(array('col' => 1.0), $result->test(array('col' => '001.000')));
Assert::same(array('col' => 1.1), $result->test(array('col' => '001.100')));
Assert::same(array('col' => 10.0), $result->test(array('col' => '10')));
Assert::same(array('col' => 11.0), $result->test(array('col' => '11')));
Assert::same(array('col' => 10.0), $result->test(array('col' => '0010')));
Assert::same(array('col' => 11.0), $result->test(array('col' => '0011')));
Assert::same(array('col' => '0.00000000000000000001'), $result->test(array('col' => '0.00000000000000000001')));
Assert::same(array('col' => '12345678901234567890'), $result->test(array('col' => '12345678901234567890')));
Assert::same(array('col' => '12345678901234567890'), $result->test(array('col' => '012345678901234567890')));
Assert::same(array('col' => '12345678901234567890'), $result->test(array('col' => '12345678901234567890.000')));
Assert::same(array('col' => '12345678901234567890.1'), $result->test(array('col' => '012345678901234567890.100')));
Assert::same(array('col' => 0.0), $result->test(array('col' => 0)));
Assert::same(array('col' => 0.0), $result->test(array('col' => 0.0)));
Assert::same(array('col' => 1.0), $result->test(array('col' => 1)));
Assert::same(array('col' => 1.0), $result->test(array('col' => 1.0)));
setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu');
Assert::same(array('col' => 0.0), $result->test(array('col' => '')));
Assert::same(array('col' => 0.0), $result->test(array('col' => '0')));
Assert::same(array('col' => 1.0), $result->test(array('col' => '1')));
Assert::same(array('col' => 0.0), $result->test(array('col' => '.0')));
Assert::same(array('col' => 0.1), $result->test(array('col' => '.1')));
Assert::same(array('col' => 0.0), $result->test(array('col' => '0.0')));
Assert::same(array('col' => 0.1), $result->test(array('col' => '0.1')));
Assert::same(array('col' => 0.0), $result->test(array('col' => '0.000')));
Assert::same(array('col' => 0.1), $result->test(array('col' => '0.100')));
Assert::same(array('col' => 1.0), $result->test(array('col' => '1.0')));
Assert::same(array('col' => 1.1), $result->test(array('col' => '1.1')));
Assert::same(array('col' => 1.0), $result->test(array('col' => '1.000')));
Assert::same(array('col' => 1.1), $result->test(array('col' => '1.100')));
Assert::same(array('col' => 1.0), $result->test(array('col' => '001.000')));
Assert::same(array('col' => 1.1), $result->test(array('col' => '001.100')));
Assert::same(array('col' => 10.0), $result->test(array('col' => '10')));
Assert::same(array('col' => 11.0), $result->test(array('col' => '11')));
Assert::same(array('col' => 10.0), $result->test(array('col' => '0010')));
Assert::same(array('col' => 11.0), $result->test(array('col' => '0011')));
Assert::same(array('col' => '0.00000000000000000001'), $result->test(array('col' => '0.00000000000000000001')));
Assert::same(array('col' => '12345678901234567890'), $result->test(array('col' => '12345678901234567890')));
Assert::same(array('col' => '12345678901234567890'), $result->test(array('col' => '012345678901234567890')));
Assert::same(array('col' => '12345678901234567890'), $result->test(array('col' => '12345678901234567890.000')));
Assert::same(array('col' => '12345678901234567890.1'), $result->test(array('col' => '012345678901234567890.100')));
Assert::same(array('col' => 0.0), $result->test(array('col' => 0)));
Assert::same(array('col' => 0.0), $result->test(array('col' => 0.0)));
Assert::same(array('col' => 1.0), $result->test(array('col' => 1)));
Assert::same(array('col' => 1.0), $result->test(array('col' => 1.0)));
setlocale(LC_NUMERIC, 'C');
});