Skip to content

Commit

Permalink
开始增加Context。
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy0v0 committed Dec 1, 2024
1 parent 5590a34 commit 8c85538
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 76 deletions.
95 changes: 95 additions & 0 deletions jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/Context.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.github.fantasy0v0.swift.jdbc;

import com.github.fantasy0v0.swift.jdbc.dialect.ANSI;
import com.github.fantasy0v0.swift.jdbc.dialect.SQLDialect;
import com.github.fantasy0v0.swift.jdbc.type.AbstractTypeHandler;
import com.github.fantasy0v0.swift.jdbc.type.TypeGetHandler;
import com.github.fantasy0v0.swift.jdbc.type.TypeSetHandler;
import com.github.fantasy0v0.swift.jdbc.util.LogUtil;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Context {

private DataSource dataSource;

private SQLDialect dialect;

private final Map<Class<?>, TypeGetHandler<?>> getHandlerMap = new ConcurrentHashMap<>();

private final Map<Class<?>, TypeSetHandler<?>> setHandlerMap = new ConcurrentHashMap<>();

private StatementConfiguration statementConfiguration;

public Context(DataSource dataSource, SQLDialect dialect) {
this.dataSource = dataSource;
configuration(dialect);
}

public void configuration(DataSource dataSource) {
this.dataSource = dataSource;
if (null != dialect) {
configuration(this.dialect);
}
LogUtil.common().debug("更新数据源");
}

public void configuration(SQLDialect dialect) {
dialect.install(dataSource);
this.dialect = dialect;
LogUtil.common().debug("配置方言");
}

public DataSource getDataSource() {
return dataSource;
}

public SQLDialect getSQLDialect() {
return null != dialect ? dialect : ANSI.Instance;
}

public <T> void configuration(AbstractTypeHandler<T> typeHandler) {
configuration((TypeGetHandler<T>)typeHandler);
configuration((TypeSetHandler<T>)typeHandler);
}

public <T> void configuration(TypeGetHandler<T> handler) {
Class<T> supported = handler.support();
if (getHandlerMap.containsKey(supported)) {
LogUtil.common().debug("{} 原有的GetHandler将被替换", supported);
}
getHandlerMap.put(supported, handler);
}

public <T> void configuration(TypeSetHandler<T> handler) {
Class<T> supported = handler.support();
if (setHandlerMap.containsKey(supported)) {
LogUtil.common().debug("{} 原有的SetHandler将被替换", supported);
}
setHandlerMap.put(supported, handler);
}

public void configuration(StatementConfiguration statementConfiguration) {
this.statementConfiguration = statementConfiguration;
LogUtil.common().debug("配置Statement Configuration");
}

public StatementConfiguration getStatementConfiguration() {
if (null == statementConfiguration) {
statementConfiguration = new StatementConfiguration();
}
return statementConfiguration;
}

public SelectBuilder select(String sql, List<Object> params) {
return new SelectBuilder(this, sql.trim(), params);
}

public SelectBuilder select(String sql, Object... params) {
return select(sql, Arrays.stream(params).toList());
}
}
89 changes: 33 additions & 56 deletions jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/JDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.github.fantasy0v0.swift.jdbc.connection.ConnectionPool;
import com.github.fantasy0v0.swift.jdbc.connection.impl.DefaultConnectionPool;
import com.github.fantasy0v0.swift.jdbc.dialect.ANSI;
import com.github.fantasy0v0.swift.jdbc.dialect.SQLDialect;
import com.github.fantasy0v0.swift.jdbc.exception.SwiftException;
import com.github.fantasy0v0.swift.jdbc.exception.SwiftSQLException;
Expand All @@ -13,61 +12,55 @@
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

public final class JDBC {

private static DataSource dataSource;
private static Context context;

private static SQLDialect dialect;

static final Map<Class<?>, TypeGetHandler<?>> GetHandlerMap = new ConcurrentHashMap<>();

static final Map<Class<?>, TypeSetHandler<?>> SetHandlerMap = new ConcurrentHashMap<>();

static {
ConnectionPoolUtil.pool = ServiceLoader.load(ConnectionPool.class)
.findFirst()
.orElse(new DefaultConnectionPool());
LogUtil.common().debug("使用的ConnectionPool: {}", ConnectionPoolUtil.pool.getClass().getName());

configuration(new ByteTypeHandler());
configuration(new ShortTypeHandler());
configuration(new IntegerTypeHandler());
configuration(new FloatTypeHandler());
configuration(new DoubleTypeHandler());
configuration(new LongTypeHandler());
configuration(new BooleanTypeHandler());
configuration(new StringTypeHandler());
configuration(new TimestampTypeHandler());
configuration(new LocalTimeTypeHandler());
configuration(new LocalDateTypeHandler());
configuration(new LocalDateTimeTypeHandler());
configuration(new OffsetDateTimeTypeHandler());
}

static StatementConfiguration statementConfiguration;

public static void configuration(DataSource dataSource) {
JDBC.dataSource = dataSource;
private static synchronized Context getContext() {
if (null == context) {
throw new SwiftException("请先初始化");
}
return context;
}

public static DataSource getDataSource() {
return JDBC.dataSource;
public static synchronized void initialization(DataSource dataSource, SQLDialect dialect) {
if (null != context) {
throw new SwiftException("请勿重复初始化");
}
context = new Context(dataSource, dialect);
context.configuration(new ByteTypeHandler());
context.configuration(new ShortTypeHandler());
context.configuration(new IntegerTypeHandler());
context.configuration(new FloatTypeHandler());
context.configuration(new DoubleTypeHandler());
context.configuration(new LongTypeHandler());
context.configuration(new BooleanTypeHandler());
context.configuration(new StringTypeHandler());
context.configuration(new TimestampTypeHandler());
context.configuration(new LocalTimeTypeHandler());
context.configuration(new LocalDateTypeHandler());
context.configuration(new LocalDateTimeTypeHandler());
context.configuration(new OffsetDateTimeTypeHandler());
}

public static void configuration(SQLDialect dialect) {
requireNonNull(dataSource);
dialect.install(dataSource);
JDBC.dialect = dialect;
LogUtil.common().debug("配置方言");
public static void configuration(DataSource dataSource) {
getContext().configuration(dataSource);
}

static SQLDialect getSQLDialect() {
return null == dialect ? ANSI.Instance : dialect;
public static void configuration(SQLDialect dialect) {
getContext().configuration(dialect);
}

public static <T> void configuration(AbstractTypeHandler<T> typeHandler) {
Expand All @@ -76,39 +69,23 @@ public static <T> void configuration(AbstractTypeHandler<T> typeHandler) {
}

public static <T> void configuration(TypeGetHandler<T> handler) {
Class<T> supported = handler.support();
if (GetHandlerMap.containsKey(supported)) {
LogUtil.common().debug("{} 原有的GetHandler将被替换", supported);
}
GetHandlerMap.put(supported, handler);
getContext().configuration(handler);
}

public static <T> void configuration(TypeSetHandler<T> handler) {
Class<T> supported = handler.support();
if (SetHandlerMap.containsKey(supported)) {
LogUtil.common().debug("{} 原有的SetHandler将被替换", supported);
}
SetHandlerMap.put(supported, handler);
getContext().configuration(handler);
}

public static void configuration(StatementConfiguration statementConfiguration) {
JDBC.statementConfiguration = statementConfiguration;
LogUtil.common().debug("配置Statement Configuration");
}

private static DataSource requireNonNull(DataSource dataSource) {
if (null == dataSource) {
throw new SwiftException("未配置DataSource");
}
return dataSource;
getContext().configuration(statementConfiguration);
}

public static SelectBuilder select(String sql, List<Object> params) {
return new SelectBuilder(requireNonNull(dataSource), statementConfiguration, sql.trim(), params);
return getContext().select(sql.trim(), params);
}

public static SelectBuilder select(String sql, Object... params) {
return select(sql, Arrays.stream(params).toList());
return getContext().select(sql, Arrays.stream(params).toList());
}

public static InsertBuilder insert(String sql) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
import com.github.fantasy0v0.swift.jdbc.exception.SwiftException;
import com.github.fantasy0v0.swift.jdbc.exception.SwiftSQLException;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

public class PaginateBuilder {

private final DataSource dataSource;

private final StatementConfiguration statementConfiguration;
private final Context context;

private final ParameterHandler parameterHandler;

Expand All @@ -29,11 +26,9 @@ public class PaginateBuilder {

private List<Object> countParams;

PaginateBuilder(DataSource dataSource,
StatementConfiguration statementConfiguration, ParameterHandler parameterHandler,
PaginateBuilder(Context context, ParameterHandler parameterHandler,
String sql, List<Object> params, long number, long size) {
this.dataSource = dataSource;
this.statementConfiguration = statementConfiguration;
this.context = context;
this.sql = sql;
this.params = params;
this.parameterHandler = parameterHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import com.github.fantasy0v0.swift.jdbc.exception.SwiftSQLException;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

public class SelectBuilder implements StatementConfigurator<SelectBuilder> {

private final DataSource dataSource;
private final Context context;

private StatementConfiguration statementConfiguration;

Expand All @@ -18,11 +17,8 @@ public class SelectBuilder implements StatementConfigurator<SelectBuilder> {

private ParameterHandler parameterHandler;

SelectBuilder(DataSource dataSource,
StatementConfiguration statementConfiguration,
String sql, List<Object> params) {
this.dataSource = dataSource;
this.statementConfiguration = statementConfiguration;
SelectBuilder(Context context, String sql, List<Object> params) {
this.context = context;
this.sql = sql;
this.params = params;
}
Expand Down Expand Up @@ -65,11 +61,7 @@ public SelectBuilder setParameterHandler(ParameterHandler parameterHandler) {
* @return PaginateBuilder
*/
public PaginateBuilder paginate(long number, long size) {
return new PaginateBuilder(
dataSource,
statementConfiguration, parameterHandler,
sql, params, number, size
);
return new PaginateBuilder(context, parameterHandler, sql, params, number, size);
}

public <T> List<T> fetch(FetchMapper<T> mapper) {
Expand Down

0 comments on commit 8c85538

Please sign in to comment.