diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/diff/ASTTreeUtils.java b/src/main/java/com/google/cloud/solutions/spannerddl/diff/ASTTreeUtils.java index 457e2c1..fca12a1 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/diff/ASTTreeUtils.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/diff/ASTTreeUtils.java @@ -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(",") @@ -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); } } diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTforeign_key.java b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTforeign_key.java index 549b69b..1dd8a87 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTforeign_key.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTforeign_key.java @@ -48,7 +48,7 @@ public List getConstrainedColumnNames() { private List 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()); } @@ -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 getReferencedColumnNames() { diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTkey_part.java b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTkey_part.java index f4bafef..c811caa 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTkey_part.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTkey_part.java @@ -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 { @@ -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(); } } } diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTname.java b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTname.java index 7252899..0274fe5 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTname.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTname.java @@ -31,6 +31,6 @@ public ASTname(DdlParser p, int id) { @Override public String toString() { - return ASTTreeUtils.tokensToString(this); + return ASTTreeUtils.tokensToString(this, false); } } diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTpath.java b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTpath.java index 89cc756..cb6866f 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTpath.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTpath.java @@ -31,6 +31,6 @@ public ASTpath(DdlParser p, int id) { @Override public String toString() { - return ASTTreeUtils.tokensToString(this); + return ASTTreeUtils.tokensToString(this, false); } } diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTstored_column.java b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTstored_column.java index f7f9f03..5e11b33 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTstored_column.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTstored_column.java @@ -29,6 +29,6 @@ public ASTstored_column(DdlParser p, int id) { @Override public String toString() { - return children[0].toString(); + return ((ASTpath)children[0]).toString(); } } diff --git a/src/test/java/com/google/cloud/solutions/spannerddl/parser/DDLParserTest.java b/src/test/java/com/google/cloud/solutions/spannerddl/parser/DDLParserTest.java index addb2c7..19863b6 100644 --- a/src/test/java/com/google/cloud/solutions/spannerddl/parser/DDLParserTest.java +++ b/src/test/java/com/google/cloud/solutions/spannerddl/parser/DDLParserTest.java @@ -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); diff --git a/src/test/resources/ddlParserValidation.txt b/src/test/resources/ddlParserValidation.txt index 02ac3c3..62a14bb 100644 --- a/src/test/resources/ddlParserValidation.txt +++ b/src/test/resources/ddlParserValidation.txt @@ -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)