Skip to content

Commit 347675e

Browse files
authored
Merge pull request #79 from jumpserver/pr@dev@feat_format_pg_dbname
feat: format pg dbname
2 parents 8c8244a + 7b7d3f4 commit 347675e

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

backend/modules/src/main/java/org.jumpserver.chen.modules/postgresql/PostgresqlActuator.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jumpserver.chen.modules.postgresql;
22

3+
import org.apache.commons.lang3.StringUtils;
34
import org.jumpserver.chen.framework.datasource.ConnectionManager;
45
import org.jumpserver.chen.framework.datasource.base.BaseSQLActuator;
56
import org.jumpserver.chen.framework.datasource.sql.SQL;
@@ -15,30 +16,45 @@ public PostgresqlActuator(ConnectionManager connectionManager) {
1516
super(connectionManager);
1617
}
1718

19+
private String dbName;
20+
1821
public PostgresqlActuator(PostgresqlActuator sqlActuator, Connection connection) {
1922
super(sqlActuator, connection);
2023
}
2124

2225
@Override
2326
public String getCurrentSchema() throws SQLException {
24-
var result = this.execute(SQL.of("SELECT CURRENT_SCHEMA()"));
25-
return (String) result.getData().get(0).get(0);
27+
var result = this.execute(SQL.of("SELECT current_schema()"));
28+
return this.formatSchemaName((String) result.getData().get(0).get(0));
2629
}
2730

2831
@Override
2932
public List<String> getSchemas() throws SQLException {
3033
var result = this.execute(SQL.of("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA"));
31-
return result.getData().stream().map(row -> (String) row.get(0)).toList();
34+
return result.getData().stream().map(row -> (String) row.get(0)).toList().stream().map(this::formatSchemaName).toList();
3235
}
3336

3437
@Override
3538
public void changeSchema(String schema) throws SQLException {
36-
this.execute(SQL.of("SET SEARCH_PATH TO '?';", schema));
39+
var ss = schema.split("\\.");
40+
this.execute(SQL.of("SET SEARCH_PATH TO '?';", ss[1]));
3741
}
3842

3943
@Override
4044
public SQLExecutePlan createPlan(String schema, String table, SQLQueryParams sqlQueryParams) throws SQLException {
4145
var sql = SQL.of("select * from \"?\".\"?\"", schema, table);
4246
return this.createPlan(sql, sqlQueryParams);
4347
}
48+
49+
private String formatSchemaName(String schema) {
50+
try {
51+
if (StringUtils.isEmpty(this.dbName)) {
52+
var result = this.execute(SQL.of("SELECT current_database();"));
53+
this.dbName = (String) result.getData().get(0).get(0);
54+
}
55+
} catch (SQLException e) {
56+
throw new RuntimeException(e);
57+
}
58+
return String.format("%s.%s", this.dbName, schema);
59+
}
4460
}

0 commit comments

Comments
 (0)