Permalink
Browse files

Database: fixes type convert of 0.0 & improved tests [Closes #806]

  • Loading branch information...
1 parent c7706ee commit 3c47da1a34c730369f4aa573443ff9bfd31723f1 @dg dg committed Oct 17, 2012
View
4 Nette/Database/Statement.php
@@ -112,7 +112,9 @@ public function normalizeRow($row)
$row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;
} elseif ($type === IReflection::FIELD_FLOAT) {
- $row[$key] = (string) ($tmp = (float) $value) === rtrim(rtrim($value, '0'), '.') ? $tmp : $value;
+ $value = strpos($value, '.') === FALSE ? $value : rtrim(rtrim($value, '0'), '.');
+ $float = (float) $value;
+ $row[$key] = (string) $float === $value ? $float : $value;
} elseif ($type === IReflection::FIELD_BOOL) {
$row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
View
162 tests/Nette/Database/Statement.normalizeRow.mysql.phpt
@@ -16,82 +16,100 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/mysql-nette_test3.
$res = $connection->query("SELECT * FROM types");
Assert::equal( array(
- 'int1' => 1,
- 'int2' => 1,
- 'int3' => 1,
- 'int4' => '1', // PHP bug #48724
- 'int5' => 1,
- 'int6' => 1,
- 'int7' => '1', // PHP bug #48724
- 'float1' => 1.0,
- 'float2' => 1.1,
- 'float3' => 1.0,
- 'float4' => 1.0,
- 'date1' => new Nette\DateTime('2012-10-13'),
- 'date2' => new Nette\DateTime('10:10:10'),
- 'date3' => new Nette\DateTime('2012-10-13 10:10:10'),
- 'date4' => new Nette\DateTime('2012-10-13 10:10:10'),
- 'date5' => '2012', // PHP bug #48724
- 'str1' => 'a',
- 'str2' => 'a',
- 'str3' => 'a',
- 'str4' => 'a',
- 'str5' => NULL,
- 'str6' => 'a',
- 'str7' => 'a',
- 'str8' => 'a',
+ 'unsigned_int' => 1,
+ 'int' => 1,
+ 'smallint' => 1,
+ 'tinyint' => '1', // PHP bug #48724
+ 'mediumint' => 1,
+ 'bigint' => 1,
+ 'bit' => '1', // PHP bug #48724
+ 'decimal' => 1.0,
+ 'decimal2' => 1.1,
+ 'float' => 1.0,
+ 'double' => 1.1,
+ 'date' => new Nette\DateTime('2012-10-13'),
+ 'time' => new Nette\DateTime('10:10:10'),
+ 'datetime' => new Nette\DateTime('2012-10-13 10:10:10'),
+ 'timestamp' => new Nette\DateTime('2012-10-13 10:10:10'),
+ 'year' => '2012', // PHP bug #48724
+ 'char' => 'a',
+ 'varchar' => 'a',
+ 'binary' => 'a',
+ 'varbinary' => 'a',
+ 'blob' => 'a',
+ 'tinyblob' => 'a',
+ 'mediumblob' => 'a',
+ 'longblob' => 'a',
+ 'text' => 'a',
+ 'tinytext' => 'a',
+ 'mediumtext' => 'a',
+ 'longtext' => 'a',
+ 'enum' => 'a',
+ 'set' => 'a',
), (array) $res->fetch() );
Assert::equal( array(
- 'int1' => 0,
- 'int2' => 0,
- 'int3' => 0,
- 'int4' => '0', // PHP bug #48724
- 'int5' => 0,
- 'int6' => 0,
- 'int7' => '0', // PHP bug #48724
- 'float1' => 0.5,
- 'float2' => 0.5,
- 'float3' => 0.5,
- 'float4' => 0.5,
- 'date1' => new Nette\DateTime('0000-00-00 00:00:00'),
- 'date2' => new Nette\DateTime('00:00:00'),
- 'date3' => new Nette\DateTime('0000-00-00 00:00:00'),
- 'date4' => new Nette\DateTime('0000-00-00 00:00:00'),
- 'date5' => '2000', // PHP bug #48724
- 'str1' => '',
- 'str2' => '',
- 'str3' => NULL,
- 'str4' => '',
- 'str5' => NULL,
- 'str6' => '',
- 'str7' => 'b',
- 'str8' => '',
+ 'unsigned_int' => 0,
+ 'int' => 0,
+ 'smallint' => 0,
+ 'tinyint' => '0', // PHP bug #48724
+ 'mediumint' => 0,
+ 'bigint' => 0,
+ 'bit' => '0', // PHP bug #48724
+ 'decimal' => 0.0,
+ 'decimal2' => 0.5,
+ 'float' => 0.5,
+ 'double' => 0.5,
+ 'date' => new Nette\DateTime('0000-00-00 00:00:00'),
+ 'time' => new Nette\DateTime('00:00:00'),
+ 'datetime' => new Nette\DateTime('0000-00-00 00:00:00'),
+ 'timestamp' => new Nette\DateTime('0000-00-00 00:00:00'),
+ 'year' => '2000', // PHP bug #48724
+ 'char' => '',
+ 'varchar' => '',
+ 'binary' => "\x00",
+ 'varbinary' => '',
+ 'blob' => '',
+ 'tinyblob' => '',
+ 'mediumblob' => '',
+ 'longblob' => '',
+ 'text' => '',
+ 'tinytext' => '',
+ 'mediumtext' => '',
+ 'longtext' => '',
+ 'enum' => 'b',
+ 'set' => '',
), (array) $res->fetch() );
Assert::equal( array(
- 'int1' => NULL,
- 'int2' => NULL,
- 'int3' => NULL,
- 'int4' => NULL,
- 'int5' => NULL,
- 'int6' => NULL,
- 'int7' => NULL,
- 'float1' => NULL,
- 'float2' => NULL,
- 'float3' => NULL,
- 'float4' => NULL,
- 'date1' => NULL,
- 'date2' => NULL,
- 'date3' => NULL,
- 'date4' => NULL,
- 'date5' => NULL,
- 'str1' => NULL,
- 'str2' => NULL,
- 'str3' => NULL,
- 'str4' => NULL,
- 'str5' => NULL,
- 'str6' => NULL,
- 'str7' => NULL,
- 'str8' => NULL,
+ 'unsigned_int' => NULL,
+ 'int' => NULL,
+ 'smallint' => NULL,
+ 'tinyint' => NULL,
+ 'mediumint' => NULL,
+ 'bigint' => NULL,
+ 'bit' => NULL,
+ 'decimal' => NULL,
+ 'decimal2' => NULL,
+ 'float' => NULL,
+ 'double' => NULL,
+ 'date' => NULL,
+ 'time' => NULL,
+ 'datetime' => NULL,
+ 'timestamp' => NULL,
+ 'year' => NULL,
+ 'char' => NULL,
+ 'varchar' => NULL,
+ 'binary' => NULL,
+ 'varbinary' => NULL,
+ 'blob' => NULL,
+ 'tinyblob' => NULL,
+ 'mediumblob' => NULL,
+ 'longblob' => NULL,
+ 'text' => NULL,
+ 'tinytext' => NULL,
+ 'mediumtext' => NULL,
+ 'longtext' => NULL,
+ 'enum' => NULL,
+ 'set' => NULL,
), (array) $res->fetch() );
View
182 tests/Nette/Database/Statement.normalizeRow.postgre.phpt
@@ -17,106 +17,106 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/pgsql-nette_test3.
$res = $connection->query("SELECT * FROM types");
$row = $res->fetch();
-Assert::true( is_string($row->float4) );
-unset($row->float4);
+Assert::true( is_string($row->money) );
+unset($row->money);
Assert::equal( array(
- 'int1' => 1,
- 'int2' => 1,
- 'int3' => 1,
- 'float1' => 1.0,
- 'float2' => 1.1,
- 'float3' => 1.11,
+ 'smallint' => 1,
+ 'integer' => 1,
+ 'bigint' => 1,
+ 'numeric' => 1.0,
+ 'real' => 1.1,
+ 'double' => 1.11,
'bool' => TRUE,
- 'date1' => new Nette\DateTime('2012-10-13'),
- 'date2' => new Nette\DateTime('10:10:10'),
- 'date3' => new Nette\DateTime('2012-10-13 10:10:10'),
- 'date4' => '1 year',
- 'str1' => 'a ',
- 'str2' => 'a',
- 'str3' => 'a',
- 'str4' => '\'a\'',
- 'str5' => '\'a\'',
- 'str6' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
- 'str7' => 'a',
- 'str8' => '192.168.1.0/24',
- 'str9' => '192.168.1.1',
- 'str10' => '08:00:2b:01:02:03',
- 'bin1' => '1',
- 'bin2' => '1',
- 'bin3' => NULL,
- 'geo1' => '(30,40),(10,20)',
- 'geo2' => '<(10,20),30>',
- 'geo3' => '[(10,20),(30,40)]',
- 'geo4' => '((10,20),(30,40))',
- 'geo5' => '(10,20)',
- 'geo6' => '((10,20),(30,40))',
+ 'date' => new Nette\DateTime('2012-10-13'),
+ 'time' => new Nette\DateTime('10:10:10'),
+ 'timestamp' => new Nette\DateTime('2012-10-13 10:10:10'),
+ 'interval' => '1 year',
+ 'character' => 'a ',
+ 'character_varying' => 'a',
+ 'text' => 'a',
+ 'tsquery' => '\'a\'',
+ 'tsvector' => '\'a\'',
+ 'uuid' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
+ 'xml' => 'a',
+ 'cidr' => '192.168.1.0/24',
+ 'inet' => '192.168.1.1',
+ 'macaddr' => '08:00:2b:01:02:03',
+ 'bit' => '1',
+ 'bit_varying' => '1',
+ 'bytea' => NULL,
+ 'box' => '(30,40),(10,20)',
+ 'circle' => '<(10,20),30>',
+ 'lseg' => '[(10,20),(30,40)]',
+ 'path' => '((10,20),(30,40))',
+ 'point' => '(10,20)',
+ 'polygon' => '((10,20),(30,40))',
), (array) $row );
Assert::equal( array(
- 'int1' => 0,
- 'int2' => 0,
- 'int3' => 0,
- 'float1' => 0.0,
- 'float2' => '0',
- 'float3' => '0',
- 'float4' => NULL,
+ 'smallint' => 0,
+ 'integer' => 0,
+ 'bigint' => 0,
+ 'numeric' => 0.0,
+ 'real' => 0.0,
+ 'double' => 0.0,
+ 'money' => NULL,
'bool' => FALSE,
- 'date1' => NULL,
- 'date2' => NULL,
- 'date3' => NULL,
- 'date4' => '00:00:00',
- 'str1' => ' ',
- 'str2' => '',
- 'str3' => '',
- 'str4' => '',
- 'str5' => '',
- 'str6' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
- 'str7' => 'a',
- 'str8' => '192.168.1.0/24',
- 'str9' => '192.168.1.1',
- 'str10' => '08:00:2b:01:02:03',
- 'bin1' => '0',
- 'bin2' => '0',
- 'bin3' => NULL,
- 'geo1' => '(30,40),(10,20)',
- 'geo2' => '<(10,20),30>',
- 'geo3' => '[(10,20),(30,40)]',
- 'geo4' => '((10,20),(30,40))',
- 'geo5' => '(10,20)',
- 'geo6' => '((10,20),(30,40))',
+ 'date' => NULL,
+ 'time' => NULL,
+ 'timestamp' => NULL,
+ 'interval' => '00:00:00',
+ 'character' => ' ',
+ 'character_varying' => '',
+ 'text' => '',
+ 'tsquery' => '',
+ 'tsvector' => '',
+ 'uuid' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
+ 'xml' => 'a',
+ 'cidr' => '192.168.1.0/24',
+ 'inet' => '192.168.1.1',
+ 'macaddr' => '08:00:2b:01:02:03',
+ 'bit' => '0',
+ 'bit_varying' => '0',
+ 'bytea' => NULL,
+ 'box' => '(30,40),(10,20)',
+ 'circle' => '<(10,20),30>',
+ 'lseg' => '[(10,20),(30,40)]',
+ 'path' => '((10,20),(30,40))',
+ 'point' => '(10,20)',
+ 'polygon' => '((10,20),(30,40))',
), (array) $res->fetch() );
Assert::equal( array(
- 'int1' => NULL,
- 'int2' => NULL,
- 'int3' => NULL,
- 'float1' => NULL,
- 'float2' => NULL,
- 'float3' => NULL,
- 'float4' => NULL,
+ 'smallint' => NULL,
+ 'integer' => NULL,
+ 'bigint' => NULL,
+ 'numeric' => NULL,
+ 'real' => NULL,
+ 'double' => NULL,
+ 'money' => NULL,
'bool' => NULL,
- 'date1' => NULL,
- 'date2' => NULL,
- 'date3' => NULL,
- 'date4' => NULL,
- 'str1' => NULL,
- 'str2' => NULL,
- 'str3' => NULL,
- 'str4' => NULL,
- 'str5' => NULL,
- 'str6' => NULL,
- 'str7' => NULL,
- 'str8' => NULL,
- 'str9' => NULL,
- 'str10' => NULL,
- 'bin1' => NULL,
- 'bin2' => NULL,
- 'bin3' => NULL,
- 'geo1' => NULL,
- 'geo2' => NULL,
- 'geo3' => NULL,
- 'geo4' => NULL,
- 'geo5' => NULL,
- 'geo6' => NULL,
+ 'date' => NULL,
+ 'time' => NULL,
+ 'timestamp' => NULL,
+ 'interval' => NULL,
+ 'character' => NULL,
+ 'character_varying' => NULL,
+ 'text' => NULL,
+ 'tsquery' => NULL,
+ 'tsvector' => NULL,
+ 'uuid' => NULL,
+ 'xml' => NULL,
+ 'cidr' => NULL,
+ 'inet' => NULL,
+ 'macaddr' => NULL,
+ 'bit' => NULL,
+ 'bit_varying' => NULL,
+ 'bytea' => NULL,
+ 'box' => NULL,
+ 'circle' => NULL,
+ 'lseg' => NULL,
+ 'path' => NULL,
+ 'point' => NULL,
+ 'polygon' => NULL,
), (array) $res->fetch() );
View
62 tests/Nette/Database/mysql-nette_test3.sql
@@ -8,33 +8,39 @@ SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `types`;
CREATE TABLE `types` (
- `int1` int(11),
- `int2` int(11),
- `int3` smallint(6),
- `int4` tinyint(4),
- `int5` mediumint(9),
- `int6` bigint(20),
- `int7` bit(1),
- `float1` decimal(10,2),
- `float2` decimal(10,2),
- `float3` float,
- `float4` double,
- `date1` date,
- `date2` time,
- `date3` datetime,
- `date4` timestamp NULL,
- `date5` year(4),
- `str1` char(1),
- `str2` varchar(30),
- `str3` binary(1),
- `str4` varbinary(30),
- `str5` blob,
- `str6` text,
- `str7` enum('a','b'),
- `str8` set('a','b')
+ `unsigned_int` int(11) unsigned,
+ `int` int(11),
+ `smallint` smallint(6),
+ `tinyint` tinyint(4),
+ `mediumint` mediumint(9),
+ `bigint` bigint(20),
+ `bit` bit(1),
+ `decimal` decimal(10,0),
+ `decimal2` decimal(10,2),
+ `float` float,
+ `double` double,
+ `date` date,
+ `time` time,
+ `datetime` datetime,
+ `timestamp` timestamp NULL,
+ `year` year(4),
+ `char` char(1),
+ `varchar` varchar(30),
+ `binary` binary(1),
+ `varbinary` varbinary(30),
+ `blob` blob,
+ `tinyblob` tinyblob,
+ `mediumblob` mediumblob,
+ `longblob` longblob,
+ `text` text,
+ `tinytext` tinytext,
+ `mediumtext` mediumtext,
+ `longtext` longtext,
+ `enum` enum('a','b'),
+ `set` set('a','b')
) ENGINE=InnoDB;
-INSERT INTO `types` (`int1`, `int2`, `int3`, `int4`, `int5`, `int6`, `int7`, `float1`, `float2`, `float3`, `float4`, `date1`, `date2`, `date3`, `date4`, `date5`, `str1`, `str2`, `str3`, `str4`, `str5`, `str6`, `str7`, `str8`) VALUES
-(1, 1, 1, 1, 1, 1, 1, 1.00, 1.10, 1, 1, '2012-10-13', '10:10:10', '2012-10-13 10:10:10', '2012-10-13 10:10:10', '2012', 'a', 'a', 'a', 'a', NULL, 'a', 'a', 'a'),
-(0, 0, 0, 0, 0, 0, 0, 0.50, 0.50, 0.5, 0.5, '0000-00-00', '00:00:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '2000', '', '', NULL, '', NULL, '', 'b', ''),
-(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
+INSERT INTO `types` (`unsigned_int`, `int`, `smallint`, `tinyint`, `mediumint`, `bigint`, `bit`, `decimal`, `decimal2`, `float`, `double`, `date`, `time`, `datetime`, `timestamp`, `year`, `char`, `varchar`, `binary`, `varbinary`, `blob`, `tinyblob`, `mediumblob`, `longblob`, `text`, `tinytext`, `mediumtext`, `longtext`, `enum`, `set`) VALUES
+(1, 1, 1, 1, 1, 1, 1, 1, 1.1, 1, 1.1, '2012-10-13', '10:10:10', '2012-10-13 10:10:10', '2012-10-13 10:10:10', '2012', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'),
+(0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0.5, 0.5, '0000-00-00', '00:00:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '2000', '', '', '\0', '', '', '', '', '', '', '', '', '', 'b', ''),
+(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
View
66 tests/Nette/Database/pgsql-nette_test3.sql
@@ -3,44 +3,44 @@ CREATE SCHEMA public;
DROP TABLE IF EXISTS "types";
CREATE TABLE "types" (
- "int1" smallint NULL,
- "int2" integer NULL,
- "int3" bigint NULL,
- "float1" numeric NULL,
- "float2" real NULL,
- "float3" double precision NULL,
- "float4" money NULL,
+ "smallint" smallint NULL,
+ "integer" integer NULL,
+ "bigint" bigint NULL,
+ "numeric" numeric NULL,
+ "real" real NULL,
+ "double" double precision NULL,
+ "money" money NULL,
"bool" boolean NULL,
- "date1" date NULL,
- "date2" time NULL,
- "date3" timestamp NULL,
- "date4" interval NULL,
- "str1" character(30) NULL,
- "str2" character varying(30) NULL,
- "str3" text NULL,
- "str4" tsquery NULL,
- "str5" tsvector NULL,
- "str6" uuid NULL,
- "str7" xml NULL,
- "str8" cidr NULL,
- "str9" inet NULL,
- "str10" macaddr NULL,
- "bin1" bit NULL,
- "bin2" bit varying NULL,
- "bin3" bytea NULL,
- "geo1" box NULL,
- "geo2" circle NULL,
- "geo3" lseg NULL,
- "geo4" path NULL,
- "geo5" point NULL,
- "geo6" polygon NULL
+ "date" date NULL,
+ "time" time NULL,
+ "timestamp" timestamp NULL,
+ "interval" interval NULL,
+ "character" character(30) NULL,
+ "character_varying" character varying(30) NULL,
+ "text" text NULL,
+ "tsquery" tsquery NULL,
+ "tsvector" tsvector NULL,
+ "uuid" uuid NULL,
+ "xml" xml NULL,
+ "cidr" cidr NULL,
+ "inet" inet NULL,
+ "macaddr" macaddr NULL,
+ "bit" bit NULL,
+ "bit_varying" bit varying NULL,
+ "bytea" bytea NULL,
+ "box" box NULL,
+ "circle" circle NULL,
+ "lseg" lseg NULL,
+ "path" path NULL,
+ "point" point NULL,
+ "polygon" polygon NULL
);
-INSERT INTO "types" ("int1", "int2", "int3", "float1", "float2", "float3", "float4", "bool", "date1", "date2", "date3", "date4", "str1", "str2", "str3", "str4", "str5", "str6", "str7", "str8", "str9", "str10", "bin1", "bin2", "geo1", "geo2", "geo3", "geo4", "geo5", "geo6")
+INSERT INTO "types" ("smallint", "integer", "bigint", "numeric", "real", "double", "money", "bool", "date", "time", "timestamp", "interval", "character", "character_varying", "text", "tsquery", "tsvector", "uuid", "xml", "cidr", "inet", "macaddr", "bit", "bit_varying", "box", "circle", "lseg", "path", "point", "polygon")
VALUES ('1', '1', '1', '1.00', '1.10', '1.11', '0', 'T', '2012-10-13', '10:10:10', '2012-10-13 10:10:10', '1 year', 'a', 'a', 'a', 'a', 'a', 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', 'a', '192.168.1', '192.168.1.1', '08002b:010203', '1', '1', '10,20,30,40', '10,20,30', '10,20,30,40', '10,20,30,40', '10,20', '10,20,30,40');
-INSERT INTO "types" ("int1", "int2", "int3", "float1", "float2", "float3", "float4", "bool", "date1", "date2", "date3", "date4", "str1", "str2", "str3", "str4", "str5", "str6", "str7", "str8", "str9", "str10", "bin1", "bin2", "geo1", "geo2", "geo3", "geo4", "geo5", "geo6")
+INSERT INTO "types" ("smallint", "integer", "bigint", "numeric", "real", "double", "money", "bool", "date", "time", "timestamp", "interval", "character", "character_varying", "text", "tsquery", "tsvector", "uuid", "xml", "cidr", "inet", "macaddr", "bit", "bit_varying", "box", "circle", "lseg", "path", "point", "polygon")
VALUES ('0', '0', '0', '0.00', '0.0', '0.0', NULL, 'F', NULL, NULL, NULL, '0 year', '', '', '', '', '', 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', 'a', '192.168.1', '192.168.1.1', '08002b:010203', '0', '0', '10,20,30,40', '10,20,30', '10,20,30,40', '10,20,30,40', '10,20', '10,20,30,40');
-INSERT INTO "types" ("int1", "int2", "int3", "float1", "float2", "float3", "float4", "bool", "date1", "date2", "date3", "date4", "str1", "str2", "str3", "str4", "str5", "str6", "str7", "str8", "str9", "str10", "bin1", "bin2", "geo1", "geo2", "geo3", "geo4", "geo5", "geo6")
+INSERT INTO "types" ("smallint", "integer", "bigint", "numeric", "real", "double", "money", "bool", "date", "time", "timestamp", "interval", "character", "character_varying", "text", "tsquery", "tsvector", "uuid", "xml", "cidr", "inet", "macaddr", "bit", "bit_varying", "box", "circle", "lseg", "path", "point", "polygon")
VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

0 comments on commit 3c47da1

Please sign in to comment.