3 #if wxUSE_DATABASE_SQLITE 5 #include <wx/tokenzr.h> 6 #include <wx/filename.h> 13 wxCSConv conv(_(
"UTF-8"));
21 wxCSConv conv(_(
"UTF-8"));
23 Open(strDatabase, mustExist);
37 if (strDatabase!= _(
":memory:") &&
38 mustExist && !(wxFileName::FileExists(strDatabase)))
41 SetErrorMessage(_(
"The specified database file '") + strDatabase + _(
"' does not exist."));
45 return Open(strDatabase);
57 int nReturn = sqlite3_open(databaseNameBuffer, &pDbPtr);
59 if (nReturn != SQLITE_OK)
80 if (nReturn != SQLITE_OK)
100 wxLogDebug(_(
"Beginning transaction"));
101 RunQuery(_(
"begin transaction;"),
false);
106 wxLogDebug(_(
"Commiting transaction"));
107 RunQuery(_(
"commit transaction;"),
false);
112 wxLogDebug(_(
"Rolling back transaction"));
113 RunQuery(_(
"rollback transaction;"),
false);
124 wxArrayString QueryArray;
128 QueryArray.push_back(strQuery);
130 wxArrayString::iterator start = QueryArray.begin();
131 wxArrayString::iterator stop = QueryArray.end();
133 while (start != stop)
135 char* szErrorMessage = NULL;
136 wxString strErrorMessage = wxT(
"");
138 int nReturn = sqlite3_exec((sqlite3*)
m_pDatabase, sqlBuffer, 0, 0, &szErrorMessage);
140 if (szErrorMessage != NULL)
143 sqlite3_free(szErrorMessage);
146 if (nReturn != SQLITE_OK)
167 for (
unsigned int i=0; i<(QueryArray.size()-1); i++)
169 char* szErrorMessage = NULL;
170 wxString strErrorMessage = wxT(
"");
172 int nReturn = sqlite3_exec((sqlite3*)
m_pDatabase, sqlBuffer, 0, 0, &szErrorMessage);
174 if (szErrorMessage != NULL)
178 sqlite3_free(szErrorMessage);
182 if (nReturn != SQLITE_OK)
218 if (pReturnStatement)
223 wxArrayString::iterator start = QueryArray.begin();
224 wxArrayString::iterator stop = QueryArray.end();
226 while (start != stop)
228 const char* szTail=0;
229 wxCharBuffer sqlBuffer;
232 sqlite3_stmt* pStatement;
236 strSQL = (wxChar*)szTail;
243 #if SQLITE_VERSION_NUMBER>=3003009 244 int nReturn = sqlite3_prepare_v2((sqlite3*)
m_pDatabase, sqlBuffer, -1, &pStatement, &szTail);
246 int nReturn = sqlite3_prepare((sqlite3*)
m_pDatabase, sqlBuffer, -1, &pStatement, &szTail);
249 if (nReturn != SQLITE_OK)
253 wxDELETE(pReturnStatement);
260 }
while (strlen(szTail) > 0);
262 }
while (wxStrlen(szTail) > 0);
270 return pReturnStatement;
281 bool bReturn =
false;
287 #if wxUSE_DATABASE_EXCEPTIONS 291 wxString query = _(
"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?;");
308 #if wxUSE_DATABASE_EXCEPTIONS 310 catch (wxDatabaseException& e)
318 if (pStatement != NULL)
334 if (pStatement != NULL)
346 bool bReturn =
false;
352 #if wxUSE_DATABASE_EXCEPTIONS 356 wxString query = _(
"SELECT COUNT(*) FROM sqlite_master WHERE type='view' AND name=?;");
373 #if wxUSE_DATABASE_EXCEPTIONS 375 catch (wxDatabaseException& e)
383 if (pStatement != NULL)
399 if (pStatement != NULL)
410 wxArrayString returnArray;
413 #if wxUSE_DATABASE_EXCEPTIONS 417 wxString query = _(
"SELECT name FROM sqlite_master WHERE type='table';");
420 while (pResult->
Next())
424 #if wxUSE_DATABASE_EXCEPTIONS 426 catch (wxDatabaseException& e)
449 wxArrayString returnArray;
452 #if wxUSE_DATABASE_EXCEPTIONS 456 wxString query = _(
"SELECT name FROM sqlite_master WHERE type='view';");
459 while (pResult->
Next())
463 #if wxUSE_DATABASE_EXCEPTIONS 465 catch (wxDatabaseException& e)
488 wxArrayString returnArray;
495 #if wxUSE_DATABASE_EXCEPTIONS 500 wxString query = wxString::Format(_(
"SELECT * FROM '%s' LIMIT 0;"), table.c_str());
503 pMetaData = pResult->GetMetaData();
511 #if wxUSE_DATABASE_EXCEPTIONS 513 catch (wxDatabaseException& e)
515 if (pMetaData != NULL)
517 pResult->CloseMetaData(pMetaData);
531 if (pMetaData != NULL)
533 pResult->CloseMetaData(pMetaData);
549 wxArrayString returnArray;
556 #if wxUSE_DATABASE_EXCEPTIONS 561 wxString query = wxString::Format(_(
"PRAGMA table_info('%s') ;"), table.c_str());
563 while(pResult->Next())
565 if(pResult->GetResultInt(wxT(
"pk"))==1)
567 returnArray.Add(pResult->GetResultString(wxT(
"pk")));
571 #if wxUSE_DATABASE_EXCEPTIONS 573 catch (wxDatabaseException& e)
575 if (pMetaData != NULL)
577 pResult->CloseMetaData(pMetaData);
591 if (pMetaData != NULL)
593 pResult->CloseMetaData(pMetaData);
689 #endif//wxUSE_DATABASE_SQLITE
virtual wxArrayString GetColumns(const wxString &table)
Retrieve all column names for a table.
virtual bool TableExists(const wxString &table)
Check for the existence of a table by name.
virtual void RollBack()
Rollback the current transaction.
virtual ~wxSqliteDatabase()
virtual wxString GetResultString(int nField)=0
Retrieve a wxString from the result set by the 1-based field index.
wxDatabaseResultSet * ExecuteQuery(const wxString &strQuery)
See RunQueryWithResults.
virtual wxPreparedStatement * PrepareStatement(const wxString &strQuery)
Prepare a SQL statement which can be reused with different parameters.
virtual void Commit()
Commit the current transaction.
virtual wxArrayString GetPKColumns(const wxString &table)
get Primary keys column names
virtual bool Close()
close database
virtual bool IsOpen()
Is the connection to the database open?
void ThrowDatabaseException()
virtual int GetResultInt(int nField)=0
Retrieve an integer from the result set by the 1-based field index.
virtual bool CloseStatement(wxPreparedStatement *pStatement)
Close a prepared statement previously prepared by the database.
virtual wxDatabaseResultSet * RunQueryWithResults(const wxString &strQuery)
Run a select query on the database.
void SetErrorMessage(const wxString &strErrorMessage)
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".
void SetEncoding(wxFontEncoding encoding)
void LogResultSetForCleanup(wxDatabaseResultSet *pResultSet)
Add result set object pointer to the list for "garbage collection".
#define wxDATABASE_QUERY_RESULT_ERROR
virtual bool Open(const wxString &strDatabase)
virtual int RunQuery(const wxString &strQuery, bool bParseQuery)
Run an insert, update, or delete query on the database.
virtual void BeginTransaction()
Begin a transaction.
virtual wxArrayString GetTables()
Retrieve all table names.
void SetErrorCode(int nErrorCode)
static int TranslateErrorCode(int nCode)
void CloseStatements()
Close all prepared statement objects that have been generated but not yet closed.
void AddPreparedStatement(sqlite3_stmt *pStatement)
virtual wxString ConvertFromUnicodeStream(const char *inputBuffer)
const wxCSConv * GetEncoding()
virtual bool ViewExists(const wxString &view)
Check for the existence of a view by name.
virtual const wxCharBuffer ConvertToUnicodeStream(const wxString &inputString)
void CloseResultSets()
Close all result set objects that have been generated but not yet closed.
virtual bool CloseResultSet(wxDatabaseResultSet *pResultSet)
Close a result set returned by the database or a prepared statement previously.
virtual void SetParamString(int nPosition, const wxString &strValue)=0
Set the parameter at the 1-based position to a wxString value.
virtual wxArrayString GetViews()
Retrieve all view names.