]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/dbtable.cpp
wxControl::SetLabel no longer strips out codes so do it here
[wxWidgets.git] / src / common / dbtable.cpp
index da7aaa8f60d9cbdd35e2467ef5bd2c21a8ee07d4..0e781f19a36f8bf6be5a3e662cf6f12f7cbfeb25 100644 (file)
 // SYNOPSIS STOP
 */
 
 // SYNOPSIS STOP
 */
 
-#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
-    #pragma implementation "dbtable.h"
-#endif
-
 #include  "wx/wxprec.h"
 
 #ifdef __BORLANDC__
 #include  "wx/wxprec.h"
 
 #ifdef __BORLANDC__
@@ -417,11 +413,13 @@ 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
                 colDefs[columnIndex].CbValue = SQL_NTS;
             break;
             if (colDefs[columnIndex].Null)
                 colDefs[columnIndex].CbValue = SQL_NULL_DATA;
             else
                 colDefs[columnIndex].CbValue = SQL_NTS;
             break;
+            break;
         case DB_DATA_TYPE_INTEGER:
             if (colDefs[columnIndex].Null)
                 colDefs[columnIndex].CbValue = SQL_NULL_DATA;
         case DB_DATA_TYPE_INTEGER:
             if (colDefs[columnIndex].Null)
                 colDefs[columnIndex].CbValue = SQL_NULL_DATA;
@@ -488,6 +486,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;
@@ -560,7 +563,7 @@ bool wxDbTable::bindUpdateParams(void)
 /********** wxDbTable::bindCols() **********/
 bool wxDbTable::bindCols(HSTMT cursor)
 {
 /********** wxDbTable::bindCols() **********/
 bool wxDbTable::bindCols(HSTMT cursor)
 {
-    static SDWORD cb;
+    static SQLLEN cb;
 
     // Bind each column of the table to a memory address for fetching data
     UWORD i;
 
     // Bind each column of the table to a memory address for fetching data
     UWORD i;
@@ -586,7 +589,7 @@ bool wxDbTable::getRec(UWORD fetchType)
     if (!pDb->FwdOnlyCursors())
     {
         // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
     if (!pDb->FwdOnlyCursors())
     {
         // Fetch the NEXT, PREV, FIRST or LAST record, depending on fetchType
-        UDWORD  cRowsFetched;
+        SQLULEN cRowsFetched;
         UWORD   rowStatus;
 
         retcode = SQLExtendedFetch(hstmt, fetchType, 0, &cRowsFetched, &rowStatus);
         UWORD   rowStatus;
 
         retcode = SQLExtendedFetch(hstmt, fetchType, 0, &cRowsFetched, &rowStatus);
@@ -755,7 +758,6 @@ bool wxDbTable::Open(bool checkPrivileges, bool checkTableExists)
     int i;
     wxString sqlStmt;
     wxString s;
     int i;
     wxString sqlStmt;
     wxString s;
-//    int NumKeyCols=0;
 
     // Calculate the maximum size of the concatenated
     // keys for use with wxDbGrid
 
     // Calculate the maximum size of the concatenated
     // keys for use with wxDbGrid
@@ -764,14 +766,23 @@ bool wxDbTable::Open(bool checkPrivileges, bool checkTableExists)
     {
         if (colDefs[i].KeyField)
         {
     {
         if (colDefs[i].KeyField)
         {
-//            NumKeyCols++;
             m_keysize += colDefs[i].SzDataObj;
         }
     }
 
     s.Empty();
             m_keysize += colDefs[i].SzDataObj;
         }
     }
 
     s.Empty();
+
+    bool exists = true;
+    if (checkTableExists)
+    {
+        if (pDb->Dbms() == dbmsPOSTGRES)
+            exists = pDb->TableExists(tableName, NULL, tablePath);
+        else
+            exists = pDb->TableExists(tableName, pDb->GetUsername(), tablePath);
+    }
+
     // Verify that the table exists in the database
     // Verify that the table exists in the database
-    if (checkTableExists && !pDb->TableExists(tableName, pDb->GetUsername(), tablePath))
+    if (!exists)
     {
         s = wxT("Table/view does not exist in the database");
         if ( *(pDb->dbInf.accessibleTables) == wxT('Y'))
     {
         s = wxT("Table/view does not exist in the database");
         if ( *(pDb->dbInf.accessibleTables) == wxT('Y'))
@@ -782,21 +793,22 @@ bool wxDbTable::Open(bool checkPrivileges, bool checkTableExists)
     else if (checkPrivileges)
     {
         // Verify the user has rights to access the table.
     else if (checkPrivileges)
     {
         // Verify the user has rights to access the table.
-        // Shortcut boolean evaluation to optimize out call to
-        // TablePrivileges
-        //
-        // Unfortunately this optimization doesn't seem to be
-        // reliable!
-        if (// *(pDb->dbInf.accessibleTables) == 'N' &&
-            !pDb->TablePrivileges(tableName, wxT("SELECT"), pDb->GetUsername(), pDb->GetUsername(), tablePath))
+        bool hasPrivs wxDUMMY_INITIALIZE(true);
+
+        if (pDb->Dbms() == dbmsPOSTGRES)
+            hasPrivs = pDb->TablePrivileges(tableName, wxT("SELECT"), pDb->GetUsername(), NULL, tablePath);
+        else
+            hasPrivs = pDb->TablePrivileges(tableName, wxT("SELECT"), pDb->GetUsername(), pDb->GetUsername(), tablePath);
+
+        if (!hasPrivs)
             s = wxT("Connecting user does not have sufficient privileges to access this table.\n");
     }
 
             s = wxT("Connecting user does not have sufficient privileges to access this table.\n");
     }
 
-    if (!s.IsEmpty())
+    if (!s.empty())
     {
         wxString p;
 
     {
         wxString p;
 
-        if (!tablePath.IsEmpty())
+        if (!tablePath.empty())
             p.Printf(wxT("Error opening '%s/%s'.\n"),tablePath.c_str(),tableName.c_str());
         else
             p.Printf(wxT("Error opening '%s'.\n"), tableName.c_str());
             p.Printf(wxT("Error opening '%s/%s'.\n"),tablePath.c_str(),tableName.c_str());
         else
             p.Printf(wxT("Error opening '%s'.\n"), tableName.c_str());
@@ -841,7 +853,6 @@ bool wxDbTable::Open(bool checkPrivileges, bool checkTableExists)
             if (needComma)
                 sqlStmt += wxT(",");
             sqlStmt += pDb->SQLColumnName(colDefs[i].ColName);
             if (needComma)
                 sqlStmt += wxT(",");
             sqlStmt += pDb->SQLColumnName(colDefs[i].ColName);
-//            sqlStmt += colDefs[i].ColName;
             needComma = true;
         }
         needComma = false;
             needComma = true;
         }
         needComma = false;
@@ -1002,7 +1013,7 @@ void wxDbTable::BuildDeleteStmt(wxString &pSqlStmt, int typeOfDel, const wxStrin
             // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
             if (CanUpdateByROWID())
             {
             // e.g. DELETE FROM PARTS WHERE ROWID = '111.222.333'
             if (CanUpdateByROWID())
             {
-                SDWORD cb;
+                SQLLEN cb;
                 wxChar   rowid[wxDB_ROWID_LEN+1];
 
                 // Get the ROWID value.  If not successful retreiving the ROWID,
                 wxChar   rowid[wxDB_ROWID_LEN+1];
 
                 // Get the ROWID value.  If not successful retreiving the ROWID,
@@ -1092,7 +1103,6 @@ void wxDbTable::BuildSelectStmt(wxString &pSqlStmt, int typeOfSelect, bool disti
         {
             pSqlStmt += wxT(",");
             pSqlStmt += pDb->SQLTableName(queryTableName);
         {
             pSqlStmt += wxT(",");
             pSqlStmt += pDb->SQLTableName(queryTableName);
-//            pSqlStmt += queryTableName;
             pSqlStmt += wxT(".ROWID");
         }
         else
             pSqlStmt += wxT(".ROWID");
         }
         else
@@ -1220,7 +1230,7 @@ void wxDbTable::BuildUpdateStmt(wxString &pSqlStmt, int typeOfUpdate, const wxSt
             // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
             if (CanUpdateByROWID())
             {
             // e.g. UPDATE PARTS SET Col1 = ?, Col2 = ? WHERE ROWID = '111.222.333'
             if (CanUpdateByROWID())
             {
-                SDWORD cb;
+                SQLLEN cb;
                 wxChar rowid[wxDB_ROWID_LEN+1];
 
                 // Get the ROWID value.  If not successful retreiving the ROWID,
                 wxChar rowid[wxDB_ROWID_LEN+1];
 
                 // Get the ROWID value.  If not successful retreiving the ROWID,
@@ -1301,9 +1311,9 @@ void wxDbTable::BuildWhereClause(wxString &pWhereClause, int typeOfWhere,
             switch(colDefs[colNumber].SqlCtype)
             {
                 case SQL_C_CHAR:
             switch(colDefs[colNumber].SqlCtype)
             {
                 case SQL_C_CHAR:
-#ifndef __UNIX__
+#ifdef SQL_C_WCHAR
                 case SQL_C_WCHAR:
                 case SQL_C_WCHAR:
-#endif                 
+#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);
                     break;
                 //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);
                     break;
@@ -1411,6 +1421,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;
@@ -1452,6 +1465,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;
@@ -1846,6 +1862,7 @@ bool wxDbTable::DropIndex(const wxString &indexName)
                   (pDb->Dbms() == dbmsSYBASE_ASE    && !wxStrcmp(pDb->sqlState,wxT("37000"))) ||
                   (pDb->Dbms() == dbmsMS_SQL_SERVER && !wxStrcmp(pDb->sqlState,wxT("S1000"))) ||
                   (pDb->Dbms() == dbmsINTERBASE     && !wxStrcmp(pDb->sqlState,wxT("S1000"))) ||
                   (pDb->Dbms() == dbmsSYBASE_ASE    && !wxStrcmp(pDb->sqlState,wxT("37000"))) ||
                   (pDb->Dbms() == dbmsMS_SQL_SERVER && !wxStrcmp(pDb->sqlState,wxT("S1000"))) ||
                   (pDb->Dbms() == dbmsINTERBASE     && !wxStrcmp(pDb->sqlState,wxT("S1000"))) ||
+                  (pDb->Dbms() == dbmsMAXDB         && !wxStrcmp(pDb->sqlState,wxT("S1000"))) ||
                   (pDb->Dbms() == dbmsFIREBIRD      && !wxStrcmp(pDb->sqlState,wxT("HY000"))) ||
                   (pDb->Dbms() == dbmsSYBASE_ASE    && !wxStrcmp(pDb->sqlState,wxT("S0002"))) ||  // Base table not found
                   (pDb->Dbms() == dbmsMY_SQL        && !wxStrcmp(pDb->sqlState,wxT("42S12"))) ||  // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
                   (pDb->Dbms() == dbmsFIREBIRD      && !wxStrcmp(pDb->sqlState,wxT("HY000"))) ||
                   (pDb->Dbms() == dbmsSYBASE_ASE    && !wxStrcmp(pDb->sqlState,wxT("S0002"))) ||  // Base table not found
                   (pDb->Dbms() == dbmsMY_SQL        && !wxStrcmp(pDb->sqlState,wxT("42S12"))) ||  // tested by Christopher Ludwik Marino-Cebulski using v3.23.21beta
@@ -2194,7 +2211,7 @@ void wxDbTable::ClearMemberVar(UWORD colNumber, bool setToNull)
     switch(colDefs[colNumber].SqlCtype)
     {
         case SQL_C_CHAR:
     switch(colDefs[colNumber].SqlCtype)
     {
         case SQL_C_CHAR:
-#ifndef __UNIX__
+#ifdef 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
         case SQL_C_WCHAR:
 #endif
         //case SQL_C_WXCHAR:  SQL_C_WXCHAR is covered by either SQL_C_CHAR or SQL_C_WCHAR
@@ -2345,6 +2362,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))
@@ -2423,7 +2445,7 @@ ULONG wxDbTable::Count(const wxString &args)
 {
     ULONG count;
     wxString sqlStmt;
 {
     ULONG count;
     wxString sqlStmt;
-    SDWORD cb;
+    SQLLEN cb;
 
     // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
     sqlStmt  = wxT("SELECT COUNT(");
 
     // Build a "SELECT COUNT(*) FROM queryTableName [WHERE whereClause]" SQL Statement
     sqlStmt  = wxT("SELECT COUNT(");
@@ -2514,7 +2536,7 @@ bool wxDbTable::Refresh(void)
 
     if (CanUpdateByROWID())
     {
 
     if (CanUpdateByROWID())
     {
-        SDWORD cb;
+        SQLLEN cb;
         wxChar rowid[wxDB_ROWID_LEN+1];
 
         // Get the ROWID value.  If not successful retreiving the ROWID,
         wxChar rowid[wxDB_ROWID_LEN+1];
 
         // Get the ROWID value.  If not successful retreiving the ROWID,
@@ -2703,6 +2725,14 @@ wxVariant wxDbTable::GetColumn(const int colNumber) const
     {
         switch (colDefs[colNumber].SqlCtype)
         {
     {
         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);
             case SQL_CHAR:
             case SQL_VARCHAR:
                 val = (wxChar *)(colDefs[colNumber].PtrDataObj);
@@ -2767,6 +2797,14 @@ void wxDbTable::SetColumn(const int colNumber, const wxVariant val)
 
         switch (colDefs[colNumber].SqlCtype)
         {
 
         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),
             case SQL_CHAR:
             case SQL_VARCHAR:
                 csstrncpyt((wxChar *)(colDefs[colNumber].PtrDataObj),