Skip to content

Add multiple connection support #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

Expand Down
19 changes: 18 additions & 1 deletion src/Objects/SqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class SqlQuery
*/
private $sql;

/**
* @var string
*/
private $connection;

/**
* @var array
*/
Expand All @@ -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;
}

/**
Expand All @@ -64,6 +71,16 @@ public function raw()
return $this->sql;
}

/**
* Get Connection Config
*
* @return string
*/
public function connection()
{
return $this->connection;
}

/**
* Get bindings.
*
Expand Down
5 changes: 3 additions & 2 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 6 additions & 2 deletions src/SqlLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Illuminate\Container\Container;
use Illuminate\Database\Events\QueryExecuted;

class SqlLogger
{
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace Mnabialek\LaravelSqlLogger\Tests;

use Illuminate\Contracts\Config\Repository;
Expand Down
14 changes: 14 additions & 0 deletions tests/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 13 additions & 13 deletions tests/Objects/SqlQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@ 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());
}

/** @test */
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());
}

/** @test */
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());
}

/** @test */
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());
}

Expand All @@ -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 = <<<EOF
SELECT * FROM tests WHERE a = '\'test' AND CONCAT('{$bindings[1]->toDateTimeString()}', '%'
Expand All @@ -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 = <<<EOF
SELECT * FROM tests WHERE a = '\'test' AND CONCAT('{$bindings[1]->toDateTimeString()}', '%'
Expand All @@ -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 = <<<EOF
SELECT * FROM tests WHERE a = '?' AND b = "?" AND c = '\'test' AND D = '\\?' AND e = "\"?" AND f = 52;
Expand All @@ -101,7 +101,7 @@ public function it_returns_valid_query_for_named_bindings()
SELECT * FROM tests WHERE a = ? AND b = :email AND c = ? AND D = :something AND true;
EOF;
$bindings = ["'test", 52, 'example', 53, 77];
$query = new SqlQuery(56, $sql, $bindings, 130);
$query = new SqlQuery(56, $sql, $bindings, 130, 'db');

$expectedSql = <<<EOF
SELECT * FROM tests WHERE a = '\'test' AND b = 52 AND c = 'example' AND D = 53 AND true;
Expand All @@ -117,7 +117,7 @@ public function it_returns_valid_query_for_multiple_named_bindings_in_other_orde
SELECT * FROM tests WHERE a = :email AND b = :something AND c = :test AND true;
EOF;
$bindings = [':test' => 'other value', ':email' => '[email protected]', ':something' => 'test'];
$query = new SqlQuery(56, $sql, $bindings, 130);
$query = new SqlQuery(56, $sql, $bindings, 130, 'db');

$expectedSql = <<<EOF
SELECT * FROM tests WHERE a = '[email protected]' AND b = 'test' AND c = 'other value' AND true;
Expand All @@ -133,7 +133,7 @@ public function it_returns_valid_query_when_empty_string_as_column_and_date_bind
SELECT id, '' AS title FROM test WHERE created_at >= :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 = <<<EOF
SELECT id, '' AS title FROM test WHERE created_at >= '2018-03-19 21:01:01' AND created_at <= '2018-03-19 22:01:01'
Expand All @@ -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' => '[email protected]', 'something' => 'test'];
$query = new SqlQuery(56, $sql, $bindings, 130);
$query = new SqlQuery(56, $sql, $bindings, 130, 'db');

$expectedSql = <<<EOF
SELECT * FROM tests WHERE a = '[email protected]' AND b = 'test';
Expand All @@ -167,7 +167,7 @@ public function it_leaves_null_values_not_changed()
EOF;

$bindings = [':email' => '[email protected]', 'something' => null, 'id' => 5];
$query = new SqlQuery(56, $sql, $bindings, 130);
$query = new SqlQuery(56, $sql, $bindings, 130, 'db');

$expectedSql = <<<EOF
UPDATE tests SET a = '[email protected]', b = null WHERE id=5;
Expand All @@ -184,7 +184,7 @@ public function it_converts_booleans_to_int()
EOF;

$bindings = [':archived' => true, ':active' => false];
$query = new SqlQuery(56, $sql, $bindings, 130);
$query = new SqlQuery(56, $sql, $bindings, 130, 'db');

$expectedSql = <<<EOF
SELECT * FROM users WHERE archived = 1 AND active = 0;
Expand Down
6 changes: 5 additions & 1 deletion tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ public function it_returns_valid_sql_query_object_when_version_is_below_5_2_0()
$query = 'SELECT * FROM everywhere WHERE user = ?';
$bindings = ['one', 2, 'three'];
$time = 516.32;
$connection = 'db';

$result = $queryObject->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 */
Expand All @@ -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);

Expand All @@ -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);

Expand Down
Loading