Skip to content

Commit

Permalink
feat: methods for returning unquoted names and identifiers
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Reichel <[email protected]>
Signed-off-by: manticore-projects <[email protected]>
  • Loading branch information
manticore-projects committed Aug 19, 2024
1 parent 6298e79 commit 728b286
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/main/java/net/sf/jsqlparser/schema/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public String getColumnName() {
return columnName;
}

public String getUnquotedColumnName() {
return unquote(columnName);
}

public void setColumnName(String string) {
columnName = string;
}
Expand All @@ -117,6 +121,11 @@ public String getFullyQualifiedName() {
return getFullyQualifiedName(false);
}

@Override
public String getUnquotedName() {
return unquote(columnName);
}

public String getFullyQualifiedName(boolean aliases) {
StringBuilder fqn = new StringBuilder();

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/sf/jsqlparser/schema/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public String getFullyQualifiedName() {
return fqn;
}

@Override
public String getUnquotedName() {
return unquote(databaseName);
}

@Override
public String toString() {
return getFullyQualifiedName();
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/net/sf/jsqlparser/schema/MultiPartName.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,24 @@
*/
package net.sf.jsqlparser.schema;

import java.util.regex.Pattern;

public interface MultiPartName {
Pattern LEADING_TRAILING_QUOTES_PATTERN = Pattern.compile("^[\"\\[`]+|[\"\\]`]+$");

/**
* Removes leading and trailing quotes from a SQL quoted identifier
*
* @param quotedIdentifier the quoted identifier
* @return the pure identifier without quotes
*/
default String unquote(String quotedIdentifier) {
return quotedIdentifier != null
? LEADING_TRAILING_QUOTES_PATTERN.matcher(quotedIdentifier).replaceAll("")
: null;
}

String getFullyQualifiedName();

String getUnquotedName();
}
5 changes: 5 additions & 0 deletions src/main/java/net/sf/jsqlparser/schema/Sequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public String getFullyQualifiedName() {
return fqn.toString();
}

@Override
public String getUnquotedName() {
return unquote(partItems.get(NAME_IDX));
}

@Override
public String toString() {
StringBuilder sql = new StringBuilder(getFullyQualifiedName());
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/sf/jsqlparser/schema/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public String getFullyQualifiedName() {
}
}

@Override
public String getUnquotedName() {
return unquote(serverName);
}

@Override
public String toString() {
return getFullyQualifiedName();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/sf/jsqlparser/schema/Synonym.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public String getFullyQualifiedName() {
return fqn.toString();
}

@Override
public String getUnquotedName() {
return unquote(partItems.get(NAME_IDX));
}

@Override
public String toString() {
StringBuilder sql = new StringBuilder(getFullyQualifiedName());
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/net/sf/jsqlparser/schema/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ public Database getDatabase() {
return new Database(getIndex(DATABASE_IDX));
}

public String getDatabaseName() {
return getIndex(DATABASE_IDX);
}

public String getUnquotedDatabaseName() {
return unquote(getDatabaseName());
}

public void setDatabase(Database database) {
setIndex(DATABASE_IDX, database.getDatabaseName());
if (database.getServer() != null) {
Expand All @@ -124,6 +132,10 @@ public String getSchemaName() {
return getIndex(SCHEMA_IDX);
}

public String getUnquotedSchemaName() {
return unquote(getSchemaName());
}

public Table setSchemaName(String schemaName) {
this.setIndex(SCHEMA_IDX, schemaName);
return this;
Expand All @@ -145,6 +157,7 @@ public String getName() {
return name;
}


public void setName(String name) {
setIndex(NAME_IDX, name);
}
Expand Down Expand Up @@ -207,7 +220,6 @@ public String getFullyQualifiedName() {
partItems.remove(partItems.size() - 1);
}


for (int i = partItems.size() - 1; i >= 0; i--) {
String part = partItems.get(i);
if (part == null) {
Expand All @@ -222,6 +234,11 @@ public String getFullyQualifiedName() {
return fqn.toString();
}

@Override
public String getUnquotedName() {
return unquote(getName());
}

@Override
public <T, S> T accept(FromItemVisitor<T> fromItemVisitor, S context) {
return fromItemVisitor.visit(this, context);
Expand Down

0 comments on commit 728b286

Please sign in to comment.