Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ydb jooq is supported view index statement #154

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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