Skip to content

Commit

Permalink
Support Boolean bindings on PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed May 15, 2022
1 parent 95d3e9c commit 79bda6b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
19 changes: 17 additions & 2 deletions src/Schema/Builders/ManagesViews.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,22 @@ protected function getQueryString($query)
return $query;
}

$bindings = $this->connection->prepareBindings($query->getBindings());
$bindings = $this->stringifyBindings(
$query->getBindings()
);

return Str::replaceArray('?', $bindings, $query->toSql());
}

/**
* Stringify the query bindings.
*
* @param array $bindings
* @return array
*/
protected function stringifyBindings(array $bindings)
{
$bindings = $this->connection->prepareBindings($bindings);

foreach ($bindings as &$binding) {
if (is_object($binding)) {
Expand All @@ -61,7 +76,7 @@ protected function getQueryString($query)
}
}

return Str::replaceArray('?', $bindings, $query->toSql());
return $bindings;
}

/**
Expand Down
21 changes: 20 additions & 1 deletion src/Schema/Builders/PostgresBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,26 @@

class PostgresBuilder extends Base
{
use ManagesViews;
use ManagesViews {
stringifyBindings as parentStringifyBindings;
}

/**
* Stringify the query bindings.
*
* @param array $bindings
* @return array
*/
protected function stringifyBindings(array $bindings)
{
foreach ($bindings as &$binding) {
if (is_bool($binding)) {
$binding = $binding ? 'true' : 'false';
}
}

return $this->parentStringifyBindings($bindings);
}

/**
* Get the bindings for a "Has View" statement.
Expand Down
4 changes: 3 additions & 1 deletion tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public function testCreateView()

public function testCreateViewWithColumns()
{
Schema::createView('active_users', 'select id from users where active = 1', ['key']);
$active = Schema::connection('testing')->getConnection()->getDriverName() === 'pgsql' ? 'true' : 1;

Schema::createView('active_users', "select id from users where active = $active", ['key']);

$users = DB::table('active_users')->get();
$this->assertCount(1, $users);
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function setUp(): void
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->tinyInteger('active');
$table->boolean('active');
$table->timestamps();
});

Expand Down

0 comments on commit 79bda6b

Please sign in to comment.