]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/dbtable.cpp
a tad bit more padding
[wxWidgets.git] / src / common / dbtable.cpp
index f32954f688ffa9e63d981c733255e1a21ce6aaba..4d8f113e6c10f560d8ec981503aeb7c128591b69 100644 (file)
@@ -1,5 +1,5 @@
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
-// Name:        dbtable.cpp
+// Name:        src/common/dbtable.cpp
 // Purpose:     Implementation of the wxDbTable class.
 // Author:      Doug Card
 // Modified by: George Tasker
 // Purpose:     Implementation of the wxDbTable class.
 // Author:      Doug Card
 // Modified by: George Tasker
 // Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
 
 // Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
 
-/*
-// SYNOPSIS START
-// SYNOPSIS STOP
-*/
-
 #include  "wx/wxprec.h"
 
 #ifdef __BORLANDC__
     #pragma hdrstop
 #endif
 
 #include  "wx/wxprec.h"
 
 #ifdef __BORLANDC__
     #pragma hdrstop
 #endif
 
+#if wxUSE_ODBC
+
+#ifndef WX_PRECOMP
+    #include "wx/object.h"
+    #include "wx/list.h"
+    #include "wx/string.h"
+    #include "wx/utils.h"
+    #include "wx/log.h"
+#endif
+
 #ifdef DBDEBUG_CONSOLE
 #if wxUSE_IOSTREAMH
     #include <iostream.h>
 #ifdef DBDEBUG_CONSOLE
 #if wxUSE_IOSTREAMH
     #include <iostream.h>
     #include "wx/ioswrap.h"
 #endif
 
     #include "wx/ioswrap.h"
 #endif
 
-#ifndef WX_PRECOMP
-    #include "wx/string.h"
-    #include "wx/object.h"
-    #include "wx/list.h"
-    #include "wx/utils.h"
-    #include "wx/log.h"
-#endif
 #include "wx/filefn.h"
 
 #include "wx/filefn.h"
 
-#if wxUSE_ODBC
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -69,7 +65,7 @@ ULONG lastTableID = 0;
 
 void csstrncpyt(wxChar *target, const wxChar *source, int n)
 {
 
 void csstrncpyt(wxChar *target, const wxChar *source, int n)
 {
-    while ( (*target++ = *source++) != '\0' && --n )
+    while ( (*target++ = *source++) != '\0' && --n != 0 )
         ;
 
     *target = '\0';
         ;
 
     *target = '\0';
@@ -159,7 +155,7 @@ bool wxDbTable::initialize(wxDb *pwxDb, const wxString &tblName, const UWORD num
     tableName.Empty();
     queryTableName.Empty();
 
     tableName.Empty();
     queryTableName.Empty();
 
-    wxASSERT(tblName.Length());
+    wxASSERT(tblName.length());
     wxASSERT(pDb);
 
     if (!pDb)
     wxASSERT(pDb);
 
     if (!pDb)
@@ -171,12 +167,12 @@ bool wxDbTable::initialize(wxDb *pwxDb, const wxString &tblName, const UWORD num
         (pDb->Dbms() == dbmsINTERBASE))
         tableName = tableName.Upper();
 
         (pDb->Dbms() == dbmsINTERBASE))
         tableName = tableName.Upper();
 
-    if (tblPath.Length())
+    if (tblPath.length())
         tablePath = tblPath;                    // Table Path - used for dBase files
     else
         tablePath.Empty();
 
         tablePath = tblPath;                    // Table Path - used for dBase files
     else
         tablePath.Empty();
 
-    if (qryTblName.Length())                    // Name of the table/view to query
+    if (qryTblName.length())                    // Name of the table/view to query
         queryTableName = qryTblName;
     else
         queryTableName = tblName;
         queryTableName = qryTblName;
     else
         queryTableName = tblName;
@@ -190,7 +186,8 @@ bool wxDbTable::initialize(wxDb *pwxDb, const wxString &tblName, const UWORD num
 
     wxString s;
     tableID = ++lastTableID;
 
     wxString s;
     tableID = ++lastTableID;
-    s.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"), tblName.c_str(), tableID, pDb);
+    s.Printf(wxT("wxDbTable constructor (%-20s) tableID:[%6lu] pDb:[%p]"),
+             tblName.c_str(), tableID, wx_static_cast(void*, pDb));
 
 #ifdef __WXDEBUG__
     wxTablesInUse *tableInUse;
 
 #ifdef __WXDEBUG__
     wxTablesInUse *tableInUse;
@@ -313,7 +310,8 @@ void wxDbTable::cleanup()
     wxString s;
     if (pDb)
     {
     wxString s;
     if (pDb)
     {
-        s.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"), tableName.c_str(), tableID, pDb);
+        s.Printf(wxT("wxDbTable destructor (%-20s) tableID:[%6lu] pDb:[%p]"),
+                 tableName.c_str(), tableID, wx_static_cast(void*, pDb));
         pDb->WriteSqlLog(s);
     }
 
         pDb->WriteSqlLog(s);
     }
 
@@ -413,6 +411,7 @@ void wxDbTable::setCbValueForColumn(int columnIndex)
     switch(colDefs[columnIndex].DbDataType)
     {
         case DB_DATA_TYPE_VARCHAR:
     switch(colDefs[columnIndex].DbDataType)
     {
         case DB_DATA_TYPE_VARCHAR:
+        case DB_DATA_TYPE_MEMO:
             if (colDefs[columnIndex].Null)
                 colDefs[columnIndex].CbValue = SQL_NULL_DATA;
             else
             if (colDefs[columnIndex].Null)
                 colDefs[columnIndex].CbValue = SQL_NULL_DATA;
             else
@@ -484,6 +483,11 @@ bool wxDbTable::bindParams(bool forUpdate)
                 precision = colDefs[i].SzDataObj;
                 scale = 0;
                 break;
                 precision = colDefs[i].SzDataObj;
                 scale = 0;
                 break;
+            case DB_DATA_TYPE_MEMO:
+                fSqlType = pDb->GetTypeInfMemo().FsqlType;
+                precision = colDefs[i].SzDataObj;
+                scale = 0;
+                break;
             case DB_DATA_TYPE_INTEGER:
                 fSqlType = pDb->GetTypeInfInteger().FsqlType;
                 precision = pDb->GetTypeInfInteger().Precision;
             case DB_DATA_TYPE_INTEGER:
                 fSqlType = pDb->GetTypeInfInteger().FsqlType;
                 precision = pDb->GetTypeInfInteger().Precision;
@@ -556,21 +560,17 @@ bool wxDbTable::bindUpdateParams(void)
 /********** wxDbTable::bindCols() **********/
 bool wxDbTable::bindCols(HSTMT cursor)
 {
 /********** wxDbTable::bindCols() **********/
 bool wxDbTable::bindCols(HSTMT cursor)
 {
-    static SQLLEN cb;
-
     // Bind each column of the table to a memory address for fetching data
     UWORD i;
     for (i = 0; i < m_numCols; i++)
     {
     // Bind each column of the table to a memory address for fetching data
     UWORD i;
     for (i = 0; i < m_numCols; i++)
     {
-        cb = colDefs[i].CbValue;
         if (SQLBindCol(cursor, (UWORD)(i+1), colDefs[i].SqlCtype, (UCHAR*) colDefs[i].PtrDataObj,
         if (SQLBindCol(cursor, (UWORD)(i+1), colDefs[i].SqlCtype, (UCHAR*) colDefs[i].PtrDataObj,
-                       colDefs[i].SzDataObj, &cb ) != SQL_SUCCESS)
+                       colDefs[i].SzDataObj, &colDefs[i].CbValue ) != SQL_SUCCESS)
           return (pDb->DispAllErrors(henv, hdbc, cursor));
     }
 
     // Completed successfully
     return true;
           return (pDb->DispAllErrors(henv, hdbc, cursor));
     }
 
     // Completed successfully
     return true;
-
 }  // wxDbTable::bindCols()
 
 
 }  // wxDbTable::bindCols()
 
 
@@ -987,7 +987,7 @@ void wxDbTable::BuildDeleteStmt(wxString &pSqlStmt, int typeOfDel, const wxStrin
 
     // Handle the case of DeleteWhere() and the where clause is blank.  It should
     // delete all records from the database in this case.
 
     // Handle the case of DeleteWhere() and the where clause is blank.  It should
     // delete all records from the database in this case.
-    if (typeOfDel == DB_DEL_WHERE && (pWhereClause.Length() == 0))
+    if (typeOfDel == DB_DEL_WHERE && (pWhereClause.length() == 0))
     {
         pSqlStmt.Printf(wxT("DELETE FROM %s"),
                         pDb->SQLTableName(tableName.c_str()).c_str());
     {
         pSqlStmt.Printf(wxT("DELETE FROM %s"),
                         pDb->SQLTableName(tableName.c_str()).c_str());
@@ -1066,7 +1066,7 @@ void wxDbTable::BuildSelectStmt(wxString &pSqlStmt, int typeOfSelect, bool disti
     if (typeOfSelect == DB_SELECT_WHERE && from && wxStrlen(from))
         appendFromClause = true;
 #else
     if (typeOfSelect == DB_SELECT_WHERE && from && wxStrlen(from))
         appendFromClause = true;
 #else
-    if (typeOfSelect == DB_SELECT_WHERE && from.Length())
+    if (typeOfSelect == DB_SELECT_WHERE && from.length())
         appendFromClause = true;
 #endif
 
         appendFromClause = true;
 #endif
 
@@ -1126,7 +1126,7 @@ void wxDbTable::BuildSelectStmt(wxString &pSqlStmt, int typeOfSelect, bool disti
 #if wxODBC_BACKWARD_COMPATABILITY
             if (where && wxStrlen(where))   // May not want a where clause!!!
 #else
 #if wxODBC_BACKWARD_COMPATABILITY
             if (where && wxStrlen(where))   // May not want a where clause!!!
 #else
-            if (where.Length())   // May not want a where clause!!!
+            if (where.length())   // May not want a where clause!!!
 #endif
             {
                 pSqlStmt += wxT(" WHERE ");
 #endif
             {
                 pSqlStmt += wxT(" WHERE ");
@@ -1135,7 +1135,7 @@ void wxDbTable::BuildSelectStmt(wxString &pSqlStmt, int typeOfSelect, bool disti
             break;
         case DB_SELECT_KEYFIELDS:
             BuildWhereClause(whereClause, DB_WHERE_KEYFIELDS);
             break;
         case DB_SELECT_KEYFIELDS:
             BuildWhereClause(whereClause, DB_WHERE_KEYFIELDS);
-            if (whereClause.Length())
+            if (whereClause.length())
             {
                 pSqlStmt += wxT(" WHERE ");
                 pSqlStmt += whereClause;
             {
                 pSqlStmt += wxT(" WHERE ");
                 pSqlStmt += whereClause;
@@ -1143,7 +1143,7 @@ void wxDbTable::BuildSelectStmt(wxString &pSqlStmt, int typeOfSelect, bool disti
             break;
         case DB_SELECT_MATCHING:
             BuildWhereClause(whereClause, DB_WHERE_MATCHING);
             break;
         case DB_SELECT_MATCHING:
             BuildWhereClause(whereClause, DB_WHERE_MATCHING);
-            if (whereClause.Length())
+            if (whereClause.length())
             {
                 pSqlStmt += wxT(" WHERE ");
                 pSqlStmt += whereClause;
             {
                 pSqlStmt += wxT(" WHERE ");
                 pSqlStmt += whereClause;
@@ -1155,7 +1155,7 @@ void wxDbTable::BuildSelectStmt(wxString &pSqlStmt, int typeOfSelect, bool disti
 #if wxODBC_BACKWARD_COMPATABILITY
     if (orderBy && wxStrlen(orderBy))
 #else
 #if wxODBC_BACKWARD_COMPATABILITY
     if (orderBy && wxStrlen(orderBy))
 #else
-    if (orderBy.Length())
+    if (orderBy.length())
 #endif
     {
         pSqlStmt += wxT(" ORDER BY ");
 #endif
     {
         pSqlStmt += wxT(" ORDER BY ");
@@ -1289,7 +1289,7 @@ void wxDbTable::BuildWhereClause(wxString &pWhereClause, int typeOfWhere,
             // Concatenate where phrase for the column
             wxString tStr = colDefs[colNumber].ColName;
 
             // Concatenate where phrase for the column
             wxString tStr = colDefs[colNumber].ColName;
 
-            if (qualTableName.Length() && tStr.Find(wxT('.')) == wxNOT_FOUND)
+            if (qualTableName.length() && tStr.Find(wxT('.')) == wxNOT_FOUND)
             {
                 pWhereClause += pDb->SQLTableName(qualTableName);
                 pWhereClause += wxT(".");
             {
                 pWhereClause += pDb->SQLTableName(qualTableName);
                 pWhereClause += wxT(".");
@@ -1308,7 +1308,7 @@ void wxDbTable::BuildWhereClause(wxString &pWhereClause, int typeOfWhere,
                 case SQL_C_WCHAR:
 #endif
                 //case SQL_C_WXCHAR:  SQL_C_WXCHAR is covered by either SQL_C_CHAR or SQL_C_WCHAR
                 case SQL_C_WCHAR:
 #endif
                 //case SQL_C_WXCHAR:  SQL_C_WXCHAR is covered by either SQL_C_CHAR or SQL_C_WCHAR
-                    colValue.Printf(wxT("'%s'"), (UCHAR FAR *) colDefs[colNumber].PtrDataObj);
+                    colValue.Printf(wxT("'%s'"), GetDb()->EscapeSqlChars((wxChar *)colDefs[colNumber].PtrDataObj).c_str());
                     break;
                 case SQL_C_SHORT:
                 case SQL_C_SSHORT:
                     break;
                 case SQL_C_SHORT:
                 case SQL_C_SSHORT:
@@ -1414,6 +1414,9 @@ bool wxDbTable::CreateTable(bool attemptDrop)
             case DB_DATA_TYPE_VARCHAR:
                 cout << pDb->GetTypeInfVarchar().TypeName << wxT("(") << (int)(colDefs[i].SzDataObj / sizeof(wxChar)) << wxT(")");
                 break;
             case DB_DATA_TYPE_VARCHAR:
                 cout << pDb->GetTypeInfVarchar().TypeName << wxT("(") << (int)(colDefs[i].SzDataObj / sizeof(wxChar)) << wxT(")");
                 break;
+            case DB_DATA_TYPE_MEMO:
+                cout << pDb->GetTypeInfMemo().TypeName;
+                break;
             case DB_DATA_TYPE_INTEGER:
                 cout << pDb->GetTypeInfInteger().TypeName;
                 break;
             case DB_DATA_TYPE_INTEGER:
                 cout << pDb->GetTypeInfInteger().TypeName;
                 break;
@@ -1455,6 +1458,9 @@ bool wxDbTable::CreateTable(bool attemptDrop)
             case DB_DATA_TYPE_VARCHAR:
                 sqlStmt += pDb->GetTypeInfVarchar().TypeName;
                 break;
             case DB_DATA_TYPE_VARCHAR:
                 sqlStmt += pDb->GetTypeInfVarchar().TypeName;
                 break;
+            case DB_DATA_TYPE_MEMO:
+                sqlStmt += pDb->GetTypeInfMemo().TypeName;
+                break;
             case DB_DATA_TYPE_INTEGER:
                 sqlStmt += pDb->GetTypeInfInteger().TypeName;
                 break;
             case DB_DATA_TYPE_INTEGER:
                 sqlStmt += pDb->GetTypeInfInteger().TypeName;
                 break;
@@ -2234,6 +2240,20 @@ void wxDbTable::ClearMemberVar(UWORD colNumber, bool setToNull)
             pDt->second = 0;
             pDt->fraction = 0;
             break;
             pDt->second = 0;
             pDt->fraction = 0;
             break;
+        case SQL_C_DATE:
+            DATE_STRUCT *pDtd;
+            pDtd = (DATE_STRUCT *) colDefs[colNumber].PtrDataObj;
+            pDtd->year = 0;
+            pDtd->month = 0;
+            pDtd->day = 0;
+            break;
+        case SQL_C_TIME:
+            TIME_STRUCT *pDtt;
+            pDtt = (TIME_STRUCT *) colDefs[colNumber].PtrDataObj;
+            pDtt->hour = 0;
+            pDtt->minute = 0;
+            pDtt->second = 0;
+            break;
     }
 
     if (setToNull)
     }
 
     if (setToNull)
@@ -2289,7 +2309,7 @@ bool wxDbTable::SetColDefs(UWORD index, const wxString &fieldName, int dataType,
     if (!colDefs)  // May happen if the database connection fails
         return false;
 
     if (!colDefs)  // May happen if the database connection fails
         return false;
 
-    if (fieldName.Length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN)
+    if (fieldName.length() > (unsigned int) DB_MAX_COLUMN_NAME_LEN)
     {
         wxStrncpy(colDefs[index].ColName, fieldName, DB_MAX_COLUMN_NAME_LEN);
         colDefs[index].ColName[DB_MAX_COLUMN_NAME_LEN] = 0;  // Prevent buffer overrun
     {
         wxStrncpy(colDefs[index].ColName, fieldName, DB_MAX_COLUMN_NAME_LEN);
         colDefs[index].ColName[DB_MAX_COLUMN_NAME_LEN] = 0;  // Prevent buffer overrun
@@ -2349,6 +2369,11 @@ wxDbColDataPtr* wxDbTable::SetColDefs(wxDbColInf *pColInfs, UWORD numCols)
                    pColDataPtrs[index].SzDataObj  = pColInfs[index].bufferSize+(1*sizeof(wxChar));
                    pColDataPtrs[index].SqlCtype   = SQL_C_WXCHAR;
                    break;
                    pColDataPtrs[index].SzDataObj  = pColInfs[index].bufferSize+(1*sizeof(wxChar));
                    pColDataPtrs[index].SqlCtype   = SQL_C_WXCHAR;
                    break;
+                case DB_DATA_TYPE_MEMO:
+                   pColDataPtrs[index].PtrDataObj = new wxChar[pColInfs[index].bufferSize+(1*sizeof(wxChar))];
+                   pColDataPtrs[index].SzDataObj  = pColInfs[index].bufferSize+(1*sizeof(wxChar));
+                   pColDataPtrs[index].SqlCtype   = SQL_C_WXCHAR;
+                   break;
                 case DB_DATA_TYPE_INTEGER:
                     // Can be long or short
                     if (pColInfs[index].bufferSize == sizeof(long))
                 case DB_DATA_TYPE_INTEGER:
                     // Can be long or short
                     if (pColInfs[index].bufferSize == sizeof(long))
@@ -2438,7 +2463,7 @@ ULONG wxDbTable::Count(const wxString &args)
 #if wxODBC_BACKWARD_COMPATABILITY
     if (from && wxStrlen(from))
 #else
 #if wxODBC_BACKWARD_COMPATABILITY
     if (from && wxStrlen(from))
 #else
-    if (from.Length())
+    if (from.length())
 #endif
         sqlStmt += from;
 
 #endif
         sqlStmt += from;
 
@@ -2446,7 +2471,7 @@ ULONG wxDbTable::Count(const wxString &args)
 #if wxODBC_BACKWARD_COMPATABILITY
     if (where && wxStrlen(where))
 #else
 #if wxODBC_BACKWARD_COMPATABILITY
     if (where && wxStrlen(where))
 #else
-    if (where.Length())
+    if (where.length())
 #endif
     {
         sqlStmt += wxT(" WHERE ");
 #endif
     {
         sqlStmt += wxT(" WHERE ");
@@ -2904,4 +2929,3 @@ void wxDbTable::SetKey(const GenericKey& k)
 
 
 #endif  // wxUSE_ODBC
 
 
 #endif  // wxUSE_ODBC
-