Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/Associations/Many.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,14 @@ function extendInstance(Model, Instance, Driver, association, opts, createInstan
}

if (Associations.length === 0) {
return Driver.remove(association.mergeTable, conditions, cb);
return Driver.remove(association.mergeTable, conditions, Instance.getConnectionId(), cb);
}

for (var i = 0; i < Associations.length; i++) {
util.populateConditions(association.model, Object.keys(association.mergeAssocId), Associations[i], conditions, false);
}

Driver.remove(association.mergeTable, conditions, cb);
Driver.remove(association.mergeTable, conditions, Instance.getConnectionId(), cb);
};

util.populateConditions(Model, Object.keys(association.mergeId), Instance, conditions);
Expand Down Expand Up @@ -393,7 +393,7 @@ function extendInstance(Model, Instance, Driver, association, opts, createInstan
util.populateConditions(Model, Object.keys(association.mergeId), Instance, data);
util.populateConditions(association.model, Object.keys(association.mergeAssocId), Association, data);

Driver.insert(association.mergeTable, data, null, function (err) {
Driver.insert(association.mergeTable, data, null, Instance.getConnectionId(), function (err) {
if (err) {
return cb(err);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ChainFind.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function ChainFind(Model, opts) {
conditions.or.push(or);
}

return opts.driver.remove(opts.table, conditions, cb);
return opts.driver.remove(opts.table, conditions, null, cb);
});
return this;
},
Expand Down
19 changes: 16 additions & 3 deletions lib/Drivers/DML/_shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ module.exports = {
if (arguments.length == 2) {
var query = arguments[0];
var cb = arguments[1];
var connectionId = null;
} else if (arguments.length == 3) {
if (arguments[1].constructor === Array) {
var query = this.query.escape(arguments[0], arguments[1]);
var cb = arguments[2];
}
else {
var query = arguments[0];
var connectionId = arguments[1];
var cb = arguments[2];
}
}
else if (arguments.length == 4) {
var query = this.query.escape(arguments[0], arguments[1]);
var cb = arguments[2];
var connectionId = arguments[2];
var cb = arguments[3];
}
return this.execSimpleQuery(query, cb);
return this.execSimpleQuery(query, connectionId, cb);
},
eagerQuery: function (association, opts, keys, cb) {
var desiredKey = Object.keys(association.field);
Expand All @@ -25,6 +38,6 @@ module.exports = {
.where(association.mergeTable, where)
.build();

this.execSimpleQuery(query, cb);
this.execSimpleQuery(query, null, cb);
}
};
15 changes: 12 additions & 3 deletions lib/Drivers/DML/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var Utilities = require("../../Utilities");
var mongodb = require("mongodb");
var util = require("util");
var _ = require('lodash');
var ORMError = require("../../Error");

exports.Driver = Driver;

Expand All @@ -21,6 +22,14 @@ function Driver(config, connection, opts) {
});
}

Driver.prototype.createPool = function (cb) {
throw new ORMError('NO_SUPPORT', "This driver does not support pool functions");
};

Driver.prototype.releasePool = function (connectionId) {
throw new ORMError('NO_SUPPORT', "This driver does not support pool functions");
};

Driver.prototype.sync = function (opts, cb) {
this.db.createCollection(opts.table, function (err, collection) {
if (err) {
Expand Down Expand Up @@ -185,7 +194,7 @@ Driver.prototype.count = function (table, conditions, opts, cb) {
});
};

Driver.prototype.insert = function (table, data, keyProperties, cb) {
Driver.prototype.insert = function (table, data, keyProperties, connectionId, cb) {
convertToDB(data, this.config.timezone);

return this.db.collection(table).insert(
Expand Down Expand Up @@ -337,7 +346,7 @@ Driver.prototype.hasMany = function (Model, association) {
};
};

Driver.prototype.update = function (table, changes, conditions, cb) {
Driver.prototype.update = function (table, changes, conditions, connectionId, cb) {
convertToDB(changes, this.config.timezone);
convertToDB(conditions, this.config.timezone);

Expand All @@ -354,7 +363,7 @@ Driver.prototype.update = function (table, changes, conditions, cb) {
);
};

Driver.prototype.remove = function (table, conditions, cb) {
Driver.prototype.remove = function (table, conditions, connectionId, cb) {
convertToDB(conditions, this.config.timezone);

return this.db.collection(table).remove(conditions, cb);
Expand Down
73 changes: 53 additions & 20 deletions lib/Drivers/DML/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var mysql = require("mysql");
var Query = require("sql-query").Query;
var shared = require("./_shared");
var DDL = require("../DDL/SQL");
var poolConnection = require("../../PoolConnection");
var ORMError = require("../../Error");

exports.Driver = Driver;

Expand Down Expand Up @@ -92,12 +94,12 @@ Driver.prototype.getQuery = function () {
return this.query;
};

Driver.prototype.execSimpleQuery = function (query, cb) {
Driver.prototype.execSimpleQuery = function (query, connectionId, cb) {
if (this.opts.debug) {
require("../../Debug").sql('mysql', query);
}
if (this.opts.pool) {
this.poolQuery(query, cb);
this.poolQuery(query, connectionId, cb);
} else {
this.db.query(query, cb);
}
Expand Down Expand Up @@ -141,7 +143,7 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {

q = q.build();

this.execSimpleQuery(q, cb);
this.execSimpleQuery(q, null, cb);
};

Driver.prototype.count = function (table, conditions, opts, cb) {
Expand All @@ -168,16 +170,16 @@ Driver.prototype.count = function (table, conditions, opts, cb) {

q = q.build();

this.execSimpleQuery(q, cb);
this.execSimpleQuery(q, null, cb);
};

Driver.prototype.insert = function (table, data, keyProperties, cb) {
Driver.prototype.insert = function (table, data, keyProperties, connectionId, cb) {
var q = this.query.insert()
.into(table)
.set(data)
.build();

this.execSimpleQuery(q, function (err, info) {
this.execSimpleQuery(q, connectionId, function (err, info) {
if (err) return cb(err);

var i, ids = {}, prop;
Expand All @@ -196,47 +198,78 @@ Driver.prototype.insert = function (table, data, keyProperties, cb) {
});
};

Driver.prototype.update = function (table, changes, conditions, cb) {
Driver.prototype.update = function (table, changes, conditions, connectionId, cb) {
var q = this.query.update()
.into(table)
.set(changes)
.where(conditions)
.build();

this.execSimpleQuery(q, cb);
this.execSimpleQuery(q, connectionId, cb);
};

Driver.prototype.remove = function (table, conditions, cb) {
Driver.prototype.remove = function (table, conditions, connectionId, cb) {
var q = this.query.remove()
.from(table)
.where(conditions)
.build();

this.execSimpleQuery(q, cb);
this.execSimpleQuery(q, connectionId, cb);
};

Driver.prototype.clear = function (table, cb) {
var q = "TRUNCATE TABLE " + this.query.escapeId(table);

this.execSimpleQuery(q, cb);
this.execSimpleQuery(q, null, cb);
};

Driver.prototype.poolQuery = function (query, cb) {
Driver.prototype.createPool = function (cb) {
if (!this.opts.pool)
throw new ORMError('NOT_DEFINED', "Pool option need to be enable");
this.db.pool.getConnection(function (err, con) {
if (err) {
if (err)
return cb(err);
return cb(null, poolConnection.addConnection(con));
});
};

Driver.prototype.releasePool = function (connectionId) {
if (!this.opts.pool)
throw new ORMError('NOT_DEFINED', "Pool option need to be enable");
var connection = poolConnection.getConnection(connectionId);
if (connection != null) {
if (connection.release) {
connection.release();
} else {
connection.end();
}
poolConnection.removeConnection(connectionId);
}
return this;
};

con.query(query, function (err, data) {
if (con.release) {
con.release();
} else {
con.end();
Driver.prototype.poolQuery = function (query, connectionId, cb) {
var connection = poolConnection.getConnection(connectionId);
if (connection != null)
connection.query(query, function (err, data) {
return cb(err, data);
});
else
this.db.pool.getConnection(function (err, con) {
if (err) {
return cb(err);
}

return cb(err, data);
con.query(query, function (err, data) {
if (con.release) {
con.release();
} else {
con.end();
}

return cb(err, data);
});
});
});
};

Driver.prototype.valueToProperty = function (value, property) {
Expand Down
Loading