From af1f9e83aeb61c57abc23dfbf24c42569ffa06d3 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Tue, 3 Jun 2014 00:47:54 -0400 Subject: [PATCH] Fix #244, normalize for inconsistent insert rows --- browser/knex.js | 114 ++++++++++++++++++++++--------------- browser/websql.js | 82 +++++++++++++++----------- lib/query/compiler.js | 21 ++++++- lib/utils.js | 15 ----- test/index.js | 15 +---- test/unit/query/builder.js | 7 +++ 6 files changed, 144 insertions(+), 110 deletions(-) diff --git a/browser/knex.js b/browser/knex.js index 4d471d0dc..18a4de554 100644 --- a/browser/knex.js +++ b/browser/knex.js @@ -1,5 +1,5 @@ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Knex=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 255) { - helpers.warn('Your comment is longer than the max comment length for MySQL'); - } - return "comment '" + comment + "'"; + if (comment && comment.length > 255) { + helpers.warn('Your comment is longer than the max comment length for MySQL'); } + return comment && "comment '" + comment + "'"; }; client.ColumnBuilder = ColumnBuilder_MySQL; @@ -1089,7 +1088,10 @@ Formatter_PG.prototype.operators = [ // Wraps a value (column, tableName) with the correct ticks. Formatter_PG.prototype.wrapValue = function(value) { - return (value !== '*' ? '"' + value + '"' : '*'); + if (value === '*') return value; + var matched = value.match(/(.*?)(\[[0-9]\])/); + if (matched) return this.wrapValue(matched[1]) + matched[2]; + return '"' + value + '"'; }; // Memoize the calls to "wrap" for a little extra perf. @@ -1485,7 +1487,7 @@ function ColumnBuilder_PG() { inherits(ColumnBuilder_PG, Schema.ColumnBuilder); function ColumnCompiler_PG() { - this.modifiers = ['nullable', 'defaultTo']; + this.modifiers = ['nullable', 'defaultTo', 'comment']; this.Formatter = client.Formatter; Schema.ColumnCompiler.apply(this, arguments); } @@ -1523,8 +1525,10 @@ ColumnCompiler_PG.prototype.uuid = 'uuid'; // Modifiers: // ------ ColumnCompiler_PG.prototype.comment = function(comment) { - this.pushQuery('comment on column ' + this.tableName + '.' + - this.formatter.wrap(this.args[0]) + " is " + (comment ? "'" + comment + "'" : 'NULL')); + this.pushAdditional(function() { + this.pushQuery('comment on column ' + this.tableCompiler.tableName() + '.' + + this.formatter.wrap(this.args[0]) + " is " + (comment ? "'" + comment + "'" : 'NULL')); + }, comment); }; client.ColumnBuilder = ColumnBuilder_PG; @@ -1637,10 +1641,8 @@ TableCompiler_PG.prototype.createQuery = function(columns) { sql: 'create table ' + this.tableName() + ' (' + columns.sql.join(', ') + ')', bindings: columns.bindings }); - var hasComment = _.has(this._single, 'comment'); - if (hasComment) { - this.pushQuery('comment on table ' + this.tableName() + ' is ' + "'" + this.attributes.comment + "'"); - } + var hasComment = _.has(this.single, 'comment'); + if (hasComment) this.comment(this.single.comment); }; // Compile a foreign key command. @@ -1649,6 +1651,11 @@ TableCompiler_PG.prototype.foreign = function(foreignData) { if (sql) this.pushQuery(sql); }; +// Compiles the comment on the table. +TableCompiler_PG.prototype.comment = function(comment) { + this.pushQuery('comment on table ' + this.tableName() + ' is ' + "'" + (this.single.comment || '') + "'"); +}; + // Indexes: // ------- @@ -4036,11 +4043,15 @@ QueryCompiler.prototype._prepInsert = function(data) { var isRaw = this.formatter.rawOrFn(data); if (isRaw) return isRaw; var values = []; - var columns; + var columns, colList; if (!_.isArray(data)) data = data ? [data] : []; for (var i = 0, l = data.length; i