Date: Thursday, December 15, 2005 @ 15:55:57
  Author: gilles
    Path: /cvsroot/carob/carob

Modified: contrib/CPP/read_example.cpp (1.1 -> 1.2)
          contrib/PHP/sample_carob_request.cpp (1.7 -> 1.8)
          include/DriverResultSet.hpp (1.23 -> 1.24)
          src/DriverResultSet.cpp (1.24 -> 1.25)
          test/TestDriverResultSet.cpp (1.1 -> 1.2)
          test/TestDriverResultSet.hpp (1.1 -> 1.2)
          test/TestExecReadRequest.cpp (1.15 -> 1.16)
          test/TestStatement.cpp (1.16 -> 1.17)

Modified getInt32 and getString functions to return the value *only* if the 
column is of given type (resp. int or string)
Introduced getAsInt and getAsString functions that try to do conversion if the 
value is not of given type, as did getInt32 and getString before...


--------------------------------------+
 contrib/CPP/read_example.cpp         |    2 -
 contrib/PHP/sample_carob_request.cpp |    2 -
 include/DriverResultSet.hpp          |   42 +++++++++++++++++++++++---------
 src/DriverResultSet.cpp              |   43 ++++++++++++++++++++++++++++-----
 test/TestDriverResultSet.cpp         |   22 ++++++++--------
 test/TestDriverResultSet.hpp         |    4 +--
 test/TestExecReadRequest.cpp         |    2 -
 test/TestStatement.cpp               |    6 ++--
 8 files changed, 87 insertions(+), 36 deletions(-)


Index: carob/contrib/CPP/read_example.cpp
diff -u carob/contrib/CPP/read_example.cpp:1.1 
carob/contrib/CPP/read_example.cpp:1.2
--- carob/contrib/CPP/read_example.cpp:1.1      Tue Dec 13 14:57:34 2005
+++ carob/contrib/CPP/read_example.cpp  Thu Dec 15 15:55:57 2005
@@ -78,7 +78,7 @@
       drsPtr->next();
       // 3.4 Iterate through the columns      
       for (int j=0; j<rsmd.getColumnCount(); j++)
-        wcout<<drsPtr->getString(j+1)<<L"\t";
+        wcout<<drsPtr->getAsString(j+1)<<L"\t";
       wcout<<endl;
     }
     // 4- Clean-up: Don't forget this step !
Index: carob/contrib/PHP/sample_carob_request.cpp
diff -u carob/contrib/PHP/sample_carob_request.cpp:1.7 
carob/contrib/PHP/sample_carob_request.cpp:1.8
--- carob/contrib/PHP/sample_carob_request.cpp:1.7      Fri Dec  2 12:11:09 2005
+++ carob/contrib/PHP/sample_carob_request.cpp  Thu Dec 15 15:55:57 2005
@@ -157,7 +157,7 @@
     
 #define WTABBING L"<TD>"
 
-      wide_oss << L"<TR>" << WTABBING << i << WTABBING << drsPtr->getInt(1)
+      wide_oss << L"<TR>" << WTABBING << i << WTABBING << drsPtr->getInt32(1)
                           << WTABBING << drsPtr->getString(2)
                           << WTABBING << drsPtr->getString(3) << std::endl;
 
Index: carob/include/DriverResultSet.hpp
diff -u carob/include/DriverResultSet.hpp:1.23 
carob/include/DriverResultSet.hpp:1.24
--- carob/include/DriverResultSet.hpp:1.23      Thu Dec 15 12:29:06 2005
+++ carob/include/DriverResultSet.hpp   Thu Dec 15 15:55:57 2005
@@ -133,41 +133,61 @@
                                   BackendException, UnexpectedException);
 
   /**
-   * Get the value of a column in the current row as a wstring
+   * Gets the string value of a column in the current row only if the given
+   * column is of string type. Throws an exception otherwise. To get the value
+   * as a string anyway, use #getAsString(int)
    * 
    * @param columnIndex the first column is 1, the second is 2...
    * @return the column value, null for SQL NULL
-   * @exception DriverException if the ResultSet is closed or the cursor is
+   * @throw DriverException if the ResultSet is closed or the cursor is
    *            out of bounds
+   * @throw DriverException if the column is not of type string
    * @throw NullValueException if the retrieved value is NULL
    */
   std::wstring                getString(int columnIndex) throw 
(DriverException,
                                   NullValueException, NotImplementedException,
                                   UnexpectedException);
   /**
-   * Get the value of a column in the current row as a java int (32 bits)
+   * Tries to get the value of a column in the current row as a wstring. If the
+   * value at the given row/col is not of type string, tries to convert it 
using
+   * STL std::wostringstream conversion functions
+   * 
+   * @param columnIndex the first column is 1, the second is 2...
+   * @return the column value, null for SQL NULL
+   * @throw DriverException if the ResultSet is closed or the cursor is
+   *            out of bounds
+   * @throw NullValueException if the retrieved value is NULL
+   */
+  std::wstring                getAsString(int columnIndex) throw 
(DriverException,
+                                  NullValueException, NotImplementedException,
+                                  UnexpectedException);
+  /**
+   * Gets the 32bits integer value of a column in the current row only if the
+   * given column is of int type, throws an exception otherwise. To get the
+   * value as an int anyway, use #getAsInt(int)
    * 
    * @param columnIndex the first column is 1, the second is 2,...
    * @return the column value; NULL if SQL NULL
-   * @exception DriverException if the ResultSet is closed or the cursor is
-   *            out of bounds
+   * @throw DriverException if the ResultSet is closed or the cursor is out of
+   *        bounds
+   * @throw DriverException if the column is not of type string
    * @throw NullValueException if the retrieved value is NULL
    */
   int32_t                     getInt32(int columnIndex) throw (DriverException,
                                   NullValueException, NotImplementedException,
                                   UnexpectedException);
   /**
-   * Get the value of a column in the current row as a native integer (system
-   * dependant). <b>Note:</b> be very carefull ! a cast is done on the actual
-   * value, so if your system is less than 32bits, you may encounter data loss!
+   * Tries to get the value of a column in the current row as an int. If the
+   * value at the given row/col is not of type int, tries to convert it using
+   * wstringToA conversion function.
    * 
    * @param columnIndex the first column is 1, the second is 2,...
    * @return the column value; NULL if SQL NULL
-   * @exception DriverException if the ResultSet is closed or the cursor is
-   *            out of bounds
+   * @throw DriverException if the ResultSet is closed or the cursor is out of
+   *        bounds
    * @throw NullValueException if the retrieved value is NULL
    */
-  int                         getInt(int columnIndex) throw (DriverException,
+  int                         getAsInt(int columnIndex) throw (DriverException,
                                   NullValueException, NotImplementedException,
                                   UnexpectedException);
   /**
Index: carob/src/DriverResultSet.cpp
diff -u carob/src/DriverResultSet.cpp:1.24 carob/src/DriverResultSet.cpp:1.25
--- carob/src/DriverResultSet.cpp:1.24  Thu Dec 15 12:29:06 2005
+++ carob/src/DriverResultSet.cpp       Thu Dec 15 15:55:57 2005
@@ -206,6 +206,23 @@
     throw (NullValueException(L"getString: value at row " + 
toWString(currentRow)
         + L" column " + toWString(columnIndex) + L" is NULL"));
   }
+  if (columnTypeTags[columnIndex - 1] != TT_STRING)
+  {
+    throw (DriverException(L"getString: value at row " + toWString(currentRow)
+        + L" column " + toWString(columnIndex) + L" is not of type string"));
+  }
+  return (*((wstring*)(data[currentRow][columnIndex - 1].as_other)));
+}
+
+wstring DriverResultSet::getAsString(int columnIndex) throw (DriverException,
+    NullValueException, NotImplementedException, UnexpectedException)
+{
+  checkRowAndColPosAndSetNullFlag(columnIndex);
+  if (wasNullFlag)
+  {
+    throw (NullValueException(L"getString: value at row " + 
toWString(currentRow)
+        + L" column " + toWString(columnIndex) + L" is NULL"));
+  }
 
   std::wostringstream buffer;
 
@@ -267,6 +284,26 @@
     throw (NullValueException(L"getInt: Value at row " + toWString(currentRow)
         + L" column " + toWString(columnIndex) + L" is NULL"));
   }
+  
+  if (columnTypeTags[columnIndex - 1] != TT_INTEGER)
+  {
+    throw (DriverException(L"getInt: Value at row " + toWString(currentRow)
+        + L" column " + toWString(columnIndex) + L" is not of type integer"));
+  }
+  
+  return ((data[currentRow])[columnIndex - 1]).as_int;
+}
+
+int DriverResultSet::getAsInt(int columnIndex) throw (DriverException,
+    NullValueException, NotImplementedException, UnexpectedException)
+{
+  checkRowAndColPosAndSetNullFlag(columnIndex);
+
+  if (wasNullFlag)
+  {
+    throw (NullValueException(L"getInt: Value at row " + toWString(currentRow)
+        + L" column " + toWString(columnIndex) + L" is NULL"));
+  }
 
   int ret = -1;
   // big switch on column data type
@@ -337,12 +374,6 @@
   return ret;
 }
 
-int DriverResultSet::getInt(int columnIndex) throw (DriverException,
-    NullValueException, NotImplementedException, UnexpectedException)
-{
-  return (int)getInt32(columnIndex);
-}
-
 void DriverResultSet::close() throw (SocketIOException, BackendException,
     ControllerException, ProtocolException, UnexpectedException)
 {
Index: carob/test/TestDriverResultSet.cpp
diff -u carob/test/TestDriverResultSet.cpp:1.1 
carob/test/TestDriverResultSet.cpp:1.2
--- carob/test/TestDriverResultSet.cpp:1.1      Thu Dec 15 12:29:56 2005
+++ carob/test/TestDriverResultSet.cpp  Thu Dec 15 15:55:57 2005
@@ -65,10 +65,10 @@
           + nve.description());
     }
   }
-  //3. lets do it again with getInt
+  //3. lets do it again with getAsInt
   try
   {
-    drsPtr->getInt(2); //should fail
+    drsPtr->getAsInt(2); //should fail
     // We should receive an exception instead of coming here
     CPPUNIT_ASSERT(false);
   }
@@ -83,9 +83,9 @@
   delete statementPtr;
 }
 
-void TestDriverResultSet::testGetIntOnString()
+void TestDriverResultSet::testGetAsIntOnString()
 {
-  wstring fctName(L"TestStatement::testGetIntOnString");
+  wstring fctName(L"TestStatement::testGetAsIntOnString");
   Statement* statementPtr = NULL;
   //1. Set a string to an int an read it
   statementPtr = connectionPtr->createStatement();
@@ -97,8 +97,8 @@
   //read
   DriverResultSet* drsPtr = statementPtr->executeQuery(L"SELECT * FROM address 
WHERE id=0");
   drsPtr->next();
-  wcerr<<L"Int val as string (should be 1234)="<<drsPtr->getInt32(2)<<endl;
-  CPPUNIT_ASSERT(drsPtr->getInt32(2) == 1234);
+  wcerr<<L"Int val as string (should be 1234)="<<drsPtr->getAsInt(2)<<endl;
+  CPPUNIT_ASSERT(drsPtr->getAsInt(2) == 1234);
 
   //2. Negative numbers
   statementPtr = connectionPtr->createStatement();
@@ -110,8 +110,8 @@
   //Read it
   drsPtr = statementPtr->executeQuery(L"SELECT * FROM address WHERE id=1");
   drsPtr->next();
-  wcerr<<L"Int val as string (should be -1)="<<drsPtr->getInt32(2)<<endl;
-  CPPUNIT_ASSERT(drsPtr->getInt32(2) == -1);
+  wcerr<<L"Int val as string (should be -1)="<<drsPtr->getAsInt(2)<<endl;
+  CPPUNIT_ASSERT(drsPtr->getAsInt(2) == -1);
 
   //3. Not a number
   statementPtr = connectionPtr->createStatement();
@@ -125,7 +125,7 @@
   drsPtr->next();
   try
   {
-    drsPtr->getInt32(2);
+    drsPtr->getAsInt(2);
     CPPUNIT_ASSERT(false);
   }
   catch (DriverException de)
@@ -146,8 +146,8 @@
                                  "testNullValueRetrieval", 
                                  
&TestDriverResultSet::testNullValueRetrieval));
   suiteOfTests->addTest(new CppUnit::TestCaller<TestDriverResultSet>(
-                                 "testGetIntOnString", 
-                                 &TestDriverResultSet::testGetIntOnString));
+                                 "testGetAsIntOnString", 
+                                 &TestDriverResultSet::testGetAsIntOnString));
   return suiteOfTests;
 }
 
Index: carob/test/TestDriverResultSet.hpp
diff -u carob/test/TestDriverResultSet.hpp:1.1 
carob/test/TestDriverResultSet.hpp:1.2
--- carob/test/TestDriverResultSet.hpp:1.1      Thu Dec 15 12:29:56 2005
+++ carob/test/TestDriverResultSet.hpp  Thu Dec 15 15:55:57 2005
@@ -42,9 +42,9 @@
   void testNullValueRetrieval();
   /**
    * Sets a string entry to various numbers as strings and tries to read it
-   * using getInt function
+   * using getAsInt function
    */
-  void testGetIntOnString();
+  void testGetAsIntOnString();
 };
 
 #endif /*TESTDRIVERRESULTSET_H_*/
Index: carob/test/TestExecReadRequest.cpp
diff -u carob/test/TestExecReadRequest.cpp:1.15 
carob/test/TestExecReadRequest.cpp:1.16
--- carob/test/TestExecReadRequest.cpp:1.15     Tue Dec  6 17:54:01 2005
+++ carob/test/TestExecReadRequest.cpp  Thu Dec 15 15:55:57 2005
@@ -105,7 +105,7 @@
   for (int i=0; i<50; i++)
   {
     drsPtr->next();
-    wcerr<<i+1<<L"\t"<<drsPtr->getInt(1)
+    wcerr<<i+1<<L"\t"<<drsPtr->getInt32(1)
               <<L"\t"<<drsPtr->getString(2)
               <<L"\t\t"<<drsPtr->getString(3)<<endl;
   }
Index: carob/test/TestStatement.cpp
diff -u carob/test/TestStatement.cpp:1.16 carob/test/TestStatement.cpp:1.17
--- carob/test/TestStatement.cpp:1.16   Thu Dec 15 12:31:42 2005
+++ carob/test/TestStatement.cpp        Thu Dec 15 15:55:57 2005
@@ -112,7 +112,7 @@
     drsPtr->next();
     wcerr<<i+1<<L"\t";
     for (int j=0; j<rsmd.getColumnCount(); j++)
-      wcerr<<drsPtr->getString(j+1)<<L"\t";
+      wcerr<<drsPtr->getAsString(j+1)<<L"\t";
     wcerr<<endl;
   }
   delete statementPtr;
@@ -212,7 +212,7 @@
   //wcerr<<L"Row\tId\tName\t\tCost"<<endl;
   wcerr<<L"Id\tFirstName\tLastName"<<endl;
   CPPUNIT_ASSERT(drsPtr->next() == true);
-  wcerr<<drsPtr->getInt(1)
+  wcerr<<drsPtr->getInt32(1)
        <<L"\t"<<drsPtr->getString(2)
        <<L"\t\t"<<drsPtr->getString(3)<<endl;
     
@@ -290,7 +290,7 @@
     drsPtr->next();
     wcerr<<i+1<<L"\t";
     for (int j=0; j<rsmd.getColumnCount(); j++)
-      wcerr<<drsPtr->getString(j+1)<<L"\t";
+      wcerr<<drsPtr->getAsString(j+1)<<L"\t";
     wcerr<<endl;
   }
   delete statementPtr;

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

Reply via email to