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

Commit dd8bb8b4 by Sascha Depold

eager loading for postgresql

1 parent aa72fcfa
......@@ -91,7 +91,7 @@ module.exports = (function() {
dropTableQuery: function(tableName, options) {
options = options || {}
var query = "DROP TABLE IF EXISTS <%= table %>;"
return Utils._.template(query)({table: addQuotes(tableName)})
return Utils._.template(query)({ table: addQuotes(tableName) })
},
renameTableQuery: function(before, after) {
......@@ -175,7 +175,9 @@ module.exports = (function() {
},
selectQuery: function(tableName, options) {
options = options || {}
var query = "SELECT <%= attributes %> FROM <%= table %>"
options = options || {}
options.table = Array.isArray(tableName) ? tableName.map(function(t){return addQuotes(t);}).join(", ") : addQuotes(tableName)
options.attributes = options.attributes && options.attributes.map(function(attr){
if(Array.isArray(attr) && attr.length == 2) {
......@@ -186,27 +188,58 @@ module.exports = (function() {
return addQuotes(attr)
}
}).join(", ")
options.attributes = options.attributes || '*'
var query = "SELECT <%= attributes %> FROM <%= table %>"
if (options.include) {
var tableNames = [options.table]
, optAttributes = [options.table + '.*']
for (var daoName in options.include) {
if (options.include.hasOwnProperty(daoName)) {
var dao = options.include[daoName]
, _tableName = Utils.addTicks(dao.tableName)
tableNames.push(_tableName)
optAttributes = optAttributes.concat(
Utils._.keys(dao.attributes).map(function(attr) {
var identifer = [_tableName, Utils.addTicks(attr)]
return identifer.join('.') + ' AS ' + Utils.addTicks(identifer.join('.'))
})
)
}
}
options.table = tableNames.join(', ').replace(/`/g, '"')
options.attributes = optAttributes.join(', ').replace(/`/g, '"')
}
if(options.where) {
options.where = QueryGenerator.getWhereConditions(options.where)
query += " WHERE <%= where %>"
}
if(options.order) {
options.order = options.order.replace(/([^ ]+)(.*)/, function(m, g1, g2) { return addQuotes(g1)+g2 })
query += " ORDER BY <%= order %>"
}
if(options.group) {
options.group = addQuotes(options.group)
query += " GROUP BY <%= group %>"
}
if(options.limit) query += " LIMIT <%= limit %>"
if(options.offset) query += " OFFSET <%= offset %>"
if (!(options.include && (options.limit === 1))) {
if (options.limit) {
query += " LIMIT <%= limit %>"
}
if (options.offset) {
query += " OFFSET <%= offset %>"
}
}
query += ";"
return Utils._.template(query)(options)
},
......
......@@ -17,70 +17,68 @@ module.exports = (function() {
Utils.inherit(Query, AbstractQuery)
Query.prototype.run = function(sql) {
var self = this
this.sql = sql
if(this.options.logging !== false) {
this.options.logging('Executing: ' + this.sql)
}
var results = [];
var receivedError = false;
var receivedError = false
, query = this.client.query(sql)
, rows = []
var query = this.client.query(sql)
query.on('row', function(row) {
if (self.callee && (self.sql.indexOf('INSERT INTO') == 0 || self.sql.indexOf('UPDATE') == 0)) {
Utils._.forEach(row, function(value, key) {
self.callee[key] = value
rows.push(row)
})
results.push(self.callee)
query.on('error', function(err) {
receivedError = true
this.emit('error', err, this.callee)
}.bind(this))
query.on('end', function() {
this.emit('sql', this.sql)
if (receivedError) {
return
}
if (self.sql.indexOf('SELECT table_name FROM information_schema.tables') == 0) {
results.push(Utils._.values(row))
} else if (self.sql.indexOf('SELECT relname FROM pg_class WHERE oid IN') == 0) {
results.push(Utils._.values(row))
} else if (self.sql.indexOf('SELECT') == 0) {
// transform results into real model instances
// return the first real model instance if options.plain is set (e.g. Model.find)
if (self.options.raw) {
results.push(row);
} else {
results.push(self.callee.build(row, { isNewRecord: false }))
onSuccess.call(this, rows)
}.bind(this))
return this
}
} else if((self.sql.indexOf('SHOW') == 0) || (self.sql.indexOf('DESCRIBE') == 0)) {
results.push(row)
Query.prototype.getInsertIdField = function() {
return 'id'
}
});
query.on('end', function() {
self.emit('sql', self.sql)
if (receivedError) return;
var onSuccess = function(rows) {
var results = []
, isTableNameQuery = (this.sql.indexOf('SELECT table_name FROM information_schema.tables') === 0)
, isRelNameQuery = (this.sql.indexOf('SELECT relname FROM pg_class WHERE oid IN') === 0)
if (self.sql.indexOf('SELECT') == 0) {
if (self.options.plain) {
self.emit('success', (results.length == 0) ? null : results[0])
} else {
self.emit('success', results)
}
} else if((self.sql.indexOf('SHOW') == 0) || (self.sql.indexOf('DESCRIBE') == 0)) {
self.emit('success', results)
} else if (self.sql.indexOf('INSERT INTO') == 0) {
self.emit('success', results[0])
} else if (self.sql.indexOf('UPDATE') == 0) {
self.emit('success', self.callee)
} else {
self.emit('success', results)
if (isTableNameQuery || isRelNameQuery) {
return this.emit('success', rows.map(function(row) { return Utils._.values(row) }))
}
});
query.on('error', function(err) {
receivedError = true
self.emit('error', err, self.callee)
});
if (this.send('isSelectQuery')) {
this.emit('success', this.send('handleSelectQuery', rows))
} else if (this.send('isShowOrDescribeQuery')) {
this.emit('success', results)
} else if (this.send('isInsertQuery')) {
for (var key in rows[0]) {
if (rows[0].hasOwnProperty(key)) {
this.callee[key] = rows[0][key]
}
}
return this
this.emit('success', this.callee)
} else if (this.send('isUpdateQuery')) {
this.emit('success', this.callee)
} else {
this.emit('success', results)
}
}
return Query
......
......@@ -360,8 +360,6 @@ dialects.forEach(function(dialect) {
})
})
if (['sqlite', 'mysql'].indexOf(dialect) !== -1) {
describe('association fetching', function() {
before(function() {
this.Task = this.sequelize.define('Task', {
......@@ -529,12 +527,9 @@ dialects.forEach(function(dialect) {
}.bind(this)) //- sequelize.sync
})
})
}
}) //- describe: find
describe('findAll', function findAll() {
if (['sqlite', 'mysql'].indexOf(dialect) !== -1) {
describe('association fetching', function() {
before(function() {
this.Task = this.sequelize.define('Task', {
......@@ -702,7 +697,6 @@ dialects.forEach(function(dialect) {
}.bind(this)) //- sequelize.sync
})
})
}
}) //- describe: findAll
describe('min', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!