{"id":1601,"date":"2013-09-30T01:50:18","date_gmt":"2013-09-30T06:50:18","guid":{"rendered":"http:\/\/www.dpriver.com\/blog\/?page_id=1601"},"modified":"2013-09-30T02:09:28","modified_gmt":"2013-09-30T07:09:28","slug":"remove-duplicated-parenthesis-in-sql-statement","status":"publish","type":"page","link":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/remove-duplicated-parenthesis-in-sql-statement\/","title":{"rendered":"Remove duplicated parenthesis in SQL statement"},"content":{"rendered":"

General SQL Parser can be use to remove duplicated parenthesis in SQL statement. After parsing sql query, all detailed parse tree information was available for further processing.<\/p>\n

Take this sql for example:<\/p>\n

    \r\nselect * from ((select f from t1) union ((select f from t2)))\r\n<\/pre>\nyou can easily remove duplicated parenthesis in this subquery<\/p>\n\r\n((select f from t2))\r\n<\/pre>\nwith code like this:<\/p>\n\r\n    private static int removeDuplicatedParenthesis(TSourceTokenList sourceTokenList){\r\n        int cnt = 0;\r\n        TSourceToken st = null, prevEndToken = null;\r\n        boolean inParenthesis = false;\r\n        for(int i=0;i<sourceTokenList.size();i++){\r\n            st = sourceTokenList.get(i);\r\n            if (st.isnonsolidtoken()) continue;\r\n            if ((st.tokencode == '(')&&(st.getLinkToken() != null)){\r\n               if (inParenthesis){\r\n                  if (st.getLinkToken() == prevEndToken.prevSolidToken()){\r\n                      \/\/this is duplicated token, remove this token\r\n                      st.setString(\"\");\r\n                      st.getLinkToken().setString(\"\");\r\n                      cnt++;\r\n                  }\r\n                  prevEndToken = st.getLinkToken();\r\n               }else {\r\n                   inParenthesis = true;\r\n                   prevEndToken = st.getLinkToken();\r\n               }\r\n            }else {\r\n                inParenthesis = false;\r\n                prevEndToken = null;\r\n            }\r\n        }\r\n        return cnt;\r\n    }\r\n\r\n<\/pre>\nand the result sql will be like this:<\/p>\n\r\nselect * from ((select f from t1) union (select f from t2))\r\n<\/pre>\nFor predicate in where clause, the same rule applied.<\/p>\n\r\nselect * from user_table where ((username like '%admin%'));\r\n<\/pre>\nwill be turned into this one:<\/p>\n\r\nselect * from user_table where (username like '%admin%');\r\n<\/pre>\nFor SQL like this:<\/p>\n\r\nselect * from user_table where (f1 > 1 or f2 > 2) and f3 > 3;\r\n<\/pre>\nthis tool will not remove parenthesis which will change semantic of the original sql.<\/p>\nDownload this demo, Java version<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"General SQL Parser can be use to remove duplicated parenthesis in SQL statement. After parsing sql query, all detailed parse tree information was available for further processing. Take this sql for example: select * from ((select f from t1) union ((select f from t2))) you can easily remove duplicated parenthesis in this subquery ((select f from t2)) with code like this: private static int removeDuplicatedParenthesis(TSourceTokenList sourceTokenList){ int cnt = 0; TSourceToken st = null, prevEndToken = null; boolean inParenthesis = false; for(int i=0;i<sourceTokenList.size();i++){ st = sourceTokenList.get(i); if (st.isnonsolidtoken()) continue; if ((st.tokencode == ‘(‘)&&(st.getLinkToken() != null)){ if (inParenthesis){ if (st.getLinkToken() ==\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":235,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"gsp_feature_page_tt.php","meta":[],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":5}},"yoast_head":"\nrefactor sql by removing duplicated parenthesis in sql query<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n