Skip to content

Commit

Permalink
Fix default values for mysql driver
Browse files Browse the repository at this point in the history
  • Loading branch information
biggora committed Aug 18, 2013
1 parent fbaec25 commit 4fe0606
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 21 deletions.
17 changes: 4 additions & 13 deletions lib/abstract-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,7 @@ function defineScope(cls, targetClass, name, params, methods) {
cb(null, this.__cachedRelations[name]);
}
};

f._scope = typeof params === 'function' ? params.call(this) : params;
f.build = build;
f.create = create;
Expand Down Expand Up @@ -1361,24 +1362,14 @@ AbstractClass.prototype.inspect = function() {
/**
* Create index in collection
*
* @param {String|Object} fields - index name
* @param {Object} params - indexed fields list { name : 1, created : -1 }
* @param {String|Object} name - index name
* @param {Boolean|Object} unique - if idex unique
* @param {Function} callback - callbacl called with (err, exists: Bool)
*/
AbstractClass.ensureIndex = function ensureIndex(params, name, unique, callback) {
AbstractClass.ensureIndex = function ensureIndex(fields, params, callback) {
if (stillConnecting(this.schema, this, arguments))
return;

if(typeof name === 'function') {
callback = name;
name = null;
unique = null;
} else if (typeof unique === 'function') {
callback = unique;
unique = null;
}

if(typeof callback === 'undefined') {
callback = function(err){ return err; };
}
Expand All @@ -1387,7 +1378,7 @@ AbstractClass.ensureIndex = function ensureIndex(params, name, unique, callback)
if (typeof this.schema.adapter.ensureIndex === 'undefined') {
callback(new Error('Model::ensureIndex not defined for this adapter'));
} else {
this.schema.adapter.ensureIndex(this.modelName, params, callback);
this.schema.adapter.ensureIndex(this.modelName, fields, params, callback);
}
} else {
callback(new Error('Model::ensureIndex requires params argument'));
Expand Down
20 changes: 20 additions & 0 deletions lib/adapters/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,22 @@ function MongoDB(s, schema, callback) {
MongoDB.prototype.define = function(descr) {
if (!descr.settings)
descr.settings = {};
var self = this;
this._models[descr.model.modelName] = descr;
setTimeout(function() {
Object.keys(descr.properties).forEach(function(k) {
if (typeof descr.properties[k].index !== 'undefined' || typeof descr.properties[k].unique !== 'undefined') {
// console.log(descr.model.modelName)
var fields = {}, params = {};
fields[k] = 1;
params['name'] = '_' + k + '_';
if (typeof descr.properties[k].unique !== 'undefined') {
params['unique'] = true;
}
self.collection(descr.model.modelName).ensureIndex(fields, params);
}
});
}, 1000);
};

MongoDB.prototype.defineProperty = function(model, prop, params) {
Expand All @@ -111,6 +126,11 @@ MongoDB.prototype.collection = function(name) {
return this.collections[name];
};

MongoDB.prototype.ensureIndex = function(model, fields, params, callback) {
this.collection(model).ensureIndex(fields, params);
return callback(null);
};

MongoDB.prototype.create = function(model, data, callback) {
if (data.id === null) {
delete data.id;
Expand Down
62 changes: 55 additions & 7 deletions lib/adapters/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ MySQL.prototype.query = function(sql, callback) {
});
};

/**
* Create multi column index callback(err, id)
* @param {Object} model
* @param {Object} fields
* @param {Object} params
* @param {Function} callback
*/
MySQL.prototype.ensureIndex = function(model, fields, params, callback) {
var self = this, sql = "", keyName = params.name || null, afld = [], kind = "";
Object.keys(fields).forEach(function(field) {
if (!keyName) {
keyName = "idx_" + field;
}
afld.push('`' + field + '`');
});
if (params.unique) {
kind = "UNIQUE";
}
// sql = 'USE `' + self.schema.settings.database + '`; ';
sql += 'ALTER TABLE `' + model + '` ADD ' + kind + ' INDEX `' + keyName + '` (' + afld.join(', ') + ');';

self.command(sql, callback);
};

/**
* Must invoke callback(err, id)
* @param {Object} model
Expand Down Expand Up @@ -452,12 +476,15 @@ MySQL.prototype.alterTable = function(model, actualFields, actualIndexes, done,
}

function changed(newSettings, oldSettings) {
if (oldSettings.Null === 'YES' && (newSettings.allowNull === false || newSettings.null === false))
if (oldSettings.Null === 'YES' && (newSettings.allowNull === false || newSettings.null === false)) {
return true;
if (oldSettings.Null === 'NO' && !(newSettings.allowNull === false || newSettings.null === false))
}
if (oldSettings.Null === 'NO' && (getDefaultValue(newSettings) !== getDefaultValue(oldSettings))) {
return true;
if (oldSettings.Type.toUpperCase() !== datatype(newSettings))
}
if (oldSettings.Type.toUpperCase() !== datatype(newSettings)) {
return true;
}
return false;
}
};
Expand All @@ -471,13 +498,16 @@ MySQL.prototype.propertiesSQL = function(model) {
sql.push('`' + prop + '` ' + self.propertySettingsSQL(model, prop));
});
return sql.join(',\n ');

};

MySQL.prototype.propertySettingsSQL = function(model, prop) {
var p = this._models[model].properties[prop];
return datatype(p) + ' ' +
(p.allowNull === false || p['null'] === false ? 'NOT NULL' : 'NULL');
var p = this._models[model].properties[prop], field = [];
field.push(datatype(p));
field.push(p.allowNull === false || (typeof p['default'] !== 'undefined' && acceptedDefaults(p)) ? 'NOT NULL' : 'NULL');
if (typeof p['default'] !== 'undefined' && acceptedDefaults(p) && typeof p['default'] !== 'function') {
field.push('DEFAULT ' + getDefaultValue(p));
}
return field.join(" ");
};

function datatype(p) {
Expand Down Expand Up @@ -606,4 +636,22 @@ function buildOrderBy(order) {

function buildLimit(limit, offset) {
return 'LIMIT ' + (offset ? (offset + ', ' + limit) : limit);
}

function acceptedDefaults(prop) {
if (/^INT|^VAR|^TINY/i.test(datatype(prop))) {
return true;
} else {
return false;
}
}

function getDefaultValue(prop) {
if (/^INT/i.test(prop.Type || datatype(prop))) {
return parseInt(prop['default'] || prop['Default'] || 0);
} else if (/^TINY/i.test(prop.Type || datatype(prop))) {
return prop['default'] || prop['Default'] ? 1 : 0;
} else {
return "'" + (prop['default'] || prop['Default']) + "'";
}
}
1 change: 1 addition & 0 deletions lib/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ BaseSQL.prototype.disconnect = function disconnect() {
BaseSQL.prototype.automigrate = function(cb) {
var self = this;
var wait = 0;

Object.keys(this._models).forEach(function(model) {
wait += 1;
self.dropTable(model, function() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "caminte",
"description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite",
"version": "0.0.6-5",
"version": "0.0.6-6",
"author": "Aleksej Gordejev <[email protected]> (http://www.gordejev.lv)",
"contributors": [
{
Expand Down

0 comments on commit 4fe0606

Please sign in to comment.