Cleanup for #383

This commit is contained in:
Tim Griesser 2014-08-14 15:32:26 -04:00
parent 980818872d
commit 0b1af71ac3
8 changed files with 34 additions and 35 deletions

View File

@ -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) {

View File

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

View File

@ -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');

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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));
};

View File

@ -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.