X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1dee6b399299241ffef19a61717bcb18d7a855d5..878711c01c1b9ad5b97d35f379a048b8ce1bfb49:/src/common/dbtable.cpp diff --git a/src/common/dbtable.cpp b/src/common/dbtable.cpp index 7ec54cbc70..384310ce8c 100644 --- a/src/common/dbtable.cpp +++ b/src/common/dbtable.cpp @@ -35,7 +35,11 @@ #endif #ifdef DBDEBUG_CONSOLE - #include "iostream.h" +#if wxUSE_IOSTREAMH + #include +#else + #include +#endif #include "wx/ioswrap.h" #endif @@ -524,15 +528,16 @@ bool wxDbTable::bindUpdateParams(void) /********** wxDbTable::bindCols() **********/ bool wxDbTable::bindCols(HSTMT cursor) { + static SDWORD cb; + // Bind each column of the table to a memory address for fetching data UWORD i; for (i = 0; i < noCols; i++) { + cb = colDefs[i].CbValue; if (SQLBindCol(cursor, (UWORD)(i+1), colDefs[i].SqlCtype, (UCHAR*) colDefs[i].PtrDataObj, - colDefs[i].SzDataObj, &colDefs[i].CbValue ) != SQL_SUCCESS) - { + colDefs[i].SzDataObj, &cb ) != SQL_SUCCESS) return (pDb->DispAllErrors(henv, hdbc, cursor)); - } } // Completed successfully @@ -763,7 +768,8 @@ bool wxDbTable::Open(bool checkPrivileges, bool checkTableExists) if (!queryOnly && noCols > 0) { bool needComma = FALSE; - sqlStmt.Printf(wxT("INSERT INTO %s ("), pDb->SQLTableName(tableName.c_str())); + sqlStmt.Printf(wxT("INSERT INTO %s ("), + pDb->SQLTableName(tableName.c_str()).c_str()); for (i = 0; i < noCols; i++) { if (! colDefs[i].InsertAllowed) @@ -915,11 +921,13 @@ void wxDbTable::BuildDeleteStmt(wxString &pSqlStmt, int typeOfDel, const wxStrin // delete all records from the database in this case. if (typeOfDel == DB_DEL_WHERE && (pWhereClause.Length() == 0)) { - pSqlStmt.Printf(wxT("DELETE FROM %s"), pDb->SQLTableName(tableName.c_str())); + pSqlStmt.Printf(wxT("DELETE FROM %s"), + pDb->SQLTableName(tableName.c_str()).c_str()); return; } - pSqlStmt.Printf(wxT("DELETE FROM %s WHERE "), pDb->SQLTableName(tableName.c_str())); + pSqlStmt.Printf(wxT("DELETE FROM %s WHERE "), + pDb->SQLTableName(tableName.c_str()).c_str()); // Append the WHERE clause to the SQL DELETE statement switch(typeOfDel) @@ -1117,7 +1125,8 @@ void wxDbTable::BuildUpdateStmt(wxString &pSqlStmt, int typeOfUpd, const wxStrin bool firstColumn = TRUE; - pSqlStmt.Printf(wxT("UPDATE %s SET "), pDb->SQLTableName(tableName.Upper().c_str())); + pSqlStmt.Printf(wxT("UPDATE %s SET "), + pDb->SQLTableName(tableName.c_str()).c_str()); // Append a list of columns to be updated int i; @@ -1341,7 +1350,8 @@ bool wxDbTable::CreateTable(bool attemptDrop) // Build a CREATE TABLE string from the colDefs structure. bool needComma = FALSE; - sqlStmt.Printf(wxT("CREATE TABLE %s ("), pDb->SQLTableName(tableName.c_str())); + sqlStmt.Printf(wxT("CREATE TABLE %s ("), + pDb->SQLTableName(tableName.c_str()).c_str()); for (i = 0; i < noCols; i++) { @@ -1375,7 +1385,8 @@ bool wxDbTable::CreateTable(bool attemptDrop) break; } // For varchars, append the size of the string - if (colDefs[i].DbDataType == DB_DATA_TYPE_VARCHAR)// || + if (colDefs[i].DbDataType == DB_DATA_TYPE_VARCHAR && + (pDb->Dbms() != dbmsMY_SQL || pDb->GetTypeInfVarchar().TypeName != "text"))// || // colDefs[i].DbDataType == DB_DATA_TYPE_BLOB) { wxString s; @@ -1406,7 +1417,9 @@ bool wxDbTable::CreateTable(bool attemptDrop) break; } } - if (j && pDb->Dbms() != dbmsDBASE) // Found a keyfield + if (j && (pDb->Dbms() != dbmsDBASE) + && (pDb->Dbms() != dbmsXBASE_SEQUITER) + ) // Found a keyfield { switch (pDb->Dbms()) { @@ -1447,7 +1460,14 @@ bool wxDbTable::CreateTable(bool attemptDrop) if (j++) // Multi part key, comma separate names sqlStmt += wxT(","); sqlStmt += pDb->SQLColumnName(colDefs[i].ColName); -// sqlStmt += colDefs[i].ColName; + + if (pDb->Dbms() == dbmsMY_SQL && + colDefs[i].DbDataType == DB_DATA_TYPE_VARCHAR) + { + wxString s; + s.Printf(wxT("(%d)"), colDefs[i].SzDataObj); + sqlStmt += s; + } } } sqlStmt += wxT(")"); @@ -1503,7 +1523,8 @@ bool wxDbTable::DropTable() wxString sqlStmt; - sqlStmt.Printf(wxT("DROP TABLE %s"), pDb->SQLTableName(tableName.c_str())); + sqlStmt.Printf(wxT("DROP TABLE %s"), + pDb->SQLTableName(tableName.c_str()).c_str()); pDb->WriteSqlLog(sqlStmt); @@ -1630,6 +1651,26 @@ bool wxDbTable::CreateIndex(const wxString &idxName, bool unique, UWORD noIdxCol sqlStmt += pDb->SQLColumnName(pIdxDefs[i].ColName); // sqlStmt += pIdxDefs[i].ColName; + // MySQL requires a key length on VARCHAR keys + if ( pDb->Dbms() == dbmsMY_SQL ) + { + // Find the details on this column + int j; + for ( j = 0; j < noCols; ++j ) + { + if ( wxStrcmp( pIdxDefs[i].ColName, colDefs[j].ColName ) == 0 ) + { + break; + } + } + if ( colDefs[j].DbDataType == DB_DATA_TYPE_VARCHAR) + { + wxString s; + s.Printf(wxT("(%d)"), colDefs[i].SzDataObj); + sqlStmt += s; + } + } + // 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)) && !(pDb->Dbms() == dbmsPOSTGRES)) @@ -1688,12 +1729,18 @@ bool wxDbTable::DropIndex(const wxString &idxName) if (pDb->Dbms() == dbmsACCESS || pDb->Dbms() == dbmsMY_SQL || pDb->Dbms() == dbmsDBASE /*|| Paradox needs this syntax too when we add support*/) - sqlStmt.Printf(wxT("DROP INDEX %s ON %s"),pDb->SQLTableName(idxName.c_str()), pDb->SQLTableName(tableName.c_str())); + sqlStmt.Printf(wxT("DROP INDEX %s ON %s"), + pDb->SQLTableName(idxName.c_str()).c_str(), + pDb->SQLTableName(tableName.c_str()).c_str()); else if ((pDb->Dbms() == dbmsMS_SQL_SERVER) || - (pDb->Dbms() == dbmsSYBASE_ASE)) - sqlStmt.Printf(wxT("DROP INDEX %s.%s"), pDb->SQLTableName(tableName.c_str()), pDb->SQLTableName(idxName.c_str())); + (pDb->Dbms() == dbmsSYBASE_ASE) || + (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()); else - sqlStmt.Printf(wxT("DROP INDEX %s"),pDb->SQLTableName(idxName.c_str())); + sqlStmt.Printf(wxT("DROP INDEX %s"), + pDb->SQLTableName(idxName.c_str()).c_str()); pDb->WriteSqlLog(sqlStmt); @@ -2095,6 +2142,9 @@ void wxDbTable::SetColDefs(UWORD index, const wxString &fieldName, int dataType, SWORD cType, int size, bool keyField, bool upd, bool insAllow, bool derivedCol) { + wxASSERT_MSG( index < noCols, + _T("Specified column index exceeds the maximum number of columns for this table.") ); + if (!colDefs) // May happen if the database connection fails return; @@ -2556,8 +2606,8 @@ wxVariant wxDbTable::GetCol(const int colNo) const void csstrncpyt(char *s, const char *t, int n) { - while ((*s++ = *t++) && --n) - {}; + while ( (*s++ = *t++) != '\0' && --n ) + ; *s = '\0'; }