I have a class that executes SQL statements (updates, inserts, and deletes) that are stored in a YAML file. I would like all the statements to be part of one transaction. If any of the SQL statements fail, then they would be rolled back. If all of the statements are successful, then they would be committed. I'm connecting to a MySQL database. Here is my code:
require 'dm-core'
class SqlExecuter
def initialize(input_yaml_file_name)
@input_yaml_file_name = input_yaml_file_name
@adapter = DataMapper.repository(:default).adapter
@sql_statements = YAML::load(File.open(input_yaml_file_name))
end
def execute()
puts "Executing SQL statements in #{@input_yaml_file_name} file...."
@sql_statements.each do | sql_statement |
@adapter.execute(sql_statement)
end
end
end # class SqlExecuter
I'd like to have all of my @adapter.execute calls be part of one transaction. I've looked at the code in the dm-transactions gem, but I can't figure out how to use it in this context.