package com.salpa.web.controller.online;

import com.salpa.common.core.domain.AjaxResult;
import com.salpa.common.utils.StringUtils;
import com.salpa.online.service.OnlineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author zhuff
 *
 * 数据库在线查询
 */
@RestController
@RequestMapping("/online/demand")
public class OnlineController {

    @Autowired
    private OnlineService onlineService;

    /**
     * 获取数据源
     * @return
     */
    @PostMapping("getDataSource")
    public AjaxResult getDataSource() {
        return AjaxResult.success(onlineService.getDataSource());
    }

    /**
     * 连接数据库
     * @param databaseName
     * @return
     */
    @GetMapping("connectDataSource")
    public AjaxResult connectDataSource(String databaseName) {
        return onlineService.connectDataSource(databaseName);
    }

    /**
     * 执行
     * @param databaseName
     * @param script
     * @return
     */
    @GetMapping("testExecute")
    public AjaxResult testExecute(String databaseName,String script) {
        if (StringUtils.isEmpty(databaseName)) {
            return AjaxResult.error("请连接数据源");
        } else {
            return onlineService.testExecute(databaseName,script);
        }
    }
}
