query-interface.js
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var Utils = require('./utils')
module.exports = (function() {
var QueryInterface = function(sequelize) {
this.sequelize = sequelize
this.QueryGenerator = require('./connectors/' + this.sequelize.options.connector + '/query-generator')
}
QueryInterface.prototype.createTable = function(tableName, attributes, options) {
return query.call(this, this.QueryGenerator.createTableQuery(tableName, attributes, options))
}
QueryInterface.prototype.dropTable = function(tableName) {
return query.call(this, this.QueryGenerator.dropTableQuery(tableName))
}
QueryInterface.prototype.dropAllTables = function() {
var self = this
return new Utils.CustomEventEmitter(function(emitter) {
var chainer = new Utils.QueryChainer()
self.sequelize.query('SHOW TABLES;', null, { raw: true }).success(function(data) {
var tableNames = Sequelize.Utils._.map(data, function(hash) {
return Sequelize.Utils._.values(hash)
})
Sequelize.Utils._.flatten(tableNames).forEach(function(tableName) {
chainer.add(self.sequelize.getQueryInterface().dropTable(tableName))
})
chainer
.run()
.success(function() { emitter.emit('success', null) })
.error(function(err) { emitter.emit('failure', err) })
}).error(function(err) {
emitter.emit('failure', err)
})
}).run()
}
QueryInterface.prototype.renameTable = function() {
}
QueryInterface.prototype.addColumn = function() {
}
QueryInterface.prototype.removeColumn = function() {
}
QueryInterface.prototype.changeColumn = function() {
}
QueryInterface.prototype.renameColumn = function() {
}
QueryInterface.prototype.addIndex = function() {
}
QueryInterface.prototype.removeIndex = function() {
}
QueryInterface.prototype.insert = function(model, tableName, values) {
var self = this
return new Utils.CustomEventEmitter(function(emitter) {
query
.call(self, self.QueryGenerator.insertQuery(tableName, values), model)
.success(function(obj) {
obj.isNewRecord = false
emitter.emit('success', obj)
})
.error(function(err) { emitter.emit('failure', err) })
}).run()
}
QueryInterface.prototype.update = function(model, tableName, values, identifier) {
var sql = this.QueryGenerator.updateQuery(tableName, values, identifier)
return query.call(this, sql, model)
}
QueryInterface.prototype.delete = function(model, tableName, identifier) {
var sql = this.QueryGenerator.deleteQuery(tableName, identifier)
return query.call(this, sql, model)
}
QueryInterface.prototype.select = function(factory, tableName, options, queryOptions) {
queryOptions = queryOptions || {}
return query.call(this, this.QueryGenerator.selectQuery(tableName, options), factory, queryOptions)
}
QueryInterface.prototype.rawSelect = function(tableName, options, attributeSelector) {
var self = this
if(attributeSelector == undefined)
throw new Error('Please pass an attribute selector!')
return new Utils.CustomEventEmitter(function(emitter) {
var sql = self.QueryGenerator.selectQuery(tableName, options)
query.call(self, sql, null, { plain: true, raw: true }).success(function(data) {
emitter.emit('success', data[attributeSelector])
}).error(function(err) {
emitter.emit('failure', err)
})
}).run()
}
// private
var query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg })
// add this as callee via the second argument
if(arguments.length == 1) args.push(this)
return this.sequelize.query.apply(this.sequelize, args)
}
return QueryInterface
})()