Skip to content

Commit

Permalink
No not capitalize reserved words when used as column names
Browse files Browse the repository at this point in the history
(note does not work in CHECK constraint and generated column expressions)
  • Loading branch information
nielm committed Jan 26, 2024
1 parent 2659e65 commit d84c962
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,20 @@ private ASTTreeUtils() {}
* spacing and capitalization of tokens.
*/
public static String tokensToString(Token firstToken, Token lastToken) {
return tokensToString(firstToken, lastToken, true);
}

/**
* Generate the original parsed text between the 2 specified tokens, normalizing the text with
* spacing and optional capitalization of reserved words.
*/
public static String tokensToString(
Token firstToken, Token lastToken, boolean upperCaseReserved) {
StringBuilder sb = new StringBuilder();
Token t = firstToken;
while (t != lastToken) {
String tok = t.toString();
sb.append(isReservedWord(tok) ? tok.toUpperCase() : tok);
sb.append(isReservedWord(tok) && upperCaseReserved ? tok.toUpperCase() : tok);

if (t.next != null
&& !t.next.toString().equals(",")
Expand All @@ -91,15 +100,23 @@ public static String tokensToString(Token firstToken, Token lastToken) {
}
// append last token
String tok = t.toString();
sb.append(isReservedWord(tok) ? tok.toUpperCase() : tok);
sb.append(isReservedWord(tok) && upperCaseReserved ? tok.toUpperCase() : tok);
return sb.toString();
}

/**
* Generate the original parsed text of the node, normalizing the text with spacing and
* capitalization of tokens.
* capitalization of reserved words.
*/
public static String tokensToString(SimpleNode node) {
return tokensToString(node.jjtGetFirstToken(), node.jjtGetLastToken());
return tokensToString(node, true);
}

/**
* Generate the original parsed text of the node, normalizing the text with spacing and optional
* capitalization of reserved words.
*/
public static String tokensToString(SimpleNode node, boolean upperCaseReserved) {
return tokensToString(node.jjtGetFirstToken(), node.jjtGetLastToken(), upperCaseReserved);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public List<String> getConstrainedColumnNames() {

private List<String> identifierListToStringList(ASTidentifier_list idList) {
return Arrays.stream(idList.children)
.map(o -> ASTTreeUtils.tokensToString((ASTidentifier) o))
.map(o -> ASTTreeUtils.tokensToString((ASTidentifier) o, false))
.collect(Collectors.toList());
}

Expand All @@ -57,7 +57,7 @@ public String getReferencedTableName() {
if (children[0] instanceof ASTconstraint_name) {
child++;
}
return ASTTreeUtils.tokensToString((ASTreferenced_table) children[child]);
return ASTTreeUtils.tokensToString((ASTreferenced_table) children[child], false);
}

public List<String> getReferencedColumnNames() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.google.cloud.solutions.spannerddl.parser;

import com.google.cloud.solutions.spannerddl.diff.ASTTreeUtils;

/** Abstract Syntax Tree parser object for "key_part" token */
public class ASTkey_part extends SimpleNode {

Expand All @@ -35,13 +33,11 @@ public String toString() {
return jjtGetFirstToken().toString();
}
if (children.length == 1) {
return ASTTreeUtils.tokensToString((ASTpath) children[0])
+ " ASC"; // key name without direction ;

return ((ASTpath) children[0]).toString() + " ASC"; // key name without direction ;
} else {
// key name and ASC/DESC
return ASTTreeUtils.tokensToString((ASTpath) children[0])
+ " "
+ children[1].toString().toUpperCase();
return ((ASTpath) children[0]).toString() + " " + children[1].toString().toUpperCase();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public ASTname(DdlParser p, int id) {

@Override
public String toString() {
return ASTTreeUtils.tokensToString(this);
return ASTTreeUtils.tokensToString(this, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public ASTpath(DdlParser p, int id) {

@Override
public String toString() {
return ASTTreeUtils.tokensToString(this);
return ASTTreeUtils.tokensToString(this, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public ASTstored_column(DdlParser p, int id) {

@Override
public String toString() {
return children[0].toString();
return ((ASTpath)children[0]).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,6 @@ public void parseAlterDatabase() throws ParseException {
parseAndVerifyToString("ALTER DATABASE dbname SET OPTIONS (opt1=NULL,opt2='1234',opt3=3)");
}

@Test
public void parseReservedWordsColNames() throws ParseException {
parseAndVerifyToString(
"CREATE TABLE mytable (`key` INT64, `index` STRING(MAX), `table` BYTES(MAX)) PRIMARY KEY"
+ " (`key` ASC)");
}

private static void parseCheckingException(String ddlStatement, String exceptionContains) {
try {
parseAndVerifyToString(ddlStatement);
Expand Down
24 changes: 22 additions & 2 deletions src/test/resources/ddlParserValidation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ INTERLEAVE IN PARENT `other_table` ON DELETE CASCADE,
ROW DELETION POLICY (OLDER_THAN ( timestampcol, INTERVAL 10 DAY ))


== Test 6 -- using reserverd words as column names
== Test 6 -- using quoted reserverd words as column names

CREATE TABLE mytable (`key` INT64, `index` STRING(MAX), `table` BYTES(MAX)) PRIMARY KEY (`key` ASC)
CREATE TABLE mytable
(`key` INT64,
`index` STRING(MAX),
`table` BYTES(MAX),
generatedcol INT64 AS ( KEY * INDEX ) STORED,
CONSTRAINT fk_col_remote2 FOREIGN KEY (`key`) REFERENCES test.other_table (`key`))
PRIMARY KEY (`key` ASC)

== Test 7 -- using unquoted reserverd words as column names

CREATE TABLE mytable
(key INT64,
index STRING(MAX),
table BYTES(MAX),
generatedcol INT64 AS ( KEY * INDEX ) STORED,
CONSTRAINT fk_col_remote2 FOREIGN KEY (key) REFERENCES test.other_table (key))
PRIMARY KEY (key ASC, index ASC)

== Test 7 -- using unquoted reserverd words as column names in index

CREATE INDEX myIndex ON mytable (key ASC, index ASC) STORING (table)

0 comments on commit d84c962

Please sign in to comment.