package com.metasoft.framework.si.eai.db; import java.util.HashSet; import java.util.Iterator; import com.metasoft.framework.si.eai.parse.FieldMapping; import com.metasoft.framework.si.eai.parse.TableMapping; import com.metasoft.framework.si.eai.parse.TableMappingHelper; /** * * @author zr.peng * @deprecated * @since 2008-4-14 */ public class MetaDataToU8Helper { /** * 把metasql中的SQL替换成U8系统中的SQl * @param sql * Meta中的处理SQL 语句 * @param map * 表间映射关系对象 * @return */ public static String transformMetaSqlToU8Sql(String sql, TableMapping map) { String sqlTrans; sqlTrans = replace(sql, map.getFrom_table(), map.getTo_table()); HashSet fieldMappings = map.getFieldMapping(); Iterator it = fieldMappings.iterator(); while (it.hasNext()) { FieldMapping fieldMapping = (FieldMapping) it.next(); String from_field = fieldMapping.getFrom_field(); String to_field = fieldMapping.getTo_field(); sqlTrans = replace(sqlTrans, from_field, to_field); } return sqlTrans; } /** * 替换 * @param sql 要处理的字符串 * @param from_field 被替换的字符 * @param to_field 替换的字符 * @return */ private static String replace(String sql, String from_field, String to_field) { String sqlTrans = sql; int sqlLen = sqlTrans.length(); int from_fieldLen = from_field.length(); int top = sqlLen - from_fieldLen; for (int i = 0; i < top;) { /*if (sqlTrans.substring(i, i + 6).equalsIgnoreCase("values")) { break; }*/ String temp = sqlTrans.substring(i, from_fieldLen + i); if (temp.equals(from_field)) { if (leftAndRightAllOK(sqlTrans, temp,i)) { String left = sqlTrans.substring(0, i); String right = sqlTrans.substring(i + from_fieldLen); sqlTrans = left.concat(to_field).concat(right); top = sqlTrans.length() - from_fieldLen; i = i + to_field.length(); } else { i++; } } else { i++; } } return sqlTrans; } /** * 判断指定sql字符串 中subStr 子串前后是否是合适的字符,用以判断他是否是一个独立的串 * @param sql * @param subStr * @param subFirstIndex * 子串subStr第一个字符在 字串串sql中的位置 * @return */ private static boolean leftAndRightAllOK(String sql, String subStr,int subFirstIndex) { boolean leftAndRightOK = false; char leftChar = sql.charAt(subFirstIndex-1); String left = Character.toString(leftChar); char rightChar = sql.charAt(subFirstIndex+subStr.length()); String right = Character.toString(rightChar); if (left.matches("[^a-zA-Z]") && right.matches("[^a-zA-Z]")) { leftAndRightOK = true; } return leftAndRightOK; } //单元测试 public static void main(String[] args) { TableMapping map = (TableMapping) TableMappingHelper.getTablMapping( "purchaseorder_SO_SOMain", "src/com/metasoft/framework/si/eai/t2txml/purchaseorderdetail_SO_SODetails_table.xml", "src/com/metasoft/framework/si/eai/t2txml/purchaseorderdetail_SO_SODetails_field.xml"); String insrtSql = "insert into purchaseorder (id,created,sname,updatedby," + "emaudit,updated,jfgl,scode,iddep,createdby,idowner,ckzh) " + "values('402880d8193bfb4a01193bfeef400328','2008-04-11 13:39:52.734','bbbbbbbb'," + "'402881e618ffd8d80119006f110d1983','402880900bb2d155010bb2fcd9ec04ec'," + "'2008-04-11 13:39:52.734','402881d8192d101201192d6fba267dcc','bb'," + "'402881e618ffd8d80119002f148b0ae5','402881e618ffd8d80119006f110d1983'," + "'402881e618ffd8d80119006f110d1983','402881d8192dbda401192e3f1b814403');"; String sqlTrans = MetaDataToU8Helper.transformMetaSqlToU8Sql(insrtSql, map); System.out.println(insrtSql); System.out.println(sqlTrans); String selctSql="select purchaseorder.id purchaseorder_id , " + " purchaseorder.scode purchaseorder_scode, " + " purchaseorder.sname purchaseorder_sname, " + " purchaseorder.chmc purchaseorder_chmc, " + " purchaseorder.idowner purchaseorder_idowner," + " idowner_ref.sname purchaseorder_idowner_ref, " + " purchaseorder.dbtotalmoney purchaseorder_dbtotalmoney, " + " purchaseorder.insurance purchaseorder_insurance, " + " purchaseorder.total purchaseorder_total, " + " purchaseorder.zjf purchaseorder_zjf, purchaseorder.iddep " + " purchaseorder_iddep, iddep_ref.sname purchaseorder_iddep_ref" + " from purchaseorder purchaseorder "; String sqlTrans2 = MetaDataToU8Helper.transformMetaSqlToU8Sql(selctSql, map); System.out.println(selctSql); System.out.println(sqlTrans2); } }