1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package com.sun.star.sdbcx.comp.postgresql; 23 24 import java.util.ArrayList; 25 import java.util.List; 26 27 import org.apache.openoffice.comp.sdbc.dbtools.comphelper.CompHelper; 28 import org.apache.openoffice.comp.sdbc.dbtools.sdbcx.OCatalog; 29 import org.apache.openoffice.comp.sdbc.dbtools.sdbcx.OContainer; 30 31 import com.sun.star.container.ElementExistException; 32 import com.sun.star.sdbc.SQLException; 33 import com.sun.star.sdbc.XResultSet; 34 import com.sun.star.sdbc.XRow; 35 import com.sun.star.uno.Any; 36 import com.sun.star.uno.UnoRuntime; 37 38 public class PostgresqlCatalog extends OCatalog { PostgresqlCatalog(PostgresqlConnection connection)39 public PostgresqlCatalog(PostgresqlConnection connection) throws SQLException { 40 super(connection.getMetaData()); 41 } 42 43 @Override refreshTables()44 public void refreshTables() { 45 XResultSet results = null; 46 try { 47 // Using { "VIEW", "TABLE", "%" } shows INFORMATION_SCHEMA and others, but it also shows indexes :-( 48 results = metadata.getTables(Any.VOID, "%", "%", new String[] { "VIEW", "TABLE" }); 49 XRow row = UnoRuntime.queryInterface(XRow.class, results); 50 List<String> names = new ArrayList<>(); 51 while (results.next()) { 52 String name = buildName(row); 53 System.out.println("Table " + name); 54 names.add(name); 55 } 56 if (tables == null) { 57 tables = new PostgresqlTables(this, metadata, this, names); 58 } else { 59 tables.refill(names); 60 } 61 } catch (ElementExistException | SQLException exception) { 62 throw new com.sun.star.uno.RuntimeException("Error", exception); 63 } finally { 64 CompHelper.disposeComponent(results); 65 } 66 } 67 68 @Override refreshViews()69 public void refreshViews() { 70 XResultSet results = null; 71 try { 72 results = metadata.getTables(Any.VOID, "%", "%", new String[] { "VIEW" }); 73 XRow row = UnoRuntime.queryInterface(XRow.class, results); 74 List<String> names = new ArrayList<>(); 75 while (results.next()) { 76 String name = buildName(row); 77 names.add(name); 78 } 79 if (views == null) { 80 views = new PostgresqlViews(this, metadata, this, names); 81 } else { 82 views.refill(names); 83 } 84 } catch (ElementExistException | SQLException exception) { 85 throw new com.sun.star.uno.RuntimeException("Error", exception); 86 } finally { 87 CompHelper.disposeComponent(results); 88 } 89 } 90 91 @Override refreshGroups()92 public void refreshGroups() { 93 } 94 95 @Override refreshUsers()96 public void refreshUsers() { 97 } 98 getTablesInternal()99 synchronized OContainer getTablesInternal() { 100 return tables; 101 } 102 } 103