去除SQL語句某查詢條件

來源:互聯網
上載者:User

標籤:查詢條件   dex   stat   val   直接   nta   資料庫   ima   進入   

工作需求:根據不同條件查詢資料,目前有下拉式清單和選項按鈕兩種

如果只是查詢具體的條件,那很簡單,只需要在where 條件之後加上相應的條件即可,像這樣:where type = ‘門診’。當查詢的是全選或全部時,顯然的就要把相應的條件給去除,如果你是使用mybatis,那也很簡單,只需要在xml對應檔裡加上一些判斷條件,像這樣:

<select id="user" parameterType="user" resultType="User">        select * from user           <where>            <if test="id!=null and id!=‘‘">              id=#{id}            </if>            <if test="type!=null and type!=‘‘">              and type=#{type}            </if>            <if test="gender!=null and gender!=‘‘">              and gender=#{gender}            </if>          </where>       </select>

但是項目中的需求往往千奇百怪,由於我的SQL語句是寫在json設定檔裡的,而我又使用的是mybatis架構,我沒辦法將SQL語句直接寫在mybatis的xml對應檔裡,我只有把SQL作為一個參數傳遞給對應檔,就像上面傳遞id,type,gender一樣。

<select id="executeSQL" parameterType="java.lang.String" resultType="Map">        <![CDATA[ ${SQL} ]]>     </select>

因為我傳遞的是一句SQL語句而不是具體條件,當我的SQL語句裡出現特殊字元如比較字元時XML是會幫我轉義的,我不需要XML去轉義我的SQL,所以我要使用

 <![CDATA[  ]]> 

將我的SQL語句包裹起來,這樣SQL語句就不會被轉義了。這裡使用${ SQL }而不是#{ SQL },#將傳入的資料都當成一個字串,會對自動傳入的資料加一個雙引號,$將傳入的資料直接顯示產生在sql中,顯然我的SQL語句不需要加雙引號。(這裡我也先不考慮使用$會導致SQL注入的問題,後期我也會去做SQL檢查)

 

現在背景已經明確,如果想完成需求,我只有在SQL進入XML對應檔之前,將一些條件處理掉。SQL是寫在json設定檔裡的,預先的條件也是寫死的,如下:

"sql":"SELECT nvl(sum(a.ssrc),0) as count FROM CB_ME_YLZL a,CB_DI_DATE b,CB_DI_YLJG c,CB_DI_JZLX d WHERE a.sjid=b.sjid and a.yljgid=c.yljgid and a.jzlxid=d.jzlxid and c.yljgdj=‘3‘ and c.yljgmc = #{select1_val_select}  AND type = #{radio1_val_radio}"

 

當使用者選擇全選或是全部時,我需要把

and c.yljgmc = #{select1_val_select}  AND type = #{radio1_val_radio}

去掉。不是全選或全部時我需要把

#{select1_val_select} 和  #{radio1_val_radio}

替換成相應的條件。

代碼如下:

/**     * 處理SQL語句     *      * @param sql     * @return     */    public static String dealSQL(String sql, String str) {        sql = sql.trim().toUpperCase().replaceAll(" +", " ");        int form = sql.indexOf(" FROM ");        String begin = sql.substring(0, form);        begin = begin.replaceAll(" AS ", " AS C_");        String end = sql.substring(form, sql.length());        sql = begin+end;        String[] split1 = str.trim().toUpperCase().replaceAll("‘", "").replaceAll("\"", "").split(",");        for (String s : split1) {            if(StringUtils.isNotBlank(s)) {                String[] split2 = s.split(":");                                if(sql.contains(split2[0])) {                    if(split2[0].contains("VAL_RADIO") || split2[0].contains("VAL_SELECT")) {                            if(split2[1].equals("全選") || split2[1].equals("全部")) {                                sql = removeSQL(sql,"#{"+split2[0]+"}");                            }else {                                sql = sql.replace("#{"+split2[0]+"}", "‘" + split2[1] + "‘");                            }                    }else {                        sql = sql.replace("#{"+split2[0]+"}", "‘" + split2[1] + "‘");                    }                }            }        }        return sql;    }

json設定檔裡規定需要展現的欄位都要使用as ** 作為別名,但是如果別名為數字或特殊字元的話,oracle是不認的,如果別名使用雙引號引起來,orace認但json檔案又不認了,所以我使用最low的辦法,將所有別名加上一個C_,這樣資料庫就認了。

看一下  removeSQL方法:

/**     * 去除SQL語句某查詢條件     * @param sql     * @param choice     * @return     */    public static String removeSQL(String sql,String choice) {        int cho_first = sql.indexOf(choice);        int before_and = sql.lastIndexOf(" AND ", cho_first);        int before_where = sql.lastIndexOf(" WHERE ", cho_first);        int after_and = sql.indexOf(" AND ", cho_first);        int after_where = sql.indexOf(" WHERE ",cho_first);                if(before_where != -1) {            if(before_and != -1) {                if(before_and > before_where) {                    sql = sql.replace(sql.substring(before_and, cho_first), " ").replace(choice, " ");                }else {                    if(after_and != -1) {                        if(after_where != -1) {                            if(after_and < after_where) {                                sql = sql.replace(sql.substring(before_where+7, after_and+5), " ");                            }else {                                sql = sql.replace(sql.substring(before_where, cho_first), " ").replace(choice, " ");                            }                        }else {                            sql = sql.replace(sql.substring(before_where+7, after_and+5), " ");                        }                    }else {                        sql = sql.replace(sql.substring(before_where, cho_first), " ").replace(choice, " ");                    }                }            }else{                if(after_and != -1) {                    if(after_where != -1) {                        if(after_and < after_where) {                            sql = sql.replace(sql.substring(before_where+7, after_and+5), " ");                        }else {                            sql = sql.replace(sql.substring(before_where, cho_first), " ").replace(choice, " ");                        }                    }else {                        sql = sql.replace(sql.substring(before_where+7, after_and+5), " ");                    }                }else {                    sql = sql.replace(sql.substring(before_where, cho_first), " ").replace(choice, " ");                }            }        }        return sql;    }

邏輯也就是:當條件在不同地方,處理方式不同而已,下面是幾種羅列的可能存在條件的地方,上面代碼就是針對這些不同地方,將這些條件連同其前的and或者where(不影響其後的and條件使用的where,如第二條語句第一處位置的條件)去掉。(針對複雜的SQL,如使用with as 將查詢資料做暫存資料表,之後再使用select查詢暫存資料表的,這些代碼是不能處理的)

去除SQL語句某查詢條件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.