-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
DatabaseUtils.java
85 lines (76 loc) · 4.25 KB
/
DatabaseUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package liquibase.database.core;
import liquibase.GlobalConfiguration;
import liquibase.Scope;
import liquibase.database.Database;
import liquibase.database.OfflineConnection;
import liquibase.exception.DatabaseException;
import liquibase.executor.Executor;
import liquibase.executor.ExecutorService;
import liquibase.statement.core.RawSqlStatement;
import liquibase.structure.core.Schema;
import liquibase.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
public class DatabaseUtils {
/**
* Executes RawSqlStatements particular to each database engine to set the default schema for the given Database
*
* @param defaultCatalogName Catalog name and schema name are similar concepts.
* Used if defaultCatalogName is null.
* @param defaultSchemaName Catalog name and schema name are similar concepts.
* Catalog is used with Oracle, DB2 and MySQL, and takes
* precedence over the schema name.
* @param database Which Database object is affected by the initialization.
* @throws DatabaseException
*/
public static void initializeDatabase(String defaultCatalogName, String defaultSchemaName, Database database)
throws DatabaseException {
if (((defaultCatalogName != null) || (defaultSchemaName != null)) && !(database.getConnection() instanceof
OfflineConnection)) {
final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
if (database instanceof OracleDatabase) {
String schema = defaultCatalogName;
if (schema == null) {
schema = defaultSchemaName;
}
executor.execute(
new RawSqlStatement("ALTER SESSION SET CURRENT_SCHEMA=" +
database.escapeObjectName(schema, Schema.class)));
} else if (database instanceof PostgresDatabase && defaultSchemaName != null) {
String searchPath = executor.queryForObject(new RawSqlStatement("SHOW SEARCH_PATH"), String.class);
if (!searchPath.equals(defaultCatalogName) && !searchPath.startsWith(defaultSchemaName + ",") && !searchPath.startsWith("\"" + defaultSchemaName + "\",")) {
String finalSearchPath;
if (GlobalConfiguration.PRESERVE_SCHEMA_CASE.getCurrentValue()) {
finalSearchPath = ((PostgresDatabase) database).quoteObject(defaultSchemaName, Schema.class);
} else {
finalSearchPath = defaultSchemaName;
}
if (StringUtils.isNotBlank(searchPath)) {
//If existing search path entries are not quoted, quote them. Some databases do not show them as quoted even though they need to be (like $user or case sensitive schemas)
finalSearchPath += ", " + StringUtil.join(StringUtil.splitAndTrim(searchPath, ","), ",", (StringUtil.StringUtilFormatter<String>) obj -> {
if (obj.startsWith("\"")) {
return obj;
}
return ((PostgresDatabase) database).quoteObject(obj, Schema.class);
});
}
executor.execute(new RawSqlStatement("SET SEARCH_PATH TO " + finalSearchPath));
}
} else if (database instanceof AbstractDb2Database) {
String schema = defaultCatalogName;
if (schema == null) {
schema = defaultSchemaName;
}
executor.execute(new RawSqlStatement("SET CURRENT SCHEMA "
+ schema));
} else if (database instanceof MySQLDatabase) {
String schema = defaultCatalogName;
if (schema == null) {
schema = defaultSchemaName;
}
executor.execute(new RawSqlStatement("USE " + schema));
} else if (database instanceof MSSQLDatabase) {
executor.execute(new RawSqlStatement("USE " + defaultCatalogName));
}
}
}
}