Skip to content

Commit 157a035

Browse files
committed
Ability to create table instances with new names
This is to more easily support sharding - we can create multiple new table objects with differing names, but other attributes the same. Or, we can create a table object with a supplier function that will change the table name at render time. This capability is NOT thread safe.
1 parent 9ede590 commit 157a035

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/main/java/org/mybatis/dynamic/sql/AliasableSqlTable.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,40 @@ protected AliasableSqlTable(Supplier<String> tableNameSupplier, Supplier<T> cons
3737
public T withAlias(String alias) {
3838
T newTable = constructor.get();
3939
((AliasableSqlTable<T>) newTable).tableAlias = alias;
40+
newTable.nameSupplier = nameSupplier;
4041
return newTable;
4142
}
4243

44+
/**
45+
* Returns a new instance of this table with the specified name supplier. All column instances are recreated.
46+
* This is useful for sharding where the table name may change at runtime based on some sharding algorithm,
47+
* but all other table attributes are the same. Use of a name supplier allows you to create one table
48+
* instance with a dynamically changing table name. Be very careful with this usage - it is NOT
49+
* thread safe unless the specified nameSupplier is thread safe.
50+
*
51+
* @param nameSupplier new name supplier for the table
52+
* @return a new AliasableSqlTable with the specified nameSupplier, all other table attributes are copied
53+
*/
54+
public T withNameSupplier(Supplier<String> nameSupplier) {
55+
T newTable = constructor.get();
56+
((AliasableSqlTable<T>) newTable).tableAlias = tableAlias;
57+
newTable.nameSupplier = Objects.requireNonNull(nameSupplier);
58+
return newTable;
59+
}
60+
61+
/**
62+
* Returns a new instance of this table with the specified name. All column instances are recreated.
63+
* This is useful for sharding where the table name may change at runtime based on some sharding algorithm,
64+
* but all other table attributes are the same.
65+
*
66+
* @param name new name for the table
67+
* @return a new AliasableSqlTable with the specified name, all other table attributes are copied
68+
*/
69+
public T withName(String name) {
70+
Objects.requireNonNull(name);
71+
return withNameSupplier(() -> name);
72+
}
73+
4374
@Override
4475
public Optional<String> tableAlias() {
4576
return Optional.ofNullable(tableAlias);

src/main/java/org/mybatis/dynamic/sql/SqlTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
public class SqlTable implements TableExpression {
2626

27-
private final Supplier<String> nameSupplier;
27+
protected Supplier<String> nameSupplier;
2828

2929
protected SqlTable(String tableName) {
3030
Objects.requireNonNull(tableName);

0 commit comments

Comments
 (0)