Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Dataset#sequence to the shared Oracle Adapter, for returning auto…

…generated primary key values on insert (Fixes #280)

Oracle is like PostgreSQL in that autogenerated primary key values
are handled by sequences.  Unlike Sequel's shared PostgreSQL adapter,
Sequel's shared Oracle adapter doesn't handle automatically
determining the primary key value based on the table.  That may be
possible to fix, but without access to an Oracle database it would
be difficult.

Dataset#sequence provides a workaround for the problem, allowing the
manual specification of the sequence. Dataset#insert is then
overridden to check for an associated sequence, and return the most
recently inserted value upon insert.
  • Loading branch information...
commit c2198d0d296285caca2730cea990484c1a8436b2 1 parent bf06ca4
@jeremyevans authored
Showing with 20 additions and 0 deletions.
  1. +2 −0  CHANGELOG
  2. +18 −0 lib/sequel/adapters/shared/oracle.rb
@@ -1,5 +1,7 @@
=== HEAD
+* Add Dataset#sequence to the shared Oracle Adapter, for returning autogenerated primary key values on insert (jeremyevans) (#280)
* Bring support for modifying joined datasets into Sequel proper, supported on MySQL and PostgreSQL (jeremyevans)
* No longer use native autoreconnection in the mysql adapter (jeremyevans)
18 lib/sequel/adapters/shared/oracle.rb
@@ -107,11 +107,29 @@ def empty?
db[:dual].where(exists).get(1) == nil
+ # If this dataset is associated with a sequence, return the most recently
+ # inserted sequence value.
+ def insert(*args)
+ r = super
+ if s = opts[:sequence]
+ with_sql("SELECT #{literal(s)}.currval FROM dual").single_value.to_i
+ else
+ r
+ end
+ end
# Oracle requires SQL standard datetimes
def requires_sql_standard_datetimes?
+ # Create a copy of this dataset associated to the given sequence name,
+ # which will be used when calling insert to find the most recently
+ # inserted value for the sequence.
+ def sequence(s)
+ clone(:sequence=>s)
+ end
# Oracle does not support DISTINCT ON
def supports_distinct_on?
Please sign in to comment.
Something went wrong with that request. Please try again.