Skip to content

Commit

Permalink
bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre tiertant committed Dec 30, 2020
1 parent bd8fe35 commit 0c4351a
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 86 deletions.
94 changes: 70 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ var util = require('util');
var LOG_QUERIES = false;
var LOG_ERRORS = false;

var warn = function(warning) {
if (LOG_ERRORS) {
console.warning(warning);
}
};

var oracleDialect = require('./lib/dialects/oracle');
var mysqlDialect = require('./lib/dialects/mysql');
var sqlite3Dialect = require('./lib/dialects/sqlite3');
Expand Down Expand Up @@ -42,23 +48,23 @@ module.exports = (function() {
switch (connection.dbType) {
case 'mariadb':
connection.db = connection.database;
client = Knex({client: 'mariadb', connection: connection, debug: LOG_QUERIES});
client = Knex({client: 'mariadb', connection: connection, debug: LOG_QUERIES, log: { warn }});
dialect = new mysqlDialect();
break;
case 'mysql':
client = Knex({client: 'mysql', connection: connection, debug: LOG_QUERIES});
client = Knex({client: 'mysql', connection: connection, debug: LOG_QUERIES, log: { warn }});
dialect = new mysqlDialect();
break;
case 'oracle':
client = Knex({client: 'oracledb', connection: connection, debug: LOG_QUERIES});
client = Knex({client: 'oracledb', connection: connection, debug: LOG_QUERIES, log: { warn }});
dialect = new oracleDialect();
break;
case 'sqlite3':
client = Knex({client: 'sqlite3', connection: connection, debug: LOG_QUERIES, useNullAsDefault: true});
client = Knex({client: 'sqlite3', connection: connection, debug: LOG_QUERIES, log: { warn }, useNullAsDefault: true});
dialect = new sqlite3Dialect();
break;
case 'postgres':
client = Knex({client: 'postgres', connection: connection, debug: LOG_QUERIES});
client = Knex({client: 'postgres', connection: connection, debug: LOG_QUERIES, log: { warn }});
dialect = new postgresDialect();
break;
}
Expand Down Expand Up @@ -93,29 +99,39 @@ module.exports = (function() {
registerTransaction: function(connection, collections, cb) {
var cnx = connections[connection];
var trxId = 'offshore-sql-trx-' + trxIdCount++;
cnx.client.transaction(function(trx) {
transactions[trxId] = {
connection: cnx,
transaction: trx
};
transactions[trxId] = {};
transactions[trxId].connection = cnx;
transactions[trxId].resolver = cnx.client.transaction(function(trx) {
transactions[trxId].transaction = trx;
return cb(null, trxId);
}).catch(function(err) {
if (LOG_ERRORS) {
console.log(err);
}
});
},
commit: function(trxId, collections, cb) {
if (!transactions[trxId]) {
return cb(new Error('No transaction with this id'));
}
transactions[trxId].transaction.commit().asCallback(cb);
transactions[trxId].transaction.commit();
transactions[trxId].resolver.asCallback(function(err) {
if (err) {
return cb(err);
}
delete transactions[trxId];
cb();
});
},
rollback: function(trxId, collections, cb) {
if (!transactions[trxId]) {
return cb(new Error('No transaction with this id'));
}
transactions[trxId].transaction.rollback(new Error('Rollback')).asCallback(cb);
var default_err = new Error('Rollback');
transactions[trxId].transaction.rollback(default_err);
transactions[trxId].resolver.asCallback(function(err) {
if (err && err !== default_err) {
return cb(err);
}
delete transactions[trxId];
cb();
});
},
define: function(connectionName, tableName, definition, cb) {
// Define a new "table" or return connection.collections[tableName];"collection" schema in the data store
Expand Down Expand Up @@ -191,9 +207,12 @@ module.exports = (function() {
if (transaction) {
select.query.transacting(transaction);
}
select.query.then(function(results) {
return cursor.process(results);
}).asCallback(cb);
select.query.asCallback(function(err, results) {
if (err) {
return cb(err);
}
cb(null, cursor.process(results));
});
},
count: function(connectionName, tableName, options, cb) {
var connection;
Expand All @@ -212,12 +231,36 @@ module.exports = (function() {
if (transaction) {
query.transacting(transaction);
}
query.then(function(cnt){ return cnt[0]['cnt']; }).asCallback(function(err, record) {
if (options.groupBy) {
var groupBy = options.groupBy;
if (!_.isArray(groupBy)) {
groupBy = [groupBy];
}
query.asCallback(function(err, cnt) {
if (err) {
return cb(err);
}
cb(null, Utils.cast({type: 'integer'}, record, options));
var reduce_count = function(data, lvl) {
data = _.groupBy(data, groupBy[lvl]);
_.keys(data).forEach(function(key) {
if (lvl === groupBy.length - 1) {
data[key] = data[key][0]['cnt'];
} else {
data[key] = reduce_count(data[key], lvl+1);
}
});
return data;
};
cb(null, reduce_count(cnt, 0));
});
} else {
query.asCallback(function(err, record) {
if (err) {
return cb(err);
}
cb(null, Utils.cast({type: 'integer'}, record[0]['cnt'], options));
});
}
},
drop: function(connectionName, tableName, relations, cb) {
var connection;
Expand Down Expand Up @@ -441,9 +484,12 @@ module.exports = (function() {
if (transaction) {
select.query.transacting(transaction);
}
select.query.then(function(results) {
return cursor.process(results);
}).asCallback(cb);
select.query.asCallback(function(err, results) {
if (err) {
return cb(err);
}
cb(null, cursor.process(results));
});
},
teardown: function(connectionName, cb) {
if(!connections[connectionName]) {
Expand Down
56 changes: 47 additions & 9 deletions lib/dialect.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ GenericDialect.prototype.count = function(connection, collection, opts) {
var options = this.Utils.normalizeCriteria(opts);
var query = new this.CriteriaProcessor(connection, tableName, options, connection.client(tableName)).getQuery();
query.count('* as cnt');
if (options.groupBy) {
if (!_.isArray(options.groupBy)) {
options.groupBy = [options.groupBy];
}
options.groupBy.forEach(function(groupVal) {
query.select(groupVal);
query.groupBy(groupVal);
});
}
return query;
};

Expand Down Expand Up @@ -276,8 +285,19 @@ GenericDialect.prototype.select = function(connection, collection, opts) {
var populated = [];
var relations = _.groupBy(select.options.joins, 'alias');

// avoid exponential rows on joins
this.joinId(select, relations);
var joinIds = {};
var deepSort = _.remove(_.keys(select.options.sort), function(sort) { return sort.includes('.'); });
var i = 1;
_.keys(relations).forEach(function(relation) {
if (deepSort.length <= 1 || !_.find(deepSort, function(sort) { return sort.startsWith(relation + '.'); })) {
joinIds[relation] = {
id: i++
};
}
});

// avoid exponential rows on joins that needn't sort
this.joinId(select, joinIds);

var associationId = 1;
_.forEach(relations, function(relation) {
Expand All @@ -300,6 +320,10 @@ GenericDialect.prototype.select = function(connection, collection, opts) {

if (association.join) {
association.name = association.join.alias;
association.joinId = null;
if (joinIds[association.name] && joinIds[association.name].id) {
association.joinId = joinIds[association.name].id;
}
// normalize tableName
association.join.parent = self.normalizeTableName(association.join.parent);
association.join.child = self.normalizeTableName(association.join.child);
Expand Down Expand Up @@ -575,7 +599,9 @@ GenericDialect.prototype.join = function(select, association) {
var joinAlias = self.createAlias(join.parent + '_' + join.parentKey, join.child + '_' + join.childKey);
select.query.leftJoin(join.child + ' as ' + joinAlias, function() {
this.on(parent + '.' + join.parentKey, '=', joinAlias + '.' + join.childKey);
this.on('joins.id', '=', association.id);
if (association.joinId) {
this.on('joins.id', '=', association.joinId);
}
});
}
else {
Expand All @@ -587,7 +613,9 @@ GenericDialect.prototype.join = function(select, association) {
}
select.query.leftJoin(join.child + ' as ' + join.alias, function() {
this.on(parent + '.' + join.parentKey, '=', join.alias + '.' + join.childKey);
this.on('joins.id', '=', association.id);
if (association.joinId) {
this.on('joins.id', '=', association.joinId);
}
if (join.criteria) {
new self.CriteriaProcessor(connection, join.alias, join.criteria, this, 'on');
}
Expand Down Expand Up @@ -627,11 +655,15 @@ GenericDialect.prototype.joinManyToMany = function(select, association) {
var junctionAlias = self.createAlias(junction.parent + '_' + junction.parentKey, junction.child + '_' + junction.childKey);
select.query.leftJoin(junction.child + ' as ' + junctionAlias, function() {
this.on(select.alias + '.' + junction.parentKey, '=', junctionAlias + '.' + junction.childKey);
this.on('joins.id', '=', association.id);
if (association.joinId) {
this.on('joins.id', '=', association.joinId);
}
});
select.query.leftJoin(join.child + ' as ' + join.alias, function() {
this.on(junctionAlias + '.' + join.parentKey, '=', join.alias + '.' + join.childKey);
this.on('joins.id', '=', association.id);
if (association.joinId) {
this.on('joins.id', '=', association.joinId);
}
if (join.criteria) {
new self.CriteriaProcessor(connection, join.alias, join.criteria, this, 'on');
}
Expand Down Expand Up @@ -666,7 +698,9 @@ GenericDialect.prototype.joinSkipLimit = function(select, association) {

select.query.leftJoin(join.child + ' as ' + join.alias, function() {
this.on(select.alias + '.' + join.parentKey, '=', join.alias + '.' + join.childKey);
this.on('joins.id', '=', association.id);
if (association.joinId) {
this.on('joins.id', '=', association.joinId);
}
if (join.criteria) {
new self.CriteriaProcessor(connection, join.alias, join.criteria, this, 'on');
}
Expand Down Expand Up @@ -774,11 +808,15 @@ GenericDialect.prototype.joinManyToManySkipLimit = function(select, association)

this.from(join.child).leftJoin(join.parent, function() {
this.on(join.parent + '.' + join.parentKey, '=', join.child + '.' + join.childKey);
this.on('joins.id', '=', association.id);
if (association.joinId) {
this.on('joins.id', '=', association.joinId);
}
}).as(join.alias);
}, function() {
this.on(select.alias + '.' + junction.parentKey, join.alias + '.' + junctionAlias);
this.on('joins.id', '=', association.id);
if (association.joinId) {
this.on('joins.id', '=', association.joinId);
}
});

join.select.forEach(function(columnName) {
Expand Down
47 changes: 17 additions & 30 deletions lib/dialects/mysql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,40 +119,25 @@ MysqlDialect.prototype.selectSkipLimit = function(connection,select) {
select.query.limit(select.options.limit);
}
} else {
select.query.andWhere(select.alias + '.' + select.pk, 'IN', connection.client.select('*').from(function() {
var from = this;
this.select(select.pk);
this.from(select.tableName);
new self.CriteriaProcessor(connection, select.tableName, select.options, this);
if (select.options.skip) {
this.offset(select.options.skip);
}
if (select.options.limit) {
this.limit(select.options.limit);
}
if (select.options.sort) {
_.keys(select.options.sort).forEach(function(toSort) {
var direction = select.options.sort[toSort] === 1 ? 'ASC' : 'DESC';
select.query.orderBy(select.alias + '.' + toSort, direction);
});
// Subquery 'order by' needs a limit or the parser ignores it (0xefffffffffffffff is around max javascript parseInt() number)
select.query.limit(0xefffffffffffffff);
}

var parentPk = connection.client.raw('??.??', ['SKLMT', connection.getPk(select.tableName)]);
var sklmtAlias = self.createAlias('SKLMT', select.alias);

if (!select.skipLimitQuery) {
select.skipLimitQuery = connection.client.select('SUBSKLMT.*');
select.skipLimitSubQuery = connection.client.select('SKLMT.*');
}

select.skipLimitSubQuery = this._getDenseRank(connection, sklmtAlias, select.skipLimitSubQuery, [parentPk]);

if (select.options.skip && select.options.limit) {
select.skipLimitQuery.having(function() {
this.andWhere(sklmtAlias, '>', select.options.skip);
this.andWhere(sklmtAlias, '<=', select.options.limit + select.options.skip);
});

} else if (select.options.skip) {
select.skipLimitQuery.having(function() {
this.andWhere(sklmtAlias, '>', select.options.skip);
});
} else if (select.options.limit) {
select.skipLimitQuery.having(function() {
this.andWhere(sklmtAlias, '<=', select.options.limit);
from.orderBy(select.tableName + '.' + toSort, direction);
});
}
this.as('SKLMT');
}));
}
}
};
Expand Down Expand Up @@ -262,7 +247,9 @@ MysqlDialect.prototype.joinManyToManySkipLimit = function(select, association) {
}).as(join.alias);
}, function() {
this.on(select.alias + '.' + junction.parentKey, join.alias + '.' + junctionAlias);
this.on('joins.id', '=', association.id);
if (association.joinId) {
this.on('joins.id', '=', association.joinId);
}
});

join.select.forEach(function(columnName) {
Expand Down

0 comments on commit 0c4351a

Please sign in to comment.