error("数据库管理插件只允许在开发环境下使用");
        }
        return parent::_initialize();
    }
    /**
     * 查看
     */
    public function index()
    {
        $tables_data_length = $tables_index_length = $tables_free_length = $tables_data_count = 0;
        $tables = $list = [];
        $list = Db::query("SHOW TABLES");
        foreach ($list as $key => $row) {
            $tables[] = ['name' => reset($row), 'rows' => 0];
        }
        $data['tables'] = $tables;
        $data['saved_sql'] = [];
        $this->view->assign($data);
        return $this->view->fetch();
    }
    /**
     * SQL查询
     */
    public function query()
    {
        $do_action = $this->request->post('do_action');
        echo '';
        if ($do_action == '') {
            exit(__('Invalid parameters'));
        }
        $tablename = $this->request->post("tablename/a");
        if(empty($tablename)){
        }
        if (in_array($do_action, array('doquery', 'optimizeall', 'repairall'))) {
            $this->$do_action();
        } elseif (count($tablename) == 0) {
            exit(__('Invalid parameters'));
        } else {
            foreach ($tablename as $table) {
                $this->$do_action($table);
                echo "
";
            }
        }
    }
    /**
     * 备份列表
     * @internal
     */
    public function backuplist()
    {
        $config = get_addon_config('database');
        $backupDir = ROOT_PATH . 'public' . DS . $config['backupDir'];
        $backuplist = [];
        foreach (glob($backupDir . "*.zip") as $filename) {
            $time = filemtime($filename);
            $backuplist[$time] =
                [
                    'file' => str_replace($backupDir, '', $filename),
                    'date' => date("Y-m-d H:i:s", $time),
                    'size' => format_bytes(filesize($filename))
                ];
        }
        krsort($backuplist);
        $this->success("", null, ['backuplist' => array_values($backuplist)]);
    }
    /**
     * 还原
     */
    public function restore($ids = '')
    {
        $config = get_addon_config('database');
        $backupDir = ROOT_PATH . 'public' . DS . $config['backupDir'];
        if ($this->request->isPost()) {
            $action = $this->request->request('action');
            $file = $this->request->request('file');
            if (!preg_match("/^backup\-([a-z0-9\-_\.]+)\.zip$/i", $file)) {
                $this->error(__("Invalid parameters"));
            }
            $file = $backupDir . $file;
            if ($action == 'restore') {
                if (!class_exists('ZipArchive')) {
                    $this->error("服务器缺少php-zip组件,无法进行还原操作");
                }
                try {
                    $dir = RUNTIME_PATH . 'database' . DS;
                    if (!is_dir($dir)) {
                        mkdir($dir, 0755);
                    }
                    $zip = new ZipArchive;
                    if ($zip->open($file) !== true) {
                        throw new Exception(__('Can not open zip file'));
                    }
                    if (!$zip->extractTo($dir)) {
                        $zip->close();
                        throw new Exception(__('Can not unzip file'));
                    }
                    $zip->close();
                    $filename = basename($file);
                    $sqlFile = $dir . str_replace('.zip', '.sql', $filename);
                    if (!is_file($sqlFile)) {
                        throw new Exception(__('Sql file not found'));
                    }
                    $filesize = filesize($sqlFile);
                    $list = Db::query('SELECT @@global.max_allowed_packet');
                    if (isset($list[0]['@@global.max_allowed_packet']) && $filesize >= $list[0]['@@global.max_allowed_packet']) {
                        Db::execute('SET @@global.max_allowed_packet = ' . ($filesize + 1024));
                        //throw new Exception('备份文件超过配置max_allowed_packet大小,请修改Mysql服务器配置');
                    }
                    $sql = file_get_contents($sqlFile);
                    Db::clear();
                    //必须重连一次
                    Db::connect([], true)->query("select 1");
                    Db::getPdo()->exec($sql);
                } catch (Exception $e) {
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    $this->error($e->getMessage());
                }
                $this->success(__('Restore successful'));
            } elseif ($action == 'delete') {
                unlink($file);
                $this->success(__('Delete successful'));
            }
        }
    }
    /**
     * 备份
     */
    public function backup()
    {
        $config = get_addon_config('database');
        $backupDir = ROOT_PATH . 'public' . DS . $config['backupDir'];
        if ($this->request->isPost()) {
            if (!class_exists('ZipArchive')) {
                $this->error("服务器缺少php-zip组件,无法进行备份操作");
            }
            $database = config('database');
            try {
                $backup = new Backup($database['hostname'], $database['username'], $database['database'], $database['password'], $database['hostport']);
                $backup->setIgnoreTable($config['backupIgnoreTables'])->backup($backupDir);
            } catch (Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success(__('Backup successful'));
        }
        return;
    }
    private function viewinfo($name)
    {
        $row = Db::query("SHOW CREATE TABLE `{$name}`");
        $row = array_values($row[0]);
        $info = $row[1];
        echo "