Comparing version 0.2.2 to 0.2.3
@@ -111,3 +111,21 @@ var When = require('when'); | ||
compileInsert: function(qb) { | ||
var sql = require('../knex').Grammar.compileInsert.call(this, qb); | ||
var values = qb.values; | ||
var columns = _.pluck(values[0], 0); | ||
var paramBlocks = []; | ||
// If there are any "where" clauses, we need to omit | ||
// any bindings that may have been associated with them. | ||
if (qb.wheres.length > 0) this._clearWhereBindings(qb); | ||
var sql = 'insert into ' + this.wrapTable(qb.table) + ' '; | ||
if (columns.length === 0) { | ||
sql += 'default values'; | ||
} else { | ||
for (var i = 0, l = values.length; i < l; ++i) { | ||
paramBlocks.push("(" + this.parameterize(_.pluck(values[i], 1)) + ")"); | ||
} | ||
sql += "(" + this.columnize(columns) + ") values " + paramBlocks.join(', '); | ||
} | ||
if (qb.isReturning) { | ||
@@ -114,0 +132,0 @@ sql += ' returning "' + qb.isReturning + '"'; |
@@ -123,3 +123,8 @@ var When = require('when'); | ||
var table = this.wrapTable(qb.table); | ||
var columns = _.pluck(values[0], 0); | ||
// If there are any "where" clauses, we need to omit | ||
// any bindings that may have been associated with them. | ||
if (qb.wheres.length > 0) this._clearWhereBindings(qb); | ||
// If there is only one record being inserted, we will just use the usual query | ||
@@ -129,6 +134,11 @@ // grammar insert builder because no special syntax is needed for the single | ||
if (values.length === 1) { | ||
return require('../knex').Grammar.compileInsert.call(this, qb); | ||
var sql = 'insert into ' + table + ' '; | ||
if (columns.length === 0) { | ||
sql += 'default values'; | ||
} else { | ||
sql += "(" + this.columnize(columns) + ") values " + "(" + this.parameterize(_.pluck(values[0], 1)) + ")"; | ||
} | ||
return sql; | ||
} | ||
var columns = _.pluck(values[0], 0); | ||
var blocks = []; | ||
@@ -209,4 +219,12 @@ | ||
if (primary) { | ||
var columns = this.columnize(primary.columns); | ||
return ', primary key (' + columns + ')'; | ||
// Ensure that autoincrement columns aren't handled here, this is handled | ||
// alongside the autoincrement clause. | ||
primary.columns = _.reduce(primary.columns, function(memo, column) { | ||
if (column.autoIncrement !== true) memo.push(column); | ||
return memo; | ||
}, []); | ||
if (primary.columns.length > 0) { | ||
var columns = this.columnize(primary.columns); | ||
return ', primary key (' + columns + ')'; | ||
} | ||
} | ||
@@ -213,0 +231,0 @@ }, |
23
knex.js
@@ -1,2 +0,2 @@ | ||
// Knex.js 0.2.2 | ||
// Knex.js 0.2.3 | ||
// | ||
@@ -7,12 +7,12 @@ // (c) 2013 Tim Griesser | ||
// http://knexjs.org | ||
(function() { | ||
(function(define) { | ||
"use strict"; | ||
"use strict"; | ||
define(function(require, exports, module) { | ||
// Required dependencies. | ||
var _ = require('underscore'); | ||
var _ = require('underscore'); | ||
var when = require('when'); | ||
var push = Array.prototype.push; | ||
// `Knex` is the root namespace and a chainable function: `Knex('tableName')` | ||
@@ -27,3 +27,3 @@ var Knex = function(table) { | ||
// Keep in sync with package.json | ||
Knex.VERSION = '0.2.2'; | ||
Knex.VERSION = '0.2.3'; | ||
@@ -1673,2 +1673,5 @@ // Methods common to both the `Grammar` and `SchemaGrammar` interfaces, | ||
var array = []; | ||
var push = array.push; | ||
// Default client paths, located in the `./clients` directory. | ||
@@ -1690,2 +1693,6 @@ var Clients = { | ||
}).call(this); | ||
}); | ||
})( | ||
typeof define === 'function' && define.amd ? define : function (factory) { factory(require, exports, module); } | ||
); |
{ | ||
"name": "knex", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "A query builder for Postgres, MySQL and SQLite3, designed to be flexible, portable, and fun to use.", | ||
@@ -5,0 +5,0 @@ "main": "knex.js", |
@@ -175,4 +175,8 @@ | ||
it('should handle empty inserts', function(ok) { | ||
Knex('test_default_table').insert({}, 'id').then(resolver(ok), ok); | ||
}); | ||
}); | ||
}; |
@@ -12,3 +12,4 @@ var When = require('when'); | ||
Knex.Schema.dropTableIfExists('datatype_test'), | ||
Knex.Schema.dropTableIfExists('accounts') | ||
Knex.Schema.dropTableIfExists('accounts'), | ||
Knex.Schema.dropTableIfExists('test_default_table') | ||
]).then(function(resp) { | ||
@@ -88,2 +89,10 @@ | ||
.then(function() { | ||
return Knex.Schema.createTable('test_default_table', function(qb) { | ||
qb.increments().primary(); | ||
qb.string('string').defaultTo('hello'); | ||
qb.tinyint('tinyint').defaultTo(0); | ||
qb.text('text').nullable(); | ||
}); | ||
}) | ||
.then(function() { | ||
return res; | ||
@@ -90,0 +99,0 @@ }) |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
211203
6330