Advertisement
Guest User

Untitled

a guest
May 26th, 2017
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.81 KB | None | 0 0
  1. /*
  2. ** Copyright (c) 2010 Ingres Corporation
  3. */
  4.  
  5.  
  6. /*
  7. ** Name: api_batch_fetch.c
  8. **
  9. ** Description:
  10. ** Demonstrates using IIapi_query(), IIapi_getDescriptor(),
  11. ** IIapi_getColumns() and IIapi_close() to batch fetch the results
  12. ** from the server.
  13. **
  14. ** SELECT data using a cursor.
  15. **
  16. ** Following actions are demonstrated in the main()
  17. ** Issue query
  18. ** Get query result descriptors
  19. ** Get query results, fetching upto 100 rows.
  20. ** Cancel query processing
  21. ** Free query resources.
  22. **
  23. ** Command syntax:
  24. ** api_batch_fetch database_name
  25. **
  26. ** To build on UNIX/Linux: (change gcc to the C compiler for your system)
  27. ** gcc -g -o apibatch_fetch apibatch_fetch.c -I$II_SYSTEM/ingres/files -L$II_SYSTEM/ingres/lib -liiapi.1 -lframe.1
  28. ** To build on Windows:
  29. ** cl /nologo /Zi apibatch_fetch.c iilibapi.lib
  30. **
  31. ** History:
  32. ** 11-Dec-2009 - grant.croker@ingres.com
  33. ** Initial version published
  34. ** 20-Jan-2009 - grant.croker@ingres.com
  35. ** Fix up some formatting bugs
  36. ** 21-Jan-2009 - grant.croker@ingres.com
  37. ** Allocate the DataBuffer in one go rather than call
  38. ** malloc() # columns * ROW_COUNT times
  39. ** Fix a SEGV when referencing non-allocated memory
  40. ** 22-Jan-2009 - grant.croker@ingres.com
  41. ** Make use of envHandle returned from IIapi_initialize()
  42. ** (Not strictly necessary for this code to work)
  43. */
  44.  
  45.  
  46. # include <stdio.h>
  47. # include <stdlib.h>
  48. # include <iiapi.h>
  49. # include <string.h>
  50.  
  51.  
  52. static void IIdemo_init(II_PTR *);
  53. static void IIdemo_term();
  54. static void IIdemo_conn( char *, II_PTR *, II_PTR );
  55. static void IIdemo_disconn( II_PTR * );
  56. static void IIdemo_rollback( II_PTR * );
  57.  
  58. static char queryText[] = "select table_name, table_owner, create_date from iitables";
  59. static char moneyqueryText[] = "select money(100.22), float8(100.2243242 - 70.00), varchar(money(100.22 - 5.00)) as change, date('now'), date('now') from ii_id";
  60.  
  61. #define ROW_COUNT 100
  62.  
  63.  
  64.  
  65. /*
  66. ** Name: main - main loop
  67. **
  68. ** Description:
  69. ** This function is the main loop of the sample code.
  70. */
  71.  
  72. int
  73. main( int argc, char** argv )
  74. {
  75. II_PTR connHandle = (II_PTR)NULL;
  76. II_PTR tranHandle = (II_PTR)NULL;
  77. II_PTR stmtHandle = (II_PTR)NULL;
  78. II_PTR envHandle = (II_PTR)NULL;
  79. IIAPI_QUERYPARM queryParm;
  80. IIAPI_SETDESCRPARM setDescrParm;
  81. IIAPI_PUTPARMPARM putParmParm;
  82. IIAPI_GETDESCRPARM getDescrParm;
  83. IIAPI_GETCOLPARM getColParm;
  84. IIAPI_GETQINFOPARM getQInfoParm;
  85. IIAPI_CLOSEPARM closeParm;
  86. IIAPI_WAITPARM waitParm = { -1 };
  87. IIAPI_DATAVALUE *ColumnData = NULL;
  88. II_PTR DataBuffer = NULL;
  89. char varvalue[ROW_COUNT][33];
  90. int i, cell, row, col, row_width=0;
  91. II_PTR column_ptr;
  92. char * DataBuffer_ptr = NULL;
  93. long int DataBufferSize=0;
  94.  
  95. if ( argc != 2 )
  96. {
  97. printf( "usage: apisselc [vnode::]dbname[/server_class]\n" );
  98. exit( 0 );
  99. }
  100.  
  101. IIdemo_init(&envHandle);
  102. IIdemo_conn(argv[1], &connHandle, envHandle);
  103. printf("connHandle is %p\n",connHandle);
  104.  
  105. /*
  106. ** Open cursor
  107. */
  108. printf( "apisselc: opening cursor\n" );
  109.  
  110. queryParm.qy_genParm.gp_callback = NULL;
  111. queryParm.qy_genParm.gp_closure = NULL;
  112. queryParm.qy_connHandle = connHandle;
  113. queryParm.qy_queryType = IIAPI_QT_OPEN;
  114. queryParm.qy_queryText = moneyqueryText;
  115. queryParm.qy_parameters = FALSE;
  116. queryParm.qy_tranHandle = tranHandle;
  117. queryParm.qy_stmtHandle = NULL;
  118.  
  119. IIapi_query( &queryParm );
  120.  
  121. while( queryParm.qy_genParm.gp_completed == FALSE )
  122. IIapi_wait( &waitParm );
  123.  
  124. tranHandle = queryParm.qy_tranHandle;
  125. stmtHandle = queryParm.qy_stmtHandle;
  126.  
  127. /*
  128. ** Get query result descriptors.
  129. */
  130. printf( "apisselc: get descriptors\n" );
  131.  
  132. getDescrParm.gd_genParm.gp_callback = NULL;
  133. getDescrParm.gd_genParm.gp_closure = NULL;
  134. getDescrParm.gd_stmtHandle = stmtHandle;
  135. getDescrParm.gd_descriptorCount = 0;
  136. getDescrParm.gd_descriptor = NULL;
  137.  
  138. IIapi_getDescriptor( &getDescrParm );
  139.  
  140. while( getDescrParm.gd_genParm.gp_completed == FALSE )
  141. IIapi_wait( &waitParm );
  142.  
  143. printf("Cols in result : %d\n",getDescrParm.gd_descriptorCount);
  144.  
  145. /*
  146. ** Get query results
  147. */
  148. printf( "apisselc: get results\n" );
  149.  
  150. /* Row width */
  151. for(col = 0; col < getDescrParm.gd_descriptorCount; col++)
  152. {
  153. row_width += getDescrParm.gd_descriptor[col].ds_length + 1;
  154. printf ("Col %d - size %d\n", col + 1, getDescrParm.gd_descriptor[col].ds_length);
  155. }
  156. printf ("%d columns - row width %d\n", getDescrParm.gd_descriptorCount, row_width);
  157.  
  158. /* Assign memory to the DataBuffer */
  159. DataBufferSize = row_width * ROW_COUNT;
  160. printf ("Allocating %ld bytes\n", DataBufferSize);
  161. DataBuffer = (II_PTR) malloc(DataBufferSize);
  162. memset (DataBuffer, 0, row_width * ROW_COUNT);
  163.  
  164. ColumnData = (IIAPI_DATAVALUE *)malloc(sizeof(IIAPI_DATAVALUE) * getDescrParm.gd_descriptorCount * ROW_COUNT);
  165.  
  166. getColParm.gc_genParm.gp_callback = NULL;
  167. getColParm.gc_genParm.gp_closure = NULL;
  168. getColParm.gc_rowCount = ROW_COUNT;
  169. getColParm.gc_columnCount = getDescrParm.gd_descriptorCount;
  170. getColParm.gc_columnData = ColumnData;
  171. getColParm.gc_stmtHandle = stmtHandle;
  172. getColParm.gc_moreSegments = 0;
  173.  
  174. /* Assign ColumnData[cell].dv_value it's space in DataBuffer*/
  175. DataBuffer_ptr = DataBuffer;
  176. for( cell = 0; cell < (getDescrParm.gd_descriptorCount * ROW_COUNT); cell++ )
  177. {
  178. ColumnData[cell].dv_value = DataBuffer_ptr;
  179. *DataBuffer_ptr += (getDescrParm.gd_descriptor[cell % getDescrParm.gd_descriptorCount ]).ds_length + 1;
  180. }
  181.  
  182. IIapi_getColumns( &getColParm );
  183.  
  184. while( getColParm.gc_genParm.gp_completed == FALSE )
  185. IIapi_wait( &waitParm );
  186.  
  187. printf("Rows returned %d\n", getColParm.gc_rowsReturned);
  188. /* Print headers */
  189. for(col = 0; col < getDescrParm.gd_descriptorCount; col++)
  190. {
  191. printf ("%s\t", getDescrParm.gd_descriptor[col].ds_columnName);
  192. }
  193. printf ("\n");
  194.  
  195. for(row = 0; row < getColParm.gc_rowsReturned; row++)
  196. {
  197. printf ("Row %d - ", row + 1);
  198. for (col = 0; col < getDescrParm.gd_descriptorCount; col ++ )
  199. {
  200. cell = (row * getDescrParm.gd_descriptorCount) + col;
  201. ((char *)ColumnData[cell].dv_value)[ColumnData[cell].dv_length] = '\0';
  202. printf ("%s\t", (char *)ColumnData[cell].dv_value);
  203. }
  204. printf ("\n");
  205. }
  206.  
  207. /*
  208. ** Get fetch result info.
  209. */
  210. getQInfoParm.gq_genParm.gp_callback = NULL;
  211. getQInfoParm.gq_genParm.gp_closure = NULL;
  212. getQInfoParm.gq_stmtHandle = stmtHandle;
  213.  
  214. IIapi_getQueryInfo( &getQInfoParm );
  215.  
  216. while( getQInfoParm.gq_genParm.gp_completed == FALSE )
  217. IIapi_wait( &waitParm );
  218.  
  219. /*
  220. ** Free query resources.
  221. */
  222. printf( "apisselc: close cursor\n" );
  223.  
  224. closeParm.cl_genParm.gp_callback = NULL;
  225. closeParm.cl_genParm.gp_closure = NULL;
  226. closeParm.cl_stmtHandle = stmtHandle;
  227.  
  228. IIapi_close( &closeParm );
  229.  
  230. /* Free allocated memory */
  231. free(DataBuffer);
  232. free(ColumnData);
  233.  
  234. while( closeParm.cl_genParm.gp_completed == FALSE )
  235. IIapi_wait( &waitParm );
  236.  
  237. IIdemo_rollback(&tranHandle);
  238. IIdemo_disconn(&connHandle);
  239. IIdemo_term();
  240.  
  241. return( 0 );
  242. }
  243.  
  244.  
  245. /*
  246. ** Name: IIdemo_init
  247. **
  248. ** Description:
  249. ** Initialize API access.
  250. **
  251. ** Input:
  252. ** None.
  253. **
  254. ** Output:
  255. ** None.
  256. **
  257. ** Return value:
  258. ** None.
  259. */
  260.  
  261. static void
  262. IIdemo_init(II_PTR * envHandle)
  263. {
  264. IIAPI_INITPARM initParm;
  265.  
  266. printf( "IIdemo_init: initializing API\n" );
  267. initParm.in_version = IIAPI_VERSION;
  268. initParm.in_timeout = -1;
  269. IIapi_initialize( &initParm );
  270.  
  271. *envHandle = initParm.in_envHandle;
  272.  
  273. return;
  274. }
  275.  
  276.  
  277. /*
  278. ** Name: IIdemo_term
  279. **
  280. ** Description:
  281. ** Terminate API access.
  282. **
  283. ** Input:
  284. ** None.
  285. **
  286. ** Output:
  287. ** None.
  288. **
  289. ** Return value:
  290. ** None.
  291. */
  292.  
  293. static void
  294. IIdemo_term()
  295. {
  296. IIAPI_TERMPARM termParm;
  297.  
  298. printf( "IIdemo_term: shutting down API\n" );
  299. IIapi_terminate( &termParm );
  300.  
  301. return;
  302. }
  303.  
  304.  
  305. /*
  306. ** Name: IIdemo_conn
  307. **
  308. ** Description:
  309. ** Open connection with target Database.
  310. **
  311. ** Input:
  312. ** dbname Database name.
  313. **
  314. ** Output:
  315. ** connHandle Connection handle.
  316. **
  317. ** Return value:
  318. ** None.
  319. */
  320.  
  321. static void
  322. IIdemo_conn( char *dbname, II_PTR *connHandle, II_PTR envHandle )
  323. {
  324. IIAPI_CONNPARM connParm;
  325. IIAPI_WAITPARM waitParm = { -1 };
  326. IIAPI_SETCONPRMPARM setConPrmParm;
  327.  
  328. char money_sign[1] = "#";
  329.  
  330. setConPrmParm.sc_genParm.gp_callback = NULL;
  331. setConPrmParm.sc_connHandle = envHandle;
  332. setConPrmParm.sc_paramID = IIAPI_CP_MONEY_SIGN;
  333. setConPrmParm.sc_paramValue =&money_sign;
  334.  
  335. IIapi_setConnectParam( &setConPrmParm );
  336.  
  337. while( setConPrmParm.sc_genParm.gp_completed == FALSE )
  338. IIapi_wait( &waitParm );
  339.  
  340. connHandle = setConPrmParm.sc_connHandle;
  341.  
  342. printf( "IIdemo_conn: establishing connection\n" );
  343.  
  344. connParm.co_genParm.gp_callback = NULL;
  345. connParm.co_genParm.gp_closure = NULL;
  346. connParm.co_target = dbname;
  347. connParm.co_connHandle = connHandle ? connHandle : NULL;
  348. connParm.co_tranHandle = NULL;
  349. connParm.co_username = NULL;
  350. connParm.co_password = NULL;
  351. connParm.co_timeout = -1;
  352. connParm.co_type = IIAPI_CT_SQL;
  353.  
  354. IIapi_connect( &connParm );
  355.  
  356. while( connParm.co_genParm.gp_completed == FALSE )
  357. IIapi_wait( &waitParm );
  358.  
  359. printf ("Connected with IIAPI_LEVEL_%d\n",connParm.co_apiLevel);
  360.  
  361. *connHandle = connParm.co_connHandle;
  362. printf("connHandle is %p\n",connHandle);
  363. return;
  364. }
  365.  
  366.  
  367. /*
  368. ** Name: IIdemo_disconn
  369. **
  370. ** Description:
  371. ** Release DBMS connection.
  372. **
  373. ** Input:
  374. ** connHandle Connection handle.
  375. **
  376. ** Output:
  377. ** connHandle Connection handle.
  378. **
  379. ** Return value:
  380. ** None.
  381. */
  382.  
  383. static void
  384. IIdemo_disconn( II_PTR *connHandle )
  385. {
  386. IIAPI_DISCONNPARM disconnParm;
  387. IIAPI_WAITPARM waitParm = { -1 };
  388.  
  389. printf( "IIdemo_disconn: releasing connection\n" );
  390.  
  391. disconnParm.dc_genParm.gp_callback = NULL;
  392. disconnParm.dc_genParm.gp_closure = NULL;
  393. disconnParm.dc_connHandle = *connHandle;
  394.  
  395. IIapi_disconnect( &disconnParm );
  396.  
  397. while( disconnParm.dc_genParm.gp_completed == FALSE )
  398. IIapi_wait( &waitParm );
  399.  
  400. *connHandle = NULL;
  401. return;
  402. }
  403.  
  404.  
  405. /*
  406. ** Name: IIdemo_rollback
  407. **
  408. ** Description:
  409. ** Invokes IIapi_rollback() to rollback current transaction
  410. ** and resets the transaction handle.
  411. **
  412. ** Input:
  413. ** tranHandle Handle of transaction.
  414. **
  415. ** Output:
  416. ** tranHandle Updated handle.
  417. **
  418. ** Return value:
  419. ** None.
  420. */
  421.  
  422. static void
  423. IIdemo_rollback( II_PTR *tranHandle )
  424. {
  425. IIAPI_ROLLBACKPARM rollbackParm;
  426. IIAPI_WAITPARM waitParm = { -1 };
  427.  
  428. printf( "IIdemo_rollback: rolling back transaction\n" );
  429.  
  430. rollbackParm.rb_genParm.gp_callback = NULL;
  431. rollbackParm.rb_genParm.gp_closure = NULL;
  432. rollbackParm.rb_tranHandle = *tranHandle;
  433. rollbackParm.rb_savePointHandle = NULL;
  434.  
  435. IIapi_rollback( &rollbackParm );
  436.  
  437. while( rollbackParm.rb_genParm.gp_completed == FALSE )
  438. IIapi_wait( &waitParm );
  439.  
  440. *tranHandle = NULL;
  441. return;
  442. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement