mirror of
https://github.com/CarmJos/EasySQL.git
synced 2026-06-04 15:28:20 +08:00
@@ -0,0 +1,44 @@
|
||||
package cc.carm.lib.easysql.builder.impl;
|
||||
|
||||
import cc.carm.lib.easysql.api.builder.TableMetadataBuilder;
|
||||
import cc.carm.lib.easysql.api.function.SQLFunction;
|
||||
import cc.carm.lib.easysql.builder.AbstractSQLBuilder;
|
||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class TableMetadataBuilderImpl
|
||||
extends AbstractSQLBuilder
|
||||
implements TableMetadataBuilder {
|
||||
|
||||
protected final @NotNull String tablePattern;
|
||||
|
||||
public TableMetadataBuilderImpl(@NotNull SQLManagerImpl manager, @NotNull String tablePattern) {
|
||||
super(manager);
|
||||
this.tablePattern = tablePattern;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Boolean> validateExist() {
|
||||
return validate((meta) -> meta.getTables(null, null, tablePattern, new String[]{"TABLE"}));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Boolean> isColumnExists(String columnPattern) {
|
||||
return validate((meta) -> meta.getColumns(null, null, tablePattern, columnPattern));
|
||||
}
|
||||
|
||||
/**
|
||||
* fast validate EXISTS.
|
||||
*
|
||||
* @param supplier supplier to get result set
|
||||
* @return result future
|
||||
*/
|
||||
private CompletableFuture<Boolean> validate(SQLFunction<DatabaseMetaData, ResultSet> supplier) {
|
||||
return getManager().fetchMetadata(supplier, ResultSet::next);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import cc.carm.lib.easysql.api.action.SQLUpdateBatchAction;
|
||||
import cc.carm.lib.easysql.api.builder.*;
|
||||
import cc.carm.lib.easysql.api.function.SQLDebugHandler;
|
||||
import cc.carm.lib.easysql.api.function.SQLExceptionHandler;
|
||||
import cc.carm.lib.easysql.api.function.SQLFunction;
|
||||
import cc.carm.lib.easysql.builder.impl.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -20,11 +21,13 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class SQLManagerImpl implements SQLManager {
|
||||
@@ -50,12 +53,7 @@ public class SQLManagerImpl implements SQLManager {
|
||||
String managerName = "SQLManager" + (name != null ? "#" + name : "");
|
||||
this.LOGGER = logger;
|
||||
this.dataSource = dataSource;
|
||||
this.executorPool = Executors.newFixedThreadPool(3, r -> {
|
||||
Thread thread = new Thread(r, managerName);
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
});
|
||||
|
||||
this.executorPool = SQLManager.defaultExecutorPool(managerName);
|
||||
this.exceptionHandler = SQLExceptionHandler.detailed(getLogger());
|
||||
this.debugHandler = SQLDebugHandler.defaultHandler(getLogger());
|
||||
}
|
||||
@@ -80,10 +78,6 @@ public class SQLManagerImpl implements SQLManager {
|
||||
this.debugHandler = debugHandler;
|
||||
}
|
||||
|
||||
public void debug(String msg) {
|
||||
if (isDebugMode()) getLogger().info("[DEBUG] " + msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Logger getLogger() {
|
||||
return LOGGER;
|
||||
@@ -160,6 +154,33 @@ public class SQLManagerImpl implements SQLManager {
|
||||
return action.execute(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> CompletableFuture<R> fetchMetadata(@NotNull SQLFunction<DatabaseMetaData, R> metadata) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try (Connection conn = getConnection()) {
|
||||
return metadata.apply(conn.getMetaData());
|
||||
} catch (SQLException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}, this.executorPool);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> CompletableFuture<R> fetchMetadata(@NotNull SQLFunction<DatabaseMetaData, ResultSet> supplier,
|
||||
@NotNull SQLFunction<@NotNull ResultSet, R> reader) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try (
|
||||
Connection conn = getConnection();
|
||||
ResultSet rs = supplier.apply(conn.getMetaData())
|
||||
) {
|
||||
if (rs == null) throw new NullPointerException("Metadata返回的ResultSet为null。");
|
||||
else return reader.apply(rs);
|
||||
} catch (SQLException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}, this.executorPool);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableCreateBuilder createTable(@NotNull String tableName) {
|
||||
return new TableCreateBuilderImpl(this, tableName);
|
||||
@@ -170,6 +191,11 @@ public class SQLManagerImpl implements SQLManager {
|
||||
return new TableAlterBuilderImpl(this, tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableMetadataBuilder fetchTableMetadata(@NotNull String tablePattern) {
|
||||
return new TableMetadataBuilderImpl(this, tablePattern);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryBuilder createQuery() {
|
||||
return new QueryBuilderImpl(this);
|
||||
|
||||
Reference in New Issue
Block a user