Date: Thursday, January 12, 2006 @ 17:33:46
  Author: gilles
    Path: /cvsroot/carob/carob

Modified: contrib/CPP/read_example.cpp (1.3 -> 1.4) include/Connection.hpp
          (1.47 -> 1.48) include/ParameterStatement.hpp (1.6 -> 1.7)
          include/Statement.hpp (1.24 -> 1.25) src/Connection.cpp (1.54 ->
          1.55) src/Statement.cpp (1.23 -> 1.24)
          test/30-ResultSet/TestSimpleUnicode.cpp (1.3 -> 1.4)
          test/40-Parameter-PreparedStatement/TestParameterStatement.cpp
          (1.1 -> 1.2)
          test/40-Parameter-PreparedStatement/TestPreparedStatement.cpp
          (1.1 -> 1.2) test/ConnectionSetup.cpp (1.4 -> 1.5)
          test/TestDriverResultSet.cpp (1.3 -> 1.4) test/TestStatement.cpp
          (1.19 -> 1.20)

Removal of [Parameter]Statement / connection friendship
-> destructor of [Parameter]Statement transfered as protected in order to force 
user to use connection's 'deleteStatement' (that replaces 
'removeStatementFromList')
-> PreparedStatementGetMetaData function transfered as public
-> removed all 'delete statement' from tests and contribs


----------------------------------------------------------------+
 contrib/CPP/read_example.cpp                                   |    8 -
 include/Connection.hpp                                         |   52 
+++++-----
 include/ParameterStatement.hpp                                 |   14 ++
 include/Statement.hpp                                          |   17 ++-
 src/Connection.cpp                                             |   10 +
 src/Statement.cpp                                              |    1 
 test/30-ResultSet/TestSimpleUnicode.cpp                        |    1 
 test/40-Parameter-PreparedStatement/TestParameterStatement.cpp |    6 -
 test/40-Parameter-PreparedStatement/TestPreparedStatement.cpp  |    8 -
 test/ConnectionSetup.cpp                                       |    4 
 test/TestDriverResultSet.cpp                                   |    2 
 test/TestStatement.cpp                                         |   13 --
 12 files changed, 68 insertions(+), 68 deletions(-)


Index: carob/contrib/CPP/read_example.cpp
diff -u carob/contrib/CPP/read_example.cpp:1.3 
carob/contrib/CPP/read_example.cpp:1.4
--- carob/contrib/CPP/read_example.cpp:1.3      Wed Dec 28 18:14:57 2005
+++ carob/contrib/CPP/read_example.cpp  Thu Jan 12 17:33:46 2006
@@ -1,6 +1,6 @@
 /*
  * Sequoia: Database clustering technology.
- * Copyright (C) 2005 Emic Networks
+ * Copyright (C) 2005-2006 Continuent, Inc.
  * Contact: [EMAIL PROTECTED]
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -75,8 +75,10 @@
         wcout<<drsPtr->getAsString(j+1)<<L"\t";
       wcout<<endl;
     }
-    // 4- Clean-up: Don't forget this step !
-    delete statementPtr;
+    // 4- Clean-up: the statement will be automatically deleted by the
+    // connection that created it. If you want to free the memory anyway, use
+    // the function from connection:
+    connection.deleteStatement(statementPtr);
   }
   // 5- Deal with errors
   catch (CarobException ce)
Index: carob/include/Connection.hpp
diff -u carob/include/Connection.hpp:1.47 carob/include/Connection.hpp:1.48
--- carob/include/Connection.hpp:1.47   Wed Jan 11 20:14:27 2006
+++ carob/include/Connection.hpp        Thu Jan 12 17:33:46 2006
@@ -106,11 +106,10 @@
  */
 class Connection
 {
-/** DriverResultSet must access getDriverSocketPtr() function */
+// For now, DriverResultSet must access getDriverSocketPtr() function
+// TODO: change this to remove the friendship
 friend class DriverResultSet;
-/** Statements must inform connection about their destruction */
-friend class Statement;
-friend class ParameterStatement;
+
 public:
   /**
    * Creates a new connection and connects to a controller with the given
@@ -202,6 +201,15 @@
   ParameterStatement* createParameterStatement(const std::wstring &query) 
                         throw (SocketIOException, UnexpectedException);
   /**
+   * Deletes given statement.
+   * Only way to delete a statement (private dtor) so connection will keep the
+   * control of its statement and can remove it from its statement list.
+   * @param stPtr pointer to the statement to destroy
+   * @throw DriverException if the given statement is not in our list
+   */
+  void                deleteStatement(Statement* stPtr) throw (DriverException,
+                          UnexpectedException);
+  /**
    * Performs a read request and returns the reply.
    * 
    * @param request the read request with result set parameters to execute
@@ -282,7 +290,14 @@
                         throw (ControllerException, SocketIOException,
                         ProtocolException, BackendException,
                         UnexpectedException);
-
+  /**
+   * Returns the metadata of the sql template as a resultset.
+   * @param sqlTemplate sql template of the PreparedStatement
+   */
+  ResultSetMetaData*  preparedStatementGetMetaData(const std::wstring 
&sqlTemplate)
+                          throw (SocketIOException, BackendException, 
ProtocolException, 
+                            NotImplementedException, UnexpectedException);
+    
 protected:
   /**
    * Starts a connection by sending all connection/authentication informations.
@@ -344,15 +359,7 @@
    * @return reference to this connection socket
    */
   const DriverSocket& getDriverSocket() { return *driverSocketPtr; }
-
-  /**
-   * Returns the metadata of the sql template as a resultset.
-   * @param sqlTemplate sql template of the PreparedStatement
-   */
-  ResultSetMetaData*  preparedStatementGetMetaData(const std::wstring 
&sqlTemplate)
-                          throw (SocketIOException, BackendException, 
ProtocolException, 
-                            NotImplementedException, UnexpectedException);
-                            
+                        
 private:
   /** This connection socket pointer */
   DriverSocket*       driverSocketPtr;
@@ -391,17 +398,6 @@
    */
   std::list<Statement*> created_statements;
   /**
-   * Removes given statement from created statement list to avoid double 
deletes
-   */
-  void                removeStatementFromList(Statement* stPtr);
-  /**
-   * Forbids Connection creation that would lead to unexpected state.
-   * A connection is either created, ie. connected to a controller or destroyed
-   * @see #Connection(const ConnectionParameters&)
-   */
-  Connection();
-
-  /**
    * Begins a new transaction if needed (<code>mustBeginTransaction</code> is
    * set to <code>true</code>).
    */
@@ -527,6 +523,12 @@
                           throw (SocketIOException, BackendException,
                           ControllerException, ProtocolException,
                           UnexpectedException);
+  /**
+   * Forbids Connection creation that would lead to unexpected state.
+   * A connection is either created, ie. connected to a controller or destroyed
+   * @see #Connection(const ConnectionParameters&)
+   */
+  Connection();
 };
 } //namespace CarobNS
 #endif //_CONNECTION_H_
Index: carob/include/ParameterStatement.hpp
diff -u carob/include/ParameterStatement.hpp:1.6 
carob/include/ParameterStatement.hpp:1.7
--- carob/include/ParameterStatement.hpp:1.6    Wed Jan 11 20:14:27 2006
+++ carob/include/ParameterStatement.hpp        Thu Jan 12 17:33:46 2006
@@ -92,8 +92,8 @@
 friend class Connection;
 
 public:
-  //No public constructor, statements must be created by connections
-       virtual ~ParameterStatement();
+  //No constructor nor destructor, statements must be created and destroyed by
+  //connections
 
   /**
    * Release objects and call Statement.close().
@@ -279,6 +279,16 @@
                                   const T& value) 
                               throw (DriverException, UnexpectedException);
 
+  /**
+   * Forbid construction to the rest of the world.
+   * ParameterStatements must be created by connection.
+   */
+  ParameterStatement();
+  /**
+   * Forbid destruction to the rest of the world.
+   * ParameterStatements must be deleted by connection.
+   */
+  virtual ~ParameterStatement();
 private:
   /** IN parameters, ready to be sent with the request */
   std::vector<std::wstring>   inStrings;
Index: carob/include/Statement.hpp
diff -u carob/include/Statement.hpp:1.24 carob/include/Statement.hpp:1.25
--- carob/include/Statement.hpp:1.24    Wed Jan 11 20:14:27 2006
+++ carob/include/Statement.hpp Thu Jan 12 17:33:46 2006
@@ -78,8 +78,8 @@
 friend class DriverResultSet;
 
 public:
-  //No constructor, statements must be created by connections
-       virtual ~Statement();
+  //No constructor nor destructor, statements must be created and destroyed by
+  //connections
   /**
    * Execute a SQL statement that may return multiple results.
    * @param sql any SQL statement
@@ -357,6 +357,17 @@
    * Returns the pointer to the connection that created us.
    */
   Connection*             getConnectionPtr() {return connectionPtr;}
+  /**
+   * Forbid construction to the rest of the world.
+   * Statements must be created by connection.
+   */
+  Statement();
+  /**
+   * Forbid destruction to the rest of the world.
+   * Statements must be deleted by connection.
+   * @see Connection::deleteStatement(Statement* stPtr)
+   */
+  virtual ~Statement();
 private:
   /** Connection that created us */
   Connection*             connectionPtr;
@@ -398,8 +409,6 @@
    * clears the list
    */
   void                    cleanUpResults();
-  //Forbid default constructor: statements must be created by connections
-  Statement();
 };
 
 } //namespace CarobNS
Index: carob/src/Connection.cpp
diff -u carob/src/Connection.cpp:1.54 carob/src/Connection.cpp:1.55
--- carob/src/Connection.cpp:1.54       Wed Jan 11 20:14:27 2006
+++ carob/src/Connection.cpp    Thu Jan 12 17:33:46 2006
@@ -243,7 +243,8 @@
   }
   return true;
 }
-void Connection::removeStatementFromList(Statement* stPtr)
+
+void Connection::deleteStatement(Statement* stPtr) throw (DriverException, 
UnexpectedException)
 {
   for (std::list<Statement*>::iterator iter = created_statements.begin();
       iter != created_statements.end(); iter++)
@@ -251,10 +252,15 @@
     if (*iter == stPtr)
     {
       created_statements.erase(iter);
-      break;
+      delete stPtr;
+      return;
     }
   }
+  // We shouldn't come here. It means we have been asked to delete a statement
+  // we don't own
+  throw DriverException(L"deleteStatement: the given statement is not owned by 
this connection!");
 }
+
 bool Connection::close()
 {
   wstring fctName(L"Connection::closeConnection");
Index: carob/src/Statement.cpp
diff -u carob/src/Statement.cpp:1.23 carob/src/Statement.cpp:1.24
--- carob/src/Statement.cpp:1.23        Wed Jan 11 20:14:27 2006
+++ carob/src/Statement.cpp     Thu Jan 12 17:33:46 2006
@@ -48,7 +48,6 @@
 Statement::~Statement()
 {
   cleanUpResults();
-  connectionPtr->removeStatementFromList(this);
 }
 
 void Statement::cleanUpResults()
Index: carob/test/30-ResultSet/TestSimpleUnicode.cpp
diff -u carob/test/30-ResultSet/TestSimpleUnicode.cpp:1.3 
carob/test/30-ResultSet/TestSimpleUnicode.cpp:1.4
--- carob/test/30-ResultSet/TestSimpleUnicode.cpp:1.3   Mon Dec 19 16:59:32 2005
+++ carob/test/30-ResultSet/TestSimpleUnicode.cpp       Thu Jan 12 17:33:46 2006
@@ -80,7 +80,6 @@
 
     CPPUNIT_ASSERT(!drsPtr->next());
 
-    delete stmt; // take cares of drsPtr
 }
 
 // List of tests in this class
Index: carob/test/40-Parameter-PreparedStatement/TestParameterStatement.cpp
diff -u 
carob/test/40-Parameter-PreparedStatement/TestParameterStatement.cpp:1.1 
carob/test/40-Parameter-PreparedStatement/TestParameterStatement.cpp:1.2
--- carob/test/40-Parameter-PreparedStatement/TestParameterStatement.cpp:1.1    
Thu Dec 22 17:02:51 2005
+++ carob/test/40-Parameter-PreparedStatement/TestParameterStatement.cpp        
Thu Jan 12 17:33:46 2006
@@ -58,7 +58,6 @@
           + be.description());
     }
   }
-  delete statementPtr;
 }
 
 void TestParameterStatement::testExecuteQueryGood()
@@ -90,7 +89,6 @@
   if (isDebugEnabled())
     logDebug(fctName, buffer.str());
 
-  delete statementPtr;
 }
 
 //EXECUTE UPDATE
@@ -119,7 +117,6 @@
           + be.description());
     }
   }
-  delete statementPtr;
 }
 
 void TestParameterStatement::testExecuteUpdateGood()
@@ -140,7 +137,6 @@
         + toWString(nbRowsAffected));
   }
   CPPUNIT_ASSERT(nbRowsAffected == 1);
-  delete statementPtr;
 }
 
 void TestParameterStatement::testExecuteWithSelect()
@@ -175,7 +171,6 @@
   {
     logDebug(fctName, buffer.str());
   }
-  delete statementPtr;
 }
 
 void TestParameterStatement::testExecuteWithUpdate()
@@ -207,7 +202,6 @@
         + toWString(statementPtr->getUpdateCount()));
   }
   CPPUNIT_ASSERT(statementPtr->getUpdateCount() == 1);
-  delete statementPtr;
 }
 
 CppUnit::Test* TestParameterStatement::suite()
Index: carob/test/40-Parameter-PreparedStatement/TestPreparedStatement.cpp
diff -u carob/test/40-Parameter-PreparedStatement/TestPreparedStatement.cpp:1.1 
carob/test/40-Parameter-PreparedStatement/TestPreparedStatement.cpp:1.2
--- carob/test/40-Parameter-PreparedStatement/TestPreparedStatement.cpp:1.1     
Wed Jan 11 20:16:19 2006
+++ carob/test/40-Parameter-PreparedStatement/TestPreparedStatement.cpp Thu Jan 
12 17:33:46 2006
@@ -52,7 +52,6 @@
           + be.description());
     }
   }
-  delete statementPtr;
 }
 void TestPreparedStatement::testPrepareEmptyParameterStatement()
 {
@@ -76,7 +75,6 @@
           + be.description());
     }
   }
-  delete psPtr;
 }
 
 void TestPreparedStatement::testPrepareUpdateStatement()
@@ -103,7 +101,6 @@
           + be.description());
     }
   }
-  delete statementPtr;
 }
 void TestPreparedStatement::testPrepareUpdateParameterStatement()
 {
@@ -117,7 +114,6 @@
   }
   ResultSetMetaData* rsmdPtr = psPtr->getMetaData();
   CPPUNIT_ASSERT(rsmdPtr == NULL);
-  delete psPtr;
 }
 
 void TestPreparedStatement::testPrepareExecuteQueryStatement()
@@ -132,7 +128,6 @@
   ResultSetMetaData* rsmdPtr = statementPtr->getMetaData();
   CPPUNIT_ASSERT(rsmdPtr != NULL); //just to avoid warning on unsused var
   CPPUNIT_ASSERT(rsmdPtr->getColumnCount() == 5);
-  delete statementPtr;
 }
 
 void TestPreparedStatement::testPrepareExecuteQueryParameterStatement()
@@ -148,7 +143,6 @@
   ResultSetMetaData* rsmdPtr = psPtr->getMetaData();
   CPPUNIT_ASSERT(rsmdPtr != NULL);
   CPPUNIT_ASSERT(rsmdPtr->getColumnName(1) == L"ID");
-  delete psPtr;
 }
 
 void TestPreparedStatement::testPrepareExecuteStatement()
@@ -163,7 +157,6 @@
   ResultSetMetaData* rsmdPtr = statementPtr->getMetaData();
   CPPUNIT_ASSERT(rsmdPtr != NULL); //just to avoid warning on unsused var
   CPPUNIT_ASSERT(rsmdPtr->getTableName(2) == L"ADDRESS");
-  delete statementPtr;
 }
 
 void TestPreparedStatement::testPrepareExecuteParameterStatement()
@@ -179,7 +172,6 @@
   ResultSetMetaData* rsmdPtr = psPtr->getMetaData();
   CPPUNIT_ASSERT(rsmdPtr != NULL);
   CPPUNIT_ASSERT(rsmdPtr->getColumnType(3) == SQLT_VARCHAR);
-  delete psPtr;
 }
 
 CppUnit::Test* TestPreparedStatement::suite()
Index: carob/test/ConnectionSetup.cpp
diff -u carob/test/ConnectionSetup.cpp:1.4 carob/test/ConnectionSetup.cpp:1.5
--- carob/test/ConnectionSetup.cpp:1.4  Wed Dec 28 18:14:57 2005
+++ carob/test/ConnectionSetup.cpp      Thu Jan 12 17:33:46 2006
@@ -70,7 +70,7 @@
         // else ignore
     }
 
-    delete dropstmt;
+    conn_p->deleteStatement(dropstmt);
 }
 
 void createOrReplaceTable(Connection *conn_p, const std::wstring& tableName, 
const std::wstring& tableType)
@@ -86,7 +86,7 @@
     int updatecount = createstmt->executeUpdate(createcommand);
     CPPUNIT_ASSERT(0 == updatecount);
 
-    delete createstmt;
+    conn_p->deleteStatement(createstmt);
 }
 
 
Index: carob/test/TestDriverResultSet.cpp
diff -u carob/test/TestDriverResultSet.cpp:1.3 
carob/test/TestDriverResultSet.cpp:1.4
--- carob/test/TestDriverResultSet.cpp:1.3      Thu Dec 15 17:43:53 2005
+++ carob/test/TestDriverResultSet.cpp  Thu Jan 12 17:33:46 2006
@@ -79,7 +79,6 @@
           + nve.description());
     }
   }
-  delete statementPtr;
 }
 
 void TestDriverResultSet::testGetAsIntOnString()
@@ -133,7 +132,6 @@
           + de.description());
     }
   }
-  delete statementPtr;
 }
 
 CppUnit::Test* TestDriverResultSet::suite()
Index: carob/test/TestStatement.cpp
diff -u carob/test/TestStatement.cpp:1.19 carob/test/TestStatement.cpp:1.20
--- carob/test/TestStatement.cpp:1.19   Mon Dec 19 09:49:04 2005
+++ carob/test/TestStatement.cpp        Thu Jan 12 17:33:46 2006
@@ -59,7 +59,6 @@
           + ce.description());
     }
   }
-  delete statementPtr;
 }
 
 void TestStatement::testExecuteQueryBadTable()
@@ -85,7 +84,6 @@
           + be.description());
     }
   }
-  delete statementPtr;
 }
 
 void TestStatement::testExecuteQueryGood()
@@ -118,7 +116,6 @@
   if (isDebugEnabled())
     logDebug(fctName, buffer.str());
 
-  delete statementPtr;
 }
 
 
@@ -147,7 +144,6 @@
           + ce.description());
     }
   }
-  delete statementPtr;
 }
 
 void TestStatement::testExecuteUpdateBadTable()
@@ -173,7 +169,6 @@
           + be.description());
     }
   }
-  delete statementPtr;
 }
 
 void TestStatement::testExecuteUpdateGood()
@@ -193,7 +188,6 @@
         + toWString(nbRowsAffected));
   }
   CPPUNIT_ASSERT(nbRowsAffected == 1);
-  delete statementPtr;
 }
 
 void TestStatement::testExecuteQueryWithMaxRows()
@@ -225,8 +219,6 @@
   }
   //This next() should return false because we asked just 1 row
   CPPUNIT_ASSERT(drsPtr->next() == false);
-  delete drsPtr;
-  delete statementPtr;
 }
 
 void TestStatement::testExecuteBadRequests()
@@ -270,7 +262,6 @@
           + be.description());
     }
   }
-  delete statementPtr;
 }
 
 void TestStatement::testExecuteWithSelect()
@@ -305,7 +296,6 @@
   {
     logDebug(fctName, buffer.str());
   }
-  delete statementPtr;
 }
 void TestStatement::testExecuteWithUpdate()
 {
@@ -324,8 +314,8 @@
         + toWString(statementPtr->getUpdateCount()));
   }
   CPPUNIT_ASSERT(statementPtr->getUpdateCount() == 1);
-  delete statementPtr;
 }
+
 void TestStatement::testExecuteWithSelectStreamed()
 {
   wstring fctName(L"TestStatement::testExecuteWithSelectStreamed");
@@ -358,7 +348,6 @@
   {
     logDebug(fctName, buffer.str());
   }
-  delete statementPtr;
 }
 
 CppUnit::Test* TestStatement::suite()

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

Reply via email to