From: Paul D. <pd...@us...> - 2003-02-08 02:03:20
|
Update of /cvsroot/ruby-dbi/src/lib/dbd_mysql In directory sc8-pr-cvs1:/tmp/cvs-serv15214 Modified Files: Mysql.rb Log Message: Add transaction support: commit and rollback methods, AutoCommit database handle attribute Index: Mysql.rb =================================================================== RCS file: /cvsroot/ruby-dbi/src/lib/dbd_mysql/Mysql.rb,v retrieving revision 1.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- Mysql.rb 8 Feb 2003 01:46:02 -0000 1.19 +++ Mysql.rb 8 Feb 2003 02:03:16 -0000 1.20 @@ -181,10 +181,21 @@ def initialize(handle, attr) super + # check server version to determine transaction capability + ver_str = @handle.get_server_info + major, minor, teeny = ver_str.split(".") + teeny.sub!(/\D*$/, "") # strip any non-numeric suffix if present + server_version = major.to_i*10000 + minor.to_i*100 + teeny.to_i + # It's not until 3.23.17 that SET AUTOCOMMIT, + # BEGIN, COMMIT, and ROLLBACK all are available + @have_transactions = (server_version >= 32317) + # assume the connection begins in AutoCommit mode + @attr['AutoCommit'] = true @mutex = Mutex.new end def disconnect + self.rollback unless @attr['AutoCommit'] @handle.close rescue MyError => err raise DBI::DatabaseError.new(err.message, err.errno) @@ -259,12 +270,24 @@ Statement.new(self, @handle, statement, @mutex) end - # TODO: Raise Error def commit + if @have_transactions + @handle.query("COMMIT") + else + raise NotSupportedError + end + rescue MyError => err + raise DBI::DatabaseError.new(err.message, err.errno) end - # TODO: Raise Error def rollback + if @have_transactions + @handle.query("ROLLBACK") + else + raise NotSupportedError + end + rescue MyError => err + raise DBI::DatabaseError.new(err.message, err.errno) end @@ -277,6 +300,20 @@ else super end + end + + def []=(attr, value) + case attr + when 'AutoCommit' + if @have_transactions + @handle.query("SET AUTOCOMMIT=" + (value ? "1" : "0")) + else + raise NotSupportedError + end + else + raise NotSupportedError + end + @attr[attr] = value end private # ------------------------------------------------- |