Skip to content

Commit 7fa9a39

Browse files
committed
fix: preserve PostgreSQL quoted names, numeric scales and comment literals
1 parent 6312f9e commit 7fa9a39

7 files changed

Lines changed: 564 additions & 46 deletions

File tree

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ public Table getResolvedTable() {
529529
public Table setResolvedTable(Table resolvedTable) {
530530
// clone, not reference
531531
if (resolvedTable != null) {
532-
this.resolvedTable = new Table(resolvedTable.getFullyQualifiedName());
532+
this.resolvedTable = resolvedTable.copyName();
533533
}
534534
return this;
535535
}
@@ -579,9 +579,16 @@ public static Table[] setUnsetCatalogAndSchema(String currentCatalogName,
579579

580580
@Override
581581
public Table clone() {
582-
Table clone = new Table(this.getFullyQualifiedName());
582+
Table clone = copyName();
583583
clone.setTableVariable(tableVariable);
584584
clone.setResolvedTable(this.resolvedTable != null ? this.resolvedTable.clone() : null);
585585
return clone;
586586
}
587+
588+
private Table copyName() {
589+
Table copy = new Table();
590+
copy.partItems = new ArrayList<>(partItems);
591+
copy.partDelimiters = new ArrayList<>(partDelimiters);
592+
return copy;
593+
}
587594
}

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)