一、前言
关于taobao的tddl-common包(5.1.0)的com.taobao.tddl.common.jdbc.SQLPreParser语句解析类,解析获取DML、DQL语句中表名(findTableName),详见源码说明。
二、源码说明
package com.taobao.tddl.common.jdbc;@b@@b@import java.io.IOException;@b@import java.io.PrintStream;@b@import java.util.ArrayList;@b@import java.util.List;@b@import java.util.regex.Matcher;@b@import java.util.regex.Pattern;@b@@b@public class SQLPreParser@b@{@b@ private static Pattern ptable = Pattern.compile("\\s+([a-z0-9_@\\.\"$]+)\\s+");@b@ private static Pattern pinsert_into = Pattern.compile("\\s+into\\s+([a-z0-9_@\\.\"$]+)[\\s(]+");@b@ private static Pattern pdelete_from = Pattern.compile("\\s+from\\s+([a-z0-9_@\\.\"$]+)\\s+");@b@ private static Pattern pselect_from = Pattern.compile("\\s+from\\s+([a-z0-9_@\\.\"$]+)[\\s)]+");@b@ private static Pattern preplace_from = Pattern.compile("\\s+into\\s+([a-z0-9_@\\.\"$]+)[\\s(]+");@b@ private static Pattern pfrom_where = Pattern.compile("\\s+from\\s+(.*)\\s+where\\s+");@b@ private static String hintregx = "/\\*.*?\\*/";@b@@b@ public static String findTableName(String sql0)@b@ {@b@ String[] tables;@b@ int i;@b@ if (sql0 == null) return null;@b@ sql0 = sql0.trim();@b@ if (sql0.length() < 7) {@b@ return null;@b@ }@b@@b@ if (sql0.indexOf("/*") != -1)@b@ {@b@ sql0 = sql0.replaceAll(hintregx, "").trim();@b@ }@b@@b@ sql0 = sql0.toLowerCase();@b@ sql0 = sql0 + " ";@b@@b@ if (sql0.startsWith("update")) {@b@ m = ptable.matcher(sql0);@b@ if (m.find(6))@b@ return m.group(1);@b@@b@ return null;@b@ }@b@@b@ if (sql0.startsWith("delete")) {@b@ m = pdelete_from.matcher(sql0);@b@ if (m.find(6)) {@b@ return m.group(1);@b@ }@b@@b@ m = ptable.matcher(sql0);@b@ if (m.find(6))@b@ return m.group(1);@b@@b@ return null;@b@ }@b@@b@ if (sql0.startsWith("insert")) {@b@ m = pinsert_into.matcher(sql0);@b@ if (m.find(6))@b@ return m.group(1);@b@@b@ return null;@b@ }@b@@b@ if (sql0.startsWith("replace")) {@b@ m = preplace_from.matcher(sql0);@b@ if (m.find(6))@b@ return m.group(1);@b@@b@ return null;@b@ }@b@@b@ if (!(sql0.startsWith("select"))) {@b@ return null;@b@ }@b@@b@ Matcher m = pselect_from.matcher(sql0);@b@ if (m.find(6)) {@b@ return m.group(1);@b@ }@b@@b@ m = pfrom_where.matcher(sql0);@b@ if (m.find(6)) {@b@ String from2where = m.group(1);@b@@b@ tables = from2where.split(",");@b@ for (i = 1; i < tables.length; ++i)@b@ {@b@ if (tables[i].indexOf(40) == -1)@b@ return tables[i].trim().split("\\s")[0];@b@@b@ String subTable = findTableName(tables[i]);@b@ if (subTable != null) {@b@ return subTable;@b@ }@b@@b@ }@b@@b@ }@b@@b@ if (sql0.indexOf(")from") != -1) {@b@ System.out.println(sql0);@b@ sql0 = sql0.replaceAll("\\)from", ") from");@b@ return findTableName(sql0);@b@ }@b@@b@ return null;@b@ }@b@@b@ public static void main(String[] args) throws IOException {@b@ List sqls = new ArrayList();@b@ sqls.add("\t\r\t\r\n \n \tupdate \tt_a$ble0 set a=1");@b@ sqls.add("delete from t_a$ble0\r\n t where t.id = 0");@b@ sqls.add("delete from t_a$ble0");@b@ sqls.add("insert into t_a$ble0 t values(?,?) where t.id = 0");@b@ sqls.add("insert into t_a$ble0(col_a, col_b) values(?,?) where id = 0");@b@ sqls.add("select count(*) from t_a$ble0");@b@ sqls.add("select 1 from t_a$ble0 t where t.id=0");@b@ sqls.add("select 1 from (select id from t_a$ble0) t where t.id = 5");@b@ sqls.add("select 1 from(select id from t_a$ble0) t where t.id = 5");@b@ sqls.add("select 1 from (select id from table2) t, t_a$ble0 a where t.id = a.id");@b@ sqls.add("select 1 from t_a$ble0 a, (select id from table2) t where t.id = a.id");@b@ sqls.add("select count(*) from CRM_KNOWLEDGE_DETAIL kc,CRM_KNOWLEDGE_BASE a where a.id=kc.KNOWLEDGE_ID");@b@ sqls.add("SELECT * FROM (SELECT CAST(STR2NUMLIST(#in#) AS NUMTABLETYPE) FROM dual) WHERE rownum <= 200");@b@ sqls.add("insert into ic_cache@lnk_icdb0 values (:b1 , sysdate) ");@b@ sqls.add("select a ,r from icuser.tb0 where spu_id=:f1 and auction_type <> 'a' ");@b@ sqls.add("select id from tb0 a, table(cast(str2numlist(:1) as numtabletype )) t where a.id=:2");@b@ sqls.add("select id from table(cast(str2numlist(:1) as numtabletype )) t, tb0 a where a.id=:2");@b@ sqls.add("select id from table(cast(str2numlist(:1) as numtabletype )) t, table(cast(str2numlist(:1) as numtabletype )) b, tb0 a where a.id=:2");@b@ sqls.add("select id from table(cast(str2numlist(:1) as numtabletype )) t, (select col1 from tb2) b, tb0 a where a.id=:2");@b@ sqls.add("select id from table(cast(str2numlist(:1) as numtabletype )) t, (select col1,col2 from tb2) b, tb0 a where a.id=:2");@b@ sqls.add("select id from table(cast(str2numlist(:1) as numtabletype )) t, (select col1,col2 from tb2 where tb2.id=0) b, tb0 a where a.id=:2");@b@ sqls.add("select max(mod(nvl(option$,0),2))from objauth$ where obj#=:1 group by grantee# order by grantee# ");@b@ sqls.add("select output from table(dbms_workload_repository.awr_report_html(:dbid, :inst, :bid, :eid, :rpt_options))");@b@ sqls.add("DELETE crm_adgroup_detail WHERE status = 1 AND adgroupno = :1");@b@ sqls.add("SELECT * FROM \"ALIMM\".\"ADZONESCORE\"");@b@ sqls.add("select nvl(min(ts#), -1) \"sysauxts#\" from sys.ts$ where name = 'sysaux'");@b@ sqls.add("/* oracleoem */ select nvl(min(ts#), -1) \"sysauxts#\" from sys.ts$ where name = 'sysaux'");@b@ sqls.add("/* oracleoem */ select /* sss */nvl(min(ts#), -1) \"sysauxts#\" from sys.ts$ where name = 'sysaux'");@b@ sqls.add("failed:select u.id from (table(str2numlist(:1))) n join et_airsupply_users u on n.column_value = u.id");@b@ sqls.add("replace into t (i,c,d,ui) values (?,?,?,?)");@b@ sqls.add(" SELECT /*+ ordered use_nl(acc,rb) */ rb.ID,rb.USER_ID,rb.DATABASE_CODE,EVENT_EXTEND FROM (SELECT /*+index(crb,IDX_RA_SC_BILL_STAT) */ crb.USER_ID, min(crb.id) dt FROM RA_SC_BILL crb WHERE crb.status = 1 and crb.process_mode = 0 and rownum <= 20000 and DATABASE_CODE in (1, 2, 3) GROUP BY crb.USER_ID) acc, RA_SC_BILL rb WHERE rb.Id = acc.dt and rownum <= 123 and not exists (select 1 from RA_SC_BILL up where up.status = 2 and up.USER_ID = acc.USER_ID)");@b@ for (String sql : sqls)@b@ System.out.println(findTableName(sql) + " <-- " + sql);@b@ }@b@}