Permalink
Browse files

Merge remote-tracking branch 'bogomips/master'

  • Loading branch information...
2 parents 4b89460 + dffbcf7 commit e9882ef501042f9fd4516b4c65358e625833dc25 @jeremyevans committed Oct 22, 2012
Showing with 60 additions and 1 deletion.
  1. +26 −0 lib/sequel/adapters/shared/sqlite.rb
  2. +34 −1 spec/adapters/sqlite_spec.rb
@@ -10,6 +10,12 @@ module DatabaseMethods
TABLES_FILTER = "type = 'table' AND NOT name = 'sqlite_sequence'".freeze
TEMP_STORE = [:default, :file, :memory].freeze
VIEWS_FILTER = "type = 'view'".freeze
+ TRANSACTION_MODE = {
+ :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
# booleans be stored as integers, but historically Sequel has used 't'/'f'.
@@ -35,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
@@ -244,6 +263,13 @@ def alter_table_sql(table, op)
end
end
+ def begin_new_transaction(conn, opts)
+ 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
+
# A name to use for the backup table
def backup_table_name(table, opts={})
table = table.gsub('`', '')
@@ -595,4 +595,37 @@
@db.drop_column :a, :b
@db.indexes(:a).should == {:a_a_index=>{:unique=>false, :columns=>[:a]}}
end
-end
+
+ specify "should have support for various #transaction modes" do
+ sqls = []
+ @db.loggers << (l=Class.new{%w'info error'.each{|m| define_method(m){|sql| sqls << sql}}}.new)
+
+ @db.transaction(:mode => :immediate) do
+ sqls.last.should == "BEGIN IMMEDIATE TRANSACTION"
+ end
+ @db.transaction(:mode => :exclusive) do
+ sqls.last.should == "BEGIN EXCLUSIVE TRANSACTION"
+ end
+ @db.transaction(:mode => :deferred) do
+ sqls.last.should == "BEGIN DEFERRED TRANSACTION"
+ end
+ @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

0 comments on commit e9882ef

Please sign in to comment.