--- /dev/null
+///////////////////////////////////////////////////////////////////////////////\r
+//\r
+// File : $Id: _ibpp.cpp,v 1.3 2009/03/19 20:00:27 faust Exp $\r
+// Subject : IBPP, Initialization of the library\r
+//\r
+///////////////////////////////////////////////////////////////////////////////\r
+//\r
+// (C) Copyright 2000-2006 T.I.P. Group S.A. and the IBPP Team (www.ibpp.org)\r
+//\r
+// The contents of this file are subject to the IBPP License (the "License");\r
+// you may not use this file except in compliance with the License. You may\r
+// obtain a copy of the License at http://www.ibpp.org or in the 'license.txt'\r
+// file which must have been distributed along with this file.\r
+//\r
+// This software, distributed under the License, is distributed on an "AS IS"\r
+// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the\r
+// License for the specific language governing rights and limitations\r
+// under the License.\r
+//\r
+///////////////////////////////////////////////////////////////////////////////\r
+//\r
+// COMMENTS\r
+// * Tabulations should be set every four characters when editing this file.\r
+//\r
+///////////////////////////////////////////////////////////////////////////////\r
+\r
+#ifdef _MSC_VER\r
+#pragma warning(disable: 4786 4996)\r
+#ifndef _DEBUG\r
+#pragma warning(disable: 4702)\r
+#endif\r
+#endif\r
+\r
+#include "_ibpp.h"\r
+\r
+#ifdef HAS_HDRSTOP\r
+#pragma hdrstop\r
+#endif\r
+\r
+#include <limits>\r
+\r
+#ifdef IBPP_WINDOWS\r
+// New (optional) Registry Keys introduced by Firebird Server 1.5\r
+#define REG_KEY_ROOT_INSTANCES "SOFTWARE\\Firebird Project\\Firebird Server\\Instances"\r
+#define FB_DEFAULT_INSTANCE "DefaultInstance"\r
+#endif\r
+\r
+namespace ibpp_internals\r
+{\r
+ const double consts::dscales[19] = {\r
+ 1, 1E1, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7, 1E8,\r
+ 1E9, 1E10, 1E11, 1E12, 1E13, 1E14, 1E15,\r
+ 1E16, 1E17, 1E18 };\r
+\r
+ const int consts::Dec31_1899 = 693595;\r
+\r
+// Many compilers confuses those following min/max with macros min and max !\r
+#undef min\r
+#undef max\r
+\r
+#ifdef __DMC__ // Needs to break-down the declaration else compiler crash (!)\r
+ const std::numeric_limits<int16_t> i16_limits;\r
+ const std::numeric_limits<int32_t> i32_limits;\r
+ const int16_t consts::min16 = i16_limits.min();\r
+ const int16_t consts::max16 = i16_limits.max();\r
+ const int32_t consts::min32 = i32_limits.min();\r
+ const int32_t consts::max32 = i32_limits.max();\r
+#else\r
+ const int16_t consts::min16 = std::numeric_limits<int16_t>::min();\r
+ const int16_t consts::max16 = std::numeric_limits<int16_t>::max();\r
+ const int32_t consts::min32 = std::numeric_limits<int32_t>::min();\r
+ const int32_t consts::max32 = std::numeric_limits<int32_t>::max();\r
+#endif\r
+\r
+ GDS gds; // Global unique GDS instance\r
+\r
+#ifdef IBPP_WINDOWS\r
+ std::string AppPath; // Used by GDS::Call() below\r
+#endif\r
+\r
+#ifdef _DEBUG\r
+ std::ostream& operator<< (std::ostream& a, flush_debug_stream_type)\r
+ {\r
+ if (std::stringstream* p = dynamic_cast<std::stringstream*>(&a))\r
+ {\r
+#ifdef IBPP_WINDOWS\r
+ ::OutputDebugString(("IBPP: " + p->str() + "\n").c_str());\r
+#endif\r
+ p->str("");\r
+ }\r
+ return a;\r
+ }\r
+#endif // _DEBUG\r
+\r
+}\r
+\r
+using namespace ibpp_internals;\r
+\r
+GDS* GDS::Call()\r
+{\r
+ // Let's load the CLIENT library, if it is not already loaded.\r
+ // The load is guaranteed to be done only once per application.\r
+\r
+ if (! mReady)\r
+ {\r
+#ifdef IBPP_WINDOWS\r
+\r
+ // Let's load the FBCLIENT.DLL or GDS32.DLL, we will never release it.\r
+ // Windows will do that for us when the executable will terminate.\r
+\r
+ char fbdll[MAX_PATH];\r
+ HKEY hkey_instances;\r
+\r
+ // Try to load FBCLIENT.DLL from each of the additional optional paths\r
+ // that may have been specified through ClientLibSearchPaths().\r
+ // We also want to actually update the environment PATH so that it references\r
+ // the specific path from where we attempt the load. This is useful because\r
+ // it directs the system to attempt finding dependencies (like the C/C++\r
+ // runtime libraries) from the same location where FBCLIENT is found.\r
+\r
+ mHandle = 0;\r
+\r
+ std::string SysPath(getenv("PATH"));\r
+ std::string::size_type pos = 0;\r
+ while (pos < mSearchPaths.size())\r
+ {\r
+ std::string::size_type newpos = mSearchPaths.find(';', pos);\r
+\r
+ std::string path;\r
+ if (newpos == std::string::npos) path = mSearchPaths.substr(pos);\r
+ else path = mSearchPaths.substr(pos, newpos-pos);\r
+\r
+ if (path.size() >= 1)\r
+ {\r
+ if (path[path.size()-1] != '\\') path += '\\';\r
+\r
+ AppPath.assign("PATH=");\r
+ AppPath.append(path).append(";").append(SysPath);\r
+ putenv(AppPath.c_str());\r
+\r
+ path.append("fbclient.dll");\r
+ mHandle = LoadLibrary(path.c_str());\r
+ if (mHandle != 0 || newpos == std::string::npos) break;\r
+ }\r
+ pos = newpos + 1;\r
+ }\r
+\r
+ if (mHandle == 0)\r
+ {\r
+ // Try to load FBCLIENT.DLL from the current application location. This\r
+ // is a usefull step for applications using the embedded version of FB\r
+ // or a local copy (for whatever reasons) of the dll.\r
+\r
+ if (! AppPath.empty())\r
+ {\r
+ // Restores the original system path\r
+ AppPath.assign("PATH=");\r
+ AppPath.append(SysPath);\r
+ putenv(AppPath.c_str());\r
+ }\r
+\r
+ int len = GetModuleFileName(NULL, fbdll, sizeof(fbdll));\r
+ if (len != 0)\r
+ {\r
+ // Get to the last '\' (this one precedes the filename part).\r
+ // There is always one after a success call to GetModuleFileName().\r
+ char* p = fbdll + len;\r
+ do {--p;} while (*p != '\\');\r
+ *p = '\0';\r
+ lstrcat(fbdll, "\\fbembed.dll");// Local copy could be named fbembed.dll\r
+ mHandle = LoadLibrary(fbdll);\r
+ if (mHandle == 0)\r
+ {\r
+ *p = '\0';\r
+ lstrcat(fbdll, "\\fbclient.dll"); // Or possibly renamed fbclient.dll\r
+ mHandle = LoadLibrary(fbdll);\r
+ }\r
+ }\r
+ }\r
+\r
+ if (mHandle == 0)\r
+ {\r
+ // Try to locate FBCLIENT.DLL through the optional FB registry key.\r
+\r
+ if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_KEY_ROOT_INSTANCES, 0,\r
+ KEY_READ, &hkey_instances) == ERROR_SUCCESS)\r
+ {\r
+ DWORD keytype;\r
+ DWORD buflen = sizeof(fbdll);\r
+ if (RegQueryValueEx(hkey_instances, FB_DEFAULT_INSTANCE, 0,\r
+ &keytype, reinterpret_cast<UCHAR*>(fbdll),\r
+ &buflen) == ERROR_SUCCESS && keytype == REG_SZ)\r
+ {\r
+ lstrcat(fbdll, "bin\\fbclient.dll");\r
+ mHandle = LoadLibrary(fbdll);\r
+ }\r
+ RegCloseKey(hkey_instances);\r
+ }\r
+ }\r
+\r
+ if (mHandle == 0)\r
+ {\r
+ // Let's try from the PATH and System directories\r
+ mHandle = LoadLibrary("fbclient.dll");\r
+ if (mHandle == 0)\r
+ {\r
+ // Not found. Last try : attemps loading gds32.dll from PATH and\r
+ // System directories\r
+ mHandle = LoadLibrary("gds32.dll");\r
+ if (mHandle == 0)\r
+ throw LogicExceptionImpl("GDS::Call()",\r
+ _("Can't find or load FBCLIENT.DLL or GDS32.DLL"));\r
+ }\r
+ }\r
+#endif\r
+\r
+ mGDSVersion = 60;\r
+\r
+ // Get the entry points that we need\r
+\r
+#ifdef IBPP_WINDOWS\r
+#define IB_ENTRYPOINT(X) \\r
+ if ((m_##X = (proto_##X*)GetProcAddress(mHandle, "isc_"#X)) == 0) \\r
+ throw LogicExceptionImpl("GDS:gds()", _("Entry-point isc_"#X" not found"))\r
+#endif\r
+#ifdef IBPP_UNIX\r
+/* TODO : perform a late-bind on unix --- not so important, well I think (OM) */\r
+#define IB_ENTRYPOINT(X) m_##X = (proto_##X*)isc_##X\r
+#endif\r
+\r
+ IB_ENTRYPOINT(create_database);\r
+ IB_ENTRYPOINT(attach_database);\r
+ IB_ENTRYPOINT(detach_database);\r
+ IB_ENTRYPOINT(drop_database);\r
+ IB_ENTRYPOINT(database_info);\r
+ IB_ENTRYPOINT(open_blob2);\r
+ IB_ENTRYPOINT(create_blob2);\r
+ IB_ENTRYPOINT(close_blob);\r
+ IB_ENTRYPOINT(cancel_blob);\r
+ IB_ENTRYPOINT(get_segment);\r
+ IB_ENTRYPOINT(put_segment);\r
+ IB_ENTRYPOINT(blob_info);\r
+ IB_ENTRYPOINT(array_lookup_bounds);\r
+ IB_ENTRYPOINT(array_get_slice);\r
+ IB_ENTRYPOINT(array_put_slice);\r
+ IB_ENTRYPOINT(vax_integer);\r
+ IB_ENTRYPOINT(sqlcode);\r
+ IB_ENTRYPOINT(sql_interprete);\r
+ IB_ENTRYPOINT(interprete);\r
+ IB_ENTRYPOINT(que_events);\r
+ IB_ENTRYPOINT(cancel_events);\r
+ IB_ENTRYPOINT(start_multiple);\r
+ IB_ENTRYPOINT(commit_transaction);\r
+ IB_ENTRYPOINT(commit_retaining);\r
+ IB_ENTRYPOINT(rollback_transaction);\r
+ IB_ENTRYPOINT(rollback_retaining);\r
+ IB_ENTRYPOINT(dsql_execute_immediate);\r
+ IB_ENTRYPOINT(dsql_allocate_statement);\r
+ IB_ENTRYPOINT(dsql_describe);\r
+ IB_ENTRYPOINT(dsql_describe_bind);\r
+ IB_ENTRYPOINT(dsql_prepare);\r
+ IB_ENTRYPOINT(dsql_execute);\r
+ IB_ENTRYPOINT(dsql_execute2);\r
+ IB_ENTRYPOINT(dsql_fetch);\r
+ IB_ENTRYPOINT(dsql_free_statement);\r
+ IB_ENTRYPOINT(dsql_set_cursor_name);\r
+ IB_ENTRYPOINT(dsql_sql_info);\r
+\r
+ IB_ENTRYPOINT(service_attach);\r
+ IB_ENTRYPOINT(service_detach);\r
+ IB_ENTRYPOINT(service_start);\r
+ IB_ENTRYPOINT(service_query);\r
+\r
+ mReady = true;\r
+ }\r
+\r
+ return this;\r
+}\r
+\r
+namespace IBPP\r
+{\r
+\r
+ bool CheckVersion(uint32_t AppVersion)\r
+ {\r
+ //(void)gds.Call(); // Just call it to trigger the initialization\r
+ return (AppVersion & 0xFFFFFF00) ==\r
+ (IBPP::Version & 0xFFFFFF00) ? true : false;\r
+ }\r
+\r
+ int GDSVersion()\r
+ {\r
+ return gds.Call()->mGDSVersion;\r
+ }\r
+\r
+#ifdef IBPP_WINDOWS\r
+ void ClientLibSearchPaths(const std::string& paths)\r
+ {\r
+ gds.mSearchPaths.assign(paths);\r
+ }\r
+#else\r
+ void ClientLibSearchPaths(const std::string&)\r
+ {\r
+ }\r
+#endif\r
+\r
+ // Factories for our Interface objects\r
+\r
+ Service ServiceFactory(const std::string& ServerName,\r
+ const std::string& UserName, const std::string& UserPassword)\r
+ {\r
+ (void)gds.Call(); // Triggers the initialization, if needed\r
+ return new ServiceImpl(ServerName, UserName, UserPassword);\r
+ }\r
+\r
+ Database DatabaseFactory(const std::string& ServerName,\r
+ const std::string& DatabaseName, const std::string& UserName,\r
+ const std::string& UserPassword, const std::string& RoleName,\r
+ const std::string& CharSet, const std::string& CreateParams)\r
+ {\r
+ (void)gds.Call(); // Triggers the initialization, if needed\r
+ return new DatabaseImpl(ServerName, DatabaseName, UserName,\r
+ UserPassword, RoleName, CharSet, CreateParams);\r
+ }\r
+\r
+ Transaction TransactionFactory(Database db, TAM am,\r
+ TIL il, TLR lr, TFF flags)\r
+ {\r
+ (void)gds.Call(); // Triggers the initialization, if needed\r
+ return new TransactionImpl( dynamic_cast<DatabaseImpl*>(db.intf()),\r
+ am, il, lr, flags);\r
+ }\r
+\r
+ Statement StatementFactory(Database db, Transaction tr,\r
+ const std::string& sql)\r
+ {\r
+ (void)gds.Call(); // Triggers the initialization, if needed\r
+ return new StatementImpl( dynamic_cast<DatabaseImpl*>(db.intf()),\r
+ dynamic_cast<TransactionImpl*>(tr.intf()),\r
+ sql);\r
+ }\r
+\r
+ Blob BlobFactory(Database db, Transaction tr)\r
+ {\r
+ (void)gds.Call(); // Triggers the initialization, if needed\r
+ return new BlobImpl(dynamic_cast<DatabaseImpl*>(db.intf()),\r
+ dynamic_cast<TransactionImpl*>(tr.intf()));\r
+ }\r
+\r
+ Array ArrayFactory(Database db, Transaction tr)\r
+ {\r
+ (void)gds.Call(); // Triggers the initialization, if needed\r
+ return new ArrayImpl(dynamic_cast<DatabaseImpl*>(db.intf()),\r
+ dynamic_cast<TransactionImpl*>(tr.intf()));\r
+ }\r
+\r
+ Events EventsFactory(Database db)\r
+ {\r
+ (void)gds.Call(); // Triggers the initialization, if needed\r
+ return new EventsImpl(dynamic_cast<DatabaseImpl*>(db.intf()));\r
+ }\r
+\r
+}\r
+\r
+//\r
+// EOF\r
+//\r
+\r