123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- const express = require('express');
- const router = express.Router();
- const crypto = require('crypto');
- const events = require('events');
- const db = require('../common/mysqlConnection');
- /**
- * 获取数据库列表
- * @param {*} req
- * @param {*} res
- * @param {function} next
- */
- function getDatabaseList(req, res, next) {
- if (req.session.signinStatus) {
- let emitter = new events.EventEmitter(),
- pageIndex = req.query.pageIndex - 1,
- orderBy = req.query.orderBy,
- searchValue = req.query.search;
- emitter.on('ok', (results, count) => {
- res.send({
- status: 'success',
- info: '获取成功',
- data: {
- 'results': results,
- 'count': count
- }
- });
- });
- emitter.on('err', (err, errInfo) => {
- res.json({
- status: 'error',
- info: errInfo,
- error: err
- });
- });
- emitter.on('selectDatabseInfoFromDbOwnTable', () => {
- switch (orderBy) {
- case 'userId':
- orderBy = 'dbOwnUuid';
- break;
- case 'databaseName':
- orderBy = 'dbName';
- break;
- case 'createDate':
- orderBy = 'createDate';
- break;
- default:
- orderBy = 'dbOwnUuid';
- break;
- }
- if (pageIndex > 0) {
- pageIndex = parseInt(pageIndex, 10) * 20;
- } else {
- pageIndex = 0;
- }
- //定义SQL语句查询
- let pool = db.pool.userInfoDatabasePool,
- 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()',
- value = [orderBy, pageIndex];
- if (searchValue) {
- // 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()';
- 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()';
- searchValue = '%' + searchValue + '%';
- value = [searchValue, searchValue, searchValue, searchValue, orderBy, pageIndex];
- }
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, '数据库查询出错');
- } else {
- emitter.emit('ok', results[0], results[1][0]['FOUND_ROWS()']);
- }
- });
- });
- emitter.emit('selectDatabseInfoFromDbOwnTable');
- } else {
- res.send({
- status: 'signout',
- info: '未登录',
- url: '#/signin'
- });
- }
- }
- /**
- * 获取数据库总数
- * @param {*} req
- * @param {*} res
- * @param {function} next
- */
- function getDatabaseCount(req, res, next) {
- if (req.session.signinStatus) {
- let emitter = new events.EventEmitter();
- emitter.on('ok', (results) => {
- res.send({
- status: 'success',
- info: '获取成功',
- data: {
- 'results': results
- }
- });
- });
- emitter.on('err', (err, errInfo) => {
- res.json({
- status: 'error',
- info: errInfo,
- error: err
- });
- });
- emitter.on('selectUserCountFromUserTable', () => {
- //定义SQL语句查询
- let pool = db.pool.userInfoDatabasePool,
- sqlString = 'SELECT COUNT(1) FROM dbOwn_test',
- value = null;
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, '数据库查询出错');
- } else {
- emitter.emit('ok', results[0]['COUNT(1)']);
- }
- });
- });
- emitter.emit('selectUserCountFromUserTable');
- } else {
- res.send({
- status: 'signout',
- info: '未登录',
- url: '#/signin'
- });
- }
- }
- /**
- * 获取数据库所有表
- * @param {*} req
- * @param {*} res
- * @param {function} next
- */
- function getDatabaseTables(req, res, next) {
- if (req.session.signinStatus) {
- let emitter = new events.EventEmitter(),
- dbName = req.query.dbName;
- emitter.on('ok', (results) => {
- res.send({
- status: 'success',
- info: '获取成功',
- data: {
- 'results': results
- }
- });
- });
- emitter.on('err', (err, errInfo) => {
- res.json({
- status: 'error',
- info: errInfo,
- error: err
- });
- });
- emitter.on('selectDatabseTables', () => {
- //定义SQL语句查询
- let pool = db.pool.userDatabasePool,
- sqlString = 'SELECT * FROM information_schema.`tables` a WHERE a.TABLE_SCHEMA = ?',
- value = [dbName];
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, '数据库查询出错');
- } else {
- emitter.emit('ok', results);
- }
- });
- });
- emitter.emit('selectDatabseTables');
- } else {
- res.send({
- status: 'signout',
- info: '未登录',
- url: '#/signin'
- });
- }
- }
- /**
- * 删除数据库
- * @param {*} req
- * @param {*} res
- * @param {function} next
- */
- function dropDatabase(req, res, next) {
- if (req.session.signinStatus) {
- let emitter = new events.EventEmitter(),
- dbName = req.body.dbName;
- emitter.on('ok', (results) => {
- res.send({
- status: 'success',
- info: '删除成功',
- data: {
- 'results': results
- }
- });
- });
- emitter.on('err', (err, errInfo) => {
- res.json({
- status: 'error',
- info: errInfo,
- error: err
- });
- });
- emitter.on('deleteRow', () => {
- let pool = db.pool.userInfoDatabasePool,
- sqlString = 'DELETE FROM dbOwn_test WHERE dbName = ?',
- value = [dbName];
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, 'sql执行出错');
- } else {
- emitter.emit('ok', results);
- }
- });
- })
- emitter.on('dropDb', () => {
- //定义SQL语句
- let pool = db.pool.userDatabasePool,
- sqlString = 'DROP DATABASE ' + dbName,
- value = null;
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, 'sql执行出错');
- } else {
- emitter.emit('deleteRow', results);
- }
- });
- });
- emitter.emit('dropDb');
- } else {
- res.send({
- status: 'signout',
- info: '未登录',
- url: '#/signin'
- });
- }
- }
- /**
- * 清空数据库
- * @param {*} req
- * @param {*} res
- * @param {function} next
- */
- function emptyDatabase(req, res, next) {
- if (req.session.signinStatus) {
- let emitter = new events.EventEmitter(),
- dbName = req.body.dbName;
- emitter.on('ok', (results) => {
- res.send({
- status: 'success',
- info: '删除成功',
- data: {
- 'results': results
- }
- });
- });
- emitter.on('err', (err, errInfo) => {
- res.json({
- status: 'error',
- info: errInfo,
- error: err
- });
- });
- emitter.on('dropTable', (results) => {
- let pool = db.pool.userDatabasePool,
- value = null;
- results.forEach((item, index) => {
- let sqlString = 'USE ' + dbName + '; ' + item['CONCAT("DROP TABLE ", table_name, ";")'];
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, 'sql执行出错');
- } else {
- emitter.emit('ok', results);
- }
- });
- }, this);
- });
- emitter.on('getAllTable', () => {
- //定义SQL语句查询
- let pool = db.pool.userDatabasePool,
- sqlString = 'SELECT CONCAT("DROP TABLE ", table_name, ";") FROM information_schema.tables WHERE table_schema = ?',
- value = [dbName];
- db.queryArgs(pool, sqlString, value, (err, results, fields) => {
- if (err) {
- emitter.emit('err', err.err, 'sql执行出错');
- } else {
- if (results.length === 0) {
- emitter.emit('err', null, '该数据库无数据表');
- } else {
- emitter.emit('dropTable', results);
- }
- }
- });
- });
- emitter.emit('getAllTable');
- } else {
- res.send({
- status: 'signout',
- info: '未登录',
- url: '#/signin'
- });
- }
- }
- router.get('/', getDatabaseList);
- router.get('/getDatabaseCount', getDatabaseCount);
- router.get('/getDatabaseAllTables', getDatabaseTables);
- router.post('/dropDatabase', dropDatabase);
- router.post('/emptyDatabase', emptyDatabase);
- module.exports = router
|