File: SQLExecDirect.c

package info (click to toggle)
unixodbc 2.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,524 kB
  • ctags: 7,656
  • sloc: ansic: 89,405; sh: 15,975; makefile: 1,574; yacc: 969; sql: 1
file content (311 lines) | stat: -rw-r--r-- 8,692 bytes parent folder | download | duplicates (4)
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
/*********************************************************************
 *
 * unixODBC Cursor Library
 *
 * Created by Nick Gorham
 * (nick@lurcher.org).
 *
 * 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: SQLExecDirect.c,v 1.6 2009/02/18 17:59:17 lurcher Exp $
 *
 * $Log: SQLExecDirect.c,v $
 * Revision 1.6  2009/02/18 17:59:17  lurcher
 * Shift to using config.h, the compile lines were making it hard to spot warnings
 *
 * Revision 1.5  2007/11/13 15:04:57  lurcher
 * Fix 64 bit cursor lib issues
 *
 * Revision 1.4  2002/11/19 18:52:28  lurcher
 *
 * Alter the cursor lib to not require linking to the driver manager.
 *
 * Revision 1.3  2002/01/21 18:00:51  lurcher
 *
 * Assorted fixed and changes, mainly UNICODE/bug fixes
 *
 * 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.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 <config.h>
#include "cursorlibrary.h"

/*
 * free the rowset info
 */

void free_rowset( CLHSTMT cl_statement )
{
    if ( cl_statement -> rowset_buffer )
    {
        free( cl_statement -> rowset_buffer );
        cl_statement -> rowset_buffer = NULL;
    }

    if ( cl_statement -> rowset_file )
    {
        fclose( cl_statement -> rowset_file );
        cl_statement -> rowset_file = NULL;
    }

    if ( cl_statement -> sql_text )
    {
        free( cl_statement -> sql_text );
        cl_statement -> sql_text = NULL;
    }

    if ( cl_statement -> column_names )
    {
        int i;

        for ( i = 0; i < cl_statement -> column_count; i ++ )
        {
            free( cl_statement -> column_names[ i ] );
        }
        free( cl_statement -> column_names );
        cl_statement -> column_names = NULL;
    }

    if ( cl_statement -> data_type )
    {
        free( cl_statement -> data_type );
        cl_statement -> data_type = NULL;
    }

    if ( cl_statement -> column_size )
    {
        free( cl_statement -> column_size );
        cl_statement -> column_size = NULL;
    }

    if ( cl_statement -> decimal_digits )
    {
        free( cl_statement -> decimal_digits );
        cl_statement -> decimal_digits = NULL;
    }
}

/*
 * run through the bound columns, calculating the offsets
 */

int calculate_buffers( CLHSTMT cl_statement, int column_count )
{
    CLBCOL *bcol;

    cl_statement -> rowset_position = CL_BEFORE_START;
    cl_statement -> rowset_count = 0;
    cl_statement -> rowset_complete = 0;
    cl_statement -> column_count = column_count;

    /*
     * row status value
     */
    cl_statement -> buffer_length = sizeof( SQLUSMALLINT );

    bcol = cl_statement -> bound_columns;
    while ( bcol )
    {
        if ( bcol -> column_number <= column_count )
        {
            bcol -> rs_buffer_offset = cl_statement -> buffer_length;
            cl_statement -> buffer_length += bcol -> bound_length;

            bcol -> rs_ind_offset = cl_statement -> buffer_length;
            cl_statement -> buffer_length += sizeof( SQLULEN );
        }

        bcol = bcol -> next;
    }

    /*
     * allocate buffer
     */

    cl_statement -> rowset_buffer = malloc( cl_statement -> buffer_length );
    if ( !cl_statement -> rowset_buffer )
    {
        cl_statement -> cl_connection -> dh.__post_internal_error( &cl_statement -> dm_statement -> error,
                    ERROR_HY001, NULL,
                    cl_statement -> dm_statement -> connection -> 
                        environment -> requested_version );

        return SQL_ERROR;
    }

    /*
     * open temp file
     */

    cl_statement -> rowset_file = tmpfile();
    if ( !cl_statement -> rowset_file )
    {
        cl_statement -> cl_connection -> dh.__post_internal_error_ex( &cl_statement -> dm_statement -> error,
                    (SQLCHAR*)"S1000", 0,
                    (SQLCHAR*)"General Error, Unable to create file buffer",
                    SUBCLASS_ODBC, SUBCLASS_ODBC );

        return SQL_ERROR;
    }

    return SQL_SUCCESS;
}

SQLRETURN get_column_names( CLHSTMT cl_statement )
{
    int i;
    char cname[ 256 ];

    /*
     * already done ?
     */

    if ( cl_statement -> column_names )
    {
        return SQL_SUCCESS;
    }

    /*
     * get the names of all the columns
     */

    cl_statement -> column_names = malloc( sizeof(char *) 
            * cl_statement -> column_count );

    cl_statement -> data_type = malloc( sizeof( SQLSMALLINT ) 
            * cl_statement -> column_count );

    cl_statement -> column_size = malloc( sizeof( SQLULEN ) 
            * cl_statement -> column_count );

    cl_statement -> decimal_digits = malloc( sizeof( SQLSMALLINT ) 
            * cl_statement -> column_count );

    for ( i = 1; i <= cl_statement -> column_count; i ++ )
    {
        SQLRETURN ret;
    
        if ( !CHECK_SQLDESCRIBECOL( cl_statement -> cl_connection ))
        {
            cl_statement -> cl_connection -> dh.__post_internal_error( &cl_statement -> 
                    dm_statement -> error,
                    ERROR_01000, "Driver does not support SQLDescribeCol",
                    cl_statement -> dm_statement -> connection ->
                        environment -> requested_version );

            return SQL_ERROR;
        }
        ret = SQLDESCRIBECOL( cl_statement -> cl_connection,
                cl_statement -> driver_stmt,
                i,
                cname, 
                sizeof( cname ),
                NULL,
                &cl_statement -> data_type[ i - 1 ], 
                &cl_statement -> column_size[ i - 1 ],
                &cl_statement -> decimal_digits[ i - 1 ],
                NULL );

        if ( !SQL_SUCCEEDED( ret ))
        {
            cl_statement -> cl_connection -> dh.__post_internal_error( &cl_statement -> 
                    dm_statement -> error,
                    ERROR_01000, "SQLDescribeCol failed in driver",
                    cl_statement -> dm_statement -> connection ->
                        environment -> requested_version );

            return SQL_ERROR;
        }

        cl_statement -> column_names[ i - 1 ] = strdup( cname );
    }

    return SQL_SUCCESS;
}

SQLRETURN CLExecDirect( SQLHSTMT statement_handle,
           SQLCHAR *statement_text,
           SQLINTEGER text_length )
{
    CLHSTMT cl_statement = (CLHSTMT) statement_handle; 
    SQLRETURN ret;

    /*
     * save the statement for later use
     */

    if ( cl_statement -> sql_text )
    {
        free( cl_statement -> sql_text );
    }
    if ( text_length < 0 )
    {
        cl_statement -> sql_text = strdup((char*) statement_text );
    }
    else
    {
        cl_statement -> sql_text = malloc( text_length + 1 );
        memcpy( cl_statement -> sql_text, statement_text, text_length );
        cl_statement -> sql_text[ text_length ] = '\0';
    }

    ret = SQLEXECDIRECT( cl_statement -> cl_connection,
           cl_statement -> driver_stmt,
           statement_text,
           text_length );

    if ( SQL_SUCCEEDED( ret ))
    {
        SQLSMALLINT column_count;

        ret = SQLNUMRESULTCOLS( cl_statement -> cl_connection,
           cl_statement -> driver_stmt,
           &column_count );

        cl_statement -> column_count = column_count;
        cl_statement -> first_fetch_done = 0;

        if ( column_count > 0 )
        {
            ret = get_column_names( cl_statement );
        }
    }

    return ret;
}