-
Notifications
You must be signed in to change notification settings - Fork 0
/
center_insert_mysql.js
162 lines (142 loc) · 4.21 KB
/
center_insert_mysql.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//存取文件的类
var fs = require('fs')
//处理路径的类
var path = require('path')
//存放配置文件
var config = require('./config')
//连接Mysql数据库
var mysql = require('mysql');
//创建mysql数据库连接池
var pool = mysql.createPool(
config.mysql
);
//获取Post数据
var data
var pathfrom
//Promise函数,用来获取第一个待传文件名称
var getFirstFile = function () {
return new Promise(function (resolve, reject) {
//获取第一个待传文件名称
fs.readdir(config.local.center_mysql_path, function (err, files) {
if (files.length > 0) {
var filePath = path.normalize(config.local.center_mysql_path + files[0])
//将待传文件名称存储到全局变量
pathfrom = filePath
if (files[0].indexOf('.data') < 0) {
fs.unlinkSync(pathfrom);
reject(-1)
return
}
resolve()
}
else {
reject(-1)
}
}
)
}
)
}
var InsertIntoMySQL = function () {
return new Promise(function (resolve, reject) {
pool.getConnection(function (err, connection) {
if (err) {
console.log("获取mysql连接池错误:" + err)
reject(-31)
return
}
else {
if (data.TABLENAME == 'T_TABLE_DATA_VER') {
console.log('特殊处理表:T_TABLE_DATA_VER')
var query = connection.query('DELETE FROM t_table_data_ver WHERE NODE_ID=? AND TABLE_NAME=?', [data.ROWS[0].NODE_ID, data.ROWS[0].TABLE_NAME],
function (error, results, fields) {
console.log('删除SQL语句:' + query.sql)
if (error) {
console.log('删除错误:' + error)
connection.release();
reject(-32)
return
}
else {
//数据删除成功
var query1 = connection.query('INSERT INTO `' + data.TABLENAME + '` SET ?',
data.ROWS[0],
function (error, results, fields) {
console.log('插入SQL语句:' + query1.sql)
if (error) {
console.log('插入错误:' + error)
//释放连接
connection.release();
reject(-33)
return
}
else {
console.log('mysql插入数据成功...')
connection.release();
resolve()
return
}
})
}
})
}
else {
console.log('普通表:' + data.TABLENAME)
// Use the connection
var query1 = connection.query('INSERT INTO `' + data.TABLENAME + '` SET ?',
data.ROWS[0],
function (error, results, fields) {
console.log('插入SQL语句:' + query1.sql)
if (error) {
console.log('插入错误:' + error)
connection.release();
reject(-34)
return
}
else {
console.log('mysql插入数据成功...')
connection.release();
resolve()
return
}
})
}
}
})
})
}
var readFileData = function () {
return new Promise(function (resolve, reject) {
var fileName = pathfrom
fs.readFile(fileName, {flag: 'r+', encoding: 'utf8'}, function (err, da) {
if (err) {
reject(-2)
return
}
//设置数据内容
data = JSON.parse(da)
//休眠后发送
// data = JSON.parse(data)
console.log(data)
//模拟无数据文件
resolve()
}
)
}
)
}
var search = function () {
getFirstFile().then(readFileData).then(InsertIntoMySQL).then(function () {
console.log('继续')
fs.unlinkSync(pathfrom);
search()
}).catch(function () {
console.log('没有')
setInterval(function () {
search()
}, 10000)
})
}
exports.processData = function () {
search()
}