/* Like cmlGetOneRowFromSql but uses bind variable array (via cllExecSqlWithResult). */ static int cmlGetOneRowFromSqlV3 (char *sql, char *cVal[], int cValSize[], int numOfCols, icatSessionStruct *icss) { int i,j, stmtNum, ii; i = cllExecSqlWithResult(icss, &stmtNum, sql); if (i != 0) { if (i <= CAT_ENV_ERR) return(i); /* already an iRODS error code */ return (CAT_SQL_ERR); } i = cllGetRow(icss,stmtNum); if (i != 0) { ii = cllFreeStatement(icss,stmtNum); return(CAT_GET_ROW_ERR); } if (icss->stmtPtr[stmtNum]->numOfCols == 0) { ii = cllFreeStatement(icss,stmtNum); return(CAT_NO_ROWS_FOUND); } for (j = 0; j < numOfCols && j < icss->stmtPtr[stmtNum]->numOfCols ; j++ ) rstrcpy(cVal[j],icss->stmtPtr[stmtNum]->resultValue[j],cValSize[j]); i = cllFreeStatement(icss,stmtNum); return(j); }
/* Execute a SQL command that returns a result table, and and bind the default row; and allow optional bind variables. */ int cllExecSqlWithResultBV( icatSessionStruct *icss, int *stmtNum, const char *sql, std::vector<std::string> &bindVars ) { for ( int i = 0; i < bindVars.size() && !bindVars[i].empty(); i++ ) { cllBindVars[cllBindVarCount++] = bindVars[i].c_str(); } return cllExecSqlWithResult( icss, stmtNum, sql ); }
int cmlGetFirstRowFromSql (char *sql, int *statement, int skipCount, icatSessionStruct *icss) { int i, stmtNum, ii; #ifdef ORA_ICAT int j; #endif *statement=0; i = cllExecSqlWithResult(icss, &stmtNum, sql); if (i != 0) { if (i <= CAT_ENV_ERR) return(i); /* already an iRODS error code */ return (CAT_SQL_ERR); } #ifdef ORA_ICAT if (skipCount > 0) { for (j=0;j<skipCount;j++) { i = cllGetRow(icss,stmtNum); if (i != 0) { ii = cllFreeStatement(icss,stmtNum); return(CAT_GET_ROW_ERR); } if (icss->stmtPtr[stmtNum]->numOfCols == 0) { i = cllFreeStatement(icss,stmtNum); return(CAT_NO_ROWS_FOUND); } } } #endif i = cllGetRow(icss,stmtNum); if (i != 0) { ii = cllFreeStatement(icss,stmtNum); return(CAT_GET_ROW_ERR); } if (icss->stmtPtr[stmtNum]->numOfCols == 0) { i = cllFreeStatement(icss,stmtNum); return(CAT_NO_ROWS_FOUND); } *statement = stmtNum; return(0); }
/* A few tests to verify basic functionality (including talking with the database via ODBC). */ int cllTest( const char *userArg, const char *pwArg ) { int i; int j, k; int OK; int stmt; int numOfCols; char userName[500]; int numRows; struct passwd *ppasswd; icatSessionStruct icss; icss.stmtPtr[0] = 0; rodsLogSqlReq( 1 ); OK = 1; i = cllOpenEnv( &icss ); if ( i != 0 ) { OK = 0; } if ( userArg == 0 || *userArg == '\0' ) { ppasswd = getpwuid( getuid() ); /* get user passwd entry */ strcpy( userName, ppasswd->pw_name ); /* get user name */ } else { strncpy( userName, userArg, 500 ); } printf( "userName=%s\n", userName ); printf( "password=%s\n", pwArg ); strncpy( icss.databaseUsername, userName, DB_USERNAME_LEN ); if ( pwArg == 0 || *pwArg == '\0' ) { strcpy( icss.databasePassword, "" ); } else { strncpy( icss.databasePassword, pwArg, DB_PASSWORD_LEN ); } i = cllConnect( &icss ); if ( i != 0 ) { exit( -1 ); } i = cllExecSqlNoResult( &icss, "drop table test" ); i = cllExecSqlNoResult( &icss, "create table test (i integer, a2345678901234567890123456789j integer, a varchar(50) )" ); if ( i != 0 && i != CAT_SUCCESS_BUT_WITH_NO_INFO ) { OK = 0; } i = cllExecSqlNoResult( &icss, "insert into test values ('1', '2', 'asdfas')" ); if ( i != 0 ) { OK = 0; } i = cllExecSqlNoResult( &icss, "commit" ); if ( i != 0 ) { OK = 0; } i = cllExecSqlNoResult( &icss, "insert into test values (2, 3, 'a')" ); if ( i != 0 ) { OK = 0; } i = cllExecSqlNoResult( &icss, "commit" ); if ( i != 0 ) { OK = 0; } i = cllExecSqlNoResult( &icss, "bad sql" ); if ( i == 0 ) { OK = 0; /* should fail, if not it's not OK */ } i = cllExecSqlNoResult( &icss, "delete from test where i = '1'" ); if ( i != 0 && i != CAT_SUCCESS_BUT_WITH_NO_INFO ) { OK = 0; } i = cllExecSqlNoResult( &icss, "commit" ); if ( i != 0 ) { OK = 0; } i = cllExecSqlWithResult( &icss, &stmt, "select * from test where a = 'a'" ); if ( i != 0 ) { OK = 0; } if ( i == 0 ) { numOfCols = 1; for ( j = 0; j < 10 && numOfCols > 0; j++ ) { i = cllGetRow( &icss, stmt ); if ( i != 0 ) { OK = 0; break; } else { numOfCols = icss.stmtPtr[stmt]->numOfCols; if ( numOfCols == 0 ) { printf( "No more rows returned\n" ); i = cllFreeStatement( &icss, stmt ); } else { for ( k = 0; k < numOfCols || k < icss.stmtPtr[stmt]->numOfCols; k++ ) { printf( "resultValue[%d]=%s\n", k, icss.stmtPtr[stmt]->resultValue[k] ); } } } } } cllBindVars[cllBindVarCount++] = "a"; i = cllExecSqlWithResult( &icss, &stmt, "select * from test where a = ?" ); if ( i != 0 ) { OK = 0; } numRows = 0; if ( i == 0 ) { numOfCols = 1; for ( j = 0; j < 10 && numOfCols > 0; j++ ) { i = cllGetRow( &icss, stmt ); if ( i != 0 ) { OK = 0; } else { numOfCols = icss.stmtPtr[stmt]->numOfCols; if ( numOfCols == 0 ) { printf( "No more rows returned\n" ); i = cllFreeStatement( &icss, stmt ); } else { numRows++; for ( k = 0; k < numOfCols || k < icss.stmtPtr[stmt]->numOfCols; k++ ) { printf( "resultValue[%d]=%s\n", k, icss.stmtPtr[stmt]->resultValue[k] ); } } } } } if ( numRows != 1 ) { printf( "Error: Did not return 1 row, %d instead\n", numRows ); OK = 0; } i = cllExecSqlNoResult( &icss, "drop table test" ); if ( i != 0 && i != CAT_SUCCESS_BUT_WITH_NO_INFO ) { OK = 0; } i = cllExecSqlNoResult( &icss, "commit" ); if ( i != 0 ) { OK = 0; } i = cllDisconnect( &icss ); if ( i != 0 ) { OK = 0; } i = cllCloseEnv( &icss ); if ( i != 0 ) { OK = 0; } if ( OK ) { printf( "The tests all completed normally\n" ); return 0; } else { printf( "One or more tests DID NOT complete normally\n" ); return -1; } }