1
mirror of https://github.com/CarmJos/EasySQL.git synced 2024-09-19 21:35:47 +00:00

feat(keys): 现在可以自定义返回的自增主键类型。

现在可以通过 returnGeneratedKey(Class<T> numberClass) 方法要求返回指定类型的自增主键。

BREAKING CHANGE: 移除了对于“是否返回主键”的选择,一旦定义了主键类型,就代表action将返回该类型的主键。
This commit is contained in:
Carm Jos 2022-06-09 12:12:11 +08:00
parent 9b4460f97a
commit 298a5c4e81
22 changed files with 218 additions and 202 deletions

View File

@ -110,7 +110,7 @@ public interface SQLManager {
* @return 更新的行数
* @see SQLUpdateAction
*/
@Nullable Long executeSQL(String sql);
@Nullable Integer executeSQL(String sql);
/**
* 执行一条不需要返回结果的预处理SQL更改(UPDATEREPLACEDELETE)
@ -120,7 +120,7 @@ public interface SQLManager {
* @return 更新的行数
* @see PreparedSQLUpdateAction
*/
@Nullable Long executeSQL(String sql, Object[] params);
@Nullable Integer executeSQL(String sql, Object[] params);
/**
* 执行多条不需要返回结果的SQL更改(UPDATEREPLACEDELETE)
@ -130,7 +130,7 @@ public interface SQLManager {
* @return 对应参数返回的行数
* @see PreparedSQLUpdateBatchAction
*/
@Nullable List<Long> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch);
@Nullable List<Integer> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch);
/**
@ -181,7 +181,7 @@ public interface SQLManager {
* @param tableName 目标表名
* @return {@link InsertBuilder}
*/
InsertBuilder<PreparedSQLUpdateAction> createInsert(@NotNull String tableName);
InsertBuilder<PreparedSQLUpdateAction<Integer>> createInsert(@NotNull String tableName);
/**
* 创建支持多组数据的插入操作
@ -189,7 +189,7 @@ public interface SQLManager {
* @param tableName 目标表名
* @return {@link InsertBuilder}
*/
InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch(@NotNull String tableName);
InsertBuilder<PreparedSQLUpdateBatchAction<Integer>> createInsertBatch(@NotNull String tableName);
/**
* 创建一条替换操作
@ -197,7 +197,7 @@ public interface SQLManager {
* @param tableName 目标表名
* @return {@link ReplaceBuilder}
*/
ReplaceBuilder<PreparedSQLUpdateAction> createReplace(@NotNull String tableName);
ReplaceBuilder<PreparedSQLUpdateAction<Integer>> createReplace(@NotNull String tableName);
/**
* 创建支持多组数据的替换操作
@ -205,7 +205,7 @@ public interface SQLManager {
* @param tableName 目标表名
* @return {@link ReplaceBuilder}
*/
ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch(@NotNull String tableName);
ReplaceBuilder<PreparedSQLUpdateBatchAction<Integer>> createReplaceBatch(@NotNull String tableName);
/**
* 创建更新操作

View File

@ -25,8 +25,10 @@ public abstract class SQLTable {
public static @NotNull SQLTable of(@NotNull String tableName, @Nullable SQLHandler<TableCreateBuilder> table) {
return new SQLTable(tableName) {
@Override
public boolean create(SQLManager sqlManager) throws SQLException {
public boolean create(@NotNull SQLManager sqlManager, String tablePrefix) throws SQLException {
if (this.manager == null) this.manager = sqlManager;
this.tablePrefix = tablePrefix;
TableCreateBuilder tableBuilder = sqlManager.createTable(getTableName());
if (table != null) table.accept(tableBuilder);
return tableBuilder.build().executeFunction(l -> l > 0, false);
@ -48,6 +50,7 @@ public abstract class SQLTable {
private final @NotNull String tableName;
protected String tablePrefix;
protected SQLManager manager;
/**
@ -60,17 +63,22 @@ public abstract class SQLTable {
}
public @NotNull String getTableName() {
return tableName;
return (tablePrefix != null ? tablePrefix : "") + tableName;
}
/**
* 使用指定 SQLManager 进行本示例的初始化
*
* @param sqlManager {@link SQLManager}
* @param sqlManager {@link SQLManager}
* @param tablePrefix 表名前缀
* @return 本表是否为首次创建
* @throws SQLException 出现任何错误时抛出
*/
public abstract boolean create(SQLManager sqlManager) throws SQLException;
public abstract boolean create(@NotNull SQLManager sqlManager, @Nullable String tablePrefix) throws SQLException;
public boolean create(@NotNull SQLManager sqlManager) throws SQLException {
return create(manager, null);
}
public @NotNull TableQueryBuilder createQuery(@NotNull SQLManager sqlManager) {
return sqlManager.createQuery().inTable(getTableName());
@ -97,38 +105,38 @@ public abstract class SQLTable {
}
public @NotNull InsertBuilder<PreparedSQLUpdateAction> createInsert() {
public @NotNull InsertBuilder<PreparedSQLUpdateAction<Integer>> createInsert() {
return createInsert(this.manager);
}
public @NotNull InsertBuilder<PreparedSQLUpdateAction> createInsert(@NotNull SQLManager sqlManager) {
public @NotNull InsertBuilder<PreparedSQLUpdateAction<Integer>> createInsert(@NotNull SQLManager sqlManager) {
return sqlManager.createInsert(getTableName());
}
public @NotNull InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch() {
public @NotNull InsertBuilder<PreparedSQLUpdateBatchAction<Integer>> createInsertBatch() {
return createInsertBatch(this.manager);
}
public @NotNull InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch(@NotNull SQLManager sqlManager) {
public @NotNull InsertBuilder<PreparedSQLUpdateBatchAction<Integer>> createInsertBatch(@NotNull SQLManager sqlManager) {
return sqlManager.createInsertBatch(getTableName());
}
public @NotNull ReplaceBuilder<PreparedSQLUpdateAction> createReplace() {
public @NotNull ReplaceBuilder<PreparedSQLUpdateAction<Integer>> createReplace() {
return createReplace(this.manager);
}
public @NotNull ReplaceBuilder<PreparedSQLUpdateAction> createReplace(@NotNull SQLManager sqlManager) {
public @NotNull ReplaceBuilder<PreparedSQLUpdateAction<Integer>> createReplace(@NotNull SQLManager sqlManager) {
return sqlManager.createReplace(getTableName());
}
public @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch() {
public @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction<Integer>> createReplaceBatch() {
return createReplaceBatch(this.manager);
}
public @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch(@NotNull SQLManager sqlManager) {
public @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction<Integer>> createReplaceBatch(@NotNull SQLManager sqlManager) {
return sqlManager.createReplaceBatch(getTableName());
}

View File

@ -2,7 +2,7 @@ package cc.carm.lib.easysql.api.action;
import org.jetbrains.annotations.Nullable;
public interface PreparedSQLUpdateAction extends SQLUpdateAction {
public interface PreparedSQLUpdateAction<T extends Number> extends SQLUpdateAction<T> {
/**
* 设定SQL语句中所有 ? 对应的参数
@ -10,7 +10,7 @@ public interface PreparedSQLUpdateAction extends SQLUpdateAction {
* @param params 参数内容
* @return {@link PreparedSQLUpdateAction}
*/
PreparedSQLUpdateAction setParams(Object... params);
PreparedSQLUpdateAction<T> setParams(Object... params);
/**
* 设定SQL语句中所有 ? 对应的参数
@ -18,6 +18,6 @@ public interface PreparedSQLUpdateAction extends SQLUpdateAction {
* @param params 参数内容
* @return {@link PreparedSQLUpdateAction}
*/
PreparedSQLUpdateAction setParams(@Nullable Iterable<Object> params);
PreparedSQLUpdateAction<T> setParams(@Nullable Iterable<Object> params);
}

View File

@ -4,7 +4,7 @@ import cc.carm.lib.easysql.api.SQLAction;
import java.util.List;
public interface PreparedSQLUpdateBatchAction extends SQLAction<List<Long>> {
public interface PreparedSQLUpdateBatchAction<T extends Number> extends SQLAction<List<T>> {
/**
* 设定多组SQL语句中所有 ? 对应的参数
@ -12,7 +12,7 @@ public interface PreparedSQLUpdateBatchAction extends SQLAction<List<Long>> {
* @param allParams 所有参数内容
* @return {@link PreparedSQLUpdateBatchAction}
*/
PreparedSQLUpdateBatchAction setAllParams(Iterable<Object[]> allParams);
PreparedSQLUpdateBatchAction<T> setAllParams(Iterable<Object[]> allParams);
/**
* 添加一组SQL语句中所有 ? 对应的参数
@ -20,37 +20,22 @@ public interface PreparedSQLUpdateBatchAction extends SQLAction<List<Long>> {
* @param params 参数内容
* @return {@link PreparedSQLUpdateBatchAction}
*/
PreparedSQLUpdateBatchAction addParamsBatch(Object... params);
/**
* 设定自增主键的序列
*
* @param keyColumnIndex 自增主键的序列
* <br>若该值 0 {@link #execute()} 返回自增主键数值
* <br>若该值 0 {@link #execute()} 返回变更的行数
* @return {@link PreparedSQLUpdateBatchAction}
* @see #setReturnGeneratedKeys(boolean)
*/
@Deprecated
default PreparedSQLUpdateBatchAction setKeyIndex(int keyColumnIndex) {
return setReturnGeneratedKeys(keyColumnIndex > 0);
}
PreparedSQLUpdateBatchAction<T> addParamsBatch(Object... params);
/**
* 设定该操作返回自增键序列
*
* @return {@link PreparedSQLUpdateBatchAction}
* @return {@link SQLUpdateAction}
*/
default PreparedSQLUpdateBatchAction returnGeneratedKeys() {
return setReturnGeneratedKeys(true);
}
PreparedSQLUpdateBatchAction<T> returnGeneratedKeys();
/**
* 设定该操作是否返回自增键序列
* 设定该操作返回自增键序列
*
* @param returnGeneratedKey 是否返回自增键序列
* @return {@link PreparedSQLUpdateBatchAction}
* @param keyTypeClass 自增序列的数字类型
* @param <N> 自增键序列类型 {@link Number}
* @return {@link SQLUpdateAction}
*/
PreparedSQLUpdateBatchAction setReturnGeneratedKeys(boolean returnGeneratedKey);
<N extends Number> PreparedSQLUpdateBatchAction<N> returnGeneratedKeys(Class<N> keyTypeClass);
}

View File

@ -2,37 +2,24 @@ package cc.carm.lib.easysql.api.action;
import cc.carm.lib.easysql.api.SQLAction;
public interface SQLUpdateAction extends SQLAction<Long> {
public interface SQLUpdateAction<T extends Number> extends SQLAction<T> {
/**
* 设定自增主键的序列
*
* @param keyColumnIndex 自增主键的序列
* <br>若该值 0 {@link #execute()} 返回自增主键数值
* <br>若该值 0 {@link #execute()} 返回变更的行数
* @return {@link SQLUpdateAction}
* @see #setReturnGeneratedKey(boolean)
*/
@Deprecated
default SQLUpdateAction setKeyIndex(int keyColumnIndex) {
return setReturnGeneratedKey(keyColumnIndex > 0);
}
/**
* 设定该操作返回自增键序列
*
* @return {@link SQLUpdateAction}
*/
default SQLUpdateAction returnGeneratedKey() {
return setReturnGeneratedKey(true);
}
SQLUpdateAction<T> returnGeneratedKey();
/**
* 设定该操作是否返回自增键序列
* 设定该操作返回自增键序列
*
* @param returnGeneratedKey 是否返回自增键序列
* @param keyTypeClass 自增序列的数字类型
* @param <N> 自增键序列类型 {@link Number}
* @return {@link SQLUpdateAction}
*/
SQLUpdateAction setReturnGeneratedKey(boolean returnGeneratedKey);
<N extends Number> SQLUpdateAction<N> returnGeneratedKey(Class<N> keyTypeClass);
}

View File

@ -2,7 +2,7 @@ package cc.carm.lib.easysql.api.builder;
import cc.carm.lib.easysql.api.SQLAction;
public interface DeleteBuilder extends ConditionalBuilder<DeleteBuilder, SQLAction<Long>> {
public interface DeleteBuilder extends ConditionalBuilder<DeleteBuilder, SQLAction<Integer>> {
String getTableName();

View File

@ -10,13 +10,13 @@ import org.jetbrains.annotations.Nullable;
public interface TableAlterBuilder extends SQLBuilder {
SQLAction<Long> renameTo(@NotNull String newTableName);
SQLAction<Integer> renameTo(@NotNull String newTableName);
SQLAction<Long> changeComment(@NotNull String newTableComment);
SQLAction<Integer> changeComment(@NotNull String newTableComment);
SQLAction<Long> setAutoIncrementIndex(int index);
SQLAction<Integer> setAutoIncrementIndex(int index);
SQLAction<Long> addIndex(@NotNull IndexType indexType, @Nullable String indexName,
SQLAction<Integer> addIndex(@NotNull IndexType indexType, @Nullable String indexName,
@NotNull String columnName, @NotNull String... moreColumns);
/**
@ -25,7 +25,7 @@ public interface TableAlterBuilder extends SQLBuilder {
* @param indexName 索引名
* @return {@link SQLUpdateAction}
*/
SQLAction<Long> dropIndex(@NotNull String indexName);
SQLAction<Integer> dropIndex(@NotNull String indexName);
/**
* 为该表移除一个外键
@ -33,14 +33,14 @@ public interface TableAlterBuilder extends SQLBuilder {
* @param keySymbol 外键名
* @return {@link SQLUpdateAction}
*/
SQLAction<Long> dropForeignKey(@NotNull String keySymbol);
SQLAction<Integer> dropForeignKey(@NotNull String keySymbol);
/**
* 为该表移除主键(须添加新主键)
*
* @return {@link SQLUpdateAction}
*/
SQLAction<Long> dropPrimaryKey();
SQLAction<Integer> dropPrimaryKey();
/**
* 为表添加一列
@ -49,7 +49,7 @@ public interface TableAlterBuilder extends SQLBuilder {
* @param settings 列的相关设定
* @return {@link SQLUpdateAction}
*/
default SQLAction<Long> addColumn(@NotNull String columnName, @NotNull String settings) {
default SQLAction<Integer> addColumn(@NotNull String columnName, @NotNull String settings) {
return addColumn(columnName, settings, null);
}
@ -63,21 +63,21 @@ public interface TableAlterBuilder extends SQLBuilder {
* <p> 若为 "" 则置于首行
* @return {@link SQLUpdateAction}
*/
SQLAction<Long> addColumn(@NotNull String columnName, @NotNull String settings, @Nullable String afterColumn);
SQLAction<Integer> addColumn(@NotNull String columnName, @NotNull String settings, @Nullable String afterColumn);
SQLAction<Long> renameColumn(@NotNull String columnName, @NotNull String newName);
SQLAction<Integer> renameColumn(@NotNull String columnName, @NotNull String newName);
SQLAction<Long> modifyColumn(@NotNull String columnName, @NotNull String settings);
SQLAction<Integer> modifyColumn(@NotNull String columnName, @NotNull String settings);
default SQLAction<Long> modifyColumn(@NotNull String columnName, @NotNull String columnSettings, @NotNull String afterColumn) {
default SQLAction<Integer> modifyColumn(@NotNull String columnName, @NotNull String columnSettings, @NotNull String afterColumn) {
return modifyColumn(columnName, columnSettings + " AFTER `" + afterColumn + "`");
}
SQLAction<Long> removeColumn(@NotNull String columnName);
SQLAction<Integer> removeColumn(@NotNull String columnName);
SQLAction<Long> setColumnDefault(@NotNull String columnName, @NotNull String defaultValue);
SQLAction<Integer> setColumnDefault(@NotNull String columnName, @NotNull String defaultValue);
SQLAction<Long> removeColumnDefault(@NotNull String columnName);
SQLAction<Integer> removeColumnDefault(@NotNull String columnName);
/**
* 为该表添加一个自增列
@ -90,7 +90,7 @@ public interface TableAlterBuilder extends SQLBuilder {
* @param unsigned 是否采用 UNSIGNED (即无负数可以增加自增键的最高数建议为true)
* @return {@link TableCreateBuilder}
*/
default SQLAction<Long> addAutoIncrementColumn(@NotNull String columnName, @Nullable NumberType numberType,
default SQLAction<Integer> addAutoIncrementColumn(@NotNull String columnName, @Nullable NumberType numberType,
boolean primary, boolean unsigned) {
return addColumn(columnName,
(numberType == null ? NumberType.INT : numberType).name()
@ -109,7 +109,7 @@ public interface TableAlterBuilder extends SQLBuilder {
* @param numberType 数字类型若省缺则为 {@link NumberType#INT}
* @return {@link TableAlterBuilder}
*/
default SQLAction<Long> addAutoIncrementColumn(@NotNull String columnName, @NotNull NumberType numberType) {
default SQLAction<Integer> addAutoIncrementColumn(@NotNull String columnName, @NotNull NumberType numberType) {
return addAutoIncrementColumn(columnName, numberType, false, true);
}
@ -122,7 +122,7 @@ public interface TableAlterBuilder extends SQLBuilder {
* @param columnName 列名
* @return {@link TableAlterBuilder}
*/
default SQLAction<Long> addAutoIncrementColumn(@NotNull String columnName) {
default SQLAction<Integer> addAutoIncrementColumn(@NotNull String columnName) {
return addAutoIncrementColumn(columnName, NumberType.INT);
}

View File

@ -21,7 +21,7 @@ public interface TableCreateBuilder extends SQLBuilder {
*
* @return {@link SQLUpdateAction}
*/
SQLUpdateAction build();
SQLUpdateAction<Integer> build();
@NotNull String getTableName();

View File

@ -6,7 +6,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.LinkedHashMap;
public interface UpdateBuilder extends ConditionalBuilder<UpdateBuilder, SQLAction<Long>> {
public interface UpdateBuilder extends ConditionalBuilder<UpdateBuilder, SQLAction<Integer>> {
String getTableName();

View File

@ -134,10 +134,10 @@ public class EasySQLDemo {
public void sqlInsert(SQLManager sqlManager) {
// 同步SQL插入 不使用try-catch的情况下返回的数值可能为空
Long id = sqlManager.createInsert("users")
int id = sqlManager.createInsert("users")
.setColumnNames("username", "phone", "email", "registerTime")
.setParams("CarmJos", "18888888888", "carm@carm.cc", TimeDateUtils.getCurrentTime())
.setReturnGeneratedKey(true)// 设定在后续返回自增主键
.returnGeneratedKey() // 设定在后续返回自增主键
.execute((exception, action) -> {
// 处理异常
System.out.println("#" + action.getShortID() + " -> " + action.getSQLContent());
@ -145,7 +145,7 @@ public class EasySQLDemo {
});
try {
Long userID = sqlManager.createInsert("users")
int userID = sqlManager.createInsert("users")
.setColumnNames("username", "phone", "email", "registerTime")
.setParams("CarmJos", "18888888888", "carm@carm.cc", TimeDateUtils.getCurrentTime())
.returnGeneratedKey().execute();

View File

@ -8,6 +8,7 @@ import com.zaxxer.hikari.HikariDataSource;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import java.math.BigInteger;
import java.util.LinkedHashSet;
import java.util.Set;
@ -16,7 +17,6 @@ public class EasySQLTest {
@Test
public void onTest() {
HikariConfig config = new HikariConfig();
config.setDriverClassName("org.h2.Driver");
config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MYSQL;");
@ -30,9 +30,10 @@ public class EasySQLTest {
// tests.add(new TableAlterTest());
// tests.add(new TableRenameTest());
// tests.add(new QueryNotCloseTest());
tests.add(new QueryCloseTest());
tests.add(new SQLUpdateBatchTests());
tests.add(new SQLUpdateReturnKeysTest());
tests.add(new QueryCloseTest());
tests.add(new QueryFunctionTest());
// tests.add(new DeleteTest());

View File

@ -4,6 +4,7 @@ import cc.carm.lib.easysql.TestHandler;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.SQLQuery;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -24,17 +25,11 @@ public class QueryCloseTest extends TestHandler {
System.out.printf(
"id: %d username: %s%n",
resultSet.getInt("id"),
resultSet.getObject("id", BigInteger.class),
resultSet.getString("username")
);
}
try {
Thread.sleep(500L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -28,6 +28,7 @@ public class SQLUpdateBatchTests extends TestHandler {
List<Long> updates = sqlManager.createInsertBatch("test_user_table")
.setColumnNames("uuid", "username", "age")
.setAllParams(generateParams())
.returnGeneratedKeys(Long.class)
.execute();
System.out.println("changes " + Arrays.toString(updates.toArray()));

View File

@ -11,10 +11,10 @@ public class SQLUpdateReturnKeysTest extends SQLUpdateBatchTests {
@Override
public void onTest(SQLManager sqlManager) throws SQLException {
List<Long> generatedKeys = sqlManager.createInsertBatch("test_user_table")
List<Integer> generatedKeys = sqlManager.createInsertBatch("test_user_table")
.setColumnNames("uuid", "username", "age")
.setAllParams(generateParams())
.returnGeneratedKeys()
.returnGeneratedKeys(Integer.class)
.execute();
System.out.println("generated " + Arrays.toString(generatedKeys.toArray()));

View File

@ -12,22 +12,33 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
public class PreparedSQLBatchUpdateActionImpl
extends AbstractSQLAction<List<Long>>
implements PreparedSQLUpdateBatchAction {
public class PreparedSQLBatchUpdateActionImpl<T extends Number>
extends AbstractSQLAction<List<T>>
implements PreparedSQLUpdateBatchAction<T> {
boolean returnKeys = false;
@NotNull List<Object[]> allParams;
@NotNull List<Object[]> allParams = new ArrayList<>();
public PreparedSQLBatchUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
protected final @NotNull Class<T> numberClass;
public PreparedSQLBatchUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull Class<T> numberClass,
@NotNull String sql) {
super(manager, sql);
this.numberClass = numberClass;
this.allParams = new ArrayList<>();
}
public PreparedSQLBatchUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull Class<T> numberClass,
@NotNull UUID uuid, @NotNull String sql) {
super(manager, sql, uuid);
this.numberClass = numberClass;
}
@Override
public PreparedSQLUpdateBatchAction setAllParams(Iterable<Object[]> allParams) {
public PreparedSQLBatchUpdateActionImpl<T> setAllParams(Iterable<Object[]> allParams) {
List<Object[]> paramsList = new ArrayList<>();
allParams.forEach(paramsList::add);
this.allParams = paramsList;
@ -35,35 +46,40 @@ public class PreparedSQLBatchUpdateActionImpl
}
@Override
public PreparedSQLUpdateBatchAction addParamsBatch(Object... params) {
public PreparedSQLBatchUpdateActionImpl<T> addParamsBatch(Object... params) {
this.allParams.add(params);
return this;
}
@Override
public PreparedSQLUpdateBatchAction setReturnGeneratedKeys(boolean returnGeneratedKey) {
this.returnKeys = returnGeneratedKey;
public PreparedSQLBatchUpdateActionImpl<T> returnGeneratedKeys() {
this.returnKeys = true;
return this;
}
@Override
public @NotNull List<Long> execute() throws SQLException {
public <N extends Number> PreparedSQLBatchUpdateActionImpl<N> returnGeneratedKeys(Class<N> keyTypeClass) {
return new PreparedSQLBatchUpdateActionImpl<>(getManager(), keyTypeClass, getActionUUID(), getSQLContent())
.setAllParams(allParams).returnGeneratedKeys();
}
@Override
public @NotNull List<T> execute() throws SQLException {
debugMessage(allParams);
try (Connection connection = getManager().getConnection()) {
try (PreparedStatement statement = StatementUtil.createPrepareStatementBatch(
connection, getSQLContent(), allParams, returnKeys
)) {
int[] executed = statement.executeBatch();
if (!returnKeys) {
return Arrays.stream(executed).mapToLong(Long::valueOf).boxed().collect(Collectors.toList());
return Arrays.stream(executed).mapToObj(numberClass::cast).collect(Collectors.toList());
} else {
try (ResultSet resultSet = statement.getGeneratedKeys()) {
List<Long> generatedKeys = new ArrayList<>();
List<T> generatedKeys = new ArrayList<>();
while (resultSet.next()) {
generatedKeys.add(resultSet.getLong(1));
generatedKeys.add(resultSet.getObject(1, numberClass));
}
return generatedKeys;
}

View File

@ -1,6 +1,7 @@
package cc.carm.lib.easysql.action;
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
import cc.carm.lib.easysql.api.action.SQLUpdateAction;
import cc.carm.lib.easysql.manager.SQLManagerImpl;
import cc.carm.lib.easysql.util.StatementUtil;
import org.jetbrains.annotations.NotNull;
@ -13,36 +14,45 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
public class PreparedSQLUpdateActionImpl
extends SQLUpdateActionImpl
implements PreparedSQLUpdateAction {
public class PreparedSQLUpdateActionImpl<T extends Number>
extends SQLUpdateActionImpl<T>
implements PreparedSQLUpdateAction<T> {
Object[] params;
public PreparedSQLUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
this(manager, sql, (Object[]) null);
public PreparedSQLUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull Class<T> numberClass,
@NotNull String sql) {
this(manager, numberClass, sql, (Object[]) null);
}
public PreparedSQLUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql,
@Nullable List<Object> params) {
this(manager, sql, params == null ? null : params.toArray());
public PreparedSQLUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull Class<T> numberClass,
@NotNull String sql, @Nullable List<Object> params) {
this(manager, numberClass, sql, params == null ? null : params.toArray());
}
public PreparedSQLUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql,
@Nullable Object[] params) {
super(manager, sql);
public PreparedSQLUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull Class<T> numberClass,
@NotNull String sql, @Nullable Object[] params) {
super(manager, numberClass, sql);
this.params = params;
}
public PreparedSQLUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull Class<T> numberClass,
@NotNull UUID uuid, @NotNull String sql,
Object[] params) {
super(manager, numberClass, uuid, sql);
this.params = params;
}
@Override
public PreparedSQLUpdateActionImpl setParams(Object... params) {
public PreparedSQLUpdateActionImpl<T> setParams(Object... params) {
this.params = params;
return this;
}
@Override
public PreparedSQLUpdateAction setParams(@Nullable Iterable<Object> params) {
public PreparedSQLUpdateActionImpl<T> setParams(@Nullable Iterable<Object> params) {
if (params == null) {
return setParams((Object[]) null);
} else {
@ -53,7 +63,7 @@ public class PreparedSQLUpdateActionImpl
}
@Override
public @NotNull Long execute() throws SQLException {
public @NotNull T execute() throws SQLException {
debugMessage(Collections.singletonList(params));
try (Connection connection = getManager().getConnection()) {
@ -63,10 +73,10 @@ public class PreparedSQLUpdateActionImpl
)) {
int changes = statement.executeUpdate();
if (!returnGeneratedKeys) return (long) changes;
if (!returnGeneratedKeys) return numberClass.cast(changes);
else {
try (ResultSet resultSet = statement.getGeneratedKeys()) {
return resultSet.next() ? resultSet.getLong(1) : -1;
return resultSet.next() ? resultSet.getObject(1, numberClass) : numberClass.cast(0);
}
}
@ -75,4 +85,10 @@ public class PreparedSQLUpdateActionImpl
}
@Override
public <N extends Number> SQLUpdateAction<N> returnGeneratedKey(Class<N> keyTypeClass) {
PreparedSQLUpdateActionImpl<N> newAction = new PreparedSQLUpdateActionImpl<>(getManager(), keyTypeClass, getActionUUID(), getSQLContent(), params);
newAction.returnGeneratedKey();
return newAction;
}
}

View File

@ -9,42 +9,57 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.UUID;
public class SQLUpdateActionImpl
extends AbstractSQLAction<Long>
implements SQLUpdateAction {
public class SQLUpdateActionImpl<T extends Number>
extends AbstractSQLAction<T>
implements SQLUpdateAction<T> {
boolean returnGeneratedKeys = false;
protected final @NotNull Class<T> numberClass;
public SQLUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
protected boolean returnGeneratedKeys = false;
public SQLUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull Class<T> numberClass,
@NotNull String sql) {
super(manager, sql);
this.numberClass = numberClass;
}
public SQLUpdateActionImpl(@NotNull SQLManagerImpl manager, @NotNull Class<T> numberClass,
@NotNull UUID uuid, @NotNull String sql) {
super(manager, sql, uuid);
this.numberClass = numberClass;
}
@Override
public @NotNull Long execute() throws SQLException {
public @NotNull T execute() throws SQLException {
debugMessage(new ArrayList<>());
try (Connection connection = getManager().getConnection()) {
try (Statement statement = connection.createStatement()) {
if (!returnGeneratedKeys) {
return (long) statement.executeUpdate(getSQLContent());
return numberClass.cast(statement.executeUpdate(getSQLContent()));
} else {
statement.executeUpdate(getSQLContent(), Statement.RETURN_GENERATED_KEYS);
try (ResultSet resultSet = statement.getGeneratedKeys()) {
return resultSet.next() ? resultSet.getLong(1) : -1;
return resultSet.next() ? resultSet.getObject(1, numberClass) : numberClass.cast(0);
}
}
}
}
}
@Override
public SQLUpdateAction setReturnGeneratedKey(boolean returnGeneratedKey) {
this.returnGeneratedKeys = returnGeneratedKey;
public SQLUpdateAction<T> returnGeneratedKey() {
this.returnGeneratedKeys = true;
return this;
}
@Override
public <N extends Number> SQLUpdateAction<N> returnGeneratedKey(Class<N> keyTypeClass) {
return new SQLUpdateActionImpl<>(getManager(), keyTypeClass, getActionUUID(), getSQLContent()).returnGeneratedKey();
}
}

View File

@ -12,7 +12,7 @@ import java.util.Objects;
import static cc.carm.lib.easysql.api.SQLBuilder.withBackQuote;
public class DeleteBuilderImpl
extends AbstractConditionalBuilder<DeleteBuilder, SQLAction<Long>>
extends AbstractConditionalBuilder<DeleteBuilder, SQLAction<Integer>>
implements DeleteBuilder {
protected final String tableName;
@ -24,7 +24,7 @@ public class DeleteBuilderImpl
}
@Override
public PreparedSQLUpdateAction build() {
public PreparedSQLUpdateAction<Integer> build() {
StringBuilder sqlBuilder = new StringBuilder();
@ -33,9 +33,9 @@ public class DeleteBuilderImpl
if (hasConditions()) sqlBuilder.append(" ").append(buildConditionSQL());
if (limit > 0) sqlBuilder.append(" ").append(buildLimitSQL());
return new PreparedSQLUpdateActionImpl(
getManager(), sqlBuilder.toString(),
hasConditionParams() ? getConditionParams() : null
return new PreparedSQLUpdateActionImpl<>(
getManager(), Integer.class, sqlBuilder.toString(),
(hasConditionParams() ? getConditionParams() : null)
);
}

View File

@ -28,31 +28,25 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
}
@Override
public SQLAction<Long> renameTo(@NotNull String newTableName) {
public SQLAction<Integer> renameTo(@NotNull String newTableName) {
Objects.requireNonNull(newTableName, "table name could not be null");
return new SQLUpdateActionImpl(getManager(),
"ALTER TABLE " + withBackQuote(tableName) + " RENAME TO " + withBackQuote(newTableName)
);
return createAction("ALTER TABLE " + withBackQuote(tableName) + " RENAME TO " + withBackQuote(newTableName));
}
@Override
public SQLAction<Long> changeComment(@NotNull String newTableComment) {
public SQLAction<Integer> changeComment(@NotNull String newTableComment) {
Objects.requireNonNull(newTableComment, "table comment could not be null");
return new SQLUpdateActionImpl(getManager(),
"ALTER TABLE " + withBackQuote(getTableName()) + " COMMENT " + withQuote(newTableComment)
);
return createAction("ALTER TABLE " + withBackQuote(getTableName()) + " COMMENT " + withQuote(newTableComment));
}
@Override
public SQLAction<Long> setAutoIncrementIndex(int index) {
return new SQLUpdateActionImpl(getManager(),
"ALTER TABLE " + withBackQuote(getTableName()) + " AUTO_INCREMENT=" + index
);
public SQLAction<Integer> setAutoIncrementIndex(int index) {
return createAction("ALTER TABLE " + withBackQuote(getTableName()) + " AUTO_INCREMENT=" + index);
}
@Override
public SQLAction<Long> addIndex(@NotNull IndexType indexType, @Nullable String indexName,
@NotNull String columnName, @NotNull String... moreColumns) {
public SQLAction<Integer> addIndex(@NotNull IndexType indexType, @Nullable String indexName,
@NotNull String columnName, @NotNull String... moreColumns) {
Objects.requireNonNull(indexType, "indexType could not be null");
Objects.requireNonNull(columnName, "column names could not be null");
Objects.requireNonNull(moreColumns, "column names could not be null");
@ -63,7 +57,7 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
}
@Override
public SQLAction<Long> dropIndex(@NotNull String indexName) {
public SQLAction<Integer> dropIndex(@NotNull String indexName) {
Objects.requireNonNull(indexName, "indexName could not be null");
return createAction(
"ALTER TABLE " + withBackQuote(getTableName()) + " DROP INDEX " + withBackQuote(indexName)
@ -71,7 +65,7 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
}
@Override
public SQLAction<Long> dropForeignKey(@NotNull String keySymbol) {
public SQLAction<Integer> dropForeignKey(@NotNull String keySymbol) {
Objects.requireNonNull(keySymbol, "keySymbol could not be null");
return createAction(
"ALTER TABLE " + withBackQuote(getTableName()) + " DROP FOREIGN KEY " + withBackQuote(keySymbol)
@ -79,14 +73,14 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
}
@Override
public SQLAction<Long> dropPrimaryKey() {
public SQLAction<Integer> dropPrimaryKey() {
return createAction(
"ALTER TABLE " + withBackQuote(getTableName()) + " DROP PRIMARY KEY"
);
}
@Override
public SQLAction<Long> addColumn(@NotNull String columnName, @NotNull String settings, @Nullable String afterColumn) {
public SQLAction<Integer> addColumn(@NotNull String columnName, @NotNull String settings, @Nullable String afterColumn) {
Objects.requireNonNull(columnName, "columnName could not be null");
Objects.requireNonNull(settings, "settings could not be null");
String orderSettings = null;
@ -105,7 +99,7 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
}
@Override
public SQLAction<Long> renameColumn(@NotNull String columnName, @NotNull String newName) {
public SQLAction<Integer> renameColumn(@NotNull String columnName, @NotNull String newName) {
Objects.requireNonNull(columnName, "columnName could not be null");
Objects.requireNonNull(newName, "please specify new column name");
return createAction(
@ -114,7 +108,7 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
}
@Override
public SQLAction<Long> modifyColumn(@NotNull String columnName, @NotNull String settings) {
public SQLAction<Integer> modifyColumn(@NotNull String columnName, @NotNull String settings) {
Objects.requireNonNull(columnName, "columnName could not be null");
Objects.requireNonNull(settings, "please specify new column settings");
return createAction(
@ -123,7 +117,7 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
}
@Override
public SQLAction<Long> removeColumn(@NotNull String columnName) {
public SQLAction<Integer> removeColumn(@NotNull String columnName) {
Objects.requireNonNull(columnName, "columnName could not be null");
return createAction(
"ALTER TABLE " + withBackQuote(getTableName()) + " DROP " + withBackQuote(columnName)
@ -131,7 +125,7 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
}
@Override
public SQLAction<Long> setColumnDefault(@NotNull String columnName, @NotNull String defaultValue) {
public SQLAction<Integer> setColumnDefault(@NotNull String columnName, @NotNull String defaultValue) {
Objects.requireNonNull(columnName, "columnName could not be null");
Objects.requireNonNull(defaultValue, "defaultValue could not be null, if you need to remove the default value, please use #removeColumnDefault().");
return createAction(
@ -140,14 +134,14 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
}
@Override
public SQLAction<Long> removeColumnDefault(@NotNull String columnName) {
public SQLAction<Integer> removeColumnDefault(@NotNull String columnName) {
Objects.requireNonNull(columnName, "columnName could not be null");
return createAction(
"ALTER TABLE " + withBackQuote(getTableName()) + " ALTER " + withBackQuote(columnName) + " DROP DEFAULT"
);
}
private SQLUpdateActionImpl createAction(@NotNull String sql) {
return new SQLUpdateActionImpl(getManager(), sql);
private SQLUpdateActionImpl<Integer> createAction(@NotNull String sql) {
return new SQLUpdateActionImpl<>(getManager(), Integer.class, sql);
}
}

View File

@ -75,7 +75,7 @@ public class TableCreateBuilderImpl extends AbstractSQLBuilder implements TableC
}
@Override
public SQLUpdateAction build() {
public SQLUpdateAction<Integer> build() {
StringBuilder createSQL = new StringBuilder();
createSQL.append("CREATE TABLE IF NOT EXISTS ").append(withBackQuote(tableName));
createSQL.append("(");
@ -94,7 +94,7 @@ public class TableCreateBuilderImpl extends AbstractSQLBuilder implements TableC
createSQL.append(" COMMENT ").append(withQuote(tableComment));
}
return new SQLUpdateActionImpl(getManager(), createSQL.toString());
return new SQLUpdateActionImpl<>(getManager(), Integer.class, createSQL.toString());
}
@Override

View File

@ -13,7 +13,7 @@ import java.util.*;
import static cc.carm.lib.easysql.api.SQLBuilder.withBackQuote;
public class UpdateBuilderImpl
extends AbstractConditionalBuilder<UpdateBuilder, SQLAction<Long>>
extends AbstractConditionalBuilder<UpdateBuilder, SQLAction<Integer>>
implements UpdateBuilder {
protected final @NotNull String tableName;
@ -27,7 +27,7 @@ public class UpdateBuilderImpl
}
@Override
public PreparedSQLUpdateAction build() {
public PreparedSQLUpdateAction<Integer> build() {
StringBuilder sqlBuilder = new StringBuilder();
@ -47,7 +47,7 @@ public class UpdateBuilderImpl
if (limit > 0) sqlBuilder.append(" ").append(buildLimitSQL());
return new PreparedSQLUpdateActionImpl(getManager(), sqlBuilder.toString(), allParams);
return new PreparedSQLUpdateActionImpl<>(getManager(), Integer.class, sqlBuilder.toString(), allParams);
}
@Override

View File

@ -120,20 +120,18 @@ public class SQLManagerImpl implements SQLManager {
}
@Override
public Long executeSQL(String sql) {
return new SQLUpdateActionImpl(this, sql).execute(null);
public Integer executeSQL(String sql) {
return new SQLUpdateActionImpl<>(this, Integer.class, sql).execute(null);
}
@Override
public Long executeSQL(String sql, Object[] params) {
return new PreparedSQLUpdateActionImpl(this, sql, params).execute(null);
public Integer executeSQL(String sql, Object[] params) {
return new PreparedSQLUpdateActionImpl<>(this, Integer.class, sql, params).execute(null);
}
@Override
public List<Long> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch) {
return new PreparedSQLBatchUpdateActionImpl(this, sql)
.setAllParams(paramsBatch)
.execute(null);
public List<Integer> executeSQLBatch(String sql, Iterable<Object[]> paramsBatch) {
return new PreparedSQLBatchUpdateActionImpl<>(this, Integer.class, sql).setAllParams(paramsBatch).execute(null);
}
@Override
@ -174,41 +172,41 @@ public class SQLManagerImpl implements SQLManager {
}
@Override
public InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch(@NotNull String tableName) {
return new InsertBuilderImpl<PreparedSQLUpdateBatchAction>(this, tableName) {
public InsertBuilder<PreparedSQLUpdateBatchAction<Integer>> createInsertBatch(@NotNull String tableName) {
return new InsertBuilderImpl<PreparedSQLUpdateBatchAction<Integer>>(this, tableName) {
@Override
public PreparedSQLUpdateBatchAction setColumnNames(List<String> columnNames) {
return new PreparedSQLBatchUpdateActionImpl(getManager(), buildSQL(getTableName(), columnNames));
public PreparedSQLUpdateBatchAction<Integer> setColumnNames(List<String> columnNames) {
return new PreparedSQLBatchUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), columnNames));
}
};
}
@Override
public InsertBuilder<PreparedSQLUpdateAction> createInsert(@NotNull String tableName) {
return new InsertBuilderImpl<PreparedSQLUpdateAction>(this, tableName) {
public InsertBuilder<PreparedSQLUpdateAction<Integer>> createInsert(@NotNull String tableName) {
return new InsertBuilderImpl<PreparedSQLUpdateAction<Integer>>(this, tableName) {
@Override
public PreparedSQLUpdateAction setColumnNames(List<String> columnNames) {
return new PreparedSQLUpdateActionImpl(getManager(), buildSQL(getTableName(), columnNames));
public PreparedSQLUpdateAction<Integer> setColumnNames(List<String> columnNames) {
return new PreparedSQLUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), columnNames));
}
};
}
@Override
public ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch(@NotNull String tableName) {
return new ReplaceBuilderImpl<PreparedSQLUpdateBatchAction>(this, tableName) {
public ReplaceBuilder<PreparedSQLUpdateBatchAction<Integer>> createReplaceBatch(@NotNull String tableName) {
return new ReplaceBuilderImpl<PreparedSQLUpdateBatchAction<Integer>>(this, tableName) {
@Override
public PreparedSQLUpdateBatchAction setColumnNames(List<String> columnNames) {
return new PreparedSQLBatchUpdateActionImpl(getManager(), buildSQL(getTableName(), columnNames));
public PreparedSQLUpdateBatchAction<Integer> setColumnNames(List<String> columnNames) {
return new PreparedSQLBatchUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), columnNames));
}
};
}
@Override
public ReplaceBuilder<PreparedSQLUpdateAction> createReplace(@NotNull String tableName) {
return new ReplaceBuilderImpl<PreparedSQLUpdateAction>(this, tableName) {
public ReplaceBuilder<PreparedSQLUpdateAction<Integer>> createReplace(@NotNull String tableName) {
return new ReplaceBuilderImpl<PreparedSQLUpdateAction<Integer>>(this, tableName) {
@Override
public PreparedSQLUpdateAction setColumnNames(List<String> columnNames) {
return new PreparedSQLUpdateActionImpl(getManager(), buildSQL(getTableName(), columnNames));
public PreparedSQLUpdateAction<Integer> setColumnNames(List<String> columnNames) {
return new PreparedSQLUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), columnNames));
}
};
}