Skip to content

Commit

Permalink
Merge branch 'devel' into CB-6088-fix-uppercases-in-localization
Browse files Browse the repository at this point in the history
  • Loading branch information
devnaumov authored Dec 30, 2024
2 parents f8abdbc + c2f0d94 commit a0c456c
Show file tree
Hide file tree
Showing 18 changed files with 605 additions and 319 deletions.
29 changes: 24 additions & 5 deletions plugins/org.jkiss.dbeaver.model.lsm/grammar/SQLStandardParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,24 @@ options {
*/
package org.jkiss.dbeaver.model.lsm.sql.impl.syntax;
import java.util.*;
import org.jkiss.dbeaver.model.lsm.*;
}

@parser::members {
private final Set<Integer> nonCorrelationNameTokens = Set.of(
SQLStandardLexer.CROSS,
SQLStandardLexer.JOIN,
SQLStandardLexer.INNER,
SQLStandardLexer.OUTER,
SQLStandardLexer.UNION,
SQLStandardLexer.LEFT,
SQLStandardLexer.RIGHT,
SQLStandardLexer.FULL,
SQLStandardLexer.NATURAL
);
private boolean isAnonymousParametersEnabled;
private boolean isNamedParametersEnabled;
Expand All @@ -53,6 +67,11 @@ options {
this.isAnonymousParametersEnabled = parameters.isAnonymousSqlParametersEnabled();
this.isNamedParametersEnabled = parameters.isSqlParametersEnabled();
}

private boolean validCorrelationNameFollows() {
int nextToken = this.getInputStream().LA(1);
return nextToken == SQLStandardLexer.AS || !this.nonCorrelationNameTokens.contains(nextToken);
};
}

// root rule for script
Expand Down Expand Up @@ -120,7 +139,7 @@ referencesSpecification: REFERENCES referencedTableAndColumns (MATCH matchType)?
referencedTableAndColumns: tableName (LeftParen referenceColumnList RightParen)?;
tableName: qualifiedName;
referenceColumnList: columnNameList;
columnNameList: columnName (Comma columnName)*;
columnNameList: (columnName|anyUnexpected??) (Comma (columnName|anyUnexpected??))* Comma*;
matchType: FULL|PARTIAL;
referentialTriggeredAction: updateRule deleteRule? | deleteRule updateRule?;
updateRule: ON UPDATE referentialAction;
Expand Down Expand Up @@ -204,18 +223,18 @@ queryExpression: (joinedTable|nonJoinQueryTerm) (unionTerm|exceptTerm)*;

// from
fromClause: FROM tableReference (Comma tableReference)*;
nonjoinedTableReference: ((tableName (PARTITION anyProperty)?)|derivedTable) correlationSpecification??;
nonjoinedTableReference: ((tableName (PARTITION anyProperty)?)|derivedTable) correlationSpecification?;
tableReference: nonjoinedTableReference|joinedTable|tableReferenceHints|anyUnexpected??; // '.*' to handle incomplete queries
tableReferenceHints: (tableHintKeywords|anyWord)+ anyProperty; // dialect-specific options, should be described and moved to dialects in future
joinedTable: (nonjoinedTableReference|(LeftParen joinedTable RightParen)) (naturalJoinTerm|crossJoinTerm)+;
correlationSpecification: (AS)? correlationName (LeftParen derivedColumnList RightParen)?;
correlationSpecification: { validCorrelationNameFollows() }? (AS)? correlationName (LeftParen derivedColumnList RightParen)?;
derivedColumnList: columnNameList;
derivedTable: tableSubquery;
tableSubquery: subquery;

//joins
crossJoinTerm: CROSS JOIN tableReference;
naturalJoinTerm: (NATURAL)? (joinType)? JOIN tableReference (joinSpecification|anyUnexpected)?; // (.*?) - for error recovery
naturalJoinTerm: (NATURAL)? (joinType)? JOIN tableReference (joinSpecification|anyUnexpected??)?; // (.*?) - for error recovery
joinType: (INNER|outerJoinType (OUTER)?|UNION);
outerJoinType: (LEFT|RIGHT|FULL);
joinSpecification: (joinCondition|namedColumnsJoin);
Expand Down Expand Up @@ -380,7 +399,7 @@ selectStatementSingleRow: SELECT (setQuantifier)? selectList INTO selectTargetLi
selectTargetList: parameterSpecification (Comma parameterSpecification)*;
deleteStatement: DELETE FROM tableName? ((AS)? correlationName)? whereClause?;
insertStatement: INSERT INTO (tableName insertColumnsAndSource?)?;
insertColumnsAndSource: (LeftParen (insertColumnList? | Asterisk) RightParen?)? (queryExpression | DEFAULT VALUES);
insertColumnsAndSource: LeftParen (insertColumnList? | Asterisk) (RightParen (queryExpression | DEFAULT VALUES)?)?;
insertColumnList: columnNameList;

// UPDATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jkiss.dbeaver.model.lsm.sql.impl.syntax.SQLStandardParser.SqlQueriesContext;
import org.jkiss.dbeaver.model.stm.LSMInspections;
import org.jkiss.dbeaver.model.stm.STMErrorListener;
import org.jkiss.dbeaver.model.stm.STMTreeNode;

import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLStreamException;
Expand Down Expand Up @@ -160,21 +161,45 @@ public void reportAmbiguity(Parser arg0, DFA arg1, int arg2, int arg3, boolean a
System.out.println(sb.toString());
}

int pos = 36;
System.out.println(LSMInspections.prepareTerms(tree));
System.out.println(LSMInspections.prepareAbstractSyntaxInspection(tree, inputText.length() - 1).getReachabilityByName());
// System.out.println(LSMInspections.prepareAbstractSyntaxInspection(tree, pos).getReachabilityByName());
// System.out.println(inputText.substring(0, pos) + "|" + inputText.substring(pos));
}

public static String collect(STMTreeNode ctx) {
var input = CharStreams.fromString("");
var params = new LSMAnalyzerParameters(
Map.of("\"", "\""),
true,
false,
'?',
List.of(Map.entry(1, Set.of(":"))),
true
);
var ll = new SQLStandardLexer(input, params);
var tokens = new CommonTokenStream(ll);
var pp = new SQLStandardParser(tokens, params);

{ // print simple parse tree view
var sb = new StringBuilder();
sb.append("\n");
collect(ctx, pp, sb, "");
return sb.toString();
}
}

private static void collect(Tree ctx, Parser pp, StringBuilder sb, String indent) {
private static void collect(STMTreeNode ctx, Parser pp, StringBuilder sb, String indent) {
sb.append(indent).append(Trees.getNodeText(ctx, pp));
while (ctx.getChildCount() == 1 && !(ctx.getChild(0).getPayload() instanceof Token)) {
ctx = ctx.getChild(0);
ctx = ctx.getChildNode(0);
sb.append(".").append(Trees.getNodeText(ctx, pp));
}
sb.append("\n");
if (ctx.getChildCount() == 1 && ctx.getChild(0).getPayload() instanceof Token) {
sb.append(indent).append(" \"").append(Trees.getNodeText(ctx.getChild(0), pp)).append("\"\n");
sb.append(" [").append(ctx.getRealInterval()).append("]\n");
if (ctx.getChildCount() == 1 && ctx.getChild(0).getPayload() instanceof Token t) {
sb.append(indent).append(" ").append(pp.getVocabulary().getDisplayName(t.getType())).append(" \"").append(Trees.getNodeText(ctx.getChild(0), pp)).append("\" [").append(ctx.getChildNode(0).getRealInterval()).append("]\n");
} else {
for (Tree t : Trees.getChildren(ctx)) {
for (STMTreeNode t : ctx.getChildren()) {
collect(t, pp, sb, indent + " ");
}
}
Expand Down
Loading

0 comments on commit a0c456c

Please sign in to comment.