Skip to content

Commit be8039f

Browse files
committed
Merge pull request brianc#217 from danrzeppa/dan-temptables
Support for temporary tables.
2 parents 2173019 + 63e3cbb commit be8039f

6 files changed

Lines changed: 82 additions & 4 deletions

File tree

lib/dialect/mssql.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ Mssql.prototype.visitColumn = function(columnNode) {
185185

186186

187187
Mssql.prototype.visitCreate = function(create) {
188-
if (!isCreateIfNotExists(create)) {
188+
var isNotExists=isCreateIfNotExists(create)
189+
var isTemporary=isCreateTemporary(create)
190+
if (!isNotExists && !isTemporary) {
189191
return Mssql.super_.prototype.visitCreate.call(this, create);
190192
}
191193
// Implement our own create if not exists:
@@ -209,6 +211,7 @@ Mssql.prototype.visitCreate = function(create) {
209211
// if (schema) { whereClause+=' AND TABLE_SCHEMA = schemaResult.join(' ')}
210212
// Add some tests for this as well
211213

214+
if (!isNotExists) return createResult
212215
return ['IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES '+whereClause+') BEGIN '+createResult.join(' ')+' END'];
213216
};
214217

@@ -414,6 +417,10 @@ function isCreateIfNotExists(create){
414417
return true;
415418
};
416419

420+
function isCreateTemporary(create){
421+
return create.options.isTemporary
422+
};
423+
417424
function isDropIfExists(drop){
418425
if (drop.nodes.length==0) return false;
419426
if (drop.nodes[0].type!='IF EXISTS') return false;

lib/dialect/postgres.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ Postgres.prototype.visitCreate = function(create) {
249249
var table = this._queryNode.table;
250250
var col_nodes = table.columns.map(function(col) { return col.toNode(); });
251251

252-
var result = ['CREATE TABLE'];
252+
var result = ['CREATE TABLE'];
253+
if (create.options.isTemporary) result=['CREATE TEMPORARY TABLE']
253254
result = result.concat(create.nodes.map(this.visit.bind(this)));
254255
result.push(this.visit(table.toNode()));
255256
var primary_col_nodes = col_nodes.filter(function(n) {

lib/node/create.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33
var Node = require(__dirname);
44

55
module.exports = Node.define({
6-
type: 'CREATE'
6+
type: 'CREATE',
7+
8+
constructor: function(isTemporary) {
9+
Node.call(this);
10+
11+
this.options = { isTemporary: isTemporary};
12+
},
13+
714
});

lib/node/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ var Query = Node.define({
276276
this.add(createIndex);
277277
return createIndex;
278278
} else {
279-
return this.add(new Create());
279+
return this.add(new Create(this.table.isTemporary));
280280
}
281281
},
282282

lib/table.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var Table = function(config) {
1515
this._name = config.name;
1616
this._initialConfig = config;
1717
this.columnWhiteList = !!config.columnWhiteList;
18+
this.isTemporary=!!config.isTemporary
1819
this.snakeToCamel = !!config.snakeToCamel;
1920
this.columns = [];
2021
this.table = this;
@@ -130,6 +131,7 @@ Table.prototype.setSchema = function(schema) {
130131
};
131132

132133
Table.prototype.getName = function() {
134+
if (this.sql && this.sql.dialectName=="mssql" && this.isTemporary) return "#"+this._name;
133135
return this._name;
134136
};
135137

test/dialects/create-table-tests.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,64 @@ Harness.test({
320320
string: 'CREATE TABLE `membership` (`group_id` int, `user_id` int, `desc` varchar, PRIMARY KEY (`group_id`, `user_id`))',
321321
}
322322
});
323+
324+
// TEMPORARY TABLE TESTS
325+
326+
// This tests explicitly setting the isTemporary flag to false, as opposed to all the test above here which have it
327+
// as undefined.
328+
Harness.test({
329+
query: Table.define({
330+
name: 'post',
331+
columns: [{
332+
name: 'id',
333+
dataType: 'int'
334+
}],
335+
isTemporary:false
336+
}).create(),
337+
pg: {
338+
text : 'CREATE TABLE "post" ("id" int)',
339+
string: 'CREATE TABLE "post" ("id" int)'
340+
},
341+
sqlite: {
342+
text : 'CREATE TABLE "post" ("id" int)',
343+
string: 'CREATE TABLE "post" ("id" int)'
344+
},
345+
mysql: {
346+
text : 'CREATE TABLE `post` (`id` int)',
347+
string: 'CREATE TABLE `post` (`id` int)'
348+
},
349+
mssql: {
350+
text : 'CREATE TABLE [post] ([id] int)',
351+
string: 'CREATE TABLE [post] ([id] int)'
352+
},
353+
params: []
354+
});
355+
356+
Harness.test({
357+
query: Table.define({
358+
name: 'post',
359+
columns: [{
360+
name: 'id',
361+
dataType: 'int'
362+
}],
363+
isTemporary:true
364+
}).create(),
365+
pg: {
366+
text : 'CREATE TEMPORARY TABLE "post" ("id" int)',
367+
string: 'CREATE TEMPORARY TABLE "post" ("id" int)'
368+
},
369+
sqlite: {
370+
text : 'CREATE TEMPORARY TABLE "post" ("id" int)',
371+
string: 'CREATE TEMPORARY TABLE "post" ("id" int)'
372+
},
373+
mysql: {
374+
text : 'CREATE TEMPORARY TABLE `post` (`id` int)',
375+
string: 'CREATE TEMPORARY TABLE `post` (`id` int)'
376+
},
377+
//mssql: {
378+
// text : 'CREATE TABLE [#post] ([id] int)',
379+
// string: 'CREATE TABLE [#post] ([id] int)'
380+
//},
381+
params: []
382+
});
383+

0 commit comments

Comments
 (0)