Method: DBGeni::Connector::Sqlite#execute

Defined in:
lib/dbgeni/connectors/sqlite.rb

#execute(sql, *binds) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dbgeni/connectors/sqlite.rb', line 37

def execute(sql, *binds)
#        unless @connection.transaction_active?
#          @connection.transaction
#        end
  if RUBY_PLATFORM == 'java'
    return execute_jdbc(sql, *binds)
  end
  begin
    # Why am I re-establishing the database connection? Well, something has changed
    # somewhere in the sqlite3 application or ruby drivers, and without this line
    # I get lots of errors in the test suite:
    #
    # sqlite 3.7.8 QLite3::SQLException: SQL logic error or missing database
    #
    # This line fixes it be reconnecting. I am not sure if this is a non-reentrant
    # issue, but it used to work just fine :-/
    # SQLITE + DBGeni is not really intended as a production combo, so this is probably
    # ok.
    @connection = SQLite3::Database.new(@database)
    query = @connection.prepare(sql)
    binds.each_with_index do |b, i|
      query.bind_param(i+1, b)
    end
    results = query.execute!
    # This shouldn't even be needed, as there are never transactions started.
    # by default everthing in sqlite is autocommit
    if @connection.transaction_active?
      @connection.commit
    end
    results
  ensure
    begin
      query.close
    rescue Exception => e
    end
  end
end