解析sql的数据流向关系获取




/**
* @Auther: LGH
* @Date: 2023/11/29 09:15
* @Description:
*/
public class AnalysisDruidSqlUtil {

public static void main(String[] args) {
String sql ="\n" +
"INSERT INTO DMT.M_CQJC_RKSJ_LHQRKJCXXB\n" +
"(ENTITY_ID,\n" +
"ENTITY_NAME,\n" +
"ID,\n" +
"NAME,\n" +
"NAME_EN,\n" +
"CARD_NO,\n" +
"CARD_TYPE,\n" +
"SEX,\n" +
"BIRTHDAY,\n" +
"AGE,\n" +
"TEL,\n" +
"NATIONALITY,\n" +
"NATION,\n" +
"HYZK,\n" +
"POLITICS,\n" +
"EDU_LEVEL,\n" +
"PRINARY_SCHOOL,\n" +
"MIDDLE_SCHOOL,\n" +
"GRANDUATED_SCHOOL,\n" +
"NATIVE_PLACE,\n" +
"HUJI_PLACE,\n" +
"HUJI_TYPE,\n" +
"HUJI_PROVINCE,\n" +
"HUJI_CITY,\n" +
"HUJI_DISTRICT,\n" +
"HUJI_STREET,\n" +
"HUJI_COMMUNITY,\n" +
"HUJI_ADDRESS,\n" +
"YHZGX,\n" +
"HOME_ADDR,\n" +
"STREET_CODE,\n" +
"STREET,\n" +
"COMMUNITY_CODE,\n" +
"COMMUNITY,\n" +
"GRID_CODE,\n" +
"GRID,\n" +
"BUILDING_CODE,\n" +
"BUILDING,\n" +
"BUILDING_TYPE,\n" +
"HOUSE_CODE,\n" +
"ROOMNO,\n" +
"HOUSEDEUSE,\n" +
"HOUSESTAIR,\n" +
"JU_TYPE,\n" +
"ZHUSUO_TYPE,\n" +
"ADDTIME,\n" +
"SHBXH,\n" +
"DWSBBH,\n" +
"CBLX,\n" +
"SBDQZT,\n" +
"SBQT,\n" +
"MATE_CARDNO,\n" +
"MATE_NAME,\n" +
"MATE_HUJI_TYPE,\n" +
"FA_CARDNO,\n" +
"FA_NAME,\n" +
"FA_HUJI_TYPE,\n" +
"MO_CARDNO,\n" +
"MO_NAME,\n" +
"MO_HUJI_TYPE,\n" +
"GLORIOUS_PERMIT_NUMBER,\n" +
"STATISTICS_NUMBER,\n" +
"COMPANY,\n" +
"TYSHXYDM,\n" +
"COMPANY_ADDR_DISTRICT,\n" +
"COMPANY_ADDR,\n" +
"COMPANYTEL,\n" +
"GJJ_NO,\n" +
"GJJ_STATUS,\n" +
"GLJT_LEVEL,\n" +
"YLBZ_LEVEL,\n" +
"SFJZRK,\n" +
"SFLDRK,\n" +
"SFCZRK,\n" +
"SFGLRK,\n" +
"SFGLJT,\n" +
"SFTXRY,\n" +
"SFDBRY,\n" +
"SFLXRY,\n" +
"SFRCYJ,\n" +
"SFGCCZY,\n" +
"SFCJ,\n" +
"SFJS,\n" +
"SFZXXS,\n" +
"SFDJ,\n" +
"SFYLBZ,\n" +
"SFSW,\n" +
"SFWG,\n" +
"SFQY,\n" +
"SFSI,\n" +
"DATA_SOURCE,\n" +
"BY1,\n" +
"BY2,\n" +
"BY3,\n" +
"BY4,\n" +
"BY5,\n" +
"SYS_BULIDING_TIME,\n" +
"CJLB,\n" +
"CJDJ,\n" +
"SFDY,\n" +
"SFGAT,\n" +
"SFTYJR,\n" +
"SFBMDRK,\n" +
"SFSY,\n" +
"SFDBH,\n" +
"SFGE,\n" +
"SFYG,\n" +
"SFLS,\n" +
"SFFLYZ,\n" +
"SFSFJDR,\n" +
"SFSG,\n" +
"SFGQ,\n" +
"SFKJET,\n" +
"SFWRFY,\n" +
"SFSHJJDX,\n" +
"TZQYS,\n" +
"QYFRSL,\n" +
"ZYZSSL,\n" +
"SFBSH ,\n" +
"SFJSZG_KJ ,\n" +
"SFJSZG_WS ,\n" +
"SFZYZG_HS ,\n" +
"SFJSZG_JSJ ,\n" +
"SFZYZG_SHGZ,\n" +
"SFZYZG_YH ,\n" +
"SFSJZS ,\n" +
"SFKCSJZCGCS,\n" +
"SFZYYS ,\n" +
"SFZCAQGCS ,\n" +
"SFYFYZYZG ,\n" +
"SFZJGCS ,\n" +
"SFJLGCS ,\n" +
"SFSWS ,\n" +
"SFJSZG_SJ ,\n" +
"SFZYZG_FCJJ,\n" +
"SFZCXFGCS ,\n" +
"SFJSZG_JJZY,\n" +
"SFZCJZS ,\n" +
"SFZYZG_GCZX,\n" +
"SFZDJB ,\n" +
"SFZYZG_JS ,\n" +
"DAT_DT,\n" +
"ETL_DT,\n" +
"GLDM_STA_DT,\n" +
"GLDM_END_DT,\n" +
"GLDM_DEL_FLAG,\n" +
"DSES_UUID)\n" +
"WITH T6 AS\n" +
" (SELECT /*+ PARALLEL(8)*/\n" +
" CARD_NO,\n" +
" SHBXH,\n" +
" DWSBBH,\n" +
" CBLX,\n" +
" SBDQZT,\n" +
" COMPANY,\n" +
" TYSHXYDM,\n" +
" SSQY,\n" +
" JYCS,\n" +
" FRLXDH\n" +
" FROM (SELECT /*+ PARALLEL(8)*/\n" +
" CARD_NO,\n" +
" SHBXH,\n" +
" DWSBBH,\n" +
" CBLX,\n" +
" SBDQZT,\n" +
" COMPANY,\n" +
" TYSHXYDM,\n" +
" SSQY,\n" +
" JYCS,\n" +
" FRLXDH,\n" +
" ROW_NUMBER() OVER(PARTITION BY CARD_NO ORDER BY UPDATETIME DESC) RN\n" +
" FROM SUM.C_RKK_SBXX_LHGRSBXXB\n" +
" WHERE CARD_NO IS NOT NULL)\n" +
" WHERE RN = 1\n" +
" AND SBDQZT = '正常参保'),\n" +
"T8 AS\n" +
" (SELECT T.CARD_NO, T.RELATION_NAME, T.RELATION_CARD_NO, A.HUJI_TYPE\n" +
" FROM (SELECT CARD_NO,\n" +
" RELATION_NAME,\n" +
" RELATION_CARDNO RELATION_CARD_NO,\n" +
" ROW_NUMBER() OVER(PARTITION BY CARD_NO ORDER BY CARD_NO DESC) RN\n" +
" FROM SUM.C_RKK_JTXX_LHQGRGX\n" +
" WHERE RELATION = '配偶') T,\n" +
" SUM.C_RKK_JBXX_LHGMHJXXB A\n" +
" WHERE T.RELATION_CARD_NO = A.CARD_NO(+)\n" +
" AND T.RN = 1),\n" +
"T9 AS\n" +
" (SELECT T.CARD_NO, T.RELATION_NAME, T.RELATION_CARD_NO, A.HUJI_TYPE\n" +
" FROM (SELECT CARD_NO,\n" +
" RELATION_NAME,\n" +
" RELATION_CARD_NO,\n" +
" ROW_NUMBER() OVER(PARTITION BY CARD_NO ORDER BY CARD_NO DESC) RN\n" +
" FROM SUM.C_RKK_JTXX_LHQGRGXB\n" +
" WHERE RELATION = '父亲') T,\n" +
" SUM.C_RKK_JBXX_LHGMHJXXB A\n" +
" WHERE T.RELATION_CARD_NO = A.CARD_NO(+)\n" +
" AND T.RN = 1),\n" +
"T10 AS\n" +
" (SELECT T.CARD_NO, T.RELATION_NAME, T.RELATION_CARD_NO, A.HUJI_TYPE\n" +
" FROM (SELECT CARD_NO,\n" +
" RELATION_NAME,\n" +
" RELATION_CARD_NO,\n" +
" ROW_NUMBER() OVER(PARTITION BY CARD_NO ORDER BY CARD_NO DESC) RN\n" +
" FROM SUM.C_RKK_JTXX_LHQGRGXB\n" +
" WHERE RELATION = '母亲') T,\n" +
" SUM.C_RKK_JBXX_LHGMHJXXB A\n" +
" WHERE T.RELATION_CARD_NO = A.CARD_NO(+)\n" +
" AND T.RN = 1)\n" +
"SELECT /*+ PARALLEL(8)*/\n" +
" '100046' AS ENTITY_ID,\n" +
" '人口数据' AS ENTITY_NAME,\n" +
" T1.IDCODE AS ID,\n" +
" T1.NAME,\n" +
" T1.NAME_EN,\n" +
" T1.CARD_NO,\n" +
" T1.CARD_TYPE,\n" +
" T1.SEX,\n" +
" T1.BIRTHDAY,\n" +
" T1.AGE,\n" +
" CASE\n" +
" WHEN T13.CARDNO IS NOT NULL THEN\n" +
" T13.MTEL\n" +
" ELSE\n" +
" NVL(T1.MTEL, '未知')\n" +
" END TEL,\n" +
" T1.NATIONALITY,\n" +
" T1.NATION,\n" +
" T1.HYZK,\n" +
" T1.POLITICS,\n" +
" -- 个人文化程度(学历)\n" +
" CASE\n" +
" WHEN T2.CARD_NO IS NOT NULL AND T2.NJ = '幼儿园' THEN\n" +
" '幼儿园'\n" +
" WHEN T2.CARD_NO IS NOT NULL AND\n" +
" T2.NJ NOT IN ('七年级', '八年级', '九年级') THEN\n" +
" '小学'\n" +
" WHEN T2.CARD_NO IS NOT NULL AND T2.NJ IN ('七年级', '八年级', '九年级') THEN\n" +
" '初中'\n" +
" WHEN T3.CARD_NO IS NOT NULL THEN\n" +
" T3.XL\n" +
" ELSE\n" +
" T1.EDU_LEVEL\n" +
" END EDU_LEVEL,\n" +
" -- 小学学校\n" +
" CASE\n" +
" WHEN T2.CARD_NO IS NOT NULL AND\n" +
" T2.NJ NOT IN ('七年级', '八年级', '九年级') AND T2.NJ <> '幼儿园' THEN\n" +
" T2.SCHOOL\n" +
" ELSE\n" +
" '未知'\n" +
" END PRINARY_SCHOOL,\n" +
" -- 中学学校\n" +
" CASE\n" +
" WHEN T2.CARD_NO IS NOT NULL AND T2.NJ IN ('七年级', '八年级', '九年级') THEN\n" +
" T2.SCHOOL\n" +
" ELSE\n" +
" '未知'\n" +
" END MIDDLE_SCHOOL,\n" +
" NVL(T3.BYYX, '未知') GRANDUATED_SCHOOL,\n" +
" NVL(T1.NATIVE_PLACE, '未知') NATIVE_PLACE,\n" +
" CASE\n" +
" WHEN T4.CARD_NO IS NOT NULL THEN\n" +
" T4.HUJI_PLACE\n" +
" ELSE\n" +
" '未知'\n" +
" END HUJI_PLACE,\n" +
" CASE\n" +
" WHEN T4.CARD_NO IS NOT NULL THEN\n" +
" T4.HUJI_TYPE\n" +
" ELSE\n" +
" '未知'\n" +
" END HUJI_TYPE,\n" +
" CASE\n" +
" WHEN T4.CARD_NO IS NOT NULL THEN\n" +
" T4.HUJI_PROVINCE\n" +
" ELSE\n" +
" '未知'\n" +
" END HUJI_PROVINCE,\n" +
" CASE\n" +
" WHEN T4.CARD_NO IS NOT NULL THEN\n" +
" T4.HUJI_CITY\n" +
" ELSE\n" +
" '未知'\n" +
" END HUJI_CITY,\n" +
" CASE\n" +
" WHEN T4.CARD_NO IS NOT NULL THEN\n" +
" T4.HUJI_DISTRICT\n" +
" ELSE\n" +
" '未知'\n" +
" END HUJI_DISTRICT,\n" +
" CASE\n" +
" WHEN T4.CARD_NO IS NOT NULL THEN\n" +
" T4.HUJI_STREET\n" +
" ELSE\n" +
" '未知'\n" +
" END HUJI_STREET,\n" +
" CASE\n" +
" WHEN T4.CARD_NO IS NOT NULL THEN\n" +
" T4.HUJI_COMMUNITY\n" +
" ELSE\n" +
" '未知'\n" +
" END HUJI_COMMUNITY,\n" +
" CASE\n" +
" WHEN T4.CARD_NO IS NOT NULL THEN\n" +
" T4.HUJI_ADDRESS\n" +
" ELSE\n" +
" '未知'\n" +
" END HUJI_ADDRESS,\n" +
" -- 与户主关系(深户)\n" +
" T1.YHZGX YHZGX,\n" +
" NVL(T5.HOME_ADDR, '未知') HOME_ADDR,\n" +
" NVL(T5.STREET_CODE, '未知') STREET_CODE,\n" +
" NVL(T5.STREET, '未知') STREET,\n" +
" NVL(T5.COMMUNITY_CODE, '未知') COMMUNITY_CODE,\n" +
" NVL(T5.COMMUNITY, '未知') COMMUNITY,\n" +
" NVL(T5.GRID_CODE, '未知') GRID_CODE,\n" +
" NVL(T5.GRID, '未知') GRID,\n" +
" NVL(T5.BUILDING_CODE, '未知') BUILDING_CODE,\n" +
" NVL(T5.BUILDING, '未知') BUILDING,\n" +
" NVL(T5.BUILDING_TYPE, '未知') BUILDING_TYPE,\n" +
" T1.HOUSECODE,\n" +
" T5.ROOMNO,\n" +
" NVL(T5.HOUSEDEUSE, '未知') HOUSEDEUSE,\n" +
" T5.HOUSESTAIR,\n" +
" NVL(T5.JUZHU_TYPE, '未知') JUZHU_TYPE,\n" +
" NVL(T5.ZHUSUO_TYPE, '未知') ZHUSUO_TYPE,\n" +
" T5.ADDTIME,\n" +
" T6.SHBXH,\n" +
" T6.DWSBBH,\n" +
" T6.CBLX,\n" +
" T6.SBDQZT,\n" +
" -- 社保群体\n" +
" CASE\n" +
" WHEN T6.CBLX = '单位缴纳' OR T12.IDCARD IS NOT NULL THEN\n" +
" '是'\n" +
" ELSE\n" +
" '否'\n" +
" END SBQT,\n" +
" T8.RELATION_CARD_NO MATE_CARDNO,\n" +
" T8.RELATION_NAME MATE_NAME,\n" +
" T8.HUJI_TYPE MATE_HUJI_TYPE,\n" +
" T9.RELATION_CARD_NO FA_CARDNO,\n" +
" T9.RELATION_NAME FA_NAME,\n" +
" T9.HUJI_TYPE FA_HUJI_TYPE,\n" +
" T10.RELATION_CARD_NO MO_CARDNO,\n" +
" T10.RELATION_NAME MO_NAME,\n" +
" T10.HUJI_TYPE MO_HUJI_TYPE,\n" +
" '' GLORIOUS_PERMIT_NUMBER,\n" +
" '' STATISTICS_NUMBER,\n" +
" T6.COMPANY,\n" +
" T6.TYSHXYDM,\n" +
" T6.SSQY COMPANY_ADDR_DISTRICT,\n" +
" T6.JYCS COMPANY_ADDR,\n" +
" T6.FRLXDH COMPANYTEL,\n" +
" T7.GJJ_NO,\n" +
" T7.GJJ_STATUS,\n" +
" T11.GLJT_LEVEL,\n" +
" T11.YLBZ_LEVEL,\n" +
" T11.SFJZRK,\n" +
" T11.SFLDRK,\n" +
" T11.SFCZRK,\n" +
" T11.SFGLRK,\n" +
" T11.SFGLJT,\n" +
" T11.SFTXRY,\n" +
" T11.SFDBRY,\n" +
" T11.SFLXRY,\n" +
" T11.SFRCYJ,\n" +
" T11.SFGCCZY,\n" +
" T11.SFCJ,\n" +
" T11.SFJS,\n" +
" T11.SFZXXS,\n" +
" NVL(T11.SFDJ, '未知') SFDJ,\n" +
" T11.SFYLBZ,\n" +
" T11.SFSW,\n" +
" T11.SFWG,\n" +
" T11.SFQY,\n" +
" T11.SFSI,\n" +
" T1.DATA_SOURCE,\n" +
" '',\n" +
" '',\n" +
" '',\n" +
" '',\n" +
" '',\n" +
" T1.SYS_BUILDING_TIME,\n" +
" T16.CJLB,\n" +
" T16.CJDJ,\n" +
" T11.SFDY,\n" +
" T11.SFGAT,\n" +
" T11.SFTYJR,\n" +
" T11.SFBMDRK,\n" +
" T11.SFSY,\n" +
" T11.SFDBH,\n" +
" T11.SFGE,\n" +
" T11.SFYG,\n" +
" T11.SFLS,\n" +
" T11.SFFLYZ,\n" +
" T11.SFSFJDR,\n" +
" T11.SFSG,\n" +
" T11.SFGQ,\n" +
" T11.SFKJET,\n" +
" T11.SFWRFY,\n" +
" T11.SFSHJJDX,\n" +
" NVL(T17.TZQYS, 0) TZQYS,\n" +
" NVL(T18.QYFRSL, 0) QYFRSL,\n" +
" NVL(T19.ZYZSSL, 0) ZYZSSL,\n" +
" T11.SFBSH,\n" +
" T11.SFJSZG_KJ,\n" +
" T11.SFJSZG_WS,\n" +
" T11.SFZYZG_HS,\n" +
" T11.SFJSZG_JSJ,\n" +
" T11.SFZYZG_SHGZ,\n" +
" T11.SFZYZG_YH,\n" +
" T11.SFSJZS,\n" +
" T11.SFKCSJZCGCS,\n" +
" T11.SFZYYS,\n" +
" T11.SFZCAQGCS,\n" +
" T11.SFYFYZYZG,\n" +
" T11.SFZJGCS,\n" +
" T11.SFJLGCS,\n" +
" T11.SFSWS,\n" +
" T11.SFJSZG_SJ,\n" +
" T11.SFZYZG_FCJJ,\n" +
" T11.SFZCXFGCS,\n" +
" T11.SFJSZG_JJZY,\n" +
" T11.SFZCJZS,\n" +
" T11.SFZYZG_GCZX,\n" +
" T11.SFZDJB,\n" +
" T11.SFZYZG_JS,\n" +
" COALESCE(T1.DAT_DT, '${DAT_DT}') AS DAT_DT,\n" +
" '${ETL_DT}' AS ETL_DT,\n" +
" '${DAT_DT}' AS GLDM_STA_DT,\n" +
" '30001231' AS GLDM_END_DT,\n" +
" '0' AS GLDM_DEL_FLAG,\n" +
" SYS_GUID() AS DSES_UUID\n" +
" FROM SUM.C_RKK_JBXX_LHGMJBXXB T1, -- 罗湖公民基本信息表\n" +
" SUM.C_RKK_JYXX_LHXSXXB T2, -- 罗湖区学生信息\n" +
" (SELECT *\n" +
" FROM (SELECT T.*,\n" +
" ROW_NUMBER() OVER(PARTITION BY CARD_NO ORDER BY BYYX NULLS LAST, SYS_BULIDING_TIME DESC) RN\n" +
" FROM SUM.C_RKK_JBXX_LHGRXLXXB T)\n" +
" WHERE RN = 1) T3, -- 罗湖个人学历信息表\n" +
" ATM.B_RKK_JBXX_LHGMHJXXB T4, -- 罗湖公民户籍信息表\n" +
" SUM.C_RKK_JZXX_LHGMJZXXB T5,\n" +
" T6,\n" +
" SUM.C_RKK_GJJXX_LHGRGJJXXB T7, -- 罗湖区公积金缴纳人员基本信息\n" +
" T8,\n" +
" T9,\n" +
" T10,\n" +
" DMT.M_CQJC_RKSJ_LHQRKBQXXB T11, -- 罗湖区人口标签信息表\n" +
" (SELECT DISTINCT IDCARD_NEW IDCARD\n" +
" FROM STD.S_PCXT_LHQZHCSJSZX_FK_USERS_ZX_CUR\n" +
" WHERE TYPE = 2) T12,\n" +
" SUM.C_RKK_JBXX_LHGMDHHMXXB T13,\n" +
" SUM.C_RKK_JKXX_LHCJRXXXB T16, -- 残疾人信息\n" +
" (SELECT DISTINCT TZRZJHM, TZQYS\n" +
" FROM SUM.C_RKK_ZCXX_QYGRGDXX\n" +
" WHERE TZRZJHM IN (SELECT CARD_NO FROM SUM.C_RKK_JBXX_LHGMJBXXB)) T17, -- 企业个人股东(投资者)信息\n" +
" (SELECT DISTINCT CARD_NO, QYSL AS QYFRSL\n" +
" FROM SUM.C_RKK_ZCXX_LHGMGRZCXXB\n" +
" WHERE CARD_NO IN (SELECT CARD_NO FROM SUM.C_RKK_JBXX_LHGMJBXXB)) T18, -- 罗湖公民个人资产信息表(企业法人)\n" +
" (SELECT DISTINCT CARD_NO, ZSSL AS ZYZSSL\n" +
" FROM SUM.C_RKK_ZZXX_ZYJSRYZGZXX) T19 -- 专业技术人员资格证信息\n" +
" WHERE T1.CARD_NO = T2.CARD_NO(+)\n" +
" AND T1.CARD_NO = T3.CARD_NO(+)\n" +
" AND T1.CARD_NO = T4.CARD_NO(+)\n" +
" AND T1.CARD_NO = T5.CARD_NO(+)\n" +
" AND T1.CARD_NO = T6.CARD_NO(+)\n" +
" AND T1.CARD_NO = T7.CARD_NO(+)\n" +
" AND T1.CARD_NO = T8.CARD_NO(+)\n" +
" AND T1.CARD_NO = T9.CARD_NO(+)\n" +
" AND T1.CARD_NO = T10.CARD_NO(+)\n" +
" AND T1.CARD_NO = T11.CARD_NO(+)\n" +
" AND T1.CARD_NO = T12.IDCARD(+)\n" +
" AND T1.CARD_NO = T13.CARDNO(+)\n" +
" AND T1.CARD_NO = T16.CARD_NO(+)\n" +
" AND T1.CARD_NO = T17.TZRZJHM(+)\n" +
" AND T1.CARD_NO = T18.CARD_NO(+)\n" +
" AND T1.CARD_NO = T19.CARD_NO(+)" ;

sql="merge into dmt.m_qyfw_jbxx_frxx a\n" +
"using (select * from (\n" +
"select a.*,row_number()over(partition by tyshxydm_new order by updatetime desc) rn from atm.b_frk_jbxx_ssztjbxx a where xzqy='罗湖区')\n" +
"where rn=1) b\n" +
"on (a.tyshxydm=b.tyshxydm_new)\n" +
"when matched then update set \n" +
"A.std_adrees=B.std_adrees,\n" +
"a.std_street=b.std_street,\n" +
"a.std_community=b.std_community;\n" ;
AnalysisDruidSqlUtil.AnalysisSql(sql,null);
}
public static TableVo AnalysisSql(String sql,String dbType ) {
TableVo tableVo=new TableVo();
if (dbType==null){
dbType = JdbcConstants.MYSQL.name(); // 可以是ORACLE、POSTGRESQL、SQLSERVER、ODPS等
}
List tableMapVos=new ArrayList<>();
Map tablsmap=new HashMap<>();
Map tablsSchemaMap=new HashMap<>();
SQLStatement stmt = SQLUtils.parseSingleStatement(sql, dbType); // 将 SQL 语句解析为 SQLStatement 对象
Map mapInsertCols=new HashMap<>();
Map mapSelectCols=new HashMap<>();
String targetTable =null;
String sourceTable1=null;
int count=1;
if (stmt instanceof SQLInsertStatement) {
SQLInsertStatement insert = (SQLInsertStatement) stmt;
List sqlSelectItems= insert.getColumns();
for (SQLExpr sqlExpr:sqlSelectItems){
System.out.println(sqlExpr.toString());
mapInsertCols.put(count,sqlExpr.toString());
count++;
}
targetTable = insert.getTableName().getSimpleName(); // 获取目标表名

System.out.println(insert.getColumnsString());
SQLSelect source = insert.getQuery(); // 找出来源表查询
List sourceTables = new ArrayList<>(); // 存储来源表的列表
Set sourceTableAliases = new HashSet<>(); // 存储来源表别名的集合
getSources(source.getQueryBlock().getFrom(), sourceTables, sourceTableAliases,tableMapVos,tablsmap,tablsSchemaMap); // 获取主表及其关联的所有来源表
sourceTable1 =sourceTables.get(0).getAlias(); // 获取查询表别名
if (sourceTables.get(0) instanceof SQLExprTableSource) { // 如果是基本表,则直接获取表名
SQLExprTableSource exprTableSource = (SQLExprTableSource) (sourceTables.get(0));
System.out.println(exprTableSource.getAlias());
String sourceTableName = exprTableSource.getName().getSimpleName();
sourceTable1=sourceTableName;
if (exprTableSource.getExpr() instanceof SQLPropertyExpr ){
SQLPropertyExpr sqlPropertyExpr=(SQLPropertyExpr)exprTableSource.getExpr();
tablsSchemaMap.put(sourceTable1,sqlPropertyExpr.getOwnerName());
}


}
Map sourceTableMap = new HashMap<>(); // 存储来源表与对应别名或表名的映射关系

for (SQLTableSource sourceTable : sourceTables) {
if (sourceTable instanceof SQLExprTableSource) { // 如果是基本表,则直接获取表名
SQLExprTableSource exprTableSource = (SQLExprTableSource) sourceTable;
System.out.println(exprTableSource.getAlias());
String sourceTableName = exprTableSource.getName().getSimpleName();
if (tablsmap.containsKey(exprTableSource.getAlias())){
tablsmap.put(exprTableSource.getAlias(),sourceTableName);
if (exprTableSource.getExpr() instanceof SQLPropertyExpr){
SQLPropertyExpr sqlPropertyExpr= (SQLPropertyExpr) exprTableSource.getExpr();
tablsSchemaMap.put(sourceTableName,sqlPropertyExpr.getOwnerName()) ;
}
}else{
System.out.println("无别名"+sourceTableName);
}
sourceTableMap.put(sourceTable, sourceTableName);
} else if (sourceTable instanceof SQLSubqueryTableSource) { // 如果是子查询,则递归解析
SQLSubqueryTableSource subqueryTableSource = (SQLSubqueryTableSource) sourceTable;
System.out.println(subqueryTableSource.getAlias());
String name=null;
if (subqueryTableSource.getAlias()!=null){
name=subqueryTableSource.getAlias();
}
SQLSelect subquery = subqueryTableSource.getSelect();
List subquerySources = new ArrayList<>();
Set subqueryAliases = new HashSet<>();
if (subquery.getQuery() instanceof SQLUnionQuery){
SQLUnionQuery sqlUnionQuery=(SQLUnionQuery)subquery.getQuery();
List sqlSelectQueries= sqlUnionQuery.getRelations();
for (SQLSelectQuery sqlSelectQuery:sqlSelectQueries){
MySqlSelectQueryBlock source1= (MySqlSelectQueryBlock)sqlSelectQuery;
getSources(source1.getFrom(), subquerySources, subqueryAliases,tableMapVos,tablsmap,tablsSchemaMap);
}
}else{
getSources(subquery.getQueryBlock().getFrom(), subquerySources, subqueryAliases,tableMapVos,tablsmap,tablsSchemaMap);
}
for (SQLTableSource subquerySource : subquerySources) {
if (!sourceTables.contains(subquerySource)) {
// sourceTables.add(subquerySource); // 将子查询中的来源表加入列表中
}
String alias = subqueryAliases.contains(subquerySource.getAlias()) ? subquerySource.getAlias() : null; // 子查询中的来源表可能存在别名,进行处理
if (alias == null && subquerySource instanceof SQLExprTableSource) { // 如果没有别名,则使用表名作为键
SQLExprTableSource exprTableSource = (SQLExprTableSource) subquerySource;
alias = exprTableSource.getName().getSimpleName();
if (name!=null){
alias=name;
if (exprTableSource.getExpr() instanceof SQLPropertyExpr ){
System.out.println(((SQLPropertyExpr) exprTableSource.getExpr()).getName());
}
}
/* if (alias == null && ((SQLExprTableSource) subquerySource).getExpr() instanceof SQLExprTableSource){
SQLExprTableSource exprTableSource = (SQLExprTableSource) subquerySource;
alias = exprTableSource.getName().getSimpleName();
}*/
}
sourceTableMap.put(subquerySource, alias);
if (subqueryTableSource.getAlias()!=null){
if (tablsmap.containsKey(subqueryTableSource.getAlias())&&tablsmap.get(subqueryTableSource.getAlias()).toLowerCase().indexOf("select")>-1){
tablsmap.put(subqueryTableSource.getAlias(),subquerySource.toString());
}
}
System.out.println("来源表ming: " + alias);
}
} else {
throw new UnsupportedOperationException("不支持的子查询类型: " + sourceTable.getClass());
}
}

SQLSelectQueryBlock firstQueryBlock=source.getFirstQueryBlock();
List selectItems=firstQueryBlock.getSelectList();
String tableSql=null;
Integer scount=1;
for (SQLSelectItem sqlSelectItem : selectItems) {
String selectColumnAlias = sqlSelectItem.getAlias();
SQLExpr expr = sqlSelectItem.getExpr();
System.out.println(expr.toString());
sql=expr.toString().trim().toLowerCase();
if (expr instanceof SQLPropertyExpr) {
SQLPropertyExpr selectColumnExpr = (SQLPropertyExpr)expr;
ColifoVo colifoVo=new ColifoVo(getTableNameByAlias(selectColumnExpr.getOwnernName(),tablsmap),selectColumnExpr.getName());
System.out.println("表名:"+colifoVo.getSourceTableName()+",列名:"+colifoVo.getSourceColName());
setSelectCols(mapSelectCols,scount,colifoVo);
}else if (expr instanceof SQLIdentifierExpr) {
/**
* 查源表
*/
SQLIdentifierExpr selectColumnExpr = (SQLIdentifierExpr)expr;
ColifoVo colifoVo=new ColifoVo(sourceTable1,selectColumnExpr.getName());
System.out.println("列名:{"+selectColumnExpr.getName()+"},别名:{}"+selectColumnAlias);
mapSelectCols.put(scount,colifoVo);
}else if (expr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr sqlMethodInvokeExpr = (SQLMethodInvokeExpr)expr;
List sqlExprs=sqlMethodInvokeExpr.getArguments();
for (SQLExpr sqlExpr:sqlExprs){
if (sqlExpr instanceof SQLPropertyExpr){
SQLPropertyExpr sqlPropertyExpr= (SQLPropertyExpr) sqlExpr;
if (sqlPropertyExpr.getOwner() instanceof SQLIdentifierExpr){
SQLIdentifierExpr sqlIdentifierExpr=(SQLIdentifierExpr) sqlPropertyExpr.getOwner();
System.out.println(sqlIdentifierExpr.getName());
ColifoVo colifoVo=new ColifoVo(getTableNameByAlias(sqlPropertyExpr.getOwnerName(),tablsmap),sqlPropertyExpr.getName());
System.out.println("表名:"+colifoVo.getSourceTableName()+",列名:"+colifoVo.getSourceColName());
setSelectCols(mapSelectCols,scount,colifoVo);
}else{
setSelectColsNUll(mapSelectCols,scount);
}
}else if (sqlExpr instanceof SQLCharExpr ||sqlExpr instanceof SQLIntegerExpr ){
System.out.println("判断固定值");
}else{
setSelectColsNUll(mapSelectCols,scount);
}
}
} else if (expr instanceof SQLCaseExpr) {
List items=((SQLCaseExpr) expr).getItems();
if (CollectionUtils.isEmpty(items)){
for (SQLCaseExpr.Item item:items){
if (item.getConditionExpr() instanceof SQLPropertyExpr){
SQLPropertyExpr sqlPropertyExpr=(SQLPropertyExpr) item.getConditionExpr();
System.out.println(sqlPropertyExpr.getOwnerName());
System.out.println(sqlPropertyExpr.getName());
ColifoVo colifoVo=new ColifoVo(((SQLPropertyExpr) item.getConditionExpr()).getOwnerName(),((SQLPropertyExpr) item.getConditionExpr()).getName());
setSelectCols(mapSelectCols,scount,colifoVo);
} else if (item.getConditionExpr() instanceof SQLBinaryOpExpr){
SQLBinaryOpExpr sqlBinaryOpExpr=(SQLBinaryOpExpr) item.getConditionExpr();
ColifoVo colifoVo= getTableAndColBySQLBinaryOpExpr(sqlBinaryOpExpr,tablsmap);
setSelectCols(mapSelectCols,scount,colifoVo);
if (item.getValueExpr()!=null&&item.getValueExpr() instanceof SQLPropertyExpr){
SQLPropertyExpr sqlPropertyExpr =(SQLPropertyExpr)item.getValueExpr();
ColifoVo colifoVo1= getTableAndColBySQLPropertyExpr(sqlPropertyExpr,tablsmap);
setSelectCols(mapSelectCols,scount,colifoVo1);
}
}else{
setSelectColsNUll(mapSelectCols,scount);
}
}
}else{
setSelectColsNUll(mapSelectCols,scount);
}
}else if (expr instanceof SQLCharExpr) {
System.out.println("固定项值");
setSelectColsNUll(mapSelectCols,scount);
}else if (sql.indexOf("select")<0) {
ColifoVo colifoVo=new ColifoVo(sourceTable1,sql);
mapSelectCols.put(scount,colifoVo);
}else{
ColifoVo colifoVo=new ColifoVo(sourceTable1,sql);
mapSelectCols.put(scount,colifoVo);
}
/**
* 防止出现解析不了的情况
*/
if (!mapSelectCols.containsKey(count)){
setSelectColsNUll(mapSelectCols,scount);
}
scount++;
}

for (SQLTableSource sourceTable : sourceTables) {
String sourceTableName = sourceTableMap.get(sourceTable);
System.out.println("来源表: " + sourceTableName);
}
if (count==scount){
for (int i=0;i
}
}
System.out.println("目标表: " + targetTable);
/**
* 存储列的衍射关系
*/
addTableColMap(mapInsertCols,mapSelectCols,tableMapVos,targetTable);
for (TableMapVo tableMapVo:tableMapVos){
if (tablsmap.containsKey(tableMapVo.getSourceTableName())){
tableMapVo.setSourceTableName(tablsmap.get(tableMapVo.getSourceTableName()));
}
if (tablsmap.containsKey(tableMapVo.getTargetTableName())){
tableMapVo.setTargetTableName(tablsmap.get(tableMapVo.getTargetTableName()));
}
System.out.println("关联表1"+tableMapVo.getSourceTableName()+"关联表字段1"+tableMapVo.getSourceColName()+"关联表2"+tableMapVo.getTargetTableName()+"关联表字段2"+tableMapVo.getTargetColName());
}
}else if (stmt instanceof SQLMergeStatement){
Map updateSourceTableCol=new HashMap<>();
Map updateTargetTableCol=new HashMap<>();
SQLMergeStatement statement=(SQLMergeStatement)stmt;
if (statement.getInto() instanceof SQLExprTableSource){
SQLExprTableSource source=(SQLExprTableSource)statement.getInto();
if (source.getExpr() instanceof SQLPropertyExpr){
SQLPropertyExpr sqlPropertyExpr=(SQLPropertyExpr)source.getExpr();
targetTable=sqlPropertyExpr.getName();
tablsmap.put(source.getAlias(),sqlPropertyExpr.getName());
tablsSchemaMap.put(sqlPropertyExpr.getName(),sqlPropertyExpr.getOwnerName()) ;
}
}
if (statement.getUsing() instanceof SQLSubqueryTableSource){
SQLSubqueryTableSource subqueryTableSource = (SQLSubqueryTableSource) statement.getUsing();
System.out.println(subqueryTableSource.getAlias());
String name=null;
if (subqueryTableSource.getAlias()!=null){
name=subqueryTableSource.getAlias();
}
SQLSelect subquery = subqueryTableSource.getSelect();
if (subquery.getQuery() instanceof MySqlSelectQueryBlock){
MySqlSelectQueryBlock mySqlSelectQueryBlock=(MySqlSelectQueryBlock)subquery.getQuery();
if (mySqlSelectQueryBlock.getFrom() instanceof SQLExprTableSource){
SQLExprTableSource exprTableSource = (SQLExprTableSource) mySqlSelectQueryBlock.getFrom() ;
System.out.println(exprTableSource.getName().getSimpleName());
String sourceTableName = exprTableSource.getName().getSimpleName();
sourceTable1=sourceTableName;
tablsmap.put(name,sourceTableName);
if (exprTableSource.getExpr() instanceof SQLPropertyExpr){
SQLPropertyExpr sqlPropertyExpr= (SQLPropertyExpr) exprTableSource.getExpr();
tablsSchemaMap.put(sourceTableName,sqlPropertyExpr.getOwnerName()) ;
}
}else if (mySqlSelectQueryBlock.getFrom() instanceof SQLSubqueryTableSource){
SQLSubqueryTableSource tableSource = (SQLSubqueryTableSource) mySqlSelectQueryBlock.getFrom();
SQLSelect subquery1 = tableSource.getSelect();
if (subquery1.getQuery() instanceof MySqlSelectQueryBlock){
MySqlSelectQueryBlock mySqlSelectQueryBlock1=(MySqlSelectQueryBlock)subquery1.getQuery();
if (mySqlSelectQueryBlock1.getFrom() instanceof SQLExprTableSource){
SQLExprTableSource exprTableSource = (SQLExprTableSource) mySqlSelectQueryBlock1.getFrom() ;
System.out.println(exprTableSource.getName().getSimpleName());
String sourceTableName = exprTableSource.getName().getSimpleName();
sourceTable1=sourceTableName;
tablsmap.put(name,sourceTableName);
if (exprTableSource.getExpr() instanceof SQLPropertyExpr){
SQLPropertyExpr sqlPropertyExpr= (SQLPropertyExpr) exprTableSource.getExpr();
tablsSchemaMap.put(sourceTableName,sqlPropertyExpr.getOwnerName()) ;
}
}
}
}
}
}
if (statement.getUpdateClause() instanceof SQLMergeStatement.MergeUpdateClause){
SQLMergeStatement.MergeUpdateClause mergeUpdateClause=(SQLMergeStatement.MergeUpdateClause) statement.getUpdateClause();
List items= mergeUpdateClause.getItems();
if (CollectionUtils.isNotEmpty(items)){
for (SQLUpdateSetItem item:items){
if (item.getColumn() instanceof SQLPropertyExpr){
TableMapVo tableMapVo=new TableMapVo();
SQLPropertyExpr sqlPropertyExpr= (SQLPropertyExpr) item.getColumn();
System.out.println(sqlPropertyExpr.getOwnerName());
if (sqlPropertyExpr.getOwner() instanceof SQLIdentifierExpr){
SQLIdentifierExpr sqlIdentifierExpr=(SQLIdentifierExpr) sqlPropertyExpr.getOwner();
if (tablsmap.containsKey(sqlIdentifierExpr.getName().toLowerCase())){
if (tablsmap.get(sqlIdentifierExpr.getName().toLowerCase()).equalsIgnoreCase(sourceTable1)){
tableMapVo.setSourceTableName(sourceTable1);
tableMapVo.setSourceColName(sqlPropertyExpr.getName());
}
if (tablsmap.get(sqlIdentifierExpr.getName().toLowerCase()).equalsIgnoreCase(targetTable)){
tableMapVo.setTargetTableName(targetTable);
tableMapVo.setTargetColName(sqlPropertyExpr.getName());
}
}
updateSourceTableCol.put(sqlPropertyExpr.getName(),tablsSchemaMap.get(sqlIdentifierExpr.getName()));
}
if (item.getValue() instanceof SQLCharExpr){
continue;
}
if (item.getValue() instanceof SQLPropertyExpr){
SQLPropertyExpr sqlPropertyExpr1= (SQLPropertyExpr) item.getValue();
if (sqlPropertyExpr1.getOwner() instanceof SQLIdentifierExpr){
SQLIdentifierExpr sqlIdentifierExpr=(SQLIdentifierExpr) sqlPropertyExpr1.getOwner();
if (tablsmap.containsKey(sqlIdentifierExpr.getName().toLowerCase())){
if (tablsmap.get(sqlIdentifierExpr.getName().toLowerCase()).equalsIgnoreCase(sourceTable1)){
tableMapVo.setSourceTableName(sourceTable1);
tableMapVo.setSourceColName(sqlPropertyExpr.getName());
}
if (tablsmap.get(sqlIdentifierExpr.getName().toLowerCase()).equalsIgnoreCase(targetTable)){
tableMapVo.setTargetTableName(targetTable);
tableMapVo.setTargetColName(sqlPropertyExpr.getName());
}
}
updateSourceTableCol.put(sqlPropertyExpr.getName(),tablsSchemaMap.get(sqlIdentifierExpr.getName()));
}
}
if (StringUtils.isNotEmpty(tableMapVo.getSourceTableName())&&StringUtils.isNotEmpty(tableMapVo.getTargetTableName())){
tableMapVos.add(tableMapVo);
}else{
tableVo=new TableVo();
}
}
}
}
}
if (statement.getOn() instanceof SQLBinaryOpExpr){
SQLBinaryOpExpr sqlBinaryOpExpr=(SQLBinaryOpExpr)statement.getOn();
if (sqlBinaryOpExpr.getLeft() instanceof SQLPropertyExpr){
SQLPropertyExpr sqlPropertyExpr=(SQLPropertyExpr)sqlBinaryOpExpr.getLeft();
System.out.println(sqlPropertyExpr.getOwnerName()+sqlPropertyExpr.getName());
}
if (sqlBinaryOpExpr.getRight() instanceof SQLPropertyExpr){
SQLPropertyExpr sqlPropertyExpr=(SQLPropertyExpr)sqlBinaryOpExpr.getRight();
System.out.println(sqlPropertyExpr.getOwnerName()+sqlPropertyExpr.getName());
}
}
for (TableMapVo tableMapVo:tableMapVos){
if (tablsmap.containsKey(tableMapVo.getSourceTableName())){
tableMapVo.setSourceTableName(tablsmap.get(tableMapVo.getSourceTableName()));
}
if (tablsmap.containsKey(tableMapVo.getTargetTableName())){
tableMapVo.setTargetTableName(tablsmap.get(tableMapVo.getTargetTableName()));
}
System.out.println("关联表1"+tableMapVo.getSourceTableName()+"关联表字段1"+tableMapVo.getSourceColName()+"关联表2"+tableMapVo.getTargetTableName()+"关联表字段2"+tableMapVo.getTargetColName());
}
}
tableVo.setTableMapVos(tableMapVos);
tableVo.setTablsSchemaMap(tablsSchemaMap);
return tableVo;
}
/**
* 存储列的衍射关系
*/
public static void addTableColMap(Map mapInsertCols,Map mapSelectCols,List tableMapVos,String targetTable ){
if (mapInsertCols!=null&&mapSelectCols!=null){
int size= mapInsertCols.values().size();
for (int i=0;i ColifoVo colifoVo=mapSelectCols.get(i);
if (colifoVo!=null&&CollectionUtils.isNotEmpty(colifoVo.getColifoVoList())){
for (ColifoVo vo:colifoVo.getColifoVoList()){
if (vo!=null){
TableMapVo vo1=new TableMapVo(vo.getSourceTableName(),vo.getSourceColName(),mapInsertCols.get(i),targetTable) ;
tableMapVos.add(vo1);
}
}
}
}
}
}
public static void setSelectCols(Map mapSelectCols,Integer scount,ColifoVo vo ){
if (mapSelectCols.containsKey(scount)){
ColifoVo colifoVo=mapSelectCols.get(scount);
List colifoVoList=colifoVo.getColifoVoList();
colifoVoList.add(vo);
mapSelectCols.put(scount,colifoVo);
}else{
ColifoVo colifoVo=new ColifoVo();
List colifoVoList=new ArrayList<>();
colifoVoList.add(vo);
colifoVo.setColifoVoList(colifoVoList);
mapSelectCols.put(scount,colifoVo);
}
}
public static void setSelectColsNUll(Map mapSelectCols,Integer scount ){
setSelectCols(mapSelectCols,scount,null);
}
public static ColifoVo getTableAndColBySQLBinaryOpExpr(SQLBinaryOpExpr sqlBinaryOpExpr,Map tablsmap){
ColifoVo colifoVo=new ColifoVo();
if (sqlBinaryOpExpr.getLeft() instanceof SQLPropertyExpr){
SQLPropertyExpr sqlPropertyExpr=(SQLPropertyExpr)sqlBinaryOpExpr.getLeft();
colifoVo.setSourceTableName(getTableNameByAlias(sqlPropertyExpr.getOwnerName(),tablsmap));
colifoVo.setSourceColName(sqlPropertyExpr.getName());
System.out.println("表名:"+colifoVo.getSourceTableName()+",列名:"+colifoVo.getSourceColName());
}
return colifoVo;
}
public static ColifoVo getTableAndColBySQLPropertyExpr(SQLPropertyExpr sqlPropertyExpr,Map tablsmap){
ColifoVo colifoVo=new ColifoVo();
if (sqlPropertyExpr.getOwner() instanceof SQLIdentifierExpr){
SQLIdentifierExpr sqlIdentifierExpr=(SQLIdentifierExpr)sqlPropertyExpr.getOwner();
colifoVo.setSourceTableName(getTableNameByAlias(sqlIdentifierExpr.getName(),tablsmap));
colifoVo.setSourceColName(sqlPropertyExpr.getName());
System.out.println("表名:"+colifoVo.getSourceTableName()+",列名:"+colifoVo.getSourceColName());
}
return colifoVo;
}

public static String getTableNameByAlias(String alias,Map tablsmap){
if (tablsmap.containsKey(alias)){
if (tablsmap.get(alias)!=null&&!tablsmap.get(alias).isEmpty()){
alias=tablsmap.get(alias);
}
}
return alias;

}
public static ColifoVo getTableAndColPoint(String string,Map tablsmap){

String[] s1= string.trim().split(".");
String name=s1[0];
if (tablsmap.containsKey(name)){
name=tablsmap.get(name);
}
ColifoVo colifoVo=new ColifoVo(name,s1[1].toString());
return colifoVo;

}
public static ColifoVo getTableAndColByBlank(String string,Map tablsmap){
ColifoVo colifoVo=new ColifoVo();
String[] s1=string.trim().split(" ");
for (String s2:s1){
colifoVo=getTableAndColPoint(s2,tablsmap);
}
return colifoVo;

}
public static void SQLBinaryOpExpr() {
String sql = "select * from users where id > 1 and age = 18";
SQLStatement stmt1 = SQLUtils.parseSingleStatement(sql, DbType.mysql); // 将 SQL 语句解析为 SQLStatement 对象
if (stmt1 instanceof SQLSelectStatement) {
SQLSelectStatement sqlSelectStatement = (SQLSelectStatement) stmt1;
sqlSelectStatement.getSelect().getFirstQueryBlock();

SQLSelectQueryBlock selectQueryBlock =sqlSelectStatement.getSelect().getFirstQueryBlock();
SQLExpr where = selectQueryBlock.getWhere();
List conditions = where.getChildren();
// [id > 1 , age = 18] 出现了操作符所以是SQLBinaryOpExpr
for (SQLObject condition : conditions) {

/*SQLBinaryOpExpr conditionExpr =Utils.cast(condition, SQLBinaryOpExpr.class);
SQLBinaryOperator operator = conditionExpr.getOperator();
SQLIdentifierExpr conditionColumn = Utils.cast(conditionExpr.getLeft(), SQLIdentifierExpr.class);
SQLValuableExpr conditionColumnValue = Utils.cast(conditionExpr.getRight(), SQLValuableExpr.class);
Utils.print("条件字段:{},操作符号:{},条件值:{}", conditionColumn.getName(), operator.name, conditionColumnValue);*/
}
}
}
/**
* 解析查询字段,注意是否使用了别名.u.id as userId, u.name as userName, u.age as userAge

* userId(sqlSelectItem.getAlias)

* 如果有别名: u.id( id = SQLPropertyExpr.getName,u = SQLPropertyExpr.getOwnernName)

* 如果没别名: id(id = SQLIdentifierExpr.name)
*
* @param selectColumnList 查询字段
*/
private void parseSQLSelectItem(List selectColumnList) {
for (SQLSelectItem sqlSelectItem : selectColumnList) {
// u.id as userId(selectColumnAlias)
String selectColumnAlias = sqlSelectItem.getAlias();
// u.id = SQLPropertyExpr
SQLExpr expr = sqlSelectItem.getExpr();
/* if (expr instanceof SQLPropertyExpr) {
SQLPropertyExpr selectColumnExpr = cast(expr, SQLPropertyExpr.class);
print("列名:{},别名:{},表别名:{}", selectColumnExpr.getName(), selectColumnAlias, selectColumnExpr.getOwnernName());
}
if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr selectColumnExpr = cast(expr, SQLIdentifierExpr.class);
print("列名:{},别名:{}", selectColumnExpr.getName(), selectColumnAlias);
}*/
}
}
/**
*
* @param tableSource 存储来源表的列表
* @param sources 存储来源表别名的集合
* @param aliases 存储来源表和对应别名或者表名的映射关系
*/
private static void getSources(SQLTableSource tableSource, List sources, Set aliases, List tableMapVos, Map tablsmap,Map tablsSchemaMap) {
if (tableSource == null) {
return;
}
if (tableSource instanceof SQLJoinTableSource) {
SQLJoinTableSource join = (SQLJoinTableSource) tableSource;
SQLBinaryOpExpr sqlBinaryOpExpr=(SQLBinaryOpExpr)join.getCondition();
if (sqlBinaryOpExpr!=null){
SQLBinaryOperator operator = sqlBinaryOpExpr.getOperator();
if (operator!=null&&operator.name.equals("=")){
System.out.println(operator.name);
SQLPropertyExpr sqlPropertyExpr=(SQLPropertyExpr)sqlBinaryOpExpr.getLeft();
System.out.println("关联关系表1"+ getTableNameByAlias(sqlPropertyExpr.getOwnerName(),tablsmap)+"表别名"+sqlPropertyExpr.getOwnerName()+"关联字段:"+sqlPropertyExpr.getName());
SQLPropertyExpr sqlPropertyExpr2=(SQLPropertyExpr)sqlBinaryOpExpr.getRight();
System.out.println("关联关系表2"+ getTableNameByAlias(sqlPropertyExpr2.getOwnerName(),tablsmap)+"表别名"+sqlPropertyExpr2.getOwnerName()+"关联字段:"+sqlPropertyExpr2.getName());
TableMapVo vo=new TableMapVo( getTableNameByAlias(sqlPropertyExpr.getOwnerName(),tablsmap),sqlPropertyExpr.getName(),getTableNameByAlias(sqlPropertyExpr2.getOwnerName(),tablsmap),sqlPropertyExpr2.getName());
// tableMapVos.add(vo);
}
}
getSources(join.getLeft(), sources, aliases,tableMapVos,tablsmap,tablsSchemaMap);
getSources(join.getRight(), sources, aliases,tableMapVos,tablsmap,tablsSchemaMap);
} else {
if (tableSource instanceof SQLSubqueryTableSource){
SQLSubqueryTableSource subqueryTableSource=(SQLSubqueryTableSource)tableSource;
String name=null;
if (subqueryTableSource.getAlias()!=null){
name=subqueryTableSource.getAlias();
}
if (subqueryTableSource.getSelect().getQuery() instanceof MySqlSelectQueryBlock){
MySqlSelectQueryBlock mySqlSelectQueryBlock= (MySqlSelectQueryBlock)subqueryTableSource.getSelect().getQuery() ;
if ( mySqlSelectQueryBlock.getFrom() instanceof SQLSubqueryTableSource) {
List subquerySources = new ArrayList<>();
Set subqueryAliases = new HashSet<>();
getSources(mySqlSelectQueryBlock.getFrom(), subquerySources, subqueryAliases,tableMapVos,tablsmap,tablsSchemaMap);
}else if ( mySqlSelectQueryBlock.getFrom() instanceof SQLExprTableSource) {
SQLExprTableSource source=(SQLExprTableSource)mySqlSelectQueryBlock.getFrom();
if (source.getExpr() instanceof SQLPropertyExpr){
SQLPropertyExpr sqlPropertyExpr=(SQLPropertyExpr)source.getExpr();
System.out.println(sqlPropertyExpr.getName());
if (name==null){
if ( tableSource.getParent().getParent()!=null&&tableSource.getParent().getParent().getParent() instanceof SQLSubqueryTableSource){
SQLSubqueryTableSource subqueryTableSource1=(SQLSubqueryTableSource)tableSource.getParent().getParent().getParent();
if (subqueryTableSource1.getAlias()!=null){
name=subqueryTableSource1.getAlias();
}
}
}
tablsmap.put(name,sqlPropertyExpr.getName());
tablsSchemaMap.put(sqlPropertyExpr.getName(),sqlPropertyExpr.getOwnerName());
}
}
//sources.add(subqueryTableSource);
}
}
if (!sources.contains(tableSource)) {
sources.add(tableSource);
String alias = tableSource.getAlias();
if (alias != null) {
aliases.add(alias);
}
if (tablsmap.containsKey(alias)){
if (tablsmap.get(alias).toLowerCase().trim().indexOf("select")>-1){
tablsmap.put(alias,tableSource.toString());
}
}else{
tablsmap.put(alias,tableSource.toString());
}

}
}
}


}

你可能感兴趣的:(sql,java,缓存)