Beispiel #1
0
static HB_ERRCODE odbcOpen( SQLBASEAREAP pArea )
{
   SDDCONN *   pSDDConn = ( SDDCONN * ) pArea->pConnection->pSDDConn;
   SDDDATA *   pSDDData;
   void *      hQuery;
   SQLHSTMT    hStmt;
   SQLSMALLINT iNameLen;
   PHB_ITEM    pItemEof, pItem;
   HB_BOOL     bError;
   HB_USHORT   uiFields, uiIndex;
   HB_ERRCODE  errCode;
   char *      szError;

   pArea->pSDDData = memset( hb_xgrab( sizeof( SDDDATA ) ), 0, sizeof( SDDDATA ) );
   pSDDData        = ( SDDDATA * ) pArea->pSDDData;

#if ODBCVER >= 0x0300
   if( ! SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_STMT, pSDDConn->hConn, &hStmt ) ) )
#else
   if( ! SQL_SUCCEEDED( SQLAllocStmt( pSDDConn->hConn, &hStmt ) ) )
#endif
   {
      szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, SQL_NULL_HSTMT, &errCode );
      hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTALLOC, szError, pArea->szQuery, errCode );
      hb_xfree( szError );
      return HB_FAILURE;
   }

   pItem = hb_itemPutC( NULL, pArea->szQuery );

   if( ! SQL_SUCCEEDED( SQLExecDirect( hStmt,
                                       ( SQLTCHAR * ) O_HB_ITEMGETSTR( pItem, &hQuery, NULL ),
                                       ( SQLINTEGER ) hb_itemGetCLen( pItem ) ) ) )
   {
      hb_strfree( hQuery );
      hb_itemRelease( pItem );
      szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, &errCode );
#if ODBCVER >= 0x0300
      SQLFreeHandle( SQL_HANDLE_STMT, hStmt );
#else
      SQLFreeStmt( hStmt, SQL_DROP );
#endif
      hb_errRT_ODBCDD( EG_OPEN, ESQLDD_INVALIDQUERY, szError, pArea->szQuery, errCode );
      hb_xfree( szError );
      return HB_FAILURE;
   }
   else
   {
      hb_strfree( hQuery );
      hb_itemRelease( pItem );
   }

   if( ! SQL_SUCCEEDED( SQLNumResultCols( hStmt, &iNameLen ) ) )
   {
      szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, &errCode );
#if ODBCVER >= 0x0300
      SQLFreeHandle( SQL_HANDLE_STMT, hStmt );
#else
      SQLFreeStmt( hStmt, SQL_DROP );
#endif
      hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTDESCR + 1000, szError, pArea->szQuery, errCode );
      hb_xfree( szError );
      return HB_FAILURE;
   }

   uiFields = ( HB_USHORT ) iNameLen;
   SELF_SETFIELDEXTENT( ( AREAP ) pArea, uiFields );

   pItemEof = hb_itemArrayNew( uiFields );

#if 0
   HB_TRACE( HB_TR_ALWAYS, ( "fieldcount=%d", iNameLen ) );
#endif

   errCode = 0;
   bError  = HB_FALSE;
   for( uiIndex = 0; uiIndex < uiFields; uiIndex++ )
   {
      DBFIELDINFO pFieldInfo;

      PHB_ITEM pName;

      SQLTCHAR    cName[ 256 ];
      SQLULEN     uiSize;
      SQLSMALLINT iDataType, iDec, iNull;

      if( ! SQL_SUCCEEDED( SQLDescribeCol( hStmt, ( SQLSMALLINT ) uiIndex + 1, ( SQLTCHAR * ) cName, HB_SIZEOFARRAY( cName ), &iNameLen, &iDataType, &uiSize, &iDec, &iNull ) ) )
      {
         hb_itemRelease( pItemEof );
         szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, NULL );
#if ODBCVER >= 0x0300
         SQLFreeHandle( SQL_HANDLE_STMT, hStmt );
#else
         SQLFreeStmt( hStmt, SQL_DROP );
#endif
         hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTDESCR + 1001, szError, pArea->szQuery, 0 );
         hb_xfree( szError );
         return HB_FAILURE;
      }

      pName = O_HB_ITEMPUTSTRLEN( NULL, ( O_HB_CHAR * ) cName, iNameLen );
      pFieldInfo.atomName = hb_itemGetCPtr( pName );

      /*
         We do mapping of many SQL types to one Harbour field type here, so, we need store
         real SQL type in uiTypeExtended. SQL types are signed, so, HB_USHORT type casting
         is a little hacky. We need to remember use this casting also in expressions like
         this:
            if( pField->uiTypeExtended == ( HB_USHORT ) SQL_BIGINT )
         or introduce our own unsigned SQL types.
         [Mindaugas]
       */
      pFieldInfo.uiTypeExtended = ( HB_USHORT ) iDataType;
      pFieldInfo.uiLen = ( HB_USHORT ) uiSize;
      pFieldInfo.uiDec = iDec;

#if 0
      HB_TRACE( HB_TR_ALWAYS, ( "field: name=%s type=%d len=%d dec=%d null=%d", pFieldInfo.atomName, iDataType, uiSize, iDec, iNull ) );
#endif

      switch( iDataType )
      {
         case SQL_CHAR:
         case SQL_VARCHAR:
         case SQL_LONGVARCHAR:
         case SQL_WCHAR:
         case SQL_WVARCHAR:
         case SQL_WLONGVARCHAR:
            pFieldInfo.uiType = HB_FT_STRING;
            break;

         case SQL_VARBINARY:
         case SQL_LONGVARBINARY:
            pFieldInfo.uiType  = HB_FT_STRING;
            pFieldInfo.uiFlags = HB_FF_BINARY;
            break;

         case SQL_TINYINT:
         case SQL_SMALLINT:
         case SQL_INTEGER:
         case SQL_BIGINT:
            pFieldInfo.uiType = HB_FT_INTEGER;
            break;

         case SQL_DECIMAL:
         case SQL_NUMERIC:
            pFieldInfo.uiType = HB_FT_LONG;
            break;

         case SQL_REAL:
         case SQL_FLOAT:
         case SQL_DOUBLE:
            pFieldInfo.uiType = HB_FT_DOUBLE;
            break;

         case SQL_BIT:
            pFieldInfo.uiType = HB_FT_LOGICAL;
            break;

         case SQL_DATE:
#if ODBCVER >= 0x0300
         case SQL_TYPE_DATE:
#endif
            pFieldInfo.uiType = HB_FT_DATE;
            break;

         case SQL_TIME:
#if ODBCVER >= 0x0300
         case SQL_TYPE_TIME:
#endif
            pFieldInfo.uiType = HB_FT_DATE;
            break;

         /*  SQL_DATETIME = SQL_DATE = 9 */
         case SQL_TIMESTAMP:
#if ODBCVER >= 0x0300
         case SQL_TYPE_TIMESTAMP:
#endif
            pFieldInfo.uiType = HB_FT_TIMESTAMP;
            break;

         default:
#if 0
            HB_TRACE( HB_TR_ALWAYS, ( "new sql type=%d", iDataType ) );
#endif
            bError  = HB_TRUE;
            errCode = ( HB_ERRCODE ) iDataType;
            pFieldInfo.uiType = 0;
            pFieldInfo.uiType = HB_FT_STRING;
            break;
      }

      if( ! bError )
      {
         switch( pFieldInfo.uiType )
         {
            case HB_FT_STRING:
            {
               char * pStr;

               pStr = ( char * ) hb_xgrab( ( HB_SIZE ) pFieldInfo.uiLen + 1 );
               memset( pStr, ' ', pFieldInfo.uiLen );
               pStr[ pFieldInfo.uiLen ] = '\0';

               pItem = hb_itemPutCL( NULL, pStr, pFieldInfo.uiLen );
               hb_xfree( pStr );
               break;
            }

            case HB_FT_MEMO:
               pItem = hb_itemPutC( NULL, NULL );
               break;

            case HB_FT_INTEGER:
               pItem = hb_itemPutNI( NULL, 0 );
               break;

            case HB_FT_LONG:
               if( pFieldInfo.uiDec == 0 )
                  pItem = hb_itemPutNLLen( NULL, 0, pFieldInfo.uiLen );
               else
                  pItem = hb_itemPutNDLen( NULL, 0.0, pFieldInfo.uiLen, pFieldInfo.uiDec );
               break;

            case HB_FT_DOUBLE:
               pItem = hb_itemPutNDLen( NULL, 0.0, pFieldInfo.uiLen, pFieldInfo.uiDec );
               break;

            case HB_FT_LOGICAL:
               pItem = hb_itemPutL( NULL, HB_FALSE );
               break;

            case HB_FT_DATE:
               pItem = hb_itemPutDL( NULL, 0 );
               break;

            case HB_FT_TIME:
               pItem = hb_itemPutTDT( NULL, 0, 0 );
               break;

            case HB_FT_TIMESTAMP:
               pItem = hb_itemPutTDT( NULL, 0, 0 );
               break;

            default:
               pItem  = hb_itemNew( NULL );
               bError = HB_TRUE;
               break;
         }

         hb_arraySetForward( pItemEof, uiIndex + 1, pItem );
         hb_itemRelease( pItem );

         if( ! bError )
            bError = ( SELF_ADDFIELD( ( AREAP ) pArea, &pFieldInfo ) == HB_FAILURE );
      }

      hb_itemRelease( pName );

      if( bError )
         break;
   }

   if( bError )
   {
      hb_itemRelease( pItemEof );
#if ODBCVER >= 0x0300
      SQLFreeHandle( SQL_HANDLE_STMT, hStmt );
#else
      SQLFreeStmt( hStmt, SQL_DROP );
#endif
      hb_errRT_ODBCDD( EG_CORRUPTION, ESQLDD_INVALIDFIELD, "Invalid field type", pArea->szQuery, errCode );
      return HB_FAILURE;
   }

   pArea->ulRecCount = 0;
   pArea->ulRecMax   = SQLDD_ROWSET_INIT;

   pArea->pRow = ( void ** ) hb_xgrab( SQLDD_ROWSET_INIT * sizeof( void * ) );
   memset( pArea->pRow, 0, SQLDD_ROWSET_INIT * sizeof( void * ) );
   pArea->pRowFlags = ( HB_BYTE * ) hb_xgrab( SQLDD_ROWSET_INIT * sizeof( HB_BYTE ) );
   memset( pArea->pRowFlags, 0, SQLDD_ROWSET_INIT * sizeof( HB_BYTE ) );

   pArea->pRow[ 0 ]      = pItemEof;
   pArea->pRowFlags[ 0 ] = SQLDD_FLAG_CACHED;

   pSDDData->hStmt = hStmt;
   return HB_SUCCESS;
}
Beispiel #2
0
/*
---------------------------------------------------------
   TestSQLDescribeCol for MP Specific
---------------------------------------------------------
*/
PassFail TestMXSQLDescribeCol(TestInfo *pTestInfo)
{                  
	TEST_DECLARE;
 	char			Heading[MAX_STRING_SIZE];
	RETCODE			returncode;
	SQLHANDLE 		henv, hdbc, hstmt;
	UWORD			icol;
	SWORD			numcol, st, cs, cnull, cl, colsize = 2;
	SQLULEN         cp; 
	CHAR			cn[COLNAME_LEN];
	CHAR			*colname[21];
	CHAR			*ExecDirStr[12];

#ifndef _WM
	SWORD			SQLType[] = {SQL_CHAR,SQL_VARCHAR,SQL_DECIMAL,SQL_NUMERIC,SQL_SMALLINT,SQL_INTEGER,
									SQL_REAL,SQL_DOUBLE,SQL_DOUBLE,SQL_DATE,SQL_TIME,SQL_TIMESTAMP,SQL_LONGVARCHAR,SQL_BIGINT,
									SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC};
    SQLULEN			ColPrec[] = {254,254,18,18,5,10,7,15,15,10,8,26,2000,19,19,19,128,128,10,18,30};
#else
    SWORD			SQLType[] = {SQL_CHAR,SQL_VARCHAR,SQL_DECIMAL,SQL_NUMERIC,SQL_SMALLINT,SQL_INTEGER,
									SQL_REAL,SQL_DOUBLE,SQL_DOUBLE,SQL_CHAR,SQL_TIME,SQL_TIMESTAMP,SQL_LONGVARCHAR,SQL_BIGINT,
									SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC};
    SQLULEN			ColPrec[] = {254,254,18,18,5,10,7,15,15,8,8,26,2000,19,19,19,128,128,10,18,30};
#endif
	
	SWORD			ColScale[]= {0,  0,  6, 6, 0,0, 0,0, 0, 0, 0,6, 0,   0, 0, 6, 0,  128,5, 5, 10};

	char			TempType1[50],TempType2[50];
	SWORD			ColNullable[] = {SQL_NULLABLE,SQL_NO_NULLS,SQL_NULLABLE};

	CHAR			*TestCase[] = {
					"before preparing stmt ",
					"before preparing & executing stmt ",
					"before preparing, executing & fetching stmt ",
					"before execdirect stmt ",
					"before execdirect & fetching stmt ",
					"before preparing param stmt ",
					"before preparing & binding stmt ",
					"before preparing, binding & executing stmt ",
					"before preparing, binding, executing & fetching stmt "
					};

	int			lend = 9, iend = 3;
	SQLUSMALLINT i = 0, l = 0;
	SQLLEN		cbIn = SQL_NTS;

//===========================================================================================================
	var_list_t *var_list;
	var_list = load_api_vars("SQLDescribeColumns", charset_file);
	if (var_list == NULL) return FAILED;
//================Modified for Longvarchar Changes===========================================================
	if(!pTestInfo->bLongOn)
	{
		SWORD iNoLong = SQL_VARCHAR;
		SQLType[12] = iNoLong;
	}
//================Modified for Longvarchar Changes===========================================================
	//print_list(var_list);
	colname[0] = var_mapping("SQLDescribeColumns_colname_1", var_list);
	colname[1] = var_mapping("SQLDescribeColumns_colname_2", var_list);
	colname[2] = var_mapping("SQLDescribeColumns_colname_3", var_list);
	colname[3] = var_mapping("SQLDescribeColumns_colname_4", var_list);
	colname[4] = var_mapping("SQLDescribeColumns_colname_5", var_list);
	colname[5] = var_mapping("SQLDescribeColumns_colname_6", var_list);
	colname[6] = var_mapping("SQLDescribeColumns_colname_7", var_list);
	colname[7] = var_mapping("SQLDescribeColumns_colname_8", var_list);
	colname[8] = var_mapping("SQLDescribeColumns_colname_9", var_list);
	colname[9] = var_mapping("SQLDescribeColumns_colname_10", var_list);
	colname[10] = var_mapping("SQLDescribeColumns_colname_11", var_list);
	colname[11] = var_mapping("SQLDescribeColumns_colname_12", var_list);
	colname[12] = var_mapping("SQLDescribeColumns_colname_13", var_list);
	colname[13] = var_mapping("SQLDescribeColumns_colname_14", var_list);
	colname[14] = var_mapping("SQLDescribeColumns_colname_15", var_list);
	colname[15] = var_mapping("SQLDescribeColumns_colname_16", var_list);
	colname[16] = var_mapping("SQLDescribeColumns_colname_17", var_list);
	colname[17] = var_mapping("SQLDescribeColumns_colname_18", var_list);
	colname[18] = var_mapping("SQLDescribeColumns_colname_19", var_list);
	colname[19] = var_mapping("SQLDescribeColumns_colname_20", var_list);
	colname[20] = var_mapping("SQLDescribeColumns_colname_21", var_list);

	ExecDirStr[0] = var_mapping("SQLDescribeColumns_ExecDirStr_1", var_list);
	ExecDirStr[1] = var_mapping("SQLDescribeColumns_ExecDirStr_2", var_list);
	ExecDirStr[2] = var_mapping("SQLDescribeColumns_ExecDirStr_3", var_list);
	ExecDirStr[3] = var_mapping("SQLDescribeColumns_ExecDirStr_4", var_list);
	ExecDirStr[4] = var_mapping("SQLDescribeColumns_ExecDirStr_5", var_list);
	ExecDirStr[5] = var_mapping("SQLDescribeColumns_ExecDirStr_6", var_list);
	ExecDirStr[6] = var_mapping("SQLDescribeColumns_ExecDirStr_7", var_list);
	ExecDirStr[7] = var_mapping("SQLDescribeColumns_ExecDirStr_8", var_list);
	ExecDirStr[8] = var_mapping("SQLDescribeColumns_ExecDirStr_9", var_list);
	ExecDirStr[9] = var_mapping("SQLDescribeColumns_ExecDirStr_10", var_list);
	ExecDirStr[10] = var_mapping("SQLDescribeColumns_ExecDirStr_11", var_list);
	ExecDirStr[11] = var_mapping("SQLDescribeColumns_ExecDirStr_12", var_list);
//===========================================================================================================

	if(isUCS2) {
		LogMsg(NONE,"Setup for UCS2 mode testing: ColPrec has to be doubled\n");

        ExecDirStr[3] = replace_str(ExecDirStr[3],"$$$","127");
        ExecDirStr[4] = replace_str(ExecDirStr[4],"$$$","127");
        ExecDirStr[5] = replace_str(ExecDirStr[5],"$$$","127");

		l = sizeof(SQLType)/sizeof(SQLType[0]);
		while((i < l) && isUCS2) {
			if(SQLType[i] == SQL_CHAR) {
				//SQLType[i] = SQL_WCHAR;
				ColPrec[i] *= 2;  //--> This is in character, so no need to double
			}
			else if (SQLType[i] == SQL_VARCHAR) {
				//SQLType[i] = SQL_WVARCHAR;
			}
			else if (SQLType[i] == SQL_LONGVARCHAR)	{
				//SQLType[i] = SQL_WLONGVARCHAR;
				ColPrec[i] *= 2;  //--> This is in character, so no need to double
			}
			else {
			}

			i++;
		}
		i = 0;
		l = 0;
    } else {
        ExecDirStr[3] = replace_str(ExecDirStr[3],"$$$","254");
        ExecDirStr[4] = replace_str(ExecDirStr[4],"$$$","254");
        ExecDirStr[5] = replace_str(ExecDirStr[5],"$$$","254");
    }

//===========================================================================================================

	LogMsg(LINEBEFORE+SHORTTIMESTAMP,"Begin testing API => MX Specific SQLDescribeColumns | SQLDescribeColumns | mxdescol.c\n");
	TEST_INIT;
	   
	TESTCASE_BEGIN("Setup for SQLDescribCol tests\n");

	if(!FullConnect(pTestInfo))
	{
		LogMsg(NONE,"Unable to connect\n");
		TEST_FAILED;
		TEST_RETURN;
	}
	henv = pTestInfo->henv;
 	hdbc = pTestInfo->hdbc;
 	hstmt = (SQLHANDLE)pTestInfo->hstmt;
	returncode = SQLAllocStmt((SQLHANDLE)hdbc, &hstmt);	
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLAllocStmt"))
	{
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		TEST_RETURN;
	}
	TESTCASE_END;  // end of setup
	for (l = 0; l < lend; l++)
	{
		for (i = 0; i < iend; i++)
		{
			//==================================================================================
			sprintf(Heading,"SQLDescribeCol: Test #%d.%d\n",l,i);
			TESTCASE_BEGIN(Heading);
			if ((i != (iend-1)) && (l < 5))
			{
				SQLExecDirect(hstmt,(SQLCHAR*)ExecDirStr[i],SQL_NTS); /* cleanup */
				returncode = SQLExecDirect(hstmt,(SQLCHAR*)ExecDirStr[i+iend],SQL_NTS); /* create table */
				if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecDirect"))
				{
					LogAllErrors(henv,hdbc,hstmt);
					TEST_FAILED;
				}
				else
				{
					returncode = SQLExecDirect(hstmt,(SQLCHAR*)ExecDirStr[i+iend+iend],SQL_NTS); /* insert into table */
					if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecDirect"))
					{
						LogAllErrors(henv,hdbc,hstmt);
						TEST_FAILED;
					}
					else
					{
						LogMsg(NONE,"SQLDescribeCol: %s\n",TestCase[l]);
						LogMsg(NONE,"     %s\n",ExecDirStr[i+iend+iend+iend]);
						switch( l )
						{
							case 0:
								returncode = SQLPrepare(hstmt,(SQLCHAR*)ExecDirStr[i+iend+iend+iend], SQL_NTS);
								if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare"))
								{
									LogAllErrors(henv,hdbc,hstmt);
									TEST_FAILED;
								}
								break;
							case 1 :
								returncode = SQLPrepare(hstmt,(SQLCHAR*)ExecDirStr[i+iend+iend+iend], SQL_NTS);
								if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare"))
								{
									LogAllErrors(henv,hdbc,hstmt);
									TEST_FAILED;
								}
								else
								{
									returncode = SQLExecute(hstmt);
									if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecute"))
									{
										LogAllErrors(henv,hdbc,hstmt);
										TEST_FAILED;
									}
								}
								break;
							case 2 :
								returncode = SQLPrepare(hstmt,(SQLCHAR*)ExecDirStr[i+iend+iend+iend], SQL_NTS);
								if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare"))
								{
									LogAllErrors(henv,hdbc,hstmt);
									TEST_FAILED;
								}
								else
								{
									returncode = SQLExecute(hstmt);
									if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecute"))
									{
										LogAllErrors(henv,hdbc,hstmt);
										TEST_FAILED;
									}
									else
									{
										returncode = SQLFetch(hstmt);
										if(!CHECKRC(SQL_SUCCESS,returncode,"SQLFetch"))
										{
											LogAllErrors(henv,hdbc,hstmt);
											TEST_FAILED;
										}
									}
								}
								break;
							case 3 :
								returncode = SQLExecDirect(hstmt,(SQLCHAR*)ExecDirStr[i+iend+iend+iend], SQL_NTS);
								if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecDirect"))
								{
									LogAllErrors(henv,hdbc,hstmt);
									TEST_FAILED;
								}
								break;
							case 4 :
								returncode = SQLExecDirect(hstmt,(SQLCHAR*)ExecDirStr[i+iend+iend+iend], SQL_NTS);
								if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecDirect"))
								{
									LogAllErrors(henv,hdbc,hstmt);
									TEST_FAILED;
								}
								else
								{
									returncode = SQLFetch(hstmt);
									if(!CHECKRC(SQL_SUCCESS,returncode,"SQLFetch"))
									{
										LogAllErrors(henv,hdbc,hstmt);
										TEST_FAILED;
									}
								}
								break;
						}
						if (returncode == SQL_SUCCESS)
						{
							returncode=SQLNumResultCols(hstmt, &numcol);
							if(!CHECKRC(SQL_SUCCESS,returncode,"SQLNumResultCols"))
							{
								LogAllErrors(henv,hdbc,hstmt);
								TEST_FAILED;
							}
							for (icol = 1; icol <= numcol; icol++)
							{
								LogMsg(LINEBEFORE,"SQLDescribeCol: checking Column #%d\n",icol);
								returncode = SQLDescribeCol(hstmt,icol,(SQLCHAR*)cn,COLNAME_LEN,&cl,&st,&cp,&cs,&cnull);
								if(!CHECKRC(SQL_SUCCESS,returncode,"SQLDescribeCol"))
								{
									TEST_FAILED;
									LogAllErrors(henv,hdbc,hstmt);
								}
								colsize=strlen(colname[icol-1]);
								if(isCharSet == TRUE)
									colsize -= 2;
								if ((cstrcmp(cn,colname[icol-1],FALSE,isCharSet) == 0) 
									 && (cl == colsize) 
									 && (st == SQLType[icol-1]) 
									 && (cp == ColPrec[icol-1]) 
									 && (cs == ColScale[icol-1]) 
									 && (cnull == ColNullable[i]))
								{
									//LogMsg(NONE,"colname expect: %s and actual: %s are matched\n",colname[icol-1],cn);
									//LogMsg(NONE,"ColNameLen expect: %d and actual: %d are matched\n",colsize,cl);
									//LogMsg(NONE,"SQLType expect: %s and actual: %s are matched\n",
									//SQLTypeToChar(SQLType[icol-1],TempType1),SQLTypeToChar(st,TempType2));
									//LogMsg(NONE,"ColPrec expect: %d and actual: %d are matched\n",ColPrec[icol-1],cp);
									//LogMsg(NONE,"ColScale expect: %d and actual: %d are matched\n",ColScale[icol-1],cs);
									//LogMsg(NONE,"ColNullable expect: %s and actual: %s are matched\n\n",
									//SQLNullToChar(ColNullable[i],TempType1),SQLNullToChar(cnull,TempType2));
								}
								else
								{
									TEST_FAILED;	
									if (cstrcmp(cn,colname[icol-1],FALSE,isCharSet) != 0)
										LogMsg(ERRMSG,"colname expect: %s and actual: %s are not matched line %d\n",colname[icol-1],cn,__LINE__);
									if (cl != colsize)
										LogMsg(ERRMSG,"ColNameLen expect: %d and actual: %d are not matched line %d\n",colsize,cl,__LINE__);
									if (st != SQLType[icol-1])
										LogMsg(ERRMSG,"SQLType expect: %s and actual: %s are not matched line %d\n",
											SQLTypeToChar(SQLType[icol-1],TempType1),SQLTypeToChar(st,TempType2),__LINE__);
									if (cp != ColPrec[icol-1])
										LogMsg(ERRMSG,"ColPrec expect: %d and actual: %d are not matched line %d\n",ColPrec[icol-1],cp,__LINE__);
									if (cs != ColScale[icol-1])
										LogMsg(ERRMSG,"ColScale expect: %d and actual: %d are not matched line %d\n",ColScale[icol-1],cs,__LINE__);
									if (cnull != ColNullable[i])
										LogMsg(ERRMSG,"ColNullable expect: %s and actual: %s are not matched line %d\n\n",
											SQLNullToChar(ColNullable[i],TempType1),SQLNullToChar(cnull,TempType2),__LINE__);
								}
							} /* end icol loop */
						}
						else
						{
							TEST_FAILED;
							LogAllErrors(henv,hdbc,hstmt);
						}
						SQLFreeStmt(hstmt,SQL_CLOSE);
						SQLExecDirect(hstmt,(SQLCHAR*) ExecDirStr[i],SQL_NTS); /* cleanup */
					}
				}
			}
			else if ((i == (iend-1)) && (l >= 5))
			{
				SQLExecDirect(hstmt,(SQLCHAR*)ExecDirStr[i],SQL_NTS); /* cleanup */ 
				returncode = SQLExecDirect(hstmt,(SQLCHAR*)ExecDirStr[i+iend],SQL_NTS); /* create table */
				if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecDirect"))
				{
					LogAllErrors(henv,hdbc,hstmt);
					TEST_FAILED;
				}
				else
				{
					returncode = SQLExecDirect(hstmt,(SQLCHAR*)ExecDirStr[i+iend+iend],SQL_NTS); /* insert into table */
					if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecDirect"))
					{
						LogAllErrors(henv,hdbc,hstmt);
						TEST_FAILED;
					}
					else
					{
						LogMsg(NONE,"SQLDescribeCol: %s\n",TestCase[l]);
						LogMsg(NONE,"     %s\n",ExecDirStr[i+iend+iend+iend]);
						switch( l ) 
						{
							case 5 :
								returncode = SQLPrepare(hstmt,(SQLCHAR*)ExecDirStr[i+iend+iend+iend], SQL_NTS);
								if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare"))
								{
									LogAllErrors(henv,hdbc,hstmt);
									TEST_FAILED;
								}
								break;
							case 6 :
								returncode = SQLPrepare(hstmt,(SQLCHAR*)ExecDirStr[i+iend+iend+iend], SQL_NTS);
								if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare"))
								{
									LogAllErrors(henv,hdbc,hstmt);
									TEST_FAILED;
								}
								else
								{
									returncode = SQLBindParameter(hstmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,2000,0,(SQLPOINTER)"0123456789",300,&cbIn);
									if(!CHECKRC(SQL_SUCCESS,returncode,"SQLBindParameter"))
									{
										LogAllErrors(henv,hdbc,hstmt);
										TEST_FAILED;
									}
									returncode = SQLBindParameter(hstmt,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,2000,0,(SQLPOINTER)"0123456789",300,&cbIn);
									if(!CHECKRC(SQL_SUCCESS,returncode,"SQLBindParameter"))
									{
										LogAllErrors(henv,hdbc,hstmt);
										TEST_FAILED;
									}
								}
								break;
							case 7 :
								returncode = SQLPrepare(hstmt,(SQLCHAR*)ExecDirStr[i+iend+iend+iend], SQL_NTS);
								if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare"))
								{
									LogAllErrors(henv,hdbc,hstmt);
									TEST_FAILED;
								}
								else
								{
									returncode = SQLBindParameter(hstmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,2000,0,(SQLPOINTER)"0123456789",300,&cbIn);
									if(!CHECKRC(SQL_SUCCESS,returncode,"SQLBindParameter"))
									{
										LogAllErrors(henv,hdbc,hstmt);
										TEST_FAILED;
									}
									returncode = SQLBindParameter(hstmt,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,2000,0,(SQLPOINTER)"0123456789",300,&cbIn);
									if(!CHECKRC(SQL_SUCCESS,returncode,"SQLBindParameter"))
									{
										LogAllErrors(henv,hdbc,hstmt);
										TEST_FAILED;
									}
									returncode = SQLExecute(hstmt);
									if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecute"))
									{
										LogAllErrors(henv,hdbc,hstmt);
										TEST_FAILED;
									}
								}
								break;
							  case 8 :
								returncode = SQLPrepare(hstmt,(SQLCHAR*)ExecDirStr[i+iend+iend+iend], SQL_NTS);
								if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare"))
								{
									LogAllErrors(henv,hdbc,hstmt);
									TEST_FAILED;
								}
								else
								{
									returncode = SQLBindParameter(hstmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,2000,0,(SQLPOINTER)"0123456789",300,&cbIn);
									if(!CHECKRC(SQL_SUCCESS,returncode,"SQLBindParameter"))
									{
										LogAllErrors(henv,hdbc,hstmt);
										TEST_FAILED;
									}
									returncode = SQLBindParameter(hstmt,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,2000,0,(SQLPOINTER)"0123456789",300,&cbIn);
									if(!CHECKRC(SQL_SUCCESS,returncode,"SQLBindParameter"))
									{
										LogAllErrors(henv,hdbc,hstmt);
										TEST_FAILED;
									}
									returncode = SQLExecute(hstmt);
									if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecute"))
									{
										LogAllErrors(henv,hdbc,hstmt);
										TEST_FAILED;
									}
									else
									{
										returncode = SQLFetch(hstmt);
										if(!CHECKRC(SQL_SUCCESS,returncode,"SQLFetch"))
										{
											LogAllErrors(henv,hdbc,hstmt);
											TEST_FAILED;
										}
									}
								}
								break;
						}
						if (returncode == SQL_SUCCESS)
						{
							SQLNumResultCols(hstmt, &numcol);
							if(!CHECKRC(SQL_SUCCESS,returncode,"SQLNumResultCols"))
							{
								LogAllErrors(henv,hdbc,hstmt);
								TEST_FAILED;
							}
							for (icol = 1; icol <= numcol; icol++)
							{
								LogMsg(LINEBEFORE,"SQLDescribeCol: checking Column #%d\n",icol);
								returncode = SQLDescribeCol(hstmt,icol,(SQLCHAR*)cn,COLNAME_LEN,&cl,&st,&cp,&cs,&cnull);
								if(!CHECKRC(SQL_SUCCESS,returncode,"SQLDescribeCol"))
								{
									TEST_FAILED;
									LogAllErrors(henv,hdbc,hstmt);
								}
								colsize=strlen(colname[icol-1]);
								if(isCharSet == TRUE)
									colsize -= 2;
								if ((cstrcmp(cn,colname[icol-1],FALSE,isCharSet) == 0) 
									 && (cl == colsize) 
									 && (st == SQLType[icol-1]) 
									 && (cp == ColPrec[icol-1]) 
									 && (cs == ColScale[icol-1]) 
									 && (cnull == ColNullable[i]))
								{
									//LogMsg(NONE,"colname expect: %s and actual: %s are matched\n",colname[icol-1],cn);
									//LogMsg(NONE,"ColNameLen expect: %d and actual: %d are matched\n",colsize,cl);
									//LogMsg(NONE,"SQLType expect: %s and actual: %s are matched\n",
									//	SQLTypeToChar(SQLType[icol-1],TempType1),SQLTypeToChar(st,TempType2));
									//LogMsg(NONE,"ColPrec expect: %d and actual: %d are matched\n",ColPrec[icol-1],cp);
									//LogMsg(NONE,"ColScale expect: %d and actual: %d are matched\n",ColScale[icol-1],cs);
									//LogMsg(NONE,"ColNullable expect: %s and actual: %s are matched\n\n",
									//	SQLNullToChar(ColNullable[i],TempType1),SQLNullToChar(cnull,TempType2));
								}
								else
								{
									TEST_FAILED;	
									if (cstrcmp(cn,colname[icol-1],FALSE,isCharSet) != 0)
										LogMsg(ERRMSG,"colname expect: %s and actual: %s are not matched at line %d\n",colname[icol-1],cn,__LINE__);
									if (cl != colsize)
										LogMsg(ERRMSG,"ColNameLen expect: %d and actual: %d are not matched at line %d\n",colsize,cl,__LINE__);
									if (st != SQLType[icol-1])
										LogMsg(ERRMSG,"SQLType expect: %s and actual: %s are not matched at line %d\n",
											SQLTypeToChar(SQLType[icol-1],TempType1),SQLTypeToChar(st,TempType2),__LINE__);
									if (cp != ColPrec[icol-1])
										LogMsg(ERRMSG,"ColPrec expect: %d and actual: %d are not matched at line %d\n",ColPrec[icol-1],cp,__LINE__);
									if (cs != ColScale[icol-1])
										LogMsg(ERRMSG,"ColScale expect: %d and actual: %d are not matched at line %d\n",ColScale[icol-1],cs,__LINE__);
									if (cnull != ColNullable[i])
										LogMsg(ERRMSG,"ColNullable expect: %s and actual: %s are not matched at line %d\n\n",
											SQLNullToChar(ColNullable[i],TempType1),SQLNullToChar(cnull,TempType2),__LINE__);
								}
							}
						}
					}
					SQLFreeStmt(hstmt,SQL_CLOSE);
					SQLExecDirect(hstmt,(SQLCHAR*) ExecDirStr[i],SQL_NTS); 
				}
			}
			TESTCASE_END;
		} /* iend loop */
	} /* lend loop */
	FullDisconnect(pTestInfo);
	LogMsg(SHORTTIMESTAMP+LINEAFTER,"End testing API => MX Specific SQLDescribeColumns.\n");
	free_list(var_list);
	TEST_RETURN;
}
Beispiel #3
0
/*
---------------------------------------------------------
   TestSQLExecute
---------------------------------------------------------
*/
PassFail TestSQLExecute(TestInfo *pTestInfo, int MX_MP_SPECIFIC)
{                  
	TEST_DECLARE;

 	RETCODE			returncode;
 	SQLHANDLE 		henv;
 	SQLHANDLE 		hdbc;
 	SQLHANDLE		hstmt;
	TCHAR			*ExecStr[15];
	TCHAR			*szInput[] = {_T("Inst char"),_T("Inst vchar"),_T("1234.56789"),_T("1234.56789"),_T("1200"),_T("12000"),_T("123.45"),_T("123.45"),_T("123.45"),_T("1993-07-01"),_T("09:45:30"),_T("1993-08-02 08:44:31.001"),_T("120000"),_T("1234567890.56789"),_T("1234567890.456789"),_T("1234567890.456789"),_T("0.01234567890123456789"),_T("1234.56789"),_T("1234567890.56789"),_T("12345678901234567890.0123456789"),_T("Inst char"),_T("Inst vchar")};
	SQLLEN			cbInput = SQL_NTS;
	SQLUSMALLINT	i = 0;
	SQLSMALLINT		Type[] = {SQL_WCHAR,SQL_WVARCHAR,SQL_DECIMAL,SQL_NUMERIC,SQL_SMALLINT,SQL_INTEGER,SQL_REAL,SQL_FLOAT,SQL_DOUBLE,SQL_DATE,SQL_TIME,SQL_TIMESTAMP,SQL_BIGINT,SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC,SQL_NUMERIC,SQL_WCHAR,SQL_WVARCHAR};
	SQLUINTEGER		ColPrec[] = {254,254,10,10,5,10,7,15,15,10,8,26,19,19,19,128,128,10,18,30,254,254};
	SQLSMALLINT		ColScale[]= {0,0,5,5,0,0,0,0,0,0,0,0,0,0,6,0,128,5,5,10,0,0};
	short			CCharOutput1,CCharOutput2;
	SQLLEN			OutputLen1,OutputLen2;
	int				actual_insert=0,num_insert=0;
	SWORD			param = 0;

//===========================================================================================================
	var_list_t *var_list;
	var_list = load_api_vars(_T("SQLExecute"), charset_file);
	if (var_list == NULL) return FAILED;

	//print_list(var_list);
	ExecStr[0] = var_mapping(_T("SQLExecute_ExecStr_0"), var_list);
	ExecStr[1] = var_mapping(_T("SQLExecute_ExecStr_1"), var_list);
	ExecStr[2] = var_mapping(_T("SQLExecute_ExecStr_2"), var_list);
	ExecStr[3] = var_mapping(_T("SQLExecute_ExecStr_3"), var_list);

	ExecStr[4] = var_mapping(_T("SQLExecute_ExecStr_4"), var_list);
	ExecStr[5] = var_mapping(_T("SQLExecute_ExecStr_5"), var_list);
	ExecStr[6] = var_mapping(_T("SQLExecute_ExecStr_6"), var_list); 
	ExecStr[7] = var_mapping(_T("SQLExecute_ExecStr_7"), var_list);
	ExecStr[8] = var_mapping(_T("SQLExecute_ExecStr_8"), var_list);
	ExecStr[9] = var_mapping(_T("SQLExecute_ExecStr_9"), var_list);
	ExecStr[10] = var_mapping(_T("SQLExecute_ExecStr_10"), var_list);
	ExecStr[11] = var_mapping(_T("SQLExecute_ExecStr_11"), var_list);
	ExecStr[12] = var_mapping(_T("SQLExecute_ExecStr_12"), var_list);
	ExecStr[13] = var_mapping(_T("SQLExecute_ExecStr_13"), var_list);
	ExecStr[14] = var_mapping(_T("SQLExecute_ExecStr_14"), var_list);

//=================================================================================================

	LogMsg(LINEBEFORE+SHORTTIMESTAMP,_T("Begin testing API => SQLExecute.\n"));

	TEST_INIT;

	TESTCASE_BEGIN("Setup for SQLExecute tests\n");

	if(!FullConnect(pTestInfo)){
		LogMsg(NONE,_T("Unable to connect\n"));
		TEST_FAILED;
		TEST_RETURN;
	}

	henv = pTestInfo->henv;
 	hdbc = pTestInfo->hdbc;
 	hstmt = (SQLHANDLE)pTestInfo->hstmt;
   	
	returncode = SQLAllocStmt((SQLHANDLE)hdbc, &hstmt);	
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLAllocStmt")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		TEST_RETURN;
		}

	returncode=SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[4],SQL_NTS);

	returncode=SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[5],SQL_NTS); 
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecDirect")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		TEST_RETURN;
		}

	returncode=SQLExecDirect(hstmt,(SQLTCHAR *)ExecStr[6],SQL_NTS); 
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecDirect")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		TEST_RETURN;
		}

	returncode=SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[7],SQL_NTS); 
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecDirect")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		TEST_RETURN;
		}
	returncode=SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[8],SQL_NTS); 
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecDirect")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		TEST_RETURN;
		}

	TESTCASE_END;  // end of setup

	TESTCASE_BEGIN("Test #1: Positive Functionality of SQLExecute\n");

	returncode = SQLPrepare(hstmt,(SQLTCHAR *)ExecStr[9],SQL_NTS);
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}

	returncode = SQLExecute(hstmt); 
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecute")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}

	returncode = SQLBindCol(hstmt,1,SQL_C_SHORT,&CCharOutput1,0,&OutputLen1);
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLBindCol")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}

	returncode = SQLBindCol(hstmt,2,SQL_C_SHORT,&CCharOutput2,0,&OutputLen2);
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLBindCol")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}

	while (returncode != SQL_NO_DATA_FOUND)
	{
		returncode = SQLFetch(hstmt);
		if (returncode != SQL_NO_DATA_FOUND)
		{
			if(!CHECKRC(SQL_SUCCESS,returncode,"SQLFetch"))
			{
				TEST_FAILED;
				LogAllErrors(henv,hdbc,hstmt);
			}
		}
	}
	SQLFreeStmt(hstmt,SQL_CLOSE);
	returncode = SQLPrepare(hstmt,(SQLTCHAR *)ExecStr[10],SQL_NTS);
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}

	returncode = SQLExecute(hstmt); 
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecute")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}

	SQLFreeStmt(hstmt,SQL_CLOSE);
	SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[4],SQL_NTS); 
	TESTCASE_END;

	TESTCASE_BEGIN("Test #2: Positive Functionality of SQLExecute\n");
	returncode = SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[0],_tcslen(ExecStr[0])); /* cleanup */

	returncode = SQLPrepare(hstmt,(SQLTCHAR*)ExecStr[1],_tcslen(ExecStr[1]));
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}

	returncode = SQLExecute(hstmt); 
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecute")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}
	TESTCASE_END;
	
	TESTCASE_BEGIN("Test #3: Positive Functionality of SQLExecute with SQL_NTS\n");
	returncode = SQLPrepare(hstmt,(SQLTCHAR*)ExecStr[2],SQL_NTS);
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}

	returncode = SQLExecute(hstmt); 
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecute")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}
	TESTCASE_END;

	TESTCASE_BEGIN("Test #4: Positive Functionality of SQLPrepare then SQLExecute twice\n");
	returncode = SQLPrepare(hstmt,(SQLTCHAR*)ExecStr[2],_tcslen(ExecStr[2]));
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}

	returncode = SQLExecute(hstmt); 
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecute")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}

	returncode = SQLExecute(hstmt); 
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecute")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}

	TESTCASE_END;

	SQLFreeStmt(hstmt,SQL_CLOSE);
	SQLFreeStmt(hstmt,SQL_UNBIND);
	SQLFreeStmt(hstmt,SQL_RESET_PARAMS);
		
	TESTCASE_BEGIN("Test #5: Positive Functionality of SQLExecute with params\n");
	returncode = SQLPrepare(hstmt,(SQLTCHAR*)ExecStr[3],SQL_NTS);
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}
	else{
		for (i = 0; i <= 3; i++){
			    
			if (i==0) Type[0] = 1;	
			returncode = SQLBindParameter(hstmt,(SWORD)(i+1),SQL_PARAM_INPUT,SQL_C_TCHAR,Type[i],EXEC_LEN,0,szInput[i],0,&cbInput);
			if(!CHECKRC(SQL_SUCCESS,returncode,"SQLBindParameter")){
				LogMsg(NONE,_T("Type[i]: %d  \n"),Type[i]);
				LogAllErrors(henv,hdbc,hstmt);
				TEST_FAILED;
				}
			
			}
		returncode = SQLExecute(hstmt); 
		if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecute")){
			LogAllErrors(henv,hdbc,hstmt);
			TEST_FAILED;
			}
		}
	TESTCASE_END;
		
	TESTCASE_BEGIN("Test #6: Negative Functionality of SQLExecute with less params\n");
	returncode = SQLFreeStmt(hstmt,SQL_DROP);
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLFreeStmt")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}
	else{
		returncode = SQLAllocStmt((SQLHANDLE)hdbc, &hstmt);
		if(!CHECKRC(SQL_SUCCESS,returncode,"SQLAllocStmt")){
			LogAllErrors(henv,hdbc,hstmt);
			TEST_FAILED;
			}
		else{
			returncode = SQLPrepare(hstmt,(SQLTCHAR*)ExecStr[3],SQL_NTS);
			if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare")){
				LogAllErrors(henv,hdbc,hstmt);
				TEST_FAILED;
				}
			else{
				returncode = SQLBindParameter(hstmt,1,SQL_PARAM_INPUT,SQL_C_TCHAR,Type[0],EXEC_LEN,0,szInput[0],0,&cbInput);
				if(!CHECKRC(SQL_SUCCESS,returncode,"SQLBindParameter")){
					LogAllErrors(henv,hdbc,hstmt);
					TEST_FAILED;
					}
				else{
					returncode = SQLExecute(hstmt); 
					if(!CHECKRC(SQL_ERROR,returncode,"SQLExecute")){
						LogAllErrors(henv,hdbc,hstmt);
						TEST_FAILED;
						}
					}
				}	
			}
		}
	TESTCASE_END;

	TESTCASE_BEGIN("Test #7: Negative Functionality of SQLExecute with not prepared stmt\n");
	returncode = SQLFreeStmt(hstmt,SQL_DROP);
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLFreeStmt")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}
	else{
		returncode = SQLAllocStmt((SQLHANDLE)hdbc, &hstmt);
		if(!CHECKRC(SQL_SUCCESS,returncode,"SQLAllocStmt")){
			LogAllErrors(henv,hdbc,hstmt);
			TEST_FAILED;
			}
		else{
			returncode = SQLExecute(hstmt);
			if(!CHECKRC(SQL_ERROR,returncode,"SQLExecute")){
				LogAllErrors(henv,hdbc,hstmt);
				TEST_FAILED;
				}
			}
		}
	TESTCASE_END;

	TESTCASE_BEGIN("Test #8: Negative Functionality of SQLExecute with invalid handle\n");
	returncode = SQLPrepare(hstmt,(SQLTCHAR*)ExecStr[2],_tcslen(ExecStr[2]));
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare")){
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
		}
	else{
		returncode = SQLExecute((SQLHANDLE)NULL); 
		if(!CHECKRC(SQL_INVALID_HANDLE,returncode,"SQLExecute")){
			LogAllErrors(henv,hdbc,hstmt);
			TEST_FAILED;
			}
		}
	TESTCASE_END;

	returncode = SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[0],_tcslen(ExecStr[0])); /* cleanup */
 
	TESTCASE_BEGIN("Test #9: Stress Positive Functionality of SQLExecute.\n");
	returncode = SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[11],SQL_NTS);

	returncode = SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[12],SQL_NTS); 
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecDirect"))
	{
		TEST_FAILED;
		LogAllErrors(henv,hdbc,hstmt);
	}
	else
	{
		returncode = SQLPrepare(hstmt,(SQLTCHAR*)ExecStr[13],SQL_NTS); 
		if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare"))
		{
			TEST_FAILED;
			LogAllErrors(henv,hdbc,hstmt);
		}
		else
		{
			actual_insert = 0;
			for (num_insert = 0; num_insert < MAX_INSERTED_ROWS; num_insert++)
			{
				returncode = SQLExecute(hstmt);
				if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecute"))
				{
					TEST_FAILED;
					LogAllErrors(henv,hdbc,hstmt);
				}
				else
					actual_insert++;
			}
			if (actual_insert != MAX_INSERTED_ROWS)
			{
				TEST_FAILED;
				LogMsg(ERRMSG,_T("failed to insert rows actual => %d & excepted => %d.\n"),actual_insert,MAX_INSERTED_ROWS);
			}
		}
	}

	SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[11],SQL_NTS); 
	TESTCASE_END;
 
	TESTCASE_BEGIN("Test #10: Stress Positive Functionality of SQLExecute with Params.\n");
	returncode = SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[11],SQL_NTS); 

	returncode = SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[12],SQL_NTS); 
	LogMsg(NONE,_T("ExecStr[12]: %s\n"), ExecStr[12]);
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecDirect"))
	{
		TEST_FAILED;
		LogAllErrors(henv,hdbc,hstmt);
	}
	else
	{
		returncode = SQLPrepare(hstmt,(SQLTCHAR*)ExecStr[14],SQL_NTS); 
		if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare"))
		{
			TEST_FAILED;
			LogAllErrors(henv,hdbc,hstmt);
		}
		else
		{
			actual_insert = 0;
			returncode = SQLNumParams(hstmt, &param);
			if(!CHECKRC(SQL_SUCCESS,returncode,"SQLNumParams"))
			{
				LogAllErrors(henv,hdbc,hstmt);
				TEST_FAILED;
			}
			for (num_insert = 0; num_insert < MAX_INSERTED_ROWS; num_insert++)
			{
				for (i = 0; i < param; i++)
				{
					if (i==0) Type[0] = 1;

					if (MX_MP_SPECIFIC == MX_SPECIFIC)
					{
						returncode = SQLBindParameter(hstmt,(SWORD)(i+1),SQL_PARAM_INPUT,SQL_C_TCHAR,Type[i],ColPrec[i],ColScale[i],szInput[i],0,&cbInput);
					}
					else
					{
						returncode = SQLBindParameter(hstmt,(SWORD)(i+1),SQL_PARAM_INPUT,SQL_C_TCHAR,Type[i],ColPrec[i],ColScale[i],szInput[i],300,&cbInput);
					}
					if(!CHECKRC(SQL_SUCCESS,returncode,"SQLBindParameter"))
					{
						LogAllErrors(henv,hdbc,hstmt);
						TEST_FAILED;
					}
				}
				returncode = SQLExecute(hstmt);
				if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecute"))
				{
					TEST_FAILED;
					LogAllErrors(henv,hdbc,hstmt);
				}
				else
					actual_insert++;
			}
			if (actual_insert != MAX_INSERTED_ROWS)
			{
				TEST_FAILED;
				LogMsg(ERRMSG,_T("failed to insert rows actual => %d & excepted => %d.\n"),actual_insert,MAX_INSERTED_ROWS);
			}
		}
	}

	SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[11],SQL_NTS); 
	TESTCASE_END;

	TESTCASE_BEGIN("Test #11: Negative Functionality of SQLExecute with already existing table\n");
	returncode = SQLFreeStmt(hstmt,SQL_DROP);
	if(!CHECKRC(SQL_SUCCESS,returncode,"SQLFreeStmt"))
	{
		LogAllErrors(henv,hdbc,hstmt);
		TEST_FAILED;
	}
	else
	{
		returncode = SQLAllocStmt((SQLHANDLE)hdbc, &hstmt);
		if(!CHECKRC(SQL_SUCCESS,returncode,"SQLAllocStmt"))
		{
			LogAllErrors(henv,hdbc,hstmt);
			TEST_FAILED;
		}
		else
		{
			SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[0],_tcslen(ExecStr[0])); /* cleanup */
			returncode = SQLPrepare(hstmt,(SQLTCHAR*)ExecStr[1],_tcslen(ExecStr[1]));
			if(!CHECKRC(SQL_SUCCESS,returncode,"SQLPrepare"))
			{
				LogAllErrors(henv,hdbc,hstmt);
				TEST_FAILED;
			}
			else
			{
				returncode = SQLExecute(hstmt); 
				if(!CHECKRC(SQL_SUCCESS,returncode,"SQLExecute"))
				{
					LogAllErrors(henv,hdbc,hstmt);
					TEST_FAILED;
				}
				else
				{
					returncode = SQLExecute(hstmt); 
					if(!CHECKRC(SQL_ERROR,returncode,"SQLExecute"))
					{
						LogAllErrors(henv,hdbc,hstmt);
						TEST_FAILED;
					}
				}
			}
			SQLExecDirect(hstmt,(SQLTCHAR*)ExecStr[0],_tcslen(ExecStr[0])); /* cleanup */
		}
	}
	TESTCASE_END;

//=================================================================================================
	
	FullDisconnect(pTestInfo);
	LogMsg(SHORTTIMESTAMP+LINEAFTER,_T("End testing API => SQLExecute.\n"));
	free_list(var_list);
	TEST_RETURN;
}
Beispiel #4
0
K eval(K x,K y,K z){K*k;S*b,s;SQLULEN w;SQLLEN*nb;SQLINTEGER*wb;H*tb,u,t,j=0,p,m;F f;C c[128];I n=xj<0;D d=d1(n?-xj:xj);U(d)x=y;Q(z->t!=-KJ||xt!=-KS&&xt!=KC,"type")
 if(z->j)SQLSetStmtAttr(d,SQL_ATTR_QUERY_TIMEOUT,(SQLPOINTER)(SQLULEN)z->j,0);
 if(xt==-KS)Q1(SQLColumns(d,(S)0,0,(S)0,0,xs,S0,(S)0,0))else{I e;K q=kpn(xG,xn);ja(&q,"\0");e=SQLExecDirect(d,q->G0,xn);r0(q);Q1(e)}
 SQLNumResultCols(d,&j);P(!j,(d0(d),knk(0)))
 b=malloc(j*SZ),tb=malloc(j*2),wb=malloc(j*SZ),nb=malloc(j*SZ),x=ktn(KS,j),y=ktn(0,j);// sqlserver: no bind past nonbind
 DO(j,Q1(SQLDescribeCol(d,(H)(i+1),c,128,&u,&t,&w,&p,&m))xS[i]=sn(c,u);
if(t>90)t-=82;
Q(t<-11||t>12,xS[i])wb[i]=ut[tb[i]=t=t>0?t:12-t]==KS&&w?w+1:wt[t];if(ut[t]==KS&&(n||!wb[i]||wb[i]>9))tb[i]=13)
 DO(j,kK(y)[i]=ktn(ut[t=tb[i]],0);if(w=wb[i])Q1(SQLBindCol(d,(H)(i+1),ct[t],b[i]=malloc(w),w,nb+i)))
 for(;SQL_SUCCEEDED(SQLFetch(d));)DO(j,k=kK(y)+i;u=ut[t=tb[i]];s=b[i];n=SQL_NULL_DATA==(int)nb[i];
if(!u)jk(k,n?ktn(ct[t]?KC:KG,0):wb[i]?kp(s):gb(d,(H)(i+1),t));
else ja(k,n?nu(u):u==KH&&wb[i]==1?(t=(H)*s,(S)&t):u==KS?(s=dtb(s,nb[i]),(S)&s):u<KD?s:u==KZ?(f=ds(s)+(vs(s+6)+*(I*)(s+12)/1e9)/8.64e4,(S)&f):(w=u==KD?ds(s):vs(s),(S)&w))) 
 if(!SQLMoreResults(d))O("more\n");DO(j,if(wb[i])free(b[i]))R free(b),free(tb),free(wb),free(nb),d0(d),xT(xD(x,y));}
Beispiel #5
0
static int
do_stmt(Con& con, const char *sqlstr)
{
  SQLHSTMT hstmt;
  SQLCHAR errmsg[256];
  SQLCHAR colname[32];
  SQLSMALLINT coltype;
  SQLSMALLINT colnamelen;
  SQLSMALLINT nullable;
  SQLUINTEGER collen[MAXCOLS];
  SQLSMALLINT scale;
  SQLINTEGER outlen[MAXCOLS];
  SQLCHAR *data[MAXCOLS];
  SQLSMALLINT nresultcols = 0;
  SQLINTEGER rowcount;
  SQLINTEGER stmttype;
  SQLRETURN rc;

  /* allocate a statement handle */
  SQLAllocHandle(SQL_HANDLE_STMT, con.hdbc, &hstmt);

  /* execute the SQL statement */
  rc = SQLExecDirect(hstmt, (SQLCHAR*)sqlstr, SQL_NTS);
  if (rc == SQL_ERROR) {
    ndbout << "Operation failed" << endl;
    print_err(SQL_HANDLE_STMT, hstmt);
    return -1;
  }
  if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO && rc != SQL_NO_DATA_FOUND) {
    ndbout << "Operation returned unknown code " << rc << endl;
    return -1;
  }

  /* see what kind of statement it was */
  SQLGetDiagField(SQL_HANDLE_STMT, hstmt, 0,
		  SQL_DIAG_DYNAMIC_FUNCTION_CODE,
		  (SQLPOINTER)&stmttype, SQL_IS_INTEGER, (SQLSMALLINT *)NULL);

  switch (stmttype) {
    /* SELECT statement */
  case SQL_DIAG_SELECT_CURSOR:
    /* determine number of result columns */
    SQLNumResultCols(hstmt, &nresultcols);

    /***********************
     * Display column names 
     ***********************/
    /* Print vertical divider */
    printf("|");
    for (int i=0; i<nresultcols; i++) {
      SQLDescribeCol(hstmt, i+1, colname, sizeof(colname),
		     &colnamelen, &coltype, &collen[i], &scale, &nullable);
      collen[i] = display_length(coltype, collen[i], colname);
      for (SQLUINTEGER j=0; j<collen[i]; j++) printf("-");
      printf("--+");
    }
    printf("\n");

    printf("|");
    for (int i=0; i<nresultcols; i++) {
      SQLDescribeCol(hstmt, i+1, colname, sizeof(colname),
		     &colnamelen, &coltype, &collen[i], &scale, &nullable);
      
      /* assume there is a display_length function which
	 computes correct length given the data type */
      collen[i] = display_length(coltype, collen[i], colname);
      (void)printf(" %*.*s |", (int)collen[i], (int)collen[i], (char *)colname);
      
      /* allocate memory to bind column */
      data[i] = (SQLCHAR *) malloc(collen[i]);
      if (data[i] == NULL) {
	ndbout << "Failed to allocate malloc memory in NDB SQL program" 
	       << endl;
	exit(-1);
      }
      
      /* bind columns to program vars, converting all types to CHAR */
      SQLBindCol(hstmt, i+1, SQL_C_CHAR, data[i], collen[i], &outlen[i]);
    }
    printf("\n");

    /* Print vertical divider */
    printf("|");
    for (int i=0; i<nresultcols; i++) {
      SQLDescribeCol(hstmt, i+1, colname, sizeof(colname),
		     &colnamelen, &coltype, &collen[i], &scale, &nullable);
      collen[i] = display_length(coltype, collen[i], colname);
      for (SQLUINTEGER j=0; j<collen[i]; j++) printf("-");
      printf("--+");
    }
    printf("\n");

    /**********************
     * Display result rows 
     **********************/
    {
      int no_of_rows_fetched=0;
      while (1) {
	rc=SQLFetch(hstmt);
	errmsg[0] = '\0';
	if (rc == SQL_ERROR) {
	  print_err(SQL_HANDLE_STMT, hstmt);
	  break;
	}
	if (rc == SQL_NO_DATA) break;
	if (rc == SQL_SUCCESS) {
	  printf("|");
	  for (int i=0; i<nresultcols; i++) {
	    if (outlen[i] == SQL_NULL_DATA
		|| outlen[i] >= (SQLINTEGER) collen[i])
	      build_indicator_message(errmsg,
				      (SQLPOINTER *)data[i], collen[i],
				      &outlen[i], i);
	    (void)printf(" %*.*s |", (int)collen[i], (int)collen[i],
			 (char *)data[i]);
	  } 
	  /* print any truncation messages */
	  (void)printf("\n%s", (char *)errmsg);
	} else if (rc == SQL_SUCCESS_WITH_INFO) {
	  printf("|");
	  for (int i=0; i<nresultcols; i++) {
	    if (outlen[i] == SQL_NULL_DATA
		|| outlen[i] >= (SQLINTEGER) collen[i])
	      build_indicator_message(errmsg,
				      (SQLPOINTER *)data[i], collen[i],
				      &outlen[i], i);
	    (void)printf(" %*.*s |", (int)collen[i], (int)collen[i],
			 (char *)data[i]);
	  } /* for all columns in this row */
	  /* print any truncation messages */
	  (void)printf("\n%s", (char *)errmsg);
	}
	no_of_rows_fetched++;
      } /* while rows to fetch */
      /* Print vertical divider */
      printf("|");
      for (int i=0; i<nresultcols; i++) {
	SQLDescribeCol(hstmt, i+1, colname, sizeof(colname),
		       &colnamelen, &coltype, &collen[i], &scale, &nullable);
	collen[i] = display_length(coltype, collen[i], colname);
	for (SQLUINTEGER j=0; j<collen[i]; j++) printf("-");
	printf("--+");
      }
      printf("\n");
      ndbout << no_of_rows_fetched << " rows fetched" << endl;
    }
    SQLCloseCursor(hstmt);
    break;
    /* searched DELETE, INSERT or searched UPDATE statement */
  case SQL_DIAG_DELETE_WHERE:
  case SQL_DIAG_INSERT:
  case SQL_DIAG_UPDATE_WHERE:
    /* check rowcount */
    SQLRowCount(hstmt, (SQLINTEGER*)&rowcount);
    ndbout << (int)rowcount << " rows affected" << endl;
    break;
    /* other statements */
  case SQL_DIAG_ALTER_TABLE:
  case SQL_DIAG_CREATE_TABLE:
  case SQL_DIAG_CREATE_VIEW:
  case SQL_DIAG_DROP_TABLE:
  case SQL_DIAG_DROP_VIEW:
  case SQL_DIAG_CREATE_INDEX:
  case SQL_DIAG_DROP_INDEX:
  case SQL_DIAG_DYNAMIC_DELETE_CURSOR:
  case SQL_DIAG_DYNAMIC_UPDATE_CURSOR:
  case SQL_DIAG_GRANT:
  case SQL_DIAG_REVOKE:
    ndbout << "Operation successful" << endl;
    break;
    /* implementation-defined statement */
  default:
    (void)printf("Unknown Statement type=%ld\n", stmttype);
    break;
  }

  /* free data buffers */
  for (int i=0; i<nresultcols; i++) {
    (void)free(data[i]);
  }
  
  SQLFreeHandle(SQL_HANDLE_STMT, hstmt);  // free statement handle 
  return(0);
}
Beispiel #6
0
/*-----------------------------------------------------------------*/
int main()
{
	SQLCHAR InputParam[25] = "Special Instructions";
	SQLINTEGER Indicator = SQL_DATA_AT_EXEC;

	GET_LOGIN_VARS();
	VERBOSE("calling SQLAllocHandle(EnvHandle) \n");
	rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &EnvHandle);
	assert(rc == SQL_SUCCESS);
	assert(EnvHandle != (SQLHANDLE) NULL);

	rc = SQLSetEnvAttr(EnvHandle, SQL_ATTR_ODBC_VERSION,
			   (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER);

	assert(rc == SQL_SUCCESS);

	VERBOSE("calling SQLAllocHandle(ConHandle) \n");

	rc = SQLAllocHandle(SQL_HANDLE_DBC, EnvHandle, &ConHandle);
	assert(ConHandle != (SQLHANDLE) NULL);
	assert(rc == SQL_SUCCESS);

	if (dsn[0])
		rc = SQLDriverConnect(ConHandle, NULL, dsn,
				      SQL_NTS, NULL, 0, NULL,
				      SQL_DRIVER_NOPROMPT);
	else
		rc = SQLConnect(ConHandle, twoTask, SQL_NTS,
				(SQLCHAR *) userName, SQL_NTS, (SQLCHAR *) pswd,
				SQL_NTS);
	assert(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO);

	VERBOSE("connected to  database %s\n", twoTask);

	rc = SQLAllocStmt(ConHandle, &StmtHandle);
	assert(rc == SQL_SUCCESS);
	sprintf(SQLStmt, "drop table blob_test");
	rc = SQLExecDirect(StmtHandle, SQLStmt, SQL_NTS);

	sprintf(SQLStmt,
		"create table blob_test (an_int integer primary key,  a_blob");

	if (strstr(dsn, "DSN=psql"))
		strcat(SQLStmt, " bytea  ) ");
	else if (strstr(dsn, "DSN=mysql"))
		strcat(SQLStmt, " blob  ) ");
	else
		strcat(SQLStmt, " long raw  ) ");

	rc = SQLExecDirect(StmtHandle, SQLStmt, SQL_NTS);

	//if(rc == SQL_SUCCESS)
	//    VERBOSE("success: executed statement: %s \n", SQLStmt);

	// Define An INSERT SQL Statement That Contains A 
	// Data-At-Execution Parameter 
	strcpy((char *)SQLStmt, "INSERT INTO blob_test ");
	strcat((char *)SQLStmt, "(an_int, a_blob");
	strcat((char *)SQLStmt, " ) VALUES ");
	strcat((char *)SQLStmt, "(1, ?)");

	// Prepare The SQL Statement
	rc = SQLPrepare(StmtHandle, SQLStmt, SQL_NTS);

	// Bind The Parameter Marker In The SQL Statement To A
	// Local Application Variable - This Parameter Will Use
	// SQLParamData() And SQLPutData() To Send Data In Pieces
	// (Note: The Column Size Is 200 Characters)
	rc = SQLBindParameter(StmtHandle, 1,
			      SQL_PARAM_INPUT, SQL_C_BINARY, SQL_CHAR, 200,
			      0, (SQLPOINTER) InputParam, CHUNKSIZE,
			      &Indicator);

	// Execute The SQL Statement
	rc = SQLExecute(StmtHandle);

	// The Return Code SQL_NEED_DATA Should Be Returned,
	// Indicating That SQLParamData() And SQLPutData() Need
	// To Be Called
	assert(rc == SQL_NEED_DATA);
	rc = SendLongData();
	assert(rc == SQL_SUCCESS);
	// Free The SQL Statement Handle
	if (StmtHandle != NULL)
		SQLFreeHandle(SQL_HANDLE_STMT, StmtHandle);

	// Disconnect From The Northwind Sample Database
	rc = SQLDisconnect(ConHandle);
	return rc;
}