Skip to content

Commit

Permalink
Merge pull request #727 from barryvdh/fix-query-nulls
Browse files Browse the repository at this point in the history
Check for null values in query bindings
  • Loading branch information
taylorotwell authored Sep 9, 2019
2 parents b61409a + 4e43ad9 commit 36fca63
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Watchers/QueryWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ public function replaceBindings($event)
? "/\?(?=(?:[^'\\\']*'[^'\\\']*')*[^'\\\']*$)/"
: "/:{$key}(?=(?:[^'\\\']*'[^'\\\']*')*[^'\\\']*$)/";

if (! is_int($binding) && ! is_float($binding)) {
if ($binding === null) {
$binding = 'null';
} elseif (! is_int($binding) && ! is_float($binding)) {
$binding = $event->connection->getPdo()->quote($binding);
}

Expand Down
37 changes: 35 additions & 2 deletions tests/Watchers/QueryWatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,46 @@ public function test_query_watcher_can_prepare_bindings()
->where('type', 'query')
->where('should_display_on_index', true)
->whereNull('family_hash')
->where('sequence', '>', 100)
->where('created_at', '<', Carbon::parse('2019-01-01'))
->count();
->update([
'content' => null,
'should_display_on_index' => false,
]);

$entry = $this->loadTelescopeEntries()->first();

$this->assertSame(EntryType::QUERY, $entry->type);
$this->assertSame('select count(*) as aggregate from "telescope_entries" where "type" = \'query\' and "should_display_on_index" = 1 and "family_hash" is null and "created_at" < \'2019-01-01 00:00:00\'', $entry->content['sql']);
$this->assertSame(<<<'SQL'
update "telescope_entries" set "content" = null, "should_display_on_index" = 0 where "type" = 'query' and "should_display_on_index" = 1 and "family_hash" is null and "sequence" > 100 and "created_at" < '2019-01-01 00:00:00'
SQL
, $entry->content['sql']);

$this->assertSame('testbench', $entry->content['connection']);
}

public function test_query_watcher_can_prepare_named_bindings()
{
$this->app->get('db')->statement(<<<'SQL'
update "telescope_entries" set "content" = :content, "should_display_on_index" = :index_new where "type" = :type and "should_display_on_index" = :index_old and "family_hash" is null and "sequence" > :sequence and "created_at" < :created_at
SQL
, [
'sequence' => 100,
'index_old' => 1,
'type' => 'query',
'created_at' => Carbon::parse('2019-01-01'),
'index_new' => 0,
'content' => null,
]);

$entry = $this->loadTelescopeEntries()->first();

$this->assertSame(EntryType::QUERY, $entry->type);
$this->assertSame(<<<'SQL'
update "telescope_entries" set "content" = null, "should_display_on_index" = 0 where "type" = 'query' and "should_display_on_index" = 1 and "family_hash" is null and "sequence" > 100 and "created_at" < '2019-01-01 00:00:00'
SQL
, $entry->content['sql']);

$this->assertSame('testbench', $entry->content['connection']);
}
}

0 comments on commit 36fca63

Please sign in to comment.