mirror of
https://github.com/CarmJos/EasySQL.git
synced 2026-06-04 23:41:15 +08:00
[0.3.8] 执行相关优化
- 优化部分调用,替换制表符为空格。 - 补充残缺的 Objects.requireNonNull(); - 对于SQLQuery的auto-close额外判断ResultSet、Statement与Connection是否已关闭,避免重复关闭报错。
This commit is contained in:
@@ -11,6 +11,16 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
*/
|
*/
|
||||||
public interface SQLBuilder {
|
public interface SQLBuilder {
|
||||||
|
|
||||||
|
static @NotNull String withBackQuote(@NotNull String str) {
|
||||||
|
str = str.trim();
|
||||||
|
return !str.isEmpty() && str.charAt(0) == '`' && str.charAt(str.length() - 1) == '`' ? str : "`" + str + "`";
|
||||||
|
}
|
||||||
|
|
||||||
|
static @NotNull String withQuote(@NotNull String str) {
|
||||||
|
str = str.trim();
|
||||||
|
return !str.isEmpty() && str.charAt(0) == '\'' && str.charAt(str.length() - 1) == '\'' ? str : "'" + str + "'";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 得到承载该Builder的对应{@link SQLManager}
|
* 得到承载该Builder的对应{@link SQLManager}
|
||||||
*
|
*
|
||||||
@@ -18,15 +28,4 @@ public interface SQLBuilder {
|
|||||||
*/
|
*/
|
||||||
@NotNull SQLManager getManager();
|
@NotNull SQLManager getManager();
|
||||||
|
|
||||||
|
|
||||||
static @NotNull String withBackQuote(@NotNull String str) {
|
|
||||||
str = str.trim();
|
|
||||||
return str.startsWith("`") && str.endsWith("`") ? str : "`" + str + "`";
|
|
||||||
}
|
|
||||||
|
|
||||||
static @NotNull String withQuote(@NotNull String str) {
|
|
||||||
str = str.trim();
|
|
||||||
return str.startsWith("'") && str.endsWith("'") ? str : "'" + str + "'";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public interface SQLUpdateAction extends SQLAction<Integer> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 设定该操作是否返回自增键序列。
|
* 设定该操作是否返回自增键序列。
|
||||||
|
*
|
||||||
* @param returnGeneratedKey 是否返回自增键序列
|
* @param returnGeneratedKey 是否返回自增键序列
|
||||||
* @return {@link SQLUpdateAction}
|
* @return {@link SQLUpdateAction}
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@SuppressWarnings("UnusedReturnValue")
|
||||||
public interface SQLUpdateBatchAction extends SQLAction<List<Integer>> {
|
public interface SQLUpdateBatchAction extends SQLAction<List<Integer>> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public interface TableAlterBuilder extends SQLBuilder {
|
|||||||
|
|
||||||
SQLAction<Integer> setAutoIncrementIndex(int index);
|
SQLAction<Integer> setAutoIncrementIndex(int index);
|
||||||
|
|
||||||
SQLAction<Integer> addIndex(@NotNull IndexType indexType, @NotNull String indexName,
|
SQLAction<Integer> addIndex(@NotNull IndexType indexType, @Nullable String indexName,
|
||||||
@NotNull String columnName, @NotNull String... moreColumns);
|
@NotNull String columnName, @NotNull String... moreColumns);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+8
-9
@@ -12,26 +12,25 @@ import java.util.logging.Logger;
|
|||||||
public class DefaultSQLExceptionHandler implements SQLExceptionHandler {
|
public class DefaultSQLExceptionHandler implements SQLExceptionHandler {
|
||||||
|
|
||||||
private static @Nullable SQLExceptionHandler customDefaultHandler = null;
|
private static @Nullable SQLExceptionHandler customDefaultHandler = null;
|
||||||
|
private final Logger logger;
|
||||||
|
|
||||||
public static void setCustomHandler(@Nullable SQLExceptionHandler handler) {
|
public DefaultSQLExceptionHandler(Logger logger) {
|
||||||
DefaultSQLExceptionHandler.customDefaultHandler = handler;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @Nullable SQLExceptionHandler getCustomHandler() {
|
public static @Nullable SQLExceptionHandler getCustomHandler() {
|
||||||
return customDefaultHandler;
|
return customDefaultHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setCustomHandler(@Nullable SQLExceptionHandler handler) {
|
||||||
|
DefaultSQLExceptionHandler.customDefaultHandler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
public static @NotNull SQLExceptionHandler get(Logger logger) {
|
public static @NotNull SQLExceptionHandler get(Logger logger) {
|
||||||
if (getCustomHandler() != null) return getCustomHandler();
|
if (getCustomHandler() != null) return getCustomHandler();
|
||||||
else return new DefaultSQLExceptionHandler(logger);
|
else return new DefaultSQLExceptionHandler(logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Logger logger;
|
|
||||||
|
|
||||||
public DefaultSQLExceptionHandler(Logger logger) {
|
|
||||||
this.logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Logger getLogger() {
|
protected Logger getLogger() {
|
||||||
return logger;
|
return logger;
|
||||||
}
|
}
|
||||||
@@ -43,7 +42,7 @@ public class DefaultSQLExceptionHandler implements SQLExceptionHandler {
|
|||||||
getLogger().severe("Error when execute SQLs : ");
|
getLogger().severe("Error when execute SQLs : ");
|
||||||
int i = 1;
|
int i = 1;
|
||||||
for (String content : ((SQLUpdateBatchAction) sqlAction).getSQLContents()) {
|
for (String content : ((SQLUpdateBatchAction) sqlAction).getSQLContents()) {
|
||||||
getLogger().severe("#" + i + " {" + content + "}");
|
getLogger().severe(String.format("#%d {%s}", i, content));
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import java.text.SimpleDateFormat;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public class TimeDateUtils {
|
public class TimeDateUtils {
|
||||||
public static DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
public static final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
||||||
public TimeDateUtils() {
|
public TimeDateUtils() {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,17 +7,16 @@ import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public abstract class AbstractSQLAction<T> implements SQLAction<T> {
|
public abstract class AbstractSQLAction<T> implements SQLAction<T> {
|
||||||
|
|
||||||
|
protected final @NotNull String sqlContent;
|
||||||
private final @NotNull SQLManagerImpl sqlManager;
|
private final @NotNull SQLManagerImpl sqlManager;
|
||||||
|
|
||||||
private final @NotNull UUID uuid;
|
private final @NotNull UUID uuid;
|
||||||
private final long createTime;
|
private final long createTime;
|
||||||
|
|
||||||
protected @NotNull String sqlContent;
|
|
||||||
|
|
||||||
public AbstractSQLAction(@NotNull SQLManagerImpl manager, @NotNull String sql) {
|
public AbstractSQLAction(@NotNull SQLManagerImpl manager, @NotNull String sql) {
|
||||||
this(manager, sql, System.currentTimeMillis());
|
this(manager, sql, System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
@@ -32,6 +31,9 @@ public abstract class AbstractSQLAction<T> implements SQLAction<T> {
|
|||||||
|
|
||||||
public AbstractSQLAction(@NotNull SQLManagerImpl manager, @NotNull String sql,
|
public AbstractSQLAction(@NotNull SQLManagerImpl manager, @NotNull String sql,
|
||||||
@NotNull UUID uuid, long createTime) {
|
@NotNull UUID uuid, long createTime) {
|
||||||
|
Objects.requireNonNull(manager);
|
||||||
|
Objects.requireNonNull(sql);
|
||||||
|
Objects.requireNonNull(uuid);
|
||||||
this.sqlManager = manager;
|
this.sqlManager = manager;
|
||||||
this.sqlContent = sql;
|
this.sqlContent = sql;
|
||||||
this.uuid = uuid;
|
this.uuid = uuid;
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ public class SQLUpdateBatchActionImpl
|
|||||||
extends AbstractSQLAction<List<Integer>>
|
extends AbstractSQLAction<List<Integer>>
|
||||||
implements SQLUpdateBatchAction {
|
implements SQLUpdateBatchAction {
|
||||||
|
|
||||||
List<String> sqlContents = new ArrayList<>();
|
protected final List<String> sqlContents = new ArrayList<>();
|
||||||
|
|
||||||
public SQLUpdateBatchActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
|
public SQLUpdateBatchActionImpl(@NotNull SQLManagerImpl manager, @NotNull String sql) {
|
||||||
super(manager, sql);
|
super(manager, sql);
|
||||||
@@ -58,7 +58,7 @@ public class SQLUpdateBatchActionImpl
|
|||||||
@Override
|
@Override
|
||||||
protected void outputDebugMessage() {
|
protected void outputDebugMessage() {
|
||||||
getManager().debug("# " + getShortID() + " -> [");
|
getManager().debug("# " + getShortID() + " -> [");
|
||||||
for (String content : getSQLContents()) getManager().debug(" { " + content + " }");
|
for (String content : getSQLContents()) getManager().debug(String.format(" { %s }", content));
|
||||||
getManager().debug("]");
|
getManager().debug("]");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,15 @@ import cc.carm.lib.easysql.api.SQLBuilder;
|
|||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public abstract class AbstractSQLBuilder implements SQLBuilder {
|
public abstract class AbstractSQLBuilder implements SQLBuilder {
|
||||||
|
|
||||||
@NotNull SQLManagerImpl sqlManager;
|
@NotNull
|
||||||
|
final SQLManagerImpl sqlManager;
|
||||||
|
|
||||||
public AbstractSQLBuilder(@NotNull SQLManagerImpl manager) {
|
public AbstractSQLBuilder(@NotNull SQLManagerImpl manager) {
|
||||||
|
Objects.requireNonNull(manager, "SQLManager must not be null");
|
||||||
this.sqlManager = manager;
|
this.sqlManager = manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
@@ -53,6 +53,7 @@ public abstract class AbstractConditionalBuilder<B extends ConditionalBuilder<B,
|
|||||||
@NotNull String columnName, @NotNull String operator, @Nullable Object queryValue
|
@NotNull String columnName, @NotNull String operator, @Nullable Object queryValue
|
||||||
) {
|
) {
|
||||||
Objects.requireNonNull(columnName, "columnName could not be null");
|
Objects.requireNonNull(columnName, "columnName could not be null");
|
||||||
|
Objects.requireNonNull(operator, "operator could not be null (e.g. > or = or <) ");
|
||||||
addCondition(withBackQuote(columnName) + " " + operator + " ?");
|
addCondition(withBackQuote(columnName) + " " + operator + " ?");
|
||||||
this.conditionParams.add(queryValue);
|
this.conditionParams.add(queryValue);
|
||||||
return getThis();
|
return getThis();
|
||||||
@@ -62,6 +63,7 @@ public abstract class AbstractConditionalBuilder<B extends ConditionalBuilder<B,
|
|||||||
public B addCondition(
|
public B addCondition(
|
||||||
@NotNull String[] columnNames, @Nullable Object[] queryValues
|
@NotNull String[] columnNames, @Nullable Object[] queryValues
|
||||||
) {
|
) {
|
||||||
|
Objects.requireNonNull(columnNames, "columnName could not be null");
|
||||||
if (queryValues == null || columnNames.length != queryValues.length) {
|
if (queryValues == null || columnNames.length != queryValues.length) {
|
||||||
throw new RuntimeException("queryNames are not match with queryValues");
|
throw new RuntimeException("queryNames are not match with queryValues");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,16 +7,19 @@ import cc.carm.lib.easysql.api.builder.DeleteBuilder;
|
|||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import static cc.carm.lib.easysql.api.SQLBuilder.withBackQuote;
|
import static cc.carm.lib.easysql.api.SQLBuilder.withBackQuote;
|
||||||
|
|
||||||
public class DeleteBuilderImpl
|
public class DeleteBuilderImpl
|
||||||
extends AbstractConditionalBuilder<DeleteBuilder, SQLAction<Integer>>
|
extends AbstractConditionalBuilder<DeleteBuilder, SQLAction<Integer>>
|
||||||
implements DeleteBuilder {
|
implements DeleteBuilder {
|
||||||
|
|
||||||
String tableName;
|
protected final String tableName;
|
||||||
|
|
||||||
public DeleteBuilderImpl(@NotNull SQLManagerImpl manager, @NotNull String tableName) {
|
public DeleteBuilderImpl(@NotNull SQLManagerImpl manager, @NotNull String tableName) {
|
||||||
super(manager);
|
super(manager);
|
||||||
|
Objects.requireNonNull(tableName);
|
||||||
this.tableName = tableName;
|
this.tableName = tableName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,24 +8,30 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import static cc.carm.lib.easysql.api.SQLBuilder.withBackQuote;
|
import static cc.carm.lib.easysql.api.SQLBuilder.withBackQuote;
|
||||||
|
|
||||||
public abstract class InsertBuilderImpl<T extends SQLAction<?>>
|
public abstract class InsertBuilderImpl<T extends SQLAction<?>>
|
||||||
extends AbstractSQLBuilder implements InsertBuilder<T> {
|
extends AbstractSQLBuilder implements InsertBuilder<T> {
|
||||||
|
|
||||||
String tableName;
|
protected final String tableName;
|
||||||
|
|
||||||
public InsertBuilderImpl(@NotNull SQLManagerImpl manager, String tableName) {
|
public InsertBuilderImpl(@NotNull SQLManagerImpl manager, String tableName) {
|
||||||
super(manager);
|
super(manager);
|
||||||
|
Objects.requireNonNull(tableName);
|
||||||
this.tableName = tableName;
|
this.tableName = tableName;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static String buildSQL(String tableName, List<String> columnNames) {
|
protected static String buildSQL(String tableName, List<String> columnNames) {
|
||||||
|
return buildSQL("INSERT IGNORE INTO", tableName, columnNames);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static String buildSQL(String sqlPrefix, String tableName, List<String> columnNames) {
|
||||||
int valueLength = columnNames.size();
|
int valueLength = columnNames.size();
|
||||||
StringBuilder sqlBuilder = new StringBuilder();
|
StringBuilder sqlBuilder = new StringBuilder();
|
||||||
|
|
||||||
sqlBuilder.append("INSERT IGNORE INTO ").append(withBackQuote(tableName)).append("(");
|
sqlBuilder.append(sqlPrefix).append(" ").append(withBackQuote(tableName)).append("(");
|
||||||
Iterator<String> iterator = columnNames.iterator();
|
Iterator<String> iterator = columnNames.iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
sqlBuilder.append(withBackQuote(iterator.next()));
|
sqlBuilder.append(withBackQuote(iterator.next()));
|
||||||
|
|||||||
+4
-26
@@ -6,46 +6,24 @@ import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
|||||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static cc.carm.lib.easysql.api.SQLBuilder.withBackQuote;
|
|
||||||
|
|
||||||
public abstract class ReplaceBuilderImpl<T extends SQLAction<?>>
|
public abstract class ReplaceBuilderImpl<T extends SQLAction<?>>
|
||||||
extends AbstractSQLBuilder implements ReplaceBuilder<T> {
|
extends AbstractSQLBuilder implements ReplaceBuilder<T> {
|
||||||
|
|
||||||
String tableName;
|
protected final @NotNull String tableName;
|
||||||
|
|
||||||
public ReplaceBuilderImpl(@NotNull SQLManagerImpl manager, String tableName) {
|
public ReplaceBuilderImpl(@NotNull SQLManagerImpl manager, @NotNull String tableName) {
|
||||||
super(manager);
|
super(manager);
|
||||||
this.tableName = tableName;
|
this.tableName = tableName;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static String buildSQL(String tableName, List<String> columnNames) {
|
protected static String buildSQL(String tableName, List<String> columnNames) {
|
||||||
int valueLength = columnNames.size();
|
return InsertBuilderImpl.buildSQL("REPLACE INTO", tableName, columnNames);
|
||||||
StringBuilder sqlBuilder = new StringBuilder();
|
|
||||||
|
|
||||||
sqlBuilder.append("REPLACE INTO ").append(withBackQuote(tableName)).append("(");
|
|
||||||
Iterator<String> iterator = columnNames.iterator();
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
sqlBuilder.append(withBackQuote(iterator.next()));
|
|
||||||
if (iterator.hasNext()) sqlBuilder.append(", ");
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlBuilder.append(") VALUES (");
|
|
||||||
|
|
||||||
for (int i = 0; i < valueLength; i++) {
|
|
||||||
sqlBuilder.append("?");
|
|
||||||
if (i != valueLength - 1) {
|
|
||||||
sqlBuilder.append(", ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sqlBuilder.append(")");
|
|
||||||
return sqlBuilder.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getTableName() {
|
public @NotNull String getTableName() {
|
||||||
return tableName;
|
return tableName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-2
@@ -31,12 +31,13 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
|
|||||||
public SQLAction<Integer> renameTo(@NotNull String newTableName) {
|
public SQLAction<Integer> renameTo(@NotNull String newTableName) {
|
||||||
Objects.requireNonNull(newTableName, "table name could not be null");
|
Objects.requireNonNull(newTableName, "table name could not be null");
|
||||||
return new SQLUpdateActionImpl(getManager(),
|
return new SQLUpdateActionImpl(getManager(),
|
||||||
"ALTER TABLE " + withBackQuote(getTableName()) + " RENAME TO " + withBackQuote(newTableName) + ""
|
"ALTER TABLE " + withBackQuote(tableName) + " RENAME TO " + withBackQuote(newTableName)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SQLAction<Integer> changeComment(@NotNull String newTableComment) {
|
public SQLAction<Integer> changeComment(@NotNull String newTableComment) {
|
||||||
|
Objects.requireNonNull(newTableComment, "table comment could not be null");
|
||||||
return new SQLUpdateActionImpl(getManager(),
|
return new SQLUpdateActionImpl(getManager(),
|
||||||
"ALTER TABLE " + withBackQuote(getTableName()) + " COMMENT " + withQuote(newTableComment)
|
"ALTER TABLE " + withBackQuote(getTableName()) + " COMMENT " + withQuote(newTableComment)
|
||||||
);
|
);
|
||||||
@@ -50,7 +51,11 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SQLAction<Integer> addIndex(@NotNull IndexType indexType, @NotNull 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");
|
||||||
return createAction(
|
return createAction(
|
||||||
"ALTER TABLE " + withBackQuote(getTableName()) + " ADD "
|
"ALTER TABLE " + withBackQuote(getTableName()) + " ADD "
|
||||||
+ TableCreateBuilderImpl.buildIndexSettings(indexType, indexName, columnName, moreColumns)
|
+ TableCreateBuilderImpl.buildIndexSettings(indexType, indexName, columnName, moreColumns)
|
||||||
@@ -59,6 +64,7 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SQLAction<Integer> dropIndex(@NotNull String indexName) {
|
public SQLAction<Integer> dropIndex(@NotNull String indexName) {
|
||||||
|
Objects.requireNonNull(indexName, "indexName could not be null");
|
||||||
return createAction(
|
return createAction(
|
||||||
"ALTER TABLE " + withBackQuote(getTableName()) + " DROP INDEX " + withBackQuote(indexName)
|
"ALTER TABLE " + withBackQuote(getTableName()) + " DROP INDEX " + withBackQuote(indexName)
|
||||||
);
|
);
|
||||||
@@ -66,6 +72,7 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SQLAction<Integer> dropForeignKey(@NotNull String keySymbol) {
|
public SQLAction<Integer> dropForeignKey(@NotNull String keySymbol) {
|
||||||
|
Objects.requireNonNull(keySymbol, "keySymbol could not be null");
|
||||||
return createAction(
|
return createAction(
|
||||||
"ALTER TABLE " + withBackQuote(getTableName()) + " DROP FOREIGN KEY " + withBackQuote(keySymbol)
|
"ALTER TABLE " + withBackQuote(getTableName()) + " DROP FOREIGN KEY " + withBackQuote(keySymbol)
|
||||||
);
|
);
|
||||||
@@ -80,6 +87,8 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SQLAction<Integer> 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;
|
String orderSettings = null;
|
||||||
if (afterColumn != null) {
|
if (afterColumn != null) {
|
||||||
if (afterColumn.length() > 0) {
|
if (afterColumn.length() > 0) {
|
||||||
@@ -97,6 +106,8 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SQLAction<Integer> 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(
|
return createAction(
|
||||||
"ALTER TABLE " + withBackQuote(getTableName()) + " RENAME COLUMN " + withBackQuote(columnName) + " TO " + withBackQuote(newName)
|
"ALTER TABLE " + withBackQuote(getTableName()) + " RENAME COLUMN " + withBackQuote(columnName) + " TO " + withBackQuote(newName)
|
||||||
);
|
);
|
||||||
@@ -104,6 +115,8 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SQLAction<Integer> 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(
|
return createAction(
|
||||||
"ALTER TABLE " + withBackQuote(getTableName()) + " MODIFY COLUMN " + withBackQuote(columnName) + " " + settings
|
"ALTER TABLE " + withBackQuote(getTableName()) + " MODIFY COLUMN " + withBackQuote(columnName) + " " + settings
|
||||||
);
|
);
|
||||||
@@ -111,6 +124,7 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SQLAction<Integer> removeColumn(@NotNull String columnName) {
|
public SQLAction<Integer> removeColumn(@NotNull String columnName) {
|
||||||
|
Objects.requireNonNull(columnName, "columnName could not be null");
|
||||||
return createAction(
|
return createAction(
|
||||||
"ALTER TABLE " + withBackQuote(getTableName()) + " DROP " + withBackQuote(columnName)
|
"ALTER TABLE " + withBackQuote(getTableName()) + " DROP " + withBackQuote(columnName)
|
||||||
);
|
);
|
||||||
@@ -118,6 +132,8 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SQLAction<Integer> 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(
|
return createAction(
|
||||||
"ALTER TABLE " + withBackQuote(getTableName()) + " ALTER " + withBackQuote(columnName) + " SET DEFAULT " + defaultValue
|
"ALTER TABLE " + withBackQuote(getTableName()) + " ALTER " + withBackQuote(columnName) + " SET DEFAULT " + defaultValue
|
||||||
);
|
);
|
||||||
@@ -125,6 +141,7 @@ public class TableAlterBuilderImpl extends AbstractSQLBuilder implements TableAl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SQLAction<Integer> removeColumnDefault(@NotNull String columnName) {
|
public SQLAction<Integer> removeColumnDefault(@NotNull String columnName) {
|
||||||
|
Objects.requireNonNull(columnName, "columnName could not be null");
|
||||||
return createAction(
|
return createAction(
|
||||||
"ALTER TABLE " + withBackQuote(getTableName()) + " ALTER " + withBackQuote(columnName) + " DROP DEFAULT"
|
"ALTER TABLE " + withBackQuote(getTableName()) + " ALTER " + withBackQuote(columnName) + " DROP DEFAULT"
|
||||||
);
|
);
|
||||||
|
|||||||
+42
-31
@@ -14,6 +14,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import static cc.carm.lib.easysql.api.SQLBuilder.withBackQuote;
|
import static cc.carm.lib.easysql.api.SQLBuilder.withBackQuote;
|
||||||
import static cc.carm.lib.easysql.api.SQLBuilder.withQuote;
|
import static cc.carm.lib.easysql.api.SQLBuilder.withQuote;
|
||||||
@@ -21,11 +22,11 @@ import static cc.carm.lib.easysql.api.SQLBuilder.withQuote;
|
|||||||
public class TableCreateBuilderImpl extends AbstractSQLBuilder implements TableCreateBuilder {
|
public class TableCreateBuilderImpl extends AbstractSQLBuilder implements TableCreateBuilder {
|
||||||
|
|
||||||
protected final @NotNull String tableName;
|
protected final @NotNull String tableName;
|
||||||
|
@NotNull
|
||||||
|
final List<String> indexes = new ArrayList<>();
|
||||||
|
@NotNull
|
||||||
|
final List<String> foreignKeys = new ArrayList<>();
|
||||||
@NotNull List<String> columns = new ArrayList<>();
|
@NotNull List<String> columns = new ArrayList<>();
|
||||||
@NotNull List<String> indexes = new ArrayList<>();
|
|
||||||
@NotNull List<String> foreignKeys = new ArrayList<>();
|
|
||||||
|
|
||||||
@NotNull String tableSettings = defaultTablesSettings();
|
@NotNull String tableSettings = defaultTablesSettings();
|
||||||
@Nullable String tableComment;
|
@Nullable String tableComment;
|
||||||
|
|
||||||
@@ -34,6 +35,35 @@ public class TableCreateBuilderImpl extends AbstractSQLBuilder implements TableC
|
|||||||
this.tableName = tableName;
|
this.tableName = tableName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static String buildIndexSettings(@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");
|
||||||
|
StringBuilder indexBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
indexBuilder.append(indexType.getName()).append(" ");
|
||||||
|
if (indexName != null) {
|
||||||
|
indexBuilder.append(withBackQuote(indexName));
|
||||||
|
}
|
||||||
|
indexBuilder.append("(");
|
||||||
|
indexBuilder.append(withBackQuote(columnName));
|
||||||
|
|
||||||
|
if (moreColumns.length > 0) {
|
||||||
|
indexBuilder.append(", ");
|
||||||
|
|
||||||
|
for (int i = 0; i < moreColumns.length; i++) {
|
||||||
|
indexBuilder.append(withBackQuote(moreColumns[i]));
|
||||||
|
if (i != moreColumns.length - 1) indexBuilder.append(", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
indexBuilder.append(")");
|
||||||
|
|
||||||
|
return indexBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull String getTableName() {
|
public @NotNull String getTableName() {
|
||||||
return this.tableName;
|
return this.tableName;
|
||||||
@@ -69,6 +99,7 @@ public class TableCreateBuilderImpl extends AbstractSQLBuilder implements TableC
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TableCreateBuilder addColumn(@NotNull String column) {
|
public TableCreateBuilder addColumn(@NotNull String column) {
|
||||||
|
Objects.requireNonNull(column, "column could not be null");
|
||||||
this.columns.add(column);
|
this.columns.add(column);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -86,6 +117,7 @@ public class TableCreateBuilderImpl extends AbstractSQLBuilder implements TableC
|
|||||||
@Override
|
@Override
|
||||||
public TableCreateBuilder setIndex(@NotNull IndexType type, @Nullable String indexName,
|
public TableCreateBuilder setIndex(@NotNull IndexType type, @Nullable String indexName,
|
||||||
@NotNull String columnName, @NotNull String... moreColumns) {
|
@NotNull String columnName, @NotNull String... moreColumns) {
|
||||||
|
Objects.requireNonNull(columnName, "columnName could not be null");
|
||||||
this.indexes.add(buildIndexSettings(type, indexName, columnName, moreColumns));
|
this.indexes.add(buildIndexSettings(type, indexName, columnName, moreColumns));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -94,6 +126,10 @@ public class TableCreateBuilderImpl extends AbstractSQLBuilder implements TableC
|
|||||||
public TableCreateBuilder addForeignKey(@NotNull String tableColumn, @Nullable String constraintName,
|
public TableCreateBuilder addForeignKey(@NotNull String tableColumn, @Nullable String constraintName,
|
||||||
@NotNull String foreignTable, @NotNull String foreignColumn,
|
@NotNull String foreignTable, @NotNull String foreignColumn,
|
||||||
@Nullable ForeignKeyRule updateRule, @Nullable ForeignKeyRule deleteRule) {
|
@Nullable ForeignKeyRule updateRule, @Nullable ForeignKeyRule deleteRule) {
|
||||||
|
Objects.requireNonNull(tableColumn, "tableColumn could not be null");
|
||||||
|
Objects.requireNonNull(foreignTable, "foreignTable could not be null");
|
||||||
|
Objects.requireNonNull(foreignColumn, "foreignColumn could not be null");
|
||||||
|
|
||||||
StringBuilder keyBuilder = new StringBuilder();
|
StringBuilder keyBuilder = new StringBuilder();
|
||||||
|
|
||||||
keyBuilder.append("CONSTRAINT ");
|
keyBuilder.append("CONSTRAINT ");
|
||||||
@@ -115,12 +151,14 @@ public class TableCreateBuilderImpl extends AbstractSQLBuilder implements TableC
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TableCreateBuilder setColumns(@NotNull String... columns) {
|
public TableCreateBuilder setColumns(@NotNull String... columns) {
|
||||||
|
Objects.requireNonNull(columns, "columns could not be null");
|
||||||
this.columns = Arrays.asList(columns);
|
this.columns = Arrays.asList(columns);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TableCreateBuilder setTableSettings(@NotNull String settings) {
|
public TableCreateBuilder setTableSettings(@NotNull String settings) {
|
||||||
|
Objects.requireNonNull(settings, "settings could not be null");
|
||||||
this.tableSettings = settings;
|
this.tableSettings = settings;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -131,31 +169,4 @@ public class TableCreateBuilderImpl extends AbstractSQLBuilder implements TableC
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static String buildIndexSettings(@NotNull IndexType indexType, @Nullable String indexName,
|
|
||||||
@NotNull String columnName, @NotNull String... moreColumns) {
|
|
||||||
|
|
||||||
StringBuilder indexBuilder = new StringBuilder();
|
|
||||||
|
|
||||||
indexBuilder.append(indexType.getName()).append(" ");
|
|
||||||
if (indexName != null) {
|
|
||||||
indexBuilder.append(withBackQuote(indexName));
|
|
||||||
}
|
|
||||||
indexBuilder.append("(");
|
|
||||||
indexBuilder.append(withBackQuote(columnName));
|
|
||||||
|
|
||||||
if (moreColumns.length > 0) {
|
|
||||||
indexBuilder.append(", ");
|
|
||||||
|
|
||||||
for (int i = 0; i < moreColumns.length; i++) {
|
|
||||||
indexBuilder.append(withBackQuote(moreColumns[i]));
|
|
||||||
if (i != moreColumns.length - 1) indexBuilder.append(", ");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
indexBuilder.append(")");
|
|
||||||
|
|
||||||
return indexBuilder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -15,7 +15,8 @@ public class TableQueryBuilderImpl
|
|||||||
extends AbstractConditionalBuilder<TableQueryBuilder, PreparedQueryAction>
|
extends AbstractConditionalBuilder<TableQueryBuilder, PreparedQueryAction>
|
||||||
implements TableQueryBuilder {
|
implements TableQueryBuilder {
|
||||||
|
|
||||||
@NotNull String tableName;
|
|
||||||
|
protected final @NotNull String tableName;
|
||||||
|
|
||||||
String[] columns;
|
String[] columns;
|
||||||
|
|
||||||
@@ -68,6 +69,7 @@ public class TableQueryBuilderImpl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TableQueryBuilderImpl selectColumns(@NotNull String... columnNames) {
|
public TableQueryBuilderImpl selectColumns(@NotNull String... columnNames) {
|
||||||
|
Objects.requireNonNull(columnNames, "columnNames could not be null");
|
||||||
this.columns = columnNames;
|
this.columns = columnNames;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public class UpdateBuilderImpl
|
|||||||
extends AbstractConditionalBuilder<UpdateBuilder, SQLAction<Integer>>
|
extends AbstractConditionalBuilder<UpdateBuilder, SQLAction<Integer>>
|
||||||
implements UpdateBuilder {
|
implements UpdateBuilder {
|
||||||
|
|
||||||
String tableName;
|
protected final @NotNull String tableName;
|
||||||
|
|
||||||
@NotNull LinkedHashMap<String, Object> columnData;
|
@NotNull LinkedHashMap<String, Object> columnData;
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ public class UpdateBuilderImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getTableName() {
|
public @NotNull String getTableName() {
|
||||||
return tableName;
|
return tableName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,10 +28,9 @@ public class SQLManagerImpl implements SQLManager {
|
|||||||
|
|
||||||
private final Logger LOGGER;
|
private final Logger LOGGER;
|
||||||
private final DataSource dataSource;
|
private final DataSource dataSource;
|
||||||
|
private final ConcurrentHashMap<UUID, SQLQuery> activeQuery = new ConcurrentHashMap<>();
|
||||||
protected ExecutorService executorPool;
|
protected ExecutorService executorPool;
|
||||||
ConcurrentHashMap<UUID, SQLQuery> activeQuery = new ConcurrentHashMap<>();
|
@NotNull Supplier<Boolean> debugMode = () -> Boolean.FALSE;
|
||||||
|
|
||||||
@NotNull Supplier<Boolean> debugMode = () -> false;
|
|
||||||
|
|
||||||
public SQLManagerImpl(@NotNull DataSource dataSource) {
|
public SQLManagerImpl(@NotNull DataSource dataSource) {
|
||||||
this(dataSource, null);
|
this(dataSource, null);
|
||||||
|
|||||||
@@ -13,14 +13,12 @@ public class SQLQueryImpl implements SQLQuery {
|
|||||||
|
|
||||||
protected final long executeTime;
|
protected final long executeTime;
|
||||||
|
|
||||||
protected SQLManagerImpl sqlManager;
|
protected final SQLManagerImpl sqlManager;
|
||||||
|
final Connection connection;
|
||||||
|
final Statement statement;
|
||||||
|
final ResultSet resultSet;
|
||||||
protected QueryActionImpl queryAction;
|
protected QueryActionImpl queryAction;
|
||||||
|
|
||||||
Connection connection;
|
|
||||||
Statement statement;
|
|
||||||
|
|
||||||
ResultSet resultSet;
|
|
||||||
|
|
||||||
public SQLQueryImpl(
|
public SQLQueryImpl(
|
||||||
SQLManagerImpl sqlManager, QueryActionImpl queryAction,
|
SQLManagerImpl sqlManager, QueryActionImpl queryAction,
|
||||||
Connection connection, Statement statement, ResultSet resultSet
|
Connection connection, Statement statement, ResultSet resultSet
|
||||||
@@ -69,9 +67,9 @@ public class SQLQueryImpl implements SQLQuery {
|
|||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
try {
|
try {
|
||||||
if (getResultSet() != null) getResultSet().close();
|
if (getResultSet() != null && !getResultSet().isClosed()) getResultSet().close();
|
||||||
if (getStatement() != null) getStatement().close();
|
if (getStatement() != null && !getStatement().isClosed()) getStatement().close();
|
||||||
if (getConnection() != null) getConnection().close();
|
if (getConnection() != null && !getConnection().isClosed()) getConnection().close();
|
||||||
|
|
||||||
getManager().debug("#" + getAction().getShortID() +
|
getManager().debug("#" + getAction().getShortID() +
|
||||||
" -> finished after " + (System.currentTimeMillis() - getExecuteTime()) + " ms."
|
" -> finished after " + (System.currentTimeMillis() - getExecuteTime()) + " ms."
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.testrunner;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public abstract class EasySQLTest {
|
|
||||||
|
|
||||||
@ApiStatus.OverrideOnly
|
|
||||||
public abstract void onTest(SQLManager sqlManager) throws SQLException;
|
|
||||||
|
|
||||||
public boolean executeTest(int index, SQLManager sqlManager) {
|
|
||||||
String testName = getClass().getSimpleName();
|
|
||||||
|
|
||||||
print(" #%s 测试 @%s 开始", index, testName);
|
|
||||||
long start = System.currentTimeMillis();
|
|
||||||
|
|
||||||
try {
|
|
||||||
onTest(sqlManager);
|
|
||||||
print(" #%s 测试 @%s 成功,耗时 %s ms。", index, testName, (System.currentTimeMillis() - start));
|
|
||||||
return true;
|
|
||||||
} catch (Exception exception) {
|
|
||||||
print(" #%s 测试 @%s 失败,耗时 %s ms。", index, testName, (System.currentTimeMillis() - start));
|
|
||||||
exception.printStackTrace();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static void print(@NotNull String format, Object... params) {
|
|
||||||
Main.print(format, params);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.testrunner;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.EasySQL;
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.testrunner.tests.DeleteTest;
|
|
||||||
import cc.carm.lib.easysql.testrunner.tests.QueryCloseTest;
|
|
||||||
import cc.carm.lib.easysql.testrunner.tests.QueryFunctionTest;
|
|
||||||
import cc.carm.lib.easysql.testrunner.tests.TableCreateTest;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.TestOnly;
|
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.LinkedHashSet;
|
|
||||||
import java.util.Scanner;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
@TestOnly
|
|
||||||
@SuppressWarnings("all")
|
|
||||||
public class Main {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
if (args.length < 4) {
|
|
||||||
print("请提供以下参数:<数据库驱动> <数据库地址(JDBC)> <数据库用户> <数据库密码> [是否采用DEBUG(y/n)]");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
SQLManager sqlManager;
|
|
||||||
try {
|
|
||||||
print("初始化 SQLManager...");
|
|
||||||
print("数据库驱动 %s", args[0]);
|
|
||||||
print("数据库地址 %s", args[1]);
|
|
||||||
print("数据库用户 %s", args[2]);
|
|
||||||
print("数据库密码 %s", args[3]);
|
|
||||||
sqlManager = EasySQL.createManager(args[0], args[1], args[2], args[3]);
|
|
||||||
sqlManager.setDebugMode(args.length > 4);
|
|
||||||
print("SQLManager 初始化完成!");
|
|
||||||
} catch (Exception exception) {
|
|
||||||
print("SQLManager 初始化失败,请检查数据库配置。");
|
|
||||||
exception.printStackTrace();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
print("加载测试类...");
|
|
||||||
Set<EasySQLTest> tests = new LinkedHashSet<>();
|
|
||||||
tests.add(new TableCreateTest());
|
|
||||||
// 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 QueryFunctionTest());
|
|
||||||
tests.add(new DeleteTest());
|
|
||||||
|
|
||||||
print("准备进行测试...");
|
|
||||||
|
|
||||||
int index = 1;
|
|
||||||
int success = 0;
|
|
||||||
|
|
||||||
Iterator<EasySQLTest> testIterator = tests.iterator();
|
|
||||||
|
|
||||||
print("准备完成,请按回车键开始执行测试。");
|
|
||||||
|
|
||||||
while (testIterator.hasNext()) {
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
|
|
||||||
if (scanner.nextLine() != null) {
|
|
||||||
|
|
||||||
EasySQLTest currentTest = testIterator.next();
|
|
||||||
|
|
||||||
if (currentTest.executeTest(index, sqlManager)) {
|
|
||||||
success++;
|
|
||||||
}
|
|
||||||
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
print(" ");
|
|
||||||
print("全部测试执行完毕,成功 %s 个,失败 %s 个。",
|
|
||||||
success, (tests.size() - success)
|
|
||||||
);
|
|
||||||
|
|
||||||
EasySQL.shutdownManager(sqlManager);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void print(@NotNull String format, Object... params) {
|
|
||||||
System.out.printf((format) + "%n", params);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-22
@@ -1,22 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.testrunner.tests;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.testrunner.EasySQLTest;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class DeleteTest extends EasySQLTest {
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
|
||||||
|
|
||||||
Integer changes = sqlManager.createDelete("test_user_table")
|
|
||||||
.addCondition("id", ">", 5)
|
|
||||||
.addNotNullCondition("username")
|
|
||||||
.build().execute();
|
|
||||||
|
|
||||||
System.out.println("change(s): " + changes);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-31
@@ -1,31 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.testrunner.tests;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
|
||||||
import cc.carm.lib.easysql.testrunner.EasySQLTest;
|
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class QueryCloseTest extends EasySQLTest {
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
|
||||||
|
|
||||||
try (SQLQuery query = sqlManager.createQuery()
|
|
||||||
.inTable("test_user_table")
|
|
||||||
.orderBy("id", false)
|
|
||||||
.setLimit(5)
|
|
||||||
.build().execute()) {
|
|
||||||
ResultSet resultSet = query.getResultSet();
|
|
||||||
|
|
||||||
while (resultSet.next()) {
|
|
||||||
|
|
||||||
System.out.println("id: " + resultSet.getInt("id") + " username: " + resultSet.getString("username"));
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-34
@@ -1,34 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.testrunner.tests;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.testrunner.EasySQLTest;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class QueryFunctionTest extends EasySQLTest {
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
|
||||||
|
|
||||||
Integer id_1 = sqlManager.createQuery()
|
|
||||||
.inTable("test_user_table")
|
|
||||||
.orderBy("id", false)
|
|
||||||
.setLimit(1)
|
|
||||||
.build().executeFunction(query -> {
|
|
||||||
if (!query.getResultSet().next()) return -1;
|
|
||||||
else return query.getResultSet().getInt("id");
|
|
||||||
});
|
|
||||||
|
|
||||||
System.out.println("id (ps): " + id_1);
|
|
||||||
|
|
||||||
Integer id_2 = sqlManager.createQuery().withSQL("SELECT id FROM test_user_table ORDER BY id DESC LIMIT 1")
|
|
||||||
.executeFunction(query -> {
|
|
||||||
if (!query.getResultSet().next()) return -1;
|
|
||||||
else return query.getResultSet().getInt("id");
|
|
||||||
});
|
|
||||||
|
|
||||||
System.out.println("id (s): " + id_2);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-30
@@ -1,30 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.testrunner.tests;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
|
||||||
import cc.carm.lib.easysql.testrunner.EasySQLTest;
|
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class QueryNotCloseTest extends EasySQLTest {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
|
||||||
SQLQuery query = sqlManager.createQuery()
|
|
||||||
.inTable("test_user_table")
|
|
||||||
.orderBy("id", false)
|
|
||||||
.setLimit(5)
|
|
||||||
.build().execute();
|
|
||||||
|
|
||||||
ResultSet resultSet = query.getResultSet();
|
|
||||||
|
|
||||||
while (resultSet.next()) {
|
|
||||||
|
|
||||||
System.out.println("id: " + resultSet.getInt("id") + " username: " + resultSet.getString("username"));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-38
@@ -1,38 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.testrunner.tests;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.testrunner.EasySQLTest;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import java.util.stream.IntStream;
|
|
||||||
|
|
||||||
public class SQLUpdateBatchTests extends EasySQLTest {
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
|
||||||
|
|
||||||
List<Integer> updates = sqlManager.createInsertBatch("test_user_table")
|
|
||||||
.setColumnNames("uuid", "username", "age")
|
|
||||||
.setAllParams(generateParams())
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
System.out.println("changes " + Arrays.toString(updates.toArray()));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected List<Object[]> generateParams() {
|
|
||||||
return IntStream.range(0, 5).mapToObj(i -> generateParam()).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Object[] generateParam() {
|
|
||||||
UUID uuid = UUID.randomUUID();
|
|
||||||
return new Object[]{uuid, uuid.toString().substring(0, 5), (int) (Math.random() * 50)};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
-24
@@ -1,24 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.testrunner.tests;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class SQLUpdateReturnKeysTest extends SQLUpdateBatchTests {
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
|
||||||
List<Integer> generatedKeys = sqlManager.createInsertBatch("test_user_table")
|
|
||||||
.setColumnNames("uuid", "username", "age")
|
|
||||||
.setAllParams(generateParams())
|
|
||||||
.returnGeneratedKeys()
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
System.out.println("generated " + Arrays.toString(generatedKeys.toArray()));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
-35
@@ -1,35 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.testrunner.tests;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.api.enums.NumberType;
|
|
||||||
import cc.carm.lib.easysql.testrunner.EasySQLTest;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class TableAlterTest extends EasySQLTest {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
|
||||||
|
|
||||||
print(" 修改 test_user_table");
|
|
||||||
sqlManager.alterTable("test_user_table")
|
|
||||||
.addColumn("test", "VARCHAR(16) NOT NULL")
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
sqlManager.alterTable("test_user_table")
|
|
||||||
.addColumn("test2", "VARCHAR(16) NOT NULL", "username")
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
|
|
||||||
print(" 修改 test_user_info");
|
|
||||||
sqlManager.alterTable("test_user_info")
|
|
||||||
.addAutoIncrementColumn("num", NumberType.BIGINT, false, true)
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
sqlManager.alterTable("test_user_info")
|
|
||||||
.addColumn("a", "VARCHAR(16) NOT NULL", "")
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-38
@@ -1,38 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.testrunner.tests;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.api.enums.ForeignKeyRule;
|
|
||||||
import cc.carm.lib.easysql.api.enums.IndexType;
|
|
||||||
import cc.carm.lib.easysql.testrunner.EasySQLTest;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class TableCreateTest extends EasySQLTest {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
|
||||||
|
|
||||||
print(" 创建 test_user_table");
|
|
||||||
sqlManager.createTable("test_user_table")
|
|
||||||
.addAutoIncrementColumn("id")
|
|
||||||
.addColumn("uuid", "VARCHAR(36) NOT NULL", "用户UUID")
|
|
||||||
.addColumn("username", "VARCHAR(16) NOT NULL", "用户名")
|
|
||||||
.addColumn("age", "TINYINT DEFAULT 0 NOT NULL", "年龄")
|
|
||||||
|
|
||||||
.setIndex("uuid", IndexType.UNIQUE_KEY)
|
|
||||||
.build().execute();
|
|
||||||
|
|
||||||
print(" 创建 test_user_info");
|
|
||||||
sqlManager.createTable("test_user_info")
|
|
||||||
.addColumn("uid", "INT UNSIGNED NOT NULL")
|
|
||||||
.addColumn("info", "TEXT", "相关信息")
|
|
||||||
.addForeignKey("uid", "user",
|
|
||||||
"test_user_table", "id",
|
|
||||||
ForeignKeyRule.CASCADE, ForeignKeyRule.CASCADE
|
|
||||||
)
|
|
||||||
.setIndex(IndexType.FULLTEXT_INDEX, "sign", "info")
|
|
||||||
.build().execute();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-16
@@ -1,16 +0,0 @@
|
|||||||
package cc.carm.lib.easysql.testrunner.tests;
|
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
|
||||||
import cc.carm.lib.easysql.testrunner.EasySQLTest;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class TableRenameTest extends EasySQLTest {
|
|
||||||
@Override
|
|
||||||
public void onTest(SQLManager sqlManager) throws SQLException {
|
|
||||||
print(" 重命名 test_user_table");
|
|
||||||
sqlManager.alterTable("test_user_table")
|
|
||||||
.renameTo("test_user_table2")
|
|
||||||
.execute();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -115,7 +115,7 @@
|
|||||||
</descriptorRefs>
|
</descriptorRefs>
|
||||||
<archive>
|
<archive>
|
||||||
<manifest>
|
<manifest>
|
||||||
<mainClass>cc.carm.lib.easysql.testrunner.Main</mainClass>
|
<mainClass>cc.carm.lib.easysql.tester.Main</mainClass>
|
||||||
</manifest>
|
</manifest>
|
||||||
</archive>
|
</archive>
|
||||||
</configuration>
|
</configuration>
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package cc.carm.lib.easysql.tester;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public abstract class EasySQLTest {
|
||||||
|
|
||||||
|
protected static void print(@NotNull String format, Object... params) {
|
||||||
|
Main.print(format, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiStatus.OverrideOnly
|
||||||
|
public abstract void onTest(SQLManager sqlManager) throws SQLException;
|
||||||
|
|
||||||
|
public boolean executeTest(int index, SQLManager sqlManager) {
|
||||||
|
String testName = getClass().getSimpleName();
|
||||||
|
|
||||||
|
print(" #%s 测试 @%s 开始", index, testName);
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
|
try {
|
||||||
|
onTest(sqlManager);
|
||||||
|
print(" #%s 测试 @%s 成功,耗时 %s ms。", index, testName, (System.currentTimeMillis() - start));
|
||||||
|
return true;
|
||||||
|
} catch (Exception exception) {
|
||||||
|
print(" #%s 测试 @%s 失败,耗时 %s ms。", index, testName, (System.currentTimeMillis() - start));
|
||||||
|
exception.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
package cc.carm.lib.easysql.tester;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.EasySQL;
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
import cc.carm.lib.easysql.tester.tests.DeleteTest;
|
||||||
|
import cc.carm.lib.easysql.tester.tests.QueryCloseTest;
|
||||||
|
import cc.carm.lib.easysql.tester.tests.QueryFunctionTest;
|
||||||
|
import cc.carm.lib.easysql.tester.tests.TableCreateTest;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.TestOnly;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@TestOnly
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
if (args.length < 4) {
|
||||||
|
print("请提供以下参数:<数据库驱动> <数据库地址(JDBC)> <数据库用户> <数据库密码> [是否采用DEBUG(y/n)]");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SQLManager sqlManager;
|
||||||
|
try {
|
||||||
|
print("初始化 SQLManager...");
|
||||||
|
print("数据库驱动 %s", args[0]);
|
||||||
|
print("数据库地址 %s", args[1]);
|
||||||
|
print("数据库用户 %s", args[2]);
|
||||||
|
print("数据库密码 %s", args[3]);
|
||||||
|
sqlManager = EasySQL.createManager(args[0], args[1], args[2], args[3]);
|
||||||
|
sqlManager.setDebugMode(args.length > 4);
|
||||||
|
print("SQLManager 初始化完成!");
|
||||||
|
} catch (Exception exception) {
|
||||||
|
print("SQLManager 初始化失败,请检查数据库配置。");
|
||||||
|
exception.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
print("加载测试类...");
|
||||||
|
Set<EasySQLTest> tests = new LinkedHashSet<>();
|
||||||
|
tests.add(new TableCreateTest());
|
||||||
|
// 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 QueryFunctionTest());
|
||||||
|
tests.add(new DeleteTest());
|
||||||
|
|
||||||
|
print("准备进行测试...");
|
||||||
|
|
||||||
|
int index = 1;
|
||||||
|
int success = 0;
|
||||||
|
|
||||||
|
Iterator<EasySQLTest> testIterator = tests.iterator();
|
||||||
|
|
||||||
|
print("准备完成,请按回车键开始执行测试。");
|
||||||
|
|
||||||
|
while (testIterator.hasNext()) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
if (scanner.nextLine() != null) {
|
||||||
|
|
||||||
|
EasySQLTest currentTest = testIterator.next();
|
||||||
|
|
||||||
|
if (currentTest.executeTest(index, sqlManager)) {
|
||||||
|
success++;
|
||||||
|
}
|
||||||
|
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
print(" ");
|
||||||
|
print("全部测试执行完毕,成功 %s 个,失败 %s 个。",
|
||||||
|
success, (tests.size() - success)
|
||||||
|
);
|
||||||
|
|
||||||
|
EasySQL.shutdownManager(sqlManager);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void print(@NotNull String format, Object... params) {
|
||||||
|
System.out.printf((format) + "%n", params);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package cc.carm.lib.easysql.tester.tests;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
import cc.carm.lib.easysql.tester.EasySQLTest;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class DeleteTest extends EasySQLTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
|
|
||||||
|
Integer changes = sqlManager.createDelete("test_user_table")
|
||||||
|
.addCondition("id", ">", 5)
|
||||||
|
.addNotNullCondition("username")
|
||||||
|
.build().execute();
|
||||||
|
|
||||||
|
System.out.println("change(s): " + changes);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+35
@@ -0,0 +1,35 @@
|
|||||||
|
package cc.carm.lib.easysql.tester.tests;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
|
import cc.carm.lib.easysql.tester.EasySQLTest;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class QueryCloseTest extends EasySQLTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
|
|
||||||
|
try (SQLQuery query = sqlManager.createQuery()
|
||||||
|
.inTable("test_user_table")
|
||||||
|
.orderBy("id", false)
|
||||||
|
.setLimit(5)
|
||||||
|
.build().execute()) {
|
||||||
|
ResultSet resultSet = query.getResultSet();
|
||||||
|
|
||||||
|
while (resultSet.next()) {
|
||||||
|
|
||||||
|
System.out.printf(
|
||||||
|
"id: %d username: %s%n",
|
||||||
|
resultSet.getInt("id"),
|
||||||
|
resultSet.getString("username")
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
package cc.carm.lib.easysql.tester.tests;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
import cc.carm.lib.easysql.tester.EasySQLTest;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class QueryFunctionTest extends EasySQLTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
|
|
||||||
|
Integer id_1 = sqlManager.createQuery()
|
||||||
|
.inTable("test_user_table")
|
||||||
|
.orderBy("id", false)
|
||||||
|
.setLimit(1)
|
||||||
|
.build().executeFunction(query -> {
|
||||||
|
if (!query.getResultSet().next()) return -1;
|
||||||
|
else return query.getResultSet().getInt("id");
|
||||||
|
});
|
||||||
|
|
||||||
|
System.out.println("id (ps): " + id_1);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+30
@@ -0,0 +1,30 @@
|
|||||||
|
package cc.carm.lib.easysql.tester.tests;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
|
import cc.carm.lib.easysql.tester.EasySQLTest;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class QueryNotCloseTest extends EasySQLTest {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
|
SQLQuery query = sqlManager.createQuery()
|
||||||
|
.inTable("test_user_table")
|
||||||
|
.orderBy("id", false)
|
||||||
|
.setLimit(5)
|
||||||
|
.build().execute();
|
||||||
|
|
||||||
|
ResultSet resultSet = query.getResultSet();
|
||||||
|
|
||||||
|
while (resultSet.next()) {
|
||||||
|
|
||||||
|
System.out.printf("id: %d username: %s%n", resultSet.getInt("id"), resultSet.getString("username"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
package cc.carm.lib.easysql.tester.tests;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
import cc.carm.lib.easysql.tester.EasySQLTest;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
public class SQLUpdateBatchTests extends EasySQLTest {
|
||||||
|
|
||||||
|
|
||||||
|
protected static List<Object[]> generateParams() {
|
||||||
|
return IntStream.range(0, 5).mapToObj(i -> generateParam()).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static Object[] generateParam() {
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
return new Object[]{uuid, uuid.toString().substring(0, 5), (int) (Math.random() * 50)};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
|
|
||||||
|
List<Integer> updates = sqlManager.createInsertBatch("test_user_table")
|
||||||
|
.setColumnNames("uuid", "username", "age")
|
||||||
|
.setAllParams(generateParams())
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
System.out.println("changes " + Arrays.toString(updates.toArray()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+24
@@ -0,0 +1,24 @@
|
|||||||
|
package cc.carm.lib.easysql.tester.tests;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SQLUpdateReturnKeysTest extends SQLUpdateBatchTests {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
|
List<Integer> generatedKeys = sqlManager.createInsertBatch("test_user_table")
|
||||||
|
.setColumnNames("uuid", "username", "age")
|
||||||
|
.setAllParams(generateParams())
|
||||||
|
.returnGeneratedKeys()
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
System.out.println("generated " + Arrays.toString(generatedKeys.toArray()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+35
@@ -0,0 +1,35 @@
|
|||||||
|
package cc.carm.lib.easysql.tester.tests;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
import cc.carm.lib.easysql.api.enums.NumberType;
|
||||||
|
import cc.carm.lib.easysql.tester.EasySQLTest;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class TableAlterTest extends EasySQLTest {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
|
|
||||||
|
print(" 修改 test_user_table");
|
||||||
|
sqlManager.alterTable("test_user_table")
|
||||||
|
.addColumn("test", "VARCHAR(16) NOT NULL")
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
sqlManager.alterTable("test_user_table")
|
||||||
|
.addColumn("test2", "VARCHAR(16) NOT NULL", "username")
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
|
||||||
|
print(" 修改 test_user_info");
|
||||||
|
sqlManager.alterTable("test_user_info")
|
||||||
|
.addAutoIncrementColumn("num", NumberType.BIGINT, false, true)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
sqlManager.alterTable("test_user_info")
|
||||||
|
.addColumn("a", "VARCHAR(16) NOT NULL", "")
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
package cc.carm.lib.easysql.tester.tests;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
import cc.carm.lib.easysql.api.enums.ForeignKeyRule;
|
||||||
|
import cc.carm.lib.easysql.api.enums.IndexType;
|
||||||
|
import cc.carm.lib.easysql.tester.EasySQLTest;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class TableCreateTest extends EasySQLTest {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
|
|
||||||
|
print(" 创建 test_user_table");
|
||||||
|
sqlManager.createTable("test_user_table")
|
||||||
|
.addAutoIncrementColumn("id")
|
||||||
|
.addColumn("uuid", "VARCHAR(36) NOT NULL", "用户UUID")
|
||||||
|
.addColumn("username", "VARCHAR(16) NOT NULL", "用户名")
|
||||||
|
.addColumn("age", "TINYINT DEFAULT 0 NOT NULL", "年龄")
|
||||||
|
|
||||||
|
.setIndex("uuid", IndexType.UNIQUE_KEY)
|
||||||
|
.build().execute();
|
||||||
|
|
||||||
|
print(" 创建 test_user_info");
|
||||||
|
sqlManager.createTable("test_user_info")
|
||||||
|
.addColumn("uid", "INT UNSIGNED NOT NULL")
|
||||||
|
.addColumn("info", "TEXT", "相关信息")
|
||||||
|
.addForeignKey("uid", "user",
|
||||||
|
"test_user_table", "id",
|
||||||
|
ForeignKeyRule.CASCADE, ForeignKeyRule.CASCADE
|
||||||
|
)
|
||||||
|
.setIndex(IndexType.FULLTEXT_INDEX, "sign", "info")
|
||||||
|
.build().execute();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
package cc.carm.lib.easysql.tester.tests;
|
||||||
|
|
||||||
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
|
import cc.carm.lib.easysql.tester.EasySQLTest;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class TableRenameTest extends EasySQLTest {
|
||||||
|
@Override
|
||||||
|
public void onTest(SQLManager sqlManager) throws SQLException {
|
||||||
|
print(" 重命名 test_user_table");
|
||||||
|
sqlManager.alterTable("test_user_table")
|
||||||
|
.renameTo("test_user_table2")
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -72,6 +72,7 @@
|
|||||||
<!--项目地址 https://github.com/brettwooldridge/HikariCP/ -->
|
<!--项目地址 https://github.com/brettwooldridge/HikariCP/ -->
|
||||||
<groupId>com.zaxxer</groupId>
|
<groupId>com.zaxxer</groupId>
|
||||||
<artifactId>HikariCP</artifactId>
|
<artifactId>HikariCP</artifactId>
|
||||||
|
<!--suppress MavenPackageUpdate -->
|
||||||
<version>4.0.3</version>
|
<version>4.0.3</version>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
|
|||||||
Reference in New Issue
Block a user