1/*
2 *  henv.h
3 *
4 *  $Id: henv.h,v 1.20 2006/07/10 13:49:29 source Exp $
5 *
6 *  Environment object management functions
7 *
8 *  The iODBC driver manager.
9 *
10 *  Copyright (C) 1995 by Ke Jin <kejin@empress.com>
11 *  Copyright (C) 1996-2006 by OpenLink Software <iodbc@openlinksw.com>
12 *  All Rights Reserved.
13 *
14 *  This software is released under the terms of either of the following
15 *  licenses:
16 *
17 *      - GNU Library General Public License (see LICENSE.LGPL)
18 *      - The BSD License (see LICENSE.BSD).
19 *
20 *  Note that the only valid version of the LGPL license as far as this
21 *  project is concerned is the original GNU Library General Public License
22 *  Version 2, dated June 1991.
23 *
24 *  While not mandated by the BSD license, any patches you make to the
25 *  iODBC source code may be contributed back into the iODBC project
26 *  at your discretion. Contributions will benefit the Open Source and
27 *  Data Access community as a whole. Submissions may be made at:
28 *
29 *      http://www.iodbc.org
30 *
31 *
32 *  GNU Library Generic Public License Version 2
33 *  ============================================
34 *  This library is free software; you can redistribute it and/or
35 *  modify it under the terms of the GNU Library General Public
36 *  License as published by the Free Software Foundation; only
37 *  Version 2 of the License dated June 1991.
38 *
39 *  This library is distributed in the hope that it will be useful,
40 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
41 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
42 *  Library General Public License for more details.
43 *
44 *  You should have received a copy of the GNU Library General Public
45 *  License along with this library; if not, write to the Free
46 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
47 *
48 *
49 *  The BSD License
50 *  ===============
51 *  Redistribution and use in source and binary forms, with or without
52 *  modification, are permitted provided that the following conditions
53 *  are met:
54 *
55 *  1. Redistributions of source code must retain the above copyright
56 *     notice, this list of conditions and the following disclaimer.
57 *  2. Redistributions in binary form must reproduce the above copyright
58 *     notice, this list of conditions and the following disclaimer in
59 *     the documentation and/or other materials provided with the
60 *     distribution.
61 *  3. Neither the name of OpenLink Software Inc. nor the names of its
62 *     contributors may be used to endorse or promote products derived
63 *     from this software without specific prior written permission.
64 *
65 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
66 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
67 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
68 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OPENLINK OR
69 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
70 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
71 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
72 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
73 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
74 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
75 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
76 */
77
78#ifndef	_HENV_H
79#define	_HENV_H
80
81#include <iodbc.h>
82#include <dlproc.h>
83
84#include <sql.h>
85#include <sqlext.h>
86#include <ithread.h>
87
88
89enum odbcapi_t
90  {
91    en_NullProc = 0
92#define FUNCDEF(A, B, C)	,B
93#include "henv.ci"
94#undef FUNCDEF
95    , __LAST_API_FUNCTION__
96  } ;
97
98
99#if (ODBCVER >= 0x300)
100/*
101 * SQL_ATTR_CONNECTION_POOLING value
102 */
103extern SQLINTEGER _iodbcdm_attr_connection_pooling;
104#endif
105
106
107typedef struct
108  {
109    int type;			/* must be 1st field */
110    HERR herr;			/* err list          */
111    SQLRETURN rc;
112
113    HENV henv;			/* driver's env list */
114    HDBC hdbc;			/* driver's dbc list */
115    int state;
116#if (ODBCVER >= 0x300)
117    SQLUINTEGER odbc_ver;    /* ODBC version of the application */
118
119    SQLINTEGER connection_pooling; /* SQL_ATTR_CONNECTION_POOLING value at the
120				      time of env creation */
121    SQLINTEGER cp_match;	/* connection pool matching method */
122    struct DBC *pdbc_pool;	/* connection pool */
123#endif
124
125    SQLSMALLINT err_rec;
126  }
127GENV_t;
128
129
130typedef struct
131  {
132    HENV next;			/* next attached env handle */
133    int refcount;		/* Driver's bookkeeping reference count */
134    HPROC dllproc_tab[__LAST_API_FUNCTION__];	/* driver api calls  */
135    HENV dhenv;			/* driver env handle    */
136    HDLL hdll;			/* driver share library handle */
137
138    SWORD thread_safe;		/* Is the driver threadsafe? */
139    SWORD unicode_driver;       /* Is the driver unicode? */
140    MUTEX_DECLARE (drv_lock);	/* Used only when driver is not threadsafe */
141
142#if (ODBCVER >= 0x300)
143    SQLUINTEGER dodbc_ver;	/* driver's ODBC version */
144#endif
145  }
146ENV_t;
147
148
149#define IS_VALID_HENV(x) \
150	((x) != SQL_NULL_HENV && ((GENV_t *)(x))->type == SQL_HANDLE_ENV)
151
152
153#define ENTER_HENV(henv, trace) \
154	GENV (genv, henv); \
155	SQLRETURN retcode = SQL_SUCCESS; \
156	ODBC_LOCK (); \
157	TRACE (trace); \
158	if (!IS_VALID_HENV (henv)) \
159	  { \
160	    retcode = SQL_INVALID_HANDLE; \
161	    goto done; \
162	  } \
163	CLEAR_ERRORS (genv)
164
165
166#define LEAVE_HENV(henv, trace) \
167    done: \
168     	TRACE(trace); \
169	ODBC_UNLOCK (); \
170	return (retcode)
171
172
173/*
174 * Multi threading
175 */
176#if defined (IODBC_THREADING)
177extern SPINLOCK_DECLARE(iodbcdm_global_lock);
178#define ODBC_LOCK()	SPINLOCK_LOCK(iodbcdm_global_lock)
179#define ODBC_UNLOCK()	SPINLOCK_UNLOCK(iodbcdm_global_lock)
180#else
181#define ODBC_LOCK()
182#define ODBC_UNLOCK()
183#endif
184
185/*
186 * Prototypes
187 */
188void Init_iODBC(void);
189void Done_iODBC(void);
190
191
192
193/* Note:
194 *
195 *  - ODBC applications only know about global environment handle,
196 *    a void pointer points to a GENV_t object. There is only one
197 *    this object per process(however, to make the library reentrant,
198 *    we still keep this object on heap). Applications only know
199 *    address of this object and needn't care about its detail.
200 *
201 *  - ODBC driver manager knows about instance environment handles,
202 *    void pointers point to ENV_t objects. There are maybe more
203 *    than one this kind of objects per process. However, multiple
204 *    connections to a same data source(i.e. call same share library)
205 *    will share one instance environment object.
206 *
207 *  - ODBC driver manager knows about their own environment handle,
208 *    a void pointer point to a driver defined object. Every driver
209 *    keeps one of its own environment object and driver manager
210 *    keeps address of it by the 'dhenv' field in the instance
211 *    environment object without care about its detail.
212 *
213 *  - Applications can get driver's environment object handle by
214 *    SQLGetInfo() with fInfoType equals to SQL_DRIVER_HENV
215 */
216#endif
217