Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…_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
  • Loading branch information
lucboudreau committed Sep 15, 2008
1 parent 82777df commit 3c39d58
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
48 changes: 38 additions & 10 deletions src/org/olap4j/driver/xmla/XmlaOlap4jConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -716,12 +730,16 @@ public String generateRequest(
+ " <PropertyList>\n");

// Add the datasource node only if this request requires it.
if (datasourceDependentRequest) {
if (metadataRequest.requiresDatasourceName()) {
buf.append(" <DataSourceInfo>");
buf.append(xmlEncode(context.olap4jConnection.getDataSourceInfo()));
buf.append("</DataSourceInfo>\n"
+ " <Catalog>");
buf.append(xmlEncode(catalog));
buf.append("</DataSourceInfo>\n");
}

// Add the catalog node only if this request requires it.
if (metadataRequest.requiresCatalogName()) {
buf.append(" <Catalog>");
buf.append(xmlEncode(context.olap4jConnection.getCatalog()));
buf.append("</Catalog>\n");
}

Expand Down Expand Up @@ -1664,6 +1682,16 @@ enum MetadataRequest {
public boolean requiresDatasourceName() {
return this != DISCOVER_DATASOURCES;
}

/**
* Returns whether this request requires a
* {@code &lt;CatalogName&gt;} 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].*");
Expand Down
4 changes: 3 additions & 1 deletion src/org/olap4j/driver/xmla/XmlaOlap4jDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
*
* <tr> <td>Server</td> <td>URL of HTTP server. Required.</td> </tr>
*
* <tr> <td>Catalog</td> <td>Catalog name to use. Required.</td> </tr>
* <tr> <td>Catalog</td> <td>Catalog name to use.
* By default, the first one returned by the
* XMLA server will be used.</td> </tr>
*
* <tr> <td>Provider</td> <td>Name of the XMLA provider.</td> </tr>
*
Expand Down
35 changes: 35 additions & 0 deletions testsrc/org/olap4j/ConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 3c39d58

Please sign in to comment.