Skip to content
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

[5.5] Better temporary tables support #22110

Merged
Merged
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
Complete temporary tables support
  • Loading branch information
paulofreitas committed Nov 17, 2017
commit a3dc235779739835584904204fc0fa9d3f365e6c
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ class SqlServerGrammar extends Grammar
*/
protected $serials = ['tinyInteger', 'smallInteger', 'mediumInteger', 'integer', 'bigInteger'];

/**
* Wrap a table in keyword identifiers.
*
* @param \Illuminate\Database\Query\Expression|string $table
* @return string
*/
public function wrapTable($table)
{
if ($table instanceof Blueprint && $table->temporary) {
$this->setTablePrefix('#');
}

return parent::wrapTable($table);
}

/**
* Compile the query to determine if a table exists.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ public function testBasicCreateTableWithPrefix()
$this->assertEquals('create table `prefix_users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null)', $statements[0]);
}

public function testCreateTemporaryTable()
{
$blueprint = new Blueprint('users');
$blueprint->create();
$blueprint->temporary();
$blueprint->increments('id');
$blueprint->string('email');

$conn = $this->getConnection();
$conn->shouldReceive('getConfig')->andReturn(null);

$statements = $blueprint->toSql($conn, $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('create temporary table `users` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null)', $statements[0]);
}

public function testDropTable()
{
$blueprint = new Blueprint('users');
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public function testBasicCreateTable()
$this->assertEquals('alter table "users" add column "id" serial primary key not null, add column "email" varchar(255) not null', $statements[0]);
}

public function testCreateTemporaryTable()
{
$blueprint = new Blueprint('users');
$blueprint->create();
$blueprint->temporary();
$blueprint->increments('id');
$blueprint->string('email');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('create temporary table "users" ("id" serial primary key not null, "email" varchar(255) not null)', $statements[0]);
}

public function testDropTable()
{
$blueprint = new Blueprint('users');
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseSQLiteSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ public function testBasicCreateTable()
$this->assertEquals($expected, $statements);
}

public function testCreateTemporaryTable()
{
$blueprint = new Blueprint('users');
$blueprint->create();
$blueprint->temporary();
$blueprint->increments('id');
$blueprint->string('email');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('create temporary table "users" ("id" integer not null primary key autoincrement, "email" varchar not null)', $statements[0]);
}

public function testDropTable()
{
$blueprint = new Blueprint('users');
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ public function testBasicCreateTable()
$this->assertEquals('create table "prefix_users" ("id" int identity primary key not null, "email" nvarchar(255) not null)', $statements[0]);
}

public function testCreateTemporaryTable()
{
$blueprint = new Blueprint('users');
$blueprint->create();
$blueprint->temporary();
$blueprint->increments('id');
$blueprint->string('email');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('create table "#users" ("id" int identity primary key not null, "email" nvarchar(255) not null)', $statements[0]);
}

public function testDropTable()
{
$blueprint = new Blueprint('users');
Expand Down