Skip to content

Commit

Permalink
Manual fix of coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Jul 27, 2016
1 parent 2e0236a commit b250ef0
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 43 deletions.
4 changes: 1 addition & 3 deletions lib/adapter.js
Expand Up @@ -19,9 +19,7 @@ module.exports = function createMySQLAdapter(client, settings) {
disconnect: cb => client.end(cb),
closeConnection: () => {
return new Promise(resolve => {
client.end(function() {
resolve();
});
client.end(() => resolve());
});
},
define: spec => informationSchema.registerModel(spec.model.modelName, spec),
Expand Down
18 changes: 8 additions & 10 deletions lib/enum-factory.js
@@ -1,39 +1,37 @@
const EnumFactory = function() {
if (arguments.length > 0) {
const Enum = function Enum(arg) {
if (typeof arg === 'number' && arg % 1 == 0) {
if (typeof arg === 'number' && arg % 1 === 0) {
return Enum._values[arg];
} else if (Enum[arg]) {
return Enum[arg];
} else if (Enum._values.indexOf(arg) !== -1) {
return arg;
} else if (arg === null) {
return null;
} else {
return '';
}
return '';
};
const dxList = [];
dxList.push(''); // Want empty value to be at index 0 to match MySQL Enum values and MySQL non-strict behavior.
for (let arg in arguments) {
arg = String(arguments[arg]);
// Want empty value to be at index 0 to match MySQL Enum values and MySQL non-strict behavior.
dxList.push('');
[].slice.call(arguments).forEach(arg => {
Object.defineProperty(Enum, arg.toUpperCase(), { configurable: false, enumerable: true, value: arg, writable: false });
dxList.push(arg);
}
});
Object.defineProperty(Enum, '_values', { configurable: false, enumerable: false, value: dxList, writable: false });
Object.defineProperty(Enum, '_members', { configurable: false, enumerable: false, value: dxList.slice(1), writable: false });
Object.defineProperty(Enum, '_string', { configurable: false, enumerable: false, value: stringified(Enum), writable: false });
Object.freeze(Enum);
return Enum;
} else {
throw 'No arguments - could not create Enum.';
}
throw 'No arguments - could not create Enum.';
};

function stringified(anEnum) {
const s = [];
for (const i in anEnum._values) {
if (anEnum._values[i] != '') {
if (anEnum._values[i] !== '') {
s.push('\'' + anEnum._values[i] + '\'');
}
}
Expand Down
41 changes: 30 additions & 11 deletions lib/information-schema.js
Expand Up @@ -73,18 +73,33 @@ module.exports = function() {
throw new Error(`Unexpected ("object") type for ${ model }.${ key }`);
}

if (!prop || 'undefined' === typeof val) return val;
if (prop.type.name === 'Number') return val;
if (val === null) return 'NULL';
if (!prop || 'undefined' === typeof val) {
return val;
}

if (prop.type.name === 'Number') {
return val;
}

if (val === null) {
return 'NULL';
}

if (prop.type.name === 'Date') {
if (!val) return 'NULL';
if (!val) {
return 'NULL';
}

if (!val.toUTCString) {
val = new Date(val);
}

return val;
}

if (prop.type.name === 'Boolean') return val ? 1 : 0;
if (prop.type.name === 'Boolean') {
return val ? 1 : 0;
}

return val.toString();
}
Expand Down Expand Up @@ -132,13 +147,15 @@ module.exports = function() {
sql = [ '`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY' ];
}

Object.keys(models[model].properties).forEach(function(prop) {
if (prop === 'id') return;
Object.keys(models[model].properties).forEach(prop => {
if (prop === 'id') {
return;
}
sql.push('`' + prop + '` ' + propertySettingsSQL(model, prop));
});

// Declared in model index property indexes.
Object.keys(models[model].properties).forEach(function(prop) {
Object.keys(models[model].properties).forEach(prop => {
const p = models[model].properties[prop];
const i = p.index;
if (i && !(p.type instanceof Array)) {
Expand All @@ -149,7 +166,7 @@ module.exports = function() {
// Settings might not have an indexes property.
const dxs = models[model].settings.indexes;
if (dxs) {
Object.keys(dxs).forEach(function(prop) {
Object.keys(dxs).forEach(prop => {
const ix = dxs[prop];
sql.push(formatIndexDefinition(prop, ix.kind, ix.type, ix.keys, ix.columns));
});
Expand Down Expand Up @@ -194,7 +211,8 @@ module.exports = function() {
dt = numericOptionsByType(p, dt);
break;
case 'Date':
dt = columnType(p, 'DATETIME'); // Currently doesn't need options.
// Currently doesn't need options.
dt = columnType(p, 'DATETIME');
break;
case 'Boolean':
dt = 'TINYINT(1)';
Expand All @@ -204,7 +222,8 @@ module.exports = function() {
break;
case 'Enum':
dt = 'ENUM(' + p.type._string + ')';
dt = stringOptions(p, dt); // Enum columns can have charset/collation.
// Enum columns can have charset/collation.
dt = stringOptions(p, dt);
break;
}
return dt;
Expand Down
17 changes: 12 additions & 5 deletions lib/lookup-by-query.js
Expand Up @@ -71,8 +71,10 @@ module.exports = function(informationSchema, db) {
});

function buildOrderBy(order) {
if (typeof order === 'string') order = [ order ];
return 'ORDER BY ' + order.map(function(o) {
if (typeof order === 'string') {
order = [ order ];
}
return 'ORDER BY ' + order.map(o => {
const t = o.split(/\s+/);
if (t.length === 1) {
return '`' + o + '`';
Expand All @@ -82,10 +84,15 @@ module.exports = function(informationSchema, db) {
}

function buildGroupBy(group) {
if (typeof group === 'string') group = [ group ];
return 'GROUP BY ' + group.map(function(o) {
if (typeof group === 'string') {
group = [ group ];
}

return 'GROUP BY ' + group.map(o => {
const t = o.split(/\s+/);
if (t.length === 1) return '`' + o + '`';
if (t.length === 1) {
return '`' + o + '`';
}
return '`' + t[0] + '` ' + t[1];
}).join(', ');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/mysql.js
Expand Up @@ -24,15 +24,15 @@ exports.initialize = function initializeSchema(schema, callback) {

function initializeConnection(connection, schema, callback) {
// Attach listeners first
connection.on('error', function(err) {
connection.on('error', err => {
schema.log('connection error', err);
schema.connected = false;
});

if (schema.settings.pool) {
callback();
} else {
connection.connect(function(err) {
connection.connect(err => {
if (err) {
console.log('connection.connect err', err);
console.log('will reconnect in 60 secs');
Expand Down
43 changes: 31 additions & 12 deletions lib/schema-operations.js
Expand Up @@ -199,8 +199,10 @@ module.exports = function(informationSchema, db, settings) {
.map(f => `DROP COLUMN ${ escapeKey(f.Field) }`));

// remove indexes
aiNames.forEach(function(indexName) {
if (indexName === 'id' || indexName === 'PRIMARY') return;
aiNames.forEach(indexName => {
if (indexName === 'id' || indexName === 'PRIMARY') {
return;
}
if (indexNames.indexOf(indexName) === -1 && !m.properties[indexName] || m.properties[indexName] && (!m.properties[indexName].index || m.properties[indexName].type instanceof Array || m.properties[indexName].type.name === 'JSON')) {
sql.push('DROP INDEX `' + indexName + '`');
} else {
Expand All @@ -215,8 +217,10 @@ module.exports = function(informationSchema, db, settings) {
if (m.settings.indexes[indexName].keys) {
m.settings.indexes[indexName].columns = m.settings.indexes[indexName].keys.join(',');
}
m.settings.indexes[indexName].columns.split(/,\s*/).forEach(function(columnName, i) {
if (ai[indexName].columns[i] !== columnName) orderMatched = false;
m.settings.indexes[indexName].columns.split(/,\s*/).forEach((columnName, i) => {
if (ai[indexName].columns[i] !== columnName) {
orderMatched = false;
}
});
}
if (!orderMatched) {
Expand Down Expand Up @@ -263,17 +267,32 @@ module.exports = function(informationSchema, db, settings) {
}

function changed(newSettings, oldSettings) {
if (oldSettings.Null === 'YES') { // Used to allow null and does not now.
if (newSettings.allowNull === false) return true;
if (newSettings.null === false) return true;
// Used to allow null and does not now.
if (oldSettings.Null === 'YES') {
if (newSettings.allowNull === false) {
return true;
}
if (newSettings.null === false) {
return true;
}
}
// Did not allow null and now does.
if (oldSettings.Null === 'NO') {
if (newSettings.allowNull === true) {
return true;
}
if (newSettings.null === true) {
return true;
}
if (typeof newSettings.null === 'undefined' && typeof newSettings.allowNull === 'undefined') {
return true;
}
}
if (oldSettings.Null === 'NO') { // Did not allow null and now does.
if (newSettings.allowNull === true) return true;
if (newSettings.null === true) return true;
if (typeof newSettings.null === 'undefined' && typeof newSettings.allowNull === 'undefined') return true;

if (oldSettings.Type.toUpperCase() !== dataType(newSettings).toUpperCase()) {
return true;
}

if (oldSettings.Type.toUpperCase() !== dataType(newSettings).toUpperCase()) return true;
return false;
}
}
Expand Down

0 comments on commit b250ef0

Please sign in to comment.