Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Database: fixes in type detection & added tests [Closes #806]

  • Loading branch information...
commit e0d54b6a8211d49003408d0c9f874bbe7e6e1278 1 parent e7b8c36
@dg dg authored
View
6 Nette/Database/Helpers.php
@@ -29,13 +29,13 @@ class Helpers
public static $typePatterns = array(
'^_' => IReflection::FIELD_TEXT, // PostgreSQL arrays
'BYTEA|BLOB|BIN' => IReflection::FIELD_BINARY,
- 'TEXT|CHAR' => IReflection::FIELD_TEXT,
- 'YEAR|BYTE|COUNTER|SERIAL|INT|LONG' => IReflection::FIELD_INTEGER,
+ 'TEXT|CHAR|POINT|INTERVAL' => IReflection::FIELD_TEXT,
+ 'YEAR|BYTE|COUNTER|SERIAL|INT|LONG|SHORT' => IReflection::FIELD_INTEGER,
'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => IReflection::FIELD_FLOAT,
'^TIME$' => IReflection::FIELD_TIME,
'TIME' => IReflection::FIELD_DATETIME, // DATETIME, TIMESTAMP
'DATE' => IReflection::FIELD_DATE,
- 'BOOL|BIT' => IReflection::FIELD_BOOL,
+ 'BOOL' => IReflection::FIELD_BOOL,
);
View
2  Nette/Database/Statement.php
@@ -112,7 +112,7 @@ public function normalizeRow($row)
$row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;
} elseif ($type === IReflection::FIELD_FLOAT) {
- $row[$key] = (string) ($tmp = (float) $value) === $value ? $tmp : $value;
+ $row[$key] = (string) ($tmp = (float) $value) === rtrim(rtrim($value, '0'), '.') ? $tmp : $value;
} elseif ($type === IReflection::FIELD_BOOL) {
$row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
View
97 tests/Nette/Database/Statement.normalizeRow.mysql.phpt
@@ -0,0 +1,97 @@
+<?php
+
+/**
+ * Test: Nette\Database\Statement::normalizeRow()
+ *
+ * @author David Grudl
+ * @package Nette\Database
+ */
+
+require __DIR__ . '/connect.inc.php'; // create $connection
+
+Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/mysql-nette_test3.sql");
+
+
+
+$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 00:00:00'),
+ 'date2' => new Nette\DateTime('2012-10-13 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',
+), (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' => '',
+), (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,
+), (array) $res->fetch() );
View
122 tests/Nette/Database/Statement.normalizeRow.postgre.phpt
@@ -0,0 +1,122 @@
+<?php
+
+/**
+ * Test: Nette\Database\Statement::normalizeRow()
+ *
+ * @author David Grudl
+ * @package Nette\Database
+ */
+
+$_SERVER['argv'][1] = 'postgresql';
+require __DIR__ . '/connect.inc.php'; // create $connection
+
+Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/pgsql-nette_test3.sql");
+
+
+
+$res = $connection->query("SELECT * FROM types");
+
+$row = $res->fetch();
+Assert::true( is_string($row->float4) );
+unset($row->float4);
+
+Assert::equal( array(
+ 'int1' => 1,
+ 'int2' => 1,
+ 'int3' => 1,
+ 'float1' => 1.0,
+ 'float2' => 1.1,
+ 'float3' => 1.11,
+ 'bool' => TRUE,
+ 'date1' => new Nette\DateTime('2012-10-13 00:00:00'),
+ 'date2' => new Nette\DateTime('2012-10-13 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))',
+), (array) $row );
+
+Assert::equal( array(
+ 'int1' => 0,
+ 'int2' => 0,
+ 'int3' => 0,
+ 'float1' => 0.0,
+ 'float2' => '0',
+ 'float3' => '0',
+ 'float4' => 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))',
+), (array) $res->fetch() );
+
+Assert::equal( array(
+ 'int1' => NULL,
+ 'int2' => NULL,
+ 'int3' => NULL,
+ 'float1' => NULL,
+ 'float2' => NULL,
+ 'float3' => NULL,
+ 'float4' => 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,
+), (array) $res->fetch() );
View
40 tests/Nette/Database/mysql-nette_test3.sql
@@ -0,0 +1,40 @@
+/*!40102 SET storage_engine = InnoDB */;
+
+DROP DATABASE IF EXISTS nette_test;
+CREATE DATABASE IF NOT EXISTS nette_test;
+USE nette_test;
+
+SET FOREIGN_KEY_CHECKS = 0;

Missing SET FOREIGN_KEY_CHECKS = 1; at the end of the file perhaps?

@hrach
hrach added a note

No. It seems just irrelevant. We could strip this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+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')
+) 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);
View
46 tests/Nette/Database/pgsql-nette_test3.sql
@@ -0,0 +1,46 @@
+DROP SCHEMA IF EXISTS public CASCADE;
+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,
+ "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
+);
+
+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")
+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")
+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")
+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);
@tomaswindsor

Missing SET FOREIGN_KEY_CHECKS = 1; at the end of the file perhaps?

@hrach

No. It seems just irrelevant. We could strip this.

Please sign in to comment.
Something went wrong with that request. Please try again.