Repository navigation
Expand file tree
/
Copy pathNameKeywordsTest.java
More file actions
118 lines (105 loc) · 5.25 KB
/
Copy pathNameKeywordsTest.java
File metadata and controls
118 lines (105 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.parser;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect;
import net.sf.jsqlparser.statement.UnsupportedStatement;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
/**
* Checks that the keyword alternatives in the grammar and the sets of {@link NameKeywords} agree,
* in both directions: a reserved keyword is read as a name exactly when one of the sets holds it,
* and {@code ReservedKeyword()}, which MySQL and PostgreSQL take after "." and PostgreSQL after the
* AS of a select item, lists every reserved keyword. With MySQL every keyword right before "." is a
* name as well, and with PostgreSQL the names of roles take its type and function name keywords.
*
* <p>
* The statements put the keyword where {@code isReservedKeywordAsIdentifier()} always finds a name
* position (after COLUMN, after "(", after "." and after AS), so that only the token alternatives
* decide.
*/
class NameKeywordsTest {
private static final File GRAMMAR =
new File("src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt");
static Stream<Arguments> reservedKeywords() throws IOException {
List<Arguments> arguments = new ArrayList<>();
for (String keyword : ParserKeywordsUtils.getReservedKeywords(GRAMMAR)) {
for (Dialect dialect : new Dialect[] {null, Dialect.MYSQL, Dialect.POSTGRESQL}) {
arguments.add(Arguments.of(keyword, dialect));
}
}
return arguments.stream();
}
@ParameterizedTest(name = "{0} with {1}")
@MethodSource("reservedKeywords")
void testReservedKeywordIsANameExactlyWhenASetHoldsIt(String keyword, Dialect dialect) {
int kind = CCJSqlParserUtil.newParser(keyword).getNextToken().kind;
boolean name = NameKeywords.isName(dialect == null ? null : dialect.name(), kind);
assertEquals(name, parses("ALTER TABLE t ADD COLUMN " + keyword + " INT", dialect),
"after COLUMN");
assertEquals(name, parses("INSERT INTO t (" + keyword + ") VALUES (1)", dialect),
"in a column list");
// RelObjectNameExt() takes FROM and SELECT as well, and MySQL and PostgreSQL every keyword
assertEquals(name || kind == CCJSqlParserConstants.K_FROM
|| kind == CCJSqlParserConstants.K_SELECT || dialect != null,
parses("SELECT t." + keyword + " FROM t", dialect), "after a dot");
// PostgreSQL takes every keyword after the AS of a select item, not of a table
assertEquals(name || dialect == Dialect.POSTGRESQL,
parses("SELECT a AS " + keyword + " FROM t", dialect), "after AS");
}
static Stream<String> reservedKeywordWords() throws IOException {
return ParserKeywordsUtils.getReservedKeywords(GRAMMAR).stream();
}
@ParameterizedTest(name = "{0}")
@MethodSource("reservedKeywordWords")
void testMySqlReadsAKeywordRightBeforeADotAsAName(String keyword) {
int kind = CCJSqlParserUtil.newParser(keyword).getNextToken().kind;
assertTrue(parses("ALTER TABLE " + keyword + ".t ADD COLUMN c INT", Dialect.MYSQL),
"before a dot");
assertTrue(parses("SELECT " + keyword + ".a FROM t", Dialect.MYSQL), "in an expression");
// with a space before the dot MySQL looks the word up as a keyword
assertEquals(NameKeywords.isName(Dialect.MYSQL.name(), kind),
parses("ALTER TABLE " + keyword + " .t ADD COLUMN c INT", Dialect.MYSQL),
"before a space");
}
@ParameterizedTest(name = "{0}")
@MethodSource("reservedKeywordWords")
void testPostgreSqlReadsAReservedKeywordAsARoleNameExactlyWhenASetHoldsIt(String keyword) {
Token token = CCJSqlParserUtil.newParser(keyword).getNextToken();
String dialect = Dialect.POSTGRESQL.name();
boolean parses = parses("ALTER TABLE t OWNER TO " + keyword, Dialect.POSTGRESQL);
if (NameKeywords.isTypeFuncName(dialect, token)) {
assertTrue(parses);
} else if (!NameKeywords.isReservedName(token.kind)
&& !NameKeywords.isAliasCheckedByFollower(token.kind)) {
// the reserved names and ARRAY depend on their follower here, the others on the sets
assertEquals(NameKeywords.isDialectName(dialect, token.kind), parses);
}
}
private static boolean parses(String sql, Dialect dialect) {
try {
CCJSqlParser parser = CCJSqlParserUtil.newParser(sql);
if (dialect != null) {
parser.withDialect(dialect);
}
return !(parser.Statement() instanceof UnsupportedStatement)
&& parser.getToken(1).kind == CCJSqlParserConstants.EOF;
} catch (ParseException | RuntimeException e) {
return false;
}
}
}