Skip to content

Commit 84f780c

Browse files
authored
fix: preserve PostgreSQL quoted names, numeric scales and comment literals (#2638)
* fix: preserve PostgreSQL quoted names, numeric scales and comment literals * refactor: clarify table identifier copying responsibilities
1 parent 0036c75 commit 84f780c

7 files changed

Lines changed: 568 additions & 48 deletions

File tree

‎src/main/java/net/sf/jsqlparser/schema/Table.java‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,15 +521,16 @@ public Table getResolvedTable() {
521521
}
522522

523523
/**
524-
* Sets resolved table.
524+
* Stores a detached copy of the resolved table's identifier. Reference-specific flags and
525+
* clauses are not copied.
525526
*
526527
* @param resolvedTable the resolved table
527528
* @return this table
528529
*/
529530
public Table setResolvedTable(Table resolvedTable) {
530-
// clone, not reference
531531
if (resolvedTable != null) {
532-
this.resolvedTable = new Table(resolvedTable.getFullyQualifiedName());
532+
this.resolvedTable = new Table();
533+
resolvedTable.copyIdentifierPartsTo(this.resolvedTable);
533534
}
534535
return this;
535536
}
@@ -577,11 +578,19 @@ public static Table[] setUnsetCatalogAndSchema(String currentCatalogName,
577578
return tables;
578579
}
579580

581+
/** Copies the table identifier, table-variable flag and resolved table identifier. */
580582
@Override
581583
public Table clone() {
582-
Table clone = new Table(this.getFullyQualifiedName());
584+
Table clone = new Table();
585+
copyIdentifierPartsTo(clone);
583586
clone.setTableVariable(tableVariable);
584587
clone.setResolvedTable(this.resolvedTable != null ? this.resolvedTable.clone() : null);
585588
return clone;
586589
}
590+
591+
/** Copies identifier components and separators, leaving the target's other state unchanged. */
592+
private void copyIdentifierPartsTo(Table target) {
593+
target.partItems = new ArrayList<>(partItems);
594+
target.partDelimiters = new ArrayList<>(partDelimiters);
595+
}
587596
}

‎src/main/java/net/sf/jsqlparser/statement/create/table/ColDataType.java‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,27 @@ public ColDataType() {
5454
}
5555

5656
public ColDataType(String dataType, int precision, int scale) {
57-
this.dataType = dataType;
57+
this(dataType);
58+
setNumericParameters(precision < 0 ? null : Integer.valueOf(precision),
59+
scale < 0 ? null : Integer.valueOf(scale));
60+
}
61+
62+
/**
63+
* Creates a parameterized type, using {@code null} for an omitted parameter. Unlike the legacy
64+
* primitive constructor, this accepts negative scales, including {@code -1}.
65+
*/
66+
public static ColDataType fromNumericParameters(String dataType, Integer precision,
67+
Integer scale) {
68+
ColDataType type = new ColDataType(dataType);
69+
type.setNumericParameters(precision, scale);
70+
return type;
71+
}
5872

59-
if (precision >= 0) {
73+
private void setNumericParameters(Integer precision, Integer scale) {
74+
if (precision != null) {
6075
this.precision = precision;
6176
this.dataType += " (" + (precision == Integer.MAX_VALUE ? "MAX" : precision);
62-
if (scale >= 0) {
77+
if (scale != null) {
6378
this.scale = scale;
6479
this.dataType += ", " + scale;
6580
}
@@ -211,8 +226,8 @@ public void setPrecision(Integer precision) {
211226
}
212227

213228
/**
214-
* The second numeric type parameter, e.g. {@code 2} for {@code DECIMAL(10, 2)}. Returns
215-
* {@code null} when absent.
229+
* The second numeric type parameter, e.g. {@code 2} for {@code DECIMAL(10, 2)} or {@code -3}
230+
* for PostgreSQL {@code NUMERIC(2, -3)}. Returns {@code null} when absent.
216231
*/
217232
public Integer getScale() {
218233
return scale;

0 commit comments

Comments
 (0)