Skip to content

Commit

Permalink
Simplify transaction classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Sep 12, 2016
1 parent 30342c3 commit 53c5984
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 177 deletions.
45 changes: 11 additions & 34 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,71 +51,48 @@ assign(Client.prototype, {
return new Formatter(this)
},

QueryBuilder,

queryBuilder() {
return new this.QueryBuilder(this)
return new QueryBuilder(this)
},

QueryCompiler,

queryCompiler(builder) {
return new this.QueryCompiler(this, builder)
return new QueryCompiler(this, builder)
},

SchemaBuilder,

schemaBuilder() {
return new this.SchemaBuilder(this)
return new SchemaBuilder(this)
},

SchemaCompiler,

schemaCompiler(builder) {
return new this.SchemaCompiler(this, builder)
return new SchemaCompiler(this, builder)
},

TableBuilder,

tableBuilder(type, tableName, fn) {
return new this.TableBuilder(this, type, tableName, fn)
return new TableBuilder(this, type, tableName, fn)
},

TableCompiler,

tableCompiler(tableBuilder) {
return new this.TableCompiler(this, tableBuilder)
return new TableCompiler(this, tableBuilder)
},

ColumnBuilder,

columnBuilder(tableBuilder, type, args) {
return new this.ColumnBuilder(this, tableBuilder, type, args)
return new ColumnBuilder(this, tableBuilder, type, args)
},

ColumnCompiler,

columnCompiler(tableBuilder, columnBuilder) {
return new this.ColumnCompiler(this, tableBuilder, columnBuilder)
return new ColumnCompiler(this, tableBuilder, columnBuilder)
},

Runner,

runner(connection) {
return new this.Runner(this, connection)
return new Runner(this, connection)
},

Transaction,

transaction(container, config, outerTx) {
return new this.Transaction(this, container, config, outerTx)
return new Transaction(this, container, config, outerTx)
},

Raw,

raw() {
const raw = new this.Raw(this)
return raw.set.apply(raw, arguments)
return new Raw(this).set(...arguments)
},

_formatQuery(sql, bindings, timeZone) {
Expand Down
4 changes: 3 additions & 1 deletion src/dialects/maria/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ assign(Client_MariaSQL.prototype, {

driverName: 'mariasql',

Transaction,
transaction() {
return new Transaction(this, ...arguments)
},

_driver() {
return require('mariasql')
Expand Down
14 changes: 2 additions & 12 deletions src/dialects/maria/transaction.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@

import inherits from 'inherits';
import Debug from 'debug';
import { assign } from 'lodash'
import Transaction from '../../transaction';
import * as helpers from '../../helpers';

const debug = Debug('knex:tx');

function Transaction_Maria() {
Transaction.apply(this, arguments)
}
inherits(Transaction_Maria, Transaction)

assign(Transaction_Maria.prototype, {
export default class Transaction_Maria extends Transaction {

query(conn, sql, status, value) {
const t = this
Expand All @@ -39,6 +31,4 @@ assign(Transaction_Maria.prototype, {
return q;
}

})

export default Transaction_Maria
}
4 changes: 3 additions & 1 deletion src/dialects/mssql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ assign(Client_MSSQL.prototype, {
return require('mssql');
},

Transaction,
transaction() {
return new Transaction(this, ...arguments)
},

QueryCompiler,

Expand Down
27 changes: 8 additions & 19 deletions src/dialects/mssql/transaction.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,45 @@

import inherits from 'inherits';
import Promise from 'bluebird';
import Transaction from '../../transaction';
const debug = require('debug')('knex:tx')

import { assign } from 'lodash'

function Transaction_MSSQL() {
Transaction.apply(this, arguments)
}
inherits(Transaction_MSSQL, Transaction)

assign(Transaction_MSSQL.prototype, {
export default class Transaction_MSSQL extends Transaction {

begin(conn) {
debug('%s: begin', this.txid)
return conn.tx_.begin()
.then(this._resolver, this._rejecter)
},
}

savepoint(conn) {
debug('%s: savepoint at', this.txid)
return Promise.resolve()
.then(() => this.query(conn, `SAVE TRANSACTION ${this.txid}`))
},
}

commit(conn, value) {
this._completed = true
debug('%s: commit', this.txid)
return conn.tx_.commit()
.then(() => this._resolver(value), this._rejecter)
},
}

release(conn, value) {
return this._resolver(value)
},
}

rollback(conn, error) {
this._completed = true
debug('%s: rolling back', this.txid)
return conn.tx_.rollback()
.then(() => this._rejecter(error))
},
}

rollbackTo(conn, error) {
debug('%s: rolling backTo', this.txid)
return Promise.resolve()
.then(() => this.query(conn, `ROLLBACK TRANSACTION ${this.txid}`, 2, error))
.then(() => this._rejecter(error))
},
}

// Acquire a connection and create a disposer - either using the one passed
// via config or getting one off the client. The disposer will be called once
Expand Down Expand Up @@ -84,6 +75,4 @@ assign(Transaction_MSSQL.prototype, {
})
}

})

export default Transaction_MSSQL
}
4 changes: 3 additions & 1 deletion src/dialects/mysql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ assign(Client_MySQL.prototype, {

ColumnCompiler,

Transaction,
transaction() {
return new Transaction(this, ...arguments)
},

_escapeBinding: makeEscape(),

Expand Down
4 changes: 3 additions & 1 deletion src/dialects/mysql2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ assign(Client_MySQL2.prototype, {
// The "dialect", for reference elsewhere.
driverName: 'mysql2',

Transaction,
transaction() {
return new Transaction(this, ...arguments)
},

_driver() {
return require('mysql2')
Expand Down
4 changes: 3 additions & 1 deletion src/dialects/oracle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ assign(Client_Oracle.prototype, {
return require('oracle')
},

Transaction,
transaction() {
return new Transaction(this, ...arguments)
},

formatter() {
return new Oracle_Formatter(this)
Expand Down
22 changes: 6 additions & 16 deletions src/dialects/oracle/transaction.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@

import inherits from 'inherits';
import Promise from 'bluebird';
import Transaction from '../../transaction';
const debugTx = require('debug')('knex:tx')

import { assign } from 'lodash'

function Oracle_Transaction(client, container, config, outerTx) {
Transaction.call(this, client, container, config, outerTx)
}
inherits(Oracle_Transaction, Transaction)

assign(Oracle_Transaction.prototype, {
export default class Oracle_Transaction extends Transaction {

// disable autocommit to allow correct behavior (default is true)
begin() {
return Promise.resolve()
},
}

commit(conn, value) {
this._completed = true
return conn.commitAsync()
.return(value)
.then(this._resolver, this._rejecter)
},
}

release(conn, value) {
return this._resolver(value)
},
}

rollback(conn, err) {
this._completed = true
debugTx('%s: rolling back', this.txid)
return conn.rollbackAsync()
.throw(err)
.catch(this._rejecter)
},
}

acquireConnection(config) {
const t = this
Expand All @@ -56,6 +48,4 @@ assign(Oracle_Transaction.prototype, {
})
}

})

export default Oracle_Transaction
}
34 changes: 15 additions & 19 deletions src/dialects/oracledb/transaction.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
const inherits = require('inherits');
const Promise = require('bluebird');
const Transaction = require('../../transaction');
const debugTx = require('debug')('knex:tx');
import {assign} from
'lodash'

function Oracle_Transaction(client, container, config, outerTx) {
Transaction.call(this, client, container, config, outerTx);
}
inherits(Oracle_Transaction, Transaction);
export default class Oracle_Transaction extends Transaction {

assign(Oracle_Transaction.prototype, {
// disable autocommit to allow correct behavior (default is true)
begin: function() {
begin() {
return Promise.resolve();
},
commit: function(conn, value) {
}

commit(conn, value) {
this._completed = true;
return conn.commitAsync()
.return(value)
.then(this._resolver, this._rejecter);
},
release: function(conn, value) {
}

release(conn, value) {
return this._resolver(value);
},
rollback: function(conn, err) {
}

rollback(conn, err) {
const self = this;
this._completed = true;
debugTx('%s: rolling back', this.txid);
Expand All @@ -33,8 +29,9 @@ assign(Oracle_Transaction.prototype, {
}).then(function() {
self._rejecter(err);
});
},
acquireConnection: function(config) {
}

acquireConnection(config) {
const t = this;
return Promise.try(function() {
return t.client.acquireConnection().completed.then(function(cnx) {
Expand All @@ -57,6 +54,5 @@ assign(Oracle_Transaction.prototype, {
});
});
}
});

module.exports = Oracle_Transaction;
}
4 changes: 3 additions & 1 deletion src/dialects/websql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ inherits(Client_WebSQL, Client_SQLite3);

assign(Client_WebSQL.prototype, {

Transaction,
transaction() {
return new Transaction(this, ...arguments)
},

dialect: 'websql',

Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { assign } from 'lodash'

// The client names we'll allow in the `{name: lib}` pairing.
const aliases = {
'mariadb' : 'maria',
'mariasql' : 'maria',
'pg' : 'postgres',
'postgresql': 'postgres',
'sqlite' : 'sqlite3'
'mariadb' : 'maria',
'mariasql' : 'maria',
'pg' : 'postgres',
'postgresql' : 'postgres',
'sqlite' : 'sqlite3'
};

export default function Knex(config) {
Expand Down
Loading

0 comments on commit 53c5984

Please sign in to comment.