From: Brian Candler Date: 2003-03-27T20:02:49+09:00 Subject: [PATCH] transaction support for dbd_mysql --VS++wcV0S1rZb1Fb Content-Type: text/plain; charset=us-ascii Content-Disposition: inline The following patch adds transaction support to dbd_mysql, e.g. require 'dbi' db = DBI.connect('dbi:mysql:test','foo') db['AutoCommit'] = false db.execute('create table foo(bar varchar(200)) type=innodb') rescue nil db.transaction do p db.select_one('select count(*) from foo') db.execute('insert into foo values (?)','hello') p db.select_one('select count(*) from foo') raise "hell" # does an automatic rollback end It was rather too simple to implement, so I'm not sure if I'm missing something fundamental here. I'd appreciate feedback from any other mysql users. I didn't implement the read accessor, e.g. "puts db['AutoCommit']", because I can't find a way to ask mysql to show the current AutoCommit state, and I don't believe that keeping a parallel instance variable is a good idea. I had a quick look at how the pg driver implements transactions. It uses an instance variable "@in_transaction", but this does not appear to be necessary in mysql, as you don't need to issue an explicit 'BEGIN'. I did notice one odd thing in the pg driver: if attr =~ /^pg_/ or attr != /_/ ^^^^^^^^^^^ I think that should be "attr !~ /_/" otherwise I can't see how it does anything useful. Regards, Brian. --VS++wcV0S1rZb1Fb Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="ruby-dbi-all-mysql.patch" diff -uNr ruby-dbi-all.orig/lib/dbd_mysql/Mysql.rb ruby-dbi-all/lib/dbd_mysql/Mysql.rb --- ruby-dbi-all.orig/lib/dbd_mysql/Mysql.rb Thu Sep 26 19:37:27 2002 +++ ruby-dbi-all/lib/dbd_mysql/Mysql.rb Wed Mar 26 18:46:46 2003 @@ -255,14 +255,20 @@ Statement.new(self, @handle, statement, @mutex) end - # TODO: Raise Error def commit + mysql_do('commit') end - # TODO: Raise Error def rollback + mysql_do('rollback') end + def []=(attr, value) + case attr + when 'AutoCommit' + mysql_do('set autocommit=' + (value ? '1' : '0')) + end + end def quote(value) case value @@ -276,6 +282,14 @@ end private # ------------------------------------------------- + + def mysql_do(sql) + @mutex.synchronize { + @handle.query_with_result = false + @handle.query(sql) + } + nil + end # Eli Green def mysql_type_info(typedef) --VS++wcV0S1rZb1Fb--