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: SQLRowCount.c,v 1.8 2009/02/18 17:59:08 lurcher Exp $
31 *
32 * $Log: SQLRowCount.c,v $
33 * Revision 1.8 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.7 2007/04/02 10:50:19 lurcher
37 * Fix some 64bit problems (only when sizeof(SQLLEN) == 8 )
38 *
39 * Revision 1.6 2003/10/30 18:20:46 lurcher
40 *
41 * Fix broken thread protection
42 * Remove SQLNumResultCols after execute, lease S4/S% to driver
43 * Fix string overrun in SQLDriverConnect
44 * Add initial support for Interix
45 *
46 * Revision 1.5 2002/12/05 17:44:31 lurcher
47 *
48 * Display unknown return values in return logging
49 *
50 * Revision 1.4 2002/08/23 09:42:37 lurcher
51 *
52 * Fix some build warnings with casts, and a AIX linker mod, to include
53 * deplib's on the link line, but not the libtool generated ones
54 *
55 * Revision 1.3 2002/08/12 13:17:52 lurcher
56 *
57 * Replicate the way the MS DM handles loading of driver libs, and allocating
58 * handles in the driver. usage counting in the driver means that dlopen is
59 * only called for the first use, and dlclose for the last. AllocHandle for
60 * the driver environment is only called for the first time per driver
61 * per application environment.
62 *
63 * Revision 1.2 2001/12/13 13:00:32 lurcher
64 *
65 * Remove most if not all warnings on 64 bit platforms
66 * Add support for new MS 3.52 64 bit changes
67 * Add override to disable the stopping of tracing
68 * Add MAX_ROWS support in postgres driver
69 *
70 * Revision 1.1.1.1 2001/10/17 16:40:06 lurcher
71 *
72 * First upload to SourceForge
73 *
74 * Revision 1.3 2001/07/03 09:30:41 nick
75 *
76 * Add ability to alter size of displayed message in the log
77 *
78 * Revision 1.2 2001/04/12 17:43:36 nick
79 *
80 * Change logging and added autotest to odbctest
81 *
82 * Revision 1.1.1.1 2000/09/04 16:42:52 nick
83 * Imported Sources
84 *
85 * Revision 1.7 1999/11/13 23:41:00 ngorham
86 *
87 * Alter the way DM logging works
88 * Upgrade the Postgres driver to 6.4.6
89 *
90 * Revision 1.6 1999/10/24 23:54:18 ngorham
91 *
92 * First part of the changes to the error reporting
93 *
94 * Revision 1.5 1999/09/21 22:34:25 ngorham
95 *
96 * Improve performance by removing unneeded logging calls when logging is
97 * disabled
98 *
99 * Revision 1.4 1999/07/10 21:10:17 ngorham
100 *
101 * Adjust error sqlstate from driver manager, depending on requested
102 * version (ODBC2/3)
103 *
104 * Revision 1.3 1999/07/04 21:05:08 ngorham
105 *
106 * Add LGPL Headers to code
107 *
108 * Revision 1.2 1999/06/30 23:56:55 ngorham
109 *
110 * Add initial thread safety code
111 *
112 * Revision 1.1.1.1 1999/05/29 13:41:08 sShandyb
113 * first go at it
114 *
115 * Revision 1.1.1.1 1999/05/27 18:23:18 pharvey
116 * Imported sources
117 *
118 * Revision 1.3 1999/04/30 16:22:47 nick
119 * Another checkpoint
120 *
121 * Revision 1.2 1999/04/29 20:47:37 nick
122 * Another checkpoint
123 *
124 * Revision 1.1 1999/04/25 23:06:11 nick
125 * Initial revision
126 *
127 *
128 **********************************************************************/
129
130#include <config.h>
131#include "drivermanager.h"
132
133static char const rcsid[]= "$RCSfile: SQLRowCount.c,v $ $Revision: 1.8 $";
134
135SQLRETURN SQLRowCount( SQLHSTMT statement_handle,
136 SQLLEN *rowcount )
137{
138 DMHSTMT statement = (DMHSTMT) statement_handle;
139 SQLRETURN ret;
140 SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ];
141
142 /*
143 * check statement
144 */
145
146 if ( !__validate_stmt( statement ))
147 {
148 if ( rowcount )
149 {
150 *rowcount = -1;
151 }
152
153 dm_log_write( __FILE__,
154 __LINE__,
155 LOG_INFO,
156 LOG_INFO,
157 "Error: SQL_INVALID_HANDLE" );
158
159 return SQL_INVALID_HANDLE;
160 }
161
162 function_entry( statement );
163
164 if ( log_info.log_flag )
165 {
166 sprintf( statement -> msg, "\n\t\tEntry:\
167\n\t\t\tStatement = %p\
168\n\t\t\tRow Count = %p",
169 statement,
170 rowcount );
171
172 dm_log_write( __FILE__,
173 __LINE__,
174 LOG_INFO,
175 LOG_INFO,
176 statement -> msg );
177 }
178
179 thread_protect( SQL_HANDLE_STMT, statement );
180
181 /*
182 * check states
183 */
184 if ( statement -> state == STATE_S1 ||
185 statement -> state == STATE_S2 ||
186 statement -> state == STATE_S3 ||
187 statement -> state == STATE_S8 ||
188 statement -> state == STATE_S9 ||
189 statement -> state == STATE_S10 ||
190 statement -> state == STATE_S11 ||
191 statement -> state == STATE_S12 ||
192 statement -> state == STATE_S13 ||
193 statement -> state == STATE_S14 ||
194 statement -> state == STATE_S15 )
195 {
196 if ( rowcount )
197 {
198 *rowcount = -1;
199 }
200
201 dm_log_write( __FILE__,
202 __LINE__,
203 LOG_INFO,
204 LOG_INFO,
205 "Error: HY010" );
206
207 __post_internal_error( &statement -> error,
208 ERROR_HY010, NULL,
209 statement -> connection -> environment -> requested_version );
210
211 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
212 }
213
214 if ( !CHECK_SQLROWCOUNT( statement -> connection ))
215 {
216 if ( rowcount )
217 {
218 *rowcount = -1;
219 }
220
221 dm_log_write( __FILE__,
222 __LINE__,
223 LOG_INFO,
224 LOG_INFO,
225 "Error: IM001" );
226
227 __post_internal_error( &statement -> error,
228 ERROR_IM001, NULL,
229 statement -> connection -> environment -> requested_version );
230
231 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
232 }
233
234 ret = DEF_SQLROWCOUNT( statement -> connection,
235 statement -> driver_stmt,
236 rowcount );
237
238 if ( log_info.log_flag )
239 {
240 sprintf( statement -> msg,
241 "\n\t\tExit:[%s]\
242\n\t\t\tRow Count = %s",
243 __get_return_status( ret, s1 ),
244 __ptr_as_string( s1, rowcount ));
245
246 dm_log_write( __FILE__,
247 __LINE__,
248 LOG_INFO,
249 LOG_INFO,
250 statement -> msg );
251 }
252
253 return function_return( SQL_HANDLE_STMT, statement, ret );
254}
255