Skip to content

Commit 2ab5568

Browse files
committed
Remove name supplier from AliasableSQLTable
Name supplier creates an effectively mutable object and this is against the principles of the library.
1 parent b51b499 commit 2ab5568

File tree

2 files changed

+5
-24
lines changed

2 files changed

+5
-24
lines changed

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,13 @@ protected AliasableSqlTable(String tableName, Supplier<T> constructor) {
2929
this.constructor = Objects.requireNonNull(constructor);
3030
}
3131

32-
protected AliasableSqlTable(Supplier<String> tableNameSupplier, Supplier<T> constructor) {
33-
super(tableNameSupplier);
34-
this.constructor = Objects.requireNonNull(constructor);
35-
}
36-
3732
public T withAlias(String alias) {
3833
T newTable = constructor.get();
3934
((AliasableSqlTable<T>) newTable).tableAlias = alias;
4035
newTable.nameSupplier = nameSupplier;
4136
return newTable;
4237
}
4338

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-
6139
/**
6240
* Returns a new instance of this table with the specified name. All column instances are recreated.
6341
* This is useful for sharding where the table name may change at runtime based on some sharding algorithm,
@@ -68,7 +46,10 @@ public T withNameSupplier(Supplier<String> nameSupplier) {
6846
*/
6947
public T withName(String name) {
7048
Objects.requireNonNull(name);
71-
return withNameSupplier(() -> name);
49+
T newTable = constructor.get();
50+
((AliasableSqlTable<T>) newTable).tableAlias = tableAlias;
51+
newTable.nameSupplier = () -> name;
52+
return newTable;
7253
}
7354

7455
@Override

src/test/java/examples/joins/UserDynamicSQLSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static final class User extends AliasableSqlTable<User> {
3232
public final SqlColumn<Integer> parentId = column("parent_id", JDBCType.INTEGER);
3333

3434
public User() {
35-
super(() -> "User", User::new);
35+
super("User", User::new);
3636
}
3737
}
3838
}

0 commit comments

Comments
 (0)