X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e262868d430219d285e8a4d4aeaaa6fe4e6fde25..878711c01c1b9ad5b97d35f379a048b8ce1bfb49:/src/common/db.cpp diff --git a/src/common/db.cpp b/src/common/db.cpp index f7227632ff..08c40f003d 100644 --- a/src/common/db.cpp +++ b/src/common/db.cpp @@ -562,7 +562,8 @@ const wxChar *wxDb::convertUserID(const wxChar *userID, wxString &UserID) UserID.Empty(); // dBase does not use user names, and some drivers fail if you try to pass one - if (Dbms() == dbmsDBASE) + if ( Dbms() == dbmsDBASE + || Dbms() == dbmsXBASE_SEQUITER ) UserID.Empty(); // Oracle user names may only be in uppercase, so force @@ -575,7 +576,7 @@ const wxChar *wxDb::convertUserID(const wxChar *userID, wxString &UserID) /********** wxDb::Open() **********/ -bool wxDb::Open(const wxString &Dsn, const wxString &Uid, const wxString &AuthStr) +bool wxDb::Open(const wxString &Dsn, const wxString &Uid, const wxString &AuthStr, bool failOnDataTypeUnsupported) { wxASSERT(Dsn.Length()); dsn = Dsn; @@ -603,15 +604,6 @@ bool wxDb::Open(const wxString &Dsn, const wxString &Uid, const wxString &AuthSt (UCHAR FAR *) uid.c_str(), SQL_NTS, (UCHAR FAR *) authStr.c_str(), SQL_NTS); -/* - if (retcode == SQL_SUCCESS_WITH_INFO) - DispAllErrors(henv, hdbc); - else if (retcode != SQL_SUCCESS) - return(DispAllErrors(henv, hdbc)); - - if (retcode == SQL_ERROR) - return(DispAllErrors(henv, hdbc)); -*/ if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO)) return(DispAllErrors(henv, hdbc)); @@ -695,7 +687,10 @@ bool wxDb::Open(const wxString &Dsn, const wxString &Uid, const wxString &AuthSt if (!getDataTypeInfo(SQL_FLOAT,typeInfFloat)) if (!getDataTypeInfo(SQL_DECIMAL,typeInfFloat)) if (!getDataTypeInfo(SQL_NUMERIC,typeInfFloat)) - return(FALSE); + { + if (failOnDataTypeUnsupported) + return(FALSE); + } else typeInfFloat.FsqlType = SQL_NUMERIC; else @@ -713,7 +708,10 @@ bool wxDb::Open(const wxString &Dsn, const wxString &Uid, const wxString &AuthSt // If SQL_INTEGER is not supported, use the floating point // data type to store integers as well as floats if (!getDataTypeInfo(typeInfFloat.FsqlType, typeInfInteger)) - return(FALSE); + { + if (failOnDataTypeUnsupported) + return(FALSE); + } else typeInfInteger.FsqlType = typeInfFloat.FsqlType; } @@ -721,25 +719,36 @@ bool wxDb::Open(const wxString &Dsn, const wxString &Uid, const wxString &AuthSt typeInfInteger.FsqlType = SQL_INTEGER; // Date/Time - if (Dbms() != dbmsDBASE) - { - if (!getDataTypeInfo(SQL_TIMESTAMP,typeInfDate)) - return(FALSE); - else - typeInfDate.FsqlType = SQL_TIMESTAMP; - } - else + if (!getDataTypeInfo(SQL_TIMESTAMP,typeInfDate)) { if (!getDataTypeInfo(SQL_DATE,typeInfDate)) - return(FALSE); + { +#ifdef SQL_DATETIME + if (getDataTypeInfo(SQL_DATETIME,typeInfDate)) + { + typeInfDate.FsqlType = SQL_TIME; + } + else +#endif // SQL_DATETIME defined + { + if (failOnDataTypeUnsupported) + return(FALSE); + } + } else typeInfDate.FsqlType = SQL_DATE; } + else + typeInfDate.FsqlType = SQL_TIMESTAMP; + if (!getDataTypeInfo(SQL_LONGVARBINARY, typeInfBlob)) { if (!getDataTypeInfo(SQL_VARBINARY,typeInfBlob)) - return(FALSE); + { + if (failOnDataTypeUnsupported) + return(FALSE); + } else typeInfBlob.FsqlType = SQL_VARBINARY; } @@ -1295,8 +1304,10 @@ bool wxDb::getDataTypeInfo(SWORD fSqlType, wxDbSqlTypeInfo &structSQLTypeInfo) // Get information about the data type specified if (SQLGetTypeInfo(hstmt, fSqlType) != SQL_SUCCESS) return(DispAllErrors(henv, hdbc, hstmt)); + // Fetch the record - if ((retcode = SQLFetch(hstmt)) != SQL_SUCCESS) + retcode = SQLFetch(hstmt); + if (retcode != SQL_SUCCESS) { #ifdef DBDEBUG_CONSOLE if (retcode == SQL_NO_DATA_FOUND) @@ -1308,6 +1319,7 @@ bool wxDb::getDataTypeInfo(SWORD fSqlType, wxDbSqlTypeInfo &structSQLTypeInfo) } wxChar typeName[DB_TYPE_NAME_LEN+1]; + // Obtain columns from the record if (SQLGetData(hstmt, 1, SQL_C_CHAR, (UCHAR*) typeName, DB_TYPE_NAME_LEN, &cbRet) != SQL_SUCCESS) return(DispAllErrors(henv, hdbc, hstmt)); @@ -1784,7 +1796,7 @@ bool wxDb::Grant(int privileges, const wxString &tableName, const wxString &user } sqlStmt += wxT(" ON "); - sqlStmt += tableName; + sqlStmt += SQLTableName(tableName); sqlStmt += wxT(" TO "); sqlStmt += userList; @@ -2687,7 +2699,7 @@ wxDbColInf *wxDb::GetColumns(const wxString &tableName, int *numCols, const wxCh // Build a generic SELECT statement which returns 0 rows wxString Stmt; - Stmt.Printf(wxT("select * from %s where 0=1"), tableName); + Stmt.Printf(wxT("select * from \"%s\" where 0=1"), tableName); // Execute query if (SQLExecDirect(hstmt, (UCHAR FAR *) Stmt.c_str(), SQL_NTS) != SQL_SUCCESS) @@ -3022,7 +3034,7 @@ bool wxDb::Catalog(const wxChar *userID, const wxString &fileName) tblNameSave.Empty(); int cnt = 0; - while (true) + while (TRUE) { retcode = SQLFetch(hstmt); if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) @@ -3281,6 +3293,34 @@ bool wxDb::TablePrivileges(const wxString &tableName, const wxString &priv, cons } // wxDb::TablePrivileges +const wxString wxDb::SQLTableName(const wxChar *tableName) +{ + wxString TableName; + + if (Dbms() == dbmsACCESS) + TableName = '"'; + TableName += tableName; + if (Dbms() == dbmsACCESS) + TableName += '"'; + + return TableName; +} // wxDb::SQLTableName() + + +const wxString wxDb::SQLColumnName(const wxChar *colName) +{ + wxString ColName; + + if (Dbms() == dbmsACCESS) + ColName = '"'; + ColName += colName; + if (Dbms() == dbmsACCESS) + ColName += '"'; + + return ColName; +} // wxDb::SQLColumnName() + + /********** wxDb::SetSqlLogging() **********/ bool wxDb::SetSqlLogging(wxDbSqlLogState state, const wxString &filename, bool append) { @@ -3459,6 +3499,12 @@ wxDBMS wxDb::Dbms(void) if (!wxStricmp(baseName,wxT("DBASE"))) return((wxDBMS)(dbmsType = dbmsDBASE)); + if (!wxStricmp(baseName,wxT("xBase"))) + return((wxDBMS)(dbmsType = dbmsXBASE_SEQUITER)); + + if (!wxStricmp(baseName,wxT("MySQL"))) + return((wxDBMS)(dbmsType = dbmsMY_SQL)); + baseName[3] = 0; if (!wxStricmp(baseName,wxT("DB2"))) return((wxDBMS)(dbmsType = dbmsDBASE)); @@ -3523,17 +3569,27 @@ bool wxDb::ModifyColumn(const wxString &tableName, const wxString &columnName, case dbmsPOSTGRES : case dbmsACCESS : case dbmsDBASE : + case dbmsXBASE_SEQUITER : default : alterSlashModify = "MODIFY"; break; } // 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) + if (dataType == DB_DATA_TYPE_VARCHAR && + (Dbms() != dbmsMY_SQL || dataTypeName != "text")) { wxString s; s.Printf(wxT("(%d)"), columnLength); @@ -3702,8 +3758,10 @@ int WXDLLEXPORT wxDbConnectionsInUse(void) /********** wxDbLogExtendedErrorMsg() **********/ // DEBUG ONLY function -const wxChar WXDLLEXPORT *wxDbLogExtendedErrorMsg(const wxChar *userText, wxDb *pDb, - char *ErrFile, int ErrLine) +const wxChar* WXDLLEXPORT wxDbLogExtendedErrorMsg(const wxChar *userText, + wxDb *pDb, + const wxChar *ErrFile, + int ErrLine) { static wxString msg; msg = userText; @@ -3722,7 +3780,7 @@ const wxChar WXDLLEXPORT *wxDbLogExtendedErrorMsg(const wxChar *userText, wxDb * msg.Append (wxT("\nODBC errors:\n")); msg += wxT("\n"); - + // Display errors for this connection int i; for (i = 0; i < DB_MAX_ERROR_HISTORY; i++)