Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** Copyright (c) 2010 Ingres Corporation
- */
- /*
- ** Name: api_batch_fetch.c
- **
- ** Description:
- ** Demonstrates using IIapi_query(), IIapi_getDescriptor(),
- ** IIapi_getColumns() and IIapi_close() to batch fetch the results
- ** from the server.
- **
- ** SELECT data using a cursor.
- **
- ** Following actions are demonstrated in the main()
- ** Issue query
- ** Get query result descriptors
- ** Get query results, fetching upto 100 rows.
- ** Cancel query processing
- ** Free query resources.
- **
- ** Command syntax:
- ** api_batch_fetch database_name
- **
- ** To build on UNIX/Linux: (change gcc to the C compiler for your system)
- ** gcc -g -o apibatch_fetch apibatch_fetch.c -I$II_SYSTEM/ingres/files -L$II_SYSTEM/ingres/lib -liiapi.1 -lframe.1
- ** To build on Windows:
- ** cl /nologo /Zi apibatch_fetch.c iilibapi.lib
- **
- ** History:
- ** 11-Dec-2009 - grant.croker@ingres.com
- ** Initial version published
- ** 20-Jan-2009 - grant.croker@ingres.com
- ** Fix up some formatting bugs
- ** 21-Jan-2009 - grant.croker@ingres.com
- ** Allocate the DataBuffer in one go rather than call
- ** malloc() # columns * ROW_COUNT times
- ** Fix a SEGV when referencing non-allocated memory
- ** 22-Jan-2009 - grant.croker@ingres.com
- ** Make use of envHandle returned from IIapi_initialize()
- ** (Not strictly necessary for this code to work)
- */
- # include <stdio.h>
- # include <stdlib.h>
- # include <iiapi.h>
- # include <string.h>
- static void IIdemo_init(II_PTR *);
- static void IIdemo_term();
- static void IIdemo_conn( char *, II_PTR *, II_PTR );
- static void IIdemo_disconn( II_PTR * );
- static void IIdemo_rollback( II_PTR * );
- static char queryText[] = "select table_name, table_owner, create_date from iitables";
- 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";
- #define ROW_COUNT 100
- /*
- ** Name: main - main loop
- **
- ** Description:
- ** This function is the main loop of the sample code.
- */
- int
- main( int argc, char** argv )
- {
- II_PTR connHandle = (II_PTR)NULL;
- II_PTR tranHandle = (II_PTR)NULL;
- II_PTR stmtHandle = (II_PTR)NULL;
- II_PTR envHandle = (II_PTR)NULL;
- IIAPI_QUERYPARM queryParm;
- IIAPI_SETDESCRPARM setDescrParm;
- IIAPI_PUTPARMPARM putParmParm;
- IIAPI_GETDESCRPARM getDescrParm;
- IIAPI_GETCOLPARM getColParm;
- IIAPI_GETQINFOPARM getQInfoParm;
- IIAPI_CLOSEPARM closeParm;
- IIAPI_WAITPARM waitParm = { -1 };
- IIAPI_DATAVALUE *ColumnData = NULL;
- II_PTR DataBuffer = NULL;
- char varvalue[ROW_COUNT][33];
- int i, cell, row, col, row_width=0;
- II_PTR column_ptr;
- char * DataBuffer_ptr = NULL;
- long int DataBufferSize=0;
- if ( argc != 2 )
- {
- printf( "usage: apisselc [vnode::]dbname[/server_class]\n" );
- exit( 0 );
- }
- IIdemo_init(&envHandle);
- IIdemo_conn(argv[1], &connHandle, envHandle);
- printf("connHandle is %p\n",connHandle);
- /*
- ** Open cursor
- */
- printf( "apisselc: opening cursor\n" );
- queryParm.qy_genParm.gp_callback = NULL;
- queryParm.qy_genParm.gp_closure = NULL;
- queryParm.qy_connHandle = connHandle;
- queryParm.qy_queryType = IIAPI_QT_OPEN;
- queryParm.qy_queryText = moneyqueryText;
- queryParm.qy_parameters = FALSE;
- queryParm.qy_tranHandle = tranHandle;
- queryParm.qy_stmtHandle = NULL;
- IIapi_query( &queryParm );
- while( queryParm.qy_genParm.gp_completed == FALSE )
- IIapi_wait( &waitParm );
- tranHandle = queryParm.qy_tranHandle;
- stmtHandle = queryParm.qy_stmtHandle;
- /*
- ** Get query result descriptors.
- */
- printf( "apisselc: get descriptors\n" );
- getDescrParm.gd_genParm.gp_callback = NULL;
- getDescrParm.gd_genParm.gp_closure = NULL;
- getDescrParm.gd_stmtHandle = stmtHandle;
- getDescrParm.gd_descriptorCount = 0;
- getDescrParm.gd_descriptor = NULL;
- IIapi_getDescriptor( &getDescrParm );
- while( getDescrParm.gd_genParm.gp_completed == FALSE )
- IIapi_wait( &waitParm );
- printf("Cols in result : %d\n",getDescrParm.gd_descriptorCount);
- /*
- ** Get query results
- */
- printf( "apisselc: get results\n" );
- /* Row width */
- for(col = 0; col < getDescrParm.gd_descriptorCount; col++)
- {
- row_width += getDescrParm.gd_descriptor[col].ds_length + 1;
- printf ("Col %d - size %d\n", col + 1, getDescrParm.gd_descriptor[col].ds_length);
- }
- printf ("%d columns - row width %d\n", getDescrParm.gd_descriptorCount, row_width);
- /* Assign memory to the DataBuffer */
- DataBufferSize = row_width * ROW_COUNT;
- printf ("Allocating %ld bytes\n", DataBufferSize);
- DataBuffer = (II_PTR) malloc(DataBufferSize);
- memset (DataBuffer, 0, row_width * ROW_COUNT);
- ColumnData = (IIAPI_DATAVALUE *)malloc(sizeof(IIAPI_DATAVALUE) * getDescrParm.gd_descriptorCount * ROW_COUNT);
- getColParm.gc_genParm.gp_callback = NULL;
- getColParm.gc_genParm.gp_closure = NULL;
- getColParm.gc_rowCount = ROW_COUNT;
- getColParm.gc_columnCount = getDescrParm.gd_descriptorCount;
- getColParm.gc_columnData = ColumnData;
- getColParm.gc_stmtHandle = stmtHandle;
- getColParm.gc_moreSegments = 0;
- /* Assign ColumnData[cell].dv_value it's space in DataBuffer*/
- DataBuffer_ptr = DataBuffer;
- for( cell = 0; cell < (getDescrParm.gd_descriptorCount * ROW_COUNT); cell++ )
- {
- ColumnData[cell].dv_value = DataBuffer_ptr;
- *DataBuffer_ptr += (getDescrParm.gd_descriptor[cell % getDescrParm.gd_descriptorCount ]).ds_length + 1;
- }
- IIapi_getColumns( &getColParm );
- while( getColParm.gc_genParm.gp_completed == FALSE )
- IIapi_wait( &waitParm );
- printf("Rows returned %d\n", getColParm.gc_rowsReturned);
- /* Print headers */
- for(col = 0; col < getDescrParm.gd_descriptorCount; col++)
- {
- printf ("%s\t", getDescrParm.gd_descriptor[col].ds_columnName);
- }
- printf ("\n");
- for(row = 0; row < getColParm.gc_rowsReturned; row++)
- {
- printf ("Row %d - ", row + 1);
- for (col = 0; col < getDescrParm.gd_descriptorCount; col ++ )
- {
- cell = (row * getDescrParm.gd_descriptorCount) + col;
- ((char *)ColumnData[cell].dv_value)[ColumnData[cell].dv_length] = '\0';
- printf ("%s\t", (char *)ColumnData[cell].dv_value);
- }
- printf ("\n");
- }
- /*
- ** Get fetch result info.
- */
- getQInfoParm.gq_genParm.gp_callback = NULL;
- getQInfoParm.gq_genParm.gp_closure = NULL;
- getQInfoParm.gq_stmtHandle = stmtHandle;
- IIapi_getQueryInfo( &getQInfoParm );
- while( getQInfoParm.gq_genParm.gp_completed == FALSE )
- IIapi_wait( &waitParm );
- /*
- ** Free query resources.
- */
- printf( "apisselc: close cursor\n" );
- closeParm.cl_genParm.gp_callback = NULL;
- closeParm.cl_genParm.gp_closure = NULL;
- closeParm.cl_stmtHandle = stmtHandle;
- IIapi_close( &closeParm );
- /* Free allocated memory */
- free(DataBuffer);
- free(ColumnData);
- while( closeParm.cl_genParm.gp_completed == FALSE )
- IIapi_wait( &waitParm );
- IIdemo_rollback(&tranHandle);
- IIdemo_disconn(&connHandle);
- IIdemo_term();
- return( 0 );
- }
- /*
- ** Name: IIdemo_init
- **
- ** Description:
- ** Initialize API access.
- **
- ** Input:
- ** None.
- **
- ** Output:
- ** None.
- **
- ** Return value:
- ** None.
- */
- static void
- IIdemo_init(II_PTR * envHandle)
- {
- IIAPI_INITPARM initParm;
- printf( "IIdemo_init: initializing API\n" );
- initParm.in_version = IIAPI_VERSION;
- initParm.in_timeout = -1;
- IIapi_initialize( &initParm );
- *envHandle = initParm.in_envHandle;
- return;
- }
- /*
- ** Name: IIdemo_term
- **
- ** Description:
- ** Terminate API access.
- **
- ** Input:
- ** None.
- **
- ** Output:
- ** None.
- **
- ** Return value:
- ** None.
- */
- static void
- IIdemo_term()
- {
- IIAPI_TERMPARM termParm;
- printf( "IIdemo_term: shutting down API\n" );
- IIapi_terminate( &termParm );
- return;
- }
- /*
- ** Name: IIdemo_conn
- **
- ** Description:
- ** Open connection with target Database.
- **
- ** Input:
- ** dbname Database name.
- **
- ** Output:
- ** connHandle Connection handle.
- **
- ** Return value:
- ** None.
- */
- static void
- IIdemo_conn( char *dbname, II_PTR *connHandle, II_PTR envHandle )
- {
- IIAPI_CONNPARM connParm;
- IIAPI_WAITPARM waitParm = { -1 };
- IIAPI_SETCONPRMPARM setConPrmParm;
- char money_sign[1] = "#";
- setConPrmParm.sc_genParm.gp_callback = NULL;
- setConPrmParm.sc_connHandle = envHandle;
- setConPrmParm.sc_paramID = IIAPI_CP_MONEY_SIGN;
- setConPrmParm.sc_paramValue =&money_sign;
- IIapi_setConnectParam( &setConPrmParm );
- while( setConPrmParm.sc_genParm.gp_completed == FALSE )
- IIapi_wait( &waitParm );
- connHandle = setConPrmParm.sc_connHandle;
- printf( "IIdemo_conn: establishing connection\n" );
- connParm.co_genParm.gp_callback = NULL;
- connParm.co_genParm.gp_closure = NULL;
- connParm.co_target = dbname;
- connParm.co_connHandle = connHandle ? connHandle : NULL;
- connParm.co_tranHandle = NULL;
- connParm.co_username = NULL;
- connParm.co_password = NULL;
- connParm.co_timeout = -1;
- connParm.co_type = IIAPI_CT_SQL;
- IIapi_connect( &connParm );
- while( connParm.co_genParm.gp_completed == FALSE )
- IIapi_wait( &waitParm );
- printf ("Connected with IIAPI_LEVEL_%d\n",connParm.co_apiLevel);
- *connHandle = connParm.co_connHandle;
- printf("connHandle is %p\n",connHandle);
- return;
- }
- /*
- ** Name: IIdemo_disconn
- **
- ** Description:
- ** Release DBMS connection.
- **
- ** Input:
- ** connHandle Connection handle.
- **
- ** Output:
- ** connHandle Connection handle.
- **
- ** Return value:
- ** None.
- */
- static void
- IIdemo_disconn( II_PTR *connHandle )
- {
- IIAPI_DISCONNPARM disconnParm;
- IIAPI_WAITPARM waitParm = { -1 };
- printf( "IIdemo_disconn: releasing connection\n" );
- disconnParm.dc_genParm.gp_callback = NULL;
- disconnParm.dc_genParm.gp_closure = NULL;
- disconnParm.dc_connHandle = *connHandle;
- IIapi_disconnect( &disconnParm );
- while( disconnParm.dc_genParm.gp_completed == FALSE )
- IIapi_wait( &waitParm );
- *connHandle = NULL;
- return;
- }
- /*
- ** Name: IIdemo_rollback
- **
- ** Description:
- ** Invokes IIapi_rollback() to rollback current transaction
- ** and resets the transaction handle.
- **
- ** Input:
- ** tranHandle Handle of transaction.
- **
- ** Output:
- ** tranHandle Updated handle.
- **
- ** Return value:
- ** None.
- */
- static void
- IIdemo_rollback( II_PTR *tranHandle )
- {
- IIAPI_ROLLBACKPARM rollbackParm;
- IIAPI_WAITPARM waitParm = { -1 };
- printf( "IIdemo_rollback: rolling back transaction\n" );
- rollbackParm.rb_genParm.gp_callback = NULL;
- rollbackParm.rb_genParm.gp_closure = NULL;
- rollbackParm.rb_tranHandle = *tranHandle;
- rollbackParm.rb_savePointHandle = NULL;
- IIapi_rollback( &rollbackParm );
- while( rollbackParm.rb_genParm.gp_completed == FALSE )
- IIapi_wait( &waitParm );
- *tranHandle = NULL;
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement