Ejemplo n.º 1
0
static int odbc_dispatch2(void)
{
	unsigned long retval;
	PWord rval; int rtype;
	PWord arg1; int type1;
	PWord arg2; int type2;
	PWord arg3; int type3;
	PWord arg4; int type4;
	PWord arg5; int type5;
	PWord arg6; int type6;
	PWord arg7; int type7;
	PWord arg8; int type8;
	PWord arg9; int type9;

	PI_getan(&arg1,&type1,1);
	if (type1 != PI_INT)
		if (!CI_get_integer((unsigned long *)&arg1,type1))
			PI_FAIL;
	PI_getan(&arg2,&type2,2);
	if (type2 != PI_INT)
		if (!CI_get_integer((unsigned long *)&arg2,type2))
			PI_FAIL;
	PI_getan(&arg3,&type3,3);
	if (type3 != PI_INT)
		if (!CI_get_integer((unsigned long *)&arg3,type3))
			PI_FAIL;
	PI_getan(&arg4,&type4,4);
	if (type4 != PI_INT)
		if (!CI_get_integer((unsigned long *)&arg4,type4))
			PI_FAIL;
	PI_getan(&arg5,&type5,5);
	if (type5 != PI_INT)
		if (!CI_get_integer((unsigned long *)&arg5,type5))
			PI_FAIL;
	PI_getan(&arg6,&type6,6);
	if (type6 != PI_INT)
		if (!CI_get_integer((unsigned long *)&arg6,type6))
			PI_FAIL;
	PI_getan(&arg7,&type7,7);
	if (type7 != PI_INT)
		if (!CI_get_integer((unsigned long *)&arg7,type7))
			PI_FAIL;
	PI_getan(&arg8,&type8,8);
	if (type8 != PI_INT)
		if (!CI_get_integer((unsigned long *)&arg8,type8))
			PI_FAIL;
	PI_getan(&arg9,&type9,9);


	switch(arg1)
	{
		case 0:
			retval = (unsigned long) SQLColAttributes(((SQLHSTMT  ) arg2),((SQLUSMALLINT  ) arg3),((SQLUSMALLINT  ) arg4),((SQLPOINTER  ) arg5),((SQLSMALLINT  ) arg6),((SQLSMALLINT * ) arg7),((SQLINTEGER * ) arg8));
			break;
		case 1:
			retval = (unsigned long) SQLGetDiagField(((SQLSMALLINT  ) arg2),((SQLHANDLE  ) arg3),((SQLSMALLINT  ) arg4),((SQLSMALLINT  ) arg5),((SQLPOINTER  ) arg6),((SQLSMALLINT  ) arg7),((SQLSMALLINT * ) arg8));
			break;
		case 2:
			retval = (unsigned long) SQLColAttribute(((SQLHSTMT  ) arg2),((SQLUSMALLINT  ) arg3),((SQLUSMALLINT  ) arg4),((SQLPOINTER  ) arg5),((SQLSMALLINT  ) arg6),((SQLSMALLINT * ) arg7),((SQLPOINTER  ) arg8));
			break;
		default:
			PI_FAIL;
	}
	PI_makedouble(&rval,&rtype,(double) retval);
	if (PI_unify(arg9,type9,rval,rtype))
		PI_SUCCEED;
	PI_FAIL;
}
Ejemplo n.º 2
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);
}
Ejemplo n.º 3
0
static int get_error_from_diag_rec(
		odbcdr_context_def *context, 
		SQLSMALLINT handle_type, 
		SQLHANDLE handle)
{
	int		rdbi_status = RDBI_SUCCESS;
	int		crit_err_found = FALSE;
	RETCODE	rec_retcode = SQL_SUCCESS;
	RETCODE	field_retcode = SQL_SUCCESS;
	SQLWCHAR	szSqlState[ODBCDR_MAX_BUFF_SIZE];
	SQLWCHAR 	szErrorMsg[ODBCDR_MAX_BUFF_SIZE];
	SDWORD		pfNativeError = 0L;
	SWORD	 	pcbErrorMsg = 0;
	SQLSMALLINT cRecNmbr = 1;
	SDWORD  	SS_Severity = 0;
	SQLINTEGER	Rownumber = 0;
	SQLINTEGER  Colnumber = 0;
    szSqlState[0] = L'\0';
    szErrorMsg[0] = L'\0';

	/*
	** Loop through the diagnostic records until there are no records
	** left or a critical error is found.
	** For now, we will only deal with the first critical error, and
	** ignore the rest.
	*/
	while ((rec_retcode != SQL_NO_DATA_FOUND) && !crit_err_found) {
		rec_retcode = (context->odbcdr_UseUnicode) ? 
            SQLGetDiagRecW(handle_type, handle,
					cRecNmbr, szSqlState, &pfNativeError,
                    szErrorMsg, ODBCDR_MAX_BUFF_SIZE, &pcbErrorMsg) : 
            SQLGetDiagRec(handle_type, handle,
					cRecNmbr, (SQLCHAR*)szSqlState, &pfNativeError,
					(SQLCHAR*)szErrorMsg, ODBCDR_MAX_BUFF_SIZE, &pcbErrorMsg);
 
		if (rec_retcode != SQL_NO_DATA_FOUND) {
			field_retcode = (context->odbcdr_UseUnicode) ? 
                SQLGetDiagFieldW( handle_type, handle, cRecNmbr,
					SQL_DIAG_ROW_NUMBER, &Rownumber,
					SQL_IS_INTEGER, NULL) :
                SQLGetDiagField( handle_type, handle, cRecNmbr,
					SQL_DIAG_ROW_NUMBER, &Rownumber,
					SQL_IS_INTEGER, NULL);

			if (Rownumber != SQL_NO_ROW_NUMBER  && Rownumber != SQL_ROW_NUMBER_UNKNOWN)	{
				field_retcode = (context->odbcdr_UseUnicode) ? 
                    SQLGetDiagFieldW( handle_type, handle, cRecNmbr,
						SQL_DIAG_COLUMN_NUMBER , &Colnumber,
						SQL_IS_INTEGER, NULL) : 
                    SQLGetDiagField( handle_type, handle, cRecNmbr,
						SQL_DIAG_COLUMN_NUMBER , &Colnumber,
						SQL_IS_INTEGER, NULL);
			}
#ifdef _WIN32
			field_retcode = (context->odbcdr_UseUnicode) ? 
                SQLGetDiagFieldW( handle_type, handle, cRecNmbr,
					SQL_DIAG_SS_SEVERITY, &SS_Severity,
                    SQL_IS_INTEGER, NULL):
                SQLGetDiagField(handle_type, handle, cRecNmbr,
					SQL_DIAG_SS_SEVERITY, &SS_Severity,
					SQL_IS_INTEGER,NULL);
#endif

			switch( pfNativeError ) {
				case 208 :
					rdbi_status = RDBI_NO_SUCH_TABLE;
					crit_err_found = TRUE;
					break;
				case 916 :
					rdbi_status = RDBI_NOT_VALID_USER_IN_DATABASE;
					crit_err_found = TRUE;
					break;
				case 1555 :
					rdbi_status = RDBI_RESOURCE_BUSY;
					crit_err_found = TRUE;
					break;
				case 18456 :
					rdbi_status = RDBI_INVLD_USER_PSWD;
					crit_err_found = TRUE;
					break;
                case 913: 
                    // "Could not find database ID 52. Database may not be activated yet or may be in transition."
                    // Severity 16. This is rather a bug related to updating views.
                    rdbi_status = RDBI_GENERIC_ERROR;
                    break;
                case 2601:
				case 2627:
                    // Duplicate index found.
                    rdbi_status = RDBI_DUPLICATE_INDEX;
					crit_err_found = TRUE;
                    break;
				case 2714:
					// There is already an object named '%.*ls' in the database.
					rdbi_status = RDBI_OBJECT_EXISTS;
					crit_err_found = TRUE;
                    break;
				case 3701:
					// No such object
					rdbi_status = RDBI_NO_SUCH_TABLE;
					crit_err_found = TRUE;
					break;
                case 229:
                    // insufficient privileges
                    rdbi_status = RDBI_INSUFFICIENT_PRIVS;
                    crit_err_found = TRUE;
                    break;
				default :
					// If we did not identify any specific errors in the 
					// diagnostic record, use the severity level to 
					// determine if a error occurred
                    // *** DS doesn't agree.
					//if (SS_Severity > 10) {
						rdbi_status = RDBI_GENERIC_ERROR;
						crit_err_found = TRUE;
					//} else {
					//	rdbi_status = RDBI_SUCCESS;
					//}
			} //switch
			cRecNmbr++; //Increment to next diagnostic record.
		} //if plm_retcode

	} // End while.

    if (context->odbcdr_UseUnicode)
	    save_err_msgW( context, handle_type, handle, 1); // 1 = assuming a connection has already been made
    else
        save_err_msg( context, handle_type, handle, 1); // 1 = assuming a connection has already been made

	return rdbi_status;
}
Ejemplo n.º 4
0
static void save_err_msg( 
	odbcdr_context_def  *context, 
	SQLSMALLINT plm_handle_type,	// Handle type
	SQLHANDLE plm_handle,			// Handle name
	int ConnInd)	    			// Connection indicator
{
	RETCODE		plm_retcode = SQL_SUCCESS;
	UCHAR		plm_szSqlState[ODBCDR_MAX_BUFF_SIZE];
	UCHAR 	 	plm_szErrorMsg[ODBCDR_MAX_BUFF_SIZE];
	SDWORD		plm_pfNativeError = 0L;
	SWORD	 	plm_pcbErrorMsg = 0;
	SQLSMALLINT 	plm_cRecNmbr = 1;
	SDWORD  	plm_SS_MsgState = 0;
	SDWORD  	plm_SS_Severity = 0;
	SQLINTEGER	plm_Rownumber = 0;
	USHORT		plm_SS_Line;
	SQLSMALLINT	plm_cbSS_Procname, plm_cbSS_Srvname;
	SQLCHAR   	plm_SS_Procname[555];
	SQLCHAR   	plm_SS_Srvname[555];
    plm_szSqlState[0] = '\0';
    plm_szSqlState[0] = '\0';

	debug_on("odbcdr_xlt_status: save_err_msg");

	context->odbcdr_last_err_msg[0] = '\0';

  	while (plm_retcode != SQL_NO_DATA_FOUND) {
		plm_retcode = SQLGetDiagRec(plm_handle_type, plm_handle,
					plm_cRecNmbr, plm_szSqlState, &plm_pfNativeError,
					plm_szErrorMsg, ODBCDR_MAX_BUFF_SIZE - 1, &plm_pcbErrorMsg);
 
		// Note that if the application has not yet made a
		// successful connection, the SQLGetDiagField
		// information has not yet been cached by ODBC
		// Driver Manager and these calls to SQLGetDiagField
		// will fail.
		if (plm_retcode != SQL_NO_DATA_FOUND) {
			if (ConnInd) {
				plm_retcode = SQLGetDiagField(
						plm_handle_type, plm_handle, plm_cRecNmbr,
						SQL_DIAG_ROW_NUMBER, &plm_Rownumber,
						SQL_IS_INTEGER,
						NULL);
#ifdef _WIN32
				plm_retcode = SQLGetDiagField(
						plm_handle_type, plm_handle, plm_cRecNmbr,
						SQL_DIAG_SS_LINE, &plm_SS_Line,
						SQL_IS_INTEGER,
						NULL);
				plm_retcode = SQLGetDiagField(
						plm_handle_type, plm_handle, plm_cRecNmbr,
						SQL_DIAG_SS_MSGSTATE, &plm_SS_MsgState,
						SQL_IS_INTEGER,
						NULL);
				plm_retcode = SQLGetDiagField(
						plm_handle_type, plm_handle, plm_cRecNmbr,
						SQL_DIAG_SS_SEVERITY, &plm_SS_Severity,
						SQL_IS_INTEGER,
						NULL);
				plm_retcode = SQLGetDiagField(
						plm_handle_type, plm_handle, plm_cRecNmbr,
						SQL_DIAG_SS_PROCNAME, &plm_SS_Procname,
						sizeof(plm_SS_Procname),
						&plm_cbSS_Procname);
				plm_retcode = SQLGetDiagField(
						plm_handle_type, plm_handle, plm_cRecNmbr,
						SQL_DIAG_SS_SRVNAME, &plm_SS_Srvname,
						sizeof(plm_SS_Srvname),
						&plm_cbSS_Srvname);
#endif
			} // if ConnInd
			// display error messages, filtering out expected "errors"
            if ((plm_pfNativeError == 5701) ||	// Change database context error code
                (plm_pfNativeError == 5703)	) {	// Change language setting error code
					break;// out of while loop, do not display error messages
			} else {
				
				// Save the message.
				strcpy( context->odbcdr_last_err_msg, (char*)plm_szErrorMsg );

				debug1("szSqlState = %s",plm_szSqlState);
				debug1("pfNativeError = %d",plm_pfNativeError);
				debug1("szErrorMsg = %s",plm_szErrorMsg);
				debug1("pcbErrorMsg = %d",plm_pcbErrorMsg);
				if (ConnInd) {
					debug1("ODBCRowNumber = %d", plm_Rownumber);
					debug1("SSrvrLine = %d", plm_Rownumber);
					debug1("SSrvrMsgState = %d",plm_SS_MsgState);
					debug1("SSrvrSeverity = %d",plm_SS_Severity);
					debug1("SSrvrProcname = %s",plm_SS_Procname);
					debug1("SSrvrSrvname = %s",plm_SS_Srvname);
				}
				break;
			} //else
		} //if plm_retcode
		plm_cRecNmbr++; //Increment to next diagnostic record.
	} // End while.

	debug_return_void(NULL);

}
Ejemplo n.º 5
0
bool CODBCObjectBase::Refresh_Error_Status( SQLRETURN error_code )
{
	FATAL_ASSERT( EnvironmentHandle != 0 );

	Errors.clear();
	BadRowNumber = -1;

	if ( error_code == SQL_SUCCESS )
	{
		ErrorState = DBEST_SUCCESS;
		return true;
	}
	else if ( error_code == SQL_INVALID_HANDLE )
	{
		ErrorState = DBEST_FATAL_ERROR;
		return true;
	}

	SQLSMALLINT handle_type = SQL_HANDLE_ENV;
	SQLHANDLE handle = EnvironmentHandle;
	if ( StatementHandle != 0 )
	{
		handle_type = SQL_HANDLE_STMT;
		handle = StatementHandle;
	}
	else if ( ConnectionHandle != 0 )
	{
		handle_type = SQL_HANDLE_DBC;
		handle = ConnectionHandle;
	}

	SQLINTEGER record_count = 0;
	SQLRETURN ec = SQLGetDiagField( handle_type, handle, 0, SQL_DIAG_NUMBER, &record_count, SQL_IS_INTEGER, NULL );
	FATAL_ASSERT( ec == SQL_SUCCESS );

	SQLWCHAR sql_state[ 6 ] = { 0 };
	SQLINTEGER sql_error_code = 0;
	SQLSMALLINT text_length = 0;
	SQLWCHAR error_buffer[ 1024 ] = { 0 };

	for ( SQLSMALLINT i = 1; i <= record_count; ++i )
	{
		ec = SQLGetDiagRec( handle_type, handle, i, sql_state, &sql_error_code, error_buffer, sizeof( error_buffer ) / sizeof( SQLWCHAR ), &text_length );
		FATAL_ASSERT( ec == SQL_SUCCESS );

		Errors.push_back( SODBCError( sql_error_code, sql_state, std::wstring( error_buffer ) ) );

		if ( handle_type == SQL_HANDLE_STMT && BadRowNumber == -1 )
		{
			SQLLEN bad_row = 0;
			ec = SQLGetDiagField( handle_type, handle, i, SQL_DIAG_ROW_NUMBER, &bad_row, SQL_IS_INTEGER, NULL );
			FATAL_ASSERT( ec == SQL_SUCCESS );

			if ( bad_row >= 1 )
			{
				BadRowNumber = static_cast< int32_t >( bad_row - 1 );
			}
		}
	}

	return false;
}