From 91ed6c2b323907e8c0150ce870efd186c2630ea7 Mon Sep 17 00:00:00 2001 From: Chris Song Date: Sat, 4 Dec 2021 22:54:58 +0800 Subject: [PATCH] Add multiple connection support --- src/Formatter.php | 1 + src/Objects/SqlQuery.php | 19 ++++++++++++- src/Query.php | 5 ++-- src/SqlLogger.php | 8 ++++-- tests/ConfigTest.php | 1 - tests/FormatterTest.php | 14 ++++++++++ tests/Objects/SqlQueryTest.php | 26 ++++++++--------- tests/QueryTest.php | 6 +++- tests/SqlLoggerTest.php | 51 ++++++++++++++++++++++++---------- tests/WriterTest.php | 28 +++++++++---------- 10 files changed, 110 insertions(+), 49 deletions(-) diff --git a/src/Formatter.php b/src/Formatter.php index 65bbf77..1eed91a 100644 --- a/src/Formatter.php +++ b/src/Formatter.php @@ -49,6 +49,7 @@ public function getLine(SqlQuery $query) '[query_time]' => $this->time($query->time()), '[query]' => $this->queryLine($query), '[separator]' => $this->separatorLine(), + '[connection]' => $query->connection(), '\n' => PHP_EOL, ]; diff --git a/src/Objects/SqlQuery.php b/src/Objects/SqlQuery.php index f4f45e9..4a8af1a 100644 --- a/src/Objects/SqlQuery.php +++ b/src/Objects/SqlQuery.php @@ -18,6 +18,11 @@ class SqlQuery */ private $sql; + /** + * @var string + */ + private $connection; + /** * @var array */ @@ -35,13 +40,15 @@ class SqlQuery * @param string $sql * @param array|null $bindings * @param float $time + * @param string $connection */ - public function __construct($number, $sql, array $bindings = null, $time) + public function __construct($number, $sql, array $bindings = null, $time, $connection) { $this->number = $number; $this->sql = $sql; $this->bindings = $bindings ?: []; $this->time = $time; + $this->connection = $connection; } /** @@ -64,6 +71,16 @@ public function raw() return $this->sql; } + /** + * Get Connection Config + * + * @return string + */ + public function connection() + { + return $this->connection; + } + /** * Get bindings. * diff --git a/src/Query.php b/src/Query.php index 1266479..fdc0f86 100644 --- a/src/Query.php +++ b/src/Query.php @@ -30,15 +30,16 @@ public function __construct(Version $version) * * @return SqlQuery */ - public function get($number, $query, array $bindings = null, $time = null) + public function get($number, $query, array $bindings = null, $time = null, $connection=null) { // for Laravel/Lumen 5.2+ $query is object and it holds all the data if ($this->version->min('5.2.0')) { $bindings = $query->bindings; $time = $query->time; + $connection = $query->connectionName; $query = $query->sql; } - return new SqlQuery($number, $query, $bindings, $time); + return new SqlQuery($number, $query, $bindings, $time, $connection); } } diff --git a/src/SqlLogger.php b/src/SqlLogger.php index 7dacdef..cee48bd 100644 --- a/src/SqlLogger.php +++ b/src/SqlLogger.php @@ -4,6 +4,7 @@ use Exception; use Illuminate\Container\Container; +use Illuminate\Database\Events\QueryExecuted; class SqlLogger { @@ -53,9 +54,12 @@ public function __construct(Container $app, Query $query, Writer $writer) public function log($query, array $bindings = null, $time = null) { ++$this->queryNumber; - try { - $sqlQuery = $this->query->get($this->queryNumber, $query, $bindings, $time); + if ($query instanceof QueryExecuted) { + $sqlQuery = $this->query->get($this->queryNumber, $query, $bindings, $time, $query->connectionName); + } else { + $sqlQuery = $this->query->get($this->queryNumber, $query, $bindings, $time, null); + } $this->writer->save($sqlQuery); } catch (Exception $e) { $this->app['log']->notice("Cannot log query nr {$this->queryNumber}. Exception:" . PHP_EOL . $e); diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 407e8ae..6b3f2b5 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -1,5 +1,4 @@ shouldReceive('number')->once()->withNoArgs()->andReturn($number); $query->shouldReceive('get')->once()->withNoArgs()->andReturn($sql); $query->shouldReceive('time')->once()->withNoArgs()->andReturn($time); + $query->shouldReceive('connection')->once()->withNoArgs()->andReturn($connection); $formatter = new Formatter($app, $config); $result = $formatter->getLine($query); @@ -77,9 +79,11 @@ public function it_formats_line_in_valid_way_when_custom_entry_format_was_used() $number = 434; $time = 617.24; $sql = 'SELECT * FROM somewhere'; + $connection = 'db'; $query->shouldReceive('number')->once()->withNoArgs()->andReturn($number); $query->shouldReceive('get')->once()->withNoArgs()->andReturn($sql); $query->shouldReceive('time')->once()->withNoArgs()->andReturn($time); + $query->shouldReceive('connection')->once()->withNoArgs()->andReturn($connection); $formatter = new Formatter($app, $config); $result = $formatter->getLine($query); @@ -119,9 +123,11 @@ public function it_formats_line_in_valid_way_when_seconds_are_used_and_running_v $number = 434; $time = 617.24; $sql = 'SELECT * FROM somewhere'; + $connection = 'db'; $query->shouldReceive('number')->once()->withNoArgs()->andReturn($number); $query->shouldReceive('get')->once()->withNoArgs()->andReturn($sql); $query->shouldReceive('time')->once()->withNoArgs()->andReturn($time); + $query->shouldReceive('connection')->once()->withNoArgs()->andReturn($connection); $formatter = new Formatter($app, $config); $result = $formatter->getLine($query); @@ -158,9 +164,11 @@ public function it_formats_line_in_valid_way_when_milliseconds_are_used_and_runn $number = 434; $time = 617.24; $sql = 'SELECT * FROM somewhere'; + $connection = 'db'; $query->shouldReceive('number')->once()->withNoArgs()->andReturn($number); $query->shouldReceive('get')->once()->withNoArgs()->andReturn($sql); $query->shouldReceive('time')->once()->withNoArgs()->andReturn($time); + $query->shouldReceive('connection')->once()->withNoArgs()->andReturn($connection); $formatter = new Formatter($app, $config); $result = $formatter->getLine($query); @@ -201,9 +209,11 @@ public function it_formats_line_in_valid_way_when_milliseconds_are_used_and_runn $number = 434; $time = 617.24; $sql = 'SELECT * FROM somewhere'; + $connection = 'db'; $query->shouldReceive('number')->once()->withNoArgs()->andReturn($number); $query->shouldReceive('get')->once()->withNoArgs()->andReturn($sql); $query->shouldReceive('time')->once()->withNoArgs()->andReturn($time); + $query->shouldReceive('connection')->once()->withNoArgs()->andReturn($connection); $formatter = new Formatter($app, $config); $result = $formatter->getLine($query); @@ -251,10 +261,12 @@ public function it_replaces_new_lines_in_query_by_spaces_when_config_set_to_true SELECT * FROM somewhere WHERE name = ' ' SQL; + $connection = 'db'; $query->shouldReceive('number')->once()->withNoArgs()->andReturn($number); $query->shouldReceive('get')->once()->withNoArgs()->andReturn($sql); $query->shouldReceive('time')->once()->withNoArgs()->andReturn($time); + $query->shouldReceive('connection')->once()->withNoArgs()->andReturn($connection); $formatter = new Formatter($app, $config); $result = $formatter->getLine($query); @@ -303,10 +315,12 @@ public function it_does_not_replace_new_lines_in_query_when_config_set_to_false( somewhere WHERE name = ' ' SQL; + $connection = 'db'; $query->shouldReceive('number')->once()->withNoArgs()->andReturn($number); $query->shouldReceive('get')->once()->withNoArgs()->andReturn($sql); $query->shouldReceive('time')->once()->withNoArgs()->andReturn($time); + $query->shouldReceive('connection')->once()->withNoArgs()->andReturn($connection); $formatter = new Formatter($app, $config); $result = $formatter->getLine($query); diff --git a/tests/Objects/SqlQueryTest.php b/tests/Objects/SqlQueryTest.php index 7f4b4d7..de32d93 100644 --- a/tests/Objects/SqlQueryTest.php +++ b/tests/Objects/SqlQueryTest.php @@ -12,7 +12,7 @@ class SqlQueryTest extends UnitTestCase public function it_returns_valid_number() { $value = 56; - $query = new SqlQuery($value, 'test', [], 130); + $query = new SqlQuery($value, 'test', [], 130, 'db'); $this->assertSame($value, $query->number()); } @@ -20,7 +20,7 @@ public function it_returns_valid_number() public function it_returns_valid_raw_query() { $value = 'SELECT * FROM tests WHERE a = ?'; - $query = new SqlQuery(56, $value, ['test'], 130); + $query = new SqlQuery(56, $value, ['test'], 130, 'db'); $this->assertSame($value, $query->raw()); } @@ -28,7 +28,7 @@ public function it_returns_valid_raw_query() public function it_returns_valid_bindings_array() { $value = ['one', new \DateTime(), 3]; - $query = new SqlQuery(56, 'test', $value, 130); + $query = new SqlQuery(56, 'test', $value, 130, 'db'); $this->assertSame($value, $query->bindings()); } @@ -36,7 +36,7 @@ public function it_returns_valid_bindings_array() public function it_returns_valid_time() { $value = 130; - $query = new SqlQuery(56, 'test', [], $value); + $query = new SqlQuery(56, 'test', [], $value, 'db'); $this->assertSame($value, $query->time()); } @@ -49,7 +49,7 @@ public function it_returns_valid_query_with_replaced_bindings() EOF; $bindings = ["'test", Carbon::yesterday(), new \DateTime('tomorrow'), 453, 67.23]; - $query = new SqlQuery(56, $sql, $bindings, 130); + $query = new SqlQuery(56, $sql, $bindings, 130, 'db'); $expectedSql = <<toDateTimeString()}', '%' @@ -68,7 +68,7 @@ public function it_returns_valid_query_with_replaced_bindings_for_immutable_date EOF; $bindings = ["'test", Carbon::yesterday(), new \DateTimeImmutable('tomorrow'), 453, 67.23]; - $query = new SqlQuery(56, $sql, $bindings, 130); + $query = new SqlQuery(56, $sql, $bindings, 130, 'db'); $expectedSql = <<toDateTimeString()}', '%' @@ -85,7 +85,7 @@ public function it_returns_valid_query_when_question_mark_in_quotes() SELECT * FROM tests WHERE a = '?' AND b = "?" AND c = ? AND D = '\\?' AND e = "\"?" AND f = ?; EOF; $bindings = ["'test", 52]; - $query = new SqlQuery(56, $sql, $bindings, 130); + $query = new SqlQuery(56, $sql, $bindings, 130, 'db'); $expectedSql = << 'other value', ':email' => 'test@example.com', ':something' => 'test']; - $query = new SqlQuery(56, $sql, $bindings, 130); + $query = new SqlQuery(56, $sql, $bindings, 130, 'db'); $expectedSql = <<= :from AND created_at <= :to EOF; $bindings = [':from' => '2018-03-19 21:01:01', ':to' => '2018-03-19 22:01:01']; - $query = new SqlQuery(56, $sql, $bindings, 130); + $query = new SqlQuery(56, $sql, $bindings, 130, 'db'); $expectedSql = <<= '2018-03-19 21:01:01' AND created_at <= '2018-03-19 22:01:01' @@ -150,7 +150,7 @@ public function it_handles_both_colon_and_non_colon_parameters() EOF; // one binding name stats with colon, other without it - both should work $bindings = [':email' => 'test@example.com', 'something' => 'test']; - $query = new SqlQuery(56, $sql, $bindings, 130); + $query = new SqlQuery(56, $sql, $bindings, 130, 'db'); $expectedSql = << 'test@example.com', 'something' => null, 'id' => 5]; - $query = new SqlQuery(56, $sql, $bindings, 130); + $query = new SqlQuery(56, $sql, $bindings, 130, 'db'); $expectedSql = << true, ':active' => false]; - $query = new SqlQuery(56, $sql, $bindings, 130); + $query = new SqlQuery(56, $sql, $bindings, 130, 'db'); $expectedSql = <<get($number, $query, $bindings, $time); + $result = $queryObject->get($number, $query, $bindings, $time, $connection); $this->assertInstanceOf(SqlQuery::class, $result); $this->assertSame($number, $result->number()); $this->assertSame($query, $result->raw()); $this->assertSame($bindings, $result->bindings()); $this->assertSame($time, $result->time()); + $this->assertSame($connection, $result->connection()); } /** @test */ @@ -45,6 +47,7 @@ public function it_returns_valid_sql_query_object_when_version_is_5_2_0() $dataObject->sql = 'SELECT * FROM everywhere WHERE user = ?'; $dataObject->bindings = ['one', 2, 'three']; $dataObject->time = 516.32; + $dataObject->connectionName = 'db'; $result = $queryObject->get($number, $dataObject); @@ -68,6 +71,7 @@ public function it_returns_valid_sql_query_object_when_bindings_are_null() $dataObject->sql = 'SELECT * FROM everywhere WHERE user = ?'; $dataObject->bindings = null; $dataObject->time = 516.32; + $dataObject->connectionName = 'db'; $result = $queryObject->get($number, $dataObject); diff --git a/tests/SqlLoggerTest.php b/tests/SqlLoggerTest.php index b5fb443..eb648a9 100644 --- a/tests/SqlLoggerTest.php +++ b/tests/SqlLoggerTest.php @@ -4,6 +4,7 @@ use ArrayAccess; use Illuminate\Container\Container; +use Illuminate\Database\Events\QueryExecuted; use Mnabialek\LaravelSqlLogger\Objects\SqlQuery; use Mnabialek\LaravelSqlLogger\Query; use Mnabialek\LaravelSqlLogger\SqlLogger; @@ -44,12 +45,16 @@ protected function setUp() /** @test */ public function it_runs_writer_with_valid_query() { - $query = 'SELECT * FROM somewhere'; + $sql = 'SELECT * FROM somewhere'; $bindings = ['one', 2]; $time = 5412; + $connection = Mockery::mock(stdClass::class); + $connection->shouldReceive('getName')->withNoArgs()->andReturn("db"); + $query = new QueryExecuted($sql, $bindings, $time, $connection); + $connectionName = 'db'; - $sqlQuery = new SqlQuery(4, 'anything', [], 3.54); - $this->query->shouldReceive('get')->once()->with(1, $query, $bindings, $time) + $sqlQuery = new SqlQuery(4, 'anything', [], 3.54, 'db'); + $this->query->shouldReceive('get')->once()->with(1, $query, $bindings, $time, $connectionName) ->andReturn($sqlQuery); $this->writer->shouldReceive('save')->once()->with($sqlQuery); @@ -60,21 +65,29 @@ public function it_runs_writer_with_valid_query() /** @test */ public function it_uses_valid_query_number_for_multiple_queries() { - $query = 'SELECT * FROM somewhere'; + $sql = 'SELECT * FROM somewhere'; $bindings = ['one', 2]; $time = 5412; + $connection = Mockery::mock(stdClass::class); + $connection->shouldReceive('getName')->withNoArgs()->andReturn("db"); + $query = new QueryExecuted($sql, $bindings, $time, $connection); + $connectionName = 'db'; - $query2 = 'SELECT * FROM world'; + $sql2 = 'SELECT * FROM world'; $bindings2 = ['three', 4]; $time2 = 45.43; + $connection2 = Mockery::mock(stdClass::class); + $connection2->shouldReceive('getName')->withNoArgs()->andReturn("db"); + $query2 = new QueryExecuted($sql2, $bindings2, $time2, $connection2); + $connectionName2 = 'db'; - $sqlQuery = new SqlQuery(4, 'anything', [], 3.54); - $this->query->shouldReceive('get')->once()->with(1, $query, $bindings, $time) + $sqlQuery = new SqlQuery(4, 'anything', [], 3.54, 'db'); + $this->query->shouldReceive('get')->once()->with(1, $query, $bindings, $time, $connectionName ) ->andReturn($sqlQuery); $this->writer->shouldReceive('save')->once()->with($sqlQuery); - $sqlQuery2 = new SqlQuery(6, 'anything2', [], 41.23); - $this->query->shouldReceive('get')->once()->with(2, $query2, $bindings2, $time2) + $sqlQuery2 = new SqlQuery(6, 'anything2', [], 41.23, 'db'); + $this->query->shouldReceive('get')->once()->with(2, $query2, $bindings2, $time2, $connectionName2) ->andReturn($sqlQuery2); $this->writer->shouldReceive('save')->once()->with($sqlQuery2); @@ -86,18 +99,26 @@ public function it_uses_valid_query_number_for_multiple_queries() /** @test */ public function it_logs_thrown_exception_and_continue_working_for_next_query() { - $query = 'SELECT * FROM somewhere'; + $sql = 'SELECT * FROM somewhere'; $bindings = ['one', 2]; $time = 5412; + $connection = Mockery::mock(stdClass::class); + $connection->shouldReceive('getName')->withNoArgs()->andReturn("db"); + $query = new QueryExecuted($sql, $bindings, $time, $connection); + $connectionName = 'db'; - $query2 = 'SELECT * FROM world'; + $sql2 = 'SELECT * FROM world'; $bindings2 = ['three', 4]; $time2 = 45.43; + $connection2 = Mockery::mock(stdClass::class); + $connection2->shouldReceive('getName')->withNoArgs()->andReturn("db"); + $query2 = new QueryExecuted($sql2, $bindings2, $time2, $connection2); + $connectionName2 = 'db'; $exception = new \Exception('Sample message'); - $sqlQuery = new SqlQuery(4, 'anything', [], 3.54); - $this->query->shouldReceive('get')->once()->with(1, $query, $bindings, $time) + $sqlQuery = new SqlQuery(4, 'anything', [], 3.54, 'db'); + $this->query->shouldReceive('get')->once()->with(1, $query, $bindings, $time, $connectionName) ->andReturn($sqlQuery); $this->writer->shouldReceive('save')->once()->with($sqlQuery)->andThrow($exception); @@ -105,8 +126,8 @@ public function it_logs_thrown_exception_and_continue_working_for_next_query() $this->app->shouldReceive('offsetGet')->once()->with('log')->andReturn($log); $log->shouldReceive('notice')->once()->with("Cannot log query nr 1. Exception:\n" . $exception); - $sqlQuery2 = new SqlQuery(6, 'anything2', [], 41.23); - $this->query->shouldReceive('get')->once()->with(2, $query2, $bindings2, $time2) + $sqlQuery2 = new SqlQuery(6, 'anything2', [], 41.23, 'db'); + $this->query->shouldReceive('get')->once()->with(2, $query2, $bindings2, $time2, $connectionName2) ->andReturn($sqlQuery2); $this->writer->shouldReceive('save')->once()->with($sqlQuery2); diff --git a/tests/WriterTest.php b/tests/WriterTest.php index 5ef083e..a9fa58d 100644 --- a/tests/WriterTest.php +++ b/tests/WriterTest.php @@ -69,7 +69,7 @@ protected function tearDown() /** @test */ public function it_creates_directory_if_it_does_not_exist_for_1st_query() { - $query = new SqlQuery(1, 'test', [], 5.41); + $query = new SqlQuery(1, 'test', [], 5.41, 'db'); $this->formatter->shouldReceive('getLine')->once()->with($query); $this->config->shouldReceive('logAllQueries')->once()->withNoArgs()->andReturn(false); $this->config->shouldReceive('logSlowQueries')->once()->withNoArgs()->andReturn(false); @@ -83,7 +83,7 @@ public function it_creates_directory_if_it_does_not_exist_for_1st_query() /** @test */ public function it_does_not_create_directory_if_it_does_not_exist_for_2nd_query() { - $query = new SqlQuery(2, 'test', [], 5.41); + $query = new SqlQuery(2, 'test', [], 5.41, 'db'); $this->formatter->shouldReceive('getLine')->once()->with($query); $this->config->shouldReceive('logAllQueries')->once()->withNoArgs()->andReturn(false); $this->config->shouldReceive('logSlowQueries')->once()->withNoArgs()->andReturn(false); @@ -99,7 +99,7 @@ public function it_creates_log_file() $lineContent = 'Sample log line'; $expectedFileName = $this->now->toDateString() . '-log.sql'; - $query = new SqlQuery(1, 'test', [], 5.41); + $query = new SqlQuery(1, 'test', [], 5.41, 'db'); $this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); $this->config->shouldReceive('logAllQueries')->once()->withNoArgs()->andReturn(true); $this->config->shouldReceive('allQueriesPattern')->once()->withNoArgs()->andReturn('#.*#i'); @@ -124,7 +124,7 @@ public function it_appends_to_existing_log_file() $lineContent = 'Sample log line'; - $query = new SqlQuery(1, 'test', [], 5.41); + $query = new SqlQuery(1, 'test', [], 5.41, 'db'); $this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); $this->config->shouldReceive('logAllQueries')->once()->withNoArgs()->andReturn(true); $this->config->shouldReceive('allQueriesPattern')->once()->withNoArgs()->andReturn('#.*#i'); @@ -149,7 +149,7 @@ public function it_replaces_current_file_content_for_1st_query_when_overriding_i $lineContent = 'Sample log line'; - $query = new SqlQuery(1, 'test', [], 5.41); + $query = new SqlQuery(1, 'test', [], 5.41, 'db'); $this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); $this->config->shouldReceive('logAllQueries')->once()->withNoArgs()->andReturn(true); $this->config->shouldReceive('allQueriesPattern')->once()->withNoArgs()->andReturn('#.*#i'); @@ -174,7 +174,7 @@ public function it_appends_to_current_file_content_for_2nd_query_when_overriding $lineContent = 'Sample log line'; - $query = new SqlQuery(2, 'test', [], 5.41); + $query = new SqlQuery(2, 'test', [], 5.41, 'db'); $this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); $this->config->shouldReceive('logAllQueries')->once()->withNoArgs()->andReturn(true); $this->config->shouldReceive('allQueriesPattern')->once()->withNoArgs()->andReturn('#.*#i'); @@ -195,7 +195,7 @@ public function it_adds_query_to_slow_log_when_its_greater_than_given_time() $lineContent = 'Sample slow log line'; $expectedFileName = $this->now->toDateString() . '-slow-log.sql'; - $query = new SqlQuery(1, 'test', [], 5.41); + $query = new SqlQuery(1, 'test', [], 5.41, 'db'); $this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); $this->config->shouldReceive('logAllQueries')->once()->withNoArgs()->andReturn(false); $this->config->shouldReceive('logSlowQueries')->once()->withNoArgs()->andReturn(true); @@ -215,7 +215,7 @@ public function it_does_not_add_query_to_slow_log_when_its_lower_than_given_time { $lineContent = 'Sample slow log line'; - $query = new SqlQuery(1, 'test', [], 5.41); + $query = new SqlQuery(1, 'test', [], 5.41, 'db'); $this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); $this->config->shouldReceive('logAllQueries')->once()->withNoArgs()->andReturn(false); $this->config->shouldReceive('logSlowQueries')->once()->withNoArgs()->andReturn(true); @@ -233,7 +233,7 @@ public function it_creates_2_files_when_both_log_set_to_true() $expectedFileName = $this->now->toDateString() . '-log.sql'; $expectedSlowFileName = $this->now->toDateString() . '-slow-log.sql'; - $query = new SqlQuery(1, 'test', [], 5.41); + $query = new SqlQuery(1, 'test', [], 5.41, 'db'); $this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); $this->config->shouldReceive('logAllQueries')->once()->withNoArgs()->andReturn(true); $this->config->shouldReceive('allQueriesPattern')->once()->withNoArgs()->andReturn('#.*#i'); @@ -261,7 +261,7 @@ public function it_saves_select_query_to_file_when_pattern_set_to_select_queries $expectedFileName = $this->now->toDateString() . '-log.sql'; $expectedSlowFileName = $this->now->toDateString() . '-slow-log.sql'; - $query = new SqlQuery(1, 'select * FROM test', [], 5.41); + $query = new SqlQuery(1, 'select * FROM test', [], 5.41, 'db'); $this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); $this->config->shouldReceive('logAllQueries')->once()->withNoArgs()->andReturn(true); $this->config->shouldReceive('allQueriesPattern')->once()->withNoArgs()->andReturn('#^SELECT .*$#i'); @@ -287,7 +287,7 @@ public function it_doesnt_save_select_query_to_file_when_pattern_set_to_insert_o { $lineContent = 'Sample log line'; - $query = new SqlQuery(1, 'select * FROM test', [], 5.41); + $query = new SqlQuery(1, 'select * FROM test', [], 5.41, 'db'); $this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); $this->config->shouldReceive('logAllQueries')->once()->withNoArgs()->andReturn(true); $this->config->shouldReceive('allQueriesPattern')->once()->withNoArgs()->andReturn('#^(?:UPDATE |INSERT ).*$#i'); @@ -307,7 +307,7 @@ public function it_saves_insert_query_to_file_when_pattern_set_to_insert_or_upda $expectedFileName = $this->now->toDateString() . '-log.sql'; $expectedSlowFileName = $this->now->toDateString() . '-slow-log.sql'; - $query = new SqlQuery(1, 'INSERT INTO test(one, two, three) values(?, ?, ?)', [], 5.41); + $query = new SqlQuery(1, 'INSERT INTO test(one, two, three) values(?, ?, ?)', [], 5.41, 'db'); $this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); $this->config->shouldReceive('logAllQueries')->once()->withNoArgs()->andReturn(true); $this->config->shouldReceive('allQueriesPattern')->once()->withNoArgs()->andReturn('#^(?:UPDATE |INSERT ).*$#i'); @@ -335,7 +335,7 @@ public function it_uses_raw_query_without_bindings_when_using_query_pattern() $expectedFileName = $this->now->toDateString() . '-log.sql'; $expectedSlowFileName = $this->now->toDateString() . '-slow-log.sql'; - $query = new SqlQuery(1, 'UPDATE test SET x = ? WHERE id = ?', [2, 3], 5.41); + $query = new SqlQuery(1, 'UPDATE test SET x = ? WHERE id = ?', [2, 3], 5.41, 'db'); $this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); $this->config->shouldReceive('logAllQueries')->once()->withNoArgs()->andReturn(true); $this->config->shouldReceive('allQueriesPattern')->once()->withNoArgs()->andReturn('#^(?:UPDATE test SET x = \? |INSERT ).*$#i'); @@ -362,7 +362,7 @@ public function it_uses_different_patterns_for_log_and_slow_log_queries() $lineContent = 'Sample log line'; $expectedFileName = $this->now->toDateString() . '-log.sql'; - $query = new SqlQuery(1, 'UPDATE test SET x = ? WHERE id = ?', [2, 3], 5.41); + $query = new SqlQuery(1, 'UPDATE test SET x = ? WHERE id = ?', [2, 3], 5.41, 'db'); $this->formatter->shouldReceive('getLine')->once()->with($query)->andReturn($lineContent); $this->config->shouldReceive('logAllQueries')->once()->withNoArgs()->andReturn(true); $this->config->shouldReceive('allQueriesPattern')->once()->withNoArgs()->andReturn('#^(?:UPDATE test SET x = \? |INSERT ).*$#i');