public String convertSql(String originSql, String sourceDialect, String targetDialect) throws SQLException { SqlConverter sqlConverter = getSqlConverter(sourceDialect, targetDialect); return sqlConverter.convertSql(originSql); }
public SqlConverter(IConfigurer configurer, ConvMaster convMaster) throws SQLException { this.sqlNodeConverter = new SqlNodeConverter(convMaster); this.configurer = configurer; sqlWriter = new ConvSqlWriter(configurer); sqlWriter.setQuoteAllIdentifiers(false); }
ConvSqlWriter(SqlConverter.IConfigurer configurer) throws SQLException { super(configurer.getSqlDialect()); this.configurer = configurer; }
private SqlConverter createSqlConverter(String sourceDialect, String targetDialect) throws SQLException { DataSourceDefProvider provider = DataSourceDefProvider.getInstance(); DataSourceDef sourceDs = provider.getById(sourceDialect); final DataSourceDef targetDs = provider.getById(targetDialect); ConvMaster convMaster = new ConvMaster(sourceDs, targetDs); SqlConverter.IConfigurer configurer = new DefaultConfiguer(targetDs); return new SqlConverter(configurer, convMaster); } }
private SqlNode convertSqlCall(SqlCall sqlCall) { SqlOperator operator = sqlCall.getOperator(); if (operator != null) { Pair<SqlNode, SqlNode> matched = convMaster.matchSqlFunc(sqlCall); if (matched != null) { Preconditions.checkState(matched.getFirst() instanceof SqlCall); SqlCall sourceTmpl = (SqlCall) matched.getFirst(); Preconditions.checkState(sourceTmpl.operandCount() == sqlCall.operandCount()); SqlNode targetTmpl = matched.getSecond(); boolean isWindowCall = sourceTmpl.getOperator() instanceof SqlOverOperator; SqlParamsFinder sqlParamsFinder = SqlParamsFinder.newInstance(sourceTmpl, sqlCall, isWindowCall); return targetTmpl.accept(new SqlFuncFiller(sqlParamsFinder.getParamNodes(), isWindowCall)); } } return null; }
@Override public void writeWith(SqlCall call, int leftPrec, int rightPrec) { final SqlWith with = (SqlWith) call; final SqlWriter.Frame frame = this.startList(SqlWriter.FrameTypeEnum.WITH, "WITH", ""); for (SqlNode node : with.withList) { this.sep(","); node.unparse(this, 0, 0); } with.body.unparse(this, 100, 100); this.endList(frame); }
/** * Used for create flatten table, sql pushdown. * * @param orig * @return the converted sql statement according to data source dialect */ public String convertSql(String orig) { return sqlConverter.convertSql(orig); }
public String convertColumn(String column, String originQuote) { return sqlConverter.convertColumn(column, originQuote); }
public static SqlParamsFinder newInstance(SqlCall sourceTmpl, final SqlCall sqlCall, boolean isWindowCall) { if (!isWindowCall) { return new SqlParamsFinder(sourceTmpl, sqlCall); } else { return new SqlParamsFinder(sourceTmpl, sqlCall) { @Override public Map<Integer, SqlNode> getParamNodes() { Map<Integer, SqlNode> sqlNodes = new HashMap<>(); List<SqlNode> sqlNodeList = sqlCall.getOperandList(); SqlNode firstParam = ((SqlCall) sqlNodeList.get(0)).getOperandList().get(0); SqlNode secondParam = (SqlCall) sqlNodeList.get(1); sqlNodes.put(0, firstParam); sqlNodes.put(1, secondParam); return sqlNodes; } }; } } }
public boolean isCaseSensitive() { return configurer.isCaseSensitive(); } }
private SqlDataTypeSpec convertSqlDataTypeSpec(SqlDataTypeSpec typeSpec) { return convMaster.findTargetSqlDataTypeSpec(typeSpec); }
private SqlConverter getSqlConverter(String sourceDialect, String targetDialect) throws SQLException { String cacheKey = sourceDialect + "_" + targetDialect; SqlConverter sqlConverter = sqlConverterCache.getIfPresent(cacheKey); if (sqlConverter == null) { sqlConverter = createSqlConverter(sourceDialect, targetDialect); sqlConverterCache.put(cacheKey, sqlConverter); } return sqlConverter; }
@Override public SqlNode visit(SqlCall call) { SqlNode target = convertSqlCall(call); return target == null ? super.visit(call) : target; }
@Override public SqlNode visit(SqlDataTypeSpec type) { SqlDataTypeSpec target = convertSqlDataTypeSpec(type); return target == null ? super.visit(type) : target; }
@Override public SqlNode visit(SqlIdentifier id) { SqlNode target = convertSqlIdentifier(id); return target == null ? super.visit(id) : target; }
@Override public String fixIdentifierCaseSensitve(String orig) { if (this.adaptor == null || !isCaseSensitive()) { return orig; } return adaptor.fixIdentifierCaseSensitve(orig); }
private void testSqlConvert(String originSql, String sourceDialect, String targetDialect, GenericSqlConverter sqlConverter) throws SQLException { String convertedSql = sqlConverter.convertSql(originSql, sourceDialect, targetDialect); String revertSql = sqlConverter.convertSql(convertedSql, targetDialect, sourceDialect); Assert.assertEquals(originSql, revertSql); } }
@AfterClass public static void after() throws Exception { staticCleanupTestMetadata(); }
@BeforeClass public static void setUp() throws Exception { staticCreateTestMetadata(); }
public JdbcConnector(AbstractJdbcAdaptor adaptor) throws SQLException { this.adaptor = adaptor; DataSourceDefProvider provider = DataSourceDefProvider.getInstance(); jdbcDs = provider.getById(this.adaptor.getDataSourceId()); DataSourceDef kylinDs = provider.getDefault(); convMaster = new ConvMaster(kylinDs, jdbcDs); SqlConverter.IConfigurer configurer = new DefaultConfiguer(this.adaptor, jdbcDs); this.sqlConverter = new SqlConverter(configurer, convMaster); }