-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[0.3.10] 新增 SQLTable 用于快速创建与该表相关的操作。
- Loading branch information
Showing
9 changed files
with
175 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
api/src/main/java/cc/carm/lib/easysql/api/SQLTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package cc.carm.lib.easysql.api; | ||
|
||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction; | ||
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction; | ||
import cc.carm.lib.easysql.api.builder.*; | ||
import cc.carm.lib.easysql.api.function.SQLHandler; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.sql.SQLException; | ||
|
||
/** | ||
* SQLTable 基于 {@link TableCreateBuilder} 构建表,用于快速创建与该表相关的操作。 | ||
* <ul> | ||
* <li>1. 调用 {@link SQLTable#of(String, String[])} 方法创建一个 SQLTable 对象;</li> | ||
* <li>2. 在应用初始化阶段调用 {@link SQLTable#create(SQLManager)} 方法初始化 SQLTable 对象;</li> | ||
* <li>3. 获取已创建的{@link SQLTable} 实例,直接调用对应方法进行关于表的相关操作。</li> | ||
* </ul> | ||
* | ||
* @author CarmJos | ||
* @since 0.3.10 | ||
*/ | ||
public abstract class SQLTable { | ||
|
||
public static @NotNull SQLTable of(@NotNull String tableName, @Nullable SQLHandler<TableCreateBuilder> table) { | ||
return new SQLTable(tableName) { | ||
@Override | ||
public int create(SQLManager sqlManager) throws SQLException { | ||
if (this.manager == null) this.manager = sqlManager; | ||
TableCreateBuilder tableBuilder = sqlManager.createTable(getTableName()); | ||
if (table != null) table.accept(tableBuilder); | ||
return tableBuilder.build().execute(); | ||
} | ||
}; | ||
} | ||
|
||
public static @NotNull SQLTable of(@NotNull String tableName, @NotNull String[] columns) { | ||
return of(tableName, columns, null); | ||
} | ||
|
||
public static @NotNull SQLTable of(@NotNull String tableName, | ||
@NotNull String[] columns, @Nullable String tableSettings) { | ||
return of(tableName, builder -> { | ||
builder.setColumns(columns); | ||
if (tableSettings != null) builder.setTableSettings(tableSettings); | ||
}); | ||
} | ||
|
||
private final @NotNull String tableName; | ||
|
||
protected SQLManager manager; | ||
|
||
/** | ||
* 请调用 {@link SQLTable} 下的静态方法进行对象的初始化。 | ||
* | ||
* @param tableName 该表的名称 | ||
*/ | ||
private SQLTable(@NotNull String tableName) { | ||
this.tableName = tableName; | ||
} | ||
|
||
public @NotNull String getTableName() { | ||
return tableName; | ||
} | ||
|
||
public abstract int create(SQLManager sqlManager) throws SQLException; | ||
|
||
public @NotNull TableQueryBuilder createQuery(@NotNull SQLManager sqlManager) { | ||
return sqlManager.createQuery().inTable(getTableName()); | ||
} | ||
|
||
public @NotNull TableQueryBuilder createQuery() { | ||
return createQuery(this.manager); | ||
} | ||
|
||
public @NotNull DeleteBuilder createDelete() { | ||
return createDelete(this.manager); | ||
} | ||
|
||
public @NotNull DeleteBuilder createDelete(@NotNull SQLManager sqlManager) { | ||
return sqlManager.createDelete(getTableName()); | ||
} | ||
|
||
public @NotNull UpdateBuilder createUpdate() { | ||
return createUpdate(this.manager); | ||
} | ||
|
||
public @NotNull UpdateBuilder createUpdate(@NotNull SQLManager sqlManager) { | ||
return sqlManager.createUpdate(getTableName()); | ||
} | ||
|
||
|
||
public @NotNull InsertBuilder<PreparedSQLUpdateAction> createInsert() { | ||
return createInsert(this.manager); | ||
} | ||
|
||
public @NotNull InsertBuilder<PreparedSQLUpdateAction> createInsert(@NotNull SQLManager sqlManager) { | ||
return sqlManager.createInsert(getTableName()); | ||
} | ||
|
||
|
||
public @NotNull InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch() { | ||
return createInsertBatch(this.manager); | ||
} | ||
|
||
public @NotNull InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch(@NotNull SQLManager sqlManager) { | ||
return sqlManager.createInsertBatch(getTableName()); | ||
} | ||
|
||
|
||
public @NotNull ReplaceBuilder<PreparedSQLUpdateAction> createReplace() { | ||
return createReplace(this.manager); | ||
} | ||
|
||
public @NotNull ReplaceBuilder<PreparedSQLUpdateAction> createReplace(@NotNull SQLManager sqlManager) { | ||
return sqlManager.createReplace(getTableName()); | ||
} | ||
|
||
|
||
public @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch() { | ||
return createReplaceBatch(this.manager); | ||
} | ||
|
||
public @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch(@NotNull SQLManager sqlManager) { | ||
return sqlManager.createReplaceBatch(getTableName()); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
api/src/main/java/cc/carm/lib/easysql/api/util/UUIDUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters