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

Commit a2a21aae by Joel Trost Committed by Matt Broadstone

Bulkinsert and ManytoMany support

1 parent 91b84530
......@@ -145,8 +145,20 @@ module.exports = (function() {
Parameters: table name + list of hashes of attribute-value-pairs.
*/
/* istanbul ignore next */
bulkInsertQuery: function(tableName, attrValueHashes) {
throwMethodUndefined('bulkInsertQuery');
bulkInsertQuery: function(tableName, attrValueHashes,options) {
var allAttributes = [],
ignoreKeys = options.fields;
Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
Utils._.forOwn(attrValueHash, function(value, key, hash) {
if (allAttributes.indexOf(key) === -1) allAttributes.push(key);
if (value !== null && ignoreKeys.indexOf(key) > 0)
ignoreKeys.splice(ignoreKeys.indexOf(key),1);
});
});
for(var i = 0; i < ignoreKeys.length; i++){
allAttributes.splice(allAttributes.indexOf(ignoreKeys[i]), 1);
}
return SqlGenerator.bulkInsertSql(tableName, allAttributes, attrValueHashes,options);
},
/*
......@@ -163,7 +175,7 @@ module.exports = (function() {
// if(where){
// throw new Error();
// }
console.log('here', where);
//console.log('here', where);
var query = [
SqlGenerator.updateSql(tableName, attrValueHash, attributes),
this.getWhereConditions(where)
......
......@@ -17,6 +17,7 @@ var attributeMap = {
defaultValue:"DEFAULT",
unique:"UNIQUE",
primaryKey:"PRIMARY KEY",
foreignKey:"FOREIGN KEY",
comment:"COMMENT",
references:"REFERENCES",
onDelete:"ON DELETE",
......@@ -104,14 +105,6 @@ function loadColumn(attributes){
}
return attrStr;
}
function loadColumnWithTypes(attributes){
var attrStr = [];
for (var attr in attributes) {
var dataType = attributes[attr];
attrStr.push(quoteIdentifier(attr) + " " + dataType);
}
return attrStr;
}
function addTableExistsWrapper(query, exists){
return [
"IF (",
......@@ -188,7 +181,10 @@ module.exports = {
return dataType.indexOf('PRIMARY KEY') >= 0;
}));
attrStr = loadColumnWithTypes(attributes);
for (var attr in attributes) {
var dataType = attributes[attr];
attrStr.push(quoteIdentifier(attr) + " " + dataType);
}
var values = {
unquotedTable: tableName,
......@@ -201,7 +197,7 @@ module.exports = {
alterTableSql: function(tableName){
var query = 'ALTER TABLE <%= tableName %>';
var value = {
tableName : tableName
tableName : quoteIdentifier(tableName)
};
return Utils._.template(query)(value);
},
......@@ -214,7 +210,29 @@ module.exports = {
query = addTableExistsWrapper(query, true);
return Utils._.template(query)(values).trim() + ";";
},
bulkInsertSql: function(tableName, attributeKeys, attributes,options) {
var query = 'INSERT<%= ignoreDuplicates %> INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;'
, tuples = [];
Utils._.forEach(attributes, function(attrValueHash, i) {
tuples.push('(' +
attributeKeys.map(function(key) {
return escape(attrValueHash[key]);
}.bind(this)).join(',') +
')');
}.bind(this));
var replacements = {
ignoreDuplicates: options && options.ignoreDuplicates ? ' IGNORE' : '',
table: quoteIdentifier(tableName),
attributes: attributeKeys.map(function(attr) {
return quoteIdentifier(attr);
}.bind(this)).join(','),
tuples: tuples
};
return Utils._.template(query)(replacements);
},
insertSql: function(tableName, valueHash, modelAttributeMap) {
var query
, valueQuery = 'INSERT INTO <%= tableName %> (<%= attributes %>)'
......@@ -393,7 +411,11 @@ module.exports = {
template.push(attribute.type.toString());
//a primary key
if(attribute.primaryKey){
template.push(attributeMap.primaryKey);
if (!attribute.references) {
template.push(attributeMap.primaryKey);
}else{
template.push(attributeMap.foreignKey);
}
//allow null
}else if(attribute.allowNull === false){
template.push(attributeMap.notNull);
......@@ -424,7 +446,8 @@ module.exports = {
if (attribute.references) {
template.push(attributeMap.references + quoteIdentifier(attribute.references));
template.push(attributeMap.references);
template.push(quoteIdentifier(attribute.references));
if (attribute.referencesKey) {
template.push('(' + quoteIdentifier(attribute.referencesKey) + ')');
......@@ -491,10 +514,15 @@ module.exports = {
}
return attrString.join(', ');
},
getTopClause: function(limit){
return "TOP(" + limit + ")";
},
getSelectorClause: function(model, options){
var query = ['SELECT'];
//we have joins
if(options.limit && !options.offset){
query.push(this.getTopClause(options.limit));
}
//add join table
if(options.include){
query.push(loadFieldsWithName(model.rawAttributes, model.name));
......@@ -520,13 +548,21 @@ module.exports = {
return query.join(' ');
},
getJoinClause: function(model, include){
var primaryKey = '';
for(var key in model.rawAttributes){
if(model.rawAttributes[key].primaryKey){
primaryKey = quoteIdentifier(key);
break;
}
//console.log(include.through);
var query = [];
var primaryKey = quoteIdentifier(model.primaryKeyAttribute);
var joinType = include.required ? 'INNER JOIN' : 'LEFT OUTER JOIN';
var joinTable = include.as ? include.as : include.model.name;
var manyJoinTable = joinTable;
joinTable = quoteIdentifier(joinTable);
var tableName = quoteIdentifier(model.name);
var hasManyToMany = false;
query.push(joinType);
if(include.through){
hasManyToMany = true;
}
//this logic handles the join types
var associationType = include.association.associationType;
var rootKey,joinKey;
if(associationType === 'BelongsTo'){
......@@ -535,43 +571,72 @@ module.exports = {
}else if (associationType === 'HasMany'){
rootKey = primaryKey;
joinKey = quoteIdentifier(include.association.identifier);
if(hasManyToMany){
//indicates many to many
manyJoinTable = quoteIdentifier(joinTable + '.' + include.through.as);
//console.log(include.through);
query = query.concat([
"(",
quoteIdentifier(include.through.model.name)
, "AS", manyJoinTable,
joinType,
joinTable, "AS", joinTable,
"ON", joinTable + '.' + quoteIdentifier(include.model.primaryKeyAttribute),
"=", manyJoinTable + '.' + quoteIdentifier(include.through.model.primaryKeyAttribute),
")"
]);
}
}else{
rootKey = primaryKey;
joinKey = quoteIdentifier(include.association.foreignKey);
}
var joinType = include.required ? 'INNER JOIN' : 'LEFT OUTER JOIN';
//var key, joinKey;
//console.log(include.association.associationType);
//if(include.association)
var joinTable = include.as ? include.as : include.model.name;
joinTable = quoteIdentifier(joinTable);
var tableName = quoteIdentifier(model.name);
return [
joinType,
quoteIdentifier(include.model.tableName),
'AS', joinTable,
if(!hasManyToMany){
query.push(quoteIdentifier(include.model.tableName));
query.push('AS');
query.push(joinTable);
}
query = query.concat([
'ON', tableName + '.' + rootKey,
'=', joinTable + '.' + joinKey
].join(' ');
'=', manyJoinTable + '.' + joinKey
]);
if(include.where){
query.push('AND');
query.push(this.formatWhereCondition(include.where, joinTable));
}
return query.join(' ');
},
getWhereClause: function(where, tableName){
var query = ['WHERE'];
//console.log(where);
//console.log(tableName);
formatWhereCondition: function(where, tableName){
var query = [];
for(var key in where){
var val = where[key];
var operator = '=';
if (val === 'NULL') {
if (!val) {
operator = 'IS';
}
query.push(quoteIdentifier(tableName) + '.' + quoteIdentifier(key));
if(tableName){
query.push(quoteIdentifier(tableName) + '.' + quoteIdentifier(key));
} else {
query.push(quoteIdentifier(key));
}
console.log('val', typeof val);
query.push(operator);
query.push(val);
if(!val){
query.push('NULL');
}else if(typeof val === 'number'){
query.push(val);
}else{
query.push(wrapSingleQuote(val));
}
}
return query.join(' ');
},
getWhereClause: function(where, tableName){
return [
'WHERE',
this.formatWhereCondition(where, tableName)
].join(' ');
}};
......
......@@ -22,7 +22,6 @@ describe(Support.getTestDialectTeaser("Include"), function () {
// Associations
A.hasMany(B);
B.belongsTo(B);
B.belongsTo(D);
B.hasMany(C, {
through: 'BC',
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!