+ {
+ sqlStmt += wxT(" WHERE ");
+ sqlStmt += where;
+ }
+
+ pDb->WriteSqlLog(sqlStmt);
+
+ // Initialize the Count cursor if it's not already initialized
+ if (!hstmtCount)
+ {
+ hstmtCount = GetNewCursor(false,false);
+ wxASSERT(hstmtCount);
+ if (!hstmtCount)
+ return(0);
+ }
+
+ // Execute the SQL statement
+ if (SQLExecDirect(*hstmtCount, (SQLTCHAR FAR *) sqlStmt.c_str(), SQL_NTS) != SQL_SUCCESS)
+ {
+ pDb->DispAllErrors(henv, hdbc, *hstmtCount);
+ return(0);
+ }
+
+ // Fetch the record
+ if (SQLFetch(*hstmtCount) != SQL_SUCCESS)
+ {
+ pDb->DispAllErrors(henv, hdbc, *hstmtCount);
+ return(0);
+ }
+
+ // Obtain the result
+ if (SQLGetData(*hstmtCount, (UWORD)1, SQL_C_ULONG, &count, sizeof(count), &cb) != SQL_SUCCESS)
+ {
+ pDb->DispAllErrors(henv, hdbc, *hstmtCount);
+ return(0);
+ }
+
+ // Free the cursor
+ if (SQLFreeStmt(*hstmtCount, SQL_CLOSE) != SQL_SUCCESS)
+ pDb->DispAllErrors(henv, hdbc, *hstmtCount);
+
+ // Return the record count
+ return(count);
+
+} // wxDbTable::Count()
+
+
+/********** wxDbTable::Refresh() **********/
+bool wxDbTable::Refresh(void)
+{
+ bool result = true;
+
+ // Switch to the internal cursor so any active cursors are not corrupted
+ HSTMT currCursor = GetCursor();
+ hstmt = hstmtInternal;
+#if wxODBC_BACKWARD_COMPATABILITY
+ // Save the where and order by clauses
+ wxChar *saveWhere = where;
+ wxChar *saveOrderBy = orderBy;
+#else
+ wxString saveWhere = where;
+ wxString saveOrderBy = orderBy;
+#endif
+ // Build a where clause to refetch the record with. Try and use the
+ // ROWID if it's available, ow use the key fields.
+ wxString whereClause;
+ whereClause.Empty();
+
+ if (CanUpdateByROWID())
+ {
+ SDWORD cb;
+ wxChar rowid[wxDB_ROWID_LEN+1];
+
+ // Get the ROWID value. If not successful retreiving the ROWID,
+ // simply fall down through the code and build the WHERE clause
+ // based on the key fields.
+ if (SQLGetData(hstmt, (UWORD)(m_numCols+1), SQL_C_WXCHAR, (UCHAR*) rowid, sizeof(rowid), &cb) == SQL_SUCCESS)
+ {
+ whereClause += pDb->SQLTableName(queryTableName);
+// whereClause += queryTableName;
+ whereClause += wxT(".ROWID = '");
+ whereClause += rowid;
+ whereClause += wxT("'");
+ }
+ }
+
+ // If unable to use the ROWID, build a where clause from the keyfields
+ if (wxStrlen(whereClause) == 0)
+ BuildWhereClause(whereClause, DB_WHERE_KEYFIELDS, queryTableName);
+
+ // Requery the record
+ where = whereClause;
+ orderBy.Empty();
+ if (!Query())
+ result = false;
+
+ if (result && !GetNext())
+ result = false;
+
+ // Switch back to original cursor
+ SetCursor(&currCursor);
+
+ // Free the internal cursor
+ if (SQLFreeStmt(hstmtInternal, SQL_CLOSE) != SQL_SUCCESS)
+ pDb->DispAllErrors(henv, hdbc, hstmtInternal);
+
+ // Restore the original where and order by clauses
+ where = saveWhere;
+ orderBy = saveOrderBy;
+
+ return(result);
+
+} // wxDbTable::Refresh()
+
+
+/********** wxDbTable::SetColNull() **********/
+bool wxDbTable::SetColNull(UWORD colNumber, bool set)
+{
+ if (colNumber < m_numCols)
+ {
+ colDefs[colNumber].Null = set;
+ if (set) // Blank out the values in the member variable
+ ClearMemberVar(colNumber, false); // Must call with false here, or infinite recursion will happen
+
+ setCbValueForColumn(colNumber);
+
+ return true;
+ }
+ else
+ return false;
+
+} // wxDbTable::SetColNull()
+
+
+/********** wxDbTable::SetColNull() **********/
+bool wxDbTable::SetColNull(const wxString &colName, bool set)
+{
+ int colNumber;
+ for (colNumber = 0; colNumber < m_numCols; colNumber++)
+ {
+ if (!wxStricmp(colName, colDefs[colNumber].ColName))
+ break;
+ }
+
+ if (colNumber < m_numCols)
+ {
+ colDefs[colNumber].Null = set;
+ if (set) // Blank out the values in the member variable
+ ClearMemberVar((UWORD)colNumber,false); // Must call with false here, or infinite recursion will happen
+
+ setCbValueForColumn(colNumber);
+
+ return true;
+ }
+ else
+ return false;
+
+} // wxDbTable::SetColNull()
+
+
+/********** wxDbTable::GetNewCursor() **********/
+HSTMT *wxDbTable::GetNewCursor(bool setCursor, bool bindColumns)
+{
+ HSTMT *newHSTMT = new HSTMT;
+ wxASSERT(newHSTMT);
+ if (!newHSTMT)
+ return(0);
+
+ if (SQLAllocStmt(hdbc, newHSTMT) != SQL_SUCCESS)
+ {
+ pDb->DispAllErrors(henv, hdbc);
+ delete newHSTMT;
+ return(0);
+ }
+
+ if (SQLSetStmtOption(*newHSTMT, SQL_CURSOR_TYPE, cursorType) != SQL_SUCCESS)
+ {
+ pDb->DispAllErrors(henv, hdbc, *newHSTMT);
+ delete newHSTMT;
+ return(0);
+ }
+
+ if (bindColumns)
+ {
+ if (!bindCols(*newHSTMT))
+ {
+ delete newHSTMT;
+ return(0);
+ }
+ }
+
+ if (setCursor)
+ SetCursor(newHSTMT);
+
+ return(newHSTMT);
+
+} // wxDbTable::GetNewCursor()
+
+
+/********** wxDbTable::DeleteCursor() **********/
+bool wxDbTable::DeleteCursor(HSTMT *hstmtDel)
+{
+ bool result = true;
+
+ 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);
+ result = false;
+ }
+
+ delete hstmtDel;
+
+ return(result);
+
+} // wxDbTable::DeleteCursor()
+
+//////////////////////////////////////////////////////////////
+// wxDbGrid support functions
+//////////////////////////////////////////////////////////////
+
+void wxDbTable::SetRowMode(const rowmode_t rowmode)
+{
+ if (!m_hstmtGridQuery)
+ {
+ m_hstmtGridQuery = GetNewCursor(false,false);
+ if (!bindCols(*m_hstmtGridQuery))
+ return;
+ }
+
+ m_rowmode = rowmode;
+ switch (m_rowmode)
+ {
+ case WX_ROW_MODE_QUERY:
+ SetCursor(m_hstmtGridQuery);
+ break;
+ case WX_ROW_MODE_INDIVIDUAL:
+ SetCursor(hstmtDefault);
+ break;
+ default:
+ wxASSERT(0);
+ }
+} // wxDbTable::SetRowMode()
+
+
+wxVariant wxDbTable::GetColumn(const int colNumber) const
+{
+ wxVariant val;
+ if ((colNumber < m_numCols) && (!IsColNull((UWORD)colNumber)))
+ {
+ switch (colDefs[colNumber].SqlCtype)
+ {
+#if wxUSE_UNICODE
+ #if defined(SQL_WCHAR)
+ case SQL_WCHAR:
+ #endif
+ #if defined(SQL_WVARCHAR)
+ case SQL_WVARCHAR:
+ #endif
+#endif
+ case SQL_CHAR:
+ case SQL_VARCHAR:
+ val = (wxChar *)(colDefs[colNumber].PtrDataObj);
+ break;
+ case SQL_C_LONG:
+ case SQL_C_SLONG:
+ val = *(long *)(colDefs[colNumber].PtrDataObj);
+ break;
+ case SQL_C_SHORT:
+ case SQL_C_SSHORT:
+ val = (long int )(*(short *)(colDefs[colNumber].PtrDataObj));
+ break;
+ case SQL_C_ULONG:
+ val = (long)(*(unsigned long *)(colDefs[colNumber].PtrDataObj));
+ break;
+ case SQL_C_TINYINT:
+ val = (long)(*(wxChar *)(colDefs[colNumber].PtrDataObj));
+ break;
+ case SQL_C_UTINYINT:
+ val = (long)(*(wxChar *)(colDefs[colNumber].PtrDataObj));
+ break;
+ case SQL_C_USHORT:
+ val = (long)(*(UWORD *)(colDefs[colNumber].PtrDataObj));
+ break;
+ case SQL_C_DATE:
+ val = (DATE_STRUCT *)(colDefs[colNumber].PtrDataObj);
+ break;
+ case SQL_C_TIME:
+ val = (TIME_STRUCT *)(colDefs[colNumber].PtrDataObj);
+ break;
+ case SQL_C_TIMESTAMP:
+ val = (TIMESTAMP_STRUCT *)(colDefs[colNumber].PtrDataObj);
+ break;
+ case SQL_C_DOUBLE:
+ val = *(double *)(colDefs[colNumber].PtrDataObj);
+ break;
+ default:
+ assert(0);
+ }
+ }
+ return val;
+} // wxDbTable::GetCol()
+
+
+void wxDbTable::SetColumn(const int colNumber, const wxVariant val)
+{
+ //FIXME: Add proper wxDateTime support to wxVariant..
+ wxDateTime dateval;
+
+ SetColNull((UWORD)colNumber, val.IsNull());
+
+ if (!val.IsNull())
+ {
+ if ((colDefs[colNumber].SqlCtype == SQL_C_DATE)
+ || (colDefs[colNumber].SqlCtype == SQL_C_TIME)
+ || (colDefs[colNumber].SqlCtype == SQL_C_TIMESTAMP))
+ {
+ //Returns null if invalid!
+ if (!dateval.ParseDate(val.GetString()))
+ SetColNull((UWORD)colNumber, true);
+ }
+
+ switch (colDefs[colNumber].SqlCtype)
+ {
+#if wxUSE_UNICODE
+ #if defined(SQL_WCHAR)
+ case SQL_WCHAR:
+ #endif
+ #if defined(SQL_WVARCHAR)
+ case SQL_WVARCHAR:
+ #endif
+#endif
+ case SQL_CHAR:
+ case SQL_VARCHAR:
+ csstrncpyt((wxChar *)(colDefs[colNumber].PtrDataObj),
+ val.GetString().c_str(),
+ colDefs[colNumber].SzDataObj-1); //TODO: glt ??? * sizeof(wxChar) ???
+ break;
+ case SQL_C_LONG:
+ case SQL_C_SLONG:
+ *(long *)(colDefs[colNumber].PtrDataObj) = val;
+ break;
+ case SQL_C_SHORT:
+ case SQL_C_SSHORT:
+ *(short *)(colDefs[colNumber].PtrDataObj) = (short)val.GetLong();
+ break;
+ case SQL_C_ULONG:
+ *(unsigned long *)(colDefs[colNumber].PtrDataObj) = val.GetLong();
+ break;
+ case SQL_C_TINYINT:
+ *(wxChar *)(colDefs[colNumber].PtrDataObj) = val.GetChar();
+ break;
+ case SQL_C_UTINYINT:
+ *(wxChar *)(colDefs[colNumber].PtrDataObj) = val.GetChar();
+ break;
+ case SQL_C_USHORT:
+ *(unsigned short *)(colDefs[colNumber].PtrDataObj) = (unsigned short)val.GetLong();
+ break;
+ //FIXME: Add proper wxDateTime support to wxVariant..
+ case SQL_C_DATE:
+ {
+ DATE_STRUCT *dataptr =
+ (DATE_STRUCT *)colDefs[colNumber].PtrDataObj;
+
+ dataptr->year = (SWORD)dateval.GetYear();
+ dataptr->month = (UWORD)(dateval.GetMonth()+1);
+ dataptr->day = (UWORD)dateval.GetDay();
+ }
+ break;
+ case SQL_C_TIME:
+ {
+ TIME_STRUCT *dataptr =
+ (TIME_STRUCT *)colDefs[colNumber].PtrDataObj;
+
+ dataptr->hour = dateval.GetHour();
+ dataptr->minute = dateval.GetMinute();
+ dataptr->second = dateval.GetSecond();
+ }
+ break;
+ case SQL_C_TIMESTAMP:
+ {
+ TIMESTAMP_STRUCT *dataptr =
+ (TIMESTAMP_STRUCT *)colDefs[colNumber].PtrDataObj;
+ dataptr->year = (SWORD)dateval.GetYear();
+ dataptr->month = (UWORD)(dateval.GetMonth()+1);
+ dataptr->day = (UWORD)dateval.GetDay();
+
+ dataptr->hour = dateval.GetHour();
+ dataptr->minute = dateval.GetMinute();
+ dataptr->second = dateval.GetSecond();
+ }
+ break;
+ case SQL_C_DOUBLE:
+ *(double *)(colDefs[colNumber].PtrDataObj) = val;
+ break;
+ default:
+ assert(0);
+ } // switch
+ } // if (!val.IsNull())
+} // wxDbTable::SetCol()
+
+
+GenericKey wxDbTable::GetKey()
+{
+ void *blk;
+ wxChar *blkptr;
+
+ blk = malloc(m_keysize);
+ blkptr = (wxChar *) blk;
+
+ int i;
+ for (i=0; i < m_numCols; i++)
+ {
+ if (colDefs[i].KeyField)
+ {
+ memcpy(blkptr,colDefs[i].PtrDataObj, colDefs[i].SzDataObj);
+ blkptr += colDefs[i].SzDataObj;
+ }
+ }
+
+ GenericKey k = GenericKey(blk, m_keysize);
+ free(blk);
+
+ return k;
+} // wxDbTable::GetKey()
+
+
+void wxDbTable::SetKey(const GenericKey& k)
+{
+ void *blk;
+ wxChar *blkptr;
+
+ blk = k.GetBlk();
+ blkptr = (wxChar *)blk;
+
+ int i;
+ for (i=0; i < m_numCols; i++)
+ {
+ if (colDefs[i].KeyField)
+ {
+ SetColNull((UWORD)i, false);
+ memcpy(colDefs[i].PtrDataObj, blkptr, colDefs[i].SzDataObj);
+ blkptr += colDefs[i].SzDataObj;
+ }
+ }
+} // wxDbTable::SetKey()
+
+
+#endif // wxUSE_ODBC
+