Skip to content

Commit 012bcbd

Browse files
pospisilvdg
authored andcommitted
ResultSet: reads fraction of seconds to DateInterval (#197)
Possibility of fraction of seconds to be readable from database by ActiveRow interface was added. Appropriate preg_match in Database/ResultSet.php was adjusted and resulting DateInterval class instance filled with fraction of second (if any). Unit test with MySQL was altered accordingly.
1 parent 0024c3b commit 012bcbd

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/Database/ResultSet.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ public function normalizeRow(array $row): array
153153
$row[$key] = new Nette\Utils\DateTime($value);
154154

155155
} elseif ($type === IStructure::FIELD_TIME_INTERVAL) {
156-
preg_match('#^(-?)(\d+)\D(\d+)\D(\d+)\z#', $value, $m);
156+
preg_match('#^(-?)(\d+)\D(\d+)\D(\d+)(\.\d+)?\z#', $value, $m);
157157
$row[$key] = new \DateInterval("PT$m[2]H$m[3]M$m[4]S");
158+
$row[$key]->f = isset($m[5]) ? (float) $m[5] : 0.0;
158159
$row[$key]->invert = (int) (bool) $m[1];
159160

160161
} elseif ($type === IStructure::FIELD_UNIX_TIMESTAMP) {

tests/Database/ResultSet.normalizeRow.mysql.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,13 @@ $res = $connection->query('SELECT `int` AS a, `char` AS a FROM types');
122122
Assert::same([
123123
'a' => 'a',
124124
], (array) @$res->fetch());
125+
126+
127+
$res = $connection->query('SELECT sec_to_time(avg(time_to_sec(`time`))) AS `avg_time` FROM `avgs`');
128+
129+
$avgTime = new DateInterval('PT10H10M10S');
130+
$avgTime->f = 0.5;
131+
132+
Assert::equal([
133+
'avg_time' => $avgTime,
134+
], (array) $res->fetch());

tests/Database/files/mysql-nette_test3.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,11 @@ INSERT INTO `types` (`unsigned_int`, `int`, `smallint`, `tinyint`, `mediumint`,
3838
(1, 1, 1, 1, 1, 1, 1, 1, 1.1, 1, 1.1, '2012-10-13', '30: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'),
3939
(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', ''),
4040
(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);
41+
42+
CREATE TEMPORARY TABLE `avgs` (
43+
`time` time
44+
) ENGINE=InnoDB;
45+
46+
INSERT INTO `avgs` (`time`) VALUES
47+
('10:10:11'),
48+
('10:10:10');

0 commit comments

Comments
 (0)