Date: Monday, December 12, 2005 @ 08:58:57
  Author: csaba
    Path: /cvsroot/carob/libmysequoia

Modified: include/CarobMySQL.hpp (1.7 -> 1.8) src/CarobMySQL.cpp (1.13 ->
          1.14) src/MySQLAPI.cpp (1.10 -> 1.11) test/TestCarobMySQL.cpp
          (1.1 -> 1.2) test/TestMySQLAPI.cpp (1.7 -> 1.8)

Refactoring the code:
- get rid of the CMYSQL structure
- store the pointer to the CarobMYSQL object in mysql->thd
- mysql_init and mysql_close finally works like are specified in MySQL C API


-------------------------+
 include/CarobMySQL.hpp  |   18 +++-----
 src/CarobMySQL.cpp      |   96 +++++++++++++++++++---------------------------
 src/MySQLAPI.cpp        |   82 +++++++++++++++++++++++++--------------
 test/TestCarobMySQL.cpp |    2 
 test/TestMySQLAPI.cpp   |    4 -
 5 files changed, 105 insertions(+), 97 deletions(-)


Index: libmysequoia/include/CarobMySQL.hpp
diff -u libmysequoia/include/CarobMySQL.hpp:1.7 
libmysequoia/include/CarobMySQL.hpp:1.8
--- libmysequoia/include/CarobMySQL.hpp:1.7     Fri Dec  9 15:29:00 2005
+++ libmysequoia/include/CarobMySQL.hpp Mon Dec 12 08:58:57 2005
@@ -31,17 +31,10 @@
 #include <DriverResultSet.hpp>
 #include <Statement.hpp>
 
-class CarobMYSQL;
-
-typedef struct st_cmysql {
-       MYSQL my;
-       CarobMYSQL *carob;
-} CMYSQL;
-
 class CarobMYSQL
 {
 public:
-       CarobMYSQL();
+       CarobMYSQL(MYSQL *mysql);
        ~CarobMYSQL();
 
     /**
@@ -192,7 +185,7 @@
        MYSQL *getMYSQL();
 
 private:
-       CMYSQL *mysqlPtr;
+       MYSQL *mysqlPtr;
        CarobNS::Connection *connectionPtr;
        CarobNS::ConnectionPool *connectionPool;
        CarobNS::Statement *stmtPtr;
@@ -201,6 +194,11 @@
        MYSQL_RES *liveResultPtr;
   
   /**
+   * Private default constructor.
+   */
+   CarobMYSQL() {}
+
+  /**
    * Allocate and fill the MYSQL structure
    * @param host name of the host to connect
    * @param user name of the user which is used in connection
@@ -215,7 +213,7 @@
    * Delete and free the actual connection and it's associated resources
    * @param free_mysql defines if the internal mysql structure is destroyed
    */
-  void delete_connection(bool free_mysql);
+  void delete_connection();
   
   /**
    * Set the current mysql error to the specified one. The mysql error message 
will be filled from the error list.
Index: libmysequoia/src/CarobMySQL.cpp
diff -u libmysequoia/src/CarobMySQL.cpp:1.13 
libmysequoia/src/CarobMySQL.cpp:1.14
--- libmysequoia/src/CarobMySQL.cpp:1.13        Fri Dec  9 15:29:00 2005
+++ libmysequoia/src/CarobMySQL.cpp     Mon Dec 12 08:58:57 2005
@@ -28,14 +28,9 @@
 
 using namespace CarobNS;
 
-CarobMYSQL::CarobMYSQL (): connectionPtr(0), stmtPtr(0), drsPtr(0), 
liveResultPtr(0)
+CarobMYSQL::CarobMYSQL (MYSQL *mysql): connectionPtr(0), stmtPtr(0), 
drsPtr(0), liveResultPtr(0)
 {
-  //TODO handle special case when not enough memory
-  mysqlPtr = new CMYSQL();
-  memset(&mysqlPtr->my, 0, sizeof(MYSQL));
-  mysqlPtr->carob = this;
-  //TODO maybe a real server version string ??
-  mysqlPtr->my.server_version = "50015";
+  mysqlPtr = mysql;
 
   connectionPool = &ConnectionPool::getInstance();
   
@@ -50,9 +45,7 @@
 
 CarobMYSQL::~CarobMYSQL ()
 {
-  if (mysqlPtr) delete mysqlPtr;
-  
-  delete_connection(true);
+  delete_connection();
 }
 
 bool
@@ -60,7 +53,7 @@
                     const char *passwd, const char *db, unsigned int port,
                     const char *unix_socket, unsigned long clientflag)
 {
-  if (mysqlPtr->my.status != MYSQL_STATUS_READY)
+  if (mysqlPtr->status != MYSQL_STATUS_READY)
   {
     set_error(CR_COMMANDS_OUT_OF_SYNC, SQLT_UNKNOWN);
     return 0;
@@ -92,7 +85,7 @@
    * The real connection will happen after the user call the command 
mysql_select_db or mysql_real_connect */
   if (!db || !*db)
   {
-    delete_connection(false);
+    delete_connection();
     set_connect_info(host, user, passwd, 0, port);
     
     return true;
@@ -107,7 +100,7 @@
     if (newConnectionPtr)
     {
       //TODO handle not enough memory
-      delete_connection(false);
+      delete_connection();
       set_connect_info(host, user, passwd, db, port);
 
       connectionPtr = newConnectionPtr;
@@ -130,9 +123,9 @@
 CarobMYSQL::select_db (const char *db)
 {
   /* Connect only if a user, password and host was already specified, 
otherwise raise an error */
-  if (mysqlPtr->my.user && mysqlPtr->my.passwd && mysqlPtr->my.host)
-    return connect(mysqlPtr->my.host, mysqlPtr->my.user, mysqlPtr->my.passwd, 
db, mysqlPtr->my.port, 
-      0, mysqlPtr->my.client_flag);
+  if (mysqlPtr->user && mysqlPtr->passwd && mysqlPtr->host)
+    return connect(mysqlPtr->host, mysqlPtr->user, mysqlPtr->passwd, db, 
mysqlPtr->port, 
+      0, mysqlPtr->client_flag);
   else
   {
     set_error(CR_NULL_POINTER, SQLT_UNKNOWN);
@@ -143,13 +136,13 @@
 bool
 CarobMYSQL::change_user (const char *user, const char *passwd, const char *db)
 {
-  return connect(mysqlPtr->my.host, user, passwd, db ? db : mysqlPtr->my.db, 
mysqlPtr->my.port, 0, mysqlPtr->my.client_flag);
+  return connect(mysqlPtr->host, user, passwd, db ? db : mysqlPtr->db, 
mysqlPtr->port, 0, mysqlPtr->client_flag);
 }
 
 bool
 CarobMYSQL::real_query (const char *query, ulong length)
 {
-  if (mysqlPtr->my.status != MYSQL_STATUS_READY)
+  if (mysqlPtr->status != MYSQL_STATUS_READY)
   {
     set_error(CR_COMMANDS_OUT_OF_SYNC, SQLT_UNKNOWN);
     return 0;
@@ -160,13 +153,13 @@
 
     if (stmtPtr->execute(fromString(std::string(query,length))))
     {
-      mysqlPtr->my.affected_rows = 0;
-      mysqlPtr->my.status = MYSQL_STATUS_GET_RESULT;
+      mysqlPtr->affected_rows = 0;
+      mysqlPtr->status = MYSQL_STATUS_GET_RESULT;
     }
     else
     {
-      mysqlPtr->my.affected_rows = stmtPtr->getUpdateCount();
-      mysqlPtr->my.status = MYSQL_STATUS_READY;
+      mysqlPtr->affected_rows = stmtPtr->getUpdateCount();
+      mysqlPtr->status = MYSQL_STATUS_READY;
     }
     
     return true;
@@ -182,7 +175,7 @@
 MYSQL_RES *
 CarobMYSQL::get_results (my_bool fetch_all)
 {
-  if (mysqlPtr->my.status != MYSQL_STATUS_GET_RESULT)
+  if (mysqlPtr->status != MYSQL_STATUS_GET_RESULT)
   {
     set_error(CR_COMMANDS_OUT_OF_SYNC, SQLT_UNKNOWN);
     return 0;
@@ -208,7 +201,7 @@
 
         //Fill the fields metadata        
         result->field_count = get_query_fields(result->fields, drsPtr);
-        result->handle = (MYSQL *)mysqlPtr;
+        result->handle = mysqlPtr;
         
         //Fetch the rows if fetch_all was specified
         if (fetch_all)
@@ -232,7 +225,7 @@
           }
           result->row_count=result->data->rows;
           result->data_cursor = result->data->data;
-          mysqlPtr->my.status = MYSQL_STATUS_READY;
+          mysqlPtr->status = MYSQL_STATUS_READY;
           
           //TODO release carob resultset
           drsPtr = 0;
@@ -241,12 +234,12 @@
         {
           //TODO check when to free the main internal liveResultPtr
           liveResultPtr = result;
-          mysqlPtr->my.status = MYSQL_STATUS_USE_RESULT;
+          mysqlPtr->status = MYSQL_STATUS_USE_RESULT;
         }
 
-        mysqlPtr->my.field_count = result->field_count;
-        mysqlPtr->my.affected_rows = result->row_count;
-        mysqlPtr->my.fields = result->fields;
+        mysqlPtr->field_count = result->field_count;
+        mysqlPtr->affected_rows = result->row_count;
+        mysqlPtr->fields = result->fields;
       }
       else
         set_error(CR_NO_RESULT_SET, SQLT_UNKNOWN);
@@ -266,7 +259,7 @@
 MYSQL_ROW
 CarobMYSQL::fetch_row()
 {
-  if (mysqlPtr->my.status != MYSQL_STATUS_USE_RESULT || !drsPtr)
+  if (mysqlPtr->status != MYSQL_STATUS_USE_RESULT || !drsPtr)
   {
     set_error(CR_COMMANDS_OUT_OF_SYNC, SQLT_UNKNOWN);
     return 0;
@@ -329,16 +322,16 @@
       delete_row_data(res->current_row,res->field_count);
     }
     
-    if (mysqlPtr->my.fields == res->fields)
+    if (mysqlPtr->fields == res->fields)
     {
-      mysqlPtr->my.fields = 0;
-      mysqlPtr->my.field_count = 0;
+      mysqlPtr->fields = 0;
+      mysqlPtr->field_count = 0;
     }
     delete [] res->fields;
     
     delete res;
   }
-  mysqlPtr->my.status = MYSQL_STATUS_READY;
+  mysqlPtr->status = MYSQL_STATUS_READY;
 }
 
 bool
@@ -386,36 +379,27 @@
 MYSQL *
 CarobMYSQL::getMYSQL ()
 {
-  return (MYSQL *)mysqlPtr;
+  return mysqlPtr;
 }
 
 void
 CarobMYSQL::set_connect_info(const char *host, const char *user,
             const char *passwd, const char *db, unsigned int port)
 {
-  cstrdupcond(mysqlPtr->my.host, host);
-  cstrdupcond(mysqlPtr->my.user, user);
-  cstrdupcond(mysqlPtr->my.passwd, passwd);
-  cstrdupcond(mysqlPtr->my.db, db);
-  mysqlPtr->my.port = port;
+  cstrdupcond(mysqlPtr->host, host);
+  cstrdupcond(mysqlPtr->user, user);
+  cstrdupcond(mysqlPtr->passwd, passwd);
+  cstrdupcond(mysqlPtr->db, db);
+  mysqlPtr->port = port;
 
   char buf[100];
   snprintf(buf, sizeof(buf), "%s via TCP/IP", host); buf[sizeof(buf)-1]='\0';
-  cstrdupcond(mysqlPtr->my.host_info, buf);
+  cstrdupcond(mysqlPtr->host_info, buf);
 }
 
 void
-CarobMYSQL::delete_connection(bool free_mysql)
+CarobMYSQL::delete_connection()
 {
-  if (free_mysql)
-  {
-    FREE_AND_NULL_ARRAY(mysqlPtr->my.host);
-    FREE_AND_NULL_ARRAY(mysqlPtr->my.user);
-    FREE_AND_NULL_ARRAY(mysqlPtr->my.passwd);
-    FREE_AND_NULL_ARRAY(mysqlPtr->my.db);
-    FREE_AND_NULL_ARRAY(mysqlPtr->my.host_info);
-  }
-  
   FREE_AND_NULL(connectionPtr);
   FREE_AND_NULL(stmtPtr);
   FREE_AND_NULL(liveResultPtr);
@@ -430,15 +414,15 @@
 void
 CarobMYSQL::set_error(int errcode, const char *errmsg, const char *sqlstate)
 {
-  mysqlPtr->my.net.last_errno = convert_errcode(errcode);
+  mysqlPtr->net.last_errno = convert_errcode(errcode);
   if (errmsg)
-    strcpy(mysqlPtr->my.net.last_error, errmsg);
+    strcpy(mysqlPtr->net.last_error, errmsg);
   else
-    *mysqlPtr->my.net.last_error = 0;
+    *mysqlPtr->net.last_error = 0;
   if (sqlstate)
-    strcpy(mysqlPtr->my.net.sqlstate, sqlstate);
+    strcpy(mysqlPtr->net.sqlstate, sqlstate);
   else
-    *mysqlPtr->my.net.sqlstate = 0;
+    *mysqlPtr->net.sqlstate = 0;
 }
 
 int
Index: libmysequoia/src/MySQLAPI.cpp
diff -u libmysequoia/src/MySQLAPI.cpp:1.10 libmysequoia/src/MySQLAPI.cpp:1.11
--- libmysequoia/src/MySQLAPI.cpp:1.10  Fri Dec  9 15:29:00 2005
+++ libmysequoia/src/MySQLAPI.cpp       Mon Dec 12 08:58:57 2005
@@ -29,41 +29,67 @@
 /* MySQL include */
 #include <errmsg.h>
 
+/**
+ * The pointer to the CarobMySQL object is stored
+ * in the thd member of the MYSQL structure.
+ * This function returns a pointer to the CarobMYSQL
+ * object
+ */ 
+static
+CarobMYSQL *getCarob(MYSQL *mysql)
+{
+  return static_cast<CarobMYSQL *> (mysql->thd);
+}
+
 /* Library initialization, shutdown */
 
-/**
- * Gets or initializes a MYSQL structure.
- * Difference from the MySQL API:
- * - we expect NULL for the argument
- * - otherwise bad things happen (the user
- *   doesn't allocate the carob object)
- */
+/*Gets or initializes a MYSQL structure. */
 MYSQL *STDCALL
 mysql_init (MYSQL * mysql)
 {
+  my_bool free_in_close = false;
+  
   if (!mysql)
   {
-    CarobMYSQL *cmysql = new CarobMYSQL();
-    if (cmysql)
-      mysql = cmysql->getMYSQL();
+    mysql = new MYSQL();
+    if (mysql)
+    {
+      free_in_close = true;
+    }
     else
+    {
       return 0;
+    }
   }
+
+  /* initialize the mysql structure */
+  memset(mysql, 0, sizeof(MYSQL));
+  mysql->free_me = free_in_close;
+  //TODO maybe a real server version string ???
+  mysql->server_version = "50015";
+
+  CarobMYSQL *cmysql = new CarobMYSQL(mysql);
+  if (cmysql)
+  {
+    mysql->thd = cmysql;
+  }
+  else
+  {
+    FREE_AND_NULL(mysql);
+    return 0;
+  }
+    
   return mysql;
 }
 
-/**
- * Closes a server connection. 
- * Difference from the MYSQL API:
- * - if mysql_init() was called with !NULL value
- *   the carob object does not exist, probably
- *   segfault
- */
+/* Closes a server connection. */
 void STDCALL
 mysql_close (MYSQL * mysql)
 {
   if (mysql)
-    delete ((CMYSQL *)mysql)->carob;
+    delete getCarob(mysql);
+  if (mysql->free_me)
+    FREE_AND_NULL(mysql);
 }
 
 /* Initialize the MYSQL client library. */
@@ -88,10 +114,10 @@
 {
   if (mysql)
   {
-    return ((CMYSQL *)mysql)->carob->connect(host, user, passwd, 0, 0, 0, 0) ? 
mysql : 0;
+    return getCarob(mysql)->connect(host, user, passwd, 0, 0, 0, 0) ? mysql : 
0;
   }
   else
-       return 0;
+    return 0;
 }
 
 /**
@@ -109,7 +135,7 @@
 {
   if (mysql)
   {
-    return ((CMYSQL *)mysql)->carob->connect(host, user, passwd, db, port, 
unix_socket, clientflag) ? mysql : 0;
+    return getCarob(mysql)->connect(host, user, passwd, db, port, unix_socket, 
clientflag) ? mysql : 0;
   }
   else
     return 0;
@@ -121,7 +147,7 @@
 {
   if (mysql)
   {
-    return ((CMYSQL *)mysql)->carob->select_db(db) ? 0 : 1;
+    return getCarob(mysql)->select_db(db) ? 0 : 1;
   }
   else
     return 0;
@@ -134,7 +160,7 @@
 {
   if (mysql)
   {
-    return !((CMYSQL *)mysql)->carob->change_user(user, passwd, db);
+    return !getCarob(mysql)->change_user(user, passwd, db);
   }
   else
     return 1;
@@ -167,7 +193,7 @@
 mysql_real_query (MYSQL * mysql, const char *q, unsigned long length)
 {
   if (mysql)
-    return ((CMYSQL *)mysql)->carob->real_query(q, length) ? 0 : 1;
+    return getCarob(mysql)->real_query(q, length) ? 0 : 1;
   else    
     return 1;
 }
@@ -177,7 +203,7 @@
 mysql_use_result (MYSQL * mysql)
 {
   if (mysql)
-    return ((CMYSQL *)mysql)->carob->get_results(false);
+    return getCarob(mysql)->get_results(false);
   else
     return 0;
 }
@@ -187,7 +213,7 @@
 mysql_store_result (MYSQL * mysql)
 {
   if (mysql)
-    return ((CMYSQL *)mysql)->carob->get_results(true);
+    return getCarob(mysql)->get_results(true);
   else
     return 0;
 }
@@ -197,7 +223,7 @@
 mysql_free_result (MYSQL_RES * result)
 {
   if (result && result->handle)
-    return ((CMYSQL *)result->handle)->carob->free_results(result);
+    return getCarob(result->handle)->free_results(result);
 }
 
 /* Fetches the next row from the result set. */
@@ -221,7 +247,7 @@
       return (result->current_row = curRow);
     }
     else
-      return ((CMYSQL *)result->handle)->carob->fetch_row();
+      return getCarob(result->handle)->fetch_row();
   }
   else
     return 0;
Index: libmysequoia/test/TestCarobMySQL.cpp
diff -u libmysequoia/test/TestCarobMySQL.cpp:1.1 
libmysequoia/test/TestCarobMySQL.cpp:1.2
--- libmysequoia/test/TestCarobMySQL.cpp:1.1    Mon Dec  5 14:03:37 2005
+++ libmysequoia/test/TestCarobMySQL.cpp        Mon Dec 12 08:58:57 2005
@@ -25,7 +25,7 @@
 
 void TestCarobMySQL::setUp(void)
 {
-  carobMySQL = new CarobMYSQL();
+  carobMySQL = new CarobMYSQL(NULL);
 }
 
 void TestCarobMySQL::tearDown(void) 
Index: libmysequoia/test/TestMySQLAPI.cpp
diff -u libmysequoia/test/TestMySQLAPI.cpp:1.7 
libmysequoia/test/TestMySQLAPI.cpp:1.8
--- libmysequoia/test/TestMySQLAPI.cpp:1.7      Fri Dec  9 13:14:42 2005
+++ libmysequoia/test/TestMySQLAPI.cpp  Mon Dec 12 08:58:57 2005
@@ -42,7 +42,7 @@
 void TestMySQLAPI::mysql_init_test(void)
 {
   CPPUNIT_ASSERT(mysql != 0);
-  CPPUNIT_ASSERT(((CMYSQL *)mysql)->carob != 0);
+  CPPUNIT_ASSERT(mysql->thd != 0); // the CarobMYSQL object
 }
 
 
@@ -74,8 +74,8 @@
 /* TODO put in the config file an username with an empty password and uncoment
    the test
   CPPUNIT_ASSERT(mysql_real_connect(mysql, HOST, USER1, "", DB1, 0, 0, 0) != 
0);  
-*/
   CPPUNIT_ASSERT(mysql_real_connect(mysql, HOST, USER1, 0, DB1, 0, 0, 0) != 
0);  
+*/
 }
 
 void TestMySQLAPI::mysql_real_connect_negative_test(void)

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to