From fcaf26ef1f7463d1d930a38e636f416bab8a5d38 Mon Sep 17 00:00:00 2001 From: Kirill Kurdyukov Date: Thu, 5 Sep 2024 18:24:21 +0300 Subject: [PATCH] feat: ydb jooq is supported view index statement (#154) --- jooq-dialect/CHANGELOG.md | 5 ++ jooq-dialect/pom.xml | 2 +- .../main/java/org/jooq/impl/YdbListener.java | 76 +++++++++++++++++++ .../tech/ydb/jooq/CustomQuoteListener.java | 41 ---------- .../tech/ydb/jooq/impl/YdbDSLContextImpl.java | 6 +- 5 files changed, 85 insertions(+), 45 deletions(-) create mode 100644 jooq-dialect/CHANGELOG.md create mode 100644 jooq-dialect/src/main/java/org/jooq/impl/YdbListener.java delete mode 100644 jooq-dialect/src/main/java/tech/ydb/jooq/CustomQuoteListener.java diff --git a/jooq-dialect/CHANGELOG.md b/jooq-dialect/CHANGELOG.md new file mode 100644 index 0000000..efc0c6d --- /dev/null +++ b/jooq-dialect/CHANGELOG.md @@ -0,0 +1,5 @@ +## 1.0.0 ## + +- `REPLACE` / `UPSERT` builders from YDB +- Supported VIEW INDEX from `useIndex("index_name")` HintedTable +- Generated tables from schema diff --git a/jooq-dialect/pom.xml b/jooq-dialect/pom.xml index 9b6403f..f297bb0 100644 --- a/jooq-dialect/pom.xml +++ b/jooq-dialect/pom.xml @@ -4,7 +4,7 @@ tech.ydb.dialects jooq-ydb-dialect - 1.0.0-RC1 + 1.0.0 YDB JOOQ Dialect module YDB JOOQ Dialect module diff --git a/jooq-dialect/src/main/java/org/jooq/impl/YdbListener.java b/jooq-dialect/src/main/java/org/jooq/impl/YdbListener.java new file mode 100644 index 0000000..4b0cf36 --- /dev/null +++ b/jooq-dialect/src/main/java/org/jooq/impl/YdbListener.java @@ -0,0 +1,76 @@ +package org.jooq.impl; + +import org.jooq.Name; +import org.jooq.QueryPart; +import org.jooq.RenderContext; +import org.jooq.VisitContext; +import org.jooq.VisitListener; + +/** + * @author Kirill Kurdyukov + */ +public class YdbListener implements VisitListener { + + private final String quote; + + private volatile int hintedTableStartSize; + + public YdbListener(String quote) { + this.quote = quote; + } + + @Override + public void visitStart(VisitContext context) { + addQuoteForName(context); + visitStartHint(context); + } + + @Override + public void visitEnd(VisitContext context) { + addQuoteForName(context); + try { + visitEndHint(context); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + private void addQuoteForName(VisitContext context) { + QueryPart part = context.queryPart(); + if (part instanceof Name) { + RenderContext renderContext = context.renderContext(); + if (renderContext != null) { + renderContext.sql(quote); + } + } + } + + private void visitStartHint(VisitContext context) { + QueryPart part = context.queryPart(); + + if (part instanceof QOM.HintedTable hintedTable) { + if (context.renderContext() instanceof DefaultRenderContext renderContext) { + hintedTableStartSize = renderContext.sql.length(); + } + } + } + + private void visitEndHint(VisitContext context) throws NoSuchFieldException, IllegalAccessException { + QueryPart part = context.queryPart(); + + if (part instanceof HintedTable hintedTable) { + if (context.renderContext() instanceof DefaultRenderContext renderContext) { + renderContext.sql.setLength(hintedTableStartSize); + + renderContext.sql(" view "); + + // Sorry, Lukas!!! :( + java.lang.reflect.Field fieldArguments = hintedTable.getClass().getDeclaredField("arguments"); + fieldArguments.setAccessible(true); + QueryPartList arguments = (QueryPartList)fieldArguments.get(hintedTable); + renderContext.visit(arguments); + } + } + } +} + diff --git a/jooq-dialect/src/main/java/tech/ydb/jooq/CustomQuoteListener.java b/jooq-dialect/src/main/java/tech/ydb/jooq/CustomQuoteListener.java deleted file mode 100644 index 775c391..0000000 --- a/jooq-dialect/src/main/java/tech/ydb/jooq/CustomQuoteListener.java +++ /dev/null @@ -1,41 +0,0 @@ -package tech.ydb.jooq; - -import org.jooq.Name; -import org.jooq.QueryPart; -import org.jooq.RenderContext; -import org.jooq.VisitContext; -import org.jooq.VisitListener; - -public class CustomQuoteListener implements VisitListener { - - private final String quote; - - public CustomQuoteListener(String quote) { - this.quote = quote; - } - - public CustomQuoteListener(char quote) { - this.quote = String.valueOf(quote); - } - - @Override - public void visitStart(VisitContext context) { - addQuoteForName(context); - } - - @Override - public void visitEnd(VisitContext context) { - addQuoteForName(context); - } - - private void addQuoteForName(VisitContext context) { - QueryPart part = context.queryPart(); - if (part instanceof Name) { - RenderContext renderContext = context.renderContext(); - if (renderContext != null) { - renderContext.sql(quote); - } - } - } -} - diff --git a/jooq-dialect/src/main/java/tech/ydb/jooq/impl/YdbDSLContextImpl.java b/jooq-dialect/src/main/java/tech/ydb/jooq/impl/YdbDSLContextImpl.java index 21e3311..b46a45e 100644 --- a/jooq-dialect/src/main/java/tech/ydb/jooq/impl/YdbDSLContextImpl.java +++ b/jooq-dialect/src/main/java/tech/ydb/jooq/impl/YdbDSLContextImpl.java @@ -68,7 +68,7 @@ public YdbDSLContextImpl(Configuration configuration) { super(configuration .deriveSettings(YdbDSLContextImpl::addRequiredParameters) .set(YDB.DIALECT) - .set(quoteListener())); + .set(ydbListener())); } private static Settings addRequiredParameters(Settings settings) { @@ -77,8 +77,8 @@ private static Settings addRequiredParameters(Settings settings) { .withRenderSchema(false); } - private static VisitListener quoteListener() { - return new CustomQuoteListener("`"); + private static VisitListener ydbListener() { + return new YdbListener("`"); } @Override