Skip to content

Commit

Permalink
Merge pull request #6031 from cktk/3.0
Browse files Browse the repository at this point in the history
✨ 支持SQLite的DDL自动维护功能 优化getDdlGenerator的方法 之前的写法会把SQLite当做pgsql进行处理
  • Loading branch information
qmdx authored Apr 2, 2024
2 parents cd02388 + 8ac7994 commit a38c105
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
*/
package com.baomidou.mybatisplus.extension.ddl;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.ddl.history.IDdlGenerator;
import com.baomidou.mybatisplus.extension.ddl.history.MysqlDdlGenerator;
import com.baomidou.mybatisplus.extension.ddl.history.OracleDdlGenerator;
import com.baomidou.mybatisplus.extension.ddl.history.PostgreDdlGenerator;
import com.baomidou.mybatisplus.extension.plugins.pagination.DialectFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.*;
import com.baomidou.mybatisplus.extension.ddl.history.SQLiteDdlGenerator;
import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
Expand Down Expand Up @@ -145,17 +146,48 @@ public static ScriptRunner getScriptRunner(Connection connection, boolean autoCo
}

protected static IDdlGenerator getDdlGenerator(String jdbcUrl) throws RuntimeException {
IDialect dialect = DialectFactory.getDialect(JdbcUtils.getDbType(jdbcUrl));
if (dialect instanceof MySqlDialect) {
return MysqlDdlGenerator.newInstance();
DbType dbType = JdbcUtils.getDbType(jdbcUrl);
switch (dbType) {
case MYSQL:
case MARIADB:
case GBASE:
case OSCAR:
case XU_GU:
case CLICK_HOUSE:
case OCEAN_BASE:
case CUBRID:
case SUNDB:
return MysqlDdlGenerator.newInstance();
case ORACLE:
case DM:
case GAUSS:
case ORACLE_12C:
case FIREBIRD:
case SQL_SERVER:
return OracleDdlGenerator.newInstance();
case SQLITE:
return SQLiteDdlGenerator.newInstance();
case POSTGRE_SQL:
case H2:
case LEALONE:
case HSQL:
case KINGBASE_ES:
case PHOENIX:
case SAP_HANA:
case IMPALA:
case HIGH_GO:
case VERTICA:
case REDSHIFT:
case OPENGAUSS:
case TDENGINE:
case UXDB:
case GBASE8S_PG:
case GBASE_8C:
return PostgreDdlGenerator.newInstance();
default:
throw ExceptionUtils.mpe("%s database not supported.", dbType.getDb());
}
if (dialect instanceof PostgreDialect) {
return PostgreDdlGenerator.newInstance();
}
if (dialect instanceof OracleDialect || dialect instanceof Oracle12cDialect) {
return OracleDdlGenerator.newInstance();
}
throw new RuntimeException("The database is not supported");

}

public static String getDatabase(String jdbcUrl) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.baomidou.mybatisplus.extension.ddl.history;

import org.springframework.stereotype.Component;

import java.util.function.Function;


@Component
public class SQLiteDdlGenerator implements IDdlGenerator {

public static IDdlGenerator newInstance() {
return new SQLiteDdlGenerator();
}

@Override
public boolean existTable(String databaseName, Function<String, Boolean> executeFunction) {
StringBuffer sql = new StringBuffer();
sql.append("SELECT count(1) FROM sqlite_master WHERE name='");
sql.append(getDdlHistory()).append("' AND type='table'");
return executeFunction.apply(sql.toString());
}

@Override
public String createDdlHistory() {
StringBuffer sql = new StringBuffer();
sql.append("CREATE TABLE IF NOT EXISTS `").append(getDdlHistory()).append("` (");
sql.append("script TEXT primary key,");
sql.append("type TEXT,");
sql.append("version TEXT");
sql.append(");");
return sql.toString();
}
}

0 comments on commit a38c105

Please sign in to comment.