Author: wyoung
Date: Mon Jul 16 16:59:14 2007
New Revision: 1699

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1699&view=rev
Log:
- Removed client_flag parameter to Connection ctor and connect()
- Added a bunch of new set_option() flags to mirror the lost client_flag
  functionality, that being the unified MySQL++ way to set all connection
  options.

Modified:
    trunk/Wishlist
    trunk/lib/connection.cpp
    trunk/lib/connection.h

Modified: trunk/Wishlist
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/Wishlist?rev=1699&r1=1698&r2=1699&view=diff
==============================================================================
--- trunk/Wishlist (original)
+++ trunk/Wishlist Mon Jul 16 16:59:14 2007
@@ -13,7 +13,15 @@
     get done in v3.0.  Most of them break the ABI, so they can't wait
     for a future version, because v4 could be years out.
 
-       o Fix single-parameter template query stuff.
+    o Restore execute(SQLQueryParams&) et al., make them public,
+      and document them in both userman and refman.  Needed for the
+      uncommon case where you don't know the number of parameters at
+      compile time.
+
+    o Use AT&T getopt code in examples for platform-independent
+      command line parsing.
+
+    o Fix single-parameter template query stuff.
 
     o Add conditional code to the Lockable mechanism to use platform
       mutexes if available, to implement these MySQL C API
@@ -229,5 +237,6 @@
       This could potentially break a lot of infrastructure, though,
       so do it only with care.
 
-    o Some sort of support for prepared statements.
-
+    o Some sort of support for prepared statements.  Can we hijack
+      the template query mechanism?
+

Modified: trunk/lib/connection.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/connection.cpp?rev=1699&r1=1698&r2=1699&view=diff
==============================================================================
--- trunk/lib/connection.cpp (original)
+++ trunk/lib/connection.cpp Mon Jul 16 16:59:14 2007
@@ -102,9 +102,15 @@
        Connection::opt_type_none,              // opt_guess_connection
        Connection::opt_type_string,    // opt_set_client_ip
        Connection::opt_type_boolean,   // opt_secure_auth
+       Connection::opt_type_boolean,   // opt_multi_results
        Connection::opt_type_boolean,   // opt_multi_statements
        Connection::opt_type_boolean,   // opt_report_data_truncation
        Connection::opt_type_boolean,   // opt_reconnect
+       Connection::opt_type_boolean,   // opt_found_rows
+       Connection::opt_type_boolean,   // opt_ignore_space
+       Connection::opt_type_boolean,   // opt_interactive
+       Connection::opt_type_boolean,   // opt_local_files
+       Connection::opt_type_boolean,   // opt_no_schema
 };
 
 
@@ -120,13 +126,13 @@
 
 
 Connection::Connection(cchar* db, cchar* server, cchar* user,
-               cchar* password, unsigned long client_flag, unsigned int port) :
+               cchar* password, unsigned int port) :
 OptionalExceptions(),
 Lockable(false),
 connecting_(false)
 {
        mysql_init(&mysql_);
-       if (connect(db, server, user, password, client_flag, port)) {
+       if (connect(db, server, user, password, port)) {
                unlock();
                copacetic_ = is_connected_ = true;
        }
@@ -174,7 +180,7 @@
 
 bool
 Connection::connect(cchar* db, cchar* server, cchar* user,
-               cchar* password, unsigned long client_flag, unsigned int port)
+               cchar* password, unsigned int port)
 {
        lock();
 
@@ -186,15 +192,6 @@
        // Set defaults for connection options.  User can override these
        // by calling set_option() before connect().
        set_option_default(opt_read_default_file, "my");
-
-#if MYSQL_VERSION_ID >= 40101
-       // Check to see if user turned on multi-statements before
-       // establishing the connection.  This one we handle specially, by
-       // setting a flag during connection establishment.
-       if (option_set(opt_multi_statements)) {
-               client_flag |= CLIENT_MULTI_STATEMENTS;
-       }
-#endif
 
        // Figure out what the server parameter means, then establish 
        // the connection.
@@ -204,7 +201,7 @@
        if (parse_ipc_method(server, host, port, socket_name) &&
                        mysql_real_connect(&mysql_, host.c_str(), user, 
password, db,
                        port, (socket_name.empty() ? 0 : socket_name.c_str()),
-                       client_flag)) {
+                       mysql_.client_flag)) {
                unlock();
                is_connected_ = true;
 
@@ -229,22 +226,45 @@
 
 
 bool
-Connection::connect(const MYSQL& mysql)
-{
-       if (mysql.unix_socket && (strlen(mysql.unix_socket) > 0)) {
-               return connect(mysql.db, mysql.passwd, mysql.user, 
-                               mysql.unix_socket, mysql.client_flag);
-       }
-       else {
-               string server(mysql.host);
-               if (mysql.port > 0) {
-                       char ac[10];
-                       snprintf(ac, sizeof(ac), ":%d", mysql.port);
-                       server += ac;
-               }
-               return connect(mysql.db, mysql.passwd, mysql.user, 
-                               server.c_str(), mysql.client_flag);
-       }
+Connection::connect(const MYSQL& other)
+{
+       lock();
+
+       // Drop previous connection, if any
+       if (connected()) {
+               disconnect();
+       }
+
+       // Set defaults for connection options.  User can override these
+       // by calling set_option() before connect().
+       set_option_default(opt_read_default_file, "my");
+
+       // Figure out what the server parameter means, then establish 
+       // the connection.
+       error_message_.clear();
+       if (mysql_real_connect(&mysql_, other.host, other.user,
+                       other.passwd, other.db, other.port, other.unix_socket,
+                       other.client_flag)) {
+               unlock();
+               is_connected_ = true;
+
+               if (other.db && other.db[0]) {
+                       // Also attach to given database
+                       copacetic_ = select_db(other.db);
+               }
+               else {
+                       copacetic_ = true;
+               }
+       }
+       else {
+               unlock();
+               copacetic_ = is_connected_ = false;
+               if (throw_exceptions()) {
+                       throw ConnectionFailed(error());
+               }
+       }
+
+       return is_connected_;
 }
 
 
@@ -551,7 +571,9 @@
 bool
 Connection::set_option(Option option, bool arg)
 {
-       if (connected() && (option != opt_multi_statements)) {
+       if (connected() &&
+                       (option != opt_multi_results) &&
+                       (option != opt_multi_statements)) {
                // We're connected and it isn't an option that can be set
                // after connection is up, so complain to user.
                return bad_option(option, opt_err_conn);
@@ -564,20 +586,24 @@
                        success = set_option_impl(MYSQL_SECURE_AUTH, &arg);
                        break;
 
+               case opt_multi_results:
                case opt_multi_statements:
-                       // If connection is up, set the flag immediately.  If 
not,
-                       // and caller wants this turned on, pretend success so 
that
-                       // we store the info we need to turn this flag on when
-                       // bringing the connection up.  (If the caller is 
turning it
-                       // off before conn comes up, we effectively ignore 
this, 
-                       // because that's the default.)
+                       // This connection option is unique in that it can be 
set
+                       // either before or after the connection is up.  Note
+                       // however that setting either of these two options 
after
+                       // connection is up sets both; else, it sets only the 
one.
                        if (connected()) {
-                               success = set_option_impl(arg ?
+                               success = set_option_impl(
+                                               arg ?
                                                
MYSQL_OPTION_MULTI_STATEMENTS_ON :
                                                
MYSQL_OPTION_MULTI_STATEMENTS_OFF);
                        }
                        else {
-                               success = arg;
+                               success = set_option_impl(
+                                               option == opt_multi_results ?
+                                               CLIENT_MULTI_RESULTS :
+                                               CLIENT_MULTI_STATEMENTS,
+                                               arg);
                        }
                        break;
 #endif
@@ -591,6 +617,26 @@
                        success = set_option_impl(MYSQL_OPT_RECONNECT, &arg);
                        break;
 #endif
+               case opt_found_rows:
+                       success = set_option_impl(CLIENT_FOUND_ROWS, arg);
+                       break;
+
+               case opt_ignore_space:
+                       success = set_option_impl(CLIENT_IGNORE_SPACE, arg);
+                       break;
+
+               case opt_interactive:
+                       success = set_option_impl(CLIENT_INTERACTIVE, arg);
+                       break;
+
+               case opt_local_files:
+                       success = set_option_impl(CLIENT_LOCAL_FILES, arg);
+                       break;
+
+               case opt_no_schema:
+                       success = set_option_impl(CLIENT_NO_SCHEMA, arg);
+                       break;
+
                default:
                        return bad_option(option, opt_err_type);
        }
@@ -647,6 +693,37 @@
        return !mysql_set_server_option(&mysql_, msoption);
 }
 #endif
+
+
+bool
+Connection::set_option_impl(int option, bool arg)
+{
+       // If we get through this loop and n is 1, only one bit is set in
+       // the option value, which is as it should be.
+       int n = option;
+       while (n && ((n & 1) == 0)) {
+               n >>= 1;
+       }
+       
+       if ((n == 1) && 
+                       (option >= CLIENT_LONG_PASSWORD) && 
+                       (option <= CLIENT_MULTI_RESULTS)) {
+               // Option value seems sane, so go ahead and set/clear the flag
+               if (arg) {
+                       mysql_.client_flag |= option;
+               }
+               else {
+                       mysql_.client_flag &= ~option;
+               }
+
+               return true;
+       }
+       else {
+               // Option value is outside the range we understand, or caller
+               // erroneously passed a value with multiple bits set.
+               return false;
+       }
+}
 
 
 bool

Modified: trunk/lib/connection.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/connection.h?rev=1699&r1=1698&r2=1699&view=diff
==============================================================================
--- trunk/lib/connection.h (original)
+++ trunk/lib/connection.h Mon Jul 16 16:59:14 2007
@@ -94,7 +94,10 @@
                opt_set_client_ip,
                opt_secure_auth,
 
-               // Set multi-query statement support; no argument
+               // Set multi-query statement support, and multiple result sets.
+               // These may be set at run time, in which case the C API turns
+               // on both flags automatically.
+               opt_multi_results,
                opt_multi_statements,
 
                // Set reporting of data truncation errors
@@ -103,6 +106,22 @@
                // Enable or disable automatic reconnection to the server if
                // the connection is found to have been lost.
                opt_reconnect,
+
+               // Return the number of matched rows in result sets, not the
+               // number of changed rows.
+               opt_found_rows,
+
+               // Allow spaces after function names in queries.
+               opt_ignore_space,
+
+               // Mark program as interactive; affects connection timeout
+               opt_interactive,
+
+               // Enable LOAD DATA LOCAL statement
+               opt_local_files,
+
+               // Disable db.tbl.col syntax in queries
+               opt_no_schema,
 
                // Number of options supported.  Never send this to
                // set_option()!
@@ -127,8 +146,6 @@
        /// \param user user name to log in under, or 0 to use the user
        ///             name this program is running under
        /// \param password password to use when logging in
-       /// \param client_flag special connection flags. See MySQL C API
-       ///     documentation for \c mysql_real_connect() for details.  
        /// \param port TCP port number MySQL server is listening on, or 0
        ///             to use default value; note that you may also give this 
as
        ///     part of the \c server parameter
@@ -156,8 +173,7 @@
        ///   service name is given here and a nonzero value is passed for
        ///   the \c port parameter, the latter takes precedence.
        Connection(cchar* db, cchar* server = 0, cchar* user = 0,
-                       cchar* password = 0, unsigned long client_flag = 0,
-                       unsigned int port = 0);
+                       cchar* password = 0, unsigned int port = 0);
 
        /// \brief Establish a new connection using the same parameters as
        /// an existing C API connection.
@@ -183,8 +199,7 @@
        /// to a database server, the previous connection is dropped and a
        /// new connection is established.
        bool connect(cchar* db, cchar* server = 0, cchar* user = 0,
-                       cchar* password = 0, unsigned long client_flag = 0,
-                       unsigned int port = 0);
+                       cchar* password = 0, unsigned int port = 0);
 
        /// \brief Close connection to MySQL server.
        ///
@@ -521,6 +536,13 @@
        bool set_option_impl(enum_mysql_set_option msoption);
 #endif
 
+       /// \brief Set MySQL C API connection option
+       ///
+       /// Manipulates the MYSQL.client_flag bit mask.  This allows these
+       /// flags to be treated the same way as any other connection option,
+       /// even though the C API handles them differently.
+       bool set_option_impl(int option, bool arg);
+
        /// \brief Establish a new connection as a copy of an existing one
        ///
        /// \param other the connection to copy


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

Reply via email to