美文网首页
Grails——Grails里面写SQL语句以及Oracle的分

Grails——Grails里面写SQL语句以及Oracle的分

作者: 奇乞祈兴 | 来源:发表于2017-06-02 09:02 被阅读0次

    Oracle分页:

    SELECT
        *
    FROM
        (
            SELECT
                a1.*, ROWNUM rn
            FROM
                (
                    SELECT
                        "t1".DATE_CREATED,
                        SUM ("t1".CHARGE_TIME) AS CHARGE_TIME,
                        SUM ("t1".CHARGING_CAPACITY) AS CHARGING_CAPACITY,
                        SUM ("t1".CHARGE_AMOUNT) AS CHARGE_AMOUNT,
                        MIN ("t1".PLATE_NUMBER) AS PLATE_NUMBER,
                        "t1".SELF_NUMBERING,
                        MIN ("t1".LINE) AS LINE,
                        MIN ("t1".FRAME_NUMBER) AS FRAME_NUMBER,
                        MIN ("t1".OWNER_NAME) AS OWNER_NAME
                    FROM
                        CHARGEPILE.V_CHARGE_BUS "t1"
                    WHERE
                        DATE_CREATED BETWEEN TO_DATE (
                            '2017-05-01',
                            'yyyy-mm-dd hh24:mi:ss'
                        )
                    AND TO_DATE (
                        '2017-05-31',
                        'yyyy-mm-dd hh24:mi:ss'
                    )
                    GROUP BY
                        "t1".DATE_CREATED,
                        "t1".SELF_NUMBERING
                    ORDER BY
                        MIN ("t1".OWNER_NAME) ASC,
                        MIN ("t1".LINE) ASC,
                        MIN ("t1".FRAME_NUMBER) ASC
                ) a1
            WHERE
                ROWNUM <= 15
        )
    WHERE
        rn >= 1
    

    Grails里面写SQL语句:

    • 首先要知道需要在哪个数据源里面执行SQL,定义一个数据源
    def dataSource
    
    • 然后写两条SQL
     //查询数据的SQL
            StringBuffer sql = new StringBuffer()
            //用来分页查询数据条数的SQL
            StringBuffer count = new StringBuffer()
            //分页参数
            Integer max = params.max ? params.int("max") : 15
            Integer offset = params.offset ? params.int("offset") : 0
            offset = offset / max + 1
            if (offset == 0) {
                offset = 1
            }
            Integer initial = (max * offset) - max + 1
            Integer end = max * offset
    
             if (count.size() == 0) {
                count.append("SELECT\n" +
                        "\t\"COUNT\" (*)\n" +
                        "FROM\n" +
                        "\t(\n" +
                        "\t\tSELECT\n" +
                        "\t\t\t\"t1\".DATE_CREATED,\n" +
                        "\t\t\tSUM (\"t1\".CHARGE_TIME) AS CHARGE_TIME,\n" +
                        "\t\t\tSUM (\"t1\".CHARGING_CAPACITY) AS CHARGING_CAPACITY,\n" +
                        "\t\t\tSUM (\"t1\".CHARGE_AMOUNT) AS CHARGE_AMOUNT,\n" +
                        "\t\t\tMIN (\"t1\".PLATE_NUMBER) AS PLATE_NUMBER,\n" +
                        "\t\t\t\"t1\".SELF_NUMBERING,\n" +
                        "\t\t\tMIN (\"t1\".LINE) AS LINE,\n" +
                        "\t\t\tMIN (\"t1\".FRAME_NUMBER) AS FRAME_NUMBER,\n" +
                        "\t\t\tMIN (\"t1\".OWNER_NAME) AS OWNER_NAME\n" +
                        "\t\tFROM\n" +
                        "\t\t\tCHARGEPILE.V_CHARGE_BUS \"t1\"")
    
                if (params.createStart && params.createEnd) {
                    count.append("WHERE\n" +
                            "\tDATE_CREATED BETWEEN TO_DATE (\n" +
                            "\t\t'" + params.createStart + "',\n" +
                            "\t\t'yyyy-mm-dd hh24:mi:ss'\n" +
                            "\t)\n" +
                            "AND TO_DATE (\n" +
                            "\t'" + params.createEnd + "',\n" +
                            "\t'yyyy-mm-dd hh24:mi:ss'\n" +
                            ")" + " ")
                }
    
                //运营商查询
                if (params.businessOwerName) {
                    count.append("AND ENTERPRISE_CODE = '" + params.businessOwerName + "'" + " ")
                }
    
                //线路查询
                if (params.line) {
                    count.append("AND line = '" + params.line + "'" + " ")
                }
    
                if (params.vehPlate) {
                    count.append("AND PLATE_NUMBER='" + params.vehPlate + "'" + " ")
                }
    
                count.append("GROUP BY\n" +
                        "\t\t\t\"t1\".DATE_CREATED,\n" +
                        "\t\t\t\"t1\".SELF_NUMBERING\n" +
                        "\t)")
            }
    
            if (sql.size() == 0) {
                sql.append("SELECT\n" +
                        "\t*\n" +
                        "FROM\n" +
                        "\t(\n" +
                        "\t\tSELECT\n" +
                        "\t\t\ta1.*, ROWNUM rn\n" +
                        "\t\tFROM\n" +
                        "\t\t\t(\n" +
                        "\t\t\t\tSELECT\n" +
                        "\t\t\t\t\t\"t1\".DATE_CREATED,\n" +
                        "\t\t\t\t\tSUM (\"t1\".CHARGE_TIME) AS CHARGE_TIME,\n" +
                        "\t\t\t\t\tSUM (\"t1\".CHARGING_CAPACITY) AS CHARGING_CAPACITY,\n" +
                        "\t\t\t\t\tSUM (\"t1\".CHARGE_AMOUNT) AS CHARGE_AMOUNT,\n" +
                        "\t\t\t\t\tMIN (\"t1\".PLATE_NUMBER) AS PLATE_NUMBER,\n" +
                        "\t\t\t\t\t\"t1\".SELF_NUMBERING,\n" +
                        "\t\t\t\t\tMIN (\"t1\".LINE) AS LINE,\n" +
                        "\t\t\t\t\tMIN (\"t1\".FRAME_NUMBER) AS FRAME_NUMBER,\n" +
                        "\t\t\t\t\tMIN (\"t1\".OWNER_NAME) AS OWNER_NAME\n" +
                        "\t\t\t\tFROM\n" +
                        "\t\t\t\t\tCHARGEPILE.V_CHARGE_BUS \"t1\"")
    
                if (params.createStart && params.createEnd) {
                    sql.append("WHERE\n" +
                            "\tDATE_CREATED BETWEEN TO_DATE (\n" +
                            "\t\t'" + params.createStart + "',\n" +
                            "\t\t'yyyy-mm-dd hh24:mi:ss'\n" +
                            "\t)\n" +
                            "AND TO_DATE (\n" +
                            "\t'" + params.createEnd + "',\n" +
                            "\t'yyyy-mm-dd hh24:mi:ss'\n" +
                            ")" + " ")
                }
    
                //运营商查询
                if (params.businessOwerName) {
                    sql.append("AND ENTERPRISE_CODE = '" + params.businessOwerName + "'" + " ")
                }
    
                //线路查询
                if (params.line) {
                    sql.append("AND line = '" + params.line + "'" + " ")
                }
    
                if (params.vehPlate) {
                    sql.append("AND PLATE_NUMBER='" + params.vehPlate + "'" + " ")
                }
    
                sql.append("GROUP BY\n" +
                        "\t\t\t\t\t\"t1\".DATE_CREATED,\n" +
                        "\t\t\t\t\t\"t1\".SELF_NUMBERING\n" +
                        "ORDER BY\n" +
                        "Min(\"t1\".OWNER_NAME) ASC,\n" +
                        "Min(\"t1\".LINE) ASC,\n" +
                        "Min(\"t1\".FRAME_NUMBER) ASC"+
                        "\t\t\t) a1\n" +
                        "\t\tWHERE\n" +
                        "\t\t\tROWNUM <= " + end + "\n" +
                        "\t)\n" +
                        "WHERE\n" +
                        "\trn >=" + initial)
            }
    
    • 最后就是执行SQL了
            def db = new groovy.sql.Sql(dataSource)
            List vehInfoList = db.rows(sql.toString())
    
            String str = db.firstRow(count.toString())
            Integer counts = str.substring(str.indexOf("=") + 1, str.indexOf("}")).toInteger()
    

    相关文章

      网友评论

          本文标题:Grails——Grails里面写SQL语句以及Oracle的分

          本文链接:https://www.haomeiwen.com/subject/nkbyfxtx.html