From: Julian Smart Date: Mon, 9 Dec 2002 10:50:14 +0000 (+0000) Subject: Applied patch [ 649284 ] Fix wxODBC to work with MySQL 2.x X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/9c136858af7a37b1ed7d5f036f8d6953f7c8d507 Applied patch [ 649284 ] Fix wxODBC to work with MySQL 2.x The current sample/db fails to build indexes with MyODBC v2.50.39 This is due to two problems: (1) wxDb::ModifyColumn() created a SQL statement that was not correctly formed for MySQL. This problem caused wxDbTable::CreateIndex() to fail to set the columns that are going to indexes to be NOT NULL. I fixed this by added a special case for MySQL. (2) When creating an Index on a VARCHAR column, MySQL requires a key length to be specified. wxDbTable::CreateIndex() current does not do this. I fixed this problem by adding code to do this. The attached patch file patches dbtable.cpp and db.cpp on the 2.5 branch. gor Mikolic-Torreira git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@18145 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/common/db.cpp b/src/common/db.cpp index 1d014c2ced..08c40f003d 100644 --- a/src/common/db.cpp +++ b/src/common/db.cpp @@ -3576,8 +3576,16 @@ bool wxDb::ModifyColumn(const wxString &tableName, const wxString &columnName, } // create the SQL statement - sqlStmt.Printf(wxT("ALTER TABLE \"%s\" \"%s\" \"%s\" %s"), tableName.c_str(), alterSlashModify.c_str(), + if ( Dbms() == dbmsMY_SQL ) + { + sqlStmt.Printf(wxT("ALTER TABLE %s %s %s %s"), tableName.c_str(), alterSlashModify.c_str(), + columnName.c_str(), dataTypeName.c_str()); + } + else + { + sqlStmt.Printf(wxT("ALTER TABLE \"%s\" \"%s\" \"%s\" %s"), tableName.c_str(), alterSlashModify.c_str(), columnName.c_str(), dataTypeName.c_str()); + } // For varchars only, append the size of the column if (dataType == DB_DATA_TYPE_VARCHAR && diff --git a/src/common/dbtable.cpp b/src/common/dbtable.cpp index 879a4dd38d..384310ce8c 100644 --- a/src/common/dbtable.cpp +++ b/src/common/dbtable.cpp @@ -1651,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))