From 7491a7f13f776bf9f1a4d384183259e9c9d5081a Mon Sep 17 00:00:00 2001 From: Alistair Leslie-Hughes Date: Tue, 25 Jan 2022 15:48:27 +1100 Subject: [PATCH] msdasql: Implement ICommandPrepare Prepare. Signed-off-by: Alistair Leslie-Hughes Signed-off-by: Alexandre Julliard --- dlls/msdasql/msdasql_main.c | 4 ++-- dlls/msdasql/msdasql_private.h | 3 ++- dlls/msdasql/session.c | 31 ++++++++++++++++++++++++++++++- dlls/msdasql/tests/provider.c | 14 ++++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/dlls/msdasql/msdasql_main.c b/dlls/msdasql/msdasql_main.c index 0d0a60ab7a2..d6c833c4c14 100644 --- a/dlls/msdasql/msdasql_main.c +++ b/dlls/msdasql/msdasql_main.c @@ -43,7 +43,7 @@ DEFINE_GUID(DBPROPSET_DBINIT, 0xc8b522bc, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0 DEFINE_GUID(DBGUID_DEFAULT, 0xc8b521fb, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d); -static void dump_sql_diag_records(SQLSMALLINT type, SQLHANDLE handle) +void dump_sql_diag_records(SQLSMALLINT type, SQLHANDLE handle) { SQLCHAR state[6], msg[SQL_MAX_MESSAGE_LENGTH]; SQLINTEGER native; @@ -611,7 +611,7 @@ static HRESULT WINAPI dbsess_CreateSession(IDBCreateSession *iface, IUnknown *ou if (outer) FIXME("outer currently not supported.\n"); - hr = create_db_session(riid, &provider->MSDASQL_iface, (void**)session); + hr = create_db_session(riid, &provider->MSDASQL_iface, provider->hdbc, (void**)session); return hr; } diff --git a/dlls/msdasql/msdasql_private.h b/dlls/msdasql/msdasql_private.h index 7d7f67c2c78..42bb8697030 100644 --- a/dlls/msdasql/msdasql_private.h +++ b/dlls/msdasql/msdasql_private.h @@ -16,4 +16,5 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ -HRESULT create_db_session(REFIID riid, IUnknown *datasource, void **unk) DECLSPEC_HIDDEN; +HRESULT create_db_session(REFIID riid, IUnknown *datasource, HDBC hdbc, void **unk) DECLSPEC_HIDDEN; +void dump_sql_diag_records(SQLSMALLINT type, SQLHANDLE handle) DECLSPEC_HIDDEN; diff --git a/dlls/msdasql/session.c b/dlls/msdasql/session.c index b4239c59037..f0e915ca710 100644 --- a/dlls/msdasql/session.c +++ b/dlls/msdasql/session.c @@ -30,6 +30,7 @@ #include "msdasql.h" #include "oledberr.h" +#include "sqlucode.h" #include "msdasql_private.h" @@ -45,6 +46,8 @@ struct msdasql_session LONG refs; IUnknown *datasource; + + HDBC hdbc; }; static inline struct msdasql_session *impl_from_IUnknown( IUnknown *iface ) @@ -305,6 +308,8 @@ struct command LONG refs; WCHAR *query; IUnknown *session; + HDBC hdbc; + SQLHSTMT hstmt; }; static inline struct command *impl_from_ICommandText( ICommandText *iface ) @@ -424,6 +429,10 @@ static ULONG WINAPI command_Release(ICommandText *iface) TRACE( "destroying %p\n", command ); if (command->session) IUnknown_Release(command->session); + + if (command->hstmt) + SQLFreeHandle(SQL_HANDLE_STMT, command->hstmt); + heap_free( command->query ); heap_free( command ); } @@ -1051,7 +1060,24 @@ static ULONG WINAPI commandprepare_Release(ICommandPrepare *iface) static HRESULT WINAPI commandprepare_Prepare(ICommandPrepare *iface, ULONG runs) { struct command *command = impl_from_ICommandPrepare( iface ); + RETCODE ret; + TRACE("%p, %u\n", command, runs); + + if (!command->query) + return DB_E_NOCOMMAND; + + if (command->hstmt) + SQLFreeHandle(SQL_HANDLE_STMT, command->hstmt); + + SQLAllocHandle(SQL_HANDLE_STMT, command->hdbc, &command->hstmt); + + ret = SQLPrepareW(command->hstmt, command->query, SQL_NTS); + if (ret != SQL_SUCCESS) + { + dump_sql_diag_records(SQL_HANDLE_STMT, command->hstmt); + return E_FAIL; + } return S_OK; } @@ -1147,6 +1173,8 @@ static HRESULT WINAPI createcommand_CreateCommand(IDBCreateCommand *iface, IUnkn command->ICommandWithParameters_iface.lpVtbl = &command_with_params_vtbl; command->refs = 1; command->query = NULL; + command->hdbc = session->hdbc; + command->hstmt = NULL; IUnknown_QueryInterface(&session->session_iface, &IID_IUnknown, (void**)&command->session); @@ -1163,7 +1191,7 @@ static const IDBCreateCommandVtbl createcommandVtbl = createcommand_CreateCommand }; -HRESULT create_db_session(REFIID riid, IUnknown *datasource, void **unk) +HRESULT create_db_session(REFIID riid, IUnknown *datasource, HDBC hdbc, void **unk) { struct msdasql_session *session; HRESULT hr; @@ -1179,6 +1207,7 @@ HRESULT create_db_session(REFIID riid, IUnknown *datasource, void **unk) session->IDBCreateCommand_iface.lpVtbl = &createcommandVtbl; IUnknown_QueryInterface(datasource, &IID_IUnknown, (void**)&session->datasource); session->refs = 1; + session->hdbc = hdbc; hr = IUnknown_QueryInterface(&session->session_iface, riid, unk); IUnknown_Release(&session->session_iface); diff --git a/dlls/msdasql/tests/provider.c b/dlls/msdasql/tests/provider.c index ce3573fbfd7..eefddbf6dcb 100644 --- a/dlls/msdasql/tests/provider.c +++ b/dlls/msdasql/tests/provider.c @@ -354,6 +354,7 @@ static void test_rowset_interfaces(IRowset *rowset, ICommandText *commandtext) static void test_command_rowset(IUnknown *cmd) { ICommandText *command_text; + ICommandPrepare *commandprepare; HRESULT hr; IUnknown *unk = NULL; IRowset *rowset; @@ -362,9 +363,22 @@ static void test_command_rowset(IUnknown *cmd) hr = IUnknown_QueryInterface(cmd, &IID_ICommandText, (void**)&command_text); ok(hr == S_OK, "got 0x%08x\n", hr); + hr = IUnknown_QueryInterface(cmd, &IID_ICommandPrepare, (void**)&commandprepare); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = ICommandText_SetCommandText(command_text, &DBGUID_DEFAULT, NULL); + ok(hr == S_OK, "got 0x%08x\n", hr); + + hr = ICommandPrepare_Prepare(commandprepare, 1); + ok(hr == DB_E_NOCOMMAND, "got 0x%08x\n", hr); + hr = ICommandText_SetCommandText(command_text, &DBGUID_DEFAULT, L"CREATE TABLE testing (col1 INT, col2 SHORT)"); ok(hr == S_OK, "got 0x%08x\n", hr); + hr = ICommandPrepare_Prepare(commandprepare, 1); + ok(hr == S_OK, "got 0x%08x\n", hr); + ICommandPrepare_Release(commandprepare); + affected = 9999; hr = ICommandText_Execute(command_text, NULL, &IID_IRowset, NULL, &affected, &unk); ok(hr == S_OK, "got 0x%08x\n", hr);