3 #if wxUSE_DATABASE_MYSQL 5 #include <wx/tokenzr.h> 10 #ifndef DONT_USE_DYNAMIC_DATABASE_LINKING 12 if (!m_pInterface->Init())
15 SetErrorMessage(wxT(
"Error loading MySQL library"));
16 ThrowDatabaseException();
21 m_strServer = _(
"localhost");
23 m_strDatabase = wxT(
"");
25 m_strPassword = wxT(
"");
31 #ifndef DONT_USE_DYNAMIC_DATABASE_LINKING 33 if (!m_pInterface->Init())
36 SetErrorMessage(wxT(
"Error loading MySQL library"));
37 ThrowDatabaseException();
42 m_strServer = _(
"localhost");
45 m_strPassword = wxT(
"");
52 #ifndef DONT_USE_DYNAMIC_DATABASE_LINKING 54 if (!m_pInterface->Init())
57 SetErrorMessage(wxT(
"Error loading MySQL library"));
58 ThrowDatabaseException();
63 ParseServerAndPort(strServer);
65 m_strPassword = wxT(
"");
72 #ifndef DONT_USE_DYNAMIC_DATABASE_LINKING 74 if (!m_pInterface->Init())
77 SetErrorMessage(wxT(
"Error loading MySQL library"));
78 ThrowDatabaseException();
83 m_strServer = _(
"localhost");
86 m_strPassword = strPassword;
93 #ifndef DONT_USE_DYNAMIC_DATABASE_LINKING 95 if (!m_pInterface->Init())
98 SetErrorMessage(wxT(
"Error loading MySQL library5"));
99 ThrowDatabaseException();
104 ParseServerAndPort(strServer);
106 m_strPassword = strPassword;
140 return Open(strDatabase);
143 bool wxMysqlDatabase::Open(
const wxString& strDatabase,
const wxString& strUser,
const wxString& strPassword)
147 return Open(strDatabase);
150 bool wxMysqlDatabase::Open(
const wxString& strServer,
const wxString& strDatabase,
const wxString& strUser,
const wxString& strPassword)
155 return Open(strDatabase);
166 long connectFlags = 0;
167 #if MYSQL_VERSION_ID >= 40100 169 #define ulong unsigned long 171 connectFlags |= CLIENT_MULTI_RESULTS;
172 connectFlags |= CLIENT_MULTI_STATEMENTS;
177 const char* sqlStatement =
"SET CHARACTER_SET_CLIENT=utf8, " 178 "CHARACTER_SET_CONNECTION=utf8, " 179 "CHARACTER_SET_RESULTS=utf8;";
182 wxCSConv conv(_(
"UTF-8"));
199 int portIndicator = strServer.Find(_(
":"));
200 if (portIndicator > -1)
202 m_strServer = strServer.SubString(0, portIndicator-1);
203 m_iPort = wxAtoi(strServer.SubString(portIndicator+1, strServer.Length()-1));
238 const char* dbCstr = database.c_str();
306 wxArrayString QueryArray;
310 QueryArray.push_back(strQuery);
312 wxArrayString::iterator start = QueryArray.begin();
313 wxArrayString::iterator stop = QueryArray.end();
315 while (start != stop)
338 int nArraySize = QueryArray.size();
340 for (
int i=0; i<nArraySize; i++)
342 wxString strCurrentQuery = QueryArray[i];
344 if (pMysqlStatement != NULL)
348 wxString sqlUTF8((
const char*)sqlBuffer, wxConvUTF8);
371 if (i == nArraySize-1)
406 wxArrayString::iterator start = QueryArray.begin();
407 wxArrayString::iterator stop = QueryArray.end();
412 while (start != stop)
415 if (pMysqlStatement != NULL)
445 bool bReturn =
false;
469 #if wxUSE_DATABASE_EXCEPTIONS 473 wxString query = _(
"SHOW TABLE STATUS WHERE Comment != 'VIEW' AND Name=?;");
484 if (table == strTable)
489 #if wxUSE_DATABASE_EXCEPTIONS 491 catch (wxDatabaseException& e)
499 if (pStatement != NULL)
515 if (pStatement != NULL)
526 bool bReturn =
false;
550 #if wxUSE_DATABASE_EXCEPTIONS 554 wxString query = _(
"SHOW TABLE STATUS WHERE Comment = 'VIEW' AND Name=?;");
570 #if wxUSE_DATABASE_EXCEPTIONS 572 catch (wxDatabaseException& e)
580 if (pStatement != NULL)
596 if (pStatement != NULL)
607 wxArrayString returnArray;
612 #if wxUSE_DATABASE_EXCEPTIONS 616 wxString query = _(
"SHOW TABLE STATUS WHERE Comment != 'VIEW';");
619 while (pResult->
Next())
622 if (!table.IsEmpty())
623 returnArray.Add(table);
625 #if wxUSE_DATABASE_EXCEPTIONS 627 catch (wxDatabaseException& e)
648 if (returnArray.Count() == 0)
651 if (pResults != NULL)
653 MYSQL_ROW currentRow = NULL;
657 returnArray.Add(strTable);
668 wxArrayString returnArray;
673 #if wxUSE_DATABASE_EXCEPTIONS 677 wxString query = _(
"SHOW TABLE STATUS WHERE Comment = 'VIEW';");
680 while (pResult->
Next())
684 #if wxUSE_DATABASE_EXCEPTIONS 686 catch (wxDatabaseException& e)
710 wxArrayString returnArray;
714 #if wxUSE_DATABASE_EXCEPTIONS 718 wxString query = wxString::Format(_(
"SHOW COLUMNS FROM %s;"), table.c_str());
721 while (pResult->Next())
723 returnArray.Add(pResult->GetResultString(1).Trim());
725 #if wxUSE_DATABASE_EXCEPTIONS 727 catch (wxDatabaseException& e)
753 wxArrayString returnArray;
757 #if wxUSE_DATABASE_EXCEPTIONS 761 wxString query = wxString::Format(_(
"SHOW KEYS FROM %s WHERE Key_name = 'PRIMARY';"), table.c_str());
764 while (pResult->Next())
766 returnArray.Add(pResult->GetResultString(wxT(
"Column_name")).Trim());
768 #if wxUSE_DATABASE_EXCEPTIONS 770 catch (wxDatabaseException& e)
802 bool bAvailable =
false;
804 bAvailable = pInterface && pInterface->
Init();
805 wxDELETE(pInterface);
809 #endif//wxUSE_DATABASE_MYSQL static bool IsAvailable()
MysqlCloseType GetMysqlClose()
MysqlQueryType GetMysqlQuery()
virtual int RunQuery(const wxString &strQuery, bool bParseQuery)
Run an insert, update, or delete query on the database.
virtual void RollBack()
Rollback the current transaction.
MysqlFreeResultType GetMysqlFreeResult()
virtual bool IsOpen()
Is the connection to the database open?
MysqlRealQueryType GetMysqlRealQuery()
virtual bool ViewExists(const wxString &view)
Check for the existence of a view by name.
MysqlStmtErrnoType GetMysqlStmtErrno()
virtual wxString GetResultString(int nField)=0
Retrieve a wxString from the result set by the 1-based field index.
MysqlErrnoType GetMysqlErrno()
MysqlStmtExecuteType GetMysqlStmtExecute()
wxDatabaseResultSet * ExecuteQuery(const wxString &strQuery)
See RunQueryWithResults.
virtual wxArrayString GetViews()
Retrieve all view names.
virtual wxArrayString GetPKColumns(const wxString &table)
get Primary keys column names
virtual wxPreparedStatement * PrepareStatement(const wxString &strQuery)
Prepare a SQL statement which can be reused with different parameters.
void ParseServerAndPort(const wxString &strServer)
MysqlCommitType GetMysqlCommit()
virtual bool ChangeDatabase(const wxString &database)
MysqlStmtCloseType GetMysqlStmtClose()
MysqlInitType GetMysqlInit()
virtual void BeginTransaction()
Begin a transaction.
void ThrowDatabaseException()
MysqlListTablesType GetMysqlListTables()
virtual bool Close()
close database
virtual bool CloseStatement(wxPreparedStatement *pStatement)
Close a prepared statement previously prepared by the database.
MysqlStmtFreeResultType GetMysqlStmtFreeResult()
void SetErrorMessage(const wxString &strErrorMessage)
MysqlStmtInitType GetMysqlStmtInit()
wxDatabaseResultSet * ExecuteQuery()
See RunQueryWithResults.
virtual bool Next()=0
Move to the next record in the result set.
wxArrayString ParseQueries(const wxString &strQuery)
void LogStatementForCleanup(wxPreparedStatement *pStatement)
Add prepared statement object pointer to the list for "garbage collection".
MysqlGetServerVersionType GetMysqlGetServerVersion()
void SetEncoding(wxFontEncoding encoding)
void LogResultSetForCleanup(wxDatabaseResultSet *pResultSet)
Add result set object pointer to the list for "garbage collection".
virtual void Commit()
Commit the current transaction.
#define wxDATABASE_QUERY_RESULT_ERROR
virtual bool TableExists(const wxString &table)
Check for the existence of a table by name.
virtual wxArrayString GetColumns(const wxString &table)
Retrieve all column names for a table.
virtual bool Open(const wxString &strDatabase)
static int TranslateErrorCode(int nCode)
void AddPreparedStatement(MYSQL_STMT *pStatement)
MysqlSelectDatabaseType GetMysqlSelectDatabase()
virtual size_t GetEncodedStreamLength(const wxString &inputString)
void SetErrorCode(int nErrorCode)
#define wxDATABASE_ERROR_LOADING_LIBRARY
MysqlStmtPrepareType GetMysqlStmtPrepare()
void CloseStatements()
Close all prepared statement objects that have been generated but not yet closed.
MysqlServerEndType GetMysqlServerEnd()
MysqlAutoCommitType GetMysqlAutoCommit()
MysqlAffectedRowsType GetMysqlAffectedRows()
virtual wxString ConvertFromUnicodeStream(const char *inputBuffer)
const wxCSConv * GetEncoding()
wxMysqlDynamicInterface * m_pInterface
virtual wxArrayString GetTables()
Retrieve all table names.
virtual const wxCharBuffer ConvertToUnicodeStream(const wxString &inputString)
virtual ~wxMysqlDatabase()
MysqlErrorType GetMysqlError()
void CloseResultSets()
Close all result set objects that have been generated but not yet closed.
MysqlFetchRowType GetMysqlFetchRow()
virtual bool CloseResultSet(wxDatabaseResultSet *pResultSet)
Close a result set returned by the database or a prepared statement previously.
MysqlRollbackType GetMysqlRollback()
MysqlStmtErrorType GetMysqlStmtError()
virtual wxDatabaseResultSet * RunQueryWithResults(const wxString &strQuery)
Run a select query on the database.
MysqlRealConnectType GetMysqlRealConnect()
virtual void SetParamString(int nPosition, const wxString &strValue)=0
Set the parameter at the 1-based position to a wxString value.