1 /* This program has two different aspect
2 * 1. Use ($./ODBCConTest conn) command to get the timer result
3 * which starts from Connection and goes through pprepare, execute, commit
4 * and ends at Disconnect for each iteration.
6 * 2. Use ($./ODBCConTest prepare) command to get the timer result
8 * which starts at prepare and ends after commit for each iteration.
10 * Author: Jitendra Lenka
19 #include "NanoTimer.h"
23 //#define DSN "DSN=mycsql;MODE=CSQL;SERVER=192.168.1.103;PORT=5678;""DSN=mycsql;MODE=CSQL;SERVER=192.168.1.103;PORT=5678;"
41 "The driver reported the following diagnostics whilst running "
47 ret
= SQLGetDiagRec(type
, handle
, ++i
, state
, &native
, text
,
49 if (SQL_SUCCEEDED(ret
))
50 printf("%s:%ld:%ld:%s\n", state
, i
, native
, text
);
52 while( ret
== SQL_SUCCESS
);
56 check_error (SQLSMALLINT handleType
, SQLHANDLE handle
, int rc
, int line
)
60 cout
<< "Error " << rc
<< " at line: " << line
<< endl
;
65 while (SQLGetDiagRec (handleType
, handle
, i
++, state
, &native
, mesg
, 300,
69 cout
<< "state: " << state
<< endl
;
70 cout
<< "native: " << native
<< endl
;
71 cout
<< "mesg: " << mesg
<< endl
;
79 checkrc (int rc
, int line
)
83 cout
<< "Error " << rc
<< " at line: " << line
<< endl
;
89 readWriteTrans (SQLHDBC hdbc
)
91 int rc
= SQLSetConnectAttr (hdbc
, SQL_ATTR_ACCESS_MODE
,
92 (SQLPOINTER
) SQL_MODE_READ_WRITE
, 0);
93 checkrc (rc
, __LINE__
);
94 rc
= SQLEndTran (SQL_HANDLE_DBC
, hdbc
, SQL_COMMIT
);
95 checkrc (rc
, __LINE__
);
99 readOnlyTrans (SQLHDBC hdbc
)
101 int rc
= SQLSetConnectAttr (hdbc
, SQL_ATTR_ACCESS_MODE
,
102 (SQLPOINTER
) SQL_MODE_READ_ONLY
, 0);
103 checkrc (rc
, __LINE__
);
104 rc
= SQLEndTran (SQL_HANDLE_DBC
, hdbc
, SQL_COMMIT
);
105 checkrc (rc
, __LINE__
);
109 runInsertTest (SQLHANDLE henv
, SQLHANDLE hdbc
, SQLHANDLE hstmt
)
118 for(i
=0;i
<ITERATION
;i
++){
121 rc
= SQLConnect (hdbc
,
122 (SQLCHAR
*) DSN
, SQL_NTS
,
124 (SQLSMALLINT
) strlen ("root"),
125 (SQLCHAR
*) "manager",
126 (SQLSMALLINT
) strlen (""));
128 if (SQL_SUCCEEDED(rc
)) {
129 if (rc
== SQL_SUCCESS_WITH_INFO
) {
130 printf("Driver reported the following diagnostics\n");
131 extract_error("SQLDriverConnect", hdbc
, SQL_HANDLE_DBC
);
134 fprintf(stderr
, "Failed to connect\n");
135 extract_error("SQLDriverConnect", hdbc
, SQL_HANDLE_DBC
);
138 //check_error (SQL_HANDLE_DBC, hdbc, rc, __LINE__);
139 checkrc (rc
, __LINE__
);
141 rc
= SQLSetConnectOption (hdbc
, SQL_AUTOCOMMIT
, SQL_AUTOCOMMIT_OFF
);
142 checkrc (rc
, __LINE__
);
143 rc
= SQLAllocHandle (SQL_HANDLE_STMT
, hdbc
, &hstmt
);
144 checkrc (rc
, __LINE__
);
147 strcpy (sData
, "0123456789012345678901234567890123456789");
148 rc
= SQLPrepare (hstmt
, (unsigned char *) "INSERT INTO t1 VALUES (?, ?)",
150 checkrc (rc
, __LINE__
);
151 rc
= SQLBindParameter (hstmt
, 1, SQL_PARAM_INPUT
, SQL_C_LONG
, SQL_INTEGER
,
152 0, 0, &iHolder
, 0, NULL
);
153 checkrc (rc
, __LINE__
);
154 SQLINTEGER sLen
= SQL_NTS
;
155 rc
= SQLBindParameter (hstmt
, 2, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
,
156 196, 0, (void *) sHolder
, 0, &sLen
);
157 checkrc (rc
, __LINE__
);
161 strcpy (sHolder
, sData
);
162 rc
= SQLExecute (hstmt
);
163 checkrc (rc
, __LINE__
);
164 rc
= SQLTransact (henv
, hdbc
, SQL_COMMIT
);
165 checkrc (rc
, __LINE__
);
168 rc
= SQLFreeHandle (SQL_HANDLE_STMT
, hstmt
);
169 checkrc (rc
, __LINE__
);
170 rc
= SQLDisconnect (hdbc
);
171 checkrc (rc
, __LINE__
);
174 printf ("Insert: %lld %lld %lld\n", timer
.minc(), timer
.maxc(),timer
.avg());
177 //From prepare to Commit (Inserting)
178 int InsertTest(SQLHANDLE henv
, SQLHANDLE hdbc
, SQLHANDLE hstmt
)
187 strcpy (sData
, "0123456789012345678901234567890123456789");
189 for(i
=0;i
<ITERATION
;i
++){
191 rc
= SQLPrepare (hstmt
, (unsigned char *) "INSERT INTO t1 VALUES (?, ?)",
193 checkrc (rc
, __LINE__
);
194 rc
= SQLBindParameter (hstmt
, 1, SQL_PARAM_INPUT
, SQL_C_LONG
, SQL_INTEGER
,
195 0, 0, &iHolder
, 0, NULL
);
196 checkrc (rc
, __LINE__
);
197 SQLINTEGER sLen
= SQL_NTS
;
198 rc
= SQLBindParameter (hstmt
, 2, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
,
199 196, 0, (void *) sHolder
, 0, &sLen
);
200 checkrc (rc
, __LINE__
);
204 strcpy (sHolder
, sData
);
205 rc
= SQLExecute (hstmt
);
206 checkrc (rc
, __LINE__
);
207 rc
= SQLTransact (henv
, hdbc
, SQL_COMMIT
);
208 checkrc (rc
, __LINE__
);
212 printf("Insert: %lld %lld %lld\n", timer
.minc(), timer
.maxc(),timer
.avg());
216 //From Prepare to commit (Read)
217 int ReadTest(SQLHANDLE henv
, SQLHANDLE hdbc
, SQLHANDLE hstmt
)
227 // Set read-only transaction type
228 readOnlyTrans (hdbc
);
230 for(i
=0;i
<ITERATION
;i
++){
232 rc
= SQLPrepare (hstmt
, (unsigned char *)
233 "SELECT f1, f2 FROM t1 where f1 = ?", SQL_NTS
);
234 checkrc (rc
, __LINE__
);
236 rc
= SQLBindParameter (hstmt
, 1, SQL_PARAM_INPUT
, SQL_C_LONG
, SQL_INTEGER
,
237 0, 0, &tempTermVar
, 0, NULL
);
238 checkrc (rc
, __LINE__
);
239 rc
= SQLBindCol (hstmt
, 1, SQL_INTEGER
, &iHolder
, 0, NULL
);
240 checkrc (rc
, __LINE__
);
241 rc
= SQLBindCol (hstmt
, 2, SQL_C_CHAR
, sHolder
, sizeof (sHolder
), NULL
);
242 checkrc (rc
, __LINE__
);
244 strcpy (sData
, "0123456789012345678901234567890123456789");
249 rc
= SQLExecute (hstmt
);
250 checkrc (rc
, __LINE__
);
251 rc
= SQLFetch (hstmt
);
252 checkrc (rc
, __LINE__
);
253 tempTermVal
= iHolder
;
254 strcpy (sData
, sHolder
);
255 rc
= SQLCloseCursor (hstmt
);
256 checkrc (rc
, __LINE__
);
257 rc
= SQLTransact (henv
, hdbc
, SQL_COMMIT
);
258 checkrc (rc
, __LINE__
);
262 printf("Select: %lld %lld %lld\n", timer
.minc(), timer
.maxc(),timer
.avg());
263 readWriteTrans (hdbc
);
270 runReadTest (SQLHANDLE henv
, SQLHANDLE hdbc
, SQLHANDLE hstmt
)
282 for(i
=0;i
<ITERATION
;i
++){
285 rc
= SQLConnect (hdbc
,
286 (SQLCHAR
*) DSN
, SQL_NTS
,
288 (SQLSMALLINT
) strlen ("root"),
289 (SQLCHAR
*) "manager",
290 (SQLSMALLINT
) strlen (""));
292 if (SQL_SUCCEEDED(rc
)) {
293 if (rc
== SQL_SUCCESS_WITH_INFO
) {
294 printf("Driver reported the following diagnostics\n");
295 extract_error("SQLDriverConnect", hdbc
, SQL_HANDLE_DBC
);
298 fprintf(stderr
, "Failed to connect\n");
299 extract_error("SQLDriverConnect", hdbc
, SQL_HANDLE_DBC
);
302 //check_error (SQL_HANDLE_DBC, hdbc, rc, __LINE__);
303 checkrc (rc
, __LINE__
);
304 rc
= SQLSetConnectOption (hdbc
, SQL_AUTOCOMMIT
, SQL_AUTOCOMMIT_OFF
);
305 checkrc (rc
, __LINE__
);
306 rc
= SQLAllocHandle (SQL_HANDLE_STMT
, hdbc
, &hstmt
);
307 checkrc (rc
, __LINE__
);
309 // Set read-only transaction type
310 readOnlyTrans (hdbc
);
311 rc
= SQLPrepare (hstmt
, (unsigned char *)
312 "SELECT f1, f2 FROM t1 where f1 = ?", SQL_NTS
);
313 checkrc (rc
, __LINE__
);
315 rc
= SQLBindParameter (hstmt
, 1, SQL_PARAM_INPUT
, SQL_C_LONG
, SQL_INTEGER
,
316 0, 0, &tempTermVar
, 0, NULL
);
317 checkrc (rc
, __LINE__
);
318 rc
= SQLBindCol (hstmt
, 1, SQL_INTEGER
, &iHolder
, 0, NULL
);
319 checkrc (rc
, __LINE__
);
320 rc
= SQLBindCol (hstmt
, 2, SQL_C_CHAR
, sHolder
, sizeof (sHolder
), NULL
);
321 checkrc (rc
, __LINE__
);
323 strcpy (sData
, "0123456789012345678901234567890123456789");
328 rc
= SQLExecute (hstmt
);
329 checkrc (rc
, __LINE__
);
330 rc
= SQLFetch (hstmt
);
331 checkrc (rc
, __LINE__
);
332 tempTermVal
= iHolder
;
333 strcpy (sData
, sHolder
);
334 rc
= SQLCloseCursor (hstmt
);
335 checkrc (rc
, __LINE__
);
336 rc
= SQLTransact (henv
, hdbc
, SQL_COMMIT
);
337 checkrc (rc
, __LINE__
);
339 rc
= SQLFreeHandle (SQL_HANDLE_STMT
, hstmt
);
340 checkrc (rc
, __LINE__
);
341 rc
= SQLDisconnect (hdbc
);
342 checkrc (rc
, __LINE__
);
346 printf("Select: %lld %lld %lld\n", timer
.minc(), timer
.maxc(),timer
.avg());
351 //From Prepare to Commit(Update
352 int UpdateTest(SQLHANDLE henv
, SQLHANDLE hdbc
, SQLHANDLE hstmt
)
361 for(i
=0;i
<ITERATION
;i
++){
363 rc
= SQLPrepare (hstmt
,
364 (unsigned char *) "UPDATE t1 SET f2 = ? WHERE f1 = ?",
366 checkrc (rc
, __LINE__
);
367 SQLINTEGER sLen
= SQL_NTS
;
368 rc
= SQLBindParameter (hstmt
, 1, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
,
369 196, 0, (void *) sHolder
, 0, &sLen
);
370 checkrc (rc
, __LINE__
);
371 rc
= SQLBindParameter (hstmt
, 2, SQL_PARAM_INPUT
, SQL_C_LONG
, SQL_INTEGER
,
372 0, 0, &iHolder
, 0, NULL
);
373 checkrc (rc
, __LINE__
);
375 strcpy (sData
, "9876543210987654321098765432109876543210");
378 strcpy (sHolder
, sData
);
379 rc
= SQLExecute (hstmt
);
380 checkrc (rc
, __LINE__
);
381 rc
= SQLTransact (henv
, hdbc
, SQL_COMMIT
);
382 checkrc (rc
, __LINE__
);
385 printf ("Update: %lld %lld %lld\n", timer
.minc (), timer
.maxc (),timer
.avg ());
389 runUpdateTest (SQLHANDLE henv
, SQLHANDLE hdbc
, SQLHANDLE hstmt
)
397 for(i
=0;i
<ITERATION
;i
++){
400 rc
= SQLConnect (hdbc
,
401 (SQLCHAR
*) DSN
, SQL_NTS
,
403 (SQLSMALLINT
) strlen ("root"),
404 (SQLCHAR
*) "manager",
405 (SQLSMALLINT
) strlen (""));
407 if (SQL_SUCCEEDED(rc
)) {
408 if (rc
== SQL_SUCCESS_WITH_INFO
) {
409 printf("Driver reported the following diagnostics\n");
410 extract_error("SQLDriverConnect", hdbc
, SQL_HANDLE_DBC
);
413 fprintf(stderr
, "Failed to connect\n");
414 extract_error("SQLDriverConnect", hdbc
, SQL_HANDLE_DBC
);
416 checkrc (rc
, __LINE__
);
417 rc
= SQLSetConnectOption (hdbc
, SQL_AUTOCOMMIT
, SQL_AUTOCOMMIT_OFF
);
418 checkrc (rc
, __LINE__
);
419 rc
= SQLAllocHandle (SQL_HANDLE_STMT
, hdbc
, &hstmt
);
421 rc
= SQLPrepare (hstmt
,
422 (unsigned char *) "UPDATE t1 SET f2 = ? WHERE f1 = ?",
424 checkrc (rc
, __LINE__
);
425 SQLINTEGER sLen
= SQL_NTS
;
426 rc
= SQLBindParameter (hstmt
, 1, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
,
427 196, 0, (void *) sHolder
, 0, &sLen
);
428 checkrc (rc
, __LINE__
);
429 rc
= SQLBindParameter (hstmt
, 2, SQL_PARAM_INPUT
, SQL_C_LONG
, SQL_INTEGER
,
430 0, 0, &iHolder
, 0, NULL
);
431 checkrc (rc
, __LINE__
);
433 strcpy (sData
, "9876543210987654321098765432109876543210");
438 strcpy (sHolder
, sData
);
439 rc
= SQLExecute (hstmt
);
440 checkrc (rc
, __LINE__
);
441 rc
= SQLTransact (henv
, hdbc
, SQL_COMMIT
);
442 checkrc (rc
, __LINE__
);
444 rc
= SQLFreeHandle (SQL_HANDLE_STMT
, hstmt
);
445 checkrc (rc
, __LINE__
);
446 rc
= SQLDisconnect (hdbc
);
447 checkrc (rc
, __LINE__
);
450 printf ("Update: %lld %lld %lld\n", timer
.minc (), timer
.maxc (),timer
.avg ());
454 //From prepare to commit(Delete)
455 int DeleteTest(SQLHANDLE henv
, SQLHANDLE hdbc
, SQLHANDLE hstmt
)
460 for(i
=0;i
<ITERATION
;i
++){
462 rc
= SQLPrepare (hstmt
, (unsigned char *)"DELETE FROM t1 WHERE f1 = ?", SQL_NTS
);
463 checkrc (rc
, __LINE__
);
464 rc
= SQLBindParameter (hstmt
, 1, SQL_PARAM_INPUT
, SQL_C_LONG
, SQL_INTEGER
,
465 0, 0, &iHolder
, 0, NULL
);
466 checkrc (rc
, __LINE__
);
472 rc
= SQLExecute (hstmt
);
473 checkrc (rc
, __LINE__
);
474 rc
= SQLTransact (henv
, hdbc
, SQL_COMMIT
);
475 checkrc (rc
, __LINE__
);
479 printf ("Delete: %lld %lld %lld\n", timer
.minc (), timer
.maxc (),timer
.avg ());
485 runDeleteTest (SQLHANDLE henv
, SQLHANDLE hdbc
, SQLHANDLE hstmt
)
491 for(i
=0;i
<ITERATION
;i
++){
494 rc
= SQLConnect (hdbc
,
495 (SQLCHAR
*) DSN
, SQL_NTS
,
497 (SQLSMALLINT
) strlen ("root"),
498 (SQLCHAR
*) "manager",
499 (SQLSMALLINT
) strlen (""));
501 if (SQL_SUCCEEDED(rc
)) {
502 if (rc
== SQL_SUCCESS_WITH_INFO
) {
503 printf("Driver reported the following diagnostics\n");
504 extract_error("SQLDriverConnect", hdbc
, SQL_HANDLE_DBC
);
507 fprintf(stderr
, "Failed to connect\n");
508 extract_error("SQLDriverConnect", hdbc
, SQL_HANDLE_DBC
);
511 //check_error (SQL_HANDLE_DBC, hdbc, rc, __LINE__);
512 checkrc (rc
, __LINE__
);
513 rc
= SQLSetConnectOption (hdbc
, SQL_AUTOCOMMIT
, SQL_AUTOCOMMIT_OFF
);
514 checkrc (rc
, __LINE__
);
516 rc
= SQLAllocHandle (SQL_HANDLE_STMT
, hdbc
, &hstmt
);
517 checkrc (rc
, __LINE__
);
520 rc
= SQLPrepare (hstmt
, (unsigned char *)"DELETE FROM t1 WHERE f1 = ?", SQL_NTS
);
521 checkrc (rc
, __LINE__
);
522 rc
= SQLBindParameter (hstmt
, 1, SQL_PARAM_INPUT
, SQL_C_LONG
, SQL_INTEGER
,
523 0, 0, &iHolder
, 0, NULL
);
524 checkrc (rc
, __LINE__
);
530 rc
= SQLExecute (hstmt
);
531 checkrc (rc
, __LINE__
);
532 rc
= SQLTransact (henv
, hdbc
, SQL_COMMIT
);
533 checkrc (rc
, __LINE__
);
535 rc
= SQLFreeHandle (SQL_HANDLE_STMT
, hstmt
);
536 checkrc (rc
, __LINE__
);
537 rc
= SQLDisconnect (hdbc
);
538 checkrc (rc
, __LINE__
);
542 printf ("Delete: %lld %lld %lld\n", timer
.minc (), timer
.maxc (),timer
.avg ());
548 main (int ac
, char *av
[])
553 if(strcmp(av
[1],"conn")==0)flag
=1;
554 else if(strcmp(av
[1],"prepare")==0)flag
=2;
557 printf("\nValid value: conn or prepare\n");
567 rc
= SQLAllocHandle (SQL_HANDLE_ENV
, SQL_NULL_HANDLE
, &henv
);
568 checkrc (rc
, __LINE__
);
569 SQLSetEnvAttr(henv
, SQL_ATTR_ODBC_VERSION
, (void *) SQL_OV_ODBC3
, 0);
571 rc
= SQLAllocHandle (SQL_HANDLE_DBC
, henv
, &hdbc
);
572 checkrc (rc
, __LINE__
);
574 rc
= SQLConnect (hdbc
,
575 (SQLCHAR
*) DSN
, SQL_NTS
,
577 (SQLSMALLINT
) strlen ("root"),
578 (SQLCHAR
*) "manager",
579 (SQLSMALLINT
) strlen (""));
581 SQLCHAR outstr
[1024];
582 SQLSMALLINT outstrlen
;
584 if (SQL_SUCCEEDED(rc
)) {
585 // printf("Connected\n");
586 if (rc
== SQL_SUCCESS_WITH_INFO
) {
587 printf("Driver reported the following diagnostics\n");
588 extract_error("SQLDriverConnect", hdbc
, SQL_HANDLE_DBC
);
591 fprintf(stderr
, "Failed to connect\n");
592 extract_error("SQLDriverConnect", hdbc
, SQL_HANDLE_DBC
);
595 //check_error (SQL_HANDLE_DBC, hdbc, rc, __LINE__);
596 checkrc (rc
, __LINE__
);
597 rc
= SQLSetConnectOption (hdbc
, SQL_AUTOCOMMIT
, SQL_AUTOCOMMIT_OFF
);
598 checkrc (rc
, __LINE__
);
599 rc
= SQLAllocHandle (SQL_HANDLE_STMT
, hdbc
, &hstmt
);
600 checkrc (rc
, __LINE__
);
601 rc
= SQLExecDirect(hstmt
, (SQLCHAR
*) "CREATE TABLE t1 (f1 INTEGER, f2 char(196), primary key (f1));", SQL_NTS
);
602 checkrc (rc
, __LINE__
);
603 printf("Table 't1' Created\n");
606 //Disconnect from the data source
608 printf("-----------------------------------\n");
609 printf("Stmt: | Min | Max | Avg\n");
610 printf("-----------------------------------\n");
614 rc
= SQLFreeHandle (SQL_HANDLE_STMT
, hstmt
);
615 checkrc (rc
, __LINE__
);
616 rc
= SQLDisconnect (hdbc
);
617 checkrc (rc
, __LINE__
);
619 runInsertTest (henv
, hdbc
, hstmt
);
620 runReadTest (henv
, hdbc
, hstmt
);
621 runUpdateTest (henv
, hdbc
, hstmt
);
622 runDeleteTest (henv
, hdbc
, hstmt
);
625 rc
= SQLConnect (hdbc
,
626 (SQLCHAR
*) DSN
, SQL_NTS
,
628 (SQLSMALLINT
) strlen ("root"),
629 (SQLCHAR
*) "manager",
630 (SQLSMALLINT
) strlen (""));
632 if (SQL_SUCCEEDED(rc
)) {
633 // printf("Connected\n");
634 if (rc
== SQL_SUCCESS_WITH_INFO
) {
635 printf("Driver reported the following diagnostics\n");
636 extract_error("SQLDriverConnect", hdbc
, SQL_HANDLE_DBC
);
639 fprintf(stderr
, "Failed to connect\n");
640 extract_error("SQLDriverConnect", hdbc
, SQL_HANDLE_DBC
);
643 check_error (SQL_HANDLE_DBC
, hdbc
, rc
, __LINE__
);
644 checkrc (rc
, __LINE__
);
645 rc
= SQLSetConnectOption (hdbc
, SQL_AUTOCOMMIT
, SQL_AUTOCOMMIT_OFF
);
646 checkrc (rc
, __LINE__
);
647 rc
= SQLAllocHandle (SQL_HANDLE_STMT
, hdbc
, &hstmt
);
653 InsertTest (henv
, hdbc
, hstmt
);
654 ReadTest (henv
, hdbc
, hstmt
);
655 UpdateTest (henv
, hdbc
, hstmt
);
656 DeleteTest (henv
, hdbc
, hstmt
);
660 printf("----------------------------------\n");
661 printf(" |Unit: NanoSec|\n");
664 rc
= SQLExecDirect(hstmt
, (SQLCHAR
*) "DROP TABLE t1;", SQL_NTS
);
665 checkrc (rc
, __LINE__
);
666 printf("Table 't1' Dropped\n");
667 rc
= SQLEndTran(SQL_HANDLE_DBC
, hdbc
, SQL_COMMIT
);
668 checkrc (rc
, __LINE__
);
670 rc
= SQLFreeHandle (SQL_HANDLE_STMT
, hstmt
);
671 checkrc (rc
, __LINE__
);
672 rc
= SQLDisconnect (hdbc
);
673 checkrc (rc
, __LINE__
);
674 rc
= SQLFreeHandle (SQL_HANDLE_DBC
, hdbc
);
675 checkrc (rc
, __LINE__
);
676 rc
= SQLFreeHandle (SQL_HANDLE_ENV
, henv
);
677 checkrc (rc
, __LINE__
);