Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add an AS400 JDBC subadapter
Dataset#insert probably won't return an autoincrementing primary key,
but limit/offset support is emulated and should work.
  • Loading branch information
jeremyevans committed Mar 22, 2010
1 parent 9c37441 commit 6d580e9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG
@@ -1,6 +1,8 @@
=== HEAD

* Make JDBC adapter recognize as400 connection strings and automatically load the Java class (need jt400.jar in classpath) (jeremyevans)
* Add an AS400 JDBC subadapter (need jt400.jar in classpath) (jeremyevans, bhauff)

* Fix the emulated MSSQL offset support when core extensions are not used (jeremyevans)

* Make Sequel::BasicObject work correctly on Rubinius (kronos)

Expand Down
2 changes: 2 additions & 0 deletions lib/sequel/adapters/jdbc.rb
Expand Up @@ -69,6 +69,8 @@ module JavaSQL
org.h2.Driver
end,
:as400=>proc do |db|
Sequel.ts_require 'adapters/jdbc/as400'
db.extend(Sequel::JDBC::AS400::DatabaseMethods)
com.ibm.as400.access.AS400JDBCDriver
end
}
Expand Down
58 changes: 58 additions & 0 deletions lib/sequel/adapters/jdbc/as400.rb
@@ -0,0 +1,58 @@
module Sequel
module JDBC
# Database and Dataset support for AS400 databases accessed via JDBC.
module AS400
# Instance methods for AS400 Database objects accessed via JDBC.
module DatabaseMethods
# AS400 uses the :as400 database type.
def database_type
:as400
end

# Return Sequel::JDBC::AS400::Dataset object with the given opts.
def dataset(opts=nil)
Sequel::JDBC::AS400::Dataset.new(self, opts)
end

# TODO: Fix for AS400
def last_insert_id(conn, opts={})
nil
end
end

# Dataset class for AS400 datasets accessed via JDBC.
class Dataset < JDBC::Dataset
WILDCARD = Sequel::LiteralString.new('*').freeze

# AS400 needs to use a couple of subselects for all limits and offsets.
def select_sql
return super unless l = @opts[:limit]
o = @opts[:offset] || 0
order = @opts[:order]
dsa1 = dataset_alias(1)
dsa2 = dataset_alias(2)
rn = row_number_column
irn = Sequel::SQL::Identifier.new(rn).qualify(dsa2)
subselect_sql(unlimited.
from_self(:alias=>dsa1).
select_more(Sequel::SQL::QualifiedIdentifier.new(dsa1, WILDCARD),
Sequel::SQL::WindowFunction.new(SQL::Function.new(:ROW_NUMBER), Sequel::SQL::Window.new(:order=>order)).as(rn)).
from_self(:alias=>dsa2).
select(Sequel::SQL::QualifiedIdentifier.new(dsa2, WILDCARD)).
where((irn > o) & (irn <= l + o)))
end

def supports_window_functions?
true
end

private

# The alias to use for the row_number column when emulating LIMIT and OFFSET
def row_number_column
:x_sequel_row_number_x
end
end
end
end
end

0 comments on commit 6d580e9

Please sign in to comment.