diff --git a/lib/Drivers/DML/axomicmysql.js b/lib/Drivers/DML/axomicmysql.js new file mode 100644 index 00000000..05eccae1 --- /dev/null +++ b/lib/Drivers/DML/axomicmysql.js @@ -0,0 +1,233 @@ +var mysqlHandler = require('mysql-handler-axomic'); +var mysqlH = mysqlHandler.con(); +var Query = require("sql-query").Query; + +exports.Driver = Driver; + +function Driver(config, connection, opts) { + this.config = config || {}; + this.opts = opts || {}; + this.query = new Query("mysql"); + + if (!this.config.timezone) { + // force UTC if not defined, UTC is always better.. + this.config.timezone = "Z"; + } + + this.reconnect(null, connection); + this.aggregate_functions = [ "ABS", "CEIL", "FLOOR", "ROUND", + "AVG", "MIN", "MAX", + "LOG", "LOG2", "LOG10", "EXP", "POWER", + "ACOS", "ASIN", "ATAN", "COS", "SIN", "TAN", + "CONV", [ "RANDOM", "RAND" ], "RADIANS", "DEGREES", + "SUM", "COUNT", + "DISTINCT" ]; + return this; +} + +Driver.prototype.sync = function (opts, cb) { + return require("../DDL/mysql").sync(this, opts, cb); +}; + +Driver.prototype.drop = function (opts, cb) { + return require("../DDL/mysql").drop(this, opts, cb); +}; + +Driver.prototype.ping = function (cb) { + this.db.ping(cb); + return this; +}; + +Driver.prototype.on = function (ev, cb) { + if (ev == "error") { + if(this.db) { + this.db.on("error", cb); + this.db.on("unhandledError", cb); + } + } + return this; +}; + +Driver.prototype.connect = function (cb) { + cb(); +}; + +Driver.prototype.reconnect = function (cb, connection) { + if (typeof cb == "function") { + cb(); + } +}; + + +Driver.prototype.close = function (cb) { + this.db.end(cb); + if (this.opts.pool) { + return cb(); + } +}; + +Driver.prototype.getQuery = function () { + return this.query; +}; + +Driver.prototype.execQuery = function (query, cb) { + if (this.opts.pool) { + this.poolQuery(query, cb); + } else { + this.db.query(query, cb); + } + if (this.opts.debug) { + require("../../Debug").sql('mysql', query); + } +}; + +Driver.prototype.find = function (fields, table, conditions, opts, cb) { + var q = this.query.select() + .from(table).select(fields); + + if (opts.offset) { + q.offset(opts.offset); + } + if (typeof opts.limit == "number") { + q.limit(opts.limit); + } else if (opts.offset) { + // OFFSET cannot be used without LIMIT so we use the biggest BIGINT number possible + q.limit('18446744073709551615'); + } + if (opts.order) { + for (var i = 0; i < opts.order.length; i++) { + q.order(opts.order[i][0], opts.order[i][1]); + } + } + + if (opts.merge) { + q.from(opts.merge.from.table, opts.merge.from.field, opts.merge.to.field).select(opts.merge.select); + if (opts.merge.where && Object.keys(opts.merge.where[1]).length) { + q = q.where(opts.merge.where[0], opts.merge.where[1], conditions); + } else { + q = q.where(conditions); + } + } else { + q = q.where(conditions); + } + + if (opts.exists) { + for (var k in opts.exists) { + q.whereExists(opts.exists[k].table, table, opts.exists[k].link, opts.exists[k].conditions); + } + } + + q = q.build(); + + this.execQuery(q, cb); +}; + +Driver.prototype.count = function (table, conditions, opts, cb) { + var q = this.query.select() + .from(table) + .count(null, 'c'); + + if (opts.merge) { + q.from(opts.merge.from.table, opts.merge.from.field, opts.merge.to.field); + if (opts.merge.where && Object.keys(opts.merge.where[1]).length) { + q = q.where(opts.merge.where[0], opts.merge.where[1], conditions); + } else { + q = q.where(conditions); + } + } else { + q = q.where(conditions); + } + + if (opts.exists) { + for (var k in opts.exists) { + q.whereExists(opts.exists[k].table, table, opts.exists[k].link, opts.exists[k].conditions); + } + } + + q = q.build(); + + this.execQuery(q, cb); +}; + +Driver.prototype.insert = function (table, data, id_prop, cb) { + var q = this.query.insert() + .into(table) + .set(data) + .build(); + + this.execQuery(q, function (err, info) { + if (err) return cb(err); + var ids = {}; + + if (id_prop !== null) { + if (id_prop.length == 1 && info.hasOwnProperty("insertId")) { + ids[id_prop[0]] = info.insertId; + } else { + for (var i = 0; i < id_prop.length; i++) { + ids[id_prop[i]] = data[id_prop[i]]; + } + } + } + return cb(null, ids); + }); +}; + +Driver.prototype.update = function (table, changes, conditions, cb) { + var q = this.query.update() + .into(table) + .set(changes) + .where(conditions) + .build(); + + this.execQuery(q, cb); +}; + +Driver.prototype.remove = function (table, conditions, cb) { + var q = this.query.remove() + .from(table) + .where(conditions) + .build(); + + this.execQuery(q, cb); +}; + +Driver.prototype.clear = function (table, cb) { + var q = "TRUNCATE TABLE " + this.query.escapeId(table); + + this.execQuery(q, cb); +}; + +Driver.prototype.poolQuery = function (query, cb) { + mysqlH.getConnection(this.config, function(coreObj) { + coreObj.connection.query(query, function (err, rows, field) { + return cb(err, rows); + }); + }); +}; + +Driver.prototype.valueToProperty = function (value, property) { + switch (property.type) { + case "boolean": + return !!value; + case "object": + try { + return JSON.parse(value); + } catch (e) { + return null; + } + break; + default: + return value; + } +}; + +Driver.prototype.propertyToValue = function (value, property) { + switch (property.type) { + case "boolean": + return (value) ? 1 : 0; + case "object": + return JSON.stringify(value); + default: + return value; + } +}; diff --git a/lib/Drivers/aliases.js b/lib/Drivers/aliases.js index bf8c9f5e..9c5ac403 100644 --- a/lib/Drivers/aliases.js +++ b/lib/Drivers/aliases.js @@ -1,4 +1,5 @@ module.exports = { postgresql : "postgres", + axomicmysql: "axomicmysql", pg : "postgres" };