]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/dbtable.cpp
message output for darwin into console, cannot use the normal converters as these...
[wxWidgets.git] / src / common / dbtable.cpp
index 1a8a58758ae60aaecf1e71cc35cd5c342b889289..56a496d843e0675b85ea35d1c888101197dd68b6 100644 (file)
 // 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.
+//                 wxWidgets-based application
+//              2) All enhancements and bug fixes are to be submitted back to the wxWidgets
+//                 user groups free of all charges for use with the wxWidgets 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.
+//                 the wxWidgets GUI development toolkit.
 ///////////////////////////////////////////////////////////////////////////////
 
 /*
@@ -25,7 +25,7 @@
 // SYNOPSIS STOP
 */
 
-#ifdef __GNUG__
+#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
     #pragma implementation "dbtable.h"
 #endif
 
@@ -80,7 +80,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 )
         ;
@@ -325,17 +325,17 @@ void wxDbTable::cleanup()
         bool found = FALSE;
 
         wxNode *pNode;
-        pNode = TablesInUse.First();
+        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!"));
             }
             else
-                pNode = pNode->Next();
+                pNode = pNode->GetNext();
         }
         if (!found)
         {
@@ -410,6 +410,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 +485,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 +500,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 +528,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 +637,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 +659,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 +671,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 +690,7 @@ bool wxDbTable::execUpdate(const wxString &pSqlStmt)
                     break;
                 }
             }
+            retcode = SQLParamData(hstmtUpdate, &pParmID);
         }
         if (retcode == SQL_SUCCESS ||
             retcode == SQL_NO_DATA_FOUND ||
@@ -706,7 +731,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 +862,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,10 +1066,10 @@ 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('.')))
         {
@@ -1276,12 +1301,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 +1321,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 +1461,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 +1492,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 +1566,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 +1606,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 +1686,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 +1749,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 +1758,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 +1774,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 +1811,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 +1825,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 +1925,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 +1945,7 @@ int wxDbTable::Insert(void)
                     break;
                 }
             }
+            retcode = SQLParamData(hstmtInsert, &pParmID);
         }
     }
 
@@ -2389,7 +2428,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 +2468,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 +2533,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 +2559,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 +2686,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 +2736,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 +2752,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();