text: /*
   * This file is released under the terms of the Artistic License.  Please see
   * the file LICENSE, included in this package, for details.
   *
   * Copyright (C) 2002 Mark Wong & Open Source Development Labs, Inc.
   *
   * 11 June 2002
   * Based on TPC-C Standard Specification Revision 5.0.
   */
  
  #include <odbc_new_order.h>
  
  int execute_new_order(struct db_context_t *odbcc, struct new_order_t *data)
  {
  	SQLRETURN rc;
  	int i = 1;
  	int j;
  
  	/* Perpare statement for New Order transaction. */
  	rc = SQLPrepare(odbcc->hstmt, STMT_NEW_ORDER, SQL_NTS);
  	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  
  	/* Bind variables for New Order transaction. */
  	rc = SQLBindParameter(odbcc->hstmt,
  		i++, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0,
  		&data->w_id, 0, NULL);
  	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  	rc = SQLBindParameter(odbcc->hstmt,
  		i++, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0,
  		&data->d_id, 0, NULL);
  	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  	rc = SQLBindParameter(odbcc->hstmt,
  		i++, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0,
  		&data->c_id, 0, NULL);
  	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  	rc = SQLBindParameter(odbcc->hstmt,
  		i++, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0,
  		&data->o_all_local, 0, NULL);
  	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  	rc = SQLBindParameter(odbcc->hstmt,
  		i++, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0,
  		&data->o_ol_cnt, 0, NULL);
  	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  	for (j = 0; j < O_OL_CNT_MAX; j++) {
  		rc = SQLBindParameter(odbcc->hstmt,
  			i++, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0,
  			&data->order_line[j].ol_i_id, 0, NULL);
  		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  			LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  			return ERROR;
  		}
  		rc = SQLBindParameter(odbcc->hstmt,
  			i++, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0,
  			&data->order_line[j].ol_supply_w_id, 0, NULL);
  		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  			LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  			return ERROR;
  		}
  		rc = SQLBindParameter(odbcc->hstmt,
  			i++, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0,
  			&data->order_line[j].ol_quantity, 0, NULL);
  		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  			LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  			return ERROR;
  		}
  		rc = SQLBindParameter(odbcc->hstmt,
  			i++, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_VARCHAR, 0, 0,
  			data->order_line[j].i_name,
  			sizeof(data->order_line[j].i_name), NULL);
  		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  			LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  			return ERROR;
  		}
  		rc = SQLBindParameter(odbcc->hstmt,
  			i++, SQL_PARAM_OUTPUT, SQL_C_DOUBLE, SQL_DOUBLE, 0, 0,
  			&data->order_line[j].i_price, 0, NULL);
  		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  			LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  			return ERROR;
  		}
  		rc = SQLBindParameter(odbcc->hstmt,
  			i++, SQL_PARAM_OUTPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0,
  			&data->order_line[j].s_quantity, 0, NULL);
  		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  			LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  			return ERROR;
  		}
  		rc = SQLBindParameter(odbcc->hstmt,
  			i++, SQL_PARAM_OUTPUT, SQL_C_DOUBLE, SQL_DOUBLE, 0, 0,
  			&data->order_line[j].ol_amount, 0, NULL);
  		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  			LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  			return ERROR;
  		}
  	}
  	rc = SQLBindParameter(odbcc->hstmt,
  		i++, SQL_PARAM_OUTPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0,
  		&data->o_id, 0, NULL);
  	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  	rc = SQLBindParameter(odbcc->hstmt,
  		i++, SQL_PARAM_OUTPUT, SQL_C_DOUBLE, SQL_DOUBLE, 0, 0,
  		&data->total_amount, 0, NULL);
  	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  	rc = SQLBindParameter(odbcc->hstmt,
  		i++, SQL_PARAM_OUTPUT, SQL_C_DOUBLE, SQL_DOUBLE, 0, 0,
  		&data->w_tax, 0, NULL);
  	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  	rc = SQLBindParameter(odbcc->hstmt,
  		i++, SQL_PARAM_OUTPUT, SQL_C_DOUBLE, SQL_DOUBLE, 0, 0,
  		&data->d_tax, 0, NULL);
  	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  	rc = SQLBindParameter(odbcc->hstmt,
  		i++, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_VARCHAR, 0, 0,
  		data->c_last,
  		sizeof(data->c_last), NULL);
  	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  	rc = SQLBindParameter(odbcc->hstmt,
  		i++, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_VARCHAR, 0, 0,
  		data->c_credit,
  		sizeof(data->c_credit), NULL);
  	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  	rc = SQLBindParameter(odbcc->hstmt,
  		i++, SQL_PARAM_OUTPUT, SQL_C_DOUBLE, SQL_DOUBLE, 0, 0,
  		&data->c_discount, 0, NULL);
  	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  	rc = SQLBindParameter(odbcc->hstmt,
  		i++, SQL_PARAM_OUTPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0,
  		&data->rollback, 0, NULL);
  	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  
  	/* Execute stored procedure. */
  	rc = SQLExecute(odbcc->hstmt);
  	if (check_odbc_rc(SQL_HANDLE_STMT, odbcc->hstmt, rc) == ERROR) {
  		LOG_ODBC_ERROR(SQL_HANDLE_STMT, odbcc->hstmt);
  		return ERROR;
  	}
  
  	return OK;
  }
  