beachmachine / django-jython

Database backends and extensions for Django development on top of Jython.
BSD 3-Clause "New" or "Revised" License
21 stars 7 forks source link

zxJDBC sqlite backend raises exception when performing a max agregate on an empty table #6

Closed thjc closed 9 years ago

thjc commented 9 years ago

We are using django 1.7.3 with reversion 1.8.4 and jython 2.7.

If we use the ignore_duplicate_revisions = True option for reversion it will perform a Max query on an empty table. With regular python backends, and the jdbc mysql backend these return an empty set. The zxJDBC sqlite backend throws an exception instead.

Specifically it ends up executing the following query

Executing sqlite query (u'SELECT MAX("reversion_version"."revision_id") AS "revision__max" FROM "reversion_version" INNER JOIN "reversion_revision" ON ( "reversion_version"."revision_id" = "reversion_revision"."id" ) WHERE ("reversion_revision"."manager_slug" = %s AND "reversion_version"."content_type_id" = %s AND "reversion_version"."object_id" = %s)', (u'default', 9, u'1')) {}

Which results in: Got an exception: column -1 out of bounds [1,1] [SQLCode: 0]

I am not sure the perfect place to resolve this, but it seems since it only affects jython, and only sqlite (that I know of) that the doj layer would be the place to catch this.

Modifying the SQliteCursorWrapper to add the execute wrapper as follows works around this issue for us. It should have minimal side effects since the exception is quite specific, but there could be a much better fix for this.

class SQLiteCursorWrapper(CursorWrapper):
    def close(self):
        try:
            return self.cursor.close()
        except BaseDatabaseWrapper.Database.ProgrammingError:
            pass

    def execute(self, *args, **kwargs):
        try:
            return super(SQLiteCursorWrapper, self).execute(*args, **kwargs)
        except Exception as e:
            if e.message == "column -1 out of bounds [1,1] [SQLCode: 0]":
                return None
            else:
                raise e
beachmachine commented 9 years ago

Hi!

Thank you for the bug report. I could reproduce the issue, included your fix, and added some test cases for the issue. Now, all database backends pass the test.