databaseList.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. const express = require('express');
  2. const router = express.Router();
  3. const crypto = require('crypto');
  4. const events = require('events');
  5. const db = require('../common/mysqlConnection');
  6. /**
  7. * 获取数据库列表
  8. * @param {*} req
  9. * @param {*} res
  10. * @param {function} next
  11. */
  12. function getDatabaseList(req, res, next) {
  13. if (req.session.signinStatus) {
  14. let emitter = new events.EventEmitter(),
  15. pageIndex = req.query.pageIndex - 1,
  16. orderBy = req.query.orderBy,
  17. searchValue = req.query.search;
  18. emitter.on('ok', (results, count) => {
  19. res.send({
  20. status: 'success',
  21. info: '获取成功',
  22. data: {
  23. 'results': results,
  24. 'count': count
  25. }
  26. });
  27. });
  28. emitter.on('err', (err, errInfo) => {
  29. res.json({
  30. status: 'error',
  31. info: errInfo,
  32. error: err
  33. });
  34. });
  35. emitter.on('selectDatabseInfoFromDbOwnTable', () => {
  36. switch (orderBy) {
  37. case 'userId':
  38. orderBy = 'dbOwnUuid';
  39. break;
  40. case 'databaseName':
  41. orderBy = 'dbName';
  42. break;
  43. case 'createDate':
  44. orderBy = 'createDate';
  45. break;
  46. default:
  47. orderBy = 'dbOwnUuid';
  48. break;
  49. }
  50. if (pageIndex > 0) {
  51. pageIndex = parseInt(pageIndex, 10) * 20;
  52. } else {
  53. pageIndex = 0;
  54. }
  55. //定义SQL语句查询
  56. let pool = db.pool.userInfoDatabasePool,
  57. sqlString = 'SELECT SQL_CALC_FOUND_ROWS * FROM (SELECT db.dbName, db.dbOwnUuid, user.account, db.createDate FROM dbOwn_test AS db, user_test AS user WHERE db.dbOwnUuid = user.uuid ORDER BY ? ASC) a LIMIT ?, 20; SELECT FOUND_ROWS()',
  58. value = [orderBy, pageIndex];
  59. if (searchValue) {
  60. // sqlString = 'SELECT SQL_CALC_FOUND_ROWS * FROM (SELECT db.dbName, db.dbOwnUuid, user.account, db.createDate FROM dbOwn_test AS db, user_test AS user WHERE db.dbOwnUuid = user.uuid AND db.dbName LIKE ? OR db.dbOwnUuid LIKE ? OR user.account LIKE ? OR db.createDate LIKE ? ORDER BY ? ASC) a LIMIT ?, 20; SELECT FOUND_ROWS()';
  61. sqlString = 'SELECT SQL_CALC_FOUND_ROWS * FROM (SELECT * FROM (SELECT db.dbName, db.dbOwnUuid, user.account, db.createDate FROM dbOwn_test AS db, user_test AS user WHERE db.dbOwnUuid = user.uuid) AS r WHERE r.dbName LIKE ? OR r.dbOwnUuid LIKE ? OR r.account LIKE ? OR r.createDate LIKE ? ORDER BY ? ASC) a LIMIT ?, 20; SELECT FOUND_ROWS()';
  62. searchValue = '%' + searchValue + '%';
  63. value = [searchValue, searchValue, searchValue, searchValue, orderBy, pageIndex];
  64. }
  65. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  66. if (err) {
  67. emitter.emit('err', err.err, '数据库查询出错');
  68. } else {
  69. emitter.emit('ok', results[0], results[1][0]['FOUND_ROWS()']);
  70. }
  71. });
  72. });
  73. emitter.emit('selectDatabseInfoFromDbOwnTable');
  74. } else {
  75. res.send({
  76. status: 'signout',
  77. info: '未登录',
  78. url: '#/signin'
  79. });
  80. }
  81. }
  82. /**
  83. * 获取数据库总数
  84. * @param {*} req
  85. * @param {*} res
  86. * @param {function} next
  87. */
  88. function getDatabaseCount(req, res, next) {
  89. if (req.session.signinStatus) {
  90. let emitter = new events.EventEmitter();
  91. emitter.on('ok', (results) => {
  92. res.send({
  93. status: 'success',
  94. info: '获取成功',
  95. data: {
  96. 'results': results
  97. }
  98. });
  99. });
  100. emitter.on('err', (err, errInfo) => {
  101. res.json({
  102. status: 'error',
  103. info: errInfo,
  104. error: err
  105. });
  106. });
  107. emitter.on('selectUserCountFromUserTable', () => {
  108. //定义SQL语句查询
  109. let pool = db.pool.userInfoDatabasePool,
  110. sqlString = 'SELECT COUNT(1) FROM dbOwn_test',
  111. value = null;
  112. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  113. if (err) {
  114. emitter.emit('err', err.err, '数据库查询出错');
  115. } else {
  116. emitter.emit('ok', results[0]['COUNT(1)']);
  117. }
  118. });
  119. });
  120. emitter.emit('selectUserCountFromUserTable');
  121. } else {
  122. res.send({
  123. status: 'signout',
  124. info: '未登录',
  125. url: '#/signin'
  126. });
  127. }
  128. }
  129. /**
  130. * 获取数据库所有表
  131. * @param {*} req
  132. * @param {*} res
  133. * @param {function} next
  134. */
  135. function getDatabaseTables(req, res, next) {
  136. if (req.session.signinStatus) {
  137. let emitter = new events.EventEmitter(),
  138. dbName = req.query.dbName;
  139. emitter.on('ok', (results) => {
  140. res.send({
  141. status: 'success',
  142. info: '获取成功',
  143. data: {
  144. 'results': results
  145. }
  146. });
  147. });
  148. emitter.on('err', (err, errInfo) => {
  149. res.json({
  150. status: 'error',
  151. info: errInfo,
  152. error: err
  153. });
  154. });
  155. emitter.on('selectDatabseTables', () => {
  156. //定义SQL语句查询
  157. let pool = db.pool.userDatabasePool,
  158. sqlString = 'SELECT * FROM information_schema.`tables` a WHERE a.TABLE_SCHEMA = ?',
  159. value = [dbName];
  160. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  161. if (err) {
  162. emitter.emit('err', err.err, '数据库查询出错');
  163. } else {
  164. emitter.emit('ok', results);
  165. }
  166. });
  167. });
  168. emitter.emit('selectDatabseTables');
  169. } else {
  170. res.send({
  171. status: 'signout',
  172. info: '未登录',
  173. url: '#/signin'
  174. });
  175. }
  176. }
  177. /**
  178. * 删除数据库
  179. * @param {*} req
  180. * @param {*} res
  181. * @param {function} next
  182. */
  183. function dropDatabase(req, res, next) {
  184. if (req.session.signinStatus) {
  185. let emitter = new events.EventEmitter(),
  186. dbName = req.body.dbName;
  187. emitter.on('ok', (results) => {
  188. res.send({
  189. status: 'success',
  190. info: '删除成功',
  191. data: {
  192. 'results': results
  193. }
  194. });
  195. });
  196. emitter.on('err', (err, errInfo) => {
  197. res.json({
  198. status: 'error',
  199. info: errInfo,
  200. error: err
  201. });
  202. });
  203. emitter.on('deleteRow', () => {
  204. let pool = db.pool.userInfoDatabasePool,
  205. sqlString = 'DELETE FROM dbOwn_test WHERE dbName = ?',
  206. value = [dbName];
  207. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  208. if (err) {
  209. emitter.emit('err', err.err, 'sql执行出错');
  210. } else {
  211. emitter.emit('ok', results);
  212. }
  213. });
  214. })
  215. emitter.on('dropDb', () => {
  216. //定义SQL语句
  217. let pool = db.pool.userDatabasePool,
  218. sqlString = 'DROP DATABASE ' + dbName,
  219. value = null;
  220. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  221. if (err) {
  222. emitter.emit('err', err.err, 'sql执行出错');
  223. } else {
  224. emitter.emit('deleteRow', results);
  225. }
  226. });
  227. });
  228. emitter.emit('dropDb');
  229. } else {
  230. res.send({
  231. status: 'signout',
  232. info: '未登录',
  233. url: '#/signin'
  234. });
  235. }
  236. }
  237. /**
  238. * 清空数据库
  239. * @param {*} req
  240. * @param {*} res
  241. * @param {function} next
  242. */
  243. function emptyDatabase(req, res, next) {
  244. if (req.session.signinStatus) {
  245. let emitter = new events.EventEmitter(),
  246. dbName = req.body.dbName;
  247. emitter.on('ok', (results) => {
  248. res.send({
  249. status: 'success',
  250. info: '删除成功',
  251. data: {
  252. 'results': results
  253. }
  254. });
  255. });
  256. emitter.on('err', (err, errInfo) => {
  257. res.json({
  258. status: 'error',
  259. info: errInfo,
  260. error: err
  261. });
  262. });
  263. emitter.on('dropTable', (results) => {
  264. let pool = db.pool.userDatabasePool,
  265. value = null;
  266. results.forEach((item, index) => {
  267. let sqlString = 'USE ' + dbName + '; ' + item['CONCAT("DROP TABLE ", table_name, ";")'];
  268. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  269. if (err) {
  270. emitter.emit('err', err.err, 'sql执行出错');
  271. } else {
  272. emitter.emit('ok', results);
  273. }
  274. });
  275. }, this);
  276. });
  277. emitter.on('getAllTable', () => {
  278. //定义SQL语句查询
  279. let pool = db.pool.userDatabasePool,
  280. sqlString = 'SELECT CONCAT("DROP TABLE ", table_name, ";") FROM information_schema.tables WHERE table_schema = ?',
  281. value = [dbName];
  282. db.queryArgs(pool, sqlString, value, (err, results, fields) => {
  283. if (err) {
  284. emitter.emit('err', err.err, 'sql执行出错');
  285. } else {
  286. if (results.length === 0) {
  287. emitter.emit('err', null, '该数据库无数据表');
  288. } else {
  289. emitter.emit('dropTable', results);
  290. }
  291. }
  292. });
  293. });
  294. emitter.emit('getAllTable');
  295. } else {
  296. res.send({
  297. status: 'signout',
  298. info: '未登录',
  299. url: '#/signin'
  300. });
  301. }
  302. }
  303. router.get('/', getDatabaseList);
  304. router.get('/getDatabaseCount', getDatabaseCount);
  305. router.get('/getDatabaseAllTables', getDatabaseTables);
  306. router.post('/dropDatabase', dropDatabase);
  307. router.post('/emptyDatabase', emptyDatabase);
  308. module.exports = router