不要怂,就是干,撸起袖子干!

Commit 5db3a650 by Joel Trost Committed by Matt Broadstone

More separation and a template file for abstract

1 parent 65df6b4f
......@@ -11,7 +11,24 @@ var Utils = require('../../utils')
module.exports = (function() {
var QueryGenerator = {
dialect: 'mssql',
get options(){
return SqlGenerator.options;
},
set options (opt) {
SqlGenerator.options = opt;
},
get dialect(){
return SqlGenerator.dialect;
},
set dialect(dial) {
SqlGenerator.dialect = dial;
},
get sequelize(){
return SqlGenerator.sequelize;
},
set sequelize(seq) {
SqlGenerator.sequelize = seq;
},
addSchema: function(param) {
var self = this
......@@ -48,10 +65,22 @@ module.exports = (function() {
return SqlGenerator.getCreateTableSql(tableName, attributes, options);
},
renameTableQuery: function(before, after) {
throwMethodUndefined('renameTableQuery');
},
showTablesQuery: function () {
return SqlGenerator.showTableSql();
},
/*
Returns a rename table query.
Parameters:
- originalTableName: Name of the table before execution.
- futureTableName: Name of the table after execution.
*/
dropTableQuery: function(tableName, options) {
return SqlGenerator.dropTableSql(tableName,options);
},
......@@ -102,13 +131,85 @@ module.exports = (function() {
return SqlGenerator.insertSql(table,valueHash,modelAttributes, options);
},
showIndexQuery: function(tableName, options) {
return SqlGenerator.showIndexSql(tableName, options);
/*
Returns an insert into command for multiple values.
Parameters: table name + list of hashes of attribute-value-pairs.
*/
/* istanbul ignore next */
bulkInsertQuery: function(tableName, attrValueHashes) {
throwMethodUndefined('bulkInsertQuery');
},
/*
Returns an update query.
Parameters:
- tableName -> Name of the table
- values -> A hash with attribute-value-pairs
- where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer
OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own.
*/
updateQuery: function(tableName, attrValueHash, where, options, attributes) {
throwMethodUndefined('updateQuery');
},
/*
Returns a deletion query.
Parameters:
- tableName -> Name of the table
- where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer
OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own.
Options:
- limit -> Maximaum count of lines to delete
- truncate -> boolean - whether to use an 'optimized' mechanism (i.e. TRUNCATE) if available,
note that this should not be the default behaviour because TRUNCATE does not
always play nicely (e.g. InnoDB tables with FK constraints)
(@see http://dev.mysql.com/doc/refman/5.6/en/truncate-table.html).
Note that truncate must ignore limit and where
*/
/* istanbul ignore next */
deleteQuery: function(tableName, where, options) {
throwMethodUndefined('deleteQuery');
},
/*
Returns an update query.
Parameters:
- tableName -> Name of the table
- values -> A hash with attribute-value-pairs
- where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer
OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own.
*/
incrementQuery: function(tableName, attrValueHash, where, options) {
throwMethodUndefined('incrementQuery');
},
nameIndexes: function (indexes, rawTablename) {
return Utils._.map(indexes, function (index) {
if (!index.hasOwnProperty('name')) {
var onlyAttributeNames = index.fields.map(function(attribute) {
return (typeof attribute === 'string') ? attribute : attribute.attribute;
}.bind(this));
index.name = Utils.inflection.underscore(rawTablename + '_' + onlyAttributeNames.join('_'));
}
return index;
});
},
addIndexQuery: function(tableName, attributes, options, rawTablename) {
return SqlGenerator.addIndexSql(tableName, attributes, options, rawTablename);
},
showIndexQuery: function(tableName, options) {
return SqlGenerator.showIndexSql(tableName, options);
},
removeIndexQuery: function(tableName, indexNameOrAttributes) {
return SqlGenerator.removeIndexSql(tableName, indexNameOrAttributes);
},
......@@ -128,10 +229,109 @@ module.exports = (function() {
return result;
},
/*
Returns all auto increment fields of a factory.
*/
/* istanbul ignore next */
findAutoIncrementField: function(factory) {
var fields = [];
for (var name in factory.attributes) {
if (factory.attributes.hasOwnProperty(name)) {
var definition = factory.attributes[name];
if (definition && definition.autoIncrement) {
fields.push(name);
}
}
}
return fields;
},
quoteTable: function(param, as) {
throwMethodUndefined('quoteTable');
},
quote: function(obj, parent, force) {
throwMethodUndefined('quote');
},
/*
Create a trigger
*/
/* istanbul ignore next */
createTrigger: function(tableName, triggerName, timingType, fireOnArray, functionName, functionParams,
optionsArray) {
throwMethodUndefined('createTrigger');
},
/*
Drop a trigger
*/
/* istanbul ignore next */
dropTrigger: function(tableName, triggerName) {
throwMethodUndefined('dropTrigger');
},
/*
Rename a trigger
*/
/* istanbul ignore next */
renameTrigger: function(tableName, oldTriggerName, newTriggerName) {
throwMethodUndefined('renameTrigger');
},
/*
Create a function
*/
/* istanbul ignore next */
createFunction: function(functionName, params, returnType, language, body, options) {
throwMethodUndefined('createFunction');
},
describeTableQuery: function(tableName, schema, schemaDelimiter) {
return SqlGenerator.describeTableSql(tableName, schema, schemaDelimiter);
},
/*
Drop a function
*/
/* istanbul ignore next */
dropFunction: function(functionName, params) {
throwMethodUndefined('dropFunction');
},
/*
Rename a function
*/
/* istanbul ignore next */
renameFunction: function(oldFunctionName, params, newFunctionName) {
throwMethodUndefined('renameFunction');
},
/*
Escape an identifier (e.g. a table or attribute name)
*/
/* istanbul ignore next */
quoteIdentifier: function(identifier, force) {
throwMethodUndefined('quoteIdentifier');
},
/*
Split an identifier into .-separated tokens and quote each part
*/
quoteIdentifiers: function(identifiers, force) {
throwMethodUndefined('quoteIdentifiers');
},
/*
Escape a value (e.g. a string, number or date)
*/
escape: function(value, field) {
throwMethodUndefined('escape');
},
/**
* Generates an SQL query that returns all foreign keys of a table.
*
......@@ -667,8 +867,59 @@ module.exports = (function() {
}
}
return query;
},
/*
Takes something and transforms it into values of a where condition.
*/
getWhereConditions: function(smth, tableName, factory, options, prepend) {
throwMethodUndefined('getWhereConditions');
},
prependTableNameToHash: function(tableName, hash) {
throwMethodUndefined('prependTableNameToHash');
},
findAssociation: function(attribute, dao) {
throwMethodUndefined('findAssociation');
},
getAssociationFilterDAO: function(filterStr, dao) {
throwMethodUndefined('getAssociationFilterDAO');
},
isAssociationFilter: function(filterStr, dao, options) {
throwMethodUndefined('isAssociationFilter');
},
getAssociationFilterColumn: function(filterStr, dao, options) {
throwMethodUndefined('getAssociationFilterColumn');
},
getConditionalJoins: function(options, originalDao) {
throwMethodUndefined('getConditionalJoins');
},
arrayValue: function(value, key, _key, factory, logicResult) {
throwMethodUndefined('arrayValue');
},
/*
Takes a hash and transforms it into a mysql where condition: {key: value, key2: value2} ==> key=value AND key2=value2
The values are transformed by the relevant datatype.
*/
hashToWhereConditions: function(hash, dao, options) {
throwMethodUndefined('hashToWhereConditions');
},
booleanValue: function(value) {
return value;
}
};
return Utils._.extend(Utils._.clone(require('../mysql/query-generator')), QueryGenerator);
/* istanbul ignore next */
var throwMethodUndefined = function(methodName) {
throw new Error('The method "' + methodName + '" is not defined! Please add it to your sql dialect.');
};
return QueryGenerator;
})();
......@@ -2,7 +2,10 @@
var Utils = require('../../utils')
, SqlString = require('../../sql-string')
, DataTypes = require('./data-types');
, DataTypes = require('./data-types')
, options
, dialect
, sequelize;
/*
Escape a value (e.g. a string, number or date)
......@@ -21,14 +24,7 @@ var attributeMap = {
onUpdate:"ON UPDATE",
default:"DEFAULT"
};
//mssql doesnt support time zone
function escape(value, field) {
if (value && value._isSequelizeMethod) {
return value.toString();
} else {
return SqlString.escape(value, false, null, dialect, field);
}
}
function quoteIdentifier(identifier, force) {
if (identifier === '*') return identifier;
return Utils.addTicks(identifier, '"');
......@@ -93,6 +89,31 @@ function addTableExistsWrapper(tableName, query, exists){
module.exports = {
get options(){
return options;
},
set options(opt) {
options = opt;
},
get dialect(){
return dialect;
},
set dialect(dial) {
dialect = dial;
},
get sequelize(){
return sequelize;
},
set sequelize(seq) {
sequelize = seq;
},
escape: function(value, field) {
if (value && value._isSequelizeMethod) {
return value.toString();
} else {
return SqlString.escape(value, false, options.timezone, dialect, field);
}
},
showTableSql: function(){
return 'SELECT name FROM sys.Tables;';
},
......@@ -143,12 +164,8 @@ module.exports = {
, value
, modelAttributeMap = {};
if (this._dialect.supports['RETURNING'] && options.returning) {
valueQuery += ' OUTPUT <%= selFields %> VALUES (<%= values %>)';
emptyQuery += ' OUTPUT <%= selFields %> VALUES ()';
}else{
valueQuery += ' VALUES (<%= values %>)';
}
valueQuery += ' OUTPUT <%= selFields %> VALUES (<%= values %>)';
emptyQuery += ' OUTPUT <%= selFields %> VALUES ()';
valueHash = Utils.removeNullValuesFromHash(valueHash, this.options.omitNull);
......@@ -162,11 +179,11 @@ module.exports = {
valueQuery = 'SET IDENTITY_INSERT <%= table %> ON;'
+ valueQuery
+ '; SET IDENTITY_INSERT <%= table %> OFF';
values.push(escape(value, (modelAttributeMap && modelAttributeMap[key]) || undefined));
values.push(this.escape(value, (modelAttributeMap && modelAttributeMap[key]) || undefined));
}else if(value) {
fields.push(quoteIdentifier(key));
selFields.push(wrapSingleQuote(key));
values.push(escape(value, (modelAttributeMap && modelAttributeMap[key]) || undefined));
values.push(this.escape(value, (modelAttributeMap && modelAttributeMap[key]) || undefined));
}
}
}
......@@ -271,7 +288,7 @@ module.exports = {
var template = 'VARCHAR(10) NOT NULL CHECK ("'
+ attribute.field + '" IN('
+ Utils._.map(attribute.values, function(value) {
return escape(value);
return this.escape(value);
}.bind(this)).join(', ') + '))';
return template;
},
......@@ -310,7 +327,7 @@ module.exports = {
if (attribute.type !== 'TEXT'
&& attribute.type._binary === false
&& Utils.defaultValueSchemable(attribute.defaultValue)) {
if(options && escape(attribute.defaultValue)){
if(options && this.escape(attribute.defaultValue)){
template.push(attributeMap.default + wrapSingleQuote(attribute.defaultValue));
}
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!