Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.86 KB | None | 0 0
  1. Developement Machine
  2. CentOS release 6.1 (Final)
  3. 2.6.32-131.0.15.el6.i686 {32bit machine}
  4.  
  5. Deployment Machine (CentOS)
  6. Linux release 6.6 (Final)
  7. 2.6.32-573.el6.x86_64 {64bit machine}
  8. unixODBC 2.2.14
  9. /usr/lib/psqlodbcw.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, stripped
  10.  
  11. #include "../boost_1_52_0/boost/property_tree/ptree.hpp"
  12. #include "../boost_1_52_0/boost/property_tree/ini_parser.hpp"
  13. #include <iostream>
  14. #include <string>
  15. #include <vector>
  16. #include <utility>
  17. #include <unistd.h>
  18. #include "../unixODBC-2.3.4/include/sql.h"
  19. #include "../unixODBC-2.3.4/include/sqlext.h"
  20. #include "../unixODBC-2.3.4/include/odbcinst.h"
  21.  
  22. using boost::property_tree::ptree;
  23. using namespace std;
  24.  
  25. void PopulateINI(const string& iniName, vector<pair<string, string> >& data);
  26. bool TestConnection(const string& connStringIn, string& connStringOut);
  27. static void extract_error(char *fn, SQLHANDLE handle, SQLSMALLINT type);
  28. void PrintIniFile(const string& iniName, const string& sDSN);
  29.  
  30.  
  31. int main(int argc, char* argv[])
  32. {
  33.  
  34. if (argc != 2)
  35. {
  36. cout << "Enter the choice ofnt1 : Full run:- ntttpopulate incorrect datantttattempt to connectntttpopulate CORRECT datantttwait 15 secsntttattempt to connectnt2 : attempt to connect with existing ini data" << endl;
  37. return 0;
  38. }
  39.  
  40. int iCh = atoi(argv[1]);
  41.  
  42. if(iCh != 1 && iCh != 2)
  43. {
  44. cout << "Invalid choice !!nAcceptable values are - '1' OR '2' only" << endl;
  45. return 0;
  46. }
  47.  
  48.  
  49. string sDSN = "PostgresTest01";
  50. string sConnStrIn, sConnStrOut;
  51. sConnStrIn.append("DSN=").append(sDSN.c_str()).append(1, ';');
  52.  
  53. string iniName = "/etc/odbc.ini";
  54.  
  55. if (iCh == 1)
  56. {
  57. //Incorrect DSN data
  58.  
  59. vector<pair<string, string> > vData;
  60.  
  61. vData.push_back(make_pair(sDSN + ".Description", "Description"));
  62. vData.push_back(make_pair(sDSN + ".Driver", "PostgreSQL"));
  63. vData.push_back(make_pair(sDSN + ".Database", "dvdrental"));
  64. vData.push_back(make_pair(sDSN + ".Servername", "192.168.45.217"));
  65. vData.push_back(make_pair(sDSN + ".Port", "1234")); //INCORRECT PORT NUMBER; '1234' instead of '5432'
  66. vData.push_back(make_pair(sDSN + ".UserName", "postgres"));
  67. vData.push_back(make_pair(sDSN + ".Password", "postgres"));
  68. vData.push_back(make_pair(sDSN + ".Trace", "Off"));
  69. vData.push_back(make_pair(sDSN + ".TraceFile", "stderr"));
  70. vData.push_back(make_pair(sDSN + ".Protocol", "7.0"));
  71. vData.push_back(make_pair(sDSN + ".ReadOnly", "No"));
  72. vData.push_back(make_pair(sDSN + ".RowVersioning", "No"));
  73. vData.push_back(make_pair(sDSN + ".ShowSystemTables", "No"));
  74. vData.push_back(make_pair(sDSN + ".ShowOidColumn", "No"));
  75. vData.push_back(make_pair(sDSN + ".FakeOidIndex", "No"));
  76. vData.push_back(make_pair(sDSN + ".ConnSettings", ""));
  77.  
  78.  
  79. //Populate ini with Incorrect data
  80. PopulateINI(iniName, vData);
  81. sleep(5); //Just so I can see the ini file changing
  82.  
  83. //First run - Call SQLDriverConnect
  84. PrintIniFile(iniName, sDSN);
  85. sConnStrOut.clear();
  86. if(TestConnection(sConnStrIn, sConnStrOut))
  87. {
  88. cout << "Test connection succeeded.nConnection String is [" << sConnStrOut << "]" << endl;
  89. }
  90. else
  91. {
  92. cout << "Test connection failed for sConnStrIn[" << sConnStrIn << "]" << endl;
  93. }
  94.  
  95.  
  96. cout << "nn====================================================================" << endl;
  97.  
  98. cout << "Updating ini file with correct data..." << endl;
  99.  
  100. vData[4].second = "5432"; //CORRECT PORT NUMBER
  101. PopulateINI(iniName, vData); //WRITE TO INI FILE
  102.  
  103. cout << "nnWaiting for 15 secs" << endl;
  104. sleep(15); //15, so that I could manually change the odbc.ini, as I had some suspicions about ptree, read_ini() & write_ini()
  105.  
  106. cout << "nn====================================================================" << endl;
  107. }
  108.  
  109. //Second run - Call SQLDriverConnect
  110. PrintIniFile(iniName, sDSN);
  111. sConnStrOut.clear();
  112. if(TestConnection(sConnStrIn, sConnStrOut))
  113. {
  114. cout << "Test connection succeeded.nConnection String is [" << sConnStrOut << "]" << endl;;
  115. }
  116. else
  117. {
  118. cout << "Test connection failed for sConnStrIn[" << sConnStrIn << "]" << endl;
  119. }
  120.  
  121. return 0;
  122. }
  123.  
  124. void PrintVector(const string& label, vector<pair<string, string> >& data)
  125. {
  126. cout << "nn " << label << "n" << endl;
  127.  
  128. for(vector<pair<string, string> >::iterator it = data.begin(); it != data.end(); ++it)
  129. {
  130. cout << "tt" << it->first << " : " << it->second << endl;
  131. }
  132. cout << "n===================================================" << endl;
  133. }
  134.  
  135. void PopulateINI(const string& iniName, vector<pair<string, string> >& data)
  136. {
  137. ptree pt;
  138. read_ini(iniName.c_str(), pt);
  139.  
  140. for(vector<pair<string, string> >::iterator it = data.begin(); it != data.end(); ++it)
  141. {
  142. pt.put(it->first.c_str(), it->second.c_str());
  143. }
  144. write_ini(iniName.c_str(), pt);
  145. }
  146.  
  147. bool TestConnection(const string& connStringIn, string& connStringOut)
  148. {
  149. bool fRC = false;
  150. SQLRETURN retcode;
  151. SQLHENV env=NULL;
  152. SQLHDBC dbc=NULL;
  153. SQLSMALLINT siOutConnStrLen;
  154.  
  155. connStringOut.resize(2048);
  156.  
  157. SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
  158. SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
  159. SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
  160. retcode = SQLDriverConnect(dbc, NULL, (SQLCHAR*)connStringIn.c_str(), SQL_NTS, (SQLCHAR*)&connStringOut.at(0), 2048, &siOutConnStrLen, SQL_DRIVER_NOPROMPT);
  161.  
  162. if(SQL_SUCCEEDED(retcode))
  163. {
  164. connStringOut.resize(siOutConnStrLen);
  165. fRC = true;
  166. if(retcode == SQL_SUCCESS_WITH_INFO)
  167. {
  168. cout << "Driver reported the following diagnostics:" << endl;
  169. extract_error("SQLDriverConnect", dbc, SQL_HANDLE_DBC);
  170. }
  171. SQLDisconnect(dbc);
  172. }
  173. else
  174. {
  175. cout << "Failed to connect:" << endl;
  176. extract_error("SQLDriverConnect", dbc, SQL_HANDLE_DBC);
  177. }
  178.  
  179. SQLFreeHandle(SQL_HANDLE_DBC, dbc);
  180. SQLFreeHandle(SQL_HANDLE_ENV, env);
  181.  
  182. return fRC;
  183. }
  184.  
  185.  
  186. void extract_error(char *fn, SQLHANDLE handle, SQLSMALLINT type)
  187. {
  188. SQLINTEGER i = 0;
  189. SQLINTEGER native;
  190. SQLCHAR state[ 7 ];
  191. SQLCHAR text[256];
  192. SQLSMALLINT len;
  193. SQLRETURN ret;
  194.  
  195. fprintf(stderr, "nThe driver reported the following diagnostics whilst running %snn", fn);
  196.  
  197. do
  198. {
  199. ret = SQLGetDiagRec(type, handle, ++i, state, &native, text, sizeof(text), &len );
  200. if (SQL_SUCCEEDED(ret))
  201. printf("%s:%ld:%ld:%sn", state, i, native, text);
  202. }
  203. while( ret == SQL_SUCCESS );
  204. }
  205.  
  206.  
  207. void PrintIniFile(const string& iniName, const string& sDSN)
  208. {
  209. ptree pt;
  210.  
  211. read_ini(iniName.c_str(), pt);
  212.  
  213.  
  214. cout << "nn[" << sDSN << "]" << endl;
  215.  
  216. cout << "Description : " << pt.get<string>((sDSN + "." + "Description").c_str()) <<endl;
  217. cout << "Driver : " << pt.get<string>((sDSN + "." + "Driver").c_str()) <<endl;
  218. cout << "Database : " << pt.get<string>((sDSN + "." + "Database").c_str()) <<endl;
  219. cout << "Servername : " << pt.get<string>((sDSN + "." + "Servername").c_str()) <<endl;
  220. cout << "Port : " << pt.get<string>((sDSN + "." + "Port").c_str()) <<endl;
  221. cout << "UserName : " << pt.get<string>((sDSN + "." + "UserName").c_str()) <<endl;
  222. cout << "Password : " << pt.get<string>((sDSN + "." + "Password").c_str()) <<endl;
  223. cout << "Trace : " << pt.get<string>((sDSN + "." + "Trace").c_str()) <<endl;
  224. cout << "TraceFile : " << pt.get<string>((sDSN + "." + "TraceFile").c_str()) <<endl;
  225. cout << "Protocol : " << pt.get<string>((sDSN + "." + "Protocol").c_str()) <<endl;
  226. cout << "ReadOnly : " << pt.get<string>((sDSN + "." + "ReadOnly").c_str()) <<endl;
  227. cout << "RowVersioning : " << pt.get<string>((sDSN + "." + "RowVersioning").c_str()) <<endl;
  228. cout << "ShowSystemTables : " << pt.get<string>((sDSN + "." + "ShowSystemTables").c_str()) <<endl;
  229. cout << "ShowOidColumn : " << pt.get<string>((sDSN + "." + "ShowOidColumn").c_str()) <<endl;
  230. cout << "FakeOidIndex : " << pt.get<string>((sDSN + "." + "FakeOidIndex").c_str()) <<endl;
  231. cout << "ConnSettings : " << pt.get<string>((sDSN + "." + "ConnSettings").c_str()) <<endl;
  232. cout << "nn" << endl;
  233. }
  234.  
  235. 1 : Full run:-
  236. populate incorrect data
  237. attempt to connect
  238. populate CORRECT data
  239. wait 15 secs
  240. attempt to connect
  241. 2 : attempt to connect with existing ini data
  242.  
  243. [PostgresTest01]
  244. Description : Description
  245. Driver : PostgreSQL
  246. Database : dvdrental
  247. Servername : 192.168.45.217
  248. Port : 1234
  249. UserName : postgres
  250. Password : postgres
  251. Trace : Off
  252. TraceFile : stderr
  253. Protocol : 7.0
  254. ReadOnly : No
  255. RowVersioning : No
  256. ShowSystemTables : No
  257. ShowOidColumn : No
  258. FakeOidIndex : No
  259. ConnSettings :
  260.  
  261.  
  262.  
  263. Failed to connect:
  264.  
  265. The driver reported the following diagnostics whilst running SQLDriverConnect
  266.  
  267. 08001:1:101:[unixODBC]Could not connect to the server;
  268. Connection refused [192.168.45.217:1234]
  269. Test connection failed for sConnStrIn[DSN=PostgresTest01;]
  270.  
  271.  
  272. ====================================================================
  273. Updating ini file with correct data...
  274.  
  275.  
  276. Waiting for 15 secs
  277.  
  278.  
  279. ====================================================================
  280.  
  281.  
  282. [PostgresTest01]
  283. Description : Description
  284. Driver : PostgreSQL
  285. Database : dvdrental
  286. Servername : 192.168.45.217
  287. Port : 5432
  288. UserName : postgres
  289. Password : postgres
  290. Trace : Off
  291. TraceFile : stderr
  292. Protocol : 7.0
  293. ReadOnly : No
  294. RowVersioning : No
  295. ShowSystemTables : No
  296. ShowOidColumn : No
  297. FakeOidIndex : No
  298. ConnSettings :
  299.  
  300.  
  301.  
  302. Failed to connect:
  303.  
  304. The driver reported the following diagnostics whilst running SQLDriverConnect
  305.  
  306. 08001:1:101:[unixODBC]Could not connect to the server;
  307. Connection refused [192.168.45.217:1234]
  308. Test connection failed for sConnStrIn[DSN=PostgresTest01;]
  309.  
  310. [PostgresTest01]
  311. Description : Description
  312. Driver : PostgreSQL
  313. Database : dvdrental
  314. Servername : 192.168.45.217
  315. Port : 5432
  316. UserName : postgres
  317. Password : postgres
  318. Trace : Off
  319. TraceFile : stderr
  320. Protocol : 7.0
  321. ReadOnly : No
  322. RowVersioning : No
  323. ShowSystemTables : No
  324. ShowOidColumn : No
  325. FakeOidIndex : No
  326. ConnSettings :
  327.  
  328.  
  329.  
  330. Test connection succeeded.
  331. Connection String is [DSN=PostgresTest01;DATABASE=dvdrental;SERVER=192.168.45.217;PORT=5432;UID=postgres;PWD=postgres;SSLmode=disable;ReadOnly=No;Protocol=7.0;FakeOidIndex=No;ShowOidColumn=No;RowVersioning=No;ShowSystemTables=No;ConnSettings=;Fetch=100;Socket=4096;UnknownSizes=0;MaxVarcharSize=255;MaxLongVarcharSize=8190;Debug=0;CommLog=0;Optimizer=0;Ksqo=1;UseDeclareFetch=0;TextAsLongVarchar=1;UnknownsAsLongVarchar=0;BoolsAsChar=1;Parse=0;CancelAsFreeStmt=0;ExtraSysTablePrefixes=dd_;;LFConversion=0;UpdatableCursors=1;DisallowPremature=0;TrueIsMinus1=0;BI=0;ByteaAsLongVarBinary=0;UseServerSidePrepare=0;LowerCaseIdentifier=0;]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement