Skip to content

Commit 7ee06bf

Browse files
committed
add a dialect-deparser module and use it in guard
Signed-off-by: Stefan Bischof <[email protected]>
1 parent f160015 commit 7ee06bf

File tree

23 files changed

+1129
-130
lines changed

23 files changed

+1129
-130
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ insert_final_newline = unset
4545
trim_trailing_whitespace = unset
4646
indent_style = unset
4747
indent_size = unset
48+
49+
[.flattened-pom.xml]
50+
insert_final_newline = false

deparser/api/pom.xml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/*********************************************************************
4+
* Copyright (c) 2025 Contributors to the Eclipse Foundation.
5+
*
6+
* This program and the accompanying materials are made
7+
* available under the terms of the Eclipse Public License 2.0
8+
* which is available at https://www.eclipse.org/legal/epl-2.0/
9+
*
10+
* SPDX-License-Identifier: EPL-2.0
11+
**********************************************************************/
12+
-->
13+
<project xmlns="http://maven.apache.org/POM/4.0.0"
14+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
15+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
16+
<modelVersion>4.0.0</modelVersion>
17+
<parent>
18+
<groupId>org.eclipse.daanse</groupId>
19+
<artifactId>org.eclipse.daanse.sql.deparser</artifactId>
20+
<version>${revision}</version>
21+
</parent>
22+
<artifactId>org.eclipse.daanse.sql.deparser.api</artifactId>
23+
<name>Daanse SQL Deparser API</name>
24+
<description>API interfaces for SQL deparser factory and dialect-aware SQL
25+
generation.</description>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.eclipse.daanse</groupId>
30+
<artifactId>org.eclipse.daanse.jdbc.db.dialect.api</artifactId>
31+
<version>0.0.1-SNAPSHOT</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.github.jsqlparser</groupId>
35+
<artifactId>jsqlparser</artifactId>
36+
<version>5.4-SNAPSHOT</version>
37+
</dependency>
38+
</dependencies>
39+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* SmartCity Jena - initial
12+
* Stefan Bischof (bipolis.org) - initial
13+
*/
14+
package org.eclipse.daanse.sql.deparser.api;
15+
16+
import org.eclipse.daanse.jdbc.db.dialect.api.Dialect;
17+
18+
/**
19+
* Dialect-aware SQL deparsers.
20+
*/
21+
public interface DialectDeparser {
22+
23+
/**
24+
* Deparses a SQL statement using the given dialect.
25+
*
26+
* @param statement the JSqlParser statement to deparse
27+
* @param dialect the database dialect to use for SQL generation
28+
* @return the generated SQL string
29+
*/
30+
String deparse(net.sf.jsqlparser.statement.Statement statement, Dialect dialect);
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* SmartCity Jena - initial
12+
* Stefan Bischof (bipolis.org) - initial
13+
*/
14+
15+
/**
16+
* API interfaces for SQL deparser factory and dialect-aware SQL generation.
17+
* <p>
18+
* This package provides the factory interface for creating dialect-aware SQL deparsers
19+
* that generate proper SQL syntax according to the target database dialect.
20+
*/
21+
@org.osgi.annotation.bundle.Export
22+
@org.osgi.annotation.versioning.Version("0.0.1")
23+
package org.eclipse.daanse.sql.deparser.api;

deparser/jsqlparser/pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/*********************************************************************
4+
* Copyright (c) 2025 Contributors to the Eclipse Foundation.
5+
*
6+
* This program and the accompanying materials are made
7+
* available under the terms of the Eclipse Public License 2.0
8+
* which is available at https://www.eclipse.org/legal/epl-2.0/
9+
*
10+
* SPDX-License-Identifier: EPL-2.0
11+
**********************************************************************/
12+
-->
13+
<project xmlns="http://maven.apache.org/POM/4.0.0"
14+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
15+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
16+
<modelVersion>4.0.0</modelVersion>
17+
<parent>
18+
<groupId>org.eclipse.daanse</groupId>
19+
<artifactId>org.eclipse.daanse.sql.deparser</artifactId>
20+
<version>${revision}</version>
21+
</parent>
22+
<artifactId>org.eclipse.daanse.sql.deparser.jsqlparser</artifactId>
23+
<name>Daanse SQL Deparser JSqlParser Implementation</name>
24+
<description>JSqlParser-based implementation of SQL deparser with
25+
comprehensive dialect support.</description>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.eclipse.daanse</groupId>
30+
<artifactId>org.eclipse.daanse.sql.deparser.api</artifactId>
31+
<version>${project.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.eclipse.daanse</groupId>
35+
<artifactId>org.eclipse.daanse.jdbc.db.dialect.api</artifactId>
36+
<version>0.0.1-SNAPSHOT</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.eclipse.daanse</groupId>
40+
<artifactId>org.eclipse.daanse.jdbc.db.dialect.db.common</artifactId>
41+
<version>0.0.1-SNAPSHOT</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.github.jsqlparser</groupId>
45+
<artifactId>jsqlparser</artifactId>
46+
<version>5.4-SNAPSHOT</version>
47+
</dependency>
48+
</dependencies>
49+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* SmartCity Jena - initial
12+
* Stefan Bischof (bipolis.org) - initial
13+
*/
14+
package org.eclipse.daanse.sql.deparser.jsqlparser;
15+
16+
import org.eclipse.daanse.jdbc.db.dialect.api.Dialect;
17+
import org.eclipse.daanse.sql.deparser.api.DialectDeparser;
18+
import org.osgi.service.component.annotations.Component;
19+
import org.osgi.service.component.annotations.ServiceScope;
20+
21+
import net.sf.jsqlparser.statement.Statement;
22+
import net.sf.jsqlparser.util.deparser.StatementDeParser;
23+
24+
/**
25+
* Factory implementation for creating dialect-aware SQL deparsers.
26+
*/
27+
@Component(service = DialectDeparser.class,scope = ServiceScope.SINGLETON)
28+
public class BasicDialectDeparser implements DialectDeparser {
29+
30+
@Override
31+
public String deparse(Statement statement, Dialect dialect) {
32+
StringBuilder buffer = new StringBuilder();
33+
StatementDeParser deparser = new BasicDialectStatementDeParser(buffer, dialect);
34+
statement.accept(deparser);
35+
return buffer.toString();
36+
}
37+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* SmartCity Jena - initial
12+
* Stefan Bischof (bipolis.org) - initial
13+
*/
14+
package org.eclipse.daanse.sql.deparser.jsqlparser;
15+
16+
import org.eclipse.daanse.jdbc.db.dialect.api.Dialect;
17+
18+
import net.sf.jsqlparser.expression.DateValue;
19+
import net.sf.jsqlparser.expression.DoubleValue;
20+
import net.sf.jsqlparser.expression.LongValue;
21+
import net.sf.jsqlparser.expression.StringValue;
22+
import net.sf.jsqlparser.expression.TimeValue;
23+
import net.sf.jsqlparser.expression.TimestampValue;
24+
import net.sf.jsqlparser.schema.Column;
25+
import net.sf.jsqlparser.schema.Table;
26+
import net.sf.jsqlparser.statement.select.SelectVisitor;
27+
import net.sf.jsqlparser.util.deparser.ExpressionDeParser;
28+
29+
public class BasicDialectExpressionDeParser extends ExpressionDeParser {
30+
31+
private final Dialect dialect;
32+
33+
public BasicDialectExpressionDeParser(Dialect dialect) {
34+
super();
35+
this.builder = new StringBuilder();
36+
this.dialect = dialect;
37+
}
38+
39+
public BasicDialectExpressionDeParser(SelectVisitor<StringBuilder> selectVisitor, StringBuilder buffer,
40+
Dialect dialect) {
41+
super(selectVisitor, buffer);
42+
this.dialect = dialect;
43+
}
44+
45+
// Identifier Quoting
46+
47+
@Override
48+
public <S> StringBuilder visit(Column tableColumn, S context) {
49+
final Table table = tableColumn.getTable();
50+
String tableName = null;
51+
52+
if (table != null) {
53+
if (table.getAlias() != null) {
54+
tableName = table.getAlias().getName();
55+
} else {
56+
tableName = table.getFullyQualifiedName();
57+
}
58+
}
59+
60+
if (tableName != null && !tableName.isEmpty()) {
61+
dialect.quoteIdentifier(builder, tableName, tableColumn.getColumnName());
62+
} else {
63+
dialect.quoteIdentifier(builder, tableColumn.getColumnName());
64+
}
65+
66+
if (tableColumn.getArrayConstructor() != null) {
67+
tableColumn.getArrayConstructor().accept(this, context);
68+
}
69+
70+
if (tableColumn.getCommentText() != null) {
71+
builder.append(" /* ").append(tableColumn.getCommentText()).append(" */");
72+
}
73+
74+
return builder;
75+
}
76+
77+
// String Literal Quoting
78+
79+
@Override
80+
public <S> StringBuilder visit(StringValue stringValue, S context) {
81+
if (stringValue.getPrefix() != null) {
82+
builder.append(stringValue.getPrefix());
83+
}
84+
dialect.quoteStringLiteral(builder, stringValue.getValue());
85+
return builder;
86+
}
87+
88+
// Date/Time/Timestamp Literal Quoting
89+
90+
@Override
91+
public <S> StringBuilder visit(DateValue dateValue, S context) {
92+
dialect.quoteDateLiteral(builder, dateValue.getValue().toString());
93+
return builder;
94+
}
95+
96+
@Override
97+
public <S> StringBuilder visit(TimeValue timeValue, S context) {
98+
dialect.quoteTimeLiteral(builder, timeValue.getValue().toString());
99+
return builder;
100+
}
101+
102+
@Override
103+
public <S> StringBuilder visit(TimestampValue timestampValue, S context) {
104+
dialect.quoteTimestampLiteral(builder, timestampValue.getValue().toString());
105+
return builder;
106+
}
107+
108+
// Numeric Literal Formatting
109+
110+
@Override
111+
public <S> StringBuilder visit(LongValue longValue, S context) {
112+
dialect.quoteNumericLiteral(builder, longValue.getStringValue());
113+
return builder;
114+
}
115+
116+
@Override
117+
public <S> StringBuilder visit(DoubleValue doubleValue, S context) {
118+
String valueString = doubleValue.toString();
119+
if (dialect.needsExponent(doubleValue.getValue(), valueString)) {
120+
valueString += "E0";
121+
}
122+
dialect.quoteNumericLiteral(builder, valueString);
123+
return builder;
124+
}
125+
}

0 commit comments

Comments
 (0)