Skip to content

Commit b2d0642

Browse files
committed
Display null values as null instead of empty strings
1 parent 7cfff08 commit b2d0642

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [2.0.3] - 2018-06-13
6+
### Changed
7+
- Null values are now displayed as **null** instead of empty strings
8+
59
## [2.0.2] - 2018-05-12
610
### Fixed
711
- Fix support for Lumen

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ Using this package is free of charge, however to keep it up-to-date and add new
8888

8989
When upgrading from `1.*` version you should remove current `sql_logger.php` config file and replace this with new one (see installation step). You should also use new variables in `.env` file (old won't be used).
9090

91+
### Authors
92+
93+
Author of this awesome package is **[Marcin Nabiałek](http://marcin.nabialek.org/en/)** and [Contributors](https://github.com/mnabialek/laravel-sql-logger/graphs/contributors)
94+
9195
### Changes
9296

9397
All changes are listed in [Changelog](CHANGELOG.md)

src/Objects/Concerns/ReplacesBindings.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,29 @@ protected function replaceBindings($sql, array $bindings)
1919
$generalRegex = $this->getRegex();
2020

2121
foreach ($this->formatBindings($bindings) as $key => $binding) {
22-
$value = is_numeric($binding) ? $binding : "'" . $binding . "'";
2322
$regex = is_numeric($key) ? $generalRegex : $this->getNamedParameterRegex($key);
24-
$sql = preg_replace($regex, $value, $sql, 1);
23+
$sql = preg_replace($regex, $this->value($binding), $sql, 1);
2524
}
2625

2726
return $sql;
2827
}
2928

29+
/**
30+
* Get final value that will be displayed in query.
31+
*
32+
* @param mixed $value
33+
*
34+
* @return int|string
35+
*/
36+
protected function value($value)
37+
{
38+
if ($value === null) {
39+
return 'null';
40+
}
41+
42+
return is_numeric($value) ? $value : "'" . $value . "'";
43+
}
44+
3045
/**
3146
* Get regex to be used for named parameter with given name.
3247
*

tests/Objects/SqlQueryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,23 @@ public function it_handles_both_colon_and_non_colon_parameters()
135135

136136
$expectedSql = <<<EOF
137137
SELECT * FROM tests WHERE a = 'test@example.com' AND b = 'test';
138+
EOF;
139+
140+
$this->assertSame($expectedSql, $query->get());
141+
}
142+
143+
/** @test */
144+
public function it_leaves_null_values_not_changed()
145+
{
146+
$sql = <<<EOF
147+
UPDATE tests SET a = :email, b = :something WHERE id=:id;
148+
EOF;
149+
150+
$bindings = [':email' => 'test@example.com', 'something' => null, 'id' => 5];
151+
$query = new SqlQuery(56, $sql, $bindings, 130);
152+
153+
$expectedSql = <<<EOF
154+
UPDATE tests SET a = 'test@example.com', b = null WHERE id=5;
138155
EOF;
139156

140157
$this->assertSame($expectedSql, $query->get());

0 commit comments

Comments
 (0)