@Test public void testGetSchema() throws Exception { PowerMockito.mockStatic(HFCAClient.class); when(HFCAClient.createNewInstance(anyString(), any())).thenReturn(mockCA); PowerMockito.mockStatic(HFClient.class); when(HFClient.createNewInstance()).thenReturn(mockClient); Channel mockChannel = mock(Channel.class); when(mockClient.newChannel(anyString())).thenReturn(mockChannel); HyperUser mockuser = mock(HyperUser.class); when(mockuser.isEnrolled()).thenReturn(true); Store mockStore = mock(Store.class); PowerMockito.whenNew(Store.class).withAnyArguments().thenReturn(mockStore); String configPath = new File("src/test/resources/blockchain-query").getAbsolutePath(); Class.forName("com.impetus.fabric.jdbc.FabricDriver"); Connection conn = DriverManager.getConnection("jdbc:fabric: "admin", "adminpw"); BlkchnStatement stat = (BlkchnStatement) conn.createStatement(); String sql = "Select block_no, previous_hash from block"; ResultSetMetaData metadata = stat.getSchema(sql); int columnCount = metadata.getColumnCount(); assertEquals(2, columnCount); assertEquals(metadata.getColumnName(1), "block_no"); assertEquals(metadata.getColumnName(2), "previous_hash"); assertEquals(metadata.getColumnType(1), Types.BIGINT); assertEquals(metadata.getColumnType(2), Types.VARCHAR); }
@Override public ResultSetMetaData getSchema(String sql) { LogicalPlan logicalPlan = getLogicalPlan(sql); if(logicalPlan.getType().equals(SQLType.QUERY)) { Table table = logicalPlan.getQuery().getChildType(FromItem.class, 0).getChildType(Table.class, 0); String tableName = table.getChildType(IdentifierNode.class, 0).getValue(); DataFrame df = getDataFrameWithSchema(tableName, logicalPlan); ResultSet rs = new FabricResultSet(this, df, tableName); try { return rs.getMetaData(); } catch (SQLException e) { throw new BlkchnException(e); } finally { try { rs.close(); } catch (SQLException e) { } } } throw new BlkchnException("This method doesn't support sql statement of type " + logicalPlan.getType()); }
FabricStatement implements BlkchnStatement { @Override public ResultSetMetaData getSchema(String sql) { LogicalPlan logicalPlan = getLogicalPlan(sql); if(logicalPlan.getType().equals(SQLType.QUERY)) { Table table = logicalPlan.getQuery().getChildType(FromItem.class, 0).getChildType(Table.class, 0); String tableName = table.getChildType(IdentifierNode.class, 0).getValue(); DataFrame df = getDataFrameWithSchema(tableName, logicalPlan); ResultSet rs = new FabricResultSet(this, df, tableName); try { return rs.getMetaData(); } catch (SQLException e) { throw new BlkchnException(e); } finally { try { rs.close(); } catch (SQLException e) { } } } throw new BlkchnException("This method doesn't support sql statement of type " + logicalPlan.getType()); } }
FabricStatement implements BlkchnStatement { @Override public ResultSetMetaData getSchema(String sql) { LogicalPlan logicalPlan = getLogicalPlan(sql); if(logicalPlan.getType().equals(SQLType.QUERY)) { Table table = logicalPlan.getQuery().getChildType(FromItem.class, 0).getChildType(Table.class, 0); String tableName = table.getChildType(IdentifierNode.class, 0).getValue(); DataFrame df = getDataFrameWithSchema(tableName, logicalPlan); ResultSet rs = new FabricResultSet(this, df, tableName); try { return rs.getMetaData(); } catch (SQLException e) { throw new BlkchnException(e); } finally { try { rs.close(); } catch (SQLException e) { } } } throw new BlkchnException("This method doesn't support sql statement of type " + logicalPlan.getType()); } FabricStatement(FabricConnection conn, int type, int concurrency, int holdability); }
FabricStatement implements BlkchnStatement { @Override public ResultSetMetaData getSchema(String sql) { LogicalPlan logicalPlan = getLogicalPlan(sql); if(logicalPlan.getType().equals(SQLType.QUERY)) { Table table = logicalPlan.getQuery().getChildType(FromItem.class, 0).getChildType(Table.class, 0); String tableName = table.getChildType(IdentifierNode.class, 0).getValue(); DataFrame df = getDataFrameWithSchema(tableName, logicalPlan); ResultSet rs = new FabricResultSet(this, df, tableName); try { return rs.getMetaData(); } catch (SQLException e) { throw new BlkchnException(e); } finally { try { rs.close(); } catch (SQLException e) { } } } throw new BlkchnException("This method doesn't support sql statement of type " + logicalPlan.getType()); } FabricStatement(FabricConnection conn, int type, int concurrency, int holdability); boolean isWrapperFor(Class<?> iface); T unwrap(Class<T> iface); void addBatch(String sql); void cancel(); void clearBatch(); void clearWarnings(); void close(); void closeOnCompletion(); boolean execute(String sql); boolean execute(String sql, int autoGeneratedKeys); boolean execute(String sql, int[] columnIndexes); boolean execute(String sql, String[] columnNames); int[] executeBatch(); ResultSet executeQuery(String query); int executeUpdate(String sql); int executeUpdate(String sql, int autoGeneratedKeys); int executeUpdate(String sql, int[] columnIndexes); int executeUpdate(String sql, String[] columnNames); Connection getConnection(); int getFetchDirection(); int getFetchSize(); ResultSet getGeneratedKeys(); int getMaxFieldSize(); int getMaxRows(); boolean getMoreResults(); boolean getMoreResults(int current); int getQueryTimeout(); ResultSet getResultSet(); int getResultSetConcurrency(); int getResultSetHoldability(); int getResultSetType(); int getUpdateCount(); SQLWarning getWarnings(); boolean isCloseOnCompletion(); boolean isClosed(); boolean isPoolable(); void setCursorName(String name); void setEscapeProcessing(boolean enable); void setFetchDirection(int direction); void setFetchSize(int rows); void setMaxFieldSize(int max); void setMaxRows(int max); void setPoolable(boolean poolable); void setQueryTimeout(int seconds); @Override void setPageRange(RangeNode<?> pageRange); @Override Number getBlockHeight(); @Override RangeNode getProbableRange(String sql); @Override ResultSetMetaData getSchema(String sql); @Override int getArrayElementType(String table, String column); }
FabricStatement implements BlkchnStatement { @Override public ResultSetMetaData getSchema(String sql) { LogicalPlan logicalPlan = getLogicalPlan(sql); if(logicalPlan.getType().equals(SQLType.QUERY)) { Table table = logicalPlan.getQuery().getChildType(FromItem.class, 0).getChildType(Table.class, 0); String tableName = table.getChildType(IdentifierNode.class, 0).getValue(); DataFrame df = getDataFrameWithSchema(tableName, logicalPlan); ResultSet rs = new FabricResultSet(this, df, tableName); try { return rs.getMetaData(); } catch (SQLException e) { throw new BlkchnException(e); } finally { try { rs.close(); } catch (SQLException e) { } } } throw new BlkchnException("This method doesn't support sql statement of type " + logicalPlan.getType()); } FabricStatement(FabricConnection conn, int type, int concurrency, int holdability); boolean isWrapperFor(Class<?> iface); T unwrap(Class<T> iface); void addBatch(String sql); void cancel(); void clearBatch(); void clearWarnings(); void close(); void closeOnCompletion(); boolean execute(String sql); boolean execute(String sql, int autoGeneratedKeys); boolean execute(String sql, int[] columnIndexes); boolean execute(String sql, String[] columnNames); int[] executeBatch(); ResultSet executeQuery(String query); int executeUpdate(String sql); int executeUpdate(String sql, int autoGeneratedKeys); int executeUpdate(String sql, int[] columnIndexes); int executeUpdate(String sql, String[] columnNames); Connection getConnection(); int getFetchDirection(); int getFetchSize(); ResultSet getGeneratedKeys(); int getMaxFieldSize(); int getMaxRows(); boolean getMoreResults(); boolean getMoreResults(int current); int getQueryTimeout(); ResultSet getResultSet(); int getResultSetConcurrency(); int getResultSetHoldability(); int getResultSetType(); int getUpdateCount(); SQLWarning getWarnings(); boolean isCloseOnCompletion(); boolean isClosed(); boolean isPoolable(); void setCursorName(String name); void setEscapeProcessing(boolean enable); void setFetchDirection(int direction); void setFetchSize(int rows); void setMaxFieldSize(int max); void setMaxRows(int max); void setPoolable(boolean poolable); void setQueryTimeout(int seconds); @Override void setPageRange(RangeNode<?> pageRange); @Override Number getBlockHeight(); @Override RangeNode getProbableRange(String sql); @Override ResultSetMetaData getSchema(String sql); @Override int getArrayElementType(String table, String column);  }