Skip to content

Commit

Permalink
feat: ydb jooq is supported view index statement (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillKurdyukov authored Sep 5, 2024
1 parent fee759a commit fcaf26e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 45 deletions.
5 changes: 5 additions & 0 deletions jooq-dialect/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 1.0.0 ##

- `REPLACE` / `UPSERT` builders from YDB
- Supported VIEW INDEX from `useIndex("index_name")` HintedTable
- Generated tables from schema
2 changes: 1 addition & 1 deletion jooq-dialect/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>tech.ydb.dialects</groupId>
<artifactId>jooq-ydb-dialect</artifactId>
<version>1.0.0-RC1</version>
<version>1.0.0</version>

<name>YDB JOOQ Dialect module</name>
<description>YDB JOOQ Dialect module</description>
Expand Down
76 changes: 76 additions & 0 deletions jooq-dialect/src/main/java/org/jooq/impl/YdbListener.java
Original file line number Diff line number Diff line change
@@ -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<Name> arguments = (QueryPartList<Name>)fieldArguments.get(hintedTable);
renderContext.visit(arguments);
}
}
}
}

41 changes: 0 additions & 41 deletions jooq-dialect/src/main/java/tech/ydb/jooq/CustomQuoteListener.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit fcaf26e

Please sign in to comment.