Method: Sequel::DataObjects::Database#execute
- Defined in:
- lib/sequel/adapters/do.rb
#execute(sql, opts = {}) ⇒ Object
Execute the given SQL. If a block is given, the DataObjects::Reader created is yielded to it. A block should not be provided unless a a SELECT statement is being used (or something else that returns rows). Otherwise, the return value is the insert id if opts is :insert, or the number of affected rows, otherwise.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sequel/adapters/do.rb', line 68 def execute(sql, opts={}) synchronize(opts[:server]) do |conn| begin command = conn.create_command(sql) res = log_yield(sql){block_given? ? command.execute_reader : command.execute_non_query} rescue ::DataObjects::Error => e raise_error(e) end if block_given? begin yield(res) ensure res.close if res end elsif opts[:type] == :insert res.insert_id else res.affected_rows end end end |