Date: Tuesday, December 13, 2005 @ 14:09:45
  Author: csaba
    Path: /cvsroot/carob/libmysequoia

Modified: include/CarobMySQL.hpp (1.9 -> 1.10) include/Utils.hpp (1.7 ->
          1.8) src/CarobMySQL.cpp (1.15 -> 1.16) src/MySQLAPI.cpp (1.12 ->
          1.13) test/TestCarobMySQL.cpp (1.2 -> 1.3) test/TestMySQLAPI.cpp
          (1.11 -> 1.12)

Safety check in mysql_init() and checking if the CarobMYSQL object is 
initialized corectly.


-------------------------+
 include/CarobMySQL.hpp  |    4 ++-
 include/Utils.hpp       |    4 +--
 src/CarobMySQL.cpp      |   21 ++++++++++++++++++-
 src/MySQLAPI.cpp        |   50 +++++++++++++++++++++++-----------------------
 test/TestCarobMySQL.cpp |    4 +--
 test/TestMySQLAPI.cpp   |   14 +++++++++++-
 6 files changed, 66 insertions(+), 31 deletions(-)


Index: libmysequoia/include/CarobMySQL.hpp
diff -u libmysequoia/include/CarobMySQL.hpp:1.9 
libmysequoia/include/CarobMySQL.hpp:1.10
--- libmysequoia/include/CarobMySQL.hpp:1.9     Mon Dec 12 14:22:40 2005
+++ libmysequoia/include/CarobMySQL.hpp Tue Dec 13 14:09:45 2005
@@ -31,10 +31,12 @@
 #include <DriverResultSet.hpp>
 #include <Statement.hpp>
 
+const unsigned long MYSEQUOIA_MAGIC = 0xC00CA10B;
+
 class CarobMYSQL
 {
 public:
-       CarobMYSQL(MYSQL *mysql);
+       CarobMYSQL(MYSQL *mysql, my_bool free_me);
        ~CarobMYSQL();
 
     /**
Index: libmysequoia/include/Utils.hpp
diff -u libmysequoia/include/Utils.hpp:1.7 libmysequoia/include/Utils.hpp:1.8
--- libmysequoia/include/Utils.hpp:1.7  Mon Dec 12 14:22:40 2005
+++ libmysequoia/include/Utils.hpp      Tue Dec 13 14:09:45 2005
@@ -35,8 +35,8 @@
 
 extern const char *error_messages[];
 
-#define FREE_AND_NULL(p) {delete p; p=0;}
-#define FREE_AND_NULL_ARRAY(p) {delete[] p; p=0;}
+#define FREE_AND_NULL(p) {delete (p); (p)=0;}
+#define FREE_AND_NULL_ARRAY(p) {delete[] (p); (p)=0;}
 
 /**
  * Creates a duplicate of a null terminated string and return the pointer to 
it.
Index: libmysequoia/src/CarobMySQL.cpp
diff -u libmysequoia/src/CarobMySQL.cpp:1.15 
libmysequoia/src/CarobMySQL.cpp:1.16
--- libmysequoia/src/CarobMySQL.cpp:1.15        Mon Dec 12 14:22:40 2005
+++ libmysequoia/src/CarobMySQL.cpp     Tue Dec 13 14:09:44 2005
@@ -29,8 +29,18 @@
 
 using namespace CarobNS;
 
-CarobMYSQL::CarobMYSQL (MYSQL *mysql): connectionPtr(0), stmtPtr(0), 
drsPtr(0), liveResultPtr(0)
+CarobMYSQL::CarobMYSQL (MYSQL *mysql, my_bool free_me) :
+   connectionPtr(0), stmtPtr(0), drsPtr(0), liveResultPtr(0)
 {
+  /* initialize the mysql structure */
+  memset(mysql, 0, sizeof(MYSQL));
+  
+  mysql->extra_info = MYSEQUOIA_MAGIC;
+  mysql->free_me = free_me;
+  //TODO maybe a real server version string ???
+  mysql->server_version = "50015";
+  // TODO fill in other required mysql fields
+  
   mysqlPtr = mysql;
 
   connectionPool = &ConnectionPool::getInstance();
@@ -39,6 +49,15 @@
 CarobMYSQL::~CarobMYSQL ()
 {
   delete_connection();
+
+  FREE_AND_NULL_ARRAY(mysqlPtr->host);
+  FREE_AND_NULL_ARRAY(mysqlPtr->user);
+  FREE_AND_NULL_ARRAY(mysqlPtr->passwd);
+  FREE_AND_NULL_ARRAY(mysqlPtr->db);
+  FREE_AND_NULL_ARRAY(mysqlPtr->host_info);
+  
+  mysqlPtr->thd = 0;
+  mysqlPtr->extra_info = 0;
 }
 
 bool
Index: libmysequoia/src/MySQLAPI.cpp
diff -u libmysequoia/src/MySQLAPI.cpp:1.12 libmysequoia/src/MySQLAPI.cpp:1.13
--- libmysequoia/src/MySQLAPI.cpp:1.12  Mon Dec 12 14:22:40 2005
+++ libmysequoia/src/MySQLAPI.cpp       Tue Dec 13 14:09:44 2005
@@ -30,6 +30,15 @@
 #include <errmsg.h>
 
 /**
+ * Return true if p is containing our Carob object
+ */
+static
+bool isCarobObject(MYSQL *p)
+{
+  return (p != 0) && (p->extra_info == MYSEQUOIA_MAGIC);
+}
+
+/**
  * The pointer to the CarobMySQL object is stored
  * in the thd member of the MYSQL structure.
  * This function returns a pointer to the CarobMYSQL
@@ -38,6 +47,9 @@
 static
 CarobMYSQL *getCarob(MYSQL *mysql)
 {
+  if (!isCarobObject(mysql))
+    mysql->thd = new CarobMYSQL(mysql, 0);
+
   return static_cast<CarobMYSQL *> (mysql->thd);
 }
 
@@ -49,37 +61,27 @@
 {
   my_bool free_in_close = false;
   
-  if (!mysql)
+  try
   {
-    mysql = new MYSQL();
-    if (mysql)
+    if (!mysql)
     {
+      mysql = new MYSQL();
+      mysql->thd = 0;
       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;
+  
+    if (isCarobObject(mysql))
+      delete static_cast<CarobMYSQL *>(mysql->thd);
+      
+    mysql->thd = new CarobMYSQL(mysql, free_in_close);
+    return mysql;
   }
-  else
+  catch (...)
   {
-    FREE_AND_NULL(mysql);
-    return 0;
+    if (free_in_close)
+      delete mysql;
+    return 0;  
   }
-    
-  return mysql;
 }
 
 /* Closes a server connection. */
Index: libmysequoia/test/TestCarobMySQL.cpp
diff -u libmysequoia/test/TestCarobMySQL.cpp:1.2 
libmysequoia/test/TestCarobMySQL.cpp:1.3
--- libmysequoia/test/TestCarobMySQL.cpp:1.2    Mon Dec 12 08:58:57 2005
+++ libmysequoia/test/TestCarobMySQL.cpp        Tue Dec 13 14:09:44 2005
@@ -25,12 +25,12 @@
 
 void TestCarobMySQL::setUp(void)
 {
-  carobMySQL = new CarobMYSQL(NULL);
+//  carobMySQL = new CarobMYSQL(0, 0);
 }
 
 void TestCarobMySQL::tearDown(void) 
 {
-  delete carobMySQL;
+//  delete carobMySQL;
 }
 
 void TestCarobMySQL::firstTest(void)
Index: libmysequoia/test/TestMySQLAPI.cpp
diff -u libmysequoia/test/TestMySQLAPI.cpp:1.11 
libmysequoia/test/TestMySQLAPI.cpp:1.12
--- libmysequoia/test/TestMySQLAPI.cpp:1.11     Mon Dec 12 18:19:22 2005
+++ libmysequoia/test/TestMySQLAPI.cpp  Tue Dec 13 14:09:44 2005
@@ -42,11 +42,16 @@
 
 void TestMySQLAPI::mysql_init_test(void)
 {
+  MYSQL m;
+  
   CPPUNIT_ASSERT(mysql != 0);
   CPPUNIT_ASSERT(mysql->thd != 0); // the CarobMYSQL object
+  
+  CPPUNIT_ASSERT(mysql_init(&m) != 0);
+  CPPUNIT_ASSERT(m.thd != 0); // the CarobMYSQL object
+  mysql_close(&m);
 }
 
-
 void TestMySQLAPI::mysql_real_connect_test(void)
 {
   // connect specifying every parameter
@@ -99,6 +104,13 @@
 
 void TestMySQLAPI::mysql_connect_test(void)
 {
+  MYSQL m;
+
+  // connect specifying every parameter
+  // without calling mysql_init() first
+  CPPUNIT_ASSERT(mysql_connect(&m, HOST, USER1, PASSWD1) != 0);  
+  mysql_close(&m);
+  
   // connect specifying every parameter
   CPPUNIT_ASSERT(mysql_connect(mysql, HOST, USER1, PASSWD1) != 0);  
 

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

Reply via email to