Author: wyoung
Date: Fri Jul 13 05:53:33 2007
New Revision: 1689

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1689&view=rev
Log:
- Collapsed the three MySQL server connection IPC selection parameters
  (host, port, and socket_name) down into a single string parameter
  which is parsed to determine what kind of connection you mean.
- The new server_addr parameter now follows the user name, and the
  password now precedes it.  (It used to be the other way around.)  This
  puts the parameter whose default is least likely to be used earlier in
  the parameter list.

Modified:
    trunk/HACKERS
    trunk/README.examples
    trunk/examples/util.cpp
    trunk/lib/connection.cpp
    trunk/lib/connection.h

Modified: trunk/HACKERS
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/HACKERS?rev=1689&r1=1688&r2=1689&view=diff
==============================================================================
--- trunk/HACKERS (original)
+++ trunk/HACKERS Fri Jul 13 05:53:33 2007
@@ -54,7 +54,7 @@
     like you would most of the examples, except that you don't need
     to run it via exrun:
 
-        $ ./dtest [host] [user] [pass]
+        $ ./dtest [password] [user] [server_addr]
 
     This automatically runs most of the examples, captures the outputs
     to a file, and then compares that to a known-good run's outputs.
@@ -63,12 +63,12 @@
     has and you can't account for it, it represents a problem that
     you'll have to fix before submitting the patch.
 
-    If your change purposely causes different outputs from a dtest
-    run, remove the bmark.txt file, then re-run dtest and include
-    the bmark.txt diffs with your patch.  This communicates to us
-    the fact that you know there are differences and want the patch
-    evaluated anyway.  Otherwise, we are likely to view the change
-    as a bug.
+    If your change purposely causes different outputs from the dtest
+    run stored in svn, remove the bmark.txt file, then re-run dtest and
+    include the bmark.txt diffs with your patch.  This communicates
+    to us the fact that you know there are differences and want the
+    patch evaluated anyway.  Otherwise, we are likely to view the
+    change as a bug.
 
 
 Adding Support for a Different Compiler

Modified: trunk/README.examples
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/README.examples?rev=1689&r1=1688&r2=1689&view=diff
==============================================================================
--- trunk/README.examples (original)
+++ trunk/README.examples Fri Jul 13 05:53:33 2007
@@ -30,11 +30,29 @@
     Before running the other examples, you must first create the
     sample database.  On POSIX systems, you do that like so:
 
-        $ ./exrun resetdb [host] [user] [password] [port]
+        $ ./exrun resetdb [password] [user] [server_addr]
 
     On Windows, that would instead be:
 
-        C:\mysql++\> exrun.bat resetdb [host] [user] [pass] [port]
+        C:\mysql++\> exrun.bat resetdb [password] [user] [server_addr]
+
+    You may give any number of these arguments, but they must be in
+    the order listed, and you can't skip over arguments.  So, you
+    may give just a password, in which case it will assume you mean
+    to connect using your current user name to a server on the local
+    machine using the default connection method.  If you wanted to give
+    a host name, you would also have to give a user name and password.
+
+    The server_addr parameter accepts many different forms.  The main
+    one is some sort of TCP/IP address, in either dotted quad IP form
+    or as a host or domain name.  You can also give a Unix domain
+    socket name, or just a period if on Windows to select named pipes.
+    If you give a TCP/IP address, you can follow it with a colon and
+    a port number or a symbolic service name.  All of these are legal:
+
+        localhost
+        172.20.0.252:12345
+        my.server.name.com:mysql
 
     If you mistakenly run resetdb directly instead of through exrun,
     it will either fail to run because it can't find the MySQL++

Modified: trunk/examples/util.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/examples/util.cpp?rev=1689&r1=1688&r2=1689&view=diff
==============================================================================
--- trunk/examples/util.cpp (original)
+++ trunk/examples/util.cpp Fri Jul 13 05:53:33 2007
@@ -176,7 +176,7 @@
 print_usage(const char* program_name, const char* extra_parms)
 {
        cout << "usage: " << program_name <<
-                       " [host [user [password [port]]]] " << extra_parms << 
endl;
+                       " [password [user [server_addr]]] " << extra_parms << 
endl;
        cout << endl;
        cout << "    If no arguments are given, connects to database "
                        "server on localhost" << endl;
@@ -228,11 +228,8 @@
        else if (argc == 3) {
                con.connect(kdb, argv[1], argv[2]);
        }
-       else if (argc == 4) {
+       else if (argc >= 4) {
                con.connect(kdb, argv[1], argv[2], argv[3]);
-       }
-       else if (argc >= 5) {
-               con.connect(kdb, argv[1], argv[2], argv[3], atoi(argv[4]));
        }
 
        if (con) {

Modified: trunk/lib/connection.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/connection.cpp?rev=1689&r1=1688&r2=1689&view=diff
==============================================================================
--- trunk/lib/connection.cpp (original)
+++ trunk/lib/connection.cpp Fri Jul 13 05:53:33 2007
@@ -31,6 +31,12 @@
 
 #include "query.h"
 #include "result.h"
+
+#if !defined(MYSQLPP_PLATFORM_WINDOWS)
+#      include <unistd.h>
+#      include <sys/stat.h>
+#      include <netdb.h>
+#endif
 
 // An argument was added to mysql_shutdown() in MySQL 4.1.3 and 5.0.1.
 #if ((MYSQL_VERSION_ID >= 40103) && (MYSQL_VERSION_ID <= 49999)) || 
(MYSQL_VERSION_ID >= 50001)
@@ -113,15 +119,14 @@
 }
 
 
-Connection::Connection(const char* db, const char* host,
-               const char* user, const char* passwd, uint port,
-               cchar* socket_name, unsigned long client_flag) :
+Connection::Connection(cchar* db, cchar* password, cchar* user,
+               cchar* server, unsigned long client_flag) :
 OptionalExceptions(),
 Lockable(false),
 connecting_(false)
 {
        mysql_init(&mysql_);
-       if (connect(db, host, user, passwd, port, socket_name, client_flag)) {
+       if (connect(db, password, user, server, client_flag)) {
                unlock();
                copacetic_ = is_connected_ = true;
        }
@@ -159,9 +164,8 @@
 
 
 bool
-Connection::connect(cchar* db, cchar* host, cchar* user,
-               cchar* passwd, uint port, cchar* socket_name,
-               unsigned long client_flag)
+Connection::connect(cchar* db, cchar* password, cchar* user,
+               cchar* server, unsigned long client_flag)
 {
        lock();
 
@@ -183,10 +187,15 @@
        }
 #endif
 
-       // Establish connection
+       // Figure out what the server parameter means, then establish 
+       // the connection.
+       unsigned int port = 0;
+       string host, socket_name;
        scoped_var_set<bool> sb(connecting_, true);
-       if (mysql_real_connect(&mysql_, host, user, passwd, db, port,
-                       socket_name, client_flag)) {
+       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)) {
                unlock();
                is_connected_ = true;
 
@@ -213,8 +222,20 @@
 bool
 Connection::connect(const MYSQL& mysql)
 {
-       return connect(mysql.db, mysql.host, mysql.user, mysql.passwd,
-                       mysql.port, mysql.unix_socket, mysql.client_flag);
+       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);
+       }
 }
 
 
@@ -715,5 +736,72 @@
 }
 
 
+bool
+Connection::parse_ipc_method(const char* server, string& host,
+               unsigned int& port, string& socket_name)
+{
+       if (server == 0) {
+               // Just take all the defaults
+               return true;
+       }
+
+       // Try the platform-specific alternatives first.
+#if MYSQLPP_PLATFORM_WINDOWS
+       if (strcmp(server, ".") == 0) {
+               // Use Windows named pipes
+               host = server;
+               return true;
+       }
+#else
+       struct stat fi;
+       if ((access(server, R_OK | W_OK) == 0) &&
+                       (stat(server, &fi) == 0) &&
+                       S_ISSOCK(fi.st_mode)) {
+               // It's a Unix domain socket, and we have permission to use it.
+               socket_name = server;
+               return true;
+       }
+#endif
+
+       // Lacking any better idea, it must be some kind of TCP/IP address.
+       // See if it includes a trailing port or service name.
+       const char* colon = strchr(server, ':');
+       if (colon) {
+               if (colon[1]) {
+                       // Not just a lonely trailing colon, so assume what 
follows
+                       // the colon is of substance.
+                       const char* service = colon + 1;
+                       if (isdigit(service[0])) {
+                               port = atoi(service);
+                               if ((port < 1) || (port > USHRT_MAX)) {
+                                       return false;
+                               }
+                               else {
+                               }
+                       }
+                       else {
+                               servent* pse = getservbyname(service, "tcp");
+                               if (pse) {
+                                       port = ntohs(pse->s_port);
+                               }
+                               else {
+                                       return false;
+                               }
+                       }
+               }
+
+               // We're happy with what we found after the colon, so treat the
+               // rest of the name as a host address.
+               host.assign(server, colon - server);
+       }
+       else {
+               // No colon, so treat the whole thing as a host address.
+               host = server;
+       }
+
+       return true;
+}
+
+
 } // end namespace mysqlpp
 

Modified: trunk/lib/connection.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/connection.h?rev=1689&r1=1688&r2=1689&view=diff
==============================================================================
--- trunk/lib/connection.h (original)
+++ trunk/lib/connection.h Fri Jul 13 05:53:33 2007
@@ -117,29 +117,40 @@
        /// \brief Create object and connect to database server in one step.
        ///
        /// This constructor allows you to most fully specify the options
-       /// used when connecting to the MySQL database.  It is the thinnest
-       /// layer in MySQL++ over the MySQL C API function
-       /// \c mysql_real_connect().  The correspondence isn't exact, as we
-       /// reorder some of the parameters, give most of them defaults, and
-       /// leave some of them out.
+       /// used when connecting to the MySQL database.  It is a thick
+       /// wrapper around the MySQL C API function \c mysql_real_connect()
+       /// with a greatly simplified interface.
        ///
        /// \param db name of database to use
-       /// \param host host name or IP address of MySQL server, or 0
-       ///     if server is running on the same host as your program
+       /// \param password password to use when logging in
        /// \param user user name to log in under, or 0 to use the user
        ///             name this program is running under
-       /// \param passwd password to use when logging in
-       /// \param port TCP port number MySQL server is listening on, or 0
-       ///             to use default value
-       /// \param socket_name Unix domain socket server is using, if
-       ///             connecting to MySQL server on the same host as this 
program
-       ///             running on, or 0 to use default name
+       /// \param server specifies the IPC method and parameters for
+       ///     contacting the server; see below for details
        /// \param client_flag special connection flags. See MySQL C API
        ///     documentation for \c mysql_real_connect() for details.  
-       Connection(const char* db, const char* host = "",
-                       const char* user = "", const char* passwd = "",
-                       uint port = 0, cchar* socket_name = 0,
-                       unsigned long client_flag = 0);
+       ///
+       /// The server parameter can be any of several different forms:
+       ///
+       /// - \b 0: Let the MySQL C API decide how to connect.  This usually
+       ///   means Unix domain sockets with the default socket name on
+       ///   *ix systems, and shared memory on Windows.  If those options
+       ///   aren't available, it will be a TCP/IP connection to the
+       ///   localhost address.
+       /// - \b ".": On Windows, this means named pipes, if the server
+       ///   supports it
+       /// - \b "/some/domain/socket/path": If the passed string doesn't
+       ///   match one of the previous alternatives and we're on a system
+       ///   that supports Unix domain sockets, MySQL++ will test it to see
+       ///   if it names one, and use it if we have permission.
+       /// - \b "host.name.or.ip:port": If the previous test fails, or if
+       ///   the system doesn't support Unix domain sockets at all, it
+       ///   assumes the string is some kind of network address, optionally
+       ///   followed by a colon and port.  The name can be in dotted quad
+       ///   form, a host name, or a domain name.  The port can either be a
+       ///   TCP/IP port number or a symbolic service name.
+       Connection(cchar* db, cchar* password = 0, cchar* user = 0,
+                       cchar* server = 0, unsigned long client_flag = 0);
 
        /// \brief Establish a new connection using the same parameters as
        /// an existing C API connection.
@@ -164,9 +175,8 @@
        /// If you call this method on an object that is already connected
        /// to a database server, the previous connection is dropped and a
        /// new connection is established.
-       bool connect(cchar* db = "", cchar* host = "",
-                       cchar* user = "", cchar* passwd = "", uint port = 0,
-                       cchar* socket_name = 0, unsigned long client_flag = 0);
+       bool connect(cchar* db, cchar* password = 0, cchar* user = 0,
+                       cchar* server = 0, unsigned long client_flag = 0);
 
        /// \brief Close connection to MySQL server.
        ///
@@ -503,6 +513,12 @@
        /// \param other the connection to copy
        void copy(const Connection& other);
 
+       /// \brief Parse the server parameter to the connect-on-creation
+       /// ctor and connect() into values suitable for passing to the
+       /// MySQL C API's \c mysql_real_connect() function.
+       bool parse_ipc_method(const char* server, std::string& host,
+                       unsigned int& port, std::string& socket_name);
+
 private:
        friend class ResNSel;
        friend class ResUse;


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

Reply via email to