File: SQLGetData.c

package info (click to toggle)
unixodbc 2.1.1-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,668 kB
  • ctags: 12,486
  • sloc: ansic: 107,685; cpp: 33,663; sh: 13,300; makefile: 2,926; yacc: 499; lex: 241; sed: 93; sql: 1
file content (351 lines) | stat: -rw-r--r-- 10,303 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*********************************************************************
 *
 * unixODBC Cursor Library
 *
 * Created by Nick Gorham
 * (nick@easysoft.com).
 *
 * copyright (c) 1999 Nick Gorham
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 **********************************************************************
 *
 * $Id: SQLGetData.c,v 1.2 2001/12/13 13:00:33 lurcher Exp $
 *
 * $Log: SQLGetData.c,v $
 * Revision 1.2  2001/12/13 13:00:33  lurcher
 *
 * Remove most if not all warnings on 64 bit platforms
 * Add support for new MS 3.52 64 bit changes
 * Add override to disable the stopping of tracing
 * Add MAX_ROWS support in postgres driver
 *
 * Revision 1.1.1.1  2001/10/17 16:40:15  lurcher
 *
 * First upload to SourceForge
 *
 * Revision 1.2  2001/03/28 14:57:22  nick
 *
 * Fix bugs in corsor lib introduced bu UNCODE and other changes
 *
 * Revision 1.1.1.1  2000/09/04 16:42:52  nick
 * Imported Sources
 *
 * Revision 1.1  1999/09/19 22:22:50  ngorham
 *
 *
 * Added first cursor library work, read only at the moment and only works
 * with selects with no where clause
 *
 *
 **********************************************************************/

#include "cursorlibrary.h"

SQLRETURN CLGetData( SQLHSTMT statement_handle,
           SQLUSMALLINT column_number,
           SQLSMALLINT target_type,
           SQLPOINTER target_value,
           SQLINTEGER buffer_length,
           SQLINTEGER *strlen_or_ind )
{
    CLHSTMT cl_statement = (CLHSTMT) statement_handle; 
    CLHDBC cl_connection = cl_statement -> cl_connection;
    SQLHANDLE hstmt;
    SQLRETURN ret;
    SQLCHAR sql[ 4095 ];
    CLBCOL *bound_columns;
    int i;

    if ( cl_statement -> not_from_select )
    {
        __post_internal_error( &cl_statement -> dm_statement -> error,
                ERROR_SL004, NULL,
                cl_statement -> dm_statement -> connection ->
                    environment -> requested_version );

        return SQL_ERROR;
    }

    /*
     * check we have what we need
     */

    if ( !CHECK_SQLBINDPARAM( cl_connection ) && 
                !CHECK_SQLBINDPARAMETER( cl_connection ))
    {
        __post_internal_error( &cl_statement -> dm_statement -> error,
                ERROR_S1000, "Driver can not bind parameters",
                cl_statement -> dm_statement -> connection ->
                    environment -> requested_version );

        return SQL_ERROR;
    }
    if ( !CHECK_SQLEXECDIRECT( cl_connection ) &&
            !( CHECK_SQLPREPARE( cl_connection ) && 
                CHECK_SQLEXECUTE( cl_connection )))
    {
        __post_internal_error( &cl_statement -> dm_statement -> error,
                ERROR_S1000, "Driver can not prepare or execute",
                cl_statement -> dm_statement -> connection ->
                    environment -> requested_version );

        return SQL_ERROR;
    }
    if ( !CHECK_SQLFETCH( cl_connection ))
    {
        __post_internal_error( &cl_statement -> dm_statement -> error,
                ERROR_S1000, "Driver can not fetch",
                cl_statement -> dm_statement -> connection ->
                    environment -> requested_version );

        return SQL_ERROR;
    }
    if ( !CHECK_SQLGETDATA( cl_connection ))
    {
        __post_internal_error( &cl_statement -> dm_statement -> error,
                ERROR_S1000, "Driver can not getdata",
                cl_statement -> dm_statement -> connection ->
                    environment -> requested_version );

        return SQL_ERROR;
    }

    /*
     * if it's not and closed resultset, and the driver 
     * can only support one active statement, then close 
     * the result set first
     */

    if ( !cl_statement -> rowset_complete &&
            cl_statement -> cl_connection -> active_statement_allowed == 1 )
    {
        int sav_start, sav_pos;

        sav_start = cl_statement -> curr_rowset_start;
        sav_pos = cl_statement -> rowset_position;

        complete_rowset( cl_statement, 0 );
        
        SQLFREESTMT( cl_connection,
                cl_statement -> driver_stmt,
                SQL_DROP );

        cl_statement -> driver_stmt_closed = 1;

        /*
         * restore the position
         */
        cl_statement -> curr_rowset_start = sav_start;
        cl_statement -> rowset_position = sav_pos;
    }

    /*
     * refresh the data
     */

    ret = fetch_row( cl_statement, 
            cl_statement -> curr_rowset_start +
                cl_statement -> cursor_pos - 1,
            -1 );

    ret = SQLALLOCSTMT( cl_connection, 
            cl_connection -> driver_dbc,
            &hstmt,
            NULL );

    if ( !SQL_SUCCEEDED( ret ))
    {       
        __post_internal_error( &cl_statement -> dm_statement -> error,
                ERROR_S1000, "SQLAllocStmt failed in driver",
                cl_statement -> dm_statement -> connection ->
                    environment -> requested_version );

        return SQL_ERROR;
    }

    /*
     * append the cryterior to the end of the statement, binding
     * the comumns while we are at it
     */

    strcpy((char*) sql, cl_statement -> sql_text );

    /*
     * assume for the moment that the selent has no WHERE
     */

    strcat((char*) sql, " WHERE" );

    /*
     * this works because the bound list is in column order
     */

    bound_columns = cl_statement -> bound_columns;

    for ( i = 0; i < cl_statement -> column_count; i ++ )
    {
        char addon[ 256 ];

        sprintf( addon, " %s = ?", cl_statement -> column_names[ i ] );

        if ( i > 0 )
        {
            strcat((char*) sql, " AND" );
        }

        strcat((char*) sql, addon );

        if ( CHECK_SQLBINDPARAMETER( cl_connection ))
        {
            ret = SQLBINDPARAMETER( cl_connection,
                    hstmt,
                    i + 1,
                    SQL_PARAM_INPUT,
                    bound_columns -> bound_type,
                    cl_statement -> data_type[ i ],
                    cl_statement -> column_size[ i ],
                    cl_statement -> decimal_digits[ i ],
                    bound_columns -> local_buffer,
                    bound_columns -> bound_length,
                    &bound_columns -> len_ind );
        }
        else
        {
            ret = SQLBINDPARAM( cl_connection,
                    hstmt,
                    i + 1,
                    bound_columns -> bound_type,
                    cl_statement -> data_type[ i ],
                    cl_statement -> column_size[ i ],
                    cl_statement -> decimal_digits[ i ],
                    bound_columns -> local_buffer,
                    &bound_columns -> len_ind );
        }

        if ( !SQL_SUCCEEDED( ret ))
        {
            __post_internal_error( &cl_statement -> dm_statement -> error,
                    ERROR_SL010, NULL,
                    cl_statement -> dm_statement -> connection ->
                        environment -> requested_version );

            SQLFREESTMT( cl_connection,
                    hstmt, 
                    SQL_DROP );

            return SQL_ERROR;
        }

        bound_columns = bound_columns -> next;
    }

    if ( CHECK_SQLEXECDIRECT( cl_connection ))
    {
        ret = SQLEXECDIRECT( cl_connection,
                    hstmt,
                    sql,
                    strlen((char*) sql ));
    }
    else
    {
        ret = SQLPREPARE( cl_connection,
                    hstmt,
                    sql,
                    strlen((char*) sql ));

        if ( SQL_SUCCEEDED( ret ))
        {
            ret = SQLEXECUTE( cl_connection,
                    hstmt );
        }
    }

    if ( !SQL_SUCCEEDED( ret ))
    {
        __post_internal_error( &cl_statement -> dm_statement -> error,
                ERROR_SL010, NULL,
                cl_statement -> dm_statement -> connection ->
                    environment -> requested_version );

        SQLFREESTMT( cl_connection,
                hstmt,
                SQL_DROP );

        return SQL_ERROR;
    }

    ret = SQLFETCH( cl_connection,
            hstmt );

    if ( !SQL_SUCCEEDED( ret ))
    {
        __post_internal_error( &cl_statement -> dm_statement -> error,
                ERROR_SL010, NULL,
                cl_statement -> dm_statement -> connection ->
                    environment -> requested_version );

        SQLFREESTMT( cl_connection,
                hstmt,
                SQL_DROP );

        return SQL_ERROR;
    }

    ret = SQLGETDATA( cl_connection,
                hstmt, 
                column_number,
                target_type,
                target_value,
                buffer_length,
                strlen_or_ind );

    if ( !SQL_SUCCEEDED( ret ))
    {
        SQLCHAR sqlstate[ 6 ];
        SQLINTEGER native_error, ind;
        SQLCHAR message_text[ SQL_MAX_MESSAGE_LENGTH + 1 ];
        SQLRETURN ret;

        /*
         * get the error from the driver before
         * loseing the connection
         */

        do
        {
            ret = SQLERROR( cl_connection,
                    SQL_NULL_HENV,
                    SQL_NULL_HSTMT,
                    hstmt,
                    sqlstate,
                    &native_error,
                    message_text,
                    sizeof( message_text ),
                    &ind );

            __post_internal_error_ex( &cl_statement -> dm_statement -> error,
                    sqlstate, native_error, message_text );
        }
        while ( SQL_SUCCEEDED( ret ));
    }

    SQLFREESTMT( cl_connection,
            hstmt, 
            SQL_DROP );

    return ret;
}