]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/dbtable.cpp
a fix for the last fix
[wxWidgets.git] / src / common / dbtable.cpp
index f85c5904f7d99a803d2c51398e097ea1f1b920d5..a1f0a15eaf1aab2604e347ab68e5f6900bb332c0 100644 (file)
@@ -102,7 +102,32 @@ ULONG lastTableID = 0;
 #endif
 
 
-/********** wxDbTable::wxDbTable() **********/
+/********** wxDbColDef::wxDbColDef() Constructor **********/
+wxDbColDef::wxDbColDef()
+{
+    Initialize();
+}  // Constructor
+
+
+bool wxDbColDef::Initialize()
+{
+    ColName[0]      = 0;
+    DbDataType      = DB_DATA_TYPE_INTEGER;
+    SqlCtype        = SQL_C_LONG;
+    PtrDataObj      = NULL;
+    SzDataObj       = 0;
+    KeyField        = FALSE;
+    Updateable      = FALSE;
+    InsertAllowed   = FALSE;
+    DerivedCol      = FALSE;
+    CbValue         = 0;
+    Null = FALSE;
+
+    return TRUE;
+}  // wxDbColDef::Initialize()
+
+
+/********** wxDbTable::wxDbTable() Constructor **********/
 wxDbTable::wxDbTable(wxDb *pwxDb, const wxString &tblName, const int nCols,
                     const wxString &qryTblName, bool qryOnly, const wxString &tblPath)
 {
@@ -177,7 +202,7 @@ bool wxDbTable::initialize(wxDb *pwxDb, const wxString &tblName, const int nCols
     
     wxString s;
     tableID = ++lastTableID;
-    s.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"), tblName,tableID,pDb);
+    s.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"), tblName.c_str(), tableID, pDb);
     
 #ifdef __WXDEBUG__
     wxTablesInUse *tableInUse;
@@ -300,7 +325,7 @@ void wxDbTable::cleanup()
     wxString s;
     if (pDb)
     {
-        s.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"), tableName,tableID,pDb);
+        s.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"), tableName.c_str(), tableID, pDb);
         pDb->WriteSqlLog(s);
     }
 
@@ -344,20 +369,41 @@ void wxDbTable::cleanup()
     if (!queryOnly)
     {
         if (hstmtInsert)
+        {
+/*
+ODBC 3.0 says to use this form
+            if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
+*/
             if (SQLFreeStmt(hstmtInsert, SQL_DROP) != SQL_SUCCESS)
                 pDb->DispAllErrors(henv, hdbc);
+        }
 
         if (hstmtDelete)
+        {
+/*
+ODBC 3.0 says to use this form
+            if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
+*/
             if (SQLFreeStmt(hstmtDelete, SQL_DROP) != SQL_SUCCESS)
+                pDb->DispAllErrors(henv, hdbc);
+        }
 
         if (hstmtUpdate)
+        {
+/*
+ODBC 3.0 says to use this form
+            if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
+*/
             if (SQLFreeStmt(hstmtUpdate, SQL_DROP) != SQL_SUCCESS)
                 pDb->DispAllErrors(henv, hdbc);
+        }
     }
 
     if (hstmtInternal)
+    {
         if (SQLFreeStmt(hstmtInternal, SQL_DROP) != SQL_SUCCESS)
             pDb->DispAllErrors(henv, hdbc);
+    }
 
     // Delete dynamically allocated cursors
     if (hstmtDefault)
@@ -441,6 +487,15 @@ bool wxDbTable::bindParams(bool forUpdate)
                 else
                     colDefs[i].CbValue = 0;
                 break;
+            case DB_DATA_TYPE_BLOB:
+                fSqlType = pDb->GetTypeInfBlob().FsqlType;
+                precision = 50000;
+                scale = 0;
+                if (colDefs[i].Null)
+                    colDefs[i].CbValue = SQL_NULL_DATA;
+                else
+                    colDefs[i].CbValue = SQL_LEN_DATA_AT_EXEC(colDefs[i].SzDataObj);
+                break;
         }
         if (forUpdate)
         {
@@ -676,9 +731,9 @@ bool wxDbTable::Open(bool checkPrivileges)
         wxString p;
 
         if (!tablePath.IsEmpty())
-            p.Printf(wxT("Error opening '%s/%s'.\n"),tablePath,tableName);
+            p.Printf(wxT("Error opening '%s/%s'.\n"),tablePath.c_str(),tableName.c_str());
         else
-            p.Printf(wxT("Error opening '%s'.\n"), tableName);
+            p.Printf(wxT("Error opening '%s'.\n"), tableName.c_str());
 
         p += s;
         pDb->LogError(p.GetData());
@@ -711,7 +766,7 @@ bool wxDbTable::Open(bool checkPrivileges)
     if (!queryOnly && noCols > 0)
     {
         bool needComma = FALSE;
-        sqlStmt.Printf(wxT("INSERT INTO %s ("), tableName);
+        sqlStmt.Printf(wxT("INSERT INTO %s ("), tableName.c_str());
         for (i = 0; i < noCols; i++)
         {
             if (! colDefs[i].InsertAllowed)
@@ -862,11 +917,11 @@ void wxDbTable::BuildDeleteStmt(wxString &pSqlStmt, int typeOfDel, const wxStrin
     // delete all records from the database in this case.
     if (typeOfDel == DB_DEL_WHERE && (pWhereClause.Length() == 0))
     {
-        pSqlStmt.Printf(wxT("DELETE FROM %s"), tableName);
+        pSqlStmt.Printf(wxT("DELETE FROM %s"), tableName.c_str());
         return;
     }
 
-    pSqlStmt.Printf(wxT("DELETE FROM %s WHERE "), tableName);
+    pSqlStmt.Printf(wxT("DELETE FROM %s WHERE "), tableName.c_str());
 
     // Append the WHERE clause to the SQL DELETE statement
     switch(typeOfDel)
@@ -1060,7 +1115,7 @@ void wxDbTable::BuildUpdateStmt(wxString &pSqlStmt, int typeOfUpd, const wxStrin
 
     bool firstColumn = TRUE;
 
-    pSqlStmt.Printf(wxT("UPDATE %s SET "), tableName);
+    pSqlStmt.Printf(wxT("UPDATE %s SET "), tableName.c_str());
 
     // Append a list of columns to be updated
     int i;
@@ -1269,6 +1324,9 @@ bool wxDbTable::CreateTable(bool attemptDrop)
             case DB_DATA_TYPE_DATE:
                 cout << pDb->typeInfDate.TypeName;
                 break;
+            case DB_DATA_TYPE_BLOB:
+                cout << pDb->typeInfBlob.TypeName;
+                break;
         }
         cout << endl;
     }
@@ -1276,7 +1334,7 @@ bool wxDbTable::CreateTable(bool attemptDrop)
 
     // Build a CREATE TABLE string from the colDefs structure.
     bool needComma = FALSE;
-    sqlStmt.Printf(wxT("CREATE TABLE %s ("), tableName);
+    sqlStmt.Printf(wxT("CREATE TABLE %s ("), tableName.c_str());
 
     for (i = 0; i < noCols; i++)
     {
@@ -1304,9 +1362,13 @@ bool wxDbTable::CreateTable(bool attemptDrop)
             case DB_DATA_TYPE_DATE:
                 sqlStmt += pDb->GetTypeInfDate().TypeName;
                 break;
+            case DB_DATA_TYPE_BLOB:
+                sqlStmt += pDb->GetTypeInfBlob().TypeName;
+                break;
         }
         // For varchars, append the size of the string
-        if (colDefs[i].DbDataType == DB_DATA_TYPE_VARCHAR)
+        if (colDefs[i].DbDataType == DB_DATA_TYPE_VARCHAR)// ||
+//            colDefs[i].DbDataType == DB_DATA_TYPE_BLOB)
         {
             wxString s;
             s.Printf(wxT("(%d)"), colDefs[i].SzDataObj);
@@ -1402,7 +1464,7 @@ bool wxDbTable::DropTable()
 
     wxString sqlStmt;
 
-    sqlStmt.Printf(wxT("DROP TABLE %s"), tableName);
+    sqlStmt.Printf(wxT("DROP TABLE %s"), tableName.c_str());
 
     pDb->WriteSqlLog(sqlStmt);
 
@@ -1576,12 +1638,12 @@ bool wxDbTable::DropIndex(const wxString &idxName)
     wxString sqlStmt;
 
     if (pDb->Dbms() == dbmsACCESS || pDb->Dbms() == dbmsMY_SQL)
-        sqlStmt.Printf(wxT("DROP INDEX %s ON %s"),idxName,tableName);
+        sqlStmt.Printf(wxT("DROP INDEX %s ON %s"),idxName.c_str(), tableName.c_str());
     else if ((pDb->Dbms() == dbmsMS_SQL_SERVER) ||
              (pDb->Dbms() == dbmsSYBASE_ASE))
-        sqlStmt.Printf(wxT("DROP INDEX %s.%s"),tableName,idxName);
+        sqlStmt.Printf(wxT("DROP INDEX %s.%s"),tableName.c_str(), idxName.c_str());
     else
-        sqlStmt.Printf(wxT("DROP INDEX %s"),idxName);
+        sqlStmt.Printf(wxT("DROP INDEX %s"),idxName.c_str());
 
     pDb->WriteSqlLog(sqlStmt);
 
@@ -2073,8 +2135,26 @@ wxDbColDataPtr* wxDbTable::SetColDefs(wxDbColInf *pColInfs, ULONG numCols)
                     pColDataPtrs[index].SzDataObj  = sizeof(TIMESTAMP_STRUCT);
                     pColDataPtrs[index].SqlCtype   = SQL_C_TIMESTAMP;
                     break;
+                case DB_DATA_TYPE_BLOB:
+                                                 int notSupportedYet = 0;
+                    wxASSERT_MSG(notSupportedYet, wxT("This form of ::SetColDefs() cannot be used with BLOB columns"));
+                    pColDataPtrs[index].PtrDataObj = /*BLOB ADDITION NEEDED*/NULL;
+                    pColDataPtrs[index].SzDataObj  = /*BLOB ADDITION NEEDED*/sizeof(void *);
+                    pColDataPtrs[index].SqlCtype   = SQL_VARBINARY;
+                    break;
+            }
+            if (pColDataPtrs[index].PtrDataObj != NULL)
+                SetColDefs (index,pColInfs[index].colName,pColInfs[index].dbDataType, pColDataPtrs[index].PtrDataObj, pColDataPtrs[index].SqlCtype, pColDataPtrs[index].SzDataObj);
+            else
+            {
+                // Unable to build all the column definitions, as either one of 
+                // the calls to "new" failed above, or there was a BLOB field
+                // to have a column definition for.  If BLOBs are to be used,
+                // the other form of ::SetColDefs() must be used, as it is impossible
+                // to know the maximum size to create the PtrDataObj to be.
+                delete [] pColDataPtrs;
+                return NULL;
             }
-            SetColDefs (index,pColInfs[index].colName,pColInfs[index].dbDataType, pColDataPtrs[index].PtrDataObj, pColDataPtrs[index].SqlCtype, pColDataPtrs[index].SzDataObj);
         }
     }
 
@@ -2319,6 +2399,11 @@ bool wxDbTable::DeleteCursor(HSTMT *hstmtDel)
     if (!hstmtDel)  // Cursor already deleted
         return(result);
 
+/*
+ODBC 3.0 says to use this form
+    if (SQLFreeHandle(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
+    
+*/
     if (SQLFreeStmt(*hstmtDel, SQL_DROP) != SQL_SUCCESS)
     {
         pDb->DispAllErrors(henv, hdbc);