10
10
package net .sf .jsqlparser .schema ;
11
11
12
12
import java .util .ArrayList ;
13
+ import java .util .Arrays ;
13
14
import java .util .Collections ;
14
15
import java .util .List ;
15
16
28
29
/**
29
30
* A table. It can have an alias and the schema name it belongs to.
30
31
*/
31
- public class Table extends ASTNodeAccessImpl implements ErrorDestination , FromItem , MultiPartName {
32
+ public class Table extends ASTNodeAccessImpl
33
+ implements ErrorDestination , FromItem , MultiPartName , Cloneable {
32
34
33
- // private Database database;
34
- // private String schemaName;
35
- // private String name;
36
35
private static final int NAME_IDX = 0 ;
37
36
38
37
private static final int SCHEMA_IDX = 1 ;
@@ -45,6 +44,9 @@ public class Table extends ASTNodeAccessImpl implements ErrorDestination, FromIt
45
44
46
45
private List <String > partDelimiters = new ArrayList <>();
47
46
47
+ // holds the various `time travel` syntax for BigQuery, RedShift, Snowflake or RedShift
48
+ private String timeTravelStr = null ;
49
+
48
50
private Alias alias ;
49
51
50
52
private SampleClause sampleClause ;
@@ -74,6 +76,10 @@ public Table(String name) {
74
76
setName (name );
75
77
}
76
78
79
+ public Table (String name , boolean splitNamesOnDelimiter ) {
80
+ setName (name , splitNamesOnDelimiter );
81
+ }
82
+
77
83
public Table (String schemaName , String name ) {
78
84
setSchemaName (schemaName );
79
85
setName (name );
@@ -197,12 +203,20 @@ public void setName(String name) {
197
203
.of ("0" , "N" , "n" , "FALSE" , "false" , "OFF" , "off" )
198
204
.contains (System .getProperty ("SPLIT_NAMES_ON_DELIMITER" ));
199
205
206
+ setName (name , splitNamesOnDelimiter );
207
+ }
208
+
209
+ public void setName (String name , boolean splitNamesOnDelimiter ) {
200
210
if (MultiPartName .isQuoted (name ) && name .contains ("." ) && splitNamesOnDelimiter ) {
201
211
partItems .clear ();
202
212
for (String unquotedIdentifier : MultiPartName .unquote (name ).split ("\\ ." )) {
203
213
partItems .add ("\" " + unquotedIdentifier + "\" " );
204
214
}
205
215
Collections .reverse (partItems );
216
+ } else if (name .contains ("." ) && splitNamesOnDelimiter ) {
217
+ partItems .clear ();
218
+ partItems .addAll (Arrays .asList (MultiPartName .unquote (name ).split ("\\ ." )));
219
+ Collections .reverse (partItems );
206
220
} else {
207
221
setIndex (NAME_IDX , name );
208
222
}
@@ -294,6 +308,15 @@ public <T, S> T accept(IntoTableVisitor<T> intoTableVisitor, S context) {
294
308
return intoTableVisitor .visit (this , context );
295
309
}
296
310
311
+ public String getTimeTravel () {
312
+ return timeTravelStr ;
313
+ }
314
+
315
+ public Table setTimeTravel (String timeTravelStr ) {
316
+ this .timeTravelStr = timeTravelStr ;
317
+ return this ;
318
+ }
319
+
297
320
@ Override
298
321
public Pivot getPivot () {
299
322
return pivot ;
@@ -346,6 +369,11 @@ public Table setSampleClause(SampleClause sampleClause) {
346
369
347
370
public StringBuilder appendTo (StringBuilder builder ) {
348
371
builder .append (getFullyQualifiedName ());
372
+
373
+ if (timeTravelStr != null ) {
374
+ builder .append (" " ).append (timeTravelStr );
375
+ }
376
+
349
377
if (alias != null ) {
350
378
builder .append (alias );
351
379
}
@@ -422,7 +450,9 @@ public Table getResolvedTable() {
422
450
*/
423
451
public Table setResolvedTable (Table resolvedTable ) {
424
452
// clone, not reference
425
- this .resolvedTable = new Table (resolvedTable .getFullyQualifiedName ());
453
+ if (resolvedTable != null ) {
454
+ this .resolvedTable = new Table (resolvedTable .getFullyQualifiedName ());
455
+ }
426
456
return this ;
427
457
}
428
458
@@ -465,4 +495,16 @@ public static Table[] setUnsetCatalogAndSchema(String currentCatalogName,
465
495
}
466
496
return tables ;
467
497
}
498
+
499
+ @ Override
500
+ public Table clone () {
501
+ try {
502
+ Table clone = (Table ) super .clone ();
503
+ clone .setName (this .getFullyQualifiedName ());
504
+ clone .setResolvedTable (this .resolvedTable );
505
+ return clone ;
506
+ } catch (CloneNotSupportedException e ) {
507
+ throw new AssertionError ();
508
+ }
509
+ }
468
510
}
0 commit comments