Merge branch 'master' into gh-pages

* master:
  0.1.8
  ensure transaction has the proper context, allow resolving the transaction with a value
  fixing link for laravel query builder
  Comment documentation.
  Table and column comments during create table.
  remove unnecessary function
This commit is contained in:
Tim Griesser 2013-07-07 14:00:04 -04:00
commit 4c6a24c363
11 changed files with 418 additions and 262 deletions

View File

@ -1,7 +1,6 @@
# .travis.yml
language: node_js
node_js:
- 0.11
- 0.10
- 0.8
- 0.6

View File

@ -81,11 +81,11 @@ exports.protoProps = {
});
},
finishTransaction: function(type, trans, dfd) {
finishTransaction: function(type, trans, dfd, msg) {
var ctx = this;
nodefn.call(trans.connection.query.bind(trans.connection), type + ';', []).then(function(resp) {
if (type === 'commit') dfd.resolve(resp);
if (type === 'rollback') dfd.reject(resp);
if (type === 'commit') dfd.resolve(msg || resp);
if (type === 'rollback') dfd.reject(msg || resp);
}).ensure(function() {
ctx.releaseConnection(trans.connection);
trans.connection = null;

View File

@ -84,7 +84,7 @@ MysqlClient.grammar = {
MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar, {
// The possible column modifiers.
modifiers: ['Unsigned', 'Nullable', 'Default', 'Increment', 'After'],
modifiers: ['Unsigned', 'Nullable', 'Default', 'Increment', 'After', 'Comment'],
// Compile a create table command.
compileCreateTable: function(blueprint, command) {
@ -98,6 +98,11 @@ MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar
sql += ' engine = ' + blueprint.isEngine;
}
if (blueprint.tableComment) {
var maxTableCommentLength = 60;
sql += " comment = '" + blueprint.tableComment + "'"
}
return sql;
},
@ -251,6 +256,14 @@ MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar
if (column.isAfter) {
return ' after ' + this.wrap(column.isAfter);
}
},
// Get the SQL for a comment column modifier. (MySQL)
modifyComment: function(blueprint, column) {
var maxColumnCommentLength = 255;
if (_.isString(column.comment)) {
return " comment '" + column.comment + "'";
}
}
});

View File

@ -169,6 +169,25 @@ PostgresClient.schemaGrammar = _.extend({}, base.schemaGrammar, PostgresClient.g
return 'alter table ' + this.wrapTable(blueprint) + ' rename to ' + this.wrapTable(command.to);
},
// Compile a comment command.
compileComment: function(blueprint, command) {
var table = this.wrapTable(blueprint);
var comment;
if (command.comment == void 0) {
comment = 'NULL'
} else {
comment = "'" + command.comment + "'";
}
var identifier;
if (command.isTable) {
identifier = 'table ' + table;
} else {
var column = this.wrap(command.columnName);
identifier = 'column ' + table + '.' + column;
}
return 'comment on ' + identifier + ' is ' + comment;
},
// Create the column definition for a string type.
typeString: function(column) {
return "varchar(" + column.length + ")";
@ -235,4 +254,4 @@ PostgresClient.schemaGrammar = _.extend({}, base.schemaGrammar, PostgresClient.g
return ' primary key';
}
}
});
});

View File

@ -87,11 +87,11 @@ _.extend(Sqlite3Client.prototype, base.protoProps, {
});
},
finishTransaction: function(type, trans, dfd) {
finishTransaction: function(type, trans, dfd, msg) {
var ctx = this;
nodefn.call(trans.connection.run.bind(trans.connection), type + ';', []).then(function(resp) {
if (type === 'commit') dfd.resolve(resp);
if (type === 'rollback') dfd.reject(resp);
if (type === 'commit') dfd.resolve(msg || resp);
if (type === 'rollback') dfd.reject(msg || resp);
}).ensure(function() {
ctx.releaseConnection(trans.connection);
trans.connection = null;
@ -210,7 +210,7 @@ Sqlite3Client.schemaGrammar = _.extend({}, base.schemaGrammar, Sqlite3Client.gra
// Get all of the commands with a given name.
getCommandsByName: function(blueprint, name) {
return _.where(blueprint.commands, function(value) { return value.name == name; });
return _.find(blueprint.commands, function(value) { return value.name == name; }) || [];
},
// Get the primary key syntax for a table creation statement.

File diff suppressed because it is too large Load Diff

View File

@ -181,7 +181,7 @@
<div id="sidebar" class="interface">
<a class="toc_title" href="#">
Knex.js <span class="version">(0.1.7)</span>
Knex.js <span class="version">(0.1.8)</span>
</a>
<ul class="toc_section">
<li>&raquo; <a href="https://github.com/tgriesser/knex">GitHub Repository</a></li>
@ -263,6 +263,7 @@
<li> <a href="#Schema-timestamp">timestamp</a></li>
<li> <a href="#Schema-binary">binary</a></li>
<li> <a href="#Schema-enum">enum / enu</a></li>
<li> <a href="#Schema-comment">comment</a></li>
<li><a href="#Chainable"><b>Chainable:</b></li>
<li> <a href="#Chainable-index">index</a></li>
<li> <a href="#Chainable-primary">primary</a></li>
@ -271,6 +272,7 @@
<li> <a href="#Chainable-unsigned">unsigned</a></li>
<li> <a href="#Chainable-nullable">nullable</a></li>
<li> <a href="#Chainable-after">after</a></li>
<li> <a href="#Chainable-comment">comment</a></li>
</ul>
<a class="toc_title" href="#Raw">
@ -319,7 +321,7 @@
or send tweets to <a href="http://twitter.com/tgriesser">@tgriesser</a>.
</p>
<h2>Latest Release: 0.1.7</h2>
<h2>Latest Release: 0.1.8</h2>
<p>
Current Develop &mdash;
@ -330,7 +332,7 @@
<p>
Special thanks to <a href="https://twitter.com/taylorotwell">Taylor Otwell</a> and his work
on the <a href="http://four.laravel.com/docs/queries">Laravel Query Builder</a>,
on the <a href="http://laravel.com/docs/queries">Laravel Query Builder</a>,
from which much of the code and syntax is derived.
</p>
@ -1015,6 +1017,12 @@ Knex.Schema.table('users', function (table) {
applicable to MySql.
</p>
<p id="Schema-comment">
<b class="header">comment</b><code>table.comment(value)</code>
<br />
Sets the comment for a table.
</p>
<h3 id="Chainable">Chainable Methods:</h3>
<p>
@ -1063,6 +1071,12 @@ Knex.Schema.table('users', function (table) {
Sets the column to be inserted after another, only used in MySql alter tables.
</p>
<p id="Chainable-comment">
<b class="header">comment</b><code>column.comment(value)</code>
<br />
Sets the comment for a column.
</p>
<pre>
Knex.Schema.createTable('accounts', function() {
t.increments().primary();
@ -1135,6 +1149,11 @@ Knex.Raw('select * from users where id = 1').then(function(resp) {
<h2 id="changelog">Change Log</h2>
<p>
<b class="header">0.1.8</b> &mdash; <small><i>July 7, 2013</i></small><br />
Somehow missing the <tt>!=</tt> operator. Using <tt>_.find</tt> rather than <tt>_.where</tt> in <tt>getCommandsByName</tt>(#22).
</p>
<p>
<b class="header">0.1.7</b> &mdash; <small><i>June 12, 2013</i></small><br />
Ensures unhandled errors in the <tt>exec</tt> callback interface are re-thrown.

62
knex.js
View File

@ -1,4 +1,4 @@
// Knex.js 0.1.7
// Knex.js 0.1.8
//
// (c) 2013 Tim Griesser
// Knex may be freely distributed under the MIT license.
@ -23,11 +23,7 @@
};
// Keep in sync with package.json
Knex.VERSION = '0.1.7';
var rethrower = function(err) {
throw err;
};
Knex.VERSION = '0.1.8';
// Methods common to both the `Grammar` and `SchemaGrammar` interfaces,
// used to generate the sql in one form or another.
@ -443,7 +439,7 @@
};
// All operators used in the `where` clause generation.
var operators = ['=', '<', '>', '<=', '>=', '<>', 'like', 'not like', 'between', 'ilike'];
var operators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'like', 'not like', 'between', 'ilike'];
_.extend(Builder.prototype, Common, {
@ -949,18 +945,22 @@
// transaction completes or fails, we know what to do.
var dfd = When.defer();
// Call the container with the transaction
// commit & rollback objects
container({
commit: function() {
client.finishTransaction('commit', this, dfd);
// Ensure the transacting object methods are bound with the correct context.
var containerObj = {
commit: function(msg) {
client.finishTransaction('commit', this, dfd, msg);
},
rollback: function() {
client.finishTransaction('rollback', this, dfd);
rollback: function(msg) {
client.finishTransaction('rollback', this, dfd, msg);
},
// "rollback to"?
connection: connection
});
};
_.bindAll(containerObj, 'commit', 'rollback');
// Call the container with the transaction
// commit & rollback objects.
container(containerObj);
return dfd.promise;
});
@ -1087,6 +1087,26 @@
}
}
// Add table comments. (Postgres)
if (this.tableComment) {
this._addCommand('comment', {
comment: this.tableComment,
isTable: true
});
}
// Add column comments. (Postgres)
for (var i = 0, l = this.columns.length; i < l; i++) {
var column = this.columns[i];
if (_.has(column, 'comment')) {
this._addCommand('comment', {
comment: column.comment,
columnName: column.name,
isTable: false
});
}
}
var statements = [];
// Each type of command has a corresponding compiler function on the schema
@ -1170,6 +1190,10 @@
return this._indexCommand('foreign', columns, name);
},
comment: function(comment) {
this.tableComment = comment || null;
},
// Create a new auto-incrementing column on the table.
increments: function(column) {
return this._addColumn('integer', (column || 'id'), {autoIncrement: true, length: 11});
@ -1377,6 +1401,12 @@
after: function(name) {
this.isAfter = name;
return this;
},
// Adds a comment to this column.
comment: function(comment) {
this.comment = comment || null;
return this;
}
};
@ -1618,4 +1648,4 @@
// Export the Knex module
module.exports = Knex;
}).call(this);
}).call(this);

View File

@ -1,6 +1,6 @@
{
"name": "knex",
"version": "0.1.7",
"version": "0.1.8",
"description": "A query builder for Postgres, MySQL and SQLite3, designed to be flexible, portable, and fun to use.",
"main": "knex.js",
"directories": {
@ -28,12 +28,9 @@
"keywords": [
"sql",
"query",
"builder",
"postgresql",
"postgres",
"mysql",
"sqlite3",
"sqlite"
"sqlite3"
],
"author": {
"name": "Tim Griesser",

View File

@ -15,12 +15,13 @@ module.exports = function(Knex, resolver, error) {
return When.all([
Knex.Schema.createTable('test_table_one', function(table) {
table.engine('InnoDB');
table.comment('A table comment.')
table.increments('id');
table.string('first_name');
table.string('last_name');
table.string('email').unique().nullable();
table.integer('logins').defaultTo(1).index();
table.text('about');
table.integer('logins').defaultTo(1).index().comment();
table.text('about').comment('A comment.');
table.timestamps();
}),
Knex.Schema.createTable('test_table_two', function(t) {
@ -67,4 +68,4 @@ module.exports = function(Knex, resolver, error) {
})
.then(resolver, error);
};
};

View File

@ -16,11 +16,11 @@ module.exports = {
},
'schema.2': {
mysql: {
sql: ['create table `test_table_one` (`id` int(11) not null auto_increment primary key, `first_name` varchar(255) not null, `last_name` varchar(255) not null, `email` varchar(255) null, `logins` int(11) not null default \'1\', `about` text not null, `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 engine = InnoDB','alter table `test_table_one` add unique test_table_one_email_unique(`email`)','alter table `test_table_one` add index test_table_one_logins_index(`logins`)'],
sql: ['create table `test_table_one` (`id` int(11) not null auto_increment primary key, `first_name` varchar(255) not null, `last_name` varchar(255) not null, `email` varchar(255) null, `logins` int(11) not null default \'1\', `about` text not null comment \'A comment.\', `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 engine = InnoDB comment = \'A table comment.\'','alter table `test_table_one` add unique test_table_one_email_unique(`email`)','alter table `test_table_one` add index test_table_one_logins_index(`logins`)'],
bindings: []
},
postgres: {
sql: ['create table "test_table_one" ("id" serial primary key not null, "first_name" varchar(255) not null, "last_name" varchar(255) not null, "email" varchar(255) null, "logins" integer not null default \'1\', "about" text not null, "created_at" timestamp not null, "updated_at" timestamp not null)','alter table "test_table_one" add constraint test_table_one_email_unique unique ("email")','create index test_table_one_logins_index on "test_table_one" ("logins")'],
sql: ['create table "test_table_one" ("id" serial primary key not null, "first_name" varchar(255) not null, "last_name" varchar(255) not null, "email" varchar(255) null, "logins" integer not null default \'1\', "about" text not null, "created_at" timestamp not null, "updated_at" timestamp not null)','alter table "test_table_one" add constraint test_table_one_email_unique unique ("email")','create index test_table_one_logins_index on "test_table_one" ("logins")','comment on table "test_table_one" is \'A table comment.\'','comment on column "test_table_one"."logins" is NULL','comment on column "test_table_one"."about" is \'A comment.\''],
bindings: []
},
sqlite3: {