8 static int tsql_handle_common(
const TDSCONTEXT * pContext, TDSSOCKET * WXUNUSED(pTdsSocket), TDSMESSAGE * pMessage,
bool error)
10 if (pMessage->msgno != 5701 && pMessage->msgno != 5703 && pMessage->msgno != 20018)
13 sprintf(message,
"%s %d, Level %d, State %d, Server %s, Line %d\n%s", error ?
"Err" :
"Msg", pMessage->msgno, pMessage->severity, pMessage->state, pMessage->server, pMessage->line_number, pMessage->message);
17 fprintf(stderr,
"%s\n", message);
24 return TDS_INT_CANCEL;
28 static int tsql_handle_message(
const TDSCONTEXT * pContext, TDSSOCKET * pTdsSocket, TDSMESSAGE * pMessage)
30 return tsql_handle_common(pContext, pTdsSocket, pMessage,
false);
55 static int tsql_handle_error(
const TDSCONTEXT * pContext, TDSSOCKET * pTdsSocket, TDSMESSAGE * pMessage)
57 return tsql_handle_common(pContext, pTdsSocket, pMessage,
true);
83 wxString RemoveLastSemiColon(
const wxString& strvalue)
85 wxString result = strvalue;
87 if (result.size() == 0)
90 if (result[result.size()-1] ==
';' )
105 m_nTdsVersion = TDS_80;
106 wxCSConv conv(_(
"UTF-8"));
110 wxTdsDatabase::wxTdsDatabase(
const wxString& strFreeTDS,
const wxString& strServer,
const wxString& strDatabase,
const wxString& strUser,
const wxString& strPassword,
int nTdsVersion)
116 wxCSConv conv(_(
"UTF-8"));
118 m_strFreeTDS = strFreeTDS;
119 m_strServer = strServer;
120 m_strLogin = strUser;
121 m_strPassword = strPassword;
122 m_nTdsVersion = nTdsVersion;
149 if (!
RunQuery(_(
"USE ") + strDatabase,
false))
177 tds_set_interfaces_file_loc(freeTDSBuffer);
179 tds_set_server(
m_pLogin, serverBuffer);
181 tds_set_user(
m_pLogin, loginBuffer);
183 tds_set_passwd(
m_pLogin, passwordBuffer);
184 tds_set_app(
m_pLogin,
"wxDatabase");
185 tds_set_library(
m_pLogin,
"TDS-Library");
186 tds_set_client_charset(
m_pLogin,
"UTF-8");
239 m_pContext->msg_handler = tsql_handle_message;
270 if (!pConnection || tds_connect_and_login(
m_pDatabase, pConnection) == TDS_FAIL)
276 tds_free_login(pConnection);
298 tds_free_login(pConnection);
353 while ((rc = tds_process_tokens(
m_pDatabase, &result_type, NULL, TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCESS)
357 case TDS_DONE_RESULT:
358 case TDS_DONEPROC_RESULT:
359 case TDS_DONEINPROC_RESULT:
360 case TDS_STATUS_RESULT:
361 case TDS_ROWFMT_RESULT:
362 case TDS_COMPUTEFMT_RESULT:
363 case TDS_DESCRIBE_RESULT:
369 while (tds_process_tokens(
m_pDatabase, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW) == TDS_SUCCESS)
372 if (result_type != TDS_ROW_RESULT)
408 else if (rc != TDS_NO_MORE_RESULTS)
418 wxLogDebug(_(
"Beginning transaction"));
419 RunQuery(_(
"begin transaction;"),
false);
424 wxLogDebug(_(
"Commiting transaction"));
425 RunQuery(_(
"commit transaction;"),
false);
430 wxLogDebug(_(
"Rolling back transaction"));
431 RunQuery(_(
"rollback transaction;"),
false);
444 wxArrayString QueryArray;
448 QueryArray.push_back(strQuery);
450 wxArrayString::iterator start = QueryArray.begin();
451 wxArrayString::iterator stop = QueryArray.end();
459 while (start != stop)
462 wxString strErrorMessage = _(
"");
463 wxString sql = RemoveLastSemiColon(*start);
467 int nReturn = tds_submit_query(
m_pDatabase, sqlBuffer);
468 if (nReturn != TDS_SUCCESS)
482 return (status.has ? status.ret :
m_pDatabase->rows_affected);
496 for (
unsigned int i=0; i<(QueryArray.size()-1); i++)
499 wxString strErrorMessage = _(
"");
500 wxString sql = RemoveLastSemiColon(QueryArray[i]);
504 int nReturn = tds_submit_query(
m_pDatabase, sqlBuffer);
505 if (nReturn != TDS_SUCCESS)
516 wxString strQuery = RemoveLastSemiColon(QueryArray[QueryArray.size()-1]);
519 int nReturn = tds_submit_query(
m_pDatabase, sqlBuffer);
520 if (nReturn != TDS_SUCCESS)
548 wxString sql = RemoveLastSemiColon(strQuery);
551 TDSDYNAMIC* pStatement = NULL;
553 int nReturn = tds_submit_prepare(
m_pDatabase, sqlBuffer, NULL, &pStatement, pParameters);
554 if (nReturn != TDS_SUCCESS)
558 if (pStatement != NULL)
559 tds_release_dynamic(&pStatement);
565 if (pParameters != NULL)
566 tds_free_param_results(pParameters);
570 if (pReturnStatement)
573 if (pReturnStatement != NULL)
575 return pReturnStatement;
586 bool bReturn =
false;
592 #if wxUSE_DATABASE_EXCEPTIONS 596 wxString query = _(
"exec sp_tables ?, NULL, NULL, '''TABLE'''");
614 #if wxUSE_DATABASE_EXCEPTIONS 616 catch (wxDatabaseException& e)
624 if (pStatement != NULL)
640 if (pStatement != NULL)
651 bool bReturn =
false;
657 #if wxUSE_DATABASE_EXCEPTIONS 661 wxString query = _(
"exec sp_tables ?, NULL, NULL, '''VIEW'''");
679 #if wxUSE_DATABASE_EXCEPTIONS 681 catch (wxDatabaseException& e)
689 if (pStatement != NULL)
705 if (pStatement != NULL)
716 wxArrayString returnArray;
719 #if wxUSE_DATABASE_EXCEPTIONS 723 wxString query = _(
"sp_tables NULL, NULL, NULL, '''TABLE'''");
726 while (pResult->
Next())
731 #if wxUSE_DATABASE_EXCEPTIONS 733 catch (wxDatabaseException& e)
756 wxArrayString returnArray;
759 #if wxUSE_DATABASE_EXCEPTIONS 763 wxString query = _(
"sp_tables NULL, NULL, NULL, '''VIEW'''");
766 while (pResult->
Next())
771 #if wxUSE_DATABASE_EXCEPTIONS 773 catch (wxDatabaseException& e)
796 wxArrayString returnArray;
799 #if wxUSE_DATABASE_EXCEPTIONS 803 wxString query = wxString::Format(_(
"sp_columns %s;"), table.c_str());
806 while (pResult->Next())
809 returnArray.Add(pResult->GetResultString(_(
"COLUMN_NAME")).Trim());
811 #if wxUSE_DATABASE_EXCEPTIONS 813 catch (wxDatabaseException& e)
836 wxArrayString returnArray;
841 #if wxUSE_DATABASE_EXCEPTIONS 845 wxString query = wxString::Format(_(
"sp_pkeys %s;"), table.c_str());
848 while (pResult->Next())
851 returnArray.Add(pResult->GetResultString(_(
"COLUMN_NAME")).Trim());
853 #if wxUSE_DATABASE_EXCEPTIONS 855 catch (wxDatabaseException& e)
880 if (msg.Length() > 0)
882 msg +=
"\n" + strMessage;
909 pLayer = finder->second;
926 TDSPARAMINFO* params = NULL;
927 wxChar previous =
'\0';
928 bool bUnicode =
false;
929 bool bInStringLiteral =
false;
930 for (
size_t i = 0; i < strQuery.length(); i++)
932 wxChar character = strQuery[i];
933 if (
'\'' == character)
936 bInStringLiteral = !bInStringLiteral;
938 if (bInStringLiteral && previous ==
'N') bUnicode =
true;
940 else if ((
'?' == character) && !bInStringLiteral)
942 params = tds_alloc_param_result(params);
943 TDSCOLUMN* col = params->columns[params->num_cols-1];
946 tds_set_param_type(
m_pDatabase->conn, col, XSYBNVARCHAR);
947 col->column_varint_size = 8;
951 tds_set_param_type(
m_pDatabase->conn, col, XSYBVARCHAR);
952 col->column_size = 8000;
953 col->on_server.column_size = 8000;
954 col->column_cur_size = 8000;
958 previous = character;
963 #endif//wxUSE_DATABASE_TDS
virtual bool ViewExists(const wxString &view)
Check for the existence of a view by name.
virtual void RollBack()
Rollback the current transaction.
virtual bool IsOpen()
Is the connection to the database open?
virtual wxArrayString GetPKColumns(const wxString &table)
get Primary keys column names
static wxTdsDatabase * LookupTdsLayer(const TDSCONTEXT *pContext)
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.
static void AddTdsLayer(TDSCONTEXT *pContext, wxTdsDatabase *pLayer)
virtual wxPreparedStatement * PrepareStatement(const wxString &strQuery)
Prepare a SQL statement which can be reused with different parameters.
virtual int RunQuery(const wxString &strQuery, bool bParseQuery)
Run an insert, update, or delete query on the database.
void ThrowDatabaseException()
const wxString & GetErrorMessage()
virtual wxArrayString GetColumns(const wxString &table)
Retrieve all column names for a table.
virtual void Commit()
Commit the current transaction.
virtual bool CloseStatement(wxPreparedStatement *pStatement)
Close a prepared statement previously prepared by the database.
static int TranslateErrorCode(int nCode)
virtual wxDatabaseResultSet * RunQueryWithResults(const wxString &strQuery)
Run a select query on the database.
virtual wxArrayString GetViews()
Retrieve all view names.
virtual bool Open(const wxString &strDatabase)
void SetErrorMessage(const wxString &strErrorMessage)
wxDatabaseResultSet * ExecuteQuery()
See RunQueryWithResults.
void SetError(int nCode, const wxString &strMessage)
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".
static TdsContextToDatabaseLayerMap m_ContextLookupMap
void SetEncoding(wxFontEncoding encoding)
void LogResultSetForCleanup(wxDatabaseResultSet *pResultSet)
Add result set object pointer to the list for "garbage collection".
static void RemoveTdsLayer(TDSCONTEXT *pContext)
virtual wxArrayString GetTables()
Retrieve all table names.
void SetErrorCode(int nErrorCode)
void CloseStatements()
Close all prepared statement objects that have been generated but not yet closed.
virtual wxString ConvertFromUnicodeStream(const char *inputBuffer)
const wxCSConv * GetEncoding()
virtual bool Close()
close database
virtual const wxCharBuffer ConvertToUnicodeStream(const wxString &inputString)
void FreeAllocatedResultSets(status_t *status=NULL)
virtual void BeginTransaction()
Begin a transaction.
virtual bool TableExists(const wxString &table)
Check for the existence of a table by name.
TDSPARAMINFO * GetParameters(const wxString &strQuery)
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.