1
1
package org .jumpserver .chen .modules .postgresql ;
2
2
3
+ import org .apache .commons .lang3 .StringUtils ;
3
4
import org .jumpserver .chen .framework .datasource .ConnectionManager ;
4
5
import org .jumpserver .chen .framework .datasource .base .BaseSQLActuator ;
5
6
import org .jumpserver .chen .framework .datasource .sql .SQL ;
@@ -15,30 +16,45 @@ public PostgresqlActuator(ConnectionManager connectionManager) {
15
16
super (connectionManager );
16
17
}
17
18
19
+ private String dbName ;
20
+
18
21
public PostgresqlActuator (PostgresqlActuator sqlActuator , Connection connection ) {
19
22
super (sqlActuator , connection );
20
23
}
21
24
22
25
@ Override
23
26
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 ) );
26
29
}
27
30
28
31
@ Override
29
32
public List <String > getSchemas () throws SQLException {
30
33
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 () ;
32
35
}
33
36
34
37
@ Override
35
38
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 ]));
37
41
}
38
42
39
43
@ Override
40
44
public SQLExecutePlan createPlan (String schema , String table , SQLQueryParams sqlQueryParams ) throws SQLException {
41
45
var sql = SQL .of ("select * from \" ?\" .\" ?\" " , schema , table );
42
46
return this .createPlan (sql , sqlQueryParams );
43
47
}
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
+ }
44
60
}
0 commit comments