01: /*
02: * GenericSchemaInfoReader.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.db;
13:
14: import java.sql.ResultSet;
15: import java.sql.SQLException;
16: import java.sql.Statement;
17: import workbench.log.LogMgr;
18: import workbench.resource.Settings;
19: import workbench.util.SqlUtil;
20: import workbench.util.StringUtil;
21:
22: /**
23: *
24: * @author support@sql-workbench.net
25: */
26: public class GenericSchemaInfoReader implements SchemaInformationReader {
27: private String schemaQuery = null;
28:
29: public GenericSchemaInfoReader(String dbid) {
30: this .schemaQuery = Settings.getInstance().getProperty(
31: "workbench.db." + dbid + ".currentschema.query", null);
32: }
33:
34: /**
35: * Retrieves the currently active schema from the server.
36: * This is done by running the query configured for the passed dbid.
37: * If no query is configured or an error is thrown, this method
38: * returns null
39: * @see #GenericSchemaInfoReader(String)
40: * @see workbench.db.DbMetadata#getDbId()
41: */
42: public String getCurrentSchema(WbConnection conn) {
43: if (conn == null)
44: return null;
45: if (StringUtil.isEmptyString(this .schemaQuery))
46: return null;
47: Statement stmt = null;
48: ResultSet rs = null;
49: String currentSchema = null;
50: try {
51: stmt = conn.createStatementForQuery();
52: rs = stmt.executeQuery(schemaQuery);
53: if (rs.next()) {
54: currentSchema = rs.getString(1);
55: }
56: if (currentSchema != null)
57: currentSchema = currentSchema.trim();
58: } catch (Exception e) {
59: LogMgr.logWarning(
60: "GenericSchemaInfoReader.getCurrentSchema()",
61: "Error reading current schema using query: "
62: + schemaQuery, e);
63: if (e instanceof SQLException) {
64: // When a SQLException is thrown, we assume an error with the configured
65: // query, so it's disabled to avoid subsequent errors
66: this.schemaQuery = null;
67: }
68: currentSchema = null;
69: } finally {
70: SqlUtil.closeAll(rs, stmt);
71: }
72: return currentSchema;
73:
74: }
75:
76: }
|