forked from p81075629/datagear
系统MbSqlDialect方言可通过配置项自定义
This commit is contained in:
parent
64316caf3f
commit
3e55ee91a9
|
@ -24,7 +24,7 @@ import org.slf4j.LoggerFactory;
|
|||
/**
|
||||
* {@linkplain MbSqlDialect}构建器。
|
||||
* <p>
|
||||
* 此类根据给定的数据库连接,构建对应的{@linkplain MbSqlDialect}。
|
||||
* 此类依次根据给定的{@linkplain #getDialectName()}、数据库连接,构建对应的{@linkplain MbSqlDialect}。
|
||||
* </p>
|
||||
*
|
||||
* @author datagear@163.com
|
||||
|
@ -39,10 +39,43 @@ public class MbSqlDialectBuilder
|
|||
*/
|
||||
public static final String DEFAULT_IDENTIFIER_QUOTE = " ";
|
||||
|
||||
public static final String DIALECT_NAME_DERBY = "derby";
|
||||
|
||||
public static final String DIALECT_NAME_MYSQL = "mysql";
|
||||
|
||||
public static final String DIALECT_NAME_ORACLE = "oracle";
|
||||
|
||||
public static final String DIALECT_NAME_POSTGRESQL = "postgresql";
|
||||
|
||||
public static final String DIALECT_NAME_DEFAULT = "default";
|
||||
|
||||
/** 方言名 */
|
||||
private String dialectName = null;
|
||||
|
||||
public MbSqlDialectBuilder()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取方言名。
|
||||
*
|
||||
* @return 可能为{@code null}
|
||||
*/
|
||||
public String getDialectName()
|
||||
{
|
||||
return dialectName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置方言,参考{@code DIALECT_NAME_*}。
|
||||
*
|
||||
* @param dialectName
|
||||
*/
|
||||
public void setDialectName(String dialectName)
|
||||
{
|
||||
this.dialectName = dialectName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建{@linkplain MbSqlDialect}。
|
||||
*
|
||||
|
@ -76,8 +109,65 @@ public class MbSqlDialectBuilder
|
|||
*/
|
||||
public MbSqlDialect build(Connection cn) throws SQLException
|
||||
{
|
||||
String url = JdbcUtil.getURLIfSupports(cn);
|
||||
return build(cn, url);
|
||||
MbSqlDialect dialect = null;
|
||||
|
||||
if (!StringUtil.isEmpty(this.dialectName))
|
||||
{
|
||||
dialect = buildByDialectName(cn, this.dialectName);
|
||||
}
|
||||
|
||||
if (dialect == null)
|
||||
{
|
||||
String url = JdbcUtil.getURLIfSupports(cn);
|
||||
dialect = buildByUrl(cn, url);
|
||||
}
|
||||
|
||||
if (dialect == null)
|
||||
dialect = buildDefaultMbSqlDialect(cn);
|
||||
|
||||
if (LOGGER.isInfoEnabled())
|
||||
LOGGER.info("Build " + dialect.toString() + " for current environment");
|
||||
|
||||
return dialect;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param cn
|
||||
* @param dialectName
|
||||
* 允许为{@code null}
|
||||
* @return 返回{@code null}表示不支持
|
||||
* @throws SQLException
|
||||
*/
|
||||
protected MbSqlDialect buildByDialectName(Connection cn, String dialectName) throws SQLException
|
||||
{
|
||||
MbSqlDialect dialect = null;
|
||||
|
||||
if (StringUtil.isEmpty(dialectName))
|
||||
{
|
||||
}
|
||||
else if (DIALECT_NAME_DERBY.equalsIgnoreCase(dialectName))
|
||||
{
|
||||
dialect = buildDerbyMbSqlDialect(cn);
|
||||
}
|
||||
else if (DIALECT_NAME_MYSQL.equalsIgnoreCase(dialectName))
|
||||
{
|
||||
dialect = buildMysqlMbSqlDialect(cn);
|
||||
}
|
||||
else if (DIALECT_NAME_ORACLE.equalsIgnoreCase(dialectName))
|
||||
{
|
||||
dialect = buildOracleMbSqlDialect(cn);
|
||||
}
|
||||
else if (DIALECT_NAME_POSTGRESQL.equalsIgnoreCase(dialectName))
|
||||
{
|
||||
dialect = buildPostgresqlMbSqlDialect(cn);
|
||||
}
|
||||
else if (DIALECT_NAME_DEFAULT.equalsIgnoreCase(dialectName))
|
||||
{
|
||||
dialect = buildDefaultMbSqlDialect(cn);
|
||||
}
|
||||
|
||||
return dialect;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -85,16 +175,15 @@ public class MbSqlDialectBuilder
|
|||
* @param cn
|
||||
* @param url
|
||||
* 允许为{@code null}
|
||||
* @return
|
||||
* @return 返回{@code null}表示不支持
|
||||
* @throws SQLException
|
||||
*/
|
||||
protected MbSqlDialect build(Connection cn, String url) throws SQLException
|
||||
protected MbSqlDialect buildByUrl(Connection cn, String url) throws SQLException
|
||||
{
|
||||
MbSqlDialect dialect = null;
|
||||
|
||||
if (StringUtil.isEmpty(url))
|
||||
{
|
||||
dialect = buildDefaultMbSqlDialect(cn);
|
||||
}
|
||||
else if (DerbyURLSensor.INSTANCE.supports(url))
|
||||
{
|
||||
|
@ -112,13 +201,6 @@ public class MbSqlDialectBuilder
|
|||
{
|
||||
dialect = buildPostgresqlMbSqlDialect(cn);
|
||||
}
|
||||
else
|
||||
{
|
||||
dialect = buildDefaultMbSqlDialect(cn);
|
||||
}
|
||||
|
||||
if (LOGGER.isInfoEnabled())
|
||||
LOGGER.info("Build " + dialect.toString() + " for current environment");
|
||||
|
||||
return dialect;
|
||||
}
|
||||
|
|
|
@ -336,7 +336,12 @@ public class CoreConfig implements InitializingBean
|
|||
@Bean
|
||||
public MbSqlDialectBuilder mbSqlDialectBuilder()
|
||||
{
|
||||
return new MbSqlDialectBuilder();
|
||||
String dialectName = environment.getProperty("datasourceDialect");
|
||||
|
||||
MbSqlDialectBuilder builder = new MbSqlDialectBuilder();
|
||||
builder.setDialectName(dialectName);
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
|
|
@ -68,6 +68,9 @@ datasource.url=jdbc:derby:${directory.derby};create=true
|
|||
datasource.username=
|
||||
datasource.password=
|
||||
|
||||
#数据库方言,可选项:derby、mysql、oracle、postgresql、default,留空则表示自动判断
|
||||
datasourceDialect=
|
||||
|
||||
#Spring Boot配置
|
||||
#-----------------------------------------
|
||||
|
||||
|
|
Loading…
Reference in New Issue