1/*********************************************************************
2 *
3 * This is based on code created by Peter Harvey,
4 * (pharvey@codebydesign.com).
5 *
6 * Modified and extended by Nick Gorham
7 * (nick@lurcher.org).
8 *
9 * Any bugs or problems should be considered the fault of Nick and not
10 * Peter.
11 *
12 * copyright (c) 1999 Nick Gorham
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 **********************************************************************
29 *
30 * $Id: SQLCloseCursor.c,v 1.5 2009/02/18 17:59:08 lurcher Exp $
31 *
32 * $Log: SQLCloseCursor.c,v $
33 * Revision 1.5 2009/02/18 17:59:08 lurcher
34 * Shift to using config.h, the compile lines were making it hard to spot warnings
35 *
36 * Revision 1.4 2003/10/30 18:20:45 lurcher
37 *
38 * Fix broken thread protection
39 * Remove SQLNumResultCols after execute, lease S4/S% to driver
40 * Fix string overrun in SQLDriverConnect
41 * Add initial support for Interix
42 *
43 * Revision 1.3 2002/12/05 17:44:30 lurcher
44 *
45 * Display unknown return values in return logging
46 *
47 * Revision 1.2 2002/08/15 08:10:33 lurcher
48 *
49 * Couple of small fixes from John L Miller
50 *
51 * Revision 1.1.1.1 2001/10/17 16:40:05 lurcher
52 *
53 * First upload to SourceForge
54 *
55 * Revision 1.2 2001/04/12 17:43:35 nick
56 *
57 * Change logging and added autotest to odbctest
58 *
59 * Revision 1.1.1.1 2000/09/04 16:42:52 nick
60 * Imported Sources
61 *
62 * Revision 1.7 1999/11/13 23:40:58 ngorham
63 *
64 * Alter the way DM logging works
65 * Upgrade the Postgres driver to 6.4.6
66 *
67 * Revision 1.6 1999/10/24 23:54:17 ngorham
68 *
69 * First part of the changes to the error reporting
70 *
71 * Revision 1.5 1999/09/21 22:34:24 ngorham
72 *
73 * Improve performance by removing unneeded logging calls when logging is
74 * disabled
75 *
76 * Revision 1.4 1999/07/10 21:10:15 ngorham
77 *
78 * Adjust error sqlstate from driver manager, depending on requested
79 * version (ODBC2/3)
80 *
81 * Revision 1.3 1999/07/04 21:05:06 ngorham
82 *
83 * Add LGPL Headers to code
84 *
85 * Revision 1.2 1999/06/30 23:56:54 ngorham
86 *
87 * Add initial thread safety code
88 *
89 * Revision 1.1.1.1 1999/05/29 13:41:05 sShandyb
90 * first go at it
91 *
92 * Revision 1.1.1.1 1999/05/27 18:23:17 pharvey
93 * Imported sources
94 *
95 * Revision 1.2 1999/04/30 16:22:47 nick
96 * Another checkpoint
97 *
98 * Revision 1.1 1999/04/25 23:06:11 nick
99 * Initial revision
100 *
101 *
102 **********************************************************************/
103
104#include <config.h>
105#include "drivermanager.h"
106
107static char const rcsid[]= "$RCSfile: SQLCloseCursor.c,v $ $Revision: 1.5 $";
108
109SQLRETURN SQLCloseCursor( SQLHSTMT statement_handle )
110{
111 DMHSTMT statement = (DMHSTMT) statement_handle;
112 SQLRETURN ret;
113 SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ];
114
115 /*
116 * check statement
117 */
118
119 if ( !__validate_stmt( statement ))
120 {
121 dm_log_write( __FILE__,
122 __LINE__,
123 LOG_INFO,
124 LOG_INFO,
125 "Error: SQL_INVALID_HANDLE" );
126
127 return SQL_INVALID_HANDLE;
128 }
129
130 function_entry( statement );
131
132 if ( log_info.log_flag )
133 {
134 sprintf( statement -> msg, "\n\t\tEntry:\
135\n\t\t\tStatement = %p",
136 statement );
137
138 dm_log_write( __FILE__,
139 __LINE__,
140 LOG_INFO,
141 LOG_INFO,
142 statement -> msg );
143 }
144
145 thread_protect( SQL_HANDLE_STMT, statement );
146
147 /*
148 * check states
149 */
150
151 if ( statement -> state == STATE_S1 ||
152 statement -> state == STATE_S2 ||
153 statement -> state == STATE_S3 ||
154 statement -> state == STATE_S4 )
155 {
156 dm_log_write( __FILE__,
157 __LINE__,
158 LOG_INFO,
159 LOG_INFO,
160 "Error: 24000" );
161
162 __post_internal_error( &statement -> error,
163 ERROR_24000, NULL,
164 statement -> connection -> environment -> requested_version );
165
166 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
167 }
168 else if ( statement -> state == STATE_S8 ||
169 statement -> state == STATE_S9 ||
170 statement -> state == STATE_S10 ||
171 statement -> state == STATE_S11 ||
172 statement -> state == STATE_S12 ||
173 statement -> state == STATE_S13 ||
174 statement -> state == STATE_S14 ||
175 statement -> state == STATE_S15 )
176 {
177 dm_log_write( __FILE__,
178 __LINE__,
179 LOG_INFO,
180 LOG_INFO,
181 "Error: HY010" );
182
183 __post_internal_error( &statement -> error,
184 ERROR_HY010, NULL,
185 statement -> connection -> environment -> requested_version );
186
187 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
188 }
189
190 if ( !CHECK_SQLCLOSECURSOR( statement -> connection ))
191 {
192 if ( !CHECK_SQLFREESTMT( statement -> connection ))
193 {
194 dm_log_write( __FILE__,
195 __LINE__,
196 LOG_INFO,
197 LOG_INFO,
198 "Error: IM001" );
199
200 __post_internal_error( &statement -> error,
201 ERROR_IM001, NULL,
202 statement -> connection -> environment -> requested_version );
203
204 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
205 }
206 else
207 {
208 ret = SQLFREESTMT( statement -> connection,
209 statement -> driver_stmt,
210 SQL_CLOSE );
211 }
212 }
213 else
214 {
215 ret = SQLCLOSECURSOR( statement -> connection,
216 statement -> driver_stmt );
217 }
218
219 if ( SQL_SUCCEEDED( ret ))
220 {
221 if ( statement -> prepared )
222 statement -> state = STATE_S3;
223 else
224 statement -> state = STATE_S1;
225 }
226
227 if ( log_info.log_flag )
228 {
229 sprintf( statement -> msg,
230 "\n\t\tExit:[%s]",
231 __get_return_status( ret, s1 ));
232
233 dm_log_write( __FILE__,
234 __LINE__,
235 LOG_INFO,
236 LOG_INFO,
237 statement -> msg );
238 }
239
240 return function_return( SQL_HANDLE_STMT, statement, ret );
241}
242