This accessor allows setting a default transaction mode for new transactions, making it easier to write and maintain database-independent code.
DB#transaction(:mode => ...) overrides the default transaction mode of the database handle. Additionally, there is now additional validation to prevent invalid transaction modes from being used. --- Jeremy Evans <[email protected]> wrote: > In terms of having a default transaction mode for the Database, I'm fine > with adding that ability, since a similar setting already exists for > transaction isolation levels. Thank you for the feedback. I made this an accessor (instead of a connect() option to be consistent with the existing DB#transaction_isolation_level My master on git://bogomips.org/sequel is now up to commit dffbcf7f96d6429e4a55bf0d1cfcd3df5036dcd7 ---------------------------------------------------------------- Eric Wong (2): sqlite: transaction mode (:immediate|:exclusive) support sqlite: support default DB#transaction_mode accessor lib/sequel/adapters/shared/sqlite.rb | 17 ++++++++++++++++- spec/adapters/sqlite_spec.rb | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/sequel/adapters/shared/sqlite.rb b/lib/sequel/adapters/shared/sqlite.rb index 64a7d3f..b4c72bf 100644 --- a/lib/sequel/adapters/shared/sqlite.rb +++ b/lib/sequel/adapters/shared/sqlite.rb @@ -14,6 +14,7 @@ module DatabaseMethods :deferred => "BEGIN DEFERRED TRANSACTION".freeze, :immediate => "BEGIN IMMEDIATE TRANSACTION".freeze, :exclusive => "BEGIN EXCLUSIVE TRANSACTION".freeze, + nil => Sequel::Database::SQL_BEGIN, }.freeze # Whether to use integers for booleans in the database. SQLite recommends @@ -40,6 +41,19 @@ def case_sensitive_like=(value) pragma_set(:case_sensitive_like, !!value ? 'on' : 'off') if sqlite_version >= 30203 end + # A symbol signifying the value of the default transaction mode + def transaction_mode + defined?(@transaction_mode) ? @transaction_mode : (@transaction_mode = nil) + end + + def transaction_mode=(value) + if TRANSACTION_MODE.include?(value) + @transaction_mode = value + else + raise Error, "Invalid value for transaction_mode. Please specify one of :deferred, :immediate, :exclusive, nil" + end + end + # SQLite uses the :sqlite database type. def database_type :sqlite @@ -250,7 +264,8 @@ def alter_table_sql(table, op) end def begin_new_transaction(conn, opts) - sql = TRANSACTION_MODE[opts[:mode]] || begin_transaction_sql + mode = opts[:mode] || @transaction_mode + sql = TRANSACTION_MODE[mode] or raise Error, "transaction :mode must be one of: :deferred, :immediate, :exclusive, nil" log_connection_execute(conn, sql) set_transaction_isolation(conn, opts) end diff --git a/spec/adapters/sqlite_spec.rb b/spec/adapters/sqlite_spec.rb index c9b3239..d37c2f9 100644 --- a/spec/adapters/sqlite_spec.rb +++ b/spec/adapters/sqlite_spec.rb @@ -612,5 +612,20 @@ @db.transaction do sqls.last.should == Sequel::Database::SQL_BEGIN end + + @db.transaction_mode.should == nil + @db.transaction_mode = :immediate + @db.transaction_mode.should == :immediate + @db.transaction do + sqls.last.should == "BEGIN IMMEDIATE TRANSACTION" + end + @db.transaction(:mode => :exclusive) do + sqls.last.should == "BEGIN EXCLUSIVE TRANSACTION" + end + + proc {@db.transaction_mode = :invalid}.should raise_error(Sequel::Error) + + @db.transaction_mode.should == :immediate + proc {@db.transaction(:mode => :invalid) {}}.should raise_error(Sequel::Error) end end -- 1.8.0.rc3.16.g8ead1bf -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sequel-talk?hl=en.
