1414 */
1515package com .github .adejanovski .cassandra .jdbc ;
1616
17+ import com .datastax .driver .core .Metadata ;
18+
19+ import java .sql .*;
20+
1721import static com .github .adejanovski .cassandra .jdbc .Utils .NOT_SUPPORTED ;
1822import static com .github .adejanovski .cassandra .jdbc .Utils .NO_INTERFACE ;
1923
20- import java .sql .Connection ;
21- import java .sql .DatabaseMetaData ;
22- import java .sql .ResultSet ;
23- import java .sql .RowIdLifetime ;
24- import java .sql .SQLException ;
25- import java .sql .SQLFeatureNotSupportedException ;
26- import java .sql .SQLSyntaxErrorException ;
27- import java .util .List ;
28-
29- import com .datastax .driver .core .ColumnMetadata ;
30- import com .datastax .driver .core .KeyspaceMetadata ;
31- import com .datastax .driver .core .Metadata ;
32- import com .datastax .driver .core .TableMetadata ;
33-
3424class CassandraDatabaseMetaData implements DatabaseMetaData
3525{
3626 private CassandraConnection connection ;
@@ -40,7 +30,7 @@ class CassandraDatabaseMetaData implements DatabaseMetaData
4030 public CassandraDatabaseMetaData (CassandraConnection connection ) throws SQLException
4131 {
4232 this .connection = connection ;
43- this .statement = new CassandraStatement (connection );
33+ this .statement = new CassandraStatement (this . connection );
4434 this .metadata = this .connection .getClusterMetadata ();
4535 }
4636
@@ -112,7 +102,10 @@ public String getCatalogTerm() throws SQLException
112102
113103 public ResultSet getCatalogs () throws SQLException
114104 {
115- ResultSet rs = MetadataResultSets .instance .makeCatalogs (statement );
105+ if (statement .isClosed ()){
106+ statement = new CassandraStatement (this .connection );
107+ }
108+ ResultSet rs = MetadataResultSets .instance .makeCatalogs (statement );
116109 return rs ;
117110
118111 }
@@ -129,13 +122,17 @@ public ResultSet getColumnPrivileges(String arg0, String arg1, String arg2, Stri
129122
130123 public ResultSet getColumns (String catalog ,String schemaPattern , String tableNamePattern , String columnNamePattern ) throws SQLException
131124 {
125+ if (statement .isClosed ()){
126+ statement = new CassandraStatement (this .connection );
127+ }
132128 if (catalog == null || connection .getCatalog ().equals (catalog ))
133129 {
130+ statement .connection = connection ;
134131 if (schemaPattern == null ) schemaPattern = connection .getSchema (); //limit to current schema if set
135132 ResultSet rs = MetadataResultSets .instance .makeColumns (statement , schemaPattern , tableNamePattern ,columnNamePattern );
136133 return rs ;
137134 }
138- return new CassandraResultSet ();
135+ return new CassandraResultSet ();
139136 }
140137
141138 public Connection getConnection () throws SQLException
@@ -225,13 +222,16 @@ public ResultSet getImportedKeys(String arg0, String arg1, String arg2) throws S
225222
226223 public ResultSet getIndexInfo (String catalog , String schema , String table , boolean unique , boolean approximate ) throws SQLException
227224 {
225+ if (statement .isClosed ()){
226+ statement = new CassandraStatement (this .connection );
227+ }
228228 if (catalog == null || connection .getCatalog ().equals (catalog ))
229229 {
230230 if (schema == null ) schema = connection .getSchema (); //limit to current schema if set
231231 ResultSet rs = MetadataResultSets .instance .makeIndexes (statement , schema , table ,unique ,approximate );
232232 return rs ;
233233 }
234- return new CassandraResultSet ();
234+ return new CassandraResultSet ();
235235 }
236236
237237 public int getJDBCMajorVersion () throws SQLException
@@ -352,13 +352,16 @@ public String getNumericFunctions() throws SQLException
352352
353353 public ResultSet getPrimaryKeys (String catalog , String schema , String table ) throws SQLException
354354 {
355+ if (statement .isClosed ()){
356+ statement = new CassandraStatement (this .connection );
357+ }
355358 if (catalog == null || connection .getCatalog ().equals (catalog ))
356359 {
357360 if (schema == null ) schema = connection .getSchema (); //limit to current schema if set
358361 ResultSet rs = MetadataResultSets .instance .makePrimaryKeys (statement , schema , table );
359362 return rs ;
360363 }
361- return new CassandraResultSet ();
364+ return new CassandraResultSet ();
362365 }
363366
364367 public ResultSet getProcedureColumns (String arg0 , String arg1 , String arg2 , String arg3 ) throws SQLException
@@ -403,18 +406,26 @@ public String getSchemaTerm() throws SQLException
403406
404407 public ResultSet getSchemas () throws SQLException
405408 {
406- ResultSet rs = MetadataResultSets .instance .makeSchemas (statement , null );
407- return rs ;
409+ if (statement .isClosed ()){
410+ statement = new CassandraStatement (this .connection );
411+ }
412+ ResultSet rs = MetadataResultSets .instance .makeSchemas (statement , null );
413+ return rs ;
408414
409415
410416 }
411417
412418 public ResultSet getSchemas (String catalog , String schemaPattern ) throws SQLException
413419 {
414- if (!(catalog == null || catalog .equals (statement .connection .getCatalog ()) ))
415- throw new SQLSyntaxErrorException ("catalog name must exactly match or be null" );
420+ if (statement .isClosed ()){
421+ statement = new CassandraStatement (this .connection );
422+ }
423+ if (!(catalog == null || catalog .equals (statement .connection .getCatalog ()) )){
424+ throw new SQLSyntaxErrorException ("catalog name must exactly match or be null" );
425+ }
416426
417427 ResultSet rs = MetadataResultSets .instance .makeSchemas (statement , schemaPattern );
428+
418429 return rs ;
419430
420431 //return new CassandraResultSet();
@@ -452,7 +463,10 @@ public ResultSet getTablePrivileges(String arg0, String arg1, String arg2) throw
452463
453464 public ResultSet getTableTypes () throws SQLException
454465 {
455- ResultSet result = MetadataResultSets .instance .makeTableTypes (statement );
466+ if (statement .isClosed ()){
467+ statement = new CassandraStatement (this .connection );
468+ }
469+ ResultSet result = MetadataResultSets .instance .makeTableTypes (statement );
456470 return result ;
457471 }
458472
@@ -473,6 +487,9 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
473487 }
474488 if ((catalog == null || connection .getCatalog ().equals (catalog )) && askingForTable )
475489 {
490+ if (statement .isClosed ()){
491+ statement = new CassandraStatement (this .connection );
492+ }
476493 //if (schemaPattern == null) schemaPattern = connection.getSchema(); //limit to current schema if set
477494 ResultSet rs = MetadataResultSets .instance .makeTables (statement , schemaPattern , tableNamePattern );
478495 return rs ;
0 commit comments