# 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, 2006, 2007  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 #if defined(UNIXODBC) || defined(TDS_NO_DM)
    00027 #include <sql.h>
    00028 #include <sqlext.h>
    00029 #ifdef UNIXODBC
    00030 #include <odbcinst.h>
    00031 #endif
    00032 #else /* IODBC */
    00033 #include <isql.h>
    00034 #include <isqlext.h>
    00035 #ifdef HAVE_IODBCINST_H
    00036 #include <iodbcinst.h>
    00037 #endif /* HAVE_IODBCINST_H */
    00038 #endif
    00039
    00040 #ifndef HAVE_SQLLEN
    00041 #ifndef SQLULEN
    00042 #define SQLULEN SQLUINTEGER
    00043 #endif
    00044 #ifndef SQLLEN
    00045 #define SQLLEN SQLINTEGER
    00046 #endif
    00047 #endif
    00048
    00049 #ifndef HAVE_SQLSETPOSIROW
    00050 #define SQLSETPOSIROW SQLUSMALLINT
    00051 #endif
    00052
    00053 #ifndef HAVE_SQLROWOFFSET
    00054 #define SQLROWOFFSET SQLLEN
    00055 #endif
    00056
    00057 #ifndef HAVE_SQLROWSETSIZE
    00058 #define SQLROWSETSIZE SQLULEN
    00059 #endif
    00060
    00061 #ifdef __cplusplus
    00062 extern "C"
    00063 {
    00064 #if 0
    00065 }
    00066 #endif
    00067 #endif
    00068
    00069 /* $Id: tdsodbc.h,v 1.103.2.1 2008/01/27 10:41:22 freddy77 Exp $ */
    00070
    00071 #if defined(__GNUC__) && __GNUC__ >= 4
    00072 #pragma GCC visibility push(hidden)
    00073 #define ODBC_API SQL_API __attribute__((externally_visible))
    00074 #else
    00075 #define ODBC_API SQL_API
    00076 #endif
    00077
    00078 #define ODBC_MAX(a,b) ( (a) > (b) ? (a) : (b) )
    00079 #define ODBC_MIN(a,b) ( (a) < (b) ? (a) : (b) )
    00080
    00081 struct _sql_error
    00082 {
    00083         const char *msg;
    00084         char state2[6];
    00085         char state3[6];
    00086         TDS_UINT native;
    00087         char *server;
    00088         int linenum;
    00089         int msgstate;
    00090 };
    00091
    00092 struct _sql_errors
    00093 {
    00094         SQLRETURN lastrc;
    00095         int num_errors;
    00096         struct _sql_error *errs;
    00097         char ranked;
    00098 };
    00099
    00100 #if ENABLE_EXTRA_CHECKS
    00101 void odbc_check_struct_extra(void *p);
    00102
    00103 #define ODBC_RETURN(handle, rc) \
    00104         do { odbc_check_struct_extra(handle); return (handle->errs.lastrc = (rc)); } while(0)
    00105 #define ODBC_RETURN_(handle) \
    00106         do { odbc_check_struct_extra(handle); return handle->errs.lastrc; } while(0)
    00107 #else
    00108 #define ODBC_RETURN(handle, rc) \
    00109         do { return (handle->errs.lastrc = (rc)); } while(0)
    00110 #define ODBC_RETURN_(handle) \
    00111         do { return handle->errs.lastrc; } while(0)
    00112 #endif
    00113
    00115 void odbc_errs_reset(struct _sql_errors *errs);
    00116
    00118 void odbc_errs_add(struct _sql_errors *errs, const char *sqlstate, const char *msg);
    00119
    00121 void odbc_errs_add_rdbms(struct _sql_errors *errs, TDS_UINT native, const char *sqlstate, const char *msg, int linenum,
    00122                          int msgstate, const char *server);
    00123
    00124 struct _dheader
    00125 {
    00126         SQLSMALLINT sql_desc_alloc_type;
    00127         SQLINTEGER sql_desc_bind_type;
    00128         SQLULEN sql_desc_array_size;
    00129         /* TODO SQLLEN ?? see http://support.microsoft.com/default.aspx?scid=kb;en-us;298678 */
    00130         SQLSMALLINT sql_desc_count;
    00131         SQLUSMALLINT *sql_desc_array_status_ptr;
    00132         SQLULEN *sql_desc_rows_processed_ptr;
    00133         SQLLEN *sql_desc_bind_offset_ptr;
    00134 };
    00135
    00136 struct _drecord
    00137 {
    00138         SQLUINTEGER sql_desc_auto_unique_value;
    00139         DSTR sql_desc_base_column_name;
    00140         DSTR sql_desc_base_table_name;
    00141         SQLINTEGER sql_desc_case_sensitive;
    00142         DSTR sql_desc_catalog_name;
    00143         SQLSMALLINT sql_desc_concise_type;
    00144         SQLPOINTER sql_desc_data_ptr;
    00145         SQLSMALLINT sql_desc_datetime_interval_code;
    00146         SQLINTEGER sql_desc_datetime_interval_precision;
    00147         SQLLEN sql_desc_display_size;
    00148         SQLSMALLINT sql_desc_fixed_prec_scale;
    00149         SQLLEN *sql_desc_indicator_ptr;
    00150         DSTR sql_desc_label;
    00151         SQLULEN sql_desc_length;
    00152         /* this point to a constant buffer, do not free or modify */
    00153         const char *sql_desc_literal_prefix;
    00154         /* this point to a constant buffer, do not free or modify */
    00155         const char *sql_desc_literal_suffix;
    00156         DSTR sql_desc_local_type_name;
    00157         DSTR sql_desc_name;
    00158         SQLSMALLINT sql_desc_nullable;
    00159         SQLINTEGER sql_desc_num_prec_radix;
    00160         SQLLEN sql_desc_octet_length;
    00161         SQLLEN *sql_desc_octet_length_ptr;
    00162         SQLSMALLINT sql_desc_parameter_type;
    00163         SQLSMALLINT sql_desc_precision;
    00164         SQLSMALLINT sql_desc_rowver;
    00165         SQLSMALLINT sql_desc_scale;
    00166         DSTR sql_desc_schema_name;
    00167         SQLSMALLINT sql_desc_searchable;
    00168         DSTR sql_desc_table_name;
    00169         SQLSMALLINT sql_desc_type;
    00170         /* this point to a constant buffer, do not free or modify */
    00171         const char *sql_desc_type_name;
    00172         SQLSMALLINT sql_desc_unnamed;
    00173         SQLSMALLINT sql_desc_unsigned;
    00174         SQLSMALLINT sql_desc_updatable;
    00175 };
    00176
    00177 struct _hdesc
    00178 {
    00179         SQLSMALLINT htype;      /* do not reorder this field */
    00180         int type;
    00181         SQLHANDLE parent;
    00182         struct _dheader header;
    00183         struct _drecord *records;
    00184         struct _sql_errors errs;
    00185 };
    00186
    00187 typedef struct _hdesc TDS_DESC;
    00188
    00189 #define DESC_IRD        1
    00190 #define DESC_IPD        2
    00191 #define DESC_ARD        3
    00192 #define DESC_APD        4
    00193
    00194 struct _heattr
    00195 {
    00196         SQLUINTEGER connection_pooling;
    00197         SQLUINTEGER cp_match;
    00198         SQLINTEGER odbc_version;
    00199         SQLINTEGER output_nts;
    00200 };
    00201
    00202 struct _hchk
    00203 {
    00204         SQLSMALLINT htype;      /* do not reorder this field */
    00205 };
    00206
    00207 struct _henv
    00208 {
    00209         SQLSMALLINT htype;      /* do not reorder this field */
    00210         TDSCONTEXT *tds_ctx;
    00211         struct _sql_errors errs;
    00212         struct _heattr attr;
    00213 };
    00214
    00215 struct _hcattr
    00216 {
    00217         SQLUINTEGER access_mode;
    00218         SQLUINTEGER async_enable;
    00219         SQLUINTEGER auto_ipd;
    00220         SQLUINTEGER autocommit;
    00221         SQLUINTEGER connection_dead;
    00222         SQLUINTEGER connection_timeout;
    00223         DSTR current_catalog;
    00224         SQLUINTEGER login_timeout;
    00225         SQLUINTEGER metadata_id;
    00226         SQLUINTEGER odbc_cursors;
    00227         SQLUINTEGER packet_size;
    00228         SQLHWND quite_mode;
    00229         DSTR translate_lib;
    00230         SQLUINTEGER translate_option;
    00231         SQLUINTEGER txn_isolation;
    00232         SQLUINTEGER cursor_type;
    00233 #ifdef TDS_NO_DM
    00234         SQLUINTEGER trace;
    00235         DSTR tracefile;
    00236 #endif
    00237 };
    00238
    00239 #define TDS_MAX_APP_DESC        100
    00240
    00241 struct _hstmt;
    00242 struct _hdbc
    00243 {
    00244         SQLSMALLINT htype;      /* do not reorder this field */
    00245         struct _henv *env;
    00246         TDSSOCKET *tds_socket;
    00247         DSTR dsn;
    00248         DSTR server;            /* aka Instance */
    00254         struct _hstmt *current_statement;
    00256         struct _hstmt *stmt_list;
    00257         struct _sql_errors errs;
    00258         struct _hcattr attr;
    00260         TDS_DESC *uad[TDS_MAX_APP_DESC];
    00262         unsigned int cursor_support;
    00263         TDS_INT default_query_timeout;
    00264 };
    00265
    00266 struct _hsattr
    00267 {
    00268         /* TODO remove IRD, ARD, IPD, APD from statement, do not duplicate */
    00269 /*      TDS_DESC *app_row_desc; */
    00270 /*      TDS_DESC *app_param_desc; */
    00271         SQLUINTEGER async_enable;
    00272         SQLUINTEGER concurrency;
    00273         SQLUINTEGER cursor_scrollable;
    00274         SQLUINTEGER cursor_sensitivity;
    00275         SQLUINTEGER cursor_type;
    00276         SQLUINTEGER enable_auto_ipd;
    00277         SQLPOINTER fetch_bookmark_ptr;
    00278         SQLULEN keyset_size;
    00279         SQLULEN max_length;
    00280         SQLULEN max_rows;
    00281         SQLUINTEGER metadata_id;
    00282         SQLUINTEGER noscan;
    00283         /* apd->sql_desc_bind_offset_ptr */
    00284         /* SQLUINTEGER *param_bind_offset_ptr; */
    00285         /* apd->sql_desc_bind_type */
    00286         /* SQLUINTEGER param_bind_type; */
    00287         /* apd->sql_desc_array_status_ptr */
    00288         /* SQLUSMALLINT *param_operation_ptr; */
    00289         /* ipd->sql_desc_array_status_ptr */
    00290         /* SQLUSMALLINT *param_status_ptr; */
    00291         /* ipd->sql_desc_rows_processed_ptr */
    00292         /* SQLUSMALLINT *params_processed_ptr; */
    00293         /* apd->sql_desc_array_size */
    00294         /* SQLUINTEGER paramset_size; */
    00295         SQLUINTEGER query_timeout;
    00296         SQLUINTEGER retrieve_data;
    00297         /* ard->sql_desc_bind_offset_ptr */
    00298         /* SQLUINTEGER *row_bind_offset_ptr; */
    00299         /* ard->sql_desc_array_size */
    00300         /* SQLUINTEGER row_array_size; */
    00301         /* ard->sql_desc_bind_type */
    00302         /* SQLUINTEGER row_bind_type; */
    00303         SQLULEN row_number;
    00304         /* ard->sql_desc_array_status_ptr */
    00305         /* SQLUINTEGER *row_operation_ptr; */
    00306         /* ird->sql_desc_array_status_ptr */
    00307         /* SQLUINTEGER *row_status_ptr; */
    00308         /* ird->sql_desc_rows_processed_ptr */
    00309         /* SQLUINTEGER *rows_fetched_ptr; */
    00310         SQLUINTEGER simulate_cursor;
    00311         SQLUINTEGER use_bookmarks;
    00312         /* SQLGetStmtAttr only */
    00313 /*      TDS_DESC *imp_row_desc; */
    00314 /*      TDS_DESC *imp_param_desc; */
    00315 };
    00316
    00317 typedef enum
    00318 {
    00319         NOT_IN_ROW,
    00320         IN_NORMAL_ROW,
    00321         IN_COMPUTE_ROW,
    00322         AFTER_COMPUTE_ROW,
    00323         PRE_NORMAL_ROW
    00324 } TDS_ODBC_ROW_STATUS;
    00325
    00326 struct _hstmt
    00327 {
    00328         SQLSMALLINT htype;      /* do not reorder this field */
    00329         struct _hdbc *dbc;
    00331         char *query;
    00332
    00334         struct _hstmt *next;
    00336         struct _hstmt *prev;
    00337
    00338         /* begin prepared query stuff */
    00339         char *prepared_query;
    00340         unsigned prepared_query_is_func:1;
    00341         unsigned prepared_query_is_rpc:1;
    00342         unsigned need_reprepare:1;
    00343         unsigned param_data_called:1;
    00344         /* end prepared query stuff */
    00345
    00347         TDSPARAMINFO *params;
    00349         int param_num;
    00351         char *prepared_pos;
    00352
    00353         unsigned int curr_param_row, num_param_rows;
    00354
    00356         unsigned int param_count;
    00357         int row;
    00359         TDS_INT8 row_count;
    00361         TDS_ODBC_ROW_STATUS row_status;
    00362         /* do NOT free dynamic, free from socket or attach to connection */
    00363         TDSDYNAMIC *dyn;
    00364         struct _sql_errors errs;
    00365         TDS_DESC *ard, *ird, *apd, *ipd;
    00366         TDS_DESC *orig_ard, *orig_apd;
    00367         SQLULEN sql_rowset_size;
    00368         struct _hsattr attr;
    00369         DSTR cursor_name;       /* auto generated cursor name */
    00370         int special_row;
    00371         /* do NOT free cursor, free from socket or attach to connection */
    00372         TDSCURSOR *cursor;
    00373         unsigned char cancel_sent;
    00374 };
    00375
    00376 typedef struct _henv TDS_ENV;
    00377 typedef struct _hdbc TDS_DBC;
    00378 typedef struct _hstmt TDS_STMT;
    00379 typedef struct _hchk TDS_CHK;
    00380
    00381 #define IS_HENV(x) (((TDS_CHK *)x)->htype == SQL_HANDLE_ENV)
    00382 #define IS_HDBC(x) (((TDS_CHK *)x)->htype == SQL_HANDLE_DBC)
    00383 #define IS_HSTMT(x) (((TDS_CHK *)x)->htype == SQL_HANDLE_STMT)
    00384 #define IS_HDESC(x) (((TDS_CHK *)x)->htype == SQL_HANDLE_DESC)
    00385
    00386 /* fix a bug in MingW headers */
    00387 #ifdef __MINGW32__
    00388 #if SQL_INTERVAL_YEAR == (100 + SQL_CODE_SECOND)
    00389
    00390 #undef SQL_INTERVAL_YEAR
    00391 #undef SQL_INTERVAL_MONTH
    00392 #undef SQL_INTERVAL_DAY
    00393 #undef SQL_INTERVAL_HOUR
    00394 #undef SQL_INTERVAL_MINUTE
    00395 #undef SQL_INTERVAL_SECOND
    00396 #undef SQL_INTERVAL_YEAR_TO_MONTH
    00397 #undef SQL_INTERVAL_DAY_TO_HOUR
    00398 #undef SQL_INTERVAL_DAY_TO_MINUTE
    00399 #undef SQL_INTERVAL_DAY_TO_SECOND
    00400 #undef SQL_INTERVAL_HOUR_TO_MINUTE
    00401 #undef SQL_INTERVAL_HOUR_TO_SECOND
    00402 #undef SQL_INTERVAL_MINUTE_TO_SECOND
    00403
    00404 #define SQL_INTERVAL_YEAR                                       (100 + SQL_CODE_YEAR)
    00405 #define SQL_INTERVAL_MONTH                                      (100 + SQL_CODE_MONTH)
    00406 #define SQL_INTERVAL_DAY                                        (100 + SQL_CODE_DAY)
    00407 #define SQL_INTERVAL_HOUR                                       (100 + SQL_CODE_HOUR)
    00408 #define SQL_INTERVAL_MINUTE                                     (100 + SQL_CODE_MINUTE)
    00409 #define SQL_INTERVAL_SECOND                     (100 + SQL_CODE_SECOND)
    00410 #define SQL_INTERVAL_YEAR_TO_MONTH                      (100 + SQL_CODE_YEAR_TO_MONTH)
    00411 #define SQL_INTERVAL_DAY_TO_HOUR                        (100 + SQL_CODE_DAY_TO_HOUR)
    00412 #define SQL_INTERVAL_DAY_TO_MINUTE                      (100 + SQL_CODE_DAY_TO_MINUTE)
    00413 #define SQL_INTERVAL_DAY_TO_SECOND                      (100 + SQL_CODE_DAY_TO_SECOND)
    00414 #define SQL_INTERVAL_HOUR_TO_MINUTE                     (100 + SQL_CODE_HOUR_TO_MINUTE)
    00415 #define SQL_INTERVAL_HOUR_TO_SECOND                     (100 + SQL_CODE_HOUR_TO_SECOND)
    00416 #define SQL_INTERVAL_MINUTE_TO_SECOND           (100 + SQL_CODE_MINUTE_TO_SECOND)
    00417
    00418 #endif
    00419 #endif
    00420
    00421 #ifdef WIN32
    00422 BOOL get_login_info(HWND hwndParent, TDSCONNECTION * connection);
    00423 #endif
    00424
    00425 /*
    00426  * connectparams.h
    00427  */
    00435 int odbc_parse_connect_string(const char *connect_string, const char *connect_string_end, TDSCONNECTION * connection);
    00436 int odbc_get_dsn_info(const char *DSN, TDSCONNECTION * connection);
    00437
    00438 /*
    00439  * convert_tds2sql.c
    00440  */
    00441 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);
    00442
    00443 /*
    00444  * descriptor.c
    00445  */
    00446 TDS_DESC *desc_alloc(SQLHANDLE parent, int desc_type, int alloc_type);
    00447 SQLRETURN desc_free(TDS_DESC * desc);
    00448 SQLRETURN desc_alloc_records(TDS_DESC * desc, unsigned count);
    00449 SQLRETURN desc_copy(TDS_DESC * dest, TDS_DESC * src);
    00450 SQLRETURN desc_free_records(TDS_DESC * desc);
    00451
    00452 /*
    00453  * odbc.c
    00454  */
    00455 SQLRETURN _SQLRowCount(SQLHSTMT hstmt, SQLLEN FAR * pcrow);
    00456
    00457 /*
    00458  * odbc_checks.h
    00459  */
    00460 #if ENABLE_EXTRA_CHECKS
    00461 /* macro */
    00462 #define CHECK_ENV_EXTRA(env) odbc_check_env_extra(env)
    00463 #define CHECK_DBC_EXTRA(dbc) odbc_check_dbc_extra(dbc)
    00464 #define CHECK_STMT_EXTRA(stmt) odbc_check_stmt_extra(stmt)
    00465 #define CHECK_DESC_EXTRA(desc) odbc_check_desc_extra(desc)
    00466 /* declarations*/
    00467 void odbc_check_env_extra(TDS_ENV * env);
    00468 void odbc_check_dbc_extra(TDS_DBC * dbc);
    00469 void odbc_check_stmt_extra(TDS_STMT * stmt);
    00470 void odbc_check_desc_extra(TDS_DESC * desc);
    00471 #else
    00472 /* macro */
    00473 #define CHECK_ENV_EXTRA(env)
    00474 #define CHECK_DBC_EXTRA(dbc)
    00475 #define CHECK_STMT_EXTRA(stmt)
    00476 #define CHECK_DESC_EXTRA(desc)
    00477 #endif
    00478
    00479 /*
    00480  * odbc_util.h
    00481  */
    00482 int odbc_set_stmt_query(struct _hstmt *stmt, const char *sql, int sql_len);
    00483 int odbc_set_stmt_prepared_query(struct _hstmt *stmt, const char *sql, int sql_len);
    00484 void odbc_set_return_status(struct _hstmt *stmt, unsigned int n_row);
    00485 void odbc_set_return_params(struct _hstmt *stmt, unsigned int n_row);
    00486
    00487 SQLSMALLINT odbc_server_to_sql_type(int col_type, int col_size);
    00488 int odbc_sql_to_c_type_default(int sql_type);
    00489 int odbc_sql_to_server_type(TDSSOCKET * tds, int sql_type);
    00490 int odbc_c_to_server_type(int c_type);
    00491
    00492 void odbc_set_sql_type_info(TDSCOLUMN * col, struct _drecord *drec, SQLINTEGER odbc_ver);
    00493 SQLINTEGER odbc_sql_to_displaysize(int sqltype, TDSCOLUMN *col);
    00494 int odbc_get_string_size(int size, SQLCHAR * str);
    00495 void odbc_rdbms_version(TDSSOCKET * tds_socket, char *pversion_string);
    00496 SQLINTEGER odbc_get_param_len(const struct _drecord *drec_axd, const struct _drecord *drec_ixd, const TDS_DESC* axd, unsigned int n_row);
    00497
    00498 SQLRETURN odbc_set_string(SQLPOINTER buffer, SQLSMALLINT cbBuffer, SQLSMALLINT FAR * pcbBuffer, const char *s, int len);
    00499 SQLRETURN odbc_set_string_i(SQLPOINTER buffer, SQLINTEGER cbBuffer, SQLINTEGER FAR * pcbBuffer, const char *s, int len);
    00500
    00501 SQLSMALLINT odbc_get_concise_sql_type(SQLSMALLINT type, SQLSMALLINT interval);
    00502 SQLRETURN odbc_set_concise_sql_type(SQLSMALLINT concise_type, struct _drecord *drec, int check_only);
    00503 SQLSMALLINT odbc_get_concise_c_type(SQLSMALLINT type, SQLSMALLINT interval);
    00504 SQLRETURN odbc_set_concise_c_type(SQLSMALLINT concise_type, struct _drecord *drec, int check_only);
    00505
    00506 SQLLEN odbc_get_octet_len(int c_type, const struct _drecord *drec);
    00507
    00508 /*
    00509  * prepare_query.c
    00510  */
    00511 SQLRETURN prepare_call(struct _hstmt *stmt);
    00512 SQLRETURN native_sql(struct _hdbc *dbc, char *s);
    00513 int parse_prepared_query(struct _hstmt *stmt, int compute_row);
    00514 int start_parse_prepared_query(struct _hstmt *stmt, int compute_row);
    00515 int continue_parse_prepared_query(struct _hstmt *stmt, SQLPOINTER DataPtr, SQLLEN StrLen_or_Ind);
    00516 const char *parse_const_param(const char * s, TDS_SERVER_TYPE *type);
    00517
    00518 /*
    00519  * sql2tds.c
    00520  */
    00521 SQLRETURN sql2tds(TDS_STMT * stmt, const struct _drecord *drec_ixd, const struct _drecord *drec_axd, TDSCOLUMN *curcol, int compute_row, const TDS_DESC* axd, unsigned int n_row);
    00522
    00523 #if defined(__GNUC__) && __GNUC__ >= 4
    00524 #pragma GCC visibility pop
    00525 #endif
    00526
    00527 #ifdef __cplusplus
    00528 #if 0
    00529 {
    00530 #endif
    00531 }
    00532 #endif
    00533
    00534 #endif