Author: wyoung
Date: Thu Jan  3 16:29:38 2008
New Revision: 2080

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2080&view=rev
Log:
First cut at examples/cpool?.cpp.  They build and run, but don't do
anything terribly useful yet.

Added:
    trunk/examples/cpoolp.cpp
    trunk/examples/cpoolw.cpp
Modified:
    trunk/   (props changed)
    trunk/mysql++.bkl

Propchange: trunk/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Thu Jan  3 16:29:38 2008
@@ -37,6 +37,7 @@
 
 cgi_jpeg
 complic1
+cpool?
 dbinfo
 deadlock
 fieldinf

Added: trunk/examples/cpoolp.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/examples/cpoolp.cpp?rev=2080&view=auto
==============================================================================
--- trunk/examples/cpoolp.cpp (added)
+++ trunk/examples/cpoolp.cpp Thu Jan  3 16:29:38 2008
@@ -1,0 +1,138 @@
+/***********************************************************************
+ cpoolp.cpp - POSIX threads version of ConnectionPool example.  Shows
+       how to create and use a concrete ConnectionPool derivative.
+
+ Copyright (c) 2008 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 "cmdline.h"
+#include "printdata.h"
+
+#include <mysql++.h>
+
+#include <iostream>
+
+using namespace std;
+
+
+// Define a concrete ConnectionPool derivative.  Takes connection
+// parameters as inputs to its ctor, which it uses to create the
+// connections we're called upon to make.  Note that we also declare
+// a global pointer to an object of this type, which we create soon
+// after startup; this should be a common usage pattern, as what use
+// are multiple pools?
+class SimpleConnectionPool : public mysqlpp::ConnectionPool
+{
+public:
+       // The object's only constructor
+       SimpleConnectionPool(const char* db, const char* server,
+                       const char* user, const char* password) :
+       db_(db ? db : ""),
+       server_(server ? server : ""),
+       user_(user ? user : ""),
+       password_(password ? password : "")
+       {
+       }
+
+       // The destructor.  We _must_ call ConnectionPool::clear() here,
+       // because our superclass can't do it for us.
+       ~SimpleConnectionPool()
+       {
+               clear();
+       }
+
+protected:
+       // Superclass overrides
+       mysqlpp::Connection* create()
+       {
+               // Create connection using the parameters we were passed upon
+               // creation.  This could be something much more complex, but for
+               // the purposes of the example, this suffices.
+               cout << "Creating new pooled connection..." << endl;
+               return new mysqlpp::Connection(
+                               db_.empty() ? 0 : db_.c_str(),
+                               server_.empty() ? 0 : server_.c_str(),
+                               user_.empty() ? 0 : user_.c_str(),
+                               password_.empty() ? "" : password_.c_str());
+       }
+
+       void destroy(mysqlpp::Connection* cp)
+       {
+               // Our superclass can't know how we created the Connection, so
+               // it delegates destruction to us, to be safe.
+               cout << "Destroying pooled connection..." << endl;
+               delete cp;
+       }
+
+       unsigned int max_idle_time()
+       {
+               // Set our idle time at an example-friendly 3 seconds.  A real
+               // pool would return some fraction of the server's connection
+               // idle timeout instead.
+               return 3;
+       }
+
+private:
+       // Our connection parameters
+       std::string db_, server_, user_, password_;
+}
+*poolptr = 0;
+
+
+int
+main(int argc, char *argv[])
+{
+       // Get database access parameters from command line
+    const char* db = 0, *server = 0, *user = 0, *pass = "";
+       if (!parse_command_line(argc, argv, &db, &server, &user, &pass)) {
+               return 1;
+       }
+
+       // Create the pool and grab a connection.  We do it partly to test
+       // that the parameters are good before we start doing real work, and
+       // partly because we need a Connection object to call thread_aware()
+       // on to check that it's okay to start doing that real work.
+       poolptr = new SimpleConnectionPool(db, server, user, pass);
+       try {
+               mysqlpp::Connection* cp = poolptr->grab();
+               if (!cp->thread_aware()) {
+                       cerr << "MySQL++ wasn't built with thread awareness!  " 
<<
+                                       argv[0] << " can't run without it." << 
endl;
+                       return 1;
+               }
+               poolptr->release(cp);
+       }
+       catch (mysqlpp::Exception& e) {
+               cerr << "Failed to set up initial pooled connection: " <<
+                               e.what() << endl;
+               return 1;
+       }
+
+       // Setup complete.  Now let's spin some threads...
+       cout << "Pool created and working correctly.  Now to do some "
+                       "real work..." << endl;
+
+       // Shut it all down...
+       delete poolptr;
+
+       return 0;
+}

Added: trunk/examples/cpoolw.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/examples/cpoolw.cpp?rev=2080&view=auto
==============================================================================
--- trunk/examples/cpoolw.cpp (added)
+++ trunk/examples/cpoolw.cpp Thu Jan  3 16:29:38 2008
@@ -1,0 +1,134 @@
+/***********************************************************************
+ cpoolw.cpp - Windows threads version of ConnectionPool example.  Shows
+       how to create and use a concrete ConnectionPool derivative.
+
+ Copyright (c) 2008 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 "cmdline.h"
+#include "printdata.h"
+
+#include <mysql++.h>
+
+#include <iostream>
+
+using namespace std;
+
+
+// Define a concrete ConnectionPool derivative.  Takes connection
+// parameters as inputs to its ctor, which it uses to create the
+// connections we're called upon to make.  Note that we also declare
+// a global pointer to an object of this type, which we create soon
+// after startup; this should be a common usage pattern, as what use
+// are multiple pools?
+class SimpleConnectionPool : public mysqlpp::ConnectionPool
+{
+public:
+       // The object's only constructor
+       SimpleConnectionPool(const char* db, const char* server,
+                       const char* user, const char* password) :
+       db_(db ? db : ""),
+       server_(server ? server : ""),
+       user_(user ? user : ""),
+       password_(password ? password : "")
+       {
+       }
+
+       // The destructor.  We _must_ call ConnectionPool::clear() here,
+       // because our superclass can't do it for us.
+       ~SimpleConnectionPool()
+       {
+               clear();
+       }
+
+protected:
+       // Superclass overrides
+       mysqlpp::Connection* create()
+       {
+               // Create connection using the parameters we were passed upon
+               // creation.  This could be something much more complex, but for
+               // the purposes of the example, this suffices.
+               cout << "Creating new pooled connection..." << endl;
+               return new mysqlpp::Connection(
+                               db_.empty() ? 0 : db_.c_str(),
+                               server_.empty() ? 0 : server_.c_str(),
+                               user_.empty() ? 0 : user_.c_str(),
+                               password_.empty() ? "" : password_.c_str());
+       }
+
+       void destroy(mysqlpp::Connection* cp)
+       {
+               // Our superclass can't know how we created the Connection, so
+               // it delegates destruction to us, to be safe.
+               cout << "Destroying pooled connection..." << endl;
+               delete cp;
+       }
+
+       unsigned int max_idle_time()
+       {
+               // Set our idle time at an example-friendly 3 seconds.  A real
+               // pool would return some fraction of the server's connection
+               // idle timeout instead.
+               return 3;
+       }
+
+private:
+       // Our connection parameters
+       std::string db_, server_, user_, password_;
+}
+*poolptr = 0;
+
+
+int
+main(int argc, char *argv[])
+{
+       // Get database access parameters from command line
+    const char* db = 0, *server = 0, *user = 0, *pass = "";
+       if (!parse_command_line(argc, argv, &db, &server, &user, &pass)) {
+               return 1;
+       }
+
+       // Create the pool and grab a connection.  We do it mainly to test
+       // that the parameters are good before we start doing real work.
+       // Note that unlike cpoolp, we don't bother to check that MySQL++
+       // and the underlying C API library are thread-aware.  It's not
+       // optional for Windows.
+       poolptr = new SimpleConnectionPool(db, server, user, pass);
+       try {
+               mysqlpp::Connection* cp = poolptr->grab();
+               poolptr->release(cp);
+       }
+       catch (mysqlpp::Exception& e) {
+               cerr << "Failed to set up initial pooled connection: " <<
+                               e.what() << endl;
+               return 1;
+       }
+
+       // Setup complete.  Now let's spin some threads...
+       cout << "Pool created and working correctly.  Now to do some "
+                       "real work..." << endl;
+
+       // Shut it all down...
+       delete poolptr;
+
+       return 0;
+}

Modified: trunk/mysql++.bkl
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/mysql%2B%2B.bkl?rev=2080&r1=2079&r2=2080&view=diff
==============================================================================
--- trunk/mysql++.bkl (original)
+++ trunk/mysql++.bkl Thu Jan  3 16:29:38 2008
@@ -247,6 +247,48 @@
                <exe id="cgi_jpeg" template="libexcommon-user,programs">
                        <sources>examples/cgi_jpeg.cpp</sources>
                </exe>
+               <if cond="FORMAT=='autoconf'">
+                       <!-- Built conditionally because it uses POSIX threads 
-->
+                       <exe id="cpoolp" template="libexcommon-user,programs">
+                               <sources>examples/cpoolp.cpp</sources>
+                       </exe>
+               </if>
+               <if cond="PLATFORM_WINDOWS_NATIVE=='yes'">
+                       <!-- Built conditionally because it uses Windows 
threads -->
+                       <exe id="cpoolw" template="libexcommon-user,programs">
+                               <sources>examples/cpoolw.cpp</sources>
+                       </exe>
+               </if>
+               <exe id="dbinfo" template="libexcommon-user,programs">
+                       <sources>examples/dbinfo.cpp</sources>
+               </exe>
+               <exe id="deadlock" template="libexcommon-user,programs">
+                       <sources>examples/deadlock.cpp</sources>
+               </exe>
+               <exe id="fieldinf" template="libexcommon-user,programs">
+                       <sources>examples/fieldinf.cpp</sources>
+               </exe>
+               <exe id="for_each" template="libexcommon-user,programs">
+                       <sources>examples/for_each.cpp</sources>
+               </exe>
+               <exe id="load_jpeg" template="libexcommon-user,programs">
+                       <sources>examples/load_jpeg.cpp</sources>
+               </exe>
+               <exe id="multiquery" template="libexcommon-user,programs">
+                       <sources>examples/multiquery.cpp</sources>
+               </exe>
+               <exe id="resetdb" template="libexcommon-user,programs">
+                       <sources>examples/resetdb.cpp</sources>
+               </exe>
+               <exe id="simple1" template="libexcommon-user,programs">
+                       <sources>examples/simple1.cpp</sources>
+               </exe>
+               <exe id="simple2" template="libexcommon-user,programs">
+                       <sources>examples/simple2.cpp</sources>
+               </exe>
+               <exe id="simple3" template="libexcommon-user,programs">
+                       <sources>examples/simple3.cpp</sources>
+               </exe>
                <exe id="ssqls1" template="libexcommon-user,programs">
                        <sources>examples/ssqls1.cpp</sources>
                </exe>
@@ -261,36 +303,6 @@
                </exe>
                <exe id="ssqls5" template="libexcommon-user,programs">
                        <sources>examples/ssqls5.cpp</sources>
-               </exe>
-               <exe id="dbinfo" template="libexcommon-user,programs">
-                       <sources>examples/dbinfo.cpp</sources>
-               </exe>
-               <exe id="deadlock" template="libexcommon-user,programs">
-                       <sources>examples/deadlock.cpp</sources>
-               </exe>
-               <exe id="fieldinf" template="libexcommon-user,programs">
-                       <sources>examples/fieldinf.cpp</sources>
-               </exe>
-               <exe id="for_each" template="libexcommon-user,programs">
-                       <sources>examples/for_each.cpp</sources>
-               </exe>
-               <exe id="load_jpeg" template="libexcommon-user,programs">
-                       <sources>examples/load_jpeg.cpp</sources>
-               </exe>
-               <exe id="multiquery" template="libexcommon-user,programs">
-                       <sources>examples/multiquery.cpp</sources>
-               </exe>
-               <exe id="resetdb" template="libexcommon-user,programs">
-                       <sources>examples/resetdb.cpp</sources>
-               </exe>
-               <exe id="simple1" template="libexcommon-user,programs">
-                       <sources>examples/simple1.cpp</sources>
-               </exe>
-               <exe id="simple2" template="libexcommon-user,programs">
-                       <sources>examples/simple2.cpp</sources>
-               </exe>
-               <exe id="simple3" template="libexcommon-user,programs">
-                       <sources>examples/simple3.cpp</sources>
                </exe>
                <exe id="store_if" template="libexcommon-user,programs">
                        <sources>examples/store_if.cpp</sources>


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

Reply via email to