Skip to content

Commit

Permalink
Work around table_exists? issue on derby with new SELECT NULL syntax
Browse files Browse the repository at this point in the history
Derby must do some aggressive optimization, where if you don't actually
need the table, it doesn't even check if it exists.  So refactor the
default handling to make it easy to override, and override it for
derby.
  • Loading branch information
jeremyevans committed Jan 13, 2012
1 parent ef39066 commit 0e9de00
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/sequel/adapters/jdbc/derby.rb
Expand Up @@ -37,6 +37,12 @@ def svn_version

private

# Derby optimizes away Sequel's default check of SELECT NULL FROM table,
# so use a SELECT * FROM table there.
def _table_exists?(ds)
ds.first
end

# Derby-specific syntax for renaming columns and changing a columns type/nullity.
def alter_table_sql(table, op)
case op[:op]
Expand Down
10 changes: 8 additions & 2 deletions lib/sequel/database/query.rb
Expand Up @@ -191,12 +191,12 @@ def schema(table, opts={})
def table_exists?(name)
sch, table_name = schema_and_table(name)
name = SQL::QualifiedIdentifier.new(sch, table_name) if sch
from(name).get(Sequel::NULL)
_table_exists?(from(name))
true
rescue DatabaseError
false
end

# Return all tables in the database as an array of symbols.
#
# DB.tables # => [:albums, :artists]
Expand Down Expand Up @@ -241,6 +241,12 @@ def views(opts={})

private

# Should raise an error if the table doesn't not exist,
# and not raise an error if the table does exist.
def _table_exists?(ds)
ds.get(Sequel::NULL)
end

# Internal generic transaction method. Any exception raised by the given
# block will cause the transaction to be rolled back. If the exception is
# not a Sequel::Rollback, the error will be reraised. If no exception occurs
Expand Down

0 comments on commit 0e9de00

Please sign in to comment.