This commit is contained in:
YuTian 2024-07-17 18:49:12 +08:00
parent fa51661a52
commit 3c454b0195
3 changed files with 20 additions and 4 deletions

View File

@ -9,6 +9,10 @@ public interface InsertBuilder<T extends SQLAction<?>> {
String getTableName();
boolean isIgnore();
InsertBuilder setIgnore(boolean ignore);
T setColumnNames(List<String> columnNames);
default T setColumnNames(String... columnNames) {

View File

@ -16,6 +16,7 @@ public abstract class InsertBuilderImpl<T extends SQLAction<?>>
extends AbstractSQLBuilder implements InsertBuilder<T> {
protected final String tableName;
protected boolean ignore;
public InsertBuilderImpl(@NotNull SQLManagerImpl manager, String tableName) {
super(manager);
@ -23,8 +24,19 @@ public abstract class InsertBuilderImpl<T extends SQLAction<?>>
this.tableName = tableName;
}
protected static String buildSQL(String tableName, List<String> columnNames) {
return buildSQL("INSERT IGNORE INTO", tableName, columnNames);
@Override
public boolean isIgnore() {
return ignore;
}
@Override
public InsertBuilder setIgnore(boolean ignore) {
this.ignore = ignore;
return this;
}
protected static String buildSQL(String tableName, boolean ignore, List<String> columnNames) {
return buildSQL("INSERT "+(ignore ? "IGNORE " : "")+"INTO", tableName, columnNames);
}
protected static String buildSQL(String sqlPrefix, String tableName, List<String> columnNames) {

View File

@ -204,7 +204,7 @@ public class SQLManagerImpl implements SQLManager {
return new InsertBuilderImpl<PreparedSQLUpdateBatchAction<Integer>>(this, tableName) {
@Override
public PreparedSQLUpdateBatchAction<Integer> setColumnNames(List<String> columnNames) {
return new PreparedSQLBatchUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), columnNames));
return new PreparedSQLBatchUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), isIgnore(), columnNames));
}
};
}
@ -214,7 +214,7 @@ public class SQLManagerImpl implements SQLManager {
return new InsertBuilderImpl<PreparedSQLUpdateAction<Integer>>(this, tableName) {
@Override
public PreparedSQLUpdateAction<Integer> setColumnNames(List<String> columnNames) {
return new PreparedSQLUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), columnNames));
return new PreparedSQLUpdateActionImpl<>(getManager(), Integer.class, buildSQL(getTableName(), isIgnore(), columnNames));
}
};
}