Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

# tdsodbc.h

    00001 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
    00002  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005  Brian Bruns
    00003  * Copyright (C) 2004, 2005 Frediano Ziglio
    00004  *
    00005  * This library is free software; you can redistribute it and/or
    00006  * modify it under the terms of the GNU Library General Public
    00007  * License as published by the Free Software Foundation; either
    00008  * version 2 of the License, or (at your option) any later version.
    00009  *
    00010  * This library is distributed in the hope that it will be useful,
    00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013  * Library General Public License for more details.
    00014  *
    00015  * You should have received a copy of the GNU Library General Public
    00016  * License along with this library; if not, write to the
    00017  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018  * Boston, MA 02111-1307, USA.
    00019  */
    00020
    00021 #ifndef _sql_h_
    00022 #define _sql_h_
    00023
    00024 #include "tds.h"
    00025
    00026 #ifdef UNIXODBC
    00027 #include <sql.h>
    00028 #include <sqlext.h>
    00029 #include <odbcinst.h>
    00030 #elif defined(TDS_NO_DM)
    00031 #include <sql.h>
    00032 #include <sqlext.h>
    00033 #else /* IODBC */
    00034 #include <isql.h>
    00035 #include <isqlext.h>
    00036 #ifdef HAVE_IODBCINST_H
    00037 #include <iodbcinst.h>
    00038 #endif /* HAVE_IODBCINST_H */
    00039 #endif
    00040
    00041 #ifndef HAVE_SQLLEN
    00042 #ifndef SQLULEN
    00043 #define SQLULEN SQLUINTEGER
    00044 #endif
    00045 #ifndef SQLLEN
    00046 #define SQLLEN SQLINTEGER
    00047 #endif
    00048 #endif
    00049
    00050 #ifdef __cplusplus
    00051 extern "C"
    00052 {
    00053 #if 0
    00054 }
    00055 #endif
    00056 #endif
    00057
    00058 /* $Id: tdsodbc.h,v 1.88 2005/08/15 07:06:47 freddy77 Exp $ */
    00059
    00060 #if defined(__GNUC__) && __GNUC__ >= 4
    00061 #pragma GCC visibility push(hidden)
    00062 #endif
    00063
    00064 struct _sql_error
    00065 {
    00066         const char *msg;
    00067         char state2[6];
    00068         char state3[6];
    00069         TDS_UINT native;
    00070         char *server;
    00071         int linenum;
    00072         int msgstate;
    00073 };
    00074
    00075 struct _sql_errors
    00076 {
    00077         SQLRETURN lastrc;
    00078         int num_errors;
    00079         struct _sql_error *errs;
    00080         char ranked;
    00081 };
    00082
    00083 #if ENABLE_EXTRA_CHECKS
    00084 void odbc_check_struct_extra(void *p);
    00085
    00086 #define ODBC_RETURN(handle, rc) \
    00087         do { odbc_check_struct_extra(handle); return (handle->errs.lastrc = (rc)); } while(0)
    00088 #define ODBC_RETURN_(handle) \
    00089         do { odbc_check_struct_extra(handle); return handle->errs.lastrc; } while(0)
    00090 #else
    00091 #define ODBC_RETURN(handle, rc) \
    00092         do { return (handle->errs.lastrc = (rc)); } while(0)
    00093 #define ODBC_RETURN_(handle) \
    00094         do { return handle->errs.lastrc; } while(0)
    00095 #endif
    00096
    00098 void odbc_errs_reset(struct _sql_errors *errs);
    00099
    00101 void odbc_errs_add(struct _sql_errors *errs, const char *sqlstate, const char *msg);
    00102
    00104 void odbc_errs_add_rdbms(struct _sql_errors *errs, TDS_UINT native, const char *sqlstate, const char *msg, int linenum,
    00105                          int msgstate, const char *server);
    00106
    00107 struct _dheader
    00108 {
    00109         SQLSMALLINT sql_desc_alloc_type;
    00110         SQLINTEGER sql_desc_bind_type;
    00111         SQLULEN sql_desc_array_size;
    00112         /* TODO SQLLEN ?? see http://support.microsoft.com/default.aspx?scid=kb;en-us;298678 */
    00113         SQLSMALLINT sql_desc_count;
    00114         SQLUSMALLINT *sql_desc_array_status_ptr;
    00115         SQLULEN *sql_desc_rows_processed_ptr;
    00116         SQLLEN *sql_desc_bind_offset_ptr;
    00117 };
    00118
    00119 struct _drecord
    00120 {
    00121         SQLUINTEGER sql_desc_auto_unique_value;
    00122         DSTR sql_desc_base_column_name;
    00123         DSTR sql_desc_base_table_name;
    00124         SQLINTEGER sql_desc_case_sensitive;
    00125         DSTR sql_desc_catalog_name;
    00126         SQLSMALLINT sql_desc_concise_type;
    00127         SQLPOINTER sql_desc_data_ptr;
    00128         SQLSMALLINT sql_desc_datetime_interval_code;
    00129         SQLINTEGER sql_desc_datetime_interval_precision;
    00130         SQLLEN sql_desc_display_size;
    00131         SQLSMALLINT sql_desc_fixed_prec_scale;
    00132         SQLLEN *sql_desc_indicator_ptr;
    00133         DSTR sql_desc_label;
    00134         SQLULEN sql_desc_length;
    00135         /* this point to a constant buffer, do not free or modify */
    00136         const char *sql_desc_literal_prefix;
    00137         /* this point to a constant buffer, do not free or modify */
    00138         const char *sql_desc_literal_suffix;
    00139         DSTR sql_desc_local_type_name;
    00140         DSTR sql_desc_name;
    00141         SQLSMALLINT sql_desc_nullable;
    00142         SQLINTEGER sql_desc_num_prec_radix;
    00143         SQLLEN sql_desc_octet_length;
    00144         SQLLEN *sql_desc_octet_length_ptr;
    00145         SQLSMALLINT sql_desc_parameter_type;
    00146         SQLSMALLINT sql_desc_precision;
    00147         SQLSMALLINT sql_desc_rowver;
    00148         SQLSMALLINT sql_desc_scale;
    00149         DSTR sql_desc_schema_name;
    00150         SQLSMALLINT sql_desc_searchable;
    00151         DSTR sql_desc_table_name;
    00152         SQLSMALLINT sql_desc_type;
    00153         /* this point to a constant buffer, do not free or modify */
    00154         const char *sql_desc_type_name;
    00155         SQLSMALLINT sql_desc_unnamed;
    00156         SQLSMALLINT sql_desc_unsigned;
    00157         SQLSMALLINT sql_desc_updatable;
    00158 };
    00159
    00160 struct _hdesc
    00161 {
    00162         SQLSMALLINT htype;      /* do not reorder this field */
    00163         int type;
    00164         SQLHANDLE parent;
    00165         struct _dheader header;
    00166         struct _drecord *records;
    00167         struct _sql_errors errs;
    00168 };
    00169
    00170 typedef struct _hdesc TDS_DESC;
    00171
    00172 #define DESC_IRD        1
    00173 #define DESC_IPD        2
    00174 #define DESC_ARD        3
    00175 #define DESC_APD        4
    00176
    00177 struct _heattr
    00178 {
    00179         SQLUINTEGER connection_pooling;
    00180         SQLUINTEGER cp_match;
    00181         SQLINTEGER odbc_version;
    00182         SQLINTEGER output_nts;
    00183 };
    00184
    00185 struct _hchk
    00186 {
    00187         SQLSMALLINT htype;      /* do not reorder this field */
    00188 };
    00189
    00190 struct _henv
    00191 {
    00192         SQLSMALLINT htype;      /* do not reorder this field */
    00193         TDSCONTEXT *tds_ctx;
    00194         struct _sql_errors errs;
    00195         struct _heattr attr;
    00196 };
    00197
    00198 struct _hcattr
    00199 {
    00200         SQLUINTEGER access_mode;
    00201         SQLUINTEGER async_enable;
    00202         SQLUINTEGER auto_ipd;
    00203         SQLUINTEGER autocommit;
    00204         SQLUINTEGER connection_dead;
    00205         SQLUINTEGER connection_timeout;
    00206         DSTR current_catalog;
    00207         SQLUINTEGER login_timeout;
    00208         SQLUINTEGER metadata_id;
    00209         SQLUINTEGER odbc_cursors;
    00210         SQLUINTEGER packet_size;
    00211         SQLHWND quite_mode;
    00212         DSTR translate_lib;
    00213         SQLUINTEGER translate_option;
    00214         SQLUINTEGER txn_isolation;
    00215 #ifdef TDS_NO_DM
    00216         SQLUINTEGER trace;
    00217         DSTR tracefile;
    00218 #endif
    00219 };
    00220
    00221 #define TDS_MAX_APP_DESC        100
    00222
    00223 struct _hstmt;
    00224 struct _hdbc
    00225 {
    00226         SQLSMALLINT htype;      /* do not reorder this field */
    00227         struct _henv *env;
    00228         TDSSOCKET *tds_socket;
    00229         DSTR dsn;
    00230         DSTR server;            /* aka Instance */
    00236         struct _hstmt *current_statement;
    00238         struct _hstmt *stmt_list;
    00239         struct _sql_errors errs;
    00240         struct _hcattr attr;
    00242         TDS_DESC *uad[TDS_MAX_APP_DESC];
    00243 };
    00244
    00245 struct _hsattr
    00246 {
    00247         /* TODO remove IRD, ARD, IPD, APD from statement, do not duplicate */
    00248 /*      TDS_DESC *app_row_desc; */
    00249 /*      TDS_DESC *app_param_desc; */
    00250         SQLUINTEGER async_enable;
    00251         SQLUINTEGER concurrency;
    00252         SQLUINTEGER cursor_scrollable;
    00253         SQLUINTEGER cursor_sensitivity;
    00254         SQLUINTEGER cursor_type;
    00255         SQLUINTEGER enable_auto_ipd;
    00256         SQLPOINTER fetch_bookmark_ptr;
    00257         SQLULEN keyset_size;
    00258         SQLULEN max_length;
    00259         SQLULEN max_rows;
    00260         SQLUINTEGER metadata_id;
    00261         SQLUINTEGER noscan;
    00262         /* apd->sql_desc_bind_offset_ptr */
    00263         /* SQLUINTEGER *param_bind_offset_ptr; */
    00264         /* apd->sql_desc_bind_type */
    00265         /* SQLUINTEGER param_bind_type; */
    00266         /* apd->sql_desc_array_status_ptr */
    00267         /* SQLUSMALLINT *param_operation_ptr; */
    00268         /* ipd->sql_desc_array_status_ptr */
    00269         /* SQLUSMALLINT *param_status_ptr; */
    00270         /* ipd->sql_desc_rows_processed_ptr */
    00271         /* SQLUSMALLINT *params_processed_ptr; */
    00272         /* apd->sql_desc_array_size */
    00273         /* SQLUINTEGER paramset_size; */
    00274         SQLUINTEGER query_timeout;
    00275         SQLUINTEGER retrieve_data;
    00276         /* ard->sql_desc_bind_offset_ptr */
    00277         /* SQLUINTEGER *row_bind_offset_ptr; */
    00278         /* ard->sql_desc_array_size */
    00279         /* SQLUINTEGER row_array_size; */
    00280         /* ard->sql_desc_bind_type */
    00281         /* SQLUINTEGER row_bind_type; */
    00282         SQLULEN row_number;
    00283         /* ard->sql_desc_array_status_ptr */
    00284         /* SQLUINTEGER *row_operation_ptr; */
    00285         /* ird->sql_desc_array_status_ptr */
    00286         /* SQLUINTEGER *row_status_ptr; */
    00287         /* ird->sql_desc_rows_processed_ptr */
    00288         /* SQLUINTEGER *rows_fetched_ptr; */
    00289         SQLUINTEGER simulate_cursor;
    00290         SQLUINTEGER use_bookmarks;
    00291         /* SQLGetStmtAttr only */
    00292 /*      TDS_DESC *imp_row_desc; */
    00293 /*      TDS_DESC *imp_param_desc; */
    00294 };
    00295
    00296 typedef enum
    00297 {
    00298         NOT_IN_ROW,
    00299         IN_NORMAL_ROW,
    00300         IN_COMPUTE_ROW,
    00301         AFTER_COMPUTE_ROW,
    00302         PRE_NORMAL_ROW
    00303 } TDS_ODBC_ROW_STATUS;
    00304
    00305 struct _hstmt
    00306 {
    00307         SQLSMALLINT htype;      /* do not reorder this field */
    00308         struct _hdbc *dbc;
    00310         char *query;
    00311
    00313         struct _hstmt *next;
    00315         struct _hstmt *prev;
    00316
    00317         /* begin prepared query stuff */
    00318         char *prepared_query;
    00319         unsigned prepared_query_is_func:1;
    00320         unsigned prepared_query_is_rpc:1;
    00321         unsigned need_reprepare:1;
    00322         unsigned param_data_called:1;
    00323         /* end prepared query stuff */
    00324
    00326         TDSPARAMINFO *params;
    00328         int param_num;
    00330         char *prepared_pos;
    00331
    00332         unsigned int curr_param_row, num_param_rows;
    00333
    00335         unsigned int param_count;
    00336         int row;
    00338         int row_count;
    00339         int next_row_count;
    00341         TDS_ODBC_ROW_STATUS row_status;
    00342         /* do NOT free dynamic, free from socket or attach to connection */
    00343         TDSDYNAMIC *dyn;
    00344         struct _sql_errors errs;
    00345         TDS_DESC *ard, *ird, *apd, *ipd;
    00346         TDS_DESC *orig_ard, *orig_apd;
    00347         SQLULEN sql_rowset_size;
    00348         struct _hsattr attr;
    00349         DSTR cursor_name;       /* auto generated cursor name */
    00350         int special_row;
    00351 };
    00352
    00353 typedef struct _henv TDS_ENV;
    00354 typedef struct _hdbc TDS_DBC;
    00355 typedef struct _hstmt TDS_STMT;
    00356 typedef struct _hchk TDS_CHK;
    00357
    00358 #define IS_HENV(x) (((TDS_CHK *)x)->htype == SQL_HANDLE_ENV)
    00359 #define IS_HDBC(x) (((TDS_CHK *)x)->htype == SQL_HANDLE_DBC)
    00360 #define IS_HSTMT(x) (((TDS_CHK *)x)->htype == SQL_HANDLE_STMT)
    00361 #define IS_HDESC(x) (((TDS_CHK *)x)->htype == SQL_HANDLE_DESC)
    00362
    00363 /* fix a bug in MingW headers */
    00364 #ifdef __MINGW32__
    00365 #if SQL_INTERVAL_YEAR == (100 + SQL_CODE_SECOND)
    00366
    00367 #undef SQL_INTERVAL_YEAR
    00368 #undef SQL_INTERVAL_MONTH
    00369 #undef SQL_INTERVAL_DAY
    00370 #undef SQL_INTERVAL_HOUR
    00371 #undef SQL_INTERVAL_MINUTE
    00372 #undef SQL_INTERVAL_SECOND
    00373 #undef SQL_INTERVAL_YEAR_TO_MONTH
    00374 #undef SQL_INTERVAL_DAY_TO_HOUR
    00375 #undef SQL_INTERVAL_DAY_TO_MINUTE
    00376 #undef SQL_INTERVAL_DAY_TO_SECOND
    00377 #undef SQL_INTERVAL_HOUR_TO_MINUTE
    00378 #undef SQL_INTERVAL_HOUR_TO_SECOND
    00379 #undef SQL_INTERVAL_MINUTE_TO_SECOND
    00380
    00381 #define SQL_INTERVAL_YEAR                                       (100 + SQL_CODE_YEAR)
    00382 #define SQL_INTERVAL_MONTH                                      (100 + SQL_CODE_MONTH)
    00383 #define SQL_INTERVAL_DAY                                        (100 + SQL_CODE_DAY)
    00384 #define SQL_INTERVAL_HOUR                                       (100 + SQL_CODE_HOUR)
    00385 #define SQL_INTERVAL_MINUTE                                     (100 + SQL_CODE_MINUTE)
    00386 #define SQL_INTERVAL_SECOND                     (100 + SQL_CODE_SECOND)
    00387 #define SQL_INTERVAL_YEAR_TO_MONTH                      (100 + SQL_CODE_YEAR_TO_MONTH)
    00388 #define SQL_INTERVAL_DAY_TO_HOUR                        (100 + SQL_CODE_DAY_TO_HOUR)
    00389 #define SQL_INTERVAL_DAY_TO_MINUTE                      (100 + SQL_CODE_DAY_TO_MINUTE)
    00390 #define SQL_INTERVAL_DAY_TO_SECOND                      (100 + SQL_CODE_DAY_TO_SECOND)
    00391 #define SQL_INTERVAL_HOUR_TO_MINUTE                     (100 + SQL_CODE_HOUR_TO_MINUTE)
    00392 #define SQL_INTERVAL_HOUR_TO_SECOND                     (100 + SQL_CODE_HOUR_TO_SECOND)
    00393 #define SQL_INTERVAL_MINUTE_TO_SECOND           (100 + SQL_CODE_MINUTE_TO_SECOND)
    00394
    00395 #endif
    00396 #endif
    00397
    00398 #ifdef WIN32
    00399 BOOL get_login_info(HWND hwndParent, TDSCONNECTION * connection);
    00400 #endif
    00401
    00402 /*
    00403  * connectparams.h
    00404  */
    00412 int odbc_parse_connect_string(const char *connect_string, const char *connect_string_end, TDSCONNECTION * connection);
    00413 int odbc_get_dsn_info(const char *DSN, TDSCONNECTION * connection);
    00414
    00415 /*
    00416  * convert_tds2sql.c
    00417  */
    00418 TDS_INT convert_tds2sql(TDSCONTEXT * context, int srctype, TDS_CHAR * src, TDS_UINT srclen, int desttype, TDS_CHAR * dest, SQLULEN destlen, const struct _drecord *drec_ixd);
    00419
    00420 /*
    00421  * descriptor.c
    00422  */
    00423 TDS_DESC *desc_alloc(SQLHANDLE parent, int desc_type, int alloc_type);
    00424 SQLRETURN desc_free(TDS_DESC * desc);
    00425 SQLRETURN desc_alloc_records(TDS_DESC * desc, unsigned count);
    00426 SQLRETURN desc_copy(TDS_DESC * dest, TDS_DESC * src);
    00427 SQLRETURN desc_free_records(TDS_DESC * desc);
    00428
    00429 /*
    00430  * odbc.c
    00431  */
    00432 SQLRETURN _SQLRowCount(SQLHSTMT hstmt, SQLLEN FAR * pcrow);
    00433
    00434 /*
    00435  * odbc_checks.h
    00436  */
    00437 #if ENABLE_EXTRA_CHECKS
    00438 /* macro */
    00439 #define CHECK_ENV_EXTRA(env) odbc_check_env_extra(env)
    00440 #define CHECK_DBC_EXTRA(dbc) odbc_check_dbc_extra(dbc)
    00441 #define CHECK_STMT_EXTRA(stmt) odbc_check_stmt_extra(stmt)
    00442 #define CHECK_DESC_EXTRA(desc) odbc_check_desc_extra(desc)
    00443 /* declarations*/
    00444 void odbc_check_env_extra(TDS_ENV * env);
    00445 void odbc_check_dbc_extra(TDS_DBC * dbc);
    00446 void odbc_check_stmt_extra(TDS_STMT * stmt);
    00447 void odbc_check_desc_extra(TDS_DESC * desc);
    00448 #else
    00449 /* macro */
    00450 #define CHECK_ENV_EXTRA(env)
    00451 #define CHECK_DBC_EXTRA(dbc)
    00452 #define CHECK_STMT_EXTRA(stmt)
    00453 #define CHECK_DESC_EXTRA(desc)
    00454 #endif
    00455
    00456 /*
    00457  * odbc_util.h
    00458  */
    00459 int odbc_set_stmt_query(struct _hstmt *stmt, const char *sql, int sql_len);
    00460 int odbc_set_stmt_prepared_query(struct _hstmt *stmt, const char *sql, int sql_len);
    00461 void odbc_set_return_status(struct _hstmt *stmt);
    00462 void odbc_set_return_params(struct _hstmt *stmt);
    00463
    00464 SQLSMALLINT odbc_server_to_sql_type(int col_type, int col_size);
    00465 int odbc_sql_to_c_type_default(int sql_type);
    00466 int odbc_sql_to_server_type(TDSSOCKET * tds, int sql_type);
    00467 int odbc_c_to_server_type(int c_type);
    00468
    00469 void odbc_set_sql_type_info(TDSCOLUMN * col, struct _drecord *drec);
    00470 SQLINTEGER odbc_sql_to_displaysize(int sqltype, int column_size, int column_prec);
    00471 int odbc_get_string_size(int size, SQLCHAR * str);
    00472 void odbc_rdbms_version(TDSSOCKET * tds_socket, char *pversion_string);
    00473 SQLINTEGER odbc_get_param_len(const struct _drecord *drec_apd, const struct _drecord *drec_ipd);
    00474
    00475 SQLRETURN odbc_set_string(SQLPOINTER buffer, SQLSMALLINT cbBuffer, SQLSMALLINT FAR * pcbBuffer, const char *s, int len);
    00476 SQLRETURN odbc_set_string_i(SQLPOINTER buffer, SQLINTEGER cbBuffer, SQLINTEGER FAR * pcbBuffer, const char *s, int len);
    00477
    00478 SQLSMALLINT odbc_get_concise_sql_type(SQLSMALLINT type, SQLSMALLINT interval);
    00479 SQLRETURN odbc_set_concise_sql_type(SQLSMALLINT concise_type, struct _drecord *drec, int check_only);
    00480 SQLSMALLINT odbc_get_concise_c_type(SQLSMALLINT type, SQLSMALLINT interval);
    00481 SQLRETURN odbc_set_concise_c_type(SQLSMALLINT concise_type, struct _drecord *drec, int check_only);
    00482
    00483 /*
    00484  * prepare_query.c
    00485  */
    00486 SQLRETURN prepare_call(struct _hstmt *stmt);
    00487 SQLRETURN native_sql(struct _hdbc *dbc, char *s);
    00488 int parse_prepared_query(struct _hstmt *stmt, int compute_row);
    00489 int start_parse_prepared_query(struct _hstmt *stmt, int compute_row);
    00490 int continue_parse_prepared_query(struct _hstmt *stmt, SQLPOINTER DataPtr, SQLLEN StrLen_or_Ind);
    00491 const char *parse_const_param(const char * s, TDS_SERVER_TYPE *type);
    00492
    00493 /*
    00494  * sql2tds.c
    00495  */
    00496 SQLRETURN sql2tds(TDS_STMT * stmt, const struct _drecord *drec_ipd, const struct _drecord *drec_apd, TDSPARAMINFO * info, int nparam, int compute_row);
    00497
    00498 #if defined(__GNUC__) && __GNUC__ >= 4
    00499 #pragma GCC visibility pop
    00500 #endif
    00501
    00502 #ifdef __cplusplus
    00503 #if 0
    00504 {
    00505 #endif
    00506 }
    00507 #endif
    00508
    00509 #endif