From a91208c0709075601e667727251dd761c3db2ad5 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Thu, 11 Apr 2013 08:29:11 -0400 Subject: [PATCH 01/13] fixing bug on insert, removing concept of idAttribtue --- docs/knex.html | 675 +++++++++++++++++++++++-------------------------- index.js | 1 - knex.js | 27 +- package.json | 2 +- 4 files changed, 319 insertions(+), 386 deletions(-) delete mode 100644 index.js diff --git a/docs/knex.html b/docs/knex.html index 75671194..0e72b5c1 100644 --- a/docs/knex.html +++ b/docs/knex.html @@ -673,7 +673,7 @@ inserts using a single query statement.

    compileInsert: function(qb, values) {
       var table = this.wrapTable(qb.table);
       var columns = this.columnize(_.keys(values[0]));
-      var parameters = this.parameterize(values[0]);
+      var parameters = this.parameterize(_.values(values[0]));
       
       var paramBlocks = [];
       for (var i = 0, l = values.length; i < l; ++i) {
@@ -692,23 +692,6 @@ inserts using a single query statement.

-

Compiles an insert, getting the id of the insert row.

- -
- -
    compileInsertGetId: function(qb, values) {
-      return this.compileInsert(qb, values);
-    },
- - - - -
  • -
    - -
    - -

    Compiles an update query.

    @@ -725,11 +708,11 @@ inserts using a single query statement.

  • -
  • +
  • - +

    Compiles a delete query.

    @@ -744,11 +727,11 @@ inserts using a single query statement.

  • -
  • +
  • - +

    Compiles a truncate query.

    @@ -804,11 +787,11 @@ inserts using a single query statement.

  • -
  • +
  • - +

    Knex.Builder

    @@ -817,11 +800,11 @@ inserts using a single query statement.

  • -
  • +
  • - +
    @@ -835,11 +818,11 @@ inserts using a single query statement.

  • -
  • +
  • - +

    All operators used in the where clause generation.

    @@ -847,18 +830,16 @@ inserts using a single query statement.

      var operators = ['=', '<', '>', '<=', '>=', 'like', 'not like', 'between', 'ilike'];
     
    -  Builder.prototype = {
    -
    -    idAttr: 'id',
    + Builder.prototype = {
  • -
  • +
  • - +

    Sets the tableName on the query.

    @@ -873,29 +854,11 @@ inserts using a single query statement.

  • -
  • +
  • - -
    -

    Set the idAttribute for the query.

    - -
    - -
        idAttribute: function(id) {
    -      this.idAttr = id;
    -      return this;
    -    },
    - -
  • - - -
  • -
    - -
    - +

    Adds a distinct clause to the query.

    @@ -909,11 +872,11 @@ inserts using a single query statement.

  • -
  • +
  • - +

    Clones the current query builder, including any pieces that have been set thus far

    @@ -923,7 +886,7 @@ pieces that have been set thus far

        clone: function() {
           var item = new Builder(this.table);
           var items = [
    -        'isDistinct', 'idAttr', 'joins',
    +        'isDistinct', 'joins',
             'wheres', 'orders', 'columns', 'bindings',
             'grammar', 'connection', 'transaction'
           ];
    @@ -937,11 +900,11 @@ pieces that have been set thus far

  • -
  • +
  • - +

    Resets all attributes on the query builder.

    @@ -953,7 +916,6 @@ pieces that have been set thus far

    this.orders = []; this.columns = []; this.bindings = []; - this.idAttr = Builder.prototype.idAttr; this.isDistinct = false; }, @@ -964,7 +926,6 @@ pieces that have been set thus far

    order: this.orders, columns: this.columns, bindings: this.bindings, - idAttr: this.idAttr, isDistinct: this.isDistinct }; },
    @@ -972,11 +933,11 @@ pieces that have been set thus far

  • -
  • +
  • - +

    Adds a join clause to the query, allowing for advanced joins with an anonymous function as the second argument.

    @@ -1000,11 +961,11 @@ with an anonymous function as the second argument.

  • -
  • +
  • - +

    The where function can be used in several ways: The most basic is where(key, value), which expands to @@ -1046,11 +1007,11 @@ where key = value.

  • -
  • +
  • - +

    Alias to where, for internal builder consistency.

    @@ -1075,11 +1036,11 @@ where key = value.

  • -
  • +
  • - +

    Adds an or where clause to the query.

    @@ -1092,11 +1053,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a where exists clause to the query.

    @@ -1118,11 +1079,11 @@ where key = value.

  • -
  • +
  • - +

    Adds an or where exists clause to the query.

    @@ -1135,11 +1096,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a where not exists clause to the query.

    @@ -1152,11 +1113,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a or where not exists clause to the query.

    @@ -1169,11 +1130,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a where in clause to the query.

    @@ -1197,11 +1158,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a or where in clause to the query.

    @@ -1214,11 +1175,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a where not in clause to the query.

    @@ -1231,11 +1192,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a or where not in clause to the query.

    @@ -1248,11 +1209,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a where null clause to the query.

    @@ -1266,11 +1227,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a or where null clause to the query.

    @@ -1283,11 +1244,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a where not null clause to the query.

    @@ -1300,11 +1261,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a or where not null clause to the query.

    @@ -1317,11 +1278,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a where between clause to the query.

    @@ -1336,11 +1297,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a or where between clause to the query.

    @@ -1355,11 +1316,11 @@ where key = value.

  • -
  • +
  • - +

    @@ -1368,11 +1329,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a group by clause to the query.

    @@ -1386,11 +1347,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a order by clause to the query.

    @@ -1404,11 +1365,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a having clause to the query.

    @@ -1423,6 +1384,43 @@ where key = value.

  • +
  • +
    + +
    + +
    +
    + +
    + +
  • + + +
  • +
    + +
    + +
    + +
    + +
        offset: function(value) {
    +      if (value == null) return this.isOffset;
    +      this.isOffset = value;
    +      return this;
    +    },
    +
    +    limit: function(value) {
    +      if (value == null) return this.isLimit;
    +      this.isLimit = value;
    +      return this;
    +    },
    + +
  • + +
  • @@ -1442,43 +1440,6 @@ where key = value.

    - -
    - -
        offset: function(value) {
    -      if (value == null) return this.isOffset;
    -      this.isOffset = value;
    -      return this;
    -    },
    -
    -    limit: function(value) {
    -      if (value == null) return this.isLimit;
    -      this.isLimit = value;
    -      return this;
    -    },
    - -
  • - - -
  • -
    - -
    - -
    -
    - -
    - -
  • - - -
  • -
    - -
    - -

    Determine if any rows exist for the current query.

    @@ -1492,11 +1453,11 @@ where key = value.

  • -
  • +
  • - +

    Retrieve the "count" result of the query.

    @@ -1509,11 +1470,11 @@ where key = value.

  • -
  • +
  • - +

    Retrieve the minimum value of a given column.

    @@ -1526,11 +1487,11 @@ where key = value.

  • -
  • +
  • - +

    Retrieve the maximum value of a given column.

    @@ -1543,11 +1504,11 @@ where key = value.

  • -
  • +
  • - +

    Retrieve the sum of the values of a given column.

    @@ -1560,11 +1521,11 @@ where key = value.

  • -
  • +
  • - +

    Increments a column's value by the specified amount.

    @@ -1577,11 +1538,11 @@ where key = value.

  • -
  • +
  • - +

    Decrements a column's value by the specified amount.

    @@ -1594,11 +1555,11 @@ where key = value.

  • -
  • +
  • - +

    Performs a select query, returning a promise.

    @@ -1615,40 +1576,34 @@ where key = value.

  • -
  • +
  • - +

    Performs an INSERT query, returning a promise.

        insert: function(values, returning) {
    -      var str;
    -      returning || (returning = this.idAttr);
           if (!_.isArray(values)) values = values ? [values] : [];
           for (var i = 0, l = values.length; i < l; i++) {
             var record = values[i];
             this.bindings = this.bindings.concat(_.values(record));
           }
    -      if (returning) {
    -        str = this.grammar.compileInsertGetId(this, values, returning);
    -      } else {
    -        str = this.grammar.compileInsert(this, values);
    -      }
    +      var str = this.grammar.compileInsert(this, values);
           return Knex.runQuery(this, {sql: str, bindings: this._cleanBindings(), type: 'insert'});
         },
  • -
  • +
  • - +

    Performs an update query, returning a promise.

    @@ -1662,11 +1617,11 @@ where key = value.

  • -
  • +
  • - +

    Executes a delete statement on the query;

    @@ -1679,11 +1634,11 @@ where key = value.

  • -
  • +
  • - +

    Alias to delete

    @@ -1696,11 +1651,11 @@ where key = value.

  • -
  • +
  • - +

    Truncate

    @@ -1713,11 +1668,11 @@ where key = value.

  • -
  • +
  • - +

    Set by transacting - contains the object with the connection needed to execute a transaction

    @@ -1729,11 +1684,11 @@ needed to execute a transaction

  • -
  • +
  • - +

    Sets the current Builder connection to that of the the currently running transaction

    @@ -1748,11 +1703,11 @@ the currently running transaction

  • -
  • +
  • - +

    @@ -1761,11 +1716,11 @@ the currently running transaction

  • -
  • +
  • - +
    @@ -1819,11 +1774,11 @@ the currently running transaction

  • -
  • +
  • - +

    Returns all bindings excluding the Knex.Raw types.

    @@ -1840,11 +1795,11 @@ the currently running transaction

  • -
  • +
  • - +

    Knex.JoinClause

    @@ -1853,11 +1808,11 @@ the currently running transaction

  • -
  • +
  • - +
    @@ -1885,11 +1840,11 @@ the currently running transaction

  • -
  • +
  • - +

    Knex.Transaction

    @@ -1898,11 +1853,11 @@ the currently running transaction

  • -
  • +
  • - +
    @@ -1914,11 +1869,11 @@ the currently running transaction

  • -
  • +
  • - +

    Initiate a deferred object, so we know when the transaction completes or fails, we know what to do.

    @@ -1930,11 +1885,11 @@ transaction completes or fails, we know what to do.

  • -
  • +
  • - +

    Finish the transaction connection

    @@ -1949,11 +1904,11 @@ transaction completes or fails, we know what to do.

  • -
  • +
  • - +

    Call the container with the transaction commit & rollback objects

    @@ -1972,11 +1927,11 @@ commit & rollback objects

  • -
  • +
  • - +

    Knex.Schema

    @@ -1985,11 +1940,11 @@ commit & rollback objects

  • -
  • +
  • - +

    Top level object for Schema related functions

    @@ -2000,11 +1955,11 @@ commit & rollback objects

  • -
  • +
  • - +

    Attach main static methods, which passthrough to the SchemaBuilder instance methods

    @@ -2022,11 +1977,11 @@ SchemaBuilder instance methods

  • -
  • +
  • - +

    Knex.SchemaBuilder

    @@ -2035,11 +1990,11 @@ SchemaBuilder instance methods

  • -
  • +
  • - +
    @@ -2057,11 +2012,11 @@ SchemaBuilder instance methods

  • -
  • +
  • - +

    Connection

    @@ -2078,11 +2033,11 @@ SchemaBuilder instance methods

  • -
  • +
  • - +

    Used before a builder call, specifying if this call is nested inside a transaction

    @@ -2097,11 +2052,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Determine if the given table exists.

    @@ -2124,11 +2079,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Modify a table on the schema.

    @@ -2141,11 +2096,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Create a new table on the schema.

    @@ -2161,11 +2116,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Drop a table from the schema.

    @@ -2180,11 +2135,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Drop a table from the schema if it exists.

    @@ -2199,11 +2154,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Rename a table on the schema.

    @@ -2218,11 +2173,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Execute the blueprint to build / modify the table.

    @@ -2235,11 +2190,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Create a new command set with a Closure.

    @@ -2253,11 +2208,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Knex.SchemaGrammar

    @@ -2266,11 +2221,11 @@ is nested inside a transaction

  • -
  • +
  • - +
    @@ -2284,11 +2239,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Compile a foreign key command.

    @@ -2301,11 +2256,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    We need to prepare several of the elements of the foreign key definition before we can create the SQL, such as wrapping the tables and convert @@ -2322,11 +2277,11 @@ an array of columns to comma-delimited strings for the SQL queries.

  • -
  • +
  • - +

    Once we have the basic foreign key creation statement constructed we can build out the syntax for what should happen on an update or delete of @@ -2343,11 +2298,11 @@ the affected columns, which will get something like "cascade", etc.

    -
  • +
  • - +

    Each of the column types have their own compiler functions which are responsible for turning the column definition into its SQL format @@ -2369,11 +2324,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Add the column modifiers to the definition.

    @@ -2393,11 +2348,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Get the primary key command if it exists on the blueprint.

    @@ -2411,11 +2366,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Get all of the commands with a given name.

    @@ -2428,11 +2383,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Get the SQL for the column data type.

    @@ -2445,11 +2400,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Add a prefix to an array of values.

    @@ -2462,11 +2417,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Wrap a table in keyword identifiers.

    @@ -2480,11 +2435,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Wrap a value in keyword identifiers.

    @@ -2498,11 +2453,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Format a value so that it can be used in "default" clauses.

    @@ -2519,11 +2474,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Knex.SchemaBlueprint

    @@ -2532,11 +2487,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +
    @@ -2563,11 +2518,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Get the raw sql statements for the blueprint.

    @@ -2578,11 +2533,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Add the commands that are implied by the blueprint.

    @@ -2595,11 +2550,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Add indicies

    @@ -2617,11 +2572,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    If the index has been specified on the given column, but is simply equal to "true" (boolean), no name has been specified for this @@ -2636,11 +2591,11 @@ index, so we will simply call the index methods without one.

  • -
  • +
  • - +

    If the index has been specified on the column and it is something other than boolean true, we will assume a name was provided on @@ -2660,11 +2615,11 @@ the index specification, and pass in the name to the method.

  • -
  • +
  • - +

    Each type of command has a corresponding compiler function on the schema grammar which is used to build the necessary SQL statements to build @@ -2687,11 +2642,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Determine if the blueprint has a create command.

    @@ -2709,11 +2664,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the table needs to be created.

    @@ -2726,11 +2681,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the table should be dropped.

    @@ -2743,11 +2698,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the table should be dropped if it exists.

    @@ -2760,11 +2715,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the given columns should be dropped.

    @@ -2778,11 +2733,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the given columns should be dropped.

    @@ -2795,11 +2750,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the given primary key should be dropped.

    @@ -2812,11 +2767,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the given unique key should be dropped.

    @@ -2829,11 +2784,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the given index should be dropped.

    @@ -2846,11 +2801,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the given foreign key should be dropped.

    @@ -2863,11 +2818,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Rename the table to a given name.

    @@ -2880,11 +2835,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Specify the primary key(s) for the table.

    @@ -2897,11 +2852,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Specify a unique index for the table.

    @@ -2914,11 +2869,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Specify an index for the table.

    @@ -2931,11 +2886,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Specify a foreign key for the table.

    @@ -2948,11 +2903,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new auto-incrementing column on the table.

    @@ -2966,11 +2921,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new string column on the table.

    @@ -2983,11 +2938,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new text column on the table.

    @@ -3000,11 +2955,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new integer column on the table.

    @@ -3017,11 +2972,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new float column on the table.

    @@ -3034,11 +2989,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new decimal column on the table.

    @@ -3051,11 +3006,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new boolean column on the table.

    @@ -3068,11 +3023,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Alias to "boolean".

    @@ -3085,11 +3040,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new date column on the table.

    @@ -3102,11 +3057,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new date-time column on the table.

    @@ -3119,11 +3074,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new time column on the table.

    @@ -3136,11 +3091,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new timestamp column on the table.

    @@ -3153,11 +3108,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Add creation and update timestamps to the table.

    @@ -3171,11 +3126,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new enum column on the table.

    @@ -3188,11 +3143,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Alias to enum.

    @@ -3205,11 +3160,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new binary column on the table.

    @@ -3222,11 +3177,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    @@ -3235,11 +3190,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new drop index command on the blueprint.

    @@ -3257,11 +3212,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Add a new index command to the blueprint. If no name was specified for this index, we will create one using a basic @@ -3283,11 +3238,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Add a new column to the blueprint.

    @@ -3304,11 +3259,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Add a new command to the blueprint.

    @@ -3324,11 +3279,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Chainable object used in creating SchemaBuilder commands.

    @@ -3343,11 +3298,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Sets the default value for a column.

    @@ -3361,11 +3316,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Sets an integer as unsigned, is a no-op if the column type is not an integer.

    @@ -3380,11 +3335,11 @@ if the column type is not an integer.

  • -
  • +
  • - +

    Allows the column to contain null values.

    @@ -3419,11 +3374,11 @@ if the column type is not an integer.

  • -
  • +
  • - +

    Knex.Raw

    @@ -3432,11 +3387,11 @@ if the column type is not an integer.

  • -
  • +
  • - +
    @@ -3448,11 +3403,11 @@ if the column type is not an integer.

  • -
  • +
  • - +

    Knex.runQuery

    @@ -3461,11 +3416,11 @@ if the column type is not an integer.

  • -
  • +
  • - +

    Query runner, the context of this function is set to the caller, (either Builder or SchemaBuilder). Checks and fails on an already @@ -3490,11 +3445,11 @@ and returns a deferred promise.

  • -
  • +
  • - +

    Export the Knex module

    diff --git a/index.js b/index.js deleted file mode 100644 index 710aeb43..00000000 --- a/index.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./knex'); \ No newline at end of file diff --git a/knex.js b/knex.js index 0c5e70d9..4444d19b 100644 --- a/knex.js +++ b/knex.js @@ -249,7 +249,7 @@ compileInsert: function(qb, values) { var table = this.wrapTable(qb.table); var columns = this.columnize(_.keys(values[0])); - var parameters = this.parameterize(values[0]); + var parameters = this.parameterize(_.values(values[0])); var paramBlocks = []; for (var i = 0, l = values.length; i < l; ++i) { @@ -259,11 +259,6 @@ return "insert into " + table + " (" + columns + ") values " + paramBlocks.join(', '); }, - // Compiles an `insert`, getting the id of the insert row. - compileInsertGetId: function(qb, values) { - return this.compileInsert(qb, values); - }, - // Compiles an `update` query. compileUpdate: function(qb, values) { var table = this.wrapTable(qb.table), columns = []; @@ -342,8 +337,6 @@ Builder.prototype = { - idAttr: 'id', - // Sets the `tableName` on the query. from: function(tableName) { if (!tableName) return this.table; @@ -351,12 +344,6 @@ return this; }, - // Set the `idAttribute` for the query. - idAttribute: function(id) { - this.idAttr = id; - return this; - }, - // Adds a `distinct` clause to the query. distinct: function() { this.isDistinct = true; @@ -368,7 +355,7 @@ clone: function() { var item = new Builder(this.table); var items = [ - 'isDistinct', 'idAttr', 'joins', + 'isDistinct', 'joins', 'wheres', 'orders', 'columns', 'bindings', 'grammar', 'connection', 'transaction' ]; @@ -386,7 +373,6 @@ this.orders = []; this.columns = []; this.bindings = []; - this.idAttr = Builder.prototype.idAttr; this.isDistinct = false; }, @@ -397,7 +383,6 @@ order: this.orders, columns: this.columns, bindings: this.bindings, - idAttr: this.idAttr, isDistinct: this.isDistinct }; }, @@ -654,18 +639,12 @@ // Performs an `INSERT` query, returning a promise. insert: function(values, returning) { - var str; - returning || (returning = this.idAttr); if (!_.isArray(values)) values = values ? [values] : []; for (var i = 0, l = values.length; i < l; i++) { var record = values[i]; this.bindings = this.bindings.concat(_.values(record)); } - if (returning) { - str = this.grammar.compileInsertGetId(this, values, returning); - } else { - str = this.grammar.compileInsert(this, values); - } + var str = this.grammar.compileInsert(this, values); return Knex.runQuery(this, {sql: str, bindings: this._cleanBindings(), type: 'insert'}); }, diff --git a/package.json b/package.json index 97d14cf1..6bc1459b 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "knex", "version": "0.0.0", "description": "a fun sql query builder", - "main": "index.js", + "main": "knex.js", "directories": { "test": "test" }, From 4d3852528a14f006f745692410820c1bbcd044cd Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Tue, 16 Apr 2013 12:37:50 -0400 Subject: [PATCH 02/13] bool is tinyint(1) --- clients/mysql.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/mysql.js b/clients/mysql.js index 287aa236..4cfe79d1 100644 --- a/clients/mysql.js +++ b/clients/mysql.js @@ -204,7 +204,7 @@ exports.schemaGrammar = _.extend({}, grammar, { // Create the column definition for a boolean type. typeBoolean: function(column) { - return 'tinyint'; + return 'tinyint(1)'; }, // Create the column definition for a enum type. From be2273d59604663397545a074d6c8f02e5ce53af Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Sun, 21 Apr 2013 18:26:22 -0400 Subject: [PATCH 03/13] adding bit column, integer constraint, other text types, other fixes --- clients/mysql.js | 27 +++++++++++++++++++++++---- clients/postgres.js | 8 ++++++-- clients/sqlite3.js | 5 +++++ knex.js | 28 +++++++++++++++++++++------- 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/clients/mysql.js b/clients/mysql.js index 4cfe79d1..accb6269 100644 --- a/clients/mysql.js +++ b/clients/mysql.js @@ -94,7 +94,7 @@ var grammar = exports.grammar = { exports.schemaGrammar = _.extend({}, grammar, { // The possible column modifiers. - modifiers: ['Unsigned', 'Nullable', 'Default', 'Increment'], + modifiers: ['Unsigned', 'Nullable', 'Default', 'Increment', 'After'], // Compile the query to determine if a table exists. compileTableExists: function() { @@ -174,7 +174,7 @@ exports.schemaGrammar = _.extend({}, grammar, { // Compile a rename table command. compileRename: function(blueprint, command) { - return "rename table " + this.wrapTable(blueprint) + " to " + this.wrapTable(command.to); + return 'rename table ' + this.wrapTable(blueprint) + ' to ' + this.wrapTable(command.to); }, // Create the column definition for a string type. @@ -184,12 +184,26 @@ exports.schemaGrammar = _.extend({}, grammar, { // Create the column definition for a text type. typeText: function(column) { - return 'text'; + switch (column.length) { + case 'medium': + case 'mediumtext': + return 'mediumtext'; + case 'long': + case 'longtext': + return 'longtext'; + default: + return 'text'; + } }, // Create the column definition for a integer type. typeInteger: function(column) { - return 'int'; + return 'int(' + column.length + ')'; + }, + + // Create the column definition for a tiny integer type. + typeTinyInteger: function() { + return 'tinyint'; }, // Create the column definition for a float type. @@ -232,6 +246,11 @@ exports.schemaGrammar = _.extend({}, grammar, { return 'timestamp default 0'; }, + // Create the column definition for a bit type. + typeBit: function(column) { + return column.length !== false ? 'bit(' + column.length + ')' : 'bit'; + }, + // Create the column definition for a binary type. typeBinary: function(column) { return 'blob'; diff --git a/clients/postgres.js b/clients/postgres.js index ebf30f70..37edcf32 100644 --- a/clients/postgres.js +++ b/clients/postgres.js @@ -197,8 +197,7 @@ exports.schemaGrammar = _.extend({}, grammar, { // Compile a rename table command. compileRename: function(blueprint, command) { - var from = this.wrapTable(blueprint); - return "alter table " + from + " rename to " + this.wrapTable(command.to); + return 'alter table ' + this.wrapTable(blueprint) + ' rename to ' + this.wrapTable(command.to); }, // Create the column definition for a string type. @@ -261,6 +260,11 @@ exports.schemaGrammar = _.extend({}, grammar, { return 'timestamp'; }, + // Create the column definition for a bit type. + typeBit: function(column) { + return column.length !== false ? 'bit(' + column.length + ')' : 'bit'; + }, + // Create the column definition for a binary type. typeBinary: function(column) { return 'bytea'; diff --git a/clients/sqlite3.js b/clients/sqlite3.js index 3c376517..f26a3e22 100644 --- a/clients/sqlite3.js +++ b/clients/sqlite3.js @@ -280,6 +280,11 @@ exports.schemaGrammar = _.extend({}, grammar, { typeBoolean: function(column) { return 'tinyint'; }, + + // Create the column definition for a tinyint type. + typeTinyInteger: function() { + return 'tinyint'; + }, // Create the column definition for a enum type. typeEnum: function(column) { diff --git a/knex.js b/knex.js index 4444d19b..a1c88716 100644 --- a/knex.js +++ b/knex.js @@ -1151,8 +1151,7 @@ // Create a new auto-incrementing column on the table. increments: function(column) { - column || (column = 'id'); - return this._addColumn('integer', column, {autoIncrement: true}); + return this._addColumn('integer', (column || 'id'), {autoIncrement: true}); }, // Create a new string column on the table. @@ -1160,14 +1159,24 @@ return this._addColumn('string', column, {length: (length || 255)}); }, + // Alias varchar to string + varchar: function(column, length) { + return this.string(column, length); + }, + // Create a new text column on the table. - text: function(column) { - return this._addColumn('text', column); + text: function(column, length) { + return this._addColumn('text', column, {length: (length || false)}); }, // Create a new integer column on the table. - integer: function(column) { - return this._addColumn('integer', column); + integer: function(column, length) { + return this._addColumn('integer', column, {length: (length || 11)}); + }, + + // Create a new tinyinteger column on the table. + tinyInteger: function(column) { + return this._addColumn('tinyInteger', column); }, // Create a new float column on the table. @@ -1182,7 +1191,7 @@ // Create a new boolean column on the table. boolean: function(column) { - return this.bool(columns); + return this._addColumn('boolean', column); }, // Alias to "boolean". @@ -1226,6 +1235,11 @@ return this._addColumn('enum', column, {allowed: allowed}); }, + // Create a new bit column on the table. + bit: function(column, length) { + return this._addColumn('bit', column, {length: (length || false)}); + }, + // Create a new binary column on the table. binary: function(column) { return this._addColumn('binary', column); From 75e658eaa9f2f9309e332666b41870613408ddc3 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Sun, 21 Apr 2013 18:26:39 -0400 Subject: [PATCH 04/13] re-doccco --- docs/knex.html | 168 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 109 insertions(+), 59 deletions(-) diff --git a/docs/knex.html b/docs/knex.html index 0e72b5c1..bd8c2580 100644 --- a/docs/knex.html +++ b/docs/knex.html @@ -2914,8 +2914,7 @@ the blueprint element, so we'll just call that compilers function.

        increments: function(column) {
    -      column || (column = 'id');
    -      return this._addColumn('integer', column, {autoIncrement: true});
    +      return this._addColumn('integer', (column || 'id'), {autoIncrement: true});
         },
  • @@ -2944,12 +2943,12 @@ the blueprint element, so we'll just call that compilers function.

    -

    Create a new text column on the table.

    +

    Alias varchar to string

    -
        text: function(column) {
    -      return this._addColumn('text', column);
    +            
        varchar: function(column, length) {
    +      return this.string(column, length);
         },
    @@ -2961,12 +2960,12 @@ the blueprint element, so we'll just call that compilers function.

    -

    Create a new integer column on the table.

    +

    Create a new text column on the table.

    -
        integer: function(column) {
    -      return this._addColumn('integer', column);
    +            
        text: function(column, length) {
    +      return this._addColumn('text', column, {length: (length || false)});
         },
    @@ -2978,6 +2977,40 @@ the blueprint element, so we'll just call that compilers function.

    +

    Create a new integer column on the table.

    + +
    + +
        integer: function(column, length) {
    +      return this._addColumn('integer', column, {length: (length || 11)});
    +    },
    + + + + +
  • +
    + +
    + +
    +

    Create a new tinyinteger column on the table.

    + +
    + +
        tinyInteger: function(column) {
    +      return this._addColumn('tinyInteger', column);
    +    },
    + +
  • + + +
  • +
    + +
    + +

    Create a new float column on the table.

    @@ -2989,11 +3022,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new decimal column on the table.

    @@ -3006,28 +3039,28 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new boolean column on the table.

        boolean: function(column) {
    -      return this.bool(columns);
    +      return this._addColumn('boolean', column);
         },
  • -
  • +
  • - +

    Alias to "boolean".

    @@ -3040,11 +3073,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new date column on the table.

    @@ -3057,11 +3090,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new date-time column on the table.

    @@ -3074,11 +3107,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new time column on the table.

    @@ -3091,11 +3124,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new timestamp column on the table.

    @@ -3108,11 +3141,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Add creation and update timestamps to the table.

    @@ -3126,11 +3159,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new enum column on the table.

    @@ -3143,11 +3176,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Alias to enum.

    @@ -3160,11 +3193,28 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - + +
    +

    Create a new bit column on the table.

    + +
    + +
        bit: function(column, length) {
    +      return this._addColumn('bit', column, {length: (length || false)});
    +    },
    + +
  • + + +
  • +
    + +
    +

    Create a new binary column on the table.

    @@ -3177,11 +3227,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    @@ -3190,11 +3240,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new drop index command on the blueprint.

    @@ -3212,11 +3262,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Add a new index command to the blueprint. If no name was specified for this index, we will create one using a basic @@ -3238,11 +3288,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Add a new column to the blueprint.

    @@ -3259,11 +3309,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Add a new command to the blueprint.

    @@ -3279,11 +3329,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Chainable object used in creating SchemaBuilder commands.

    @@ -3298,11 +3348,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Sets the default value for a column.

    @@ -3316,11 +3366,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Sets an integer as unsigned, is a no-op if the column type is not an integer.

    @@ -3335,11 +3385,11 @@ if the column type is not an integer.

  • -
  • +
  • - +

    Allows the column to contain null values.

    @@ -3374,11 +3424,11 @@ if the column type is not an integer.

  • -
  • +
  • - +

    Knex.Raw

    @@ -3387,11 +3437,11 @@ if the column type is not an integer.

  • -
  • +
  • - +
    @@ -3403,11 +3453,11 @@ if the column type is not an integer.

  • -
  • +
  • - +

    Knex.runQuery

    @@ -3416,11 +3466,11 @@ if the column type is not an integer.

  • -
  • +
  • - +

    Query runner, the context of this function is set to the caller, (either Builder or SchemaBuilder). Checks and fails on an already @@ -3445,11 +3495,11 @@ and returns a deferred promise.

  • -
  • +
  • - +

    Export the Knex module

    From bab186562e5e1fbfac521b31918d7763597c3fff Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Mon, 22 Apr 2013 09:06:07 -0400 Subject: [PATCH 05/13] allowing tinyint as alias for tinyInteger --- docs/knex.html | 121 ++++++++++++++++++++++++++++--------------------- knex.js | 5 ++ 2 files changed, 74 insertions(+), 52 deletions(-) diff --git a/docs/knex.html b/docs/knex.html index bd8c2580..4a44f3cd 100644 --- a/docs/knex.html +++ b/docs/knex.html @@ -3011,6 +3011,23 @@ the blueprint element, so we'll just call that compilers function.

    +

    Alias for tinyinteger column.

    + +
    + +
        tinyint: function(column) {
    +      return this._addColumn('tinyInteger', column);
    +    },
    + +
  • + + +
  • +
    + +
    + +

    Create a new float column on the table.

    @@ -3022,11 +3039,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new decimal column on the table.

    @@ -3039,11 +3056,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new boolean column on the table.

    @@ -3056,11 +3073,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Alias to "boolean".

    @@ -3073,11 +3090,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new date column on the table.

    @@ -3090,11 +3107,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new date-time column on the table.

    @@ -3107,11 +3124,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new time column on the table.

    @@ -3124,11 +3141,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new timestamp column on the table.

    @@ -3141,11 +3158,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Add creation and update timestamps to the table.

    @@ -3159,11 +3176,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new enum column on the table.

    @@ -3176,11 +3193,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Alias to enum.

    @@ -3193,11 +3210,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new bit column on the table.

    @@ -3210,11 +3227,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new binary column on the table.

    @@ -3227,11 +3244,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    @@ -3240,11 +3257,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new drop index command on the blueprint.

    @@ -3262,11 +3279,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Add a new index command to the blueprint. If no name was specified for this index, we will create one using a basic @@ -3288,11 +3305,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Add a new column to the blueprint.

    @@ -3309,11 +3326,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Add a new command to the blueprint.

    @@ -3329,11 +3346,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Chainable object used in creating SchemaBuilder commands.

    @@ -3348,11 +3365,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Sets the default value for a column.

    @@ -3366,11 +3383,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Sets an integer as unsigned, is a no-op if the column type is not an integer.

    @@ -3385,11 +3402,11 @@ if the column type is not an integer.

  • -
  • +
  • - +

    Allows the column to contain null values.

    @@ -3424,11 +3441,11 @@ if the column type is not an integer.

  • -
  • +
  • - +

    Knex.Raw

    @@ -3437,11 +3454,11 @@ if the column type is not an integer.

  • -
  • +
  • - +
    @@ -3453,11 +3470,11 @@ if the column type is not an integer.

  • -
  • +
  • - +

    Knex.runQuery

    @@ -3466,11 +3483,11 @@ if the column type is not an integer.

  • -
  • +
  • - +

    Query runner, the context of this function is set to the caller, (either Builder or SchemaBuilder). Checks and fails on an already @@ -3495,11 +3512,11 @@ and returns a deferred promise.

  • -
  • +
  • - +

    Export the Knex module

    diff --git a/knex.js b/knex.js index a1c88716..f21d596b 100644 --- a/knex.js +++ b/knex.js @@ -1179,6 +1179,11 @@ return this._addColumn('tinyInteger', column); }, + // Alias for tinyinteger column. + tinyint: function(column) { + return this._addColumn('tinyInteger', column); + }, + // Create a new float column on the table. float: function(column, total, places) { return this._addColumn('float', column, {total: (total || 8), places: (places || 2)}); From 779030e7e75c299a2b92ac4672b30cd307d23726 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Thu, 25 Apr 2013 21:04:56 -0400 Subject: [PATCH 06/13] fixing issue with increments id, adding debug for config --- clients/mysql.js | 2 ++ clients/postgres.js | 5 ----- knex.js | 5 ++--- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/clients/mysql.js b/clients/mysql.js index accb6269..4dbbb424 100644 --- a/clients/mysql.js +++ b/clients/mysql.js @@ -48,6 +48,8 @@ exports.initialize = function (options) { // to the database. exports.query = function (querystring, params, callback, connection) { + if (debug) console.log([querystring, params]); + // If there is a connection, use it. if (connection) { return connection.query(querystring, params, callback); diff --git a/clients/postgres.js b/clients/postgres.js index 37edcf32..e6982335 100644 --- a/clients/postgres.js +++ b/clients/postgres.js @@ -100,11 +100,6 @@ var grammar = exports.grammar = { return (value !== '*' ? util.format('"%s"', value) : "*"); }, - compileInsertGetId: function(qb, values, sequence) { - if (!sequence) sequence = 'id'; - return this.compileInsert(qb, values); // + ' returning ' + this.wrap(sequence); - }, - compileTruncate: function (qb) { var query = {}; query['truncate ' + this.wrapTable(qb.from) + ' restart identity'] = []; diff --git a/knex.js b/knex.js index f21d596b..0fbcee4a 100644 --- a/knex.js +++ b/knex.js @@ -641,8 +641,7 @@ insert: function(values, returning) { if (!_.isArray(values)) values = values ? [values] : []; for (var i = 0, l = values.length; i < l; i++) { - var record = values[i]; - this.bindings = this.bindings.concat(_.values(record)); + this.bindings = this.bindings.concat(_.values(values[i])); } var str = this.grammar.compileInsert(this, values); return Knex.runQuery(this, {sql: str, bindings: this._cleanBindings(), type: 'insert'}); @@ -1151,7 +1150,7 @@ // Create a new auto-incrementing column on the table. increments: function(column) { - return this._addColumn('integer', (column || 'id'), {autoIncrement: true}); + return this._addColumn('integer', (column || 'id'), {autoIncrement: true, length: 11}); }, // Create a new string column on the table. From 8a5027621cc67302fc6d43f23d315b07116c9a9b Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Thu, 25 Apr 2013 21:05:15 -0400 Subject: [PATCH 07/13] re-docco --- docs/knex.html | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/knex.html b/docs/knex.html index 4a44f3cd..6af3a184 100644 --- a/docs/knex.html +++ b/docs/knex.html @@ -1589,8 +1589,7 @@ where key = value.

        insert: function(values, returning) {
           if (!_.isArray(values)) values = values ? [values] : [];
           for (var i = 0, l = values.length; i < l; i++) {
    -        var record = values[i];
    -        this.bindings = this.bindings.concat(_.values(record));
    +        this.bindings = this.bindings.concat(_.values(values[i]));
           }
           var str = this.grammar.compileInsert(this, values);
           return Knex.runQuery(this, {sql: str, bindings: this._cleanBindings(), type: 'insert'});
    @@ -2914,7 +2913,7 @@ the blueprint element, so we'll just call that compilers function.

        increments: function(column) {
    -      return this._addColumn('integer', (column || 'id'), {autoIncrement: true});
    +      return this._addColumn('integer', (column || 'id'), {autoIncrement: true, length: 11});
         },
  • From 696859937f4fabcf2658f67175b3b77d198af40f Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Fri, 26 Apr 2013 10:25:32 -0400 Subject: [PATCH 08/13] ensure update/insert hashes are ordered properly for bindings --- index.html | 3 ++- knex.js | 27 ++++++++++++++++++--------- test/builder.js | 22 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/index.html b/index.html index b1c63ffc..8530410f 100644 --- a/index.html +++ b/index.html @@ -12,7 +12,7 @@ font-size: 14px; line-height: 22px; font-family: Helvetica Neue, Helvetica, Arial; - background: #FEFFFC url(docs/images/background.png); + background: #f7f7f7 url(docs/images/background.png); } .interface { font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, sans-serif !important; @@ -336,6 +336,7 @@
    • – connection
    • – createTable
    • +
    • – createTable
    • – dropTable
    • – dropTableIfExists
    • – table
    • diff --git a/knex.js b/knex.js index 0fbcee4a..615ed9c9 100644 --- a/knex.js +++ b/knex.js @@ -248,7 +248,7 @@ // inserts using a single query statement. compileInsert: function(qb, values) { var table = this.wrapTable(qb.table); - var columns = this.columnize(_.keys(values[0])); + var columns = this.columnize(_.keys(values[0]).sort()); var parameters = this.parameterize(_.values(values[0])); var paramBlocks = []; @@ -376,7 +376,7 @@ this.isDistinct = false; }, - toJSON: function () { + toJSON: function() { return { joins: this.joins, wheres: this.wheres, @@ -637,11 +637,14 @@ return this; }, - // Performs an `INSERT` query, returning a promise. + // Performs an `insert` query, returning a promise. insert: function(values, returning) { if (!_.isArray(values)) values = values ? [values] : []; - for (var i = 0, l = values.length; i < l; i++) { - this.bindings = this.bindings.concat(_.values(values[i])); + for (var i = 0, l = values.length; i Date: Fri, 26 Apr 2013 11:28:50 -0400 Subject: [PATCH 09/13] omitting wheres from inserts --- knex.js | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/knex.js b/knex.js index 615ed9c9..b88d93bd 100644 --- a/knex.js +++ b/knex.js @@ -178,12 +178,12 @@ // Compiles a where in clause. whereIn: function(qb, where) { - return this.wrap(where.column) + ' in (' + this.parameterize(where.values) + ')'; + return this.wrap(where.column) + ' in (' + this.parameterize(where.value) + ')'; }, // Compiles a where not in clause. whereNotIn: function(qb, where) { - return this.wrap(where.column) + ' not in (' + this.parameterize(where.values) + ')'; + return this.wrap(where.column) + ' not in (' + this.parameterize(where.value) + ')'; }, // Compiles a sub-where in clause. @@ -250,8 +250,12 @@ var table = this.wrapTable(qb.table); var columns = this.columnize(_.keys(values[0]).sort()); var parameters = this.parameterize(_.values(values[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); + for (var i = 0, l = values.length; i < l; ++i) { paramBlocks.push("(" + parameters + ")"); } @@ -259,6 +263,25 @@ return "insert into " + table + " (" + columns + ") values " + paramBlocks.join(', '); }, + // Depending on the type of `where` clause, this will appropriately + // remove any binding caused by "where" constraints, allowing the same + // query to be used for `insert` and `update` without issue. + _clearWhereBindings: function(qb) { + var wheres = qb.wheres; + var bindingCount = 0; + for (var i = 0, l = wheres.length; i Date: Fri, 26 Apr 2013 13:31:42 -0400 Subject: [PATCH 10/13] bumping q.js --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6bc1459b..870bfedb 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "mysql": "~2.0.0-alpha5" }, "dependencies": { - "q": "0.8.x", + "q": "0.9.x", "underscore": "1.4.x", "generic-pool": "2.0.x" }, From 3c28a90cb5c582a234f62fdc7f9933c039842bf2 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Sun, 28 Apr 2013 20:58:38 -0400 Subject: [PATCH 11/13] working on transaction api --- clients/mysql.js | 14 ++++++++++++++ clients/postgres.js | 17 +++++++++++++++++ clients/sqlite3.js | 2 ++ knex.js | 14 ++++++++++---- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/clients/mysql.js b/clients/mysql.js index 4dbbb424..a9c5884e 100644 --- a/clients/mysql.js +++ b/clients/mysql.js @@ -70,6 +70,20 @@ exports.query = function (querystring, params, callback, connection) { }); }; +exports.beginTransaction = function() { + var connection = exports.getConnection(); + connection.query("start transaction;"); + return connection; +}; + +exports.commitTransaction = function(conn) { + conn.query("commit;"); +}; + +exports.rollbackTransaction = function(conn) { + conn.query("rollback;"); +}; + // TODO: Return table prefix. exports.getTablePrefix = function () {}; diff --git a/clients/postgres.js b/clients/postgres.js index e6982335..18eb90d9 100644 --- a/clients/postgres.js +++ b/clients/postgres.js @@ -43,16 +43,33 @@ exports.initialize = function (options) { }, options.pool)); }; +exports.beginTransaction = function() { + var connection = exports.getConnection(); + connection.query("begin;"); + return connection; +}; + +exports.commitTransaction = function(conn) { + conn.query("commit;"); +}; + +exports.rollbackTransaction = function(conn) { + conn.query("rollback;"); +}; + // Execute a query on the database. // If the fourth parameter is set, this will be used as the connection // to the database. exports.query = function (querystring, params, callback, connection) { + if (debug) console.log([querystring, params]); + // If there is a connection, use it. if (connection) { return connection.query(querystring, params, callback); } + // Bind all of the ? to numbered vars. var questionCount = 0; querystring = querystring.replace(/\?/g, function () { questionCount++; diff --git a/clients/sqlite3.js b/clients/sqlite3.js index f26a3e22..86f15d9e 100644 --- a/clients/sqlite3.js +++ b/clients/sqlite3.js @@ -49,6 +49,8 @@ exports.initialize = function (options) { exports.query = function (querystring, params, callback, connection, type) { + if (debug) console.log([querystring, params]); + // If there is a connection, use it. if (connection) { return connection.run(querystring, params, callback); diff --git a/knex.js b/knex.js index b88d93bd..a030f01d 100644 --- a/knex.js +++ b/knex.js @@ -790,12 +790,12 @@ Knex.Transaction = function(container) { - var connection = Knex.client.getConnection(); - // Initiate a deferred object, so we know when the // transaction completes or fails, we know what to do. var deferred = Q.defer(); + var connection = Knex.client.beginTransaction(); + // Finish the transaction connection var finish = function(type, data) { this.connection.end(); @@ -806,8 +806,14 @@ // Call the container with the transaction // commit & rollback objects container({ - commit: function(data) { finish.call(this, 'resolve', data); }, - rollback: function(data) { finish.call(this, 'reject', data); }, + commit: function(data) { + Knex.client.commitTransaction(connection); + finish.call(this, 'resolve', data); + }, + rollback: function(data) { + Knex.client.rollbackTransaction(connection); + finish.call(this, 'reject', data); + }, connection: connection }); From 3de0a50e11aba645958824d7ac39525832e748c7 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Mon, 29 Apr 2013 10:44:16 -0400 Subject: [PATCH 12/13] finished up an initial version of the transactions api --- clients/mysql.js | 23 +- clients/postgres.js | 17 +- clients/sqlite3.js | 18 +- docs/knex.html | 865 ++++++++++++++++++++++++-------------------- knex.js | 51 +-- 5 files changed, 537 insertions(+), 437 deletions(-) diff --git a/clients/mysql.js b/clients/mysql.js index a9c5884e..f96badab 100644 --- a/clients/mysql.js +++ b/clients/mysql.js @@ -48,7 +48,11 @@ exports.initialize = function (options) { // to the database. exports.query = function (querystring, params, callback, connection) { - if (debug) console.log([querystring, params]); + if (debug) { + var debugVars = [querystring, params]; + if (connection) debugVars.push(connection.__cid); + console.log(debugVars); + } // If there is a connection, use it. if (connection) { @@ -70,18 +74,19 @@ exports.query = function (querystring, params, callback, connection) { }); }; -exports.beginTransaction = function() { - var connection = exports.getConnection(); - connection.query("start transaction;"); - return connection; +exports.beginTransaction = function(callback) { + var connection = this.getConnection(); + this.query("begin;", null, function(err) { + callback(err, connection); + }, connection); }; -exports.commitTransaction = function(conn) { - conn.query("commit;"); +exports.commitTransaction = function(connection, callback) { + this.query("commit;", null, callback, connection); }; -exports.rollbackTransaction = function(conn) { - conn.query("rollback;"); +exports.rollbackTransaction = function(connection, callback) { + this.query("rollback;", null, callback, connection); }; // TODO: Return table prefix. diff --git a/clients/postgres.js b/clients/postgres.js index 18eb90d9..6f651c66 100644 --- a/clients/postgres.js +++ b/clients/postgres.js @@ -43,18 +43,19 @@ exports.initialize = function (options) { }, options.pool)); }; -exports.beginTransaction = function() { - var connection = exports.getConnection(); - connection.query("begin;"); - return connection; +exports.beginTransaction = function(callback) { + var connection = this.getConnection(); + this.query("begin;", null, function(err) { + callback(err, connection); + }, connection); }; -exports.commitTransaction = function(conn) { - conn.query("commit;"); +exports.commitTransaction = function(connection, callback) { + this.query("commit;", null, callback, connection); }; -exports.rollbackTransaction = function(conn) { - conn.query("rollback;"); +exports.rollbackTransaction = function(connection, callback) { + this.query("rollback;", null, callback, connection); }; // Execute a query on the database. diff --git a/clients/sqlite3.js b/clients/sqlite3.js index 86f15d9e..6643ce15 100644 --- a/clients/sqlite3.js +++ b/clients/sqlite3.js @@ -40,8 +40,8 @@ exports.initialize = function (options) { destroy : function(client) { client.close(); }, - max : 10, - min : 2, + max : 1, + min : 1, idleTimeoutMillis: 30000, log : false }, options.pool)); @@ -71,7 +71,21 @@ exports.query = function (querystring, params, callback, connection, type) { }); }); +}; +exports.beginTransaction = function(callback) { + var connection = this.getConnection(); + this.query("begin;", null, function(err) { + callback(err, connection); + }, connection); +}; + +exports.commitTransaction = function(connection, callback) { + this.query("commit;", null, callback, connection); +}; + +exports.rollbackTransaction = function(connection, callback) { + this.query("rollback;", null, callback, connection); }; // Returns a mysql connection, with a __cid property uniquely diff --git a/docs/knex.html b/docs/knex.html index 6af3a184..652de195 100644 --- a/docs/knex.html +++ b/docs/knex.html @@ -482,7 +482,7 @@ including any nested join queries.

        whereIn: function(qb, where) {
    -      return this.wrap(where.column) + ' in (' + this.parameterize(where.values) + ')';
    +      return this.wrap(where.column) + ' in (' + this.parameterize(where.value) + ')';
         },
    @@ -499,7 +499,7 @@ including any nested join queries.

        whereNotIn: function(qb, where) {
    -      return this.wrap(where.column) + ' not in (' + this.parameterize(where.values) + ')';
    +      return this.wrap(where.column) + ' not in (' + this.parameterize(where.value) + ')';
         },
    @@ -672,10 +672,26 @@ inserts using a single query statement.

        compileInsert: function(qb, values) {
           var table = this.wrapTable(qb.table);
    -      var columns = this.columnize(_.keys(values[0]));
    +      var columns = this.columnize(_.keys(values[0]).sort());
           var parameters = this.parameterize(_.values(values[0]));
    -      
    -      var paramBlocks = [];
    +      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);
    +
           for (var i = 0, l = values.length; i < l; ++i) {
             paramBlocks.push("(" + parameters + ")");
           }
    @@ -686,11 +702,42 @@ inserts using a single query statement.

  • -
  • +
  • - + +
    +

    Depending on the type of where clause, this will appropriately +remove any binding caused by "where" constraints, allowing the same +query to be used for insert and update without issue.

    + +
    + +
        _clearWhereBindings: function(qb) {
    +      var wheres = qb.wheres;
    +      var bindingCount = 0;
    +      for (var i = 0, l = wheres.length; i<l; i++) {
    +        var where = wheres[i];
    +        if (_.isArray(where.value)) {
    +          bindingCount += where.value.length;
    +        } else if (where.query) {
    +          bindingCount += where.query.bindings.length;
    +        } else {
    +          bindingCount += 1;
    +        }
    +      }
    +      qb.bindings = qb.bindings.slice(bindingCount);
    +    },
    + +
  • + + +
  • +
    + +
    +

    Compiles an update query.

    @@ -708,11 +755,11 @@ inserts using a single query statement.

  • -
  • +
  • - +

    Compiles a delete query.

    @@ -727,11 +774,11 @@ inserts using a single query statement.

  • -
  • +
  • - +

    Compiles a truncate query.

    @@ -787,11 +834,11 @@ inserts using a single query statement.

  • -
  • +
  • - +

    Knex.Builder

    @@ -800,11 +847,11 @@ inserts using a single query statement.

  • -
  • +
  • - +
    @@ -818,11 +865,11 @@ inserts using a single query statement.

  • -
  • +
  • - +

    All operators used in the where clause generation.

    @@ -835,11 +882,11 @@ inserts using a single query statement.

  • -
  • +
  • - +

    Sets the tableName on the query.

    @@ -854,11 +901,11 @@ inserts using a single query statement.

  • -
  • +
  • - +

    Adds a distinct clause to the query.

    @@ -872,11 +919,11 @@ inserts using a single query statement.

  • -
  • +
  • - +

    Clones the current query builder, including any pieces that have been set thus far

    @@ -900,11 +947,11 @@ pieces that have been set thus far

  • -
  • +
  • - +

    Resets all attributes on the query builder.

    @@ -919,7 +966,7 @@ pieces that have been set thus far

    this.isDistinct = false; }, - toJSON: function () { + toJSON: function() { return { joins: this.joins, wheres: this.wheres, @@ -933,11 +980,11 @@ pieces that have been set thus far

  • -
  • +
  • - +

    Adds a join clause to the query, allowing for advanced joins with an anonymous function as the second argument.

    @@ -961,11 +1008,11 @@ with an anonymous function as the second argument.

  • -
  • +
  • - +

    The where function can be used in several ways: The most basic is where(key, value), which expands to @@ -1007,11 +1054,11 @@ where key = value.

  • -
  • +
  • - +

    Alias to where, for internal builder consistency.

    @@ -1036,11 +1083,11 @@ where key = value.

  • -
  • +
  • - +

    Adds an or where clause to the query.

    @@ -1053,11 +1100,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a where exists clause to the query.

    @@ -1079,11 +1126,11 @@ where key = value.

  • -
  • +
  • - +

    Adds an or where exists clause to the query.

    @@ -1096,11 +1143,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a where not exists clause to the query.

    @@ -1113,11 +1160,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a or where not exists clause to the query.

    @@ -1130,11 +1177,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a where in clause to the query.

    @@ -1148,7 +1195,7 @@ where key = value.

    this.wheres.push({ type: (condition || 'In'), column: column, - values: values, + value: values, bool: bool }); push.apply(this.bindings, values); @@ -1158,11 +1205,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a or where in clause to the query.

    @@ -1175,11 +1222,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a where not in clause to the query.

    @@ -1192,11 +1239,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a or where not in clause to the query.

    @@ -1209,11 +1256,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a where null clause to the query.

    @@ -1227,11 +1274,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a or where null clause to the query.

    @@ -1244,11 +1291,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a where not null clause to the query.

    @@ -1261,11 +1308,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a or where not null clause to the query.

    @@ -1278,11 +1325,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a where between clause to the query.

    @@ -1297,11 +1344,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a or where between clause to the query.

    @@ -1316,11 +1363,11 @@ where key = value.

  • -
  • +
  • - +

    @@ -1329,11 +1376,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a group by clause to the query.

    @@ -1347,11 +1394,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a order by clause to the query.

    @@ -1365,11 +1412,11 @@ where key = value.

  • -
  • +
  • - +

    Adds a having clause to the query.

    @@ -1384,43 +1431,6 @@ where key = value.

  • -
  • -
    - -
    - -
    -
    - -
    - -
  • - - -
  • -
    - -
    - -
    - -
    - -
        offset: function(value) {
    -      if (value == null) return this.isOffset;
    -      this.isOffset = value;
    -      return this;
    -    },
    -
    -    limit: function(value) {
    -      if (value == null) return this.isLimit;
    -      this.isLimit = value;
    -      return this;
    -    },
    - -
  • - -
  • @@ -1440,6 +1450,43 @@ where key = value.

    + +
    + +
        offset: function(value) {
    +      if (value == null) return this.isOffset;
    +      this.isOffset = value;
    +      return this;
    +    },
    +
    +    limit: function(value) {
    +      if (value == null) return this.isLimit;
    +      this.isLimit = value;
    +      return this;
    +    },
    + +
  • + + +
  • +
    + +
    + +
    +
    + +
    + +
  • + + +
  • +
    + +
    + +

    Determine if any rows exist for the current query.

    @@ -1453,11 +1500,11 @@ where key = value.

  • -
  • +
  • - +

    Retrieve the "count" result of the query.

    @@ -1470,11 +1517,11 @@ where key = value.

  • -
  • +
  • - +

    Retrieve the minimum value of a given column.

    @@ -1487,11 +1534,11 @@ where key = value.

  • -
  • +
  • - +

    Retrieve the maximum value of a given column.

    @@ -1504,11 +1551,11 @@ where key = value.

  • -
  • +
  • - +

    Retrieve the sum of the values of a given column.

    @@ -1521,11 +1568,11 @@ where key = value.

  • -
  • +
  • - +

    Increments a column's value by the specified amount.

    @@ -1538,11 +1585,11 @@ where key = value.

  • -
  • +
  • - +

    Decrements a column's value by the specified amount.

    @@ -1555,11 +1602,11 @@ where key = value.

  • -
  • +
  • - +

    Performs a select query, returning a promise.

    @@ -1576,20 +1623,23 @@ where key = value.

  • -
  • +
  • - +
    -

    Performs an INSERT query, returning a promise.

    +

    Performs an insert query, returning a promise.

        insert: function(values, returning) {
           if (!_.isArray(values)) values = values ? [values] : [];
    -      for (var i = 0, l = values.length; i < l; i++) {
    -        this.bindings = this.bindings.concat(_.values(values[i]));
    +      for (var i = 0, l = values.length; i<l; i++) {
    +        var obj = sortObject(values[i]);
    +        for (var i2 = 0, l2 = obj.length; i2 < l2; i2++) {
    +          this.bindings.push(obj[i2][1]);
    +        }
           }
           var str = this.grammar.compileInsert(this, values);
           return Knex.runQuery(this, {sql: str, bindings: this._cleanBindings(), type: 'insert'});
    @@ -1598,11 +1648,11 @@ where key = value. 

  • -
  • +
  • - +

    Performs an update query, returning a promise.

    @@ -1616,11 +1666,11 @@ where key = value.

  • -
  • +
  • - +

    Executes a delete statement on the query;

    @@ -1633,11 +1683,11 @@ where key = value.

  • -
  • +
  • - +

    Alias to delete

    @@ -1650,11 +1700,11 @@ where key = value.

  • -
  • +
  • - +

    Truncate

    @@ -1667,11 +1717,11 @@ where key = value.

  • -
  • +
  • - +

    Set by transacting - contains the object with the connection needed to execute a transaction

    @@ -1683,11 +1733,11 @@ needed to execute a transaction

  • -
  • +
  • - +

    Sets the current Builder connection to that of the the currently running transaction

    @@ -1702,11 +1752,11 @@ the currently running transaction

  • -
  • +
  • - +

    @@ -1715,11 +1765,11 @@ the currently running transaction

  • -
  • +
  • - +
    @@ -1740,7 +1790,7 @@ the currently running transaction

    query.table = this.table; callback.call(query, query); this.wheres.push({type: 'Nested', query: query, bool: bool}); - this.bindings = this.bindings.concat(query.bindings); + push.apply(this.bindings, query.bindings); return this; }, @@ -1773,11 +1823,11 @@ the currently running transaction

  • -
  • +
  • - +

    Returns all bindings excluding the Knex.Raw types.

    @@ -1794,11 +1844,11 @@ the currently running transaction

  • -
  • +
  • - +

    Knex.JoinClause

    @@ -1807,11 +1857,11 @@ the currently running transaction

  • -
  • +
  • - +
    @@ -1839,11 +1889,11 @@ the currently running transaction

  • -
  • +
  • - +

    Knex.Transaction

    @@ -1852,53 +1902,16 @@ the currently running transaction

  • -
  • -
    - -
    - -
    - -
    - -
      Knex.Transaction = function(container) {
    -
    -    var connection = Knex.client.getConnection();
    - -
  • - - -
  • -
    - -
    - -
    -

    Initiate a deferred object, so we know when the -transaction completes or fails, we know what to do.

    - -
    - -
        var deferred = Q.defer();
    - -
  • - -
  • -

    Finish the transaction connection

    - +
    -
        var finish = function(type, data) {
    -      this.connection.end();
    -      this.transaction.connection = null;
    -      deferred[type](data);
    -    };
    +
      Knex.Transaction = function(container) {
  • @@ -1909,19 +1922,14 @@ transaction completes or fails, we know what to do.

    -

    Call the container with the transaction -commit & rollback objects

    +

    Initiate a deferred object, so we know when the +transaction completes or fails, we know what to do.

    -
        container({
    -      commit: function(data) { finish.call(this, 'resolve', data); },
    -      rollback: function(data) { finish.call(this, 'reject', data); },
    -      connection: connection
    -    });
    +            
        var deferred = Q.defer();
     
    -    return deferred.promise;
    -  };
    + Knex.client.beginTransaction(function(err, connection) {
    @@ -1929,13 +1937,19 @@ commit & rollback objects

  • -
    +
    -

    Knex.Schema

    +

    Finish the transaction connection

    +
          var finish = function(type, data) {
    +        connection.end();
    +        this.connection = null;
    +        deferred[type](data);
    +      };
    +
  • @@ -1945,6 +1959,56 @@ commit & rollback objects

    +

    Call the container with the transaction +commit & rollback objects

    + + + +
          container({
    +        commit: function(data) {
    +          var transaction = this;
    +          Knex.client.commitTransaction(connection, function(err) {
    +            if (err) throw new Error(err);
    +            finish.call(transaction, 'resolve', data);
    +          });
    +        },
    +        rollback: function(data) {
    +          var transaction = this;
    +          Knex.client.rollbackTransaction(connection, function(err) {
    +            if (err) throw new Error(err);
    +            finish.call(transaction, 'reject', data);
    +          });
    +        },
    +        connection: connection
    +      });
    +
    +    });
    +
    +    return deferred.promise;
    +  };
    + + + + +
  • +
    + +
    + +
    +

    Knex.Schema

    + +
    + +
  • + + +
  • +
    + +
    + +

    Top level object for Schema related functions

    @@ -1954,11 +2018,11 @@ commit & rollback objects

  • -
  • +
  • - +

    Attach main static methods, which passthrough to the SchemaBuilder instance methods

    @@ -1976,11 +2040,11 @@ SchemaBuilder instance methods

  • -
  • +
  • - +

    Knex.SchemaBuilder

    @@ -1989,11 +2053,11 @@ SchemaBuilder instance methods

  • -
  • +
  • - +
    @@ -2011,11 +2075,11 @@ SchemaBuilder instance methods

  • -
  • +
  • - +

    Connection

    @@ -2032,11 +2096,11 @@ SchemaBuilder instance methods

  • -
  • +
  • - +

    Used before a builder call, specifying if this call is nested inside a transaction

    @@ -2051,11 +2115,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Determine if the given table exists.

    @@ -2078,11 +2142,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Modify a table on the schema.

    @@ -2095,11 +2159,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Create a new table on the schema.

    @@ -2115,11 +2179,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Drop a table from the schema.

    @@ -2134,11 +2198,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Drop a table from the schema if it exists.

    @@ -2153,11 +2217,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Rename a table on the schema.

    @@ -2172,11 +2236,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Execute the blueprint to build / modify the table.

    @@ -2189,11 +2253,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Create a new command set with a Closure.

    @@ -2207,11 +2271,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Knex.SchemaGrammar

    @@ -2220,11 +2284,11 @@ is nested inside a transaction

  • -
  • +
  • - +
    @@ -2238,11 +2302,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    Compile a foreign key command.

    @@ -2255,11 +2319,11 @@ is nested inside a transaction

  • -
  • +
  • - +

    We need to prepare several of the elements of the foreign key definition before we can create the SQL, such as wrapping the tables and convert @@ -2276,11 +2340,11 @@ an array of columns to comma-delimited strings for the SQL queries.

  • -
  • +
  • - +

    Once we have the basic foreign key creation statement constructed we can build out the syntax for what should happen on an update or delete of @@ -2297,11 +2361,11 @@ the affected columns, which will get something like "cascade", etc.

    -
  • +
  • - +

    Each of the column types have their own compiler functions which are responsible for turning the column definition into its SQL format @@ -2323,11 +2387,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Add the column modifiers to the definition.

    @@ -2347,11 +2411,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Get the primary key command if it exists on the blueprint.

    @@ -2365,11 +2429,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Get all of the commands with a given name.

    @@ -2382,11 +2446,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Get the SQL for the column data type.

    @@ -2399,11 +2463,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Add a prefix to an array of values.

    @@ -2416,11 +2480,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Wrap a table in keyword identifiers.

    @@ -2434,11 +2498,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Wrap a value in keyword identifiers.

    @@ -2452,11 +2516,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Format a value so that it can be used in "default" clauses.

    @@ -2473,11 +2537,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Knex.SchemaBlueprint

    @@ -2486,11 +2550,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +
    @@ -2517,11 +2581,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Get the raw sql statements for the blueprint.

    @@ -2532,11 +2596,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Add the commands that are implied by the blueprint.

    @@ -2549,11 +2613,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    Add indicies

    @@ -2571,11 +2635,11 @@ for the platform. Then column modifiers are compiled and added.

  • -
  • +
  • - +

    If the index has been specified on the given column, but is simply equal to "true" (boolean), no name has been specified for this @@ -2590,11 +2654,11 @@ index, so we will simply call the index methods without one.

  • -
  • +
  • - +

    If the index has been specified on the column and it is something other than boolean true, we will assume a name was provided on @@ -2614,11 +2678,11 @@ the index specification, and pass in the name to the method.

  • -
  • +
  • - +

    Each type of command has a corresponding compiler function on the schema grammar which is used to build the necessary SQL statements to build @@ -2641,11 +2705,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Determine if the blueprint has a create command.

    @@ -2663,11 +2727,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the table needs to be created.

    @@ -2680,11 +2744,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the table should be dropped.

    @@ -2697,11 +2761,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the table should be dropped if it exists.

    @@ -2714,11 +2778,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the given columns should be dropped.

    @@ -2732,11 +2796,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the given columns should be dropped.

    @@ -2749,11 +2813,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the given primary key should be dropped.

    @@ -2766,11 +2830,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the given unique key should be dropped.

    @@ -2783,11 +2847,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the given index should be dropped.

    @@ -2800,11 +2864,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Indicate that the given foreign key should be dropped.

    @@ -2817,11 +2881,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Rename the table to a given name.

    @@ -2834,11 +2898,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Specify the primary key(s) for the table.

    @@ -2851,11 +2915,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Specify a unique index for the table.

    @@ -2868,11 +2932,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Specify an index for the table.

    @@ -2885,11 +2949,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Specify a foreign key for the table.

    @@ -2902,11 +2966,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new auto-incrementing column on the table.

    @@ -2919,11 +2983,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new string column on the table.

    @@ -2936,11 +3000,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Alias varchar to string

    @@ -2953,11 +3017,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new text column on the table.

    @@ -2970,11 +3034,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new integer column on the table.

    @@ -2987,11 +3051,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new tinyinteger column on the table.

    @@ -3004,11 +3068,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Alias for tinyinteger column.

    @@ -3021,11 +3085,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new float column on the table.

    @@ -3038,11 +3102,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new decimal column on the table.

    @@ -3055,11 +3119,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new boolean column on the table.

    @@ -3072,11 +3136,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Alias to "boolean".

    @@ -3089,11 +3153,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new date column on the table.

    @@ -3106,11 +3170,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new date-time column on the table.

    @@ -3123,11 +3187,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new time column on the table.

    @@ -3140,11 +3204,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new timestamp column on the table.

    @@ -3157,11 +3221,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Add creation and update timestamps to the table.

    @@ -3175,11 +3239,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new enum column on the table.

    @@ -3192,11 +3256,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Alias to enum.

    @@ -3209,11 +3273,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new bit column on the table.

    @@ -3226,11 +3290,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new binary column on the table.

    @@ -3243,11 +3307,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    @@ -3256,11 +3320,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Create a new drop index command on the blueprint.

    @@ -3278,11 +3342,11 @@ the blueprint element, so we'll just call that compilers function.

  • -
  • +
  • - +

    Add a new index command to the blueprint. If no name was specified for this index, we will create one using a basic @@ -3296,7 +3360,7 @@ index type, such as primary or index, which makes the index unique.

    if (!_.isArray(columns)) columns = columns ? [columns] : []; if (index === null) { var table = this.table.replace(/\.|-/g, '_'); - index = (table + '_' + _.map(columns, function (col) { return col.name; }).join('_') + '_' + type).toLowerCase(); + index = (table + '_' + _.map(columns, function(col) { return col.name; }).join('_') + '_' + type).toLowerCase(); } return this._addCommand(type, {index: index, columns: columns}); },
    @@ -3304,11 +3368,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Add a new column to the blueprint.

    @@ -3325,11 +3389,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Add a new command to the blueprint.

    @@ -3345,11 +3409,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Chainable object used in creating SchemaBuilder commands.

    @@ -3364,11 +3428,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Sets the default value for a column.

    @@ -3382,11 +3446,11 @@ index type, such as primary or index, which makes the index unique.

  • -
  • +
  • - +

    Sets an integer as unsigned, is a no-op if the column type is not an integer.

    @@ -3401,11 +3465,11 @@ if the column type is not an integer.

  • -
  • +
  • - +

    Allows the column to contain null values.

    @@ -3416,17 +3480,17 @@ if the column type is not an integer.

    return this; }, - index: function (name) { + index: function(name) { this.isIndex = name || true; return this; }, - primary: function (name) { + primary: function(name) { this.isPrimary = name || true; return this; }, - unique: function (name) { + unique: function(name) { this.isUnique = name || true; return this; } @@ -3435,35 +3499,12 @@ if the column type is not an integer.

    var capitalize = function(word) { return word.charAt(0).toUpperCase() + word.slice(1); - };
    - -
  • - - -
  • -
    - -
    - -
    -

    Knex.Raw

    + }; -
    - -
  • - - -
  • -
    - -
    - -
    - -
    - -
      Knex.Raw = function(value) {
    -    this.value = value;
    +  var sortObject = function(obj) {
    +    return _.sortBy(_.pairs(obj), function(a) { 
    +      return a[0]; 
    +    });
       };
  • @@ -3475,7 +3516,7 @@ if the column type is not an integer.

    -

    Knex.runQuery

    +

    Knex.Raw

    @@ -3488,6 +3529,35 @@ if the column type is not an integer.

    + + + +
      Knex.Raw = function(value) {
    +    this.value = value;
    +  };
    + + + + +
  • +
    + +
    + +
    +

    Knex.runQuery

    + +
    + +
  • + + +
  • +
    + +
    + +

    Query runner, the context of this function is set to the caller, (either Builder or SchemaBuilder). Checks and fails on an already resolved transaction, otherwise calls the query on the specified client @@ -3496,8 +3566,9 @@ and returns a deferred promise.

      Knex.runQuery = function(builder, data) {
    -    if (builder.transaction && ! builder.transaction.connection) {
    -      return Q.reject(new Error('The transaction has already completed.'));
    +    if (builder.transaction) {
    +      if (!builder.transaction.connection) return Q.reject(new Error('The transaction has already completed.'));
    +      builder.connection = builder.transaction.connection;
         }
         var deferred = Q.defer();
         Knex.client.query(data.sql, (data.bindings || []), function(err, resp) {
    @@ -3511,11 +3582,11 @@ and returns a deferred promise.

  • -
  • +
  • - +

    Export the Knex module

    diff --git a/knex.js b/knex.js index a030f01d..50ca6395 100644 --- a/knex.js +++ b/knex.js @@ -794,27 +794,35 @@ // transaction completes or fails, we know what to do. var deferred = Q.defer(); - var connection = Knex.client.beginTransaction(); + Knex.client.beginTransaction(function(err, connection) { - // Finish the transaction connection - var finish = function(type, data) { - this.connection.end(); - this.transaction.connection = null; - deferred[type](data); - }; + // Finish the transaction connection + var finish = function(type, data) { + connection.end(); + this.connection = null; + deferred[type](data); + }; + + // Call the container with the transaction + // commit & rollback objects + container({ + commit: function(data) { + var transaction = this; + Knex.client.commitTransaction(connection, function(err) { + if (err) throw new Error(err); + finish.call(transaction, 'resolve', data); + }); + }, + rollback: function(data) { + var transaction = this; + Knex.client.rollbackTransaction(connection, function(err) { + if (err) throw new Error(err); + finish.call(transaction, 'reject', data); + }); + }, + connection: connection + }); - // Call the container with the transaction - // commit & rollback objects - container({ - commit: function(data) { - Knex.client.commitTransaction(connection); - finish.call(this, 'resolve', data); - }, - rollback: function(data) { - Knex.client.rollbackTransaction(connection); - finish.call(this, 'reject', data); - }, - connection: connection }); return deferred.promise; @@ -1391,8 +1399,9 @@ // resolved transaction, otherwise calls the query on the specified client // and returns a deferred promise. Knex.runQuery = function(builder, data) { - if (builder.transaction && ! builder.transaction.connection) { - return Q.reject(new Error('The transaction has already completed.')); + if (builder.transaction) { + if (!builder.transaction.connection) return Q.reject(new Error('The transaction has already completed.')); + builder.connection = builder.transaction.connection; } var deferred = Q.defer(); Knex.client.query(data.sql, (data.bindings || []), function(err, resp) { From d41e19609f729dba8a96cedefcb85589442c9696 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Mon, 29 Apr 2013 15:52:16 -0400 Subject: [PATCH 13/13] fixing precision on float/decimal to not treat 0 as falsy --- index.html | 30 ++++++++++++++++++++++++++++++ knex.js | 14 +++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index 8530410f..0a2845a4 100644 --- a/index.html +++ b/index.html @@ -720,6 +720,36 @@ Knex('users')

    +Knex.Transaction(function(t) {
    +  
    +  Knex('books')
    +    .transacting(t)
    +    .insert({name: 'Old Books'})
    +    .then(function(row) {
    +      
    +      return Q.all(_.map([
    +        {title: 'Canterbury Tales'},
    +        {title: 'Moby Dick'},
    +        {title: 'Hamlet'}
    +      ], function(info) {
    +
    +        info.row_id = row.id;
    +
    +        // Some validation could take place here.
    +        return Knex('book').transacting(t).insert(info);
    +      
    +      }));
    +
    +    })
    +    .then(t.commit, t.rollback);
    +
    +}).then(function() {
    +  console.log('3 new books saved.');
    +}, function() {
    +  console.log('Error saving the books.');
    +});
    +
    +
     Knex.Transaction(function (t) {
       
       Knex('pieces')
    diff --git a/knex.js b/knex.js
    index 50ca6395..644206ef 100644
    --- a/knex.js
    +++ b/knex.js
    @@ -1224,13 +1224,19 @@
         },
     
         // Create a new float column on the table.
    -    float: function(column, total, places) {
    -      return this._addColumn('float', column, {total: (total || 8), places: (places || 2)});
    +    float: function(column, precision, scale) {
    +      return this._addColumn('float', column, {
    +        precision: (precision == null ? 8 : precision),
    +        scale: (scale == null ? 2 : scale)
    +      });
         },
     
         // Create a new decimal column on the table.
         decimal: function(column, precision, scale) {
    -      return this._addColumn('decimal', column, {precision: (precision || 8), scale: (scale || 2)});
    +      return this._addColumn('decimal', column, {
    +        precision: (precision == null ? 8 : precision),
    +        scale: (scale == null ? 2 : scale)
    +      });
         },
     
         // Create a new boolean column on the table.
    @@ -1292,6 +1298,8 @@
         // ----------------------------------------------------------------------
     
         // Create a new drop index command on the blueprint.
    +    // If the index is an array of columns, the developer means
    +    // to drop an index merely by specifying the columns involved.
         _dropIndexCommand: function(type, index) {
           var columns = [];
           if (_.isArray(index)) {