Date: Wednesday, February 22, 2006 @ 15:23:57
  Author: zsolt
    Path: /cvsroot/carob/libmysequoia

Modified: include/CarobMySQL.hpp (1.25 -> 1.26) include/CarobStmt.hpp (1.8
          -> 1.9) src/CarobMySQL.cpp (1.52 -> 1.53) src/CarobStmt.cpp
          (1.24 -> 1.25) src/MySQLAPI.cpp (1.45 -> 1.46)

- restructurated, that prepared statements can be created only within 
CarobMYSQL.
- CarobMYSQL holds all prepared statements created by himself
- when a prepared statement is destroyed, it is removed from CarobMYSQL
- when CarobMYSQL is destroyed the reference is removed from the prepared 
statements
- debug level set to off in carob


------------------------+
 include/CarobMySQL.hpp |   34 +++++++++++++++++++++++++++----
 include/CarobStmt.hpp  |   19 ++++++++++++-----
 src/CarobMySQL.cpp     |   51 +++++++++++++++++++++++++++++++++++++++++++----
 src/CarobStmt.cpp      |   38 +++++++++++++++++++++++++++--------
 src/MySQLAPI.cpp       |    2 -
 5 files changed, 122 insertions(+), 22 deletions(-)


Index: libmysequoia/include/CarobMySQL.hpp
diff -u libmysequoia/include/CarobMySQL.hpp:1.25 
libmysequoia/include/CarobMySQL.hpp:1.26
--- libmysequoia/include/CarobMySQL.hpp:1.25    Wed Feb  1 10:25:56 2006
+++ libmysequoia/include/CarobMySQL.hpp Wed Feb 22 15:23:57 2006
@@ -38,10 +38,14 @@
 #include <string>
 #include <vector>
 
+#include <log4cxx/helpers/criticalsection.h>
+
 const unsigned long MYSEQUOIA_MAGIC = 0xC00CA10B;
 
 #define LOGGER_CONFIG_FILE CONFIGFILE_DIR"/logger.cfg"
 
+class CarobStmt;
+
 class CarobMYSQL : public CarobCommon
 {
 friend class CarobStmt;
@@ -199,21 +203,33 @@
    */
   void push_init_command(const char *cmd);
   
+  /**
+   * Creates a prepared statement
+   */
+  CarobStmt *createPreparedStatment();
+  
        /**
         * Returns a pointer to the MYSQL structure
    */
   MYSQL *getMYSQL ();
-  
-protected:
-  MYSQL * mysqlPtr;
-  CarobNS::Connection * connectionPtr;
 
 private:
+  //pointer to a MYSQL structure which is connected to this class
+  MYSQL * mysqlPtr;
+  //pointer to a Connection pointer which is not null, if the connection is 
established
+  CarobNS::Connection * connectionPtr;
+  //pointer holding a carob statement, used to execute the commands
   CarobNS::Statement * stmtPtr;
+  //pointer to a DriverResultSet containing the result of the executed select 
like statement
   CarobNS::DriverResultSet * drsPtr;
   //holds the result set for 'live' type of results
   MYSQL_RES *liveResultPtr;
+  //list holding the command that need to be sent when a connection is 
established
   std::vector<std::string> init_cmd_list;
+  //list holding all the prepared statements that this connection created
+  std::vector<CarobStmt *> carobStmts;
+  //critical section for accessing the carobStmts list
+  log4cxx::helpers::CriticalSection cs_carobStmts;
 
   /**
    * Private default constructor.
@@ -283,6 +299,16 @@
    * Read the MySQL ini file.
    */
    void read_ini_file();
+
+  /**
+   * Removes a prepared statement from the list
+   */
+  void removePreparedStatment(CarobStmt *stmt);
+  
+  /**
+   * Remove this class from the prepared statements
+   */
+  void resetPreparedStatments();
 };
 
 #endif /* _CAROBMYSQL_HPP */
Index: libmysequoia/include/CarobStmt.hpp
diff -u libmysequoia/include/CarobStmt.hpp:1.8 
libmysequoia/include/CarobStmt.hpp:1.9
--- libmysequoia/include/CarobStmt.hpp:1.8      Tue Jan 31 16:31:52 2006
+++ libmysequoia/include/CarobStmt.hpp  Wed Feb 22 15:23:57 2006
@@ -33,13 +33,10 @@
 
 class CarobStmt : public CarobCommon
 {
+friend class CarobMYSQL;
+
 public:
   /**
-   * Initialize and constructs the class. Allocate a MYSQL_STMT and initialize 
a Carob::ParameterStatement.
-   * @param mysql object (must be initialized with mysql_init)
-   */
-  CarobStmt(MYSQL *mysql);
-  /**
    * Free the allocated statement and resources used by it. Also delete Carob 
statement
    */
        ~CarobStmt();
@@ -164,6 +161,18 @@
    */
   MYSQL_STMT *getMYSQL_STMT() {return m_stmt;}
 
+protected:
+  /**
+   * Initialize and constructs the class. Allocate a MYSQL_STMT and initialize 
a Carob::ParameterStatement.
+   * @param mysql object (must be initialized with mysql_init)
+   */
+  CarobStmt(CarobMYSQL *c);
+
+  /**
+   * set CarobMYSQL pointer to null
+   */
+   void resetParent() {cmysql = 0; m_stmt->mysql = 0; liveResultSet = 0;}
+   
 private:
   //Internal MYSQL_STMT associated with the statement
   MYSQL_STMT *m_stmt;
Index: libmysequoia/src/CarobMySQL.cpp
diff -u libmysequoia/src/CarobMySQL.cpp:1.52 
libmysequoia/src/CarobMySQL.cpp:1.53
--- libmysequoia/src/CarobMySQL.cpp:1.52        Tue Feb 21 14:35:38 2006
+++ libmysequoia/src/CarobMySQL.cpp     Wed Feb 22 15:23:57 2006
@@ -20,9 +20,8 @@
  */
 
 #include <CarobMySQL.hpp>
-#include <ResultSetMetaData.hpp>
+#include <CarobStmt.hpp>
 #include <IniParser.hpp>
-
 #include <Utils.hpp>
 
 /* MySQL include */
@@ -30,9 +29,11 @@
 
 /* Carob include */
 #include <StringCodecs.hpp>
+#include <ResultSetMetaData.hpp>
 
 #include <cstdlib>
 #include <string>
+#include <algorithm>
 
 using namespace CarobNS;
 using namespace std;
@@ -66,6 +67,8 @@
 {
   LOG4CXX_DEBUG(logger, "Entering destructor.");
   
+  resetPreparedStatments();
+
   delete_connection();
 
   LOG4CXX_INFO(logger, "Closing: host=" << mysqlPtr->host << " user=" << 
mysqlPtr->user << " passwd=" << mysqlPtr->passwd << 
@@ -188,7 +191,7 @@
   try 
   {
     ConnectionParameters connectionParameters(fromString(host), port, 
-      fromString(db), fromString(user), fromString(passwd), DEBUG_LEVEL_DEBUG);
+      fromString(db), fromString(user), fromString(passwd), DEBUG_LEVEL_OFF);
 
     Connection *newConnectionPtr;
     
@@ -202,7 +205,7 @@
       if (defaultport && (port == 25322))
       {
         ConnectionParameters connectionParameters2(fromString(host), 3306,
-          fromString(db), fromString(user), fromString(passwd), 
DEBUG_LEVEL_DEBUG);
+          fromString(db), fromString(user), fromString(passwd), 
DEBUG_LEVEL_OFF);
         newConnectionPtr = new Connection(connectionParameters2);
       }
       else
@@ -989,3 +992,43 @@
 
   LOG4CXX_DEBUG(logger, "Leaving read_ini_file.");
 }
+
+CarobStmt *
+CarobMYSQL::createPreparedStatment()
+{
+  LOG4CXX_DEBUG(logger, "Entering createPreparedStatment");
+  CarobStmt *result = new CarobStmt(this);
+
+  if (result)
+  {
+    cs_carobStmts.lock();
+    carobStmts.push_back(result);
+    cs_carobStmts.unlock();
+  }
+
+  LOG4CXX_DEBUG(logger, "Leaving createPreparedStatment result=" << result);
+  return result;
+}
+  
+void
+CarobMYSQL::removePreparedStatment(CarobStmt *stmt)
+{
+  LOG4CXX_DEBUG(logger, "Entering deletePreparedStatment");
+  cs_carobStmts.lock();
+  vector<CarobStmt *>::iterator stmtIter = find(carobStmts.begin(), 
carobStmts.end(), stmt);
+  if (stmtIter != carobStmts.end())
+    carobStmts.erase(stmtIter);
+  cs_carobStmts.unlock();
+  LOG4CXX_DEBUG(logger, "Leaving deletePreparedStatment");
+}
+
+void
+CarobMYSQL::resetPreparedStatments()
+{
+  LOG4CXX_DEBUG(logger, "Entering resetPreparedStatment");
+  cs_carobStmts.lock();
+  for (vector<CarobStmt *>::iterator stmtIter = carobStmts.begin(); stmtIter 
!= carobStmts.end(); stmtIter++)
+    (*stmtIter)->resetParent();
+  cs_carobStmts.unlock();
+  LOG4CXX_DEBUG(logger, "Leaving resetPreparedStatment");
+}
Index: libmysequoia/src/CarobStmt.cpp
diff -u libmysequoia/src/CarobStmt.cpp:1.24 libmysequoia/src/CarobStmt.cpp:1.25
--- libmysequoia/src/CarobStmt.cpp:1.24 Tue Feb 21 14:09:22 2006
+++ libmysequoia/src/CarobStmt.cpp      Wed Feb 22 15:23:57 2006
@@ -40,9 +40,9 @@
 #define DATA_NORMAL 1
 #define DATA_TRUNCATED 2
 
-CarobStmt::CarobStmt(MYSQL *mysql): c_stmt(0), liveResultSet(0)
+CarobStmt::CarobStmt(CarobMYSQL *c): c_stmt(0), liveResultSet(0)
 {
-  LOG4CXX_DEBUG(logger, "Entering constructor: mysql=" << mysql);
+  LOG4CXX_DEBUG(logger, "Entering constructor: mysql=" << c);
 
   m_stmt = new MYSQL_STMT();
   memset(m_stmt, 0, sizeof(MYSQL_STMT));
@@ -50,13 +50,13 @@
   //Initialize the structure
   m_stmt->mem_root.used = (USED_MEM *) this;
   m_stmt->mem_root.block_size = MYSEQUOIA_MAGIC;
-  m_stmt->mysql = mysql;
+  m_stmt->mysql = c->getMYSQL();
 #ifdef MYSQL5
   m_stmt->prefetch_rows = 1;
 #endif
   m_stmt->state = MYSQL_STMT_INIT_DONE;
   
-  cmysql = (CarobMYSQL *)mysql->field_alloc.used;
+  cmysql = c;
   
   LOG4CXX_DEBUG(logger, "Leaving constructor.");
 }
@@ -65,6 +65,9 @@
 {
   LOG4CXX_DEBUG(logger, "Entering destructor.");
 
+  if (cmysql)
+    cmysql->removePreparedStatment(this);
+    
   clear();
   
   FREE_AND_NULL(m_stmt);
@@ -77,6 +80,13 @@
 {
   LOG4CXX_DEBUG(logger, "Entering prepare: query=" << query << " length=" << 
length);
 
+  if (!cmysql)
+  {
+    set_error(CR_SERVER_LOST, SQLT_UNKNOWN);
+    LOG4CXX_DEBUG(logger, "Leaving prepare: result=1");
+    return 1;
+  }
+  
   if (!cmysql->connectionPtr)
   {
     set_error(CR_COMMANDS_OUT_OF_SYNC, SQLT_UNKNOWN);
@@ -104,8 +114,9 @@
         get_query_fields(m_stmt->fields, rsmdPtr);
         m_stmt->bind = new MYSQL_BIND[m_stmt->field_count];
       }
-    } catch (BackendException)
+    } catch (BackendException &e)
     {
+      LOG4CXX_DEBUG(logger, "Prepare: errno=" << e.getErrorCode() << " error=" 
<< toString(e.description()));
     }
   }
   catch (CarobException &e)
@@ -248,7 +259,9 @@
   
   int result = 1;
   
-  if (m_stmt->state < MYSQL_STMT_PREPARE_DONE)
+  if (!cmysql)
+    set_error(CR_SERVER_LOST, SQLT_UNKNOWN);
+  else if (m_stmt->state < MYSQL_STMT_PREPARE_DONE)
     set_error(CR_NO_PREPARE_STMT, SQLT_UNKNOWN);
   else
     if (m_stmt->param_count && !m_stmt->params)
@@ -436,7 +449,12 @@
   bool truncation = false;
 #endif
   
-  if (!m_stmt->bind_result_done || (m_stmt->state < MYSQL_STMT_EXECUTE_DONE))
+  if (!cmysql && !m_stmt->result.data)
+  {
+    set_error(CR_SERVER_LOST, SQLT_UNKNOWN);
+    result = 1;
+  }
+  else if (!m_stmt->bind_result_done || (m_stmt->state < 
MYSQL_STMT_EXECUTE_DONE))
   {
     set_error(CR_COMMANDS_OUT_OF_SYNC, SQLT_UNKNOWN);
     result = 1;
@@ -519,7 +537,6 @@
     free_long_data(m_stmt);
     if (c_stmt)
     {
-      //TODO maybe implement a reset in Carob?? It can use cleanUpResults, but 
it's private...
       c_stmt->close();
       liveResultSet = 0;
     }
@@ -560,6 +577,11 @@
 
   int result = 0;
   
+  if (!cmysql)
+  {
+    set_error(CR_SERVER_LOST, SQLT_UNKNOWN);
+    result = 1;
+  }
   if (m_stmt->state < MYSQL_STMT_EXECUTE_DONE)
   {
     set_error(CR_COMMANDS_OUT_OF_SYNC, SQLT_UNKNOWN);
Index: libmysequoia/src/MySQLAPI.cpp
diff -u libmysequoia/src/MySQLAPI.cpp:1.45 libmysequoia/src/MySQLAPI.cpp:1.46
--- libmysequoia/src/MySQLAPI.cpp:1.45  Tue Feb 21 11:05:53 2006
+++ libmysequoia/src/MySQLAPI.cpp       Wed Feb 22 15:23:57 2006
@@ -1210,7 +1210,7 @@
   
   if (isCarobObject(mysql))
   {
-    CarobStmt *stmt = new CarobStmt(mysql);
+    CarobStmt *stmt = getCarob(mysql)->createPreparedStatment();
     result = stmt->getMYSQL_STMT();
   }
   else

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

Reply via email to