/*

  +----------------------------------------------------------------------+
  | Copyright IBM Corporation 2005.                                      |
  +----------------------------------------------------------------------+
  |                                                                      |
  | Licensed under the Apache License, Version 2.0 (the "License"); you  |
  | may not use this file except in compliance with the License. You may |
  | obtain a copy of the License at                                      |
  | http://www.apache.org/licenses/LICENSE-2.0                           |
  |                                                                      |
  | Unless required by applicable law or agreed to in writing, software  |
  | distributed under the License is distributed on an "AS IS" BASIS,    |
  | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
  | implied. See the License for the specific language governing         |
  | permissions and limitations under the License.                       |
  +----------------------------------------------------------------------+
  | Authors: Sushant Koduru, Lynh Nguyen, Kanchana Padmanabhan,          |
  |          Dan Scott, Helmut Tessarek, Sam Ruby                        |
  +----------------------------------------------------------------------+

  $Id$
*/

#ifndef RUBY_IBM_DB2_H
#define RUBY_IBM_DB2_H

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlcli1.h>

#ifdef _WIN32
#define RUBY_IBM_DB2_API __declspec(dllexport)
#else
#define RUBY_IBM_DB2_API
#endif

/* strlen(" SQLCODE=") added in */
#define DB2_MAX_ERR_MSG_LEN (SQL_MAX_MESSAGE_LENGTH + SQL_SQLSTATE_SIZE + 10)

/* Used in _ruby_parse_options */
#define DB2_ERRMSG 1
#define DB2_ERR 2

/* DB2 instance environment variable */
#define DB2_VAR_INSTANCE "DB2INSTANCE="

/******** Makes code compatible with the options used by the user */
#define DB2_BINARY 1
#define DB2_CONVERT 2
#define DB2_PASSTHRU 3

#define DB2_SCROLLABLE SQL_CURSOR_KEYSET_DRIVEN
#define DB2_FORWARD_ONLY SQL_SCROLL_FORWARD_ONLY

#define DB2_AUTOCOMMIT_ON SQL_AUTOCOMMIT_ON
#define DB2_AUTOCOMMIT_OFF SQL_AUTOCOMMIT_OFF

#define DB2_PARAM_IN SQL_PARAM_INPUT
#define DB2_PARAM_OUT SQL_PARAM_OUTPUT
#define DB2_PARAM_INOUT SQL_PARAM_INPUT_OUTPUT
#define DB2_PARAM_FILE 11
/******** end DB2 defines */

/*fetch*/
#define DB2_FETCH_INDEX	0x01
#define DB2_FETCH_ASSOC	0x02
#define DB2_FETCH_BOTH	0x03

/* Change column case */
#define DB2_CASE_NATURAL 0
#define DB2_CASE_LOWER 1
#define DB2_CASE_UPPER 2

void Init_ibm_db2();

#define RUBY_FUNCTION(func) \
  VALUE func(int argc, VALUE *argv, VALUE self)

RUBY_FUNCTION(db2_connect);
RUBY_FUNCTION(db2_commit);
RUBY_FUNCTION(db2_pconnect);
RUBY_FUNCTION(db2_autocommit);
RUBY_FUNCTION(db2_bind_param);
RUBY_FUNCTION(db2_close);
RUBY_FUNCTION(db2_columnprivileges);
RUBY_FUNCTION(db2_column_privileges);
RUBY_FUNCTION(db2_columns);
RUBY_FUNCTION(db2_foreignkeys);
RUBY_FUNCTION(db2_foreign_keys);
RUBY_FUNCTION(db2_primarykeys);
RUBY_FUNCTION(db2_primary_keys);
RUBY_FUNCTION(db2_procedure_columns);
RUBY_FUNCTION(db2_procedures);
RUBY_FUNCTION(db2_specialcolumns);
RUBY_FUNCTION(db2_special_columns);
RUBY_FUNCTION(db2_statistics);
RUBY_FUNCTION(db2_tableprivileges);
RUBY_FUNCTION(db2_table_privileges);
RUBY_FUNCTION(db2_tables);
RUBY_FUNCTION(db2_commit);
RUBY_FUNCTION(db2_exec);
RUBY_FUNCTION(db2_prepare);
RUBY_FUNCTION(db2_execute);
RUBY_FUNCTION(db2_conn_errormsg);
RUBY_FUNCTION(db2_stmt_errormsg);
RUBY_FUNCTION(db2_conn_error);
RUBY_FUNCTION(db2_stmt_error);
RUBY_FUNCTION(db2_next_result);
RUBY_FUNCTION(db2_num_fields);
RUBY_FUNCTION(db2_num_rows);
RUBY_FUNCTION(db2_field_name);
RUBY_FUNCTION(db2_field_display_size);
RUBY_FUNCTION(db2_field_num);
RUBY_FUNCTION(db2_field_precision);
RUBY_FUNCTION(db2_field_scale);
RUBY_FUNCTION(db2_field_type);
RUBY_FUNCTION(db2_field_width);
RUBY_FUNCTION(db2_cursor_type);
RUBY_FUNCTION(db2_rollback);
RUBY_FUNCTION(db2_free_stmt);
RUBY_FUNCTION(db2_result);
RUBY_FUNCTION(db2_fetch_row);
RUBY_FUNCTION(db2_fetch_assoc);
RUBY_FUNCTION(db2_fetch_array);
RUBY_FUNCTION(db2_fetch_both);
RUBY_FUNCTION(db2_result_all);
RUBY_FUNCTION(db2_free_result);
RUBY_FUNCTION(db2_set_option);
RUBY_FUNCTION(db2_setoption);
RUBY_FUNCTION(db2_fetch_object);
RUBY_FUNCTION(db2_server_info);
RUBY_FUNCTION(db2_client_info);

/*
	Declare any global variables you may need between the BEGIN
	and END macros here:
*/
struct _ibm_db2_globals {
	int		bin_mode;
	char		__ruby_conn_err_msg[DB2_MAX_ERR_MSG_LEN];
	char		__ruby_conn_err_state[SQL_SQLSTATE_SIZE + 1];
	char		__ruby_stmt_err_msg[DB2_MAX_ERR_MSG_LEN];
	char		__ruby_stmt_err_state[SQL_SQLSTATE_SIZE + 1];
};

/*
  TODO: make this threadsafe
*/

#define IBM_DB2_G(v) (ibm_db2_globals->v)

#endif	/* RUBY_IBM_DB2_H */


/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * indent-tabs-mode: t
 * End:
 */
