diff --git a/lib/dialects/mysql/schema/table.js b/lib/dialects/mysql/schema/table.js index 5edff696c..ebe5908bf 100644 --- a/lib/dialects/mysql/schema/table.js +++ b/lib/dialects/mysql/schema/table.js @@ -24,8 +24,9 @@ function TableCompiler_MySQL() { } inherits(TableCompiler_MySQL, Schema.TableCompiler); -TableCompiler_MySQL.prototype.createQuery = function(columns) { - var conn = {}, sql = (this._createIfNot ? 'create table if not exists ' : 'create table ') + this.tableName() + ' (' + columns.sql.join(', ') + ')'; +TableCompiler_MySQL.prototype.createQuery = function(columns, ifNot) { + var createStatement = ifNot ? 'create table if not exists ' : 'create table '; + var conn = {}, sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')'; // Check if the connection settings are set. if (client.connectionSettings) { @@ -37,9 +38,9 @@ TableCompiler_MySQL.prototype.createQuery = function(columns) { var engine = this.single.engine || ''; // var conn = builder.client.connectionSettings; - if (charset) sql += ' default character set ' + charset; + if (charset) sql += ' default character set ' + charset; if (collation) sql += ' collate ' + collation; - if (engine) sql += ' engine = ' + engine; + if (engine) sql += ' engine = ' + engine; var hasComment = this.single.comment != void 0; if (hasComment) { diff --git a/lib/dialects/oracle/schema/table.js b/lib/dialects/oracle/schema/table.js index 10e1703b1..a13f3c3ff 100644 --- a/lib/dialects/oracle/schema/table.js +++ b/lib/dialects/oracle/schema/table.js @@ -1,13 +1,9 @@ // Oracle Table Builder & Compiler // ------- -'use strict'; - module.exports = function(client) { -var _ = require('lodash'); var inherits = require('inherits'); var Schema = require('../../../schema'); -var helpers = require('../../../helpers'); var utils = require('../utils'); // Table Builder @@ -45,9 +41,10 @@ TableCompiler_Oracle.prototype.compileAdd = function(builder) { }; // Adds the "create" query to the query sequence. -TableCompiler_Oracle.prototype.createQuery = function(columns) { +TableCompiler_Oracle.prototype.createQuery = function(columns, ifNot) { + var createStatement = ifNot ? 'create table if not exists ' : 'create table '; this.pushQuery({ - sql: 'create table ' + this.tableName() + ' (' + columns.sql.join(', ') + ')', + sql: createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')', bindings: columns.bindings }); if (this.single.comment) this.comment(this.single.comment); diff --git a/lib/dialects/postgres/schema/table.js b/lib/dialects/postgres/schema/table.js index 167d8ac80..e679ecc85 100644 --- a/lib/dialects/postgres/schema/table.js +++ b/lib/dialects/postgres/schema/table.js @@ -38,9 +38,10 @@ TableCompiler_PG.prototype.compileAdd = function(builder) { }; // Adds the "create" query to the query sequence. -TableCompiler_PG.prototype.createQuery = function(columns) { +TableCompiler_PG.prototype.createQuery = function(columns, ifNot) { + var createStatement = ifNot ? 'create table if not exists ' : 'create table '; this.pushQuery({ - sql: (this._createIfNot ? 'create table if not exists ' : 'create table ') + this.tableName() + ' (' + columns.sql.join(', ') + ')', + sql: createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')', bindings: columns.bindings }); var hasComment = _.has(this.single, 'comment'); diff --git a/lib/dialects/sqlite3/schema/table.js b/lib/dialects/sqlite3/schema/table.js index c2b902fa0..8e4558861 100644 --- a/lib/dialects/sqlite3/schema/table.js +++ b/lib/dialects/sqlite3/schema/table.js @@ -28,8 +28,9 @@ function TableCompiler_SQLite3() { inherits(TableCompiler_SQLite3, Schema.TableCompiler); // Create a new table. -TableCompiler_SQLite3.prototype.createQuery = function(columns) { - var sql = (this._createIfNot ? 'create table if not exists ' : 'create table ') + this.tableName() + ' (' + columns.sql.join(', '); +TableCompiler_SQLite3.prototype.createQuery = function(columns, ifNot) { + var createStatement = ifNot ? 'create table if not exists ' : 'create table '; + var sql = createStatement + this.tableName() + ' (' + columns.sql.join(', '); // SQLite forces primary keys to be added when the table is initially created // so we will need to check for a primary key commands and add the columns diff --git a/lib/dialects/websql/runner.js b/lib/dialects/websql/runner.js index a499f3371..7b9f7393b 100644 --- a/lib/dialects/websql/runner.js +++ b/lib/dialects/websql/runner.js @@ -10,10 +10,9 @@ var Runner_SQLite3 = client.Runner; var inherits = require('inherits'); var _ = require('lodash'); -var SKIP = '!!!SKIP!!!'; + // Inherit from the `Runner` constructor's prototype, // so we can add the correct `then` method. - function Runner_WebSQL() { Runner_SQLite3.apply(this, arguments); } @@ -39,9 +38,12 @@ Runner_WebSQL.prototype._query = Promise.method(function(obj) { }); }); }); -Runner_WebSQL.prototype._beginTransaction = SKIP; -Runner_WebSQL.prototype._commitTransaction = SKIP; -Runner_WebSQL.prototype._rollbackTransaction = SKIP; + +// Null out the transaction statements so they aren't run. +Runner_WebSQL.prototype._beginTransaction = null; +Runner_WebSQL.prototype._commitTransaction = null; +Runner_WebSQL.prototype._rollbackTransaction = null; + // Ensures the response is returned in the same format as other clients. Runner_WebSQL.prototype.processResponse = function(obj) { var resp = obj.response; diff --git a/lib/runner.js b/lib/runner.js index 1b263b32c..c168ba908 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -175,7 +175,7 @@ Runner.prototype.startTransaction = Promise.method(function() { .then(function(connection) { this.connection = connection; this.transaction = true; - return this.query({sql: this._beginTransaction}); + return (this._beginTransaction && this.query({sql: this._beginTransaction})); }).thenReturn(this); }); @@ -223,10 +223,10 @@ Runner.prototype.finishTransaction = Promise.method(function(action, containerOb }); Runner.prototype.commitTransaction = function() { - return this.query({sql: this._commitTransaction}); + return this._commitTransaction && this.query({sql: this._commitTransaction}); }; Runner.prototype.rollbackTransaction = function() { - return this.query({sql: this._rollbackTransaction}); + return this._rollbackTransaction && this.query({sql: this._rollbackTransaction}); }; // Cleanup the connection as necessary, if the `_connection` was diff --git a/lib/schema/compiler.js b/lib/schema/compiler.js index 57dfc8aaa..2cf3f0e69 100644 --- a/lib/schema/compiler.js +++ b/lib/schema/compiler.js @@ -4,17 +4,12 @@ function SchemaCompiler(builder) { this.builder = builder; this.initCompiler(); - this._createIfNot = false; } function buildTable(type) { return function(tableName, fn) { var TableBuilder = this.client.TableBuilder; - var builder = new TableBuilder(type, tableName, fn); - if (this._createIfNot){ - builder._createIfNot = true; - } - var sql = builder.toSQL(); + var sql = new TableBuilder(type, tableName, fn).toSQL(); for (var i = 0, l = sql.length; i < l; i++) { this.sequence.push(sql[i]); } @@ -22,11 +17,9 @@ function buildTable(type) { } SchemaCompiler.prototype.createTable = buildTable('create'); -SchemaCompiler.prototype.createTableIfNotExists = function () { - this._createIfNot = true; - this.createTable.apply(this, arguments); -}; +SchemaCompiler.prototype.createTableIfNotExists = buildTable('createIfNot'); SchemaCompiler.prototype.alterTable = buildTable('alter'); + SchemaCompiler.prototype.dropTable = function(tableName) { this.pushQuery('drop table ' + this.formatter.wrap(tableName)); }; diff --git a/lib/schema/tablecompiler.js b/lib/schema/tablecompiler.js index 0b3b60713..2036ef388 100644 --- a/lib/schema/tablecompiler.js +++ b/lib/schema/tablecompiler.js @@ -9,7 +9,6 @@ function TableCompiler(tableBuilder) { this.tableNameRaw = tableBuilder._tableName; this.single = tableBuilder._single; this.grouped = _.groupBy(tableBuilder._statements, 'grouping'); - this._createIfNot = tableBuilder._createIfNot; this.initCompiler(); } @@ -25,15 +24,20 @@ TableCompiler.prototype.toSQL = function() { // If this is a table "creation", we need to first run through all // of the columns to build them into a single string, // and then run through anything else and push it to the query sequence. -TableCompiler.prototype.create = function() { +TableCompiler.prototype.create = function(ifNot) { var columns = this.getColumns(); var columnTypes = this.getColumnTypes(columns); - this.createQuery(columnTypes); + this.createQuery(columnTypes, ifNot); this.columnQueries(columns); delete this.single.comment; this.alterTable(); }; +// Only create the table if it doesn't exist. +TableCompiler.prototype.createIfNot = function() { + this.create(true); +}; + // If we're altering the table, we need to one-by-one // go through and handle each of the queries associated // with altering the table's schema.