9
9
import java .util .ArrayList ;
10
10
import java .util .List ;
11
11
import java .util .Map ;
12
+ import java .util .Objects ;
12
13
13
14
public class DatabricksSchema extends Schema <DatabricksDatabase , DatabricksTable > {
14
15
/**
@@ -20,25 +21,40 @@ public DatabricksSchema(JdbcTemplate jdbcTemplate, DatabricksDatabase database,
20
21
super (jdbcTemplate , database , name );
21
22
}
22
23
23
- private List <String > fetchAllObjs (String obj ) throws SQLException {
24
+ private List <String > fetchAllObjs (String obj , String column ) throws SQLException {
24
25
List <Map <String , String >> tableInfos = jdbcTemplate .queryForList (
25
26
"show " + obj + "s from " + database .quote (name )
26
27
);
27
28
List <String > tableNames = new ArrayList <String >();
28
29
for (Map <String , String > tableInfo : tableInfos ) {
29
- tableNames .add (tableInfo .get ("tableName" ));
30
+ tableNames .add (tableInfo .get (column ));
30
31
}
31
32
return tableNames ;
32
33
}
34
+
35
+ private List <String > fetchAllSchemas () throws SQLException {
36
+ List <Map <String , String >> schemaInfos = jdbcTemplate .queryForList ("show schemas" );
37
+ List <String > schemaNames = new ArrayList <>();
38
+ for (Map <String , String > schemaInfo : schemaInfos ) {
39
+ schemaNames .add (schemaInfo .get ("databaseName" ));
40
+ }
41
+ return schemaNames ;
42
+ }
43
+
44
+ private List <String > fetchAllTables () throws SQLException {
45
+ var tables = fetchAllObjs ("table" , "tableName" );
46
+ var views = fetchAllObjs ("view" , "viewName" );
47
+ return tables .stream ().filter (t -> !views .contains (t )).toList ();
48
+ }
33
49
34
50
@ Override
35
51
protected boolean doExists () throws SQLException {
36
- return fetchAllObjs ( "table" ). size () > 0 ;
52
+ return fetchAllSchemas (). stream (). anyMatch ( schema -> Objects . equals ( schema , name )) ;
37
53
}
38
54
39
55
@ Override
40
56
protected boolean doEmpty () throws SQLException {
41
- return fetchAllObjs ( "table" ).size () == 0 ;
57
+ return fetchAllTables ( ).size () == 0 ;
42
58
}
43
59
44
60
@ Override
@@ -53,19 +69,18 @@ protected void doDrop() throws SQLException {
53
69
54
70
@ Override
55
71
protected void doClean () throws SQLException {
56
- for (String statement : generateDropStatements ("TABLE" )) {
72
+ for (String statement : generateDropStatements ("TABLE" , fetchAllTables () )) {
57
73
jdbcTemplate .execute (statement );
58
74
}
59
- for (String statement : generateDropStatements ("VIEW" )) {
75
+ for (String statement : generateDropStatements ("VIEW" , fetchAllObjs ( "VIEW" , "viewName" ) )) {
60
76
jdbcTemplate .execute (statement );
61
77
}
62
- for (String statement : generateDropStatements ("FUNCTION" )) {
78
+ for (String statement : generateDropStatements ("FUNCTION" , fetchAllObjs ( "USER FUNCTION" , "function" ) )) {
63
79
jdbcTemplate .execute (statement );
64
80
}
65
81
}
66
82
67
- private List <String > generateDropStatements (String objType ) throws SQLException {
68
- List <String > names = fetchAllObjs (objType );
83
+ private List <String > generateDropStatements (String objType , List <String > names ) throws SQLException {
69
84
List <String > statements = new ArrayList <>();
70
85
for (String domainName : names ) {
71
86
statements .add ("drop " + objType + " if exists " + database .quote (name , domainName ) + ";" );
@@ -76,7 +91,7 @@ private List<String> generateDropStatements(String objType) throws SQLException
76
91
77
92
@ Override
78
93
protected DatabricksTable [] doAllTables () throws SQLException {
79
- List <String > tableNames = fetchAllObjs ( "TABLE" );
94
+ List <String > tableNames = fetchAllTables ( );
80
95
DatabricksTable [] tables = new DatabricksTable [tableNames .size ()];
81
96
for (int i = 0 ; i < tableNames .size (); i ++) {
82
97
tables [i ] = new DatabricksTable (jdbcTemplate , database , this , tableNames .get (i ));
0 commit comments