From 3c39d584ffc638913ff5844ccd1601d67c8f20f0 Mon Sep 17 00:00:00 2001 From: Luc Boudreau Date: Mon, 15 Sep 2008 17:17:52 +0000 Subject: [PATCH] Fixed https://sourceforge.net/forum/forum.php?thread_id=2248227&forum_id=577988 Not providing a catalog name now makes the driver use the first one returned by the server. Created a test case for this fix too. git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@115 c6a108a4-781c-0410-a6c6-c2d559e19af0 --- .../driver/xmla/XmlaOlap4jConnection.java | 48 +++++++++++++++---- .../olap4j/driver/xmla/XmlaOlap4jDriver.java | 4 +- testsrc/org/olap4j/ConnectionTest.java | 35 ++++++++++++++ 3 files changed, 76 insertions(+), 11 deletions(-) diff --git a/src/org/olap4j/driver/xmla/XmlaOlap4jConnection.java b/src/org/olap4j/driver/xmla/XmlaOlap4jConnection.java index 59c9195..97c2ca3 100644 --- a/src/org/olap4j/driver/xmla/XmlaOlap4jConnection.java +++ b/src/org/olap4j/driver/xmla/XmlaOlap4jConnection.java @@ -365,7 +365,25 @@ public void setCatalog(String catalog) throws SQLException { this.catalogName = catalog; } - public String getCatalog() { + /* (non-Javadoc) + * @see java.sql.Connection#getCatalog() + */ + public String getCatalog() throws OlapException { + if (this.catalogName == null) { + // This means that no particular catalog name + // was specified by the user. + this.catalogName = this.getCatalogs().get(0).getName(); + } else { + // We must verify that the requested catalog name + // exists in the metadata. + Catalog buf = this.getCatalogs().get(this.catalogName); + if (buf != null) { + this.catalogName = buf.getName(); + } else { + throw new OlapException("There is no catalog named " + + this.catalogName + " available to query against."); + } + } return catalogName; } @@ -513,7 +531,7 @@ public synchronized org.olap4j.metadata.Schema getSchema() throws OlapException final XmlaOlap4jCatalog catalog = (XmlaOlap4jCatalog) this.olap4jDatabaseMetaData.getCatalogObjects().get( - catalogName); + this.getCatalog()); this.olap4jSchema = (XmlaOlap4jSchema) catalog.getSchemas() .get(0); } @@ -667,10 +685,6 @@ public String generateRequest( MetadataRequest metadataRequest, Object[] restrictions) throws OlapException { - final boolean datasourceDependentRequest = - metadataRequest.requiresDatasourceName(); - final String catalog = - context.olap4jConnection.getCatalog(); final String content = "Data"; final String encoding = proxy.getEncodingCharsetName(); final StringBuilder buf = new StringBuilder( @@ -716,12 +730,16 @@ public String generateRequest( + " \n"); // Add the datasource node only if this request requires it. - if (datasourceDependentRequest) { + if (metadataRequest.requiresDatasourceName()) { buf.append(" "); buf.append(xmlEncode(context.olap4jConnection.getDataSourceInfo())); - buf.append("\n" - + " "); - buf.append(xmlEncode(catalog)); + buf.append("\n"); + } + + // Add the catalog node only if this request requires it. + if (metadataRequest.requiresCatalogName()) { + buf.append(" "); + buf.append(xmlEncode(context.olap4jConnection.getCatalog())); buf.append("\n"); } @@ -1664,6 +1682,16 @@ enum MetadataRequest { public boolean requiresDatasourceName() { return this != DISCOVER_DATASOURCES; } + + /** + * Returns whether this request requires a + * {@code <CatalogName>} element. + * + * @return whether this request requires a DatasourceName element + */ + public boolean requiresCatalogName() { + return this != DBSCHEMA_CATALOGS; + } } private static final Pattern LOWERCASE_PATTERN = Pattern.compile(".*[a-z].*"); diff --git a/src/org/olap4j/driver/xmla/XmlaOlap4jDriver.java b/src/org/olap4j/driver/xmla/XmlaOlap4jDriver.java index 38038ad..e6de2c2 100644 --- a/src/org/olap4j/driver/xmla/XmlaOlap4jDriver.java +++ b/src/org/olap4j/driver/xmla/XmlaOlap4jDriver.java @@ -55,7 +55,9 @@ * * Server URL of HTTP server. Required. * - * Catalog Catalog name to use. Required. + * Catalog Catalog name to use. + * By default, the first one returned by the + * XMLA server will be used. * * Provider Name of the XMLA provider. * diff --git a/testsrc/org/olap4j/ConnectionTest.java b/testsrc/org/olap4j/ConnectionTest.java index 9bae735..f67cff8 100644 --- a/testsrc/org/olap4j/ConnectionTest.java +++ b/testsrc/org/olap4j/ConnectionTest.java @@ -12,6 +12,7 @@ import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.olap4j.impl.Olap4jUtil; +import org.olap4j.driver.xmla.*; import org.olap4j.mdx.*; import org.olap4j.mdx.parser.*; import org.olap4j.metadata.*; @@ -304,6 +305,40 @@ public void testConnectionUnwrap() throws SQLException { } } + public void testXmlaCatalogParameter() throws Exception { + if (tester.getFlavor() == TestContext.Tester.Flavor.XMLA) + { + // We won't use the tester itself since we want to test + // creating a connection with and without a Catalog parameter. + Properties info = new Properties(); + connection = + DriverManager.getConnection( + tester.getURL().replaceFirst("\\;Catalog=FoodMart", ""), + info); + assertEquals("FoodMart", connection.getCatalog()); + + info.setProperty( + XmlaOlap4jDriver.Property.Catalog.name(), "FoodMart"); + connection = + DriverManager.getConnection( + tester.getURL().replaceFirst("\\;Catalog=FoodMart", ""), + info); + assertEquals("FoodMart", connection.getCatalog()); + + info.setProperty( + XmlaOlap4jDriver.Property.Catalog.name(), "FoodMartError"); + try { + connection = DriverManager.getConnection( + tester.getURL().replaceFirst("\\;Catalog=FoodMart", ""), + info); + connection.getCatalog(); + } catch (OlapException e) { + return; + } + fail("XmlaOlap4jConnection did not detect an inexistant catalog name."); + } + } + public void testStatement() throws SQLException { connection = tester.createConnection(); Statement statement = connection.createStatement();