Skip to content

Commit 4d53c5a

Browse files
author
Bogdan Degtyariov
committed
Bug #33721056 - C/C++ classic driver does not detect the plugin directory based on driver path
1 parent 02183ba commit 4d53c5a

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

driver/mysql_connection.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
576576
*/
577577

578578
{
579+
sql::SQLString plugin_dir;
579580
it = properties.find(OPT_PLUGIN_DIR);
580581

581582
if (it != properties.end()) {
@@ -584,12 +585,28 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
584585
} catch (sql::InvalidArgumentException&) {
585586
throw sql::InvalidArgumentException("Wrong type passed for pluginDir expected sql::SQLString");
586587
}
587-
if (p_s) {
588-
proxy->options(sql::mysql::MYSQL_PLUGIN_DIR, *p_s);
589-
}
590-
else {
591-
throw sql::InvalidArgumentException("No string value passed for pluginDir");
592-
}
588+
}
589+
#if(_WIN32 && CONCPP_BUILD_SHARED)
590+
else {
591+
/*
592+
Note: For DLL in Windows we will try to set the plugin directory
593+
based on driver_dll_path.
594+
*/
595+
plugin_dir = driver_dll_path;
596+
#ifdef _DEBUG
597+
// Debug dll is placed inside debug subdirectory
598+
plugin_dir.append("..\\");
599+
#endif
600+
plugin_dir.append("plugin");
601+
p_s = &plugin_dir;
602+
}
603+
#endif
604+
605+
if (p_s) {
606+
proxy->options(sql::mysql::MYSQL_PLUGIN_DIR, *p_s);
607+
} else if (it != properties.end()) {
608+
// Throw only when OPT_PLUGIN_DIR is used, but no value is given
609+
throw sql::InvalidArgumentException("No string value passed for pluginDir");
593610
}
594611
}
595612

driver/mysql_connection.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
#include "cppconn/connection.h"
3737
#include <memory>
3838

39+
#if(_WIN32 && CONCPP_BUILD_SHARED)
40+
extern std::string driver_dll_path;
41+
#endif
42+
3943
namespace sql
4044
{
4145
namespace mysql

driver/mysql_driver.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
*/
3030

3131

32+
#ifdef _WIN32
33+
#include <Windows.h>
34+
#endif
3235

3336
#include "mysql_connection.h"
3437
#include "version_info.h"
@@ -187,6 +190,27 @@ void MySQL_Driver::setCallBack(sql::Fido_Callback& cb)
187190

188191
} /* namespace sql */
189192

193+
#if(_WIN32 && CONCPP_BUILD_SHARED)
194+
std::string driver_dll_path;
195+
196+
BOOL WINAPI DllMain(
197+
_In_ HINSTANCE hmodule,
198+
_In_ DWORD,
199+
_In_ LPVOID
200+
)
201+
{
202+
/*
203+
For Windows DLL we will store the current module path to allow detecting
204+
directories with the plugins and dependencies.
205+
*/
206+
char _driver_dll_path[1024] = { 0 };
207+
GetModuleFileNameA(hmodule, _driver_dll_path, sizeof(_driver_dll_path));
208+
driver_dll_path = _driver_dll_path;
209+
driver_dll_path = driver_dll_path.substr(0, driver_dll_path.find_last_of('\\') + 1);
210+
return true;
211+
}
212+
#endif
213+
190214
/*
191215
* Local variables:
192216
* tab-width: 4

0 commit comments

Comments
 (0)