public SqlConverter(IConfigurer configurer, ConvMaster convMaster) throws SQLException { this.sqlNodeConverter = new SqlNodeConverter(convMaster); this.configurer = configurer; sqlWriter = new ConvSqlWriter(configurer); sqlWriter.setQuoteAllIdentifiers(false); }
@Override public void fetchOffset(SqlNode fetch, SqlNode offset) { if (fetch == null && offset == null) { return; } switch (configurer.getPagingType().toUpperCase(Locale.ROOT)) { case "ROWNUM": doWriteRowNum(fetch, offset); break; case "FETCH_NEXT": doWriteFetchNext(fetch, offset); break; case "LIMIT_OFFSET": doWriteLimitOffset(fetch, offset); break; default: if (getDialect().supportsOffsetFetch()) { doWriteFetchNext(fetch, offset); } else { doWriteLimitOffset(fetch, offset); } break; } }
private void doWriteLimitOffset(SqlNode fetch, SqlNode offset) { // Dialect does not support OFFSET/FETCH clause. // Assume it uses LIMIT/OFFSET. if (fetch != null) { this.newlineAndIndent(); final Frame fetchFrame = this.startList(FrameTypeEnum.FETCH); this.keyword("LIMIT"); fetch.unparse(this, -1, -1); this.endList(fetchFrame); } if (offset != null) { this.newlineAndIndent(); final Frame offsetFrame = this.startList(FrameTypeEnum.OFFSET); this.keyword("OFFSET"); offset.unparse(this, -1, -1); this.endList(offsetFrame); } }
@Override public void userDefinedType(SqlDataTypeSpec typeSpec, int leftPrec, int rightPrec) { keyword(typeSpec.getTypeName().getSimple()); // also print precision and scale for user-defined-type int precision = typeSpec.getPrecision(); int scale = typeSpec.getScale(); if (precision >= 0) { final SqlWriter.Frame frame = startList(SqlWriter.FrameTypeEnum.FUN_CALL, "(", ")"); this.print(precision); if (scale >= 0) { this.sep(",", true); this.print(scale); } this.endList(frame); } }
@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); }
@Override public void writeWithItem(SqlCall call, SqlWithItem.SqlWithItemOperator sqlWithItemOperator, int leftPrec, int rightPrec) { final SqlWithItem withItem = (SqlWithItem) call; leftPrec = sqlWithItemOperator.getLeftPrec(); rightPrec = sqlWithItemOperator.getRightPrec(); withItem.name.unparse(this, leftPrec, rightPrec); if (withItem.columnList != null) { withItem.columnList.unparse(this, leftPrec, rightPrec); } this.keyword("AS"); Frame frame = this.startList(FrameTypeEnum.WITH_ITEM, "(", ")"); withItem.query.unparse(this, 10, 10); this.endList(frame); } }
@Override public void identifier(String name) { String convertName = name; if (configurer.isCaseSensitive()) { convertName = configurer.fixIdentifierCaseSensitve(name); } if (configurer.enableQuote()) { String quoted = getDialect().quoteIdentifier(convertName); print(quoted); setNeedWhitespace(true); } else { if (!configurer.skipHandleDefault() && convertName.trim().equalsIgnoreCase("default")) { String quoted = getDialect().quoteIdentifier(convertName); print(quoted); setNeedWhitespace(true); } else { super.identifier(convertName); } } }
public String convertColumn(String column, String originQuote) { String converted = column.replace(originQuote, ""); try { SqlNode sqlNode = SqlParser.create(converted).parseExpression(); sqlNode = sqlNode.accept(sqlNodeConverter); converted = sqlWriter.format(sqlNode); } catch (Throwable e) { logger.error("Failed to default convert Column, will use the input: {}", column, e); } finally { sqlWriter.reset(); } return converted; }
@Override public void userDefinedType(SqlDataTypeSpec typeSpec, int leftPrec, int rightPrec) { keyword(typeSpec.getTypeName().getSimple()); // also print precision and scale for user-defined-type int precision = typeSpec.getPrecision(); int scale = typeSpec.getScale(); if (precision >= 0) { final SqlWriter.Frame frame = startList(SqlWriter.FrameTypeEnum.FUN_CALL, "(", ")"); this.print(precision); if (scale >= 0) { this.sep(",", true); this.print(scale); } this.endList(frame); } }
@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); }
@Override public void writeWithItem(SqlCall call, SqlWithItem.SqlWithItemOperator sqlWithItemOperator, int leftPrec, int rightPrec) { final SqlWithItem withItem = (SqlWithItem) call; leftPrec = sqlWithItemOperator.getLeftPrec(); rightPrec = sqlWithItemOperator.getRightPrec(); withItem.name.unparse(this, leftPrec, rightPrec); if (withItem.columnList != null) { withItem.columnList.unparse(this, leftPrec, rightPrec); } this.keyword("AS"); Frame frame = this.startList(FrameTypeEnum.WITH_ITEM, "(", ")"); withItem.query.unparse(this, 10, 10); this.endList(frame); } }
@Override public void identifier(String name) { String convertName = name; if (configurer.isCaseSensitive()) { convertName = configurer.fixIdentifierCaseSensitve(name); } if (configurer.enableQuote()) { String quoted = getDialect().quoteIdentifier(convertName); print(quoted); setNeedWhitespace(true); } else { if (!configurer.skipHandleDefault() && convertName.trim().equalsIgnoreCase("default")) { String quoted = getDialect().quoteIdentifier(convertName); print(quoted); setNeedWhitespace(true); } else { super.identifier(convertName); } } }
public String convertSql(String orig) { // for jdbc source, convert quote from backtick to double quote String converted = orig.replaceAll("`", "\""); if (!configurer.skipHandleDefault()) { String escapedDefault = SqlDialect.CALCITE .quoteIdentifier(configurer.useUppercaseDefault() ? "DEFAULT" : "default"); converted = converted.replaceAll("(?i)default\\.", escapedDefault + "."); // use Calcite dialect to cater to SqlParser converted = converted.replaceAll("\"(?i)default\"\\.", escapedDefault + "."); } if (!configurer.skipDefaultConvert()) { try { SqlNode sqlNode = SqlParser.create(converted).parseQuery(); sqlNode = sqlNode.accept(sqlNodeConverter); converted = sqlWriter.format(sqlNode); } catch (Throwable e) { logger.error("Failed to default convert sql, will use the input: {}", orig, e); } finally { sqlWriter.reset(); } } converted = configurer.fixAfterDefaultConvert(converted); return converted; }
private void doWriteFetchNext(SqlNode fetch, SqlNode offset) { if (offset == null && !configurer.allowNoOffset()) offset = SqlLiteral.createExactNumeric("0", SqlParserPos.ZERO); if (fetch != null && !configurer.allowNoOrderByWithFetch() && lastFrame != null && lastFrame.getFrameType() != FrameTypeEnum.ORDER_BY_LIST) { // MSSQL requires ORDER_BY list for FETCH clause, so must append one here. DUMMY_ORDER_BY_NODE.unparse(this, 0, 0); } if (offset != null) { this.newlineAndIndent(); final Frame offsetFrame = this.startList(FrameTypeEnum.OFFSET); this.keyword("OFFSET"); offset.unparse(this, -1, -1); this.keyword("ROWS"); this.endList(offsetFrame); } if (fetch != null) { if (!configurer.allowFetchNoRows() && fetch instanceof SqlNumericLiteral) if (((SqlNumericLiteral) fetch).toValue().equals("0")) fetch = SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO); this.newlineAndIndent(); final Frame fetchFrame = this.startList(FrameTypeEnum.FETCH); this.keyword("FETCH"); this.keyword("NEXT"); fetch.unparse(this, -1, -1); this.keyword("ROWS"); this.keyword("ONLY"); this.endList(fetchFrame); } }
@Override public void fetchOffset(SqlNode fetch, SqlNode offset) { if (fetch == null && offset == null) { return; } switch (configurer.getPagingType().toUpperCase(Locale.ROOT)) { case "ROWNUM": doWriteRowNum(fetch, offset); break; case "FETCH_NEXT": doWriteFetchNext(fetch, offset); break; case "LIMIT_OFFSET": doWriteLimitOffset(fetch, offset); break; default: if (getDialect().supportsOffsetFetch()) { doWriteFetchNext(fetch, offset); } else { doWriteLimitOffset(fetch, offset); } break; } }
public String convertColumn(String column, String originQuote) { String converted = column.replace(originQuote, ""); try { SqlNode sqlNode = SqlParser.create(converted).parseExpression(); sqlNode = sqlNode.accept(sqlNodeConverter); converted = sqlWriter.format(sqlNode); } catch (Throwable e) { logger.error("Failed to default convert Column, will use the input: {}", column, e); } finally { sqlWriter.reset(); } return converted; }
public SqlConverter(IConfigurer configurer, ConvMaster convMaster) throws SQLException { this.sqlNodeConverter = new SqlNodeConverter(convMaster); this.configurer = configurer; sqlWriter = new ConvSqlWriter(configurer); sqlWriter.setQuoteAllIdentifiers(false); }
private void doWriteLimitOffset(SqlNode fetch, SqlNode offset) { // Dialect does not support OFFSET/FETCH clause. // Assume it uses LIMIT/OFFSET. if (fetch != null) { this.newlineAndIndent(); final Frame fetchFrame = this.startList(FrameTypeEnum.FETCH); this.keyword("LIMIT"); fetch.unparse(this, -1, -1); this.endList(fetchFrame); } if (offset != null) { this.newlineAndIndent(); final Frame offsetFrame = this.startList(FrameTypeEnum.OFFSET); this.keyword("OFFSET"); offset.unparse(this, -1, -1); this.endList(offsetFrame); } }
public String convertSql(String orig) { // for jdbc source, convert quote from backtick to double quote String converted = orig.replaceAll("`", "\""); if (!configurer.skipHandleDefault()) { String escapedDefault = SqlDialect.CALCITE .quoteIdentifier(configurer.useUppercaseDefault() ? "DEFAULT" : "default"); converted = converted.replaceAll("(?i)default\\.", escapedDefault + "."); // use Calcite dialect to cater to SqlParser converted = converted.replaceAll("\"(?i)default\"\\.", escapedDefault + "."); } if (!configurer.skipDefaultConvert()) { try { SqlNode sqlNode = SqlParser.create(converted).parseQuery(); sqlNode = sqlNode.accept(sqlNodeConverter); converted = sqlWriter.format(sqlNode); } catch (Throwable e) { logger.error("Failed to default convert sql, will use the input: {}", orig, e); } finally { sqlWriter.reset(); } } converted = configurer.fixAfterDefaultConvert(converted); return converted; }
private void doWriteFetchNext(SqlNode fetch, SqlNode offset) { if (offset == null && !configurer.allowNoOffset()) offset = SqlLiteral.createExactNumeric("0", SqlParserPos.ZERO); if (fetch != null && !configurer.allowNoOrderByWithFetch() && lastFrame != null && lastFrame.getFrameType() != FrameTypeEnum.ORDER_BY_LIST) { // MSSQL requires ORDER_BY list for FETCH clause, so must append one here. DUMMY_ORDER_BY_NODE.unparse(this, 0, 0); } if (offset != null) { this.newlineAndIndent(); final Frame offsetFrame = this.startList(FrameTypeEnum.OFFSET); this.keyword("OFFSET"); offset.unparse(this, -1, -1); this.keyword("ROWS"); this.endList(offsetFrame); } if (fetch != null) { if (!configurer.allowFetchNoRows() && fetch instanceof SqlNumericLiteral) if (((SqlNumericLiteral) fetch).toValue().equals("0")) fetch = SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO); this.newlineAndIndent(); final Frame fetchFrame = this.startList(FrameTypeEnum.FETCH); this.keyword("FETCH"); this.keyword("NEXT"); fetch.unparse(this, -1, -1); this.keyword("ROWS"); this.keyword("ONLY"); this.endList(fetchFrame); } }