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

Commit b2b4fa9b by Jason Jung Committed by Matt Broadstone

added support for delete

1 parent 6307f1dc
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
var Utils = require('../../utils') var Utils = require('../../utils')
, DataTypes = require('./data-types') , DataTypes = require('./data-types')
, SqlGenerator = require('./sql-generator') , SqlGenerator = require('./sql-generator')
, Model = require('../../model') , Model = require('../../model')
, _ = require('lodash') , _ = require('lodash')
, util = require('util'); , util = require('util');
...@@ -26,7 +26,7 @@ module.exports = (function() { ...@@ -26,7 +26,7 @@ module.exports = (function() {
get sequelize(){ get sequelize(){
return SqlGenerator.sequelize; return SqlGenerator.sequelize;
}, },
set sequelize(seq) { set sequelize(seq) {
SqlGenerator.sequelize = seq; SqlGenerator.sequelize = seq;
}, },
...@@ -64,9 +64,9 @@ module.exports = (function() { ...@@ -64,9 +64,9 @@ module.exports = (function() {
createTableQuery: function(tableName, attributes, options) { createTableQuery: function(tableName, attributes, options) {
return SqlGenerator.getCreateTableSql(tableName, attributes, options); return SqlGenerator.getCreateTableSql(tableName, attributes, options);
}, },
renameTableQuery: function(before, after) {
renameTableQuery: function(before, after) {
throwMethodUndefined('renameTableQuery'); throwMethodUndefined('renameTableQuery');
}, },
...@@ -116,7 +116,7 @@ module.exports = (function() { ...@@ -116,7 +116,7 @@ module.exports = (function() {
var newColumnName; var newColumnName;
for (var attrName in attributes) { for (var attrName in attributes) {
newColumnName = attrName; newColumnName = attrName;
} }
var query = [ var query = [
SqlGenerator.renameColumnSql(tableName, attrBefore, newColumnName), SqlGenerator.renameColumnSql(tableName, attrBefore, newColumnName),
this.changeColumnQuery(tableName, attributes) this.changeColumnQuery(tableName, attributes)
...@@ -189,7 +189,11 @@ module.exports = (function() { ...@@ -189,7 +189,11 @@ module.exports = (function() {
*/ */
/* istanbul ignore next */ /* istanbul ignore next */
deleteQuery: function(tableName, where, options) { deleteQuery: function(tableName, where, options) {
throwMethodUndefined('deleteQuery'); console.log('where:', where);
var query = SqlGenerator.deleteSql(tableName, where);
return query;
}, },
/* /*
...@@ -202,11 +206,11 @@ module.exports = (function() { ...@@ -202,11 +206,11 @@ module.exports = (function() {
OR a string with conditions (e.g. 'name="foo"'). OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own. If you use a string, you have to escape it on your own.
*/ */
incrementQuery: function(tableName, attrValueHash, where, options) { incrementQuery: function(tableName, attrValueHash, where, options) {
throwMethodUndefined('incrementQuery'); throwMethodUndefined('incrementQuery');
}, },
nameIndexes: function (indexes, rawTablename) { nameIndexes: function (indexes, rawTablename) {
return Utils._.map(indexes, function (index) { return Utils._.map(indexes, function (index) {
if (!index.hasOwnProperty('name')) { if (!index.hasOwnProperty('name')) {
var onlyAttributeNames = index.fields.map(function(attribute) { var onlyAttributeNames = index.fields.map(function(attribute) {
...@@ -264,7 +268,7 @@ module.exports = (function() { ...@@ -264,7 +268,7 @@ module.exports = (function() {
} }
return fields; return fields;
}, },
quoteTable: function(param, as) { quoteTable: function(param, as) {
throwMethodUndefined('quoteTable'); throwMethodUndefined('quoteTable');
}, },
...@@ -308,7 +312,7 @@ module.exports = (function() { ...@@ -308,7 +312,7 @@ module.exports = (function() {
describeTableQuery: function(tableName, schema, schemaDelimiter) { describeTableQuery: function(tableName, schema, schemaDelimiter) {
return SqlGenerator.describeTableSql(tableName, schema, schemaDelimiter); return SqlGenerator.describeTableSql(tableName, schema, schemaDelimiter);
}, },
/* /*
...@@ -346,7 +350,7 @@ module.exports = (function() { ...@@ -346,7 +350,7 @@ module.exports = (function() {
Escape a value (e.g. a string, number or date) Escape a value (e.g. a string, number or date)
*/ */
escape: function(value, field) { escape: function(value, field) {
throwMethodUndefined('escape'); throwMethodUndefined('escape');
}, },
...@@ -405,11 +409,11 @@ module.exports = (function() { ...@@ -405,11 +409,11 @@ module.exports = (function() {
} }
} }
if(options.hasOwnProperty('where')){ if(options.hasOwnProperty('where')){
query.push(this.getWhereConditions(options.where, model.name, model, options)); query.push(this.getWhereConditions(options.where, model.name, model, options));
} }
//console.log(query.join(' ') + ';'); //console.log(query.join(' ') + ';');
return query.join(' ') + ';'; return query.join(' ') + ';';
}, },
/** /**
* Returns a query that starts a transaction. * Returns a query that starts a transaction.
* *
......
...@@ -261,7 +261,7 @@ module.exports = { ...@@ -261,7 +261,7 @@ module.exports = {
query = 'UPDATE <%= tableName %> SET <%= values %> OUTPUT <%= selFields %>'; query = 'UPDATE <%= tableName %> SET <%= values %> OUTPUT <%= selFields %>';
for (var key in valueHash) { for (var key in valueHash) {
var value = valueHash[key]; var value = valueHash[key];
selFields.push('INSERTED.' + quoteIdentifier(key)); selFields.push('INSERTED.' + quoteIdentifier(key));
values.push(quoteIdentifier(key) + '=' + escape(value, (!!attributes && !!attributes[key] ? attributes[key] : undefined))); values.push(quoteIdentifier(key) + '=' + escape(value, (!!attributes && !!attributes[key] ? attributes[key] : undefined)));
} }
...@@ -274,7 +274,17 @@ module.exports = { ...@@ -274,7 +274,17 @@ module.exports = {
}; };
return Utils._.template(query)(replacements); return Utils._.template(query)(replacements);
}, },
deleteSql: function(tableName, where) {
var query = "DELETE FROM <%= table %> <%= where %>";
var replacements = {
table: tableName,
where: this.getWhereClause(where, tableName)
};
return Utils._.template(query)(replacements);
},
addColumnSql: function(key, dataType){ addColumnSql: function(key, dataType){
var attribute = Utils._.template('<%= key %> <%= definition %>')({ var attribute = Utils._.template('<%= key %> <%= definition %>')({
key: quoteIdentifier(key), key: quoteIdentifier(key),
...@@ -523,10 +533,10 @@ module.exports = { ...@@ -523,10 +533,10 @@ module.exports = {
var joinKey = quoteIdentifier(include.association.foreignKey); var joinKey = quoteIdentifier(include.association.foreignKey);
if(associationType === 'BelongsTo'){ if(associationType === 'BelongsTo'){
rootKey = quoteIdentifier(include.association.foreignKey); rootKey = quoteIdentifier(include.association.foreignKey);
joinKey = primaryKey; joinKey = primaryKey;
} }
var joinType = include.required ? 'INNER JOIN' : 'LEFT OUTER JOIN'; var joinType = include.required ? 'INNER JOIN' : 'LEFT OUTER JOIN';
//var key, joinKey; //var key, joinKey;
//console.log(include.association.associationType); //console.log(include.association.associationType);
//if(include.association) //if(include.association)
...@@ -546,13 +556,16 @@ module.exports = { ...@@ -546,13 +556,16 @@ module.exports = {
//console.log(where); //console.log(where);
//console.log(tableName); //console.log(tableName);
for(var key in where){ for(var key in where){
if(tableName){ var val = where[key];
query.push(quoteIdentifier(tableName) + '.' + quoteIdentifier(key)); var operator = '=';
}else{
query.push(quoteIdentifier(key)); if (val == 'NULL') {
operator = 'IS';
} }
query.push('=');
query.push(where[key]); query.push(quoteIdentifier(tableName) + '.' + quoteIdentifier(key));
query.push(operator);
query.push(val);
} }
return query.join(' '); return query.join(' ');
}}; }};
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!