Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDBC unwrapping doesn't work #21

Closed
imd opened this issue Aug 23, 2012 · 5 comments
Closed

JDBC unwrapping doesn't work #21

imd opened this issue Aug 23, 2012 · 5 comments
Labels

Comments

@imd
Copy link

imd commented Aug 23, 2012

Problem

I'm using Olap4j, and, as documented on page 11 of this PDF, Java is supposed to automatically unwrap JDBC connection objects to gain access to their true connection, which works under Jython but not PyJNIus. (This seems like magic to me, so I don't understand the details.)

Steps to reproduce

  1. Download Pentaho Mondrian. Create a Postgres table called 'foodmart', username 'postgres', password 'password'. You may need to load the sample data as described in the Mondrian documentation.
  2. Create the test.py file given at the bottom of this report in the Mondrian directory. Run it. Under Jython 2.7, it runs fine, but under PyJNIus, it reports:
Traceback (most recent call last):
  File "test.py", line 23, in <module>
    result = connection.prepareOlapStatement(mdx).executeQuery()
AttributeError: 'java.sql.Connection' object has no attribute 'prepareOlapStatement'

test.py:

import sys

_is_jython = sys.platform.startswith("java")
if _is_jython:
    from java.sql import DriverManager
    from org.olap4j import OlapConnection
else:
    from jnius import autoclass
    DriverManager = autoclass('java.sql.DriverManager')
    OlapConnection = autoclass('org.olap4j.OlapConnection')


url = 'jdbc:mondrian:JdbcDrivers=org.postgresql.Driver;Jdbc=jdbc:postgresql://localhost/foodmart?user=postgres&password=password;Catalog=demo/FoodMart.xml;'

mdx = '''
SELECT
  [Measures].[Customer Count] ON COLUMNS,
  [Gender].[Gender].Members ON ROWS
FROM [Sales]
'''

connection = DriverManager.getConnection(url)
result = connection.prepareOlapStatement(mdx).executeQuery()
@tito
Copy link
Member

tito commented Aug 24, 2012

Hi,

Ok, i didn't tried the example, but a fast research on the internet show that DriverManager.getConnection(url) return a Connection from java.sql.Connection on which the method prepareOlapStatement doesn't exist. What you need is an OlapConnection.

The difference here is that we guess the type from the signature. DriverManager.getConnection have no indication about the OlapConnection. And since you don't declare the type of the variable... You need to cast.

Try:

connection = DriverManager.getConnection(url)
connection = cast('org.olap4j.OlapConnection', connection)
result = connection.prepareOlapStatement(mdx).executeQuery()

This might work, not tested. Try and report here :)

@apalala
Copy link
Contributor

apalala commented Aug 24, 2012

I think that the problem is that Jnius handles objects returned from Java as JavaObject instead of registering their class an returning a proper JavaClass descendant instance.

I'm sorry I have so much to say, and so little time to contribute right now.

@tito
Copy link
Member

tito commented Aug 24, 2012

Can you do a "print connection" and give the result ?

@tito
Copy link
Member

tito commented Aug 24, 2012

Try to:

connection = DriverManager.getConnection(url)
print 'before', connection
connection = cast(OlapConnection, connection)
print 'after', connection

@imd imd closed this as completed Aug 24, 2012
@imd
Copy link
Author

imd commented Aug 24, 2012

OK, the Jython worked by a fluke; the equivalent Java wasn't correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants