-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdb.js
More file actions
executable file
·137 lines (122 loc) · 2.99 KB
/
db.js
File metadata and controls
executable file
·137 lines (122 loc) · 2.99 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
128
129
130
131
132
133
134
135
136
137
var orm = require("orm");
var opt = {
host: 'localhost',
database: 'lark',
user: 'root',
password: '',
protocol: 'mysql'
}
var _cbk = function(data, self, key){
self.count --;
self.stack[key] = data;
return false;
}
function doDb (self, key, callback, _opt) {
orm.connect(opt, function (err, db) {
if (err)
return _cbk(false, self, key);
callback(db, self, key, _cbk, _opt);
});
}
function dbOut (self, url, key){
self.count ++;
libHttp.get(url, function(res){
var _data = '';
res.on('data', function (data) {
_data += data;
});
res.on('end', function (data) {
_cbk(_data, self, key);
});
}).on('error', function(e) {
console.log("错误:" + e.message);
});
}
function dbFind (self, key, _opt) {
self.count ++;
var callback = function(db, self, key, _cbk, _opt){
var _self = self,
_key = key;
var MYTABLE = db.define(_opt.table, _opt.list);
db.models[_opt.table].find(_opt.findList, function(err, rows) {
if (err) {
return console.error('Connection error: ' + err);
_cbk([false], self, key);
}
_cbk(rows, self, key);
});
}
doDb(self, key, callback, _opt);
}
function dbUpdate (self, key, _opt) {
self.count ++;
var callback = function(db, self, key, _cbk, _opt){
var _self = self,
_key = key;
var MYTABLE = db.define(_opt.table, _opt.list);
db.models[_opt.table].find(_opt.findList).each(function(rows) {
for(var i in _opt.updateList){
rows[i] = _opt.updateList[i];
}
}).save(function (err) {
if (err) {
return console.error('Connection error: ' + err);
_cbk([false], self, key);
}
_cbk([true], self, key);
})
}
doDb(self, key, callback, _opt);
}
function dbRemove (self, key, _opt) {
self.count ++;
var callback = function(db, self, key, _cbk, _opt){
var _self = self,
_key = key;
var MYTABLE = db.define(_opt.table, _opt.list);
db.models[_opt.table].find(_opt.removeList).remove(function (err) {
if (err) {
return console.error('Connection error: ' + err);
_cbk([false], self, key);
}
_cbk([true], self, key);
})
}
doDb(self, key, callback, _opt);
}
function dbInsert (self, key, _opt) {
self.count ++;
var callback = function(db, self, key, _cbk, _opt){
var _self = self,
_key = key;
var MYTABLE = db.define(_opt.table, _opt.list);
db.models[_opt.table].create(_opt.insertList, function (err, items) {
if (err) {
return console.error('Connection error: ' + err);
_cbk([false], self, key);
}
_cbk([true], self, key);
})
}
doDb(self, key, callback, _opt);
}
function dbResult (_this, fun) {
var _this = _this;
var listenFun = function(fun, _this){
if (!_this.count) {
var data = fun(_this);
return false;
}else{
setTimeout(function(){
listenFun(fun, _this);
},100)
}
}
listenFun(fun, _this);
}
exports.dbFind = dbFind;
exports.dbRemove = dbRemove;
exports.dbUpdate = dbUpdate;
exports.dbInsert = dbInsert;
exports.dbResult = dbResult;
exports.dbOut = dbOut;