Author: wyoung
Date: Mon Mar  6 23:03:48 2006
New Revision: 1235

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1235&view=rev
Log:
- Initial version of Transaction class written
- Added xaction example to test new class

Added:
    branches/v2.1-bakefile/examples/xaction.cpp
    branches/v2.1-bakefile/lib/transaction.cpp
    branches/v2.1-bakefile/lib/transaction.h
Modified:
    branches/v2.1-bakefile/Wishlist
    branches/v2.1-bakefile/examples/   (props changed)
    branches/v2.1-bakefile/examples/examples.bkl
    branches/v2.1-bakefile/lib/lib.bkl

Modified: branches/v2.1-bakefile/Wishlist
URL: 
http://svn.gna.org/viewcvs/mysqlpp/branches/v2.1-bakefile/Wishlist?rev=1235&r1=1234&r2=1235&view=diff
==============================================================================
--- branches/v2.1-bakefile/Wishlist (original)
+++ branches/v2.1-bakefile/Wishlist Mon Mar  6 23:03:48 2006
@@ -28,19 +28,6 @@
        possible to subclass yourself from the "MySQL++'s developers"
        base class.
 
-
-       o Transaction support.  Create a "Transaction" class, an
-         object of which you create on the stack, giving it a
-         reference to the Connection object.  Transaction object's
-         ctor calls a function on the Connection object to start
-         a transaction set, and its dtor calls another function to
-         commit the transaction.  Also provide a "commit()" member
-         function, to commit before destruction.  This has a couple
-         of uses.  First, it's useful for avoiding exceptions coming
-         from ~Transaction(), if such a thing is possible.  Second,
-         sometimes it's inconvenient to wait until the end of a block
-         to commit the transaction, and adding artifical blocks is
-         somewhat ugly.
 
        o Add a configure script option to allow the new lock
          mechanism to use platform mutexes via the Boost.Threads

Propchange: branches/v2.1-bakefile/examples/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Mon Mar  6 23:03:48 2006
@@ -39,5 +39,6 @@
 simple[0-9]
 updel
 usequery
+xaction
 
 tags

Modified: branches/v2.1-bakefile/examples/examples.bkl
URL: 
http://svn.gna.org/viewcvs/mysqlpp/branches/v2.1-bakefile/examples/examples.bkl?rev=1235&r1=1234&r2=1235&view=diff
==============================================================================
--- branches/v2.1-bakefile/examples/examples.bkl (original)
+++ branches/v2.1-bakefile/examples/examples.bkl Mon Mar  6 23:03:48 2006
@@ -117,6 +117,9 @@
                <exe id="fieldinf1" template="std-example">
                        <sources>fieldinf1.cpp</sources>
                </exe>
+               <exe id="xaction" template="std-example">
+                       <sources>xaction.cpp</sources>
+               </exe>
 
                <!-- The "nonstandard" examples -->
                <exe id="cgi_image" template="example">

Added: branches/v2.1-bakefile/examples/xaction.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/branches/v2.1-bakefile/examples/xaction.cpp?rev=1235&view=auto
==============================================================================
--- branches/v2.1-bakefile/examples/xaction.cpp (added)
+++ branches/v2.1-bakefile/examples/xaction.cpp Mon Mar  6 23:03:48 2006
@@ -1,0 +1,110 @@
+/***********************************************************************
+ xaction.cpp - Example showing how to use the transaction support in
+       MySQL++ v2.1 and up.
+
+ Copyright (c) 1998 by Kevin Atkinson, (c) 1999, 2000 and 2001 by
+ MySQL AB, and (c) 2004-2006 by Educational Technology Resources, Inc.
+ Others may also hold copyrights on code in this file.  See the CREDITS
+ file in the top directory of the distribution for details.
+
+ This file is part of MySQL++.
+
+ MySQL++ is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ MySQL++ is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with MySQL++; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+ USA
+***********************************************************************/
+
+#include "stock.h"
+#include "util.h"
+
+#include <transaction.h>
+
+#include <iostream>
+
+using namespace std;
+
+int
+main(int argc, char *argv[])
+{
+       try {
+               // Establish the connection to the database server.
+               mysqlpp::Connection con(mysqlpp::use_exceptions);
+               if (!connect_to_db(argc, argv, con)) {
+                       return 1;
+               }
+
+               // Show initial state
+               mysqlpp::Query query = con.query();
+               cout << "Initial state of stock table:" << endl;
+               print_stock_table(query);
+
+               // Insert a few rows in a single transaction set
+               {
+                       mysqlpp::Transaction trans(con);
+
+                       stock row1("Sauerkraut", 42, 1.2, 0.75, "2006-03-06");
+                       query.insert(row1);
+                       query.execute();
+                       query.reset();
+
+                       stock row2("Bratwurst", 24, 3.0, 4.99, "2006-03-06");
+                       query.insert(row2);
+                       query.execute();
+                       query.reset();
+
+                       cout << "\nRows are inserted, but not committed." << 
endl;
+                       cout << "Verify this with another program (e.g. 
simple1), "
+                                       "then hit Enter." << endl;
+                       getchar();
+
+                       cout << "\nCommitting transaction gives us:" << endl;
+                       trans.commit();
+                       print_stock_table(query);
+               }
+                       
+               // Now let's test auto-rollback
+               {
+                       mysqlpp::Transaction trans(con);
+                       cout << "\nNow adding catsup to the database..." << 
endl;
+
+                       stock row("Catsup", 3, 3.9, 2.99, "2006-03-06");
+                       query.insert(row);
+                       query.execute();
+                       query.reset();
+               }
+               cout << "\nNo, yuck! We don't like catsup. Rolling it back:" <<
+                               endl;
+               print_stock_table(query);
+                       
+       }
+       catch (const mysqlpp::BadQuery& er) {
+               // Handle any query errors
+               cerr << "Query error: " << er.what() << endl;
+               return -1;
+       }
+       catch (const mysqlpp::BadConversion& er) {      
+               // Handle bad conversions
+               cerr << "Conversion error: " << er.what() << endl <<
+                               "\tretrieved data size: " << er.retrieved <<
+                               ", actual size: " << er.actual_size << endl;
+               return -1;
+       }
+       catch (const mysqlpp::Exception& er) {
+               // Catch-all for any other MySQL++ exceptions
+               cerr << "Error: " << er.what() << endl;
+               return -1;
+       }
+
+       return 0;
+}

Modified: branches/v2.1-bakefile/lib/lib.bkl
URL: 
http://svn.gna.org/viewcvs/mysqlpp/branches/v2.1-bakefile/lib/lib.bkl?rev=1235&r1=1234&r2=1235&view=diff
==============================================================================
--- branches/v2.1-bakefile/lib/lib.bkl (original)
+++ branches/v2.1-bakefile/lib/lib.bkl Mon Mar  6 23:03:48 2006
@@ -11,7 +11,7 @@
                <sources>coldata.cpp connection.cpp datetime.cpp field_names.cpp
                fields.cpp field_types.cpp manip.cpp myset.cpp qparms.cpp
                query.cpp result.cpp row.cpp sql_string.cpp string_util.cpp
-               type_info.cpp vallist.cpp</sources>
+               transaction.cpp type_info.cpp vallist.cpp</sources>
 
                <depends>custom.h</depends>
 

Added: branches/v2.1-bakefile/lib/transaction.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/branches/v2.1-bakefile/lib/transaction.cpp?rev=1235&view=auto
==============================================================================
--- branches/v2.1-bakefile/lib/transaction.cpp (added)
+++ branches/v2.1-bakefile/lib/transaction.cpp Mon Mar  6 23:03:48 2006
@@ -1,0 +1,92 @@
+/***********************************************************************
+ transaction.cpp - Implements the Transaction class.
+
+ Copyright (c) 2006 by Educational Technology Resources, Inc.  Others
+ may also hold copyrights on code in this file.  See the CREDITS file
+ in the top directory of the distribution for details.
+
+ This file is part of MySQL++.
+
+ MySQL++ is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ MySQL++ is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with MySQL++; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+ USA
+***********************************************************************/
+
+#define MYSQLPP_NOT_HEADER
+#include "platform.h"
+
+#include "transaction.h"
+
+#include "connection.h"
+#include "query.h"
+
+using namespace std;
+using namespace mysqlpp;
+
+
+//// ctor //////////////////////////////////////////////////////////////
+
+Transaction::Transaction(Connection& conn, bool consistent) :
+conn_(conn),
+finished_(true)                // don't bother rolling it back if ctor fails
+{
+       // Begin the transaction set
+       Query q(conn_.query());
+       q << "START TRANSACTION";
+       if (consistent) {
+               q << " WITH CONSISTENT SNAPSHOT";
+       }
+       q.execute();
+
+       // Setup succeeded, so mark our transaction as not-finished.
+       finished_ = false;
+}
+
+
+//// dtor //////////////////////////////////////////////////////////////
+
+Transaction::~Transaction()
+{
+       if (!finished_) {
+               rollback();
+       }
+}
+
+
+//// commit ////////////////////////////////////////////////////////////
+
+void
+Transaction::commit()
+{
+       Query q(conn_.query());
+       q << "COMMIT";
+       q.execute();
+
+       finished_ = true;
+}
+
+
+//// rollback //////////////////////////////////////////////////////////
+
+void
+Transaction::rollback()
+{
+       Query q(conn_.query());
+       q << "ROLLBACK";
+       q.execute();
+
+       finished_ = true;
+}
+
+

Added: branches/v2.1-bakefile/lib/transaction.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/branches/v2.1-bakefile/lib/transaction.h?rev=1235&view=auto
==============================================================================
--- branches/v2.1-bakefile/lib/transaction.h (added)
+++ branches/v2.1-bakefile/lib/transaction.h Mon Mar  6 23:03:48 2006
@@ -1,0 +1,86 @@
+/// \file transaction.h
+/// \brief Declares the Transaction class.
+///
+/// This object works with the Connection class to automate the use of
+/// MySQL transactions.  It allows you to express these transactions
+/// directly in C++ code instead of sending the raw SQL commands.
+
+/***********************************************************************
+ Copyright (c) 2006 by Educational Technology Resources, Inc.  Others
+ may also hold copyrights on code in this file.  See the CREDITS file
+ in the top directory of the distribution for details.
+
+ This file is part of MySQL++.
+
+ MySQL++ is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ MySQL++ is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with MySQL++; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+ USA
+***********************************************************************/
+
+#if !defined(MYSQLPP_TRANSACTION_H)
+#define MYSQLPP_TRANSACTION_H
+
+#include "platform.h"
+
+namespace mysqlpp {
+
+class Connection;
+
+class Transaction
+{
+public:
+       /// \brief Constructor
+       ///
+       /// \param conn The connection we use to manage the transaction set
+       /// \param consistent Whether to use "consistent snapshots" during
+       /// the transaction. See the documentation for "START TRANSACTION"
+       /// in the MySQL manual for more on this.
+       Transaction(Connection& conn, bool consistent = false);
+
+       /// \brief Destructor
+       ///
+       /// If the transaction has not been committed or rolled back by the
+       /// time the destructor is called, it is rolled back.  This is the
+       /// right thing because one way this can happen is if the object is
+       /// being destroyed as the stack is unwound to handle an exception.
+       /// In that instance, you certainly want to roll back the
+       /// transaction.
+       ~Transaction();
+
+       /// \brief Commits the transaction
+       ///
+       /// This commits all updates to the database using the connection
+       /// we were created with since this object was created.  This is a
+       /// no-op if the table isn't stored using a transaction-aware
+       /// storage engine.  See CREATE TABLE in the MySQL manual for
+       /// details.
+       void commit();
+
+       /// \brief Rolls back the transaction
+       ///
+       /// This abandons all SQL statements made on the connection since
+       /// this object was created.  This only works on tables stored using
+       /// a transaction-aware storage engine.  See CREATE TABLE in the
+       /// MySQL manual for details.
+       void rollback();
+
+private:
+       Connection& conn_;      ///! Connection to send queries through
+       bool finished_;         ///! True when we commit or roll back xaction
+};
+
+} // end namespace mysqlpp
+
+#endif // !defined(MYSQLPP_TRANSACTION_H)
+


_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits

Reply via email to