]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/dbtable.cpp
compilation fix
[wxWidgets.git] / src / common / dbtable.cpp
index b66711044c41093d387a0a354ca33abe4e85cda3..ce60d096dcc3dddef1c69486ac7770c4741c8fd7 100644 (file)
@@ -8,16 +8,7 @@
 // Created:     9.96
 // RCS-ID:      $Id$
 // Copyright:   (c) 1996 Remstar International, Inc.
-// Licence:     wxWindows licence, plus:
-// Notice:      This class library and its intellectual design are free of charge for use,
-//              modification, enhancement, debugging under the following conditions:
-//              1) These classes may only be used as part of the implementation of a
-//                 wxWindows-based application
-//              2) All enhancements and bug fixes are to be submitted back to the wxWindows
-//                 user groups free of all charges for use with the wxWindows library.
-//              3) These classes may not be distributed as part of any other class library,
-//                 DLL, text (written or electronic), other than a complete distribution of
-//                 the wxWindows GUI development toolkit.
+// Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
 
 /*
@@ -80,7 +71,7 @@ ULONG lastTableID = 0;
 #endif
 
 
-void csstrncpyt(char *target, const char *source, int n)
+void csstrncpyt(wxChar *target, const wxChar *source, int n)
 {
     while ( (*target++ = *source++) != '\0' && --n )
         ;
@@ -321,21 +312,20 @@ void wxDbTable::cleanup()
 #ifdef __WXDEBUG__
     if (tableID)
     {
-        TablesInUse.DeleteContents(TRUE);
         bool found = FALSE;
 
-        wxNode *pNode;
-        pNode = TablesInUse.First();
+        wxList::compatibility_iterator pNode;
+        pNode = TablesInUse.GetFirst();
         while (pNode && !found)
         {
-            if (((wxTablesInUse *)pNode->Data())->tableID == tableID)
+            if (((wxTablesInUse *)pNode->GetData())->tableID == tableID)
             {
                 found = TRUE;
-                if (!TablesInUse.DeleteNode(pNode))
-                    wxLogDebug (s,wxT("Unable to delete node!"));
+                delete (wxTablesInUse *)pNode->GetData();
+                TablesInUse.Erase(pNode);
             }
             else
-                pNode = pNode->Next();
+                pNode = pNode->GetNext();
         }
         if (!found)
         {
@@ -410,6 +400,46 @@ ODBC 3.0 says to use this form
 /***************************** PRIVATE FUNCTIONS *****************************/
 
 
+void wxDbTable::setCbValueForColumn(int columnIndex)
+{
+    switch(colDefs[columnIndex].DbDataType)
+    {
+        case DB_DATA_TYPE_VARCHAR:
+            if (colDefs[columnIndex].Null)
+                colDefs[columnIndex].CbValue = SQL_NULL_DATA;
+            else
+                colDefs[columnIndex].CbValue = SQL_NTS;
+            break;
+        case DB_DATA_TYPE_INTEGER:
+            if (colDefs[columnIndex].Null)
+                colDefs[columnIndex].CbValue = SQL_NULL_DATA;
+            else
+                colDefs[columnIndex].CbValue = 0;
+            break;
+        case DB_DATA_TYPE_FLOAT:
+            if (colDefs[columnIndex].Null)
+                colDefs[columnIndex].CbValue = SQL_NULL_DATA;
+            else
+                colDefs[columnIndex].CbValue = 0;
+            break;
+        case DB_DATA_TYPE_DATE:
+            if (colDefs[columnIndex].Null)
+                colDefs[columnIndex].CbValue = SQL_NULL_DATA;
+            else
+                colDefs[columnIndex].CbValue = 0;
+            break;
+        case DB_DATA_TYPE_BLOB:
+            if (colDefs[columnIndex].Null)
+                colDefs[columnIndex].CbValue = SQL_NULL_DATA;
+            else
+                if (colDefs[columnIndex].SqlCtype == SQL_C_CHAR)
+                    colDefs[columnIndex].CbValue = SQL_NTS;
+                else
+                    colDefs[columnIndex].CbValue = SQL_LEN_DATA_AT_EXEC(colDefs[columnIndex].SzDataObj);
+            break;
+    }
+}
+
 /********** wxDbTable::bindParams() **********/
 bool wxDbTable::bindParams(bool forUpdate)
 {
@@ -445,19 +475,11 @@ bool wxDbTable::bindParams(bool forUpdate)
                 fSqlType = pDb->GetTypeInfVarchar().FsqlType;
                 precision = colDefs[i].SzDataObj;
                 scale = 0;
-                if (colDefs[i].Null)
-                    colDefs[i].CbValue = SQL_NULL_DATA;
-                else
-                    colDefs[i].CbValue = SQL_NTS;
                 break;
             case DB_DATA_TYPE_INTEGER:
                 fSqlType = pDb->GetTypeInfInteger().FsqlType;
                 precision = pDb->GetTypeInfInteger().Precision;
                 scale = 0;
-                if (colDefs[i].Null)
-                    colDefs[i].CbValue = SQL_NULL_DATA;
-                else
-                    colDefs[i].CbValue = 0;
                 break;
             case DB_DATA_TYPE_FLOAT:
                 fSqlType = pDb->GetTypeInfFloat().FsqlType;
@@ -468,30 +490,21 @@ bool wxDbTable::bindParams(bool forUpdate)
                 // I check for this here and set the scale = precision.
                 //if (scale < 0)
                 // scale = (short) precision;
-                if (colDefs[i].Null)
-                    colDefs[i].CbValue = SQL_NULL_DATA;
-                else
-                    colDefs[i].CbValue = 0;
                 break;
             case DB_DATA_TYPE_DATE:
                 fSqlType = pDb->GetTypeInfDate().FsqlType;
                 precision = pDb->GetTypeInfDate().Precision;
                 scale = 0;
-                if (colDefs[i].Null)
-                    colDefs[i].CbValue = SQL_NULL_DATA;
-                else
-                    colDefs[i].CbValue = 0;
                 break;
             case DB_DATA_TYPE_BLOB:
                 fSqlType = pDb->GetTypeInfBlob().FsqlType;
-                precision = -1;
+                precision = colDefs[i].SzDataObj;
                 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;
         }
+
+        setCbValueForColumn(i);
+
         if (forUpdate)
         {
             if (SQLBindParameter(hstmtUpdate, colNo++, SQL_PARAM_INPUT, colDefs[i].SqlCtype,
@@ -505,7 +518,7 @@ bool wxDbTable::bindParams(bool forUpdate)
         {
             if (SQLBindParameter(hstmtInsert, colNo++, SQL_PARAM_INPUT, colDefs[i].SqlCtype,
                                  fSqlType, precision, scale, (UCHAR*) colDefs[i].PtrDataObj,
-                                 precision+1,&colDefs[i].CbValue) != SQL_SUCCESS)
+                                 precision+1, &colDefs[i].CbValue) != SQL_SUCCESS)
             {
                 return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
             }
@@ -614,7 +627,7 @@ bool wxDbTable::execDelete(const wxString &pSqlStmt)
     RETCODE retcode;
 
     // Execute the DELETE statement
-    retcode = SQLExecDirect(hstmtDelete, (UCHAR FAR *) pSqlStmt.c_str(), SQL_NTS);
+    retcode = SQLExecDirect(hstmtDelete, (SQLTCHAR FAR *) pSqlStmt.c_str(), SQL_NTS);
 
     if (retcode == SQL_SUCCESS ||
         retcode == SQL_NO_DATA_FOUND ||
@@ -636,7 +649,7 @@ bool wxDbTable::execUpdate(const wxString &pSqlStmt)
     RETCODE retcode;
 
     // Execute the UPDATE statement
-    retcode = SQLExecDirect(hstmtUpdate, (UCHAR FAR *) pSqlStmt.c_str(), SQL_NTS);
+    retcode = SQLExecDirect(hstmtUpdate, (SQLTCHAR FAR *) pSqlStmt.c_str(), SQL_NTS);
 
     if (retcode == SQL_SUCCESS ||
         retcode == SQL_NO_DATA_FOUND ||
@@ -648,7 +661,8 @@ bool wxDbTable::execUpdate(const wxString &pSqlStmt)
     else if (retcode == SQL_NEED_DATA)
     {
         PTR pParmID;
-        while ((retcode = SQLParamData(hstmtUpdate, &pParmID) == SQL_NEED_DATA))
+        retcode = SQLParamData(hstmtUpdate, &pParmID);
+        while (retcode == SQL_NEED_DATA)
         {
             // Find the parameter
             int i;
@@ -666,6 +680,7 @@ bool wxDbTable::execUpdate(const wxString &pSqlStmt)
                     break;
                 }
             }
+            retcode = SQLParamData(hstmtUpdate, &pParmID);
         }
         if (retcode == SQL_SUCCESS ||
             retcode == SQL_NO_DATA_FOUND ||
@@ -706,7 +721,7 @@ bool wxDbTable::query(int queryType, bool forUpdate, bool distinct, const wxStri
 
     // Execute the SQL SELECT statement
     int retcode;
-    retcode = SQLExecDirect(hstmt, (UCHAR FAR *) (queryType == DB_SELECT_STATEMENT ? pSqlStmt.c_str() : sqlStmt.c_str()), SQL_NTS);
+    retcode = SQLExecDirect(hstmt, (SQLTCHAR FAR *) (queryType == DB_SELECT_STATEMENT ? pSqlStmt.c_str() : sqlStmt.c_str()), SQL_NTS);
     if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
         return(pDb->DispAllErrors(henv, hdbc, hstmt));
 
@@ -837,11 +852,11 @@ bool wxDbTable::Open(bool checkPrivileges, bool checkTableExists)
         // Prepare the insert statement for execution
         if (insertableCount)
         {
-            if (SQLPrepare(hstmtInsert, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
+            if (SQLPrepare(hstmtInsert, (SQLTCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
                 return(pDb->DispAllErrors(henv, hdbc, hstmtInsert));
         }
         else
-            insertable= FALSE;
+            insertable = FALSE;
     }
 
     // Completed successfully
@@ -1041,12 +1056,12 @@ void wxDbTable::BuildSelectStmt(wxString &pSqlStmt, int typeOfSelect, bool disti
 
     // Add the column list
     int i;
-        wxString tStr;
+    wxString tStr;
     for (i = 0; i < noCols; i++)
     {
-                 tStr = colDefs[i].ColName;
+        tStr = colDefs[i].ColName;
         // If joining tables, the base table column names must be qualified to avoid ambiguity
-        if ((appendFromClause || pDb->Dbms() == dbmsACCESS) && !tStr.Find(wxT('.')))
+        if ((appendFromClause || pDb->Dbms() == dbmsACCESS) && tStr.Find(wxT('.')) == wxNOT_FOUND)
         {
             pSqlStmt += pDb->SQLTableName(queryTableName.c_str());
             pSqlStmt += wxT(".");
@@ -1259,7 +1274,7 @@ void wxDbTable::BuildWhereClause(wxString &pWhereClause, int typeOfWhere,
             // Concatenate where phrase for the column
             wxString tStr = colDefs[colNo].ColName;
 
-            if (qualTableName.Length() && !tStr.Find(wxT('.')))
+            if (qualTableName.Length() && tStr.Find(wxT('.')) == wxNOT_FOUND)
             {
                 pWhereClause += pDb->SQLTableName(qualTableName);
                 pWhereClause += wxT(".");
@@ -1276,12 +1291,14 @@ void wxDbTable::BuildWhereClause(wxString &pWhereClause, int typeOfWhere,
                 case SQL_C_CHAR:
                     colValue.Printf(wxT("'%s'"), (UCHAR FAR *) colDefs[colNo].PtrDataObj);
                     break;
+                case SQL_C_SHORT:
                 case SQL_C_SSHORT:
                     colValue.Printf(wxT("%hi"), *((SWORD *) colDefs[colNo].PtrDataObj));
                     break;
                 case SQL_C_USHORT:
                     colValue.Printf(wxT("%hu"), *((UWORD *) colDefs[colNo].PtrDataObj));
                     break;
+                case SQL_C_LONG:
                 case SQL_C_SLONG:
                     colValue.Printf(wxT("%li"), *((SDWORD *) colDefs[colNo].PtrDataObj));
                     break;
@@ -1294,6 +1311,14 @@ void wxDbTable::BuildWhereClause(wxString &pWhereClause, int typeOfWhere,
                 case SQL_C_DOUBLE:
                     colValue.Printf(wxT("%.6f"), *((SDOUBLE *) colDefs[colNo].PtrDataObj));
                     break;
+                default:
+                    {
+                        wxString strMsg;
+                        strMsg.Printf(wxT("wxDbTable::bindParams(): Unknown column type for colDefs %d colName %s"),
+                                    colNo,colDefs[colNo].ColName);
+                        wxFAIL_MSG(strMsg.c_str());
+                    }
+                    break;
             }
             pWhereClause += colValue;
         }
@@ -1426,7 +1451,7 @@ bool wxDbTable::CreateTable(bool attemptDrop)
         }
         // For varchars, append the size of the string
         if (colDefs[i].DbDataType == DB_DATA_TYPE_VARCHAR &&
-            (pDb->Dbms() != dbmsMY_SQL || pDb->GetTypeInfVarchar().TypeName != "text"))// ||
+            (pDb->Dbms() != dbmsMY_SQL || pDb->GetTypeInfVarchar().TypeName != _T("text")))// ||
 //            colDefs[i].DbDataType == DB_DATA_TYPE_BLOB)
         {
             wxString s;
@@ -1457,9 +1482,8 @@ bool wxDbTable::CreateTable(bool attemptDrop)
             break;
         }
     }
-    if (j && (pDb->Dbms() != dbmsDBASE) 
-                 && (pDb->Dbms() != dbmsXBASE_SEQUITER)
-          )  // Found a keyfield
+    if ( j && (pDb->Dbms() != dbmsDBASE)
+        && (pDb->Dbms() != dbmsXBASE_SEQUITER) )  // Found a keyfield
     {
         switch (pDb->Dbms())
         {
@@ -1532,7 +1556,7 @@ bool wxDbTable::CreateTable(bool attemptDrop)
 #endif
 
     // Execute the CREATE TABLE statement
-    RETCODE retcode = SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS);
+    RETCODE retcode = SQLExecDirect(hstmt, (SQLTCHAR FAR *) sqlStmt.c_str(), SQL_NTS);
     if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
     {
         pDb->DispAllErrors(henv, hdbc, hstmt);
@@ -1572,7 +1596,7 @@ bool wxDbTable::DropTable()
     cout << endl << sqlStmt.c_str() << endl;
 #endif
 
-    RETCODE retcode = SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS);
+    RETCODE retcode = SQLExecDirect(hstmt, (SQLTCHAR FAR *) sqlStmt.c_str(), SQL_NTS);
     if (retcode != SQL_SUCCESS)
     {
         // Check for "Base table not found" error and ignore
@@ -1652,11 +1676,14 @@ bool wxDbTable::CreateIndex(const wxString &idxName, bool unique, UWORD noIdxCol
 
                 if (!ok)
                 {
+                    #if 0
+                    // retcode is not used
                     wxODBC_ERRORS retcode;
                     // Oracle returns a DB_ERR_GENERAL_ERROR if the column is already
                     // defined to be NOT NULL, but reportedly MySQL doesn't mind.
                     // This line is just here for debug checking of the value
                     retcode = (wxODBC_ERRORS)pDb->DB_STATUS;
+                    #endif
                 }
             }
             else
@@ -1712,7 +1739,7 @@ bool wxDbTable::CreateIndex(const wxString &idxName, bool unique, UWORD noIdxCol
         }
         
         // Postgres and SQL Server 7 do not support the ASC/DESC keywords for index columns
-        if (!((pDb->Dbms() == dbmsMS_SQL_SERVER) && (strncmp(pDb->dbInf.dbmsVer,"07",2)==0)) &&
+        if (!((pDb->Dbms() == dbmsMS_SQL_SERVER) && (wxStrncmp(pDb->dbInf.dbmsVer,_T("07"),2)==0)) &&
             !(pDb->Dbms() == dbmsPOSTGRES))
         {
             if (pIdxDefs[i].Ascending)
@@ -1721,7 +1748,7 @@ bool wxDbTable::CreateIndex(const wxString &idxName, bool unique, UWORD noIdxCol
                 sqlStmt += wxT(" DESC");
         }
         else
-            wxASSERT_MSG(pIdxDefs[i].Ascending, "Datasource does not support DESCending index columns");
+            wxASSERT_MSG(pIdxDefs[i].Ascending, _T("Datasource does not support DESCending index columns"));
 
         if ((i + 1) < noIdxCols)
             sqlStmt += wxT(",");
@@ -1737,7 +1764,7 @@ bool wxDbTable::CreateIndex(const wxString &idxName, bool unique, UWORD noIdxCol
 #endif
 
     // Execute the CREATE INDEX statement
-    if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
+    if (SQLExecDirect(hstmt, (SQLTCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
     {
         pDb->DispAllErrors(henv, hdbc, hstmt);
         pDb->RollbackTrans();
@@ -1774,7 +1801,7 @@ bool wxDbTable::DropIndex(const wxString &idxName)
                        pDb->SQLTableName(tableName.c_str()).c_str());
     else if ((pDb->Dbms() == dbmsMS_SQL_SERVER) ||
              (pDb->Dbms() == dbmsSYBASE_ASE) ||
-                        (pDb->Dbms() == dbmsXBASE_SEQUITER))
+             (pDb->Dbms() == dbmsXBASE_SEQUITER))
         sqlStmt.Printf(wxT("DROP INDEX %s.%s"),
                        pDb->SQLTableName(tableName.c_str()).c_str(),
                        pDb->SQLTableName(idxName.c_str()).c_str());
@@ -1788,7 +1815,7 @@ bool wxDbTable::DropIndex(const wxString &idxName)
     cout << endl << sqlStmt.c_str() << endl;
 #endif
 
-    if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
+    if (SQLExecDirect(hstmt, (SQLTCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
     {
         // Check for "Index not found" error and ignore
         pDb->GetNextError(henv, hdbc, hstmt);
@@ -1888,7 +1915,8 @@ int wxDbTable::Insert(void)
     if (retcode == SQL_NEED_DATA)
     {
         PTR pParmID;
-        while ((retcode = SQLParamData(hstmtInsert, &pParmID) == SQL_NEED_DATA))
+        retcode = SQLParamData(hstmtInsert, &pParmID);
+        while (retcode == SQL_NEED_DATA)
         {
             // Find the parameter
             int i;
@@ -1907,6 +1935,15 @@ int wxDbTable::Insert(void)
                     break;
                 }
             }
+            retcode = SQLParamData(hstmtInsert, &pParmID);
+            if (retcode != SQL_SUCCESS &&
+                retcode != SQL_SUCCESS_WITH_INFO)
+            {
+                // record was not inserted
+                pDb->DispNextError();
+                pDb->DispAllErrors(henv, hdbc, hstmtInsert);
+                return(DB_FAILURE);
+            }
         }
     }
 
@@ -2389,7 +2426,7 @@ ULONG wxDbTable::Count(const wxString &args)
     }
 
     // Execute the SQL statement
-    if (SQLExecDirect(*hstmtCount, (UCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
+    if (SQLExecDirect(*hstmtCount, (SQLTCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
     {
         pDb->DispAllErrors(henv, hdbc, *hstmtCount);
         return(0);
@@ -2429,8 +2466,8 @@ bool wxDbTable::Refresh(void)
     hstmt = hstmtInternal;
 #if wxODBC_BACKWARD_COMPATABILITY
     // Save the where and order by clauses
-    char *saveWhere = where;
-    char *saveOrderBy = orderBy;
+    wxChar *saveWhere = where;
+    wxChar *saveOrderBy = orderBy;
 #else
     wxString saveWhere = where;
     wxString saveOrderBy = orderBy;
@@ -2494,10 +2531,10 @@ bool wxDbTable::SetColNull(UWORD colNo, bool set)
     {
         colDefs[colNo].Null = set;
         if (set)  // Blank out the values in the member variable
-        {
-           colDefs[colNo].CbValue = SQL_NULL_DATA; // SF PATCH#766404
-           ClearMemberVar(colNo,FALSE);  // Must call with FALSE, or infinite recursion will happen
-        }
+           ClearMemberVar(colNo, FALSE);  // Must call with FALSE here, or infinite recursion will happen
+
+        setCbValueForColumn(colNo);
+
         return(TRUE);
     }
     else
@@ -2520,10 +2557,10 @@ bool wxDbTable::SetColNull(const wxString &colName, bool set)
     {
         colDefs[colNo].Null = set;
         if (set)  // Blank out the values in the member variable
-        {
-           colDefs[colNo].CbValue = SQL_NULL_DATA;  // SF PATCH#766404
-           ClearMemberVar(colNo,FALSE);  // Must call with FALSE, or infinite recursion will happen
-        }
+           ClearMemberVar(colNo,FALSE);  // Must call with FALSE here, or infinite recursion will happen
+
+        setCbValueForColumn(colNo);
+
         return(TRUE);
     }
     else
@@ -2647,10 +2684,10 @@ wxVariant wxDbTable::GetCol(const int colNo) const
                 val = (long)(*(unsigned long *)(colDefs[colNo].PtrDataObj));
                 break;
             case SQL_C_TINYINT:
-                val = (long)(*(char *)(colDefs[colNo].PtrDataObj));
+                val = (long)(*(wxChar *)(colDefs[colNo].PtrDataObj));
                 break;
             case SQL_C_UTINYINT:
-                val = (long)(*(unsigned char *)(colDefs[colNo].PtrDataObj));
+                val = (long)(*(wxChar *)(colDefs[colNo].PtrDataObj));
                 break;
             case SQL_C_USHORT:
                 val = (long)(*(UWORD *)(colDefs[colNo].PtrDataObj));
@@ -2697,7 +2734,7 @@ void wxDbTable::SetCol(const int colNo, const wxVariant val)
         {
             case SQL_CHAR:
             case SQL_VARCHAR:
-                csstrncpyt((char *)(colDefs[colNo].PtrDataObj),
+                csstrncpyt((wxChar *)(colDefs[colNo].PtrDataObj),
                            val.GetString().c_str(),
                            colDefs[colNo].SzDataObj-1);
                 break;
@@ -2713,10 +2750,10 @@ void wxDbTable::SetCol(const int colNo, const wxVariant val)
                 *(unsigned long *)(colDefs[colNo].PtrDataObj) = val.GetLong();
                 break;
             case SQL_C_TINYINT:
-                *(char *)(colDefs[colNo].PtrDataObj) = val.GetChar();
+                *(wxChar *)(colDefs[colNo].PtrDataObj) = val.GetChar();
                 break;
             case SQL_C_UTINYINT:
-                *(unsigned char *)(colDefs[colNo].PtrDataObj) = val.GetChar();
+                *(wxChar *)(colDefs[colNo].PtrDataObj) = val.GetChar();
                 break;
             case SQL_C_USHORT:
                 *(unsigned short *)(colDefs[colNo].PtrDataObj) = val.GetLong();