var Service = require('Service');
var util = require('util');
var Dao = require('Dao');

/**
 * 获得数据库内的所有表和视图
 */
var GetTableViewList = function() { 
  Service.call(this);
  this.checkLogin = false;
};

util.inherits(GetTableViewList, Service);

module.exports = GetTableViewList;

/**
 * 处理服务请求
 * @param  {Object} req 服务请求对象
 * @param  {Object} res 服务响应对象
 */
GetTableViewList.prototype.process =  function(req, res) {
  var self = this;
  var where = "";
  if(req.type) {
    if(req.type.toLowerCase()=="table") {
      where += " and table_type='BASE TABLE'";
    } else {
      where += " and table_type='VIEW'";
    }
  }
  var dao = new Dao(this);
  dao.execute('select table_type,table_name from information_schema.TABLES '
    +'where table_schema="'+server.config.Database.database
    +'" '+ where +' order by table_name asc', null, function(rows) {
    res.tables = [];
    res.views = [];
    for(var i=0;i<rows.length;i++) {
      var table = rows[i];
      if(table.table_type=='VIEW') {
        res.views.push(table.table_name);
      } else {
        res.tables.push(table.table_name);
      }
    }
    self.end(res);
  });
};