sqloperate.jsp 17.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
<%@page language="java" contentType="text/html;charset=utf-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="<%=basePath %>css/bootstrap.min.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/bootstrap-datetimepicker.css"/>
    <link href="<%=basePath %>js/bootstrap-plugin/css/bootstrap.table.css" rel="stylesheet" media="screen"/>
    <link rel="stylesheet" href="<%=basePath %>css/bootstrap-responsive.min.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/fullcalendar.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/unicorn.main.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/unicorn.grey.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/jquery-ui.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/uniform.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/select2.css"/>
    <link rel="stylesheet" href="<%=basePath %>js/jstree/themes/proton/style.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/select2.css"/>
    <link rel="stylesheet" href="<%=basePath %>css/yoho.css"/>
    <script src="<%=basePath %>js/excanvas.min.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/jquery-1.12.0.min.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/jquery-ui.custom.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>/js/bootstrap.min.js"></script>
    <script src="<%=basePath %>/js/unicorn.js"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/datetimepicker/moment-with-locales.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/datetimepicker/bootstrap-datetimepicker.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/global.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.pagination.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.table.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.dialog.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.form.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.panel.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.alerts.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.accordion.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.breadcrumb.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.validate.js" charset="UTF-8"
            type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.form.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/layer/layer.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/bootstrap-plugin/bootstrap.select.js" charset="UTF-8" type="text/javascript"></script>
    <script src="<%=basePath %>js/jstree/jstree.min.js"></script>
    <script src="<%=basePath %>js/jquery.toaster.js"></script>
    <script src="<%=basePath %>js/jquery-plugin/jquery.cookies.js" charset="UTF-8" type="text/javascript"></script>
    <script>
        var contextPath = '<%=basePath %>';
    </script>
    <title>SQL通用查询</title>
</head>
<body>

<!-- 头部 -->
<div id="head">
</div>
<!-- 右侧具体内容 -->
<div id="content">
    <div id="breadcrumb">
        <a href="#" title="Go to Home" class="tip-bottom"><i
                class="icon-home"></i> Home</a> <a href="#" class="current">SQL通用查询</a>
    </div>
    <div class="container-fluid">
        <div class="widget-box">

            <div class="widget-content nopadding">
                <div class="widget-title" style="height: 53px;">
                    <div>
                        <div class="form-inline" role="form" style="margin-top: 12px;margin-left: 25px;">
                            <div class="input-group">
                                <span class="input-group-addon">当前数据源:</span>
                                <select id="dataSourceInfo" class="form-control">
                                    <option value="">请选择</option>
                                    <c:forEach items="${dbSourceList }" var="dbSource">
                                        <option value="${dbSource.id}">${dbSource.text}</option>
                                    </c:forEach>
                                    <%--<option value="10.66.6.7:3306">cms.yoho-mysql.org</option>
                                    <option value="10.66.6.8:3306">passport.yoho-mysql.org</option>
                                    <option value="10.66.6.9:3306">orders.yoho-mysql.org</option>
                                    <option value="10.66.6.10:3306">shops.yoho-mysql.org</option>
                                    <option value="172.31.200.189:3306">erp.yoho-mysql.org</option>
                                    <option value="10.67.2.36:3306">cobar1.yoho-mysql.org</option>
                                    <option value="10.67.2.36:3307">cobar2.yoho-mysql.org</option>
                                    <option value="10.67.2.36:3308">cobar3.yoho-mysql.org</option>
                                    <option value="10.67.2.36:3309">cobar4.yoho-mysql.org</option>
                                    <option value="172.31.200.228:3306">sso.yoho-mysql.org</option>
                                    <option value="172.31.57.114:3306">crm.yoho-mysql.org</option>
                                    <option value="10.66.6.136:3306">uic.yoho-mysql.org</option>
                                    <option value="10.67.2.40:3306">mars.yoho-mysql.org</option>
                                    <option value="10.67.2.40:3307">golbal&zixun.yoho-mysql.org</option>
                                    <option value="10.67.1.65:3307">yohologs.yoho-mysql.org</option>
                                    <option value="10.67.2.103:3306">msg.yoho-mysql.org</option>
                                    <option value="172.31.200.221:3306">lineshops</option>
                                    <option value="10.66.1.219:3306">yh_live</option>
                                    <option value="10.67.2.144:3307">seo</option>
                                    <option value="10.67.2.40:3306">MARS</option>
                                    <option value="10.67.2.40:3307">咨询</option>--%>
                                    <%--<option value="localhost:3306">Test</option>--%>
                                </select>
                            </div>

                            <div class="input-group">
                                <span class="input-group-addon">DB列表:</span>
                                <select id="dbInfo" class="form-control">
                                    <option value="">请选择</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>


        <div style="min-height:600px;">
            <!--search form-->
            <div class="form-inline">
                <div class="form-group">
                    <textarea id="queryContent" type="text" class="form-control" style="width:1000px" rows="4" placeholder="请输入SQL查询内容,自动过滤/* */和--注释内容,[Ctrl+Enter]提交查询" onKeyDown="ctlSubmit(event);"></textarea>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button class="btn btn-primary" onclick="queryFromBtn()" style="margin-top: 50px;">提交查询</button>
                    </div>
                </div>
            </div>


            <!-- result form-->
            <div class="widget-box">
                <div class="widget-title">
                    <h5>查询结果</h5>
                </div>
                <div class="widget-content nopadding">
                    <div id="resultDiv">

                    </div>
                    <div id="simplePageInfoDiv">

                    </div>
                    <div id="jobsTable">

                    </div>

                </div>
            </div>





    </div>
    </div>
</div>

<script src="<%=basePath %>script/common/genarate_left_panel.js"></script>
<script>
    $("#li_other").addClass("active open");
    $("#li_sql_operate").addClass("active");
</script>
</body>
</html>

<script>
    var ds_cookie_name="ds_cookie_name_sqloperate";
    var db_cookie_name="db_cookie_name_sqloperate";
    var page_info_current_page_index=0;
    //var default_source_user="root";
    //var default_source_pwd="123456";

    $(document).ready(function () {
        $('#dataSourceInfo').change(function(){
            show_db();
        });
        var dsName=$.cookie(ds_cookie_name);
        if(dsName){
            $("#dataSourceInfo").find("option[value='"+dsName+"']").attr("selected",true);
            show_db();
        }
        $("#queryContent").focus();
    });

    /**
     * 数据源发生变化时,加载该数据源下的db
     */
    function show_db(){
        $("#dbInfo").empty();
        $("#dbInfo").append("<option value=''>请选择</option>");
        var dsname=$.trim($("#dataSourceInfo option:selected").val());
        if(dsname){
            var param={};
            param.dataSourceInfo=dsname;
            //param.dataSourceUser=default_source_user;
            //param.dataSourcePwd=default_source_pwd;

            $.ajax({
                type: 'post',
                url: contextPath + "/sqlOperate/queryDbList",
                data: param,
                dataType: 'json',
                success: function (resp) {
                    if(resp.code == 200){
                        //加载dbinfos
                        var dbName=$.cookie(db_cookie_name);
                        $.each(resp.data, function (n, val) {
                            if(dbName&&dbName==val){
                                $("#dbInfo").append("<option selected = 'selected' value="+val+">" + val + "</option>");
                            }else{
                                $("#dbInfo").append("<option value="+val+">" + val + "</option>");
                            }
                        });
                    }
                },
                error: function () {
                    return;
                }
            });
        }

    }

    /**
     * ctrl+enter,事件
     */
    function ctlSubmit(event) {
        if (event.ctrlKey && event.keyCode == 13) {
            page_info_current_page_index=0;
            query();
        }
    }
    /**
     * 去掉sql注释 --类型
     */
    function removeAnnotateTypeOne(sql){
        var arry = sql.split("\n");//以换行符为分隔符将内容分割成数组
        var newArry = new Array();
        for(var i=0;i<arry.length;i++){
            var rowContent= $.trim(arry[i]);
            if(rowContent){
                var annoteTypeNum=rowContent.indexOf("--");
                if(annoteTypeNum>0){
                   // newArry.push(rowContent.substr(0,annoteTypeNum));
                    newArry.push(rowContent);
                }else if(annoteTypeNum==0){
                    //去掉
                }else{
                    newArry.push(rowContent);
                }
            }
        }
        return newArry.join(" ");
    }

    /**
     * 去掉sql注释 /*类型
     */
    function removeAnnotateTypeTwo(sql){
        var whileFlag=false;
        do{
            var beginAnnoteTypeNum = sql.indexOf("/*");
            var endAnnoteTypeNum=0;
            var headAnoteTypeOneStr='';
            var tailAnoteTypeOneStr='';
            if(beginAnnoteTypeNum>0){
                headAnoteTypeOneStr=sql.substr(0,beginAnnoteTypeNum);
                var otherSql=sql.substr(beginAnnoteTypeNum+2);
                endAnnoteTypeNum= otherSql.indexOf("*/");
                if(endAnnoteTypeNum<0){
                    alert("注释不符合规范:/*和*/必须成对出现");
                    return "";
                }
                tailAnoteTypeOneStr=otherSql.substr(endAnnoteTypeNum+2);
                sql=headAnoteTypeOneStr+" "+tailAnoteTypeOneStr;
                whileFlag=true;
            }else if(beginAnnoteTypeNum==0){
                headAnoteTypeOneStr="";
                var otherSql=sql.substr(beginAnnoteTypeNum+2);
                endAnnoteTypeNum= otherSql.indexOf("*/");
                if(endAnnoteTypeNum<0){
                    alert("注释不符合规范:/*和*/必须成对出现");
                    return "";
                }
                tailAnoteTypeOneStr=otherSql.substr(endAnnoteTypeNum+2);
                sql=headAnoteTypeOneStr+" "+tailAnoteTypeOneStr;
                whileFlag=true;
            }else{
                whileFlag=false;
            }
        }while(whileFlag);
        return sql;
    }

    function queryFromBtn(){
        page_info_current_page_index=0;
        query();
    }


    /**
     * 发送请求,并构造返回结果
     */
    function query(fromPaginationFlag){
        var queryThings=$.trim($("#queryContent").val());
        queryThings=removeAnnotateTypeOne(queryThings);
        queryThings=removeAnnotateTypeTwo(queryThings);
        queryThings= $.trim(queryThings);
        if(queryThings==null||queryThings==''){
            alert("参数错误,请输入查询条件");
            $("#queryContent").focus();
            return;
        }
        var param={};
        param.dataSourceInfo=$.trim($("#dataSourceInfo option:selected").val());
        param.dbInfo=$.trim($("#dbInfo option:selected").val());
        param.queryContent=queryThings;
        if(page_info_current_page_index<0){
            page_info_current_page_index=0;
        }
        param.currentPage=page_info_current_page_index;
        //param.dataSourceUser=default_source_user;
        //param.dataSourcePwd=default_source_pwd;

        if(param.dataSourceInfo==null||param.dataSourceInfo==''){
            alert("参数错误,请选择数据源");
            return;
        }else{
            $.cookie(ds_cookie_name, param.dataSourceInfo , { path: '/', expires: 365 });
            $.cookie(db_cookie_name, param.dbInfo , { path: '/', expires: 365 });
        }

        if(param.queryContent==null||param.queryContent==''){
            alert("参数错误,请输入查询条件");
            $("#queryContent").focus();
            return;
        }
        $("#resultDiv").html("<span style='color:red'>SQL:"+param.queryContent+"</span>");
        //发送请求到后台,获取数据源下的所有table信息
        $.ajax({
            type: 'post',
            url: contextPath + "/sqlOperate/query",
            data: param,
            dataType: 'json',
            success: function (resp) {
                if(resp.code == 200){
                    //加载表格
                    if(resp.data.queryDescr){
                        $("#resultDiv").append("<br>"+"<span style='color:red'>"+resp.data.queryDescr+"</span>");
                    }

                    $("#jobsTable").show();
                    if(resp.data.queryCount>0){
                        $("#jobsTable").table({
                            columnAutoWidth: false,
                            striped: true,
                            dataType: "json",
                            columns:resp.data.jsonArrayColumn ,
                            data:resp.data.jsonArrayData
                        });
                        if(!fromPaginationFlag){
                            showPageDiv(resp.data);
                        }
                    }else{
                        $("#jobsTable").hide();
                    }

                }else{
                    alert("查询报错,请检查数据库连接用户名、密码及sql语法!");
                    return;
                }
            },
            error: function () {
                alert("错误, 请求发生错误!");
                return;
            }
        });
    }

    function showPageDiv(respDataMap){
        if(respDataMap.showPi){
            $("#simplePageInfoDiv").html("");
            $("#simplePageInfoDiv").pagination({
                    totalCount:respDataMap.totalPi,
                    pageSize:respDataMap.pageSizePi,
                    //currentPage : respDataMap.currentPagePi,
                    onSelectPage: function(currentPage, pageSize) {
                        //alert("currentPage= " + currentPage + ",pageSize= " + pageSize);
                        page_info_current_page_index = (currentPage -1 );
                        $("#jobsTable").table("loadLoaclData",[]);
                        query(true);
                    }
            });

            $("#simplePageInfoDiv").show();
        }else{
            $("#simplePageInfoDiv").hide();
            $("#simplePageInfoDiv").html("");
        }
    }
</script>