sqldata/sqlmysqlapi.cpp
// Initialize API for process (MySQL C library can be compiled as non thread-safe)
Initialize API for process (MySQL C library can be compiled as non thread-safe)
Initialize API for process ( MySQL C library can be compiled as non thread - safe )
int SqlMysqlApi::InitStatic()
{
	if(_static_init == true)
		return 0;
	TRACE("MySQL/C InitStatic() Entered");
	const char *mysql_default_lib = MYSQL_C_DLL; 
	const char *mysql_load_error = MYSQL_DLL_LOAD_ERROR;
	const char *mysql_64bit_load_error = MYSQL_64BIT_DLL_LOAD_ERROR; 
	const char *mysql_c_func_load_error = MYSQL_C_FUNC_LOAD_ERROR; 
	const char *mysql_lib = NULL; 
	if(_subtype == SQLDATA_SUBTYPE_MARIADB)
	{
		TRACE("MySQL/C InitStatic() for MariaDB");
		mysql_default_lib = MARIADB_C_DLL;
		mysql_load_error = MARIADB_DLL_LOAD_ERROR;
		mysql_64bit_load_error = MARIADB_64BIT_DLL_LOAD_ERROR; 
		mysql_c_func_load_error = MARIADB_C_FUNC_LOAD_ERROR; 
		if(_parameters != NULL)
			mysql_lib = _parameters->Get("-mariadb_lib");
	}
	if(mysql_lib != NULL)
	{
		TRACE_P("MySQL/C InitStatic() library: %s", mysql_lib);
		_dll = Os::LoadLibrary(mysql_lib);
	}
	else
	{
		TRACE_P("MySQL/C InitStatic() default library: %s", mysql_default_lib);
		_dll = Os::LoadLibrary(mysql_default_lib);
	}
	if(_dll == NULL)
	{
		Os::GetLastErrorText(mysql_load_error, _native_error_text, 1024);
		TRACE_P("MySQL/C InitStatic() error: %s", _native_error_text);
	}
	if(_dll == NULL && mysql_lib == NULL)
	{
#if defined(WIN32) || defined(_WIN64)
		bool sf = Os::Is64Bit(mysql_default_lib);
		if(sf == true)
			strcpy(_native_error_text, mysql_64bit_load_error);
		FindMysqlPaths();
		for(std::list<std::string>::iterator i = _driver_paths.begin(); i != _driver_paths.end() ; i++)
		{
			std::string loc = (*i);
			loc += MYSQL_C_DLL;
			_dll = LoadLibrary(loc.c_str());
			if(_dll != NULL)
				break;
			Os::GetLastErrorText(MYSQL_DLL_LOAD_ERROR, _native_error_text, 1024);
			sf = Os::Is64Bit(MYSQL_C_DLL);
			if(sf == true)
				strcpy(_native_error_text, MYSQL_64BIT_DLL_LOAD_ERROR);
		}		
#else
		char *error = Os::LoadLibraryError();
		if(error != NULL)
			strcpy(_native_error_text, error);
#endif
	}
	if(_dll != NULL)
	{
		Os::GetModuleFileName(_dll, _loaded_driver, 1024);
		_mysql_init = (mysql_initFunc)Os::GetProcAddress(_dll, "mysql_init");
		_mysql_close = (mysql_closeFunc)Os::GetProcAddress(_dll, "mysql_close");
		_mysql_errno = (mysql_errnoFunc)Os::GetProcAddress(_dll, "mysql_errno");
		_mysql_error = (mysql_errorFunc)Os::GetProcAddress(_dll, "mysql_error");
		_mysql_fetch_fields = (mysql_fetch_fieldsFunc)Os::GetProcAddress(_dll, "mysql_fetch_fields");
		_mysql_fetch_row = (mysql_fetch_rowFunc)Os::GetProcAddress(_dll, "mysql_fetch_row");
		_mysql_fetch_lengths = (mysql_fetch_lengthsFunc)Os::GetProcAddress(_dll, "mysql_fetch_lengths");
		_mysql_free_result = (mysql_free_resultFunc)Os::GetProcAddress(_dll, "mysql_free_result");
		_mysql_num_fields = (mysql_num_fieldsFunc)Os::GetProcAddress(_dll, "mysql_num_fields");
		_mysql_options = (mysql_optionsFunc)Os::GetProcAddress(_dll, "mysql_options");
		_mysql_real_connect = (mysql_real_connectFunc)Os::GetProcAddress(_dll, "mysql_real_connect");
		_mysql_query = (mysql_queryFunc)Os::GetProcAddress(_dll, "mysql_query");
		_mysql_set_character_set = (mysql_set_character_setFunc)Os::GetProcAddress(_dll, "mysql_set_character_set");
		_mysql_set_local_infile_handler = (mysql_set_local_infile_handlerFunc)Os::GetProcAddress(_dll, "mysql_set_local_infile_handler");
		_mysql_thread_init = (mysql_thread_initFunc)Os::GetProcAddress(_dll, "mysql_thread_init");
		_mysql_use_result = (mysql_use_resultFunc)Os::GetProcAddress(_dll, "mysql_use_result");
		if(_mysql_init == NULL || _mysql_close == NULL || _mysql_errno == NULL || _mysql_error == NULL || 
			_mysql_fetch_fields == NULL || _mysql_fetch_row == NULL || _mysql_fetch_lengths == NULL || 
			_mysql_free_result == NULL || _mysql_num_fields == NULL || _mysql_options == NULL || _mysql_real_connect == NULL || 
			_mysql_query == NULL || _mysql_set_local_infile_handler == NULL || 
			_mysql_thread_init == NULL || _mysql_use_result == NULL)
		{
			strcpy(_native_error_text, MYSQL_C_FUNC_LOAD_ERROR);
			return -1;
		}
	}
	else
		return -1;
	if(_mysql_init != NULL)
	{
		MYSQL *mysql = _mysql_init(&_mysql);
		if(mysql == NULL)
			return -1;
		InitGlobalOptions();
	}
	_static_init = true;
	TRACE("MySQL/C InitStatic() Left");
	return 0;
}
int SqlMysqlApi :: InitStatic ( ) { if ( _static_init == true ) return 0 ; TRACE ( " " ) ; const char * mysql_default_lib = MYSQL_C_DLL ; const char * mysql_load_error = MYSQL_DLL_LOAD_ERROR ; const char * mysql_64bit_load_error = MYSQL_64BIT_DLL_LOAD_ERROR ; const char * mysql_c_func_load_error = MYSQL_C_FUNC_LOAD_ERROR ; const char * mysql_lib = NULL ; if ( _subtype == SQLDATA_SUBTYPE_MARIADB ) { TRACE ( " " ) ; mysql_default_lib = MARIADB_C_DLL ; mysql_load_error = MARIADB_DLL_LOAD_ERROR ; mysql_64bit_load_error = MARIADB_64BIT_DLL_LOAD_ERROR ; mysql_c_func_load_error = MARIADB_C_FUNC_LOAD_ERROR ; if ( _parameters != NULL ) mysql_lib = _parameters -> Get ( " " ) ; } if ( mysql_lib != NULL ) { TRACE_P ( " " , mysql_lib ) ; _dll = Os :: LoadLibrary ( mysql_lib ) ; } else { TRACE_P ( " " , mysql_default_lib ) ; _dll = Os :: LoadLibrary ( mysql_default_lib ) ; } if ( _dll == NULL ) { Os :: GetLastErrorText ( mysql_load_error , _native_error_text , 1024 ) ; TRACE_P ( " " , _native_error_text ) ; } if ( _dll == NULL && mysql_lib == NULL ) { #if defined ( WIN32 ) || defined ( _WIN64 ) bool sf = Os :: Is64Bit ( mysql_default_lib ) ; if ( sf == true ) strcpy ( _native_error_text , mysql_64bit_load_error ) ; FindMysqlPaths ( ) ; for ( std :: list < std :: string > :: iterator i = _driver_paths . begin ( ) ; i != _driver_paths . end ( ) ; i ++ ) { std :: string loc = ( * i ) ; loc += MYSQL_C_DLL ; _dll = LoadLibrary ( loc . c_str ( ) ) ; if ( _dll != NULL ) break ; Os :: GetLastErrorText ( MYSQL_DLL_LOAD_ERROR , _native_error_text , 1024 ) ; sf = Os :: Is64Bit ( MYSQL_C_DLL ) ; if ( sf == true ) strcpy ( _native_error_text , MYSQL_64BIT_DLL_LOAD_ERROR ) ; } #else char * error = Os :: LoadLibraryError ( ) ; if ( error != NULL ) strcpy ( _native_error_text , error ) ; #endif } if ( _dll != NULL ) { Os :: GetModuleFileName ( _dll , _loaded_driver , 1024 ) ; _mysql_init = ( mysql_initFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_close = ( mysql_closeFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_errno = ( mysql_errnoFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_error = ( mysql_errorFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_fetch_fields = ( mysql_fetch_fieldsFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_fetch_row = ( mysql_fetch_rowFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_fetch_lengths = ( mysql_fetch_lengthsFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_free_result = ( mysql_free_resultFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_num_fields = ( mysql_num_fieldsFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_options = ( mysql_optionsFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_real_connect = ( mysql_real_connectFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_query = ( mysql_queryFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_set_character_set = ( mysql_set_character_setFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_set_local_infile_handler = ( mysql_set_local_infile_handlerFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_thread_init = ( mysql_thread_initFunc ) Os :: GetProcAddress ( _dll , " " ) ; _mysql_use_result = ( mysql_use_resultFunc ) Os :: GetProcAddress ( _dll , " " ) ; if ( _mysql_init == NULL || _mysql_close == NULL || _mysql_errno == NULL || _mysql_error == NULL || _mysql_fetch_fields == NULL || _mysql_fetch_row == NULL || _mysql_fetch_lengths == NULL || _mysql_free_result == NULL || _mysql_num_fields == NULL || _mysql_options == NULL || _mysql_real_connect == NULL || _mysql_query == NULL || _mysql_set_local_infile_handler == NULL || _mysql_thread_init == NULL || _mysql_use_result == NULL ) { strcpy ( _native_error_text , MYSQL_C_FUNC_LOAD_ERROR ) ; return -1 ; } } else return -1 ; if ( _mysql_init != NULL ) { MYSQL * mysql = _mysql_init ( & _mysql ) ; if ( mysql == NULL ) return -1 ; InitGlobalOptions ( ) ; } _static_init = true ; TRACE ( " " ) ; return 0 ; }
Initialize API for process (MySQL C library can be compiled as non thread-safe)
Initialize API for process ( MySQL C library can be compiled as non thread - safe )
// Try to load the library by default path // DLL load failed (do not search if library path is set) // No error set if DLL is 64-bit and current sqldata build is 32-bit // Try to find MySQL installation paths // Note that search is performed for MySQL C Connector even for MariaDB // Try to open DLL // Set error for the current search item  // No error set if DLL is 64-bit // Get functions // Save the full path of the loaded driver // mysql_set_character_set is available since 5.0.7, check for NULL when use // Error message already set // Initialize the MySQL library  // mysql_library_init is not exported DLL function it is define for mysql_server_init // but by calling mysql_init we force mysql_library_init() call to initialize the library for the process // Must be called before threads are launched, otherwise the application crashes on some systems // Initialize global options that take effect only for new connections // Did not work for MariaDB  //_mysql_options(&_mysql, MYSQL_OPT_LOCAL_INFILE, 0);