~ubuntu-branches/ubuntu/trusty/psqlodbc/trusty-proposed

« back to all changes in this revision

Viewing changes to results.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2013-10-24 07:21:55 UTC
  • mfrom: (16.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20131024072155-xlf5odyk3iblcd51
Tags: 1:09.02.0100-2ubuntu1
* Merge with Debian unstable. Remaining Ubuntu changes:
  - debian/tests: Disable iodbc test and dependency, as in Ubuntu iodbc and
    unixodbc are not installable in parallel, and iodbc is obsolete and
    should be removed at some point.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Module:                      results.c
3
 
 *
4
 
 * Description:         This module contains functions related to
5
 
 *                                      retrieving result information through the ODBC API.
6
 
 *
7
 
 * Classes:                     n/a
8
 
 *
9
 
 * API functions:       SQLRowCount, SQLNumResultCols, SQLDescribeCol,
10
 
 *                                      SQLColAttributes, SQLGetData, SQLFetch, SQLExtendedFetch,
11
 
 *                                      SQLMoreResults, SQLSetPos, SQLSetScrollOptions(NI),
12
 
 *                                      SQLSetCursorName, SQLGetCursorName
13
 
 *
14
 
 * Comments:            See "notice.txt" for copyright and license information.
15
 
 *-------
16
 
 */
17
 
 
18
 
#include "psqlodbc.h"
19
 
 
20
 
#include <string.h>
21
 
#include "psqlodbc.h"
22
 
#include "dlg_specific.h"
23
 
#include "environ.h"
24
 
#include "connection.h"
25
 
#include "statement.h"
26
 
#include "bind.h"
27
 
#include "qresult.h"
28
 
#include "convert.h"
29
 
#include "pgtypes.h"
30
 
 
31
 
#include <stdio.h>
32
 
#include <limits.h>
33
 
 
34
 
#include "pgapifunc.h"
35
 
 
36
 
/*      Helper macro */
37
 
#define getEffectiveOid(conn, fi) pg_true_type((conn), (fi)->columntype, FI_type(fi))
38
 
 
39
 
 
40
 
RETCODE         SQL_API
41
 
PGAPI_RowCount(
42
 
                           HSTMT hstmt,
43
 
                           SQLLEN FAR * pcrow)
44
 
{
45
 
        CSTR func = "PGAPI_RowCount";
46
 
        StatementClass *stmt = (StatementClass *) hstmt;
47
 
        QResultClass *res;
48
 
        ConnInfo   *ci;
49
 
 
50
 
        mylog("%s: entering...\n", func);
51
 
        if (!stmt)
52
 
        {
53
 
                SC_log_error(func, NULL_STRING, NULL);
54
 
                return SQL_INVALID_HANDLE;
55
 
        }
56
 
        ci = &(SC_get_conn(stmt)->connInfo);
57
 
        if (stmt->proc_return > 0)
58
 
        {
59
 
                if (pcrow)
60
 
{
61
 
                        *pcrow = 0;
62
 
inolog("returning RowCount=%d\n", *pcrow);
63
 
}
64
 
                return SQL_SUCCESS;
65
 
        }
66
 
 
67
 
        res = SC_get_Curres(stmt);
68
 
        if (res && pcrow)
69
 
        {
70
 
                if (stmt->status != STMT_FINISHED)
71
 
                {
72
 
                        SC_set_error(stmt, STMT_SEQUENCE_ERROR, "Can't get row count while statement is still executing.", func);
73
 
                        return  SQL_ERROR;
74
 
                }
75
 
                if (res->recent_processed_row_count >= 0)
76
 
                {
77
 
                        *pcrow = res->recent_processed_row_count;
78
 
                        mylog("**** %s: THE ROWS: *pcrow = %d\n", func, *pcrow);
79
 
 
80
 
                        return SQL_SUCCESS;
81
 
                }
82
 
                else if (QR_NumResultCols(res) > 0)
83
 
                {
84
 
                        *pcrow = QR_get_cursor(res) ? -1 : QR_get_num_total_tuples(res) - res->dl_count;
85
 
                        mylog("RowCount=%d\n", *pcrow);
86
 
                        return SQL_SUCCESS;
87
 
                }
88
 
        }
89
 
 
90
 
        *pcrow = -1;
91
 
        return SQL_SUCCESS;
92
 
}
93
 
 
94
 
static BOOL SC_pre_execute_ok(StatementClass *stmt, BOOL build_fi, int col_idx, const char *func)
95
 
{
96
 
        Int2 num_fields = SC_pre_execute(stmt);
97
 
        QResultClass    *result = SC_get_Curres(stmt);
98
 
        BOOL    exec_ok = TRUE;
99
 
 
100
 
        mylog("%s: result = %p, status = %d, numcols = %d\n", func, result, stmt->status, result != NULL ? QR_NumResultCols(result) : -1);
101
 
        /****if ((!result) || ((stmt->status != STMT_FINISHED) && (stmt->status != STMT_PREMATURE))) ****/
102
 
        if (!QR_command_maybe_successful(result) || num_fields < 0)
103
 
        {
104
 
                /* no query has been executed on this statement */
105
 
                SC_set_error(stmt, STMT_EXEC_ERROR, "No query has been executed with that handle", func);
106
 
                exec_ok = FALSE;
107
 
        }
108
 
        else if (col_idx >= 0 && col_idx < num_fields)
109
 
        {
110
 
                OID     reloid = QR_get_relid(result, col_idx);
111
 
                IRDFields       *irdflds = SC_get_IRDF(stmt);
112
 
                FIELD_INFO      *fi;
113
 
                TABLE_INFO      *ti = NULL;
114
 
 
115
 
inolog("build_fi=%d reloid=%u\n", build_fi, reloid);
116
 
                if (build_fi && 0 != QR_get_attid(result, col_idx))
117
 
                        getCOLIfromTI(func, NULL, stmt, reloid, &ti);
118
 
inolog("nfields=%d\n", irdflds->nfields);
119
 
                if (irdflds->fi && col_idx < (int) irdflds->nfields)
120
 
                {
121
 
                        fi = irdflds->fi[col_idx];
122
 
                        if (fi)
123
 
                        {
124
 
                                if (ti)
125
 
                                {
126
 
                                        if (NULL == fi->ti)
127
 
                                                fi->ti = ti;
128
 
                                        if (!FI_is_applicable(fi)
129
 
                                            && 0 != (ti->flags & TI_COLATTRIBUTE))
130
 
                                                fi->flag |= FIELD_COL_ATTRIBUTE;
131
 
                                }
132
 
                                fi->basetype = QR_get_field_type(result, col_idx);
133
 
                                if (0 == fi->columntype)
134
 
                                        fi->columntype = fi->basetype;
135
 
                        }
136
 
                }
137
 
        }
138
 
        return exec_ok;
139
 
}
140
 
 
141
 
/*
142
 
 *      This returns the number of columns associated with the database
143
 
 *      attached to "hstmt".
144
 
 */
145
 
RETCODE         SQL_API
146
 
PGAPI_NumResultCols(
147
 
                                        HSTMT hstmt,
148
 
                                        SQLSMALLINT FAR * pccol)
149
 
{
150
 
        CSTR func = "PGAPI_NumResultCols";
151
 
        StatementClass *stmt = (StatementClass *) hstmt;
152
 
        QResultClass *result;
153
 
        char            parse_ok;
154
 
        ConnInfo   *ci;
155
 
        RETCODE         ret = SQL_SUCCESS;
156
 
 
157
 
        mylog("%s: entering...\n", func);
158
 
        if (!stmt)
159
 
        {
160
 
                SC_log_error(func, NULL_STRING, NULL);
161
 
                return SQL_INVALID_HANDLE;
162
 
        }
163
 
        ci = &(SC_get_conn(stmt)->connInfo);
164
 
 
165
 
        SC_clear_error(stmt);
166
 
#define return  DONT_CALL_RETURN_FROM_HERE???
167
 
        /* StartRollbackState(stmt); */
168
 
 
169
 
        if (stmt->proc_return > 0)
170
 
        {
171
 
                *pccol = 0;
172
 
                goto cleanup;
173
 
        }
174
 
        parse_ok = FALSE;
175
 
        if (!stmt->catalog_result && SC_is_parse_forced(stmt) && SC_can_parse_statement(stmt))
176
 
        {
177
 
                if (SC_parsed_status(stmt) == STMT_PARSE_NONE)
178
 
                {
179
 
                        mylog("%s: calling parse_statement on stmt=%p\n", func, stmt);
180
 
                        parse_statement(stmt, FALSE);
181
 
                }
182
 
 
183
 
                if (SC_parsed_status(stmt) != STMT_PARSE_FATAL)
184
 
                {
185
 
                        parse_ok = TRUE;
186
 
                        *pccol = SC_get_IRDF(stmt)->nfields;
187
 
                        mylog("PARSE: %s: *pccol = %d\n", func, *pccol);
188
 
                }
189
 
        }
190
 
 
191
 
        if (!parse_ok)
192
 
        {
193
 
                if (!SC_pre_execute_ok(stmt, FALSE, -1, func))
194
 
                {
195
 
                        ret = SQL_ERROR;
196
 
                        goto cleanup;
197
 
                }
198
 
 
199
 
                result = SC_get_Curres(stmt);
200
 
                *pccol = QR_NumPublicResultCols(result);
201
 
        }
202
 
 
203
 
cleanup:
204
 
#undef  return
205
 
        if (stmt->internal)
206
 
                ret = DiscardStatementSvp(stmt, ret, FALSE);
207
 
        return ret;
208
 
}
209
 
 
210
 
 
211
 
/*
212
 
 *      Return information about the database column the user wants
213
 
 *      information about.
214
 
 */
215
 
RETCODE         SQL_API
216
 
PGAPI_DescribeCol(
217
 
                                  HSTMT hstmt,
218
 
                                  SQLUSMALLINT icol,
219
 
                                  SQLCHAR FAR * szColName,
220
 
                                  SQLSMALLINT cbColNameMax,
221
 
                                  SQLSMALLINT FAR * pcbColName,
222
 
                                  SQLSMALLINT FAR * pfSqlType,
223
 
                                  SQLULEN FAR * pcbColDef,
224
 
                                  SQLSMALLINT FAR * pibScale,
225
 
                                  SQLSMALLINT FAR * pfNullable)
226
 
{
227
 
        CSTR func = "PGAPI_DescribeCol";
228
 
 
229
 
        /* gets all the information about a specific column */
230
 
        StatementClass *stmt = (StatementClass *) hstmt;
231
 
        ConnectionClass *conn;
232
 
        IRDFields       *irdflds;
233
 
        QResultClass    *res = NULL;
234
 
        char       *col_name = NULL;
235
 
        OID             fieldtype = 0;
236
 
        SQLLEN          column_size = 0;
237
 
        SQLINTEGER      decimal_digits = 0;
238
 
        ConnInfo   *ci;
239
 
        FIELD_INFO      *fi;
240
 
        char            buf[255];
241
 
        int                     len = 0;
242
 
        RETCODE         result = SQL_SUCCESS;
243
 
 
244
 
        mylog("%s: entering.%d..\n", func, icol);
245
 
 
246
 
        if (!stmt)
247
 
        {
248
 
                SC_log_error(func, NULL_STRING, NULL);
249
 
                return SQL_INVALID_HANDLE;
250
 
        }
251
 
 
252
 
        conn = SC_get_conn(stmt);
253
 
        ci = &(conn->connInfo);
254
 
 
255
 
        SC_clear_error(stmt);
256
 
 
257
 
#define return  DONT_CALL_RETURN_FROM_HERE???
258
 
        irdflds = SC_get_IRDF(stmt);
259
 
#if (ODBCVER >= 0x0300)
260
 
        if (0 == icol) /* bookmark column */
261
 
        {
262
 
                SQLSMALLINT     fType = stmt->options.use_bookmarks == SQL_UB_VARIABLE ? SQL_BINARY : SQL_INTEGER;
263
 
 
264
 
inolog("answering bookmark info\n");
265
 
                if (szColName && cbColNameMax > 0)
266
 
                        *szColName = '\0';
267
 
                if (pcbColName)
268
 
                        *pcbColName = 0;
269
 
                if (pfSqlType)
270
 
                        *pfSqlType = fType;
271
 
                if (pcbColDef)
272
 
                        *pcbColDef = 10;
273
 
                if (pibScale)
274
 
                        *pibScale = 0;
275
 
                if (pfNullable)
276
 
                        *pfNullable = SQL_NO_NULLS;
277
 
                result = SQL_SUCCESS;
278
 
                goto cleanup;
279
 
        }
280
 
#endif /* ODBCVER */
281
 
        /*
282
 
         * Dont check for bookmark column. This is the responsibility of the
283
 
         * driver manager.
284
 
         */
285
 
 
286
 
        icol--;                                         /* use zero based column numbers */
287
 
 
288
 
        fi = NULL;
289
 
        if (icol < irdflds->nfields && irdflds->fi)
290
 
                fi = irdflds->fi[icol];
291
 
        if (!FI_is_applicable(fi) && !stmt->catalog_result && SC_is_parse_forced(stmt) && SC_can_parse_statement(stmt))
292
 
        {
293
 
                if (SC_parsed_status(stmt) == STMT_PARSE_NONE)
294
 
                {
295
 
                        mylog("%s: calling parse_statement on stmt=%p\n", func, stmt);
296
 
                        parse_statement(stmt, FALSE);
297
 
                }
298
 
 
299
 
                mylog("PARSE: DescribeCol: icol=%d, stmt=%p, stmt->nfld=%d, stmt->fi=%p\n", icol, stmt, irdflds->nfields, irdflds->fi);
300
 
 
301
 
                if (SC_parsed_status(stmt) != STMT_PARSE_FATAL && irdflds->fi)
302
 
                {
303
 
                        if (icol < irdflds->nfields)
304
 
                                fi = irdflds->fi[icol];
305
 
                        else
306
 
                        {
307
 
                                SC_set_error(stmt, STMT_INVALID_COLUMN_NUMBER_ERROR, "Invalid column number in DescribeCol.", func);
308
 
                                result = SQL_ERROR;
309
 
                                goto cleanup;
310
 
                        }
311
 
                        mylog("DescribeCol: getting info for icol=%d\n", icol);
312
 
                }
313
 
        }
314
 
 
315
 
        if (!FI_is_applicable(fi))
316
 
        {
317
 
                /*
318
 
                 * If couldn't parse it OR the field being described was not parsed
319
 
                 * (i.e., because it was a function or expression, etc, then do it the
320
 
                 * old fashioned way.
321
 
                 */
322
 
                BOOL    build_fi = PROTOCOL_74(ci) && (NULL != pfNullable || NULL != pfSqlType);
323
 
                fi = NULL;
324
 
                if (!SC_pre_execute_ok(stmt, build_fi, icol, func))
325
 
                {
326
 
                        result = SQL_ERROR;
327
 
                        goto cleanup;
328
 
                }
329
 
 
330
 
                res = SC_get_Curres(stmt);
331
 
                if (icol >= QR_NumPublicResultCols(res))
332
 
                {
333
 
                        SC_set_error(stmt, STMT_INVALID_COLUMN_NUMBER_ERROR, "Invalid column number in DescribeCol.", NULL);
334
 
                        snprintf(buf, sizeof(buf), "Col#=%d, #Cols=%d,%d keys=%d", icol, QR_NumResultCols(res), QR_NumPublicResultCols(res), res->num_key_fields);
335
 
                        SC_log_error(func, buf, stmt);
336
 
                        result = SQL_ERROR;
337
 
                        goto cleanup;
338
 
                }
339
 
                if (icol < irdflds->nfields && irdflds->fi)
340
 
                        fi = irdflds->fi[icol];
341
 
        }
342
 
        if (FI_is_applicable(fi))
343
 
        {
344
 
                fieldtype = getEffectiveOid(conn, fi);
345
 
                if (NAME_IS_VALID(fi->column_alias))
346
 
                        col_name = GET_NAME(fi->column_alias);
347
 
                else
348
 
                        col_name = GET_NAME(fi->column_name);
349
 
                column_size = fi->column_size;
350
 
                decimal_digits = fi->decimal_digits;
351
 
 
352
 
                mylog("PARSE: fieldtype=%d, col_name='%s', column_size=%d\n", fieldtype, col_name, column_size);
353
 
        }               
354
 
        else
355
 
        {
356
 
                col_name = QR_get_fieldname(res, icol);
357
 
                fieldtype = QR_get_field_type(res, icol);
358
 
 
359
 
                /* atoi(ci->unknown_sizes) */
360
 
                column_size = pgtype_column_size(stmt, fieldtype, icol, ci->drivers.unknown_sizes);
361
 
                decimal_digits = pgtype_decimal_digits(stmt, fieldtype, icol);
362
 
        }
363
 
 
364
 
        mylog("describeCol: col %d fieldname = '%s'\n", icol, col_name);
365
 
        mylog("describeCol: col %d fieldtype = %d\n", icol, fieldtype);
366
 
        mylog("describeCol: col %d column_size = %d\n", icol, column_size);
367
 
 
368
 
        result = SQL_SUCCESS;
369
 
 
370
 
        /*
371
 
         * COLUMN NAME
372
 
         */
373
 
        len = (int) strlen(col_name);
374
 
 
375
 
        if (pcbColName)
376
 
                *pcbColName = len;
377
 
 
378
 
        if (szColName && cbColNameMax > 0)
379
 
        {
380
 
                strncpy_null(szColName, col_name, cbColNameMax);
381
 
 
382
 
                if (len >= cbColNameMax)
383
 
                {
384
 
                        result = SQL_SUCCESS_WITH_INFO;
385
 
                        SC_set_error(stmt, STMT_TRUNCATED, "The buffer was too small for the colName.", func);
386
 
                }
387
 
        }
388
 
 
389
 
        /*
390
 
         * CONCISE(SQL) TYPE
391
 
         */
392
 
        if (pfSqlType)
393
 
        {
394
 
                *pfSqlType = pgtype_to_concise_type(stmt, fieldtype, icol);
395
 
 
396
 
                mylog("describeCol: col %d *pfSqlType = %d\n", icol, *pfSqlType);
397
 
        }
398
 
 
399
 
        /*
400
 
         * COLUMN SIZE(PRECISION in 2.x)
401
 
         */
402
 
        if (pcbColDef)
403
 
        {
404
 
                if (column_size < 0)
405
 
                        column_size = 0;                /* "I dont know" */
406
 
 
407
 
                *pcbColDef = column_size;
408
 
 
409
 
                mylog("describeCol: col %d  *pcbColDef = %d\n", icol, *pcbColDef);
410
 
        }
411
 
 
412
 
        /*
413
 
         * DECIMAL DIGITS(SCALE in 2.x)
414
 
         */
415
 
        if (pibScale)
416
 
        {
417
 
                if (decimal_digits < 0)
418
 
                        decimal_digits = 0;
419
 
 
420
 
                *pibScale = (SQLSMALLINT) decimal_digits;
421
 
                mylog("describeCol: col %d  *pibScale = %d\n", icol, *pibScale);
422
 
        }
423
 
 
424
 
        /*
425
 
         * NULLABILITY
426
 
         */
427
 
        if (pfNullable)
428
 
        {
429
 
                if (SC_has_outer_join(stmt))
430
 
                        *pfNullable = TRUE;
431
 
                else
432
 
                        *pfNullable = fi ? fi->nullable : pgtype_nullable(conn, fieldtype);
433
 
 
434
 
                mylog("describeCol: col %d  *pfNullable = %d\n", icol, *pfNullable);
435
 
        }
436
 
 
437
 
cleanup:
438
 
#undef  return
439
 
        if (stmt->internal)
440
 
                result = DiscardStatementSvp(stmt, result, FALSE);
441
 
        return result;
442
 
}
443
 
 
444
 
 
445
 
/*              Returns result column descriptor information for a result set. */
446
 
RETCODE         SQL_API
447
 
PGAPI_ColAttributes(
448
 
                                        HSTMT hstmt,
449
 
                                        SQLUSMALLINT icol,
450
 
                                        SQLUSMALLINT fDescType,
451
 
                                        PTR rgbDesc,
452
 
                                        SQLSMALLINT cbDescMax,
453
 
                                        SQLSMALLINT FAR * pcbDesc,
454
 
                                        SQLLEN FAR * pfDesc)
455
 
{
456
 
        CSTR func = "PGAPI_ColAttributes";
457
 
        StatementClass *stmt = (StatementClass *) hstmt;
458
 
        IRDFields       *irdflds;
459
 
        OID             field_type = 0;
460
 
        Int2            col_idx;
461
 
        ConnectionClass *conn;
462
 
        ConnInfo        *ci;
463
 
        int             column_size, unknown_sizes;
464
 
        int                     cols = 0;
465
 
        RETCODE         result;
466
 
        const char   *p = NULL;
467
 
        SQLLEN          value = 0;
468
 
        const   FIELD_INFO      *fi = NULL;
469
 
        const   TABLE_INFO      *ti = NULL;
470
 
        QResultClass    *res;
471
 
        BOOL            stmt_updatable;
472
 
 
473
 
        mylog("%s: entering..col=%d %d len=%d.\n", func, icol, fDescType,
474
 
                                cbDescMax);
475
 
 
476
 
        if (!stmt)
477
 
        {
478
 
                SC_log_error(func, NULL_STRING, NULL);
479
 
                return SQL_INVALID_HANDLE;
480
 
        }
481
 
        stmt_updatable = SC_is_updatable(stmt)
482
 
                /* The following doesn't seem appropriate for client side cursors
483
 
                  && stmt->options.scroll_concurrency != SQL_CONCUR_READ_ONLY
484
 
                 */
485
 
                ;
486
 
 
487
 
        if (pcbDesc)
488
 
                *pcbDesc = 0;
489
 
        irdflds = SC_get_IRDF(stmt);
490
 
        conn = SC_get_conn(stmt);
491
 
        ci = &(conn->connInfo);
492
 
 
493
 
        /*
494
 
         * Dont check for bookmark column.      This is the responsibility of the
495
 
         * driver manager.      For certain types of arguments, the column number
496
 
         * is ignored anyway, so it may be 0.
497
 
         */
498
 
 
499
 
        res = SC_get_Curres(stmt);
500
 
#if (ODBCVER >= 0x0300)
501
 
        if (0 == icol && SQL_DESC_COUNT != fDescType) /* bookmark column */
502
 
        {
503
 
inolog("answering bookmark info\n");
504
 
                switch (fDescType)
505
 
                {
506
 
                        case SQL_DESC_OCTET_LENGTH:
507
 
                                if (pfDesc)
508
 
                                        *pfDesc = 4;
509
 
                                break;
510
 
                        case SQL_DESC_TYPE:
511
 
                                if (pfDesc)
512
 
                                        *pfDesc = stmt->options.use_bookmarks == SQL_UB_VARIABLE ? SQL_BINARY : SQL_INTEGER;
513
 
                                break;
514
 
                }
515
 
                return SQL_SUCCESS;
516
 
        }
517
 
#endif /* ODBCVER */
518
 
        col_idx = icol - 1;
519
 
 
520
 
        /* atoi(ci->unknown_sizes); */
521
 
        unknown_sizes = ci->drivers.unknown_sizes;
522
 
 
523
 
        /* not appropriate for SQLColAttributes() */
524
 
        if (stmt->catalog_result)
525
 
                unknown_sizes = UNKNOWNS_AS_CATALOG;
526
 
        else if (unknown_sizes == UNKNOWNS_AS_DONTKNOW)
527
 
                unknown_sizes = UNKNOWNS_AS_MAX;
528
 
 
529
 
        if (!stmt->catalog_result && SC_is_parse_forced(stmt) && SC_can_parse_statement(stmt))
530
 
        {
531
 
                if (SC_parsed_status(stmt) == STMT_PARSE_NONE)
532
 
                {
533
 
                        mylog("%s: calling parse_statement\n", func);
534
 
                        parse_statement(stmt, FALSE);
535
 
                }
536
 
 
537
 
                cols = irdflds->nfields;
538
 
 
539
 
                /*
540
 
                 * Column Count is a special case.      The Column number is ignored
541
 
                 * in this case.
542
 
                 */
543
 
#if (ODBCVER >= 0x0300)
544
 
                if (fDescType == SQL_DESC_COUNT)
545
 
#else
546
 
                if (fDescType == SQL_COLUMN_COUNT)
547
 
#endif /* ODBCVER */
548
 
                {
549
 
                        if (pfDesc)
550
 
                                *pfDesc = cols;
551
 
 
552
 
                        return SQL_SUCCESS;
553
 
                }
554
 
 
555
 
                if (SC_parsed_status(stmt) != STMT_PARSE_FATAL && irdflds->fi)
556
 
                {
557
 
                        if (col_idx >= cols)
558
 
                        {
559
 
                                SC_set_error(stmt, STMT_INVALID_COLUMN_NUMBER_ERROR, "Invalid column number in ColAttributes.", func);
560
 
                                return SQL_ERROR;
561
 
                        }
562
 
                }
563
 
        }
564
 
 
565
 
        if (col_idx < irdflds->nfields && irdflds->fi)
566
 
                fi = irdflds->fi[col_idx];
567
 
        if (FI_is_applicable(fi))
568
 
                field_type = getEffectiveOid(conn, fi);
569
 
        else
570
 
        {
571
 
                BOOL    build_fi = FALSE;
572
 
 
573
 
                fi = NULL;
574
 
                if (PROTOCOL_74(ci))
575
 
                {
576
 
                        switch (fDescType)
577
 
                        {
578
 
                                case SQL_COLUMN_OWNER_NAME:
579
 
                                case SQL_COLUMN_TABLE_NAME:
580
 
                                case SQL_COLUMN_TYPE:
581
 
                                case SQL_COLUMN_TYPE_NAME:
582
 
                                case SQL_COLUMN_AUTO_INCREMENT:
583
 
#if (ODBCVER >= 0x0300)
584
 
                                case SQL_DESC_NULLABLE:
585
 
                                case SQL_DESC_BASE_TABLE_NAME:
586
 
                                case SQL_DESC_BASE_COLUMN_NAME:
587
 
#else
588
 
                                case SQL_COLUMN_NULLABLE:
589
 
#endif /* ODBCVER */
590
 
                                case SQL_COLUMN_UPDATABLE:
591
 
                                case 1212: /* SQL_CA_SS_COLUMN_KEY ? */
592
 
                                        build_fi = TRUE;
593
 
                                        break;
594
 
                        }
595
 
                }
596
 
                if (!SC_pre_execute_ok(stmt, build_fi, col_idx, func))
597
 
                        return SQL_ERROR;
598
 
 
599
 
                res = SC_get_Curres(stmt);
600
 
                cols = QR_NumPublicResultCols(res);
601
 
 
602
 
                /*
603
 
                 * Column Count is a special case.      The Column number is ignored
604
 
                 * in this case.
605
 
                 */
606
 
#if (ODBCVER >= 0x0300)
607
 
                if (fDescType == SQL_DESC_COUNT)
608
 
#else
609
 
                if (fDescType == SQL_COLUMN_COUNT)
610
 
#endif /* ODBCVER */
611
 
                {
612
 
                        if (pfDesc)
613
 
                                *pfDesc = cols;
614
 
 
615
 
                        return SQL_SUCCESS;
616
 
                }
617
 
 
618
 
                if (col_idx >= cols)
619
 
                {
620
 
                        SC_set_error(stmt, STMT_INVALID_COLUMN_NUMBER_ERROR, "Invalid column number in ColAttributes.", func);
621
 
                        return SQL_ERROR;
622
 
                }
623
 
 
624
 
                field_type = QR_get_field_type(res, col_idx);
625
 
                if (col_idx < irdflds->nfields && irdflds->fi)
626
 
                        fi = irdflds->fi[col_idx];
627
 
        }
628
 
        if (FI_is_applicable(fi))
629
 
        {
630
 
                ti = fi->ti;
631
 
                field_type = getEffectiveOid(conn, fi);
632
 
        }
633
 
 
634
 
        mylog("colAttr: col %d field_type=%d fi,ti=%p,%p\n", col_idx, field_type, fi, ti);
635
 
 
636
 
        column_size = (fi != NULL && fi->column_size > 0) ? fi->column_size : pgtype_column_size(stmt, field_type, col_idx, unknown_sizes);
637
 
        switch (fDescType)
638
 
        {
639
 
                case SQL_COLUMN_AUTO_INCREMENT: /* == SQL_DESC_AUTO_UNIQUE_VALUE */
640
 
                        if (fi && fi->auto_increment)
641
 
                                value = TRUE;
642
 
                        else
643
 
                                value = pgtype_auto_increment(conn, field_type);
644
 
                        if (value == -1)        /* non-numeric becomes FALSE (ODBC Doc) */
645
 
                                value = FALSE;
646
 
                        mylog("AUTO_INCREMENT=%d\n", value);
647
 
 
648
 
                        break;
649
 
 
650
 
                case SQL_COLUMN_CASE_SENSITIVE: /* == SQL_DESC_CASE_SENSITIVE */
651
 
                        value = pgtype_case_sensitive(conn, field_type);
652
 
                        break;
653
 
 
654
 
                        /*
655
 
                         * This special case is handled above.
656
 
                         *
657
 
                         * case SQL_COLUMN_COUNT:
658
 
                         */
659
 
                case SQL_COLUMN_DISPLAY_SIZE: /* == SQL_DESC_DISPLAY_SIZE */
660
 
                        value = (fi && 0 != fi->display_size) ? fi->display_size : pgtype_display_size(stmt, field_type, col_idx, unknown_sizes);
661
 
 
662
 
                        mylog("%s: col %d, display_size= %d\n", func, col_idx, value);
663
 
 
664
 
                        break;
665
 
 
666
 
                case SQL_COLUMN_LABEL: /* == SQL_DESC_LABEL */
667
 
                        if (fi && (NAME_IS_VALID(fi->column_alias)))
668
 
                        {
669
 
                                p = GET_NAME(fi->column_alias);
670
 
 
671
 
                                mylog("%s: COLUMN_LABEL = '%s'\n", func, p);
672
 
                                break;
673
 
                        }
674
 
                        /* otherwise same as column name -- FALL THROUGH!!! */
675
 
 
676
 
#if (ODBCVER >= 0x0300)
677
 
                case SQL_DESC_NAME:
678
 
#else
679
 
                case SQL_COLUMN_NAME:
680
 
#endif /* ODBCVER */
681
 
inolog("fi=%p", fi);
682
 
if (fi)
683
 
inolog(" (%s,%s)", PRINT_NAME(fi->column_alias), PRINT_NAME(fi->column_name));
684
 
                        p = fi ? (NAME_IS_NULL(fi->column_alias) ? SAFE_NAME(fi->column_name) : GET_NAME(fi->column_alias)) : QR_get_fieldname(res, col_idx);
685
 
 
686
 
                        mylog("%s: COLUMN_NAME = '%s'\n", func, p);
687
 
                        break;
688
 
 
689
 
                case SQL_COLUMN_LENGTH:
690
 
                        value = (fi && fi->length > 0) ? fi->length : pgtype_buffer_length(stmt, field_type, col_idx, unknown_sizes);
691
 
                        if (0 > value)
692
 
                        /* if (-1 == value)  I'm not sure which is right */
693
 
                                value = 0;
694
 
 
695
 
                        mylog("%s: col %d, column_length = %d\n", func, col_idx, value);
696
 
                        break;
697
 
 
698
 
                case SQL_COLUMN_MONEY: /* == SQL_DESC_FIXED_PREC_SCALE */
699
 
                        value = pgtype_money(conn, field_type);
700
 
inolog("COLUMN_MONEY=%d\n", value);
701
 
                        break;
702
 
 
703
 
#if (ODBCVER >= 0x0300)
704
 
                case SQL_DESC_NULLABLE:
705
 
#else
706
 
                case SQL_COLUMN_NULLABLE:
707
 
#endif /* ODBCVER */
708
 
                        if (SC_has_outer_join(stmt))
709
 
                                value = TRUE;
710
 
                        else
711
 
                                value = fi ? fi->nullable : pgtype_nullable(conn, field_type);
712
 
inolog("COLUMN_NULLABLE=%d\n", value);
713
 
                        break;
714
 
 
715
 
                case SQL_COLUMN_OWNER_NAME: /* == SQL_DESC_SCHEMA_NAME */
716
 
                        p = ti ? SAFE_NAME(ti->schema_name) : NULL_STRING;
717
 
                        mylog("%s: SCHEMA_NAME = '%s'\n", func, p);
718
 
                        break;
719
 
 
720
 
                case SQL_COLUMN_PRECISION: /* in 2.x */
721
 
                        value = column_size;
722
 
                        if (value < 0)
723
 
                                value = 0;
724
 
 
725
 
                        mylog("%s: col %d, column_size = %d\n", func, col_idx, value);
726
 
                        break;
727
 
 
728
 
                case SQL_COLUMN_QUALIFIER_NAME: /* == SQL_DESC_CATALOG_NAME */
729
 
                        p = ti ? CurrCatString(conn) : NULL_STRING;     /* empty string means *not supported* */
730
 
                        break;
731
 
 
732
 
                case SQL_COLUMN_SCALE: /* in 2.x */
733
 
                        value = pgtype_decimal_digits(stmt, field_type, col_idx);
734
 
inolog("COLUMN_SCALE=%d\n", value);
735
 
                        if (value < 0)
736
 
                                value = 0;
737
 
                        break;
738
 
 
739
 
                case SQL_COLUMN_SEARCHABLE: /* == SQL_DESC_SEARCHABLE */
740
 
                        value = pgtype_searchable(conn, field_type);
741
 
                        break;
742
 
 
743
 
                case SQL_COLUMN_TABLE_NAME: /* == SQL_DESC_TABLE_NAME */
744
 
                        p = ti ? SAFE_NAME(ti->table_name) : NULL_STRING;
745
 
 
746
 
                        mylog("%s: TABLE_NAME = '%s'\n", func, p);
747
 
                        break;
748
 
 
749
 
                case SQL_COLUMN_TYPE: /* == SQL_DESC_CONCISE_TYPE */
750
 
                        value = pgtype_to_concise_type(stmt, field_type, col_idx);
751
 
                        mylog("COLUMN_TYPE=%d\n", value);
752
 
                        break;
753
 
 
754
 
                case SQL_COLUMN_TYPE_NAME: /* == SQL_DESC_TYPE_NAME */
755
 
                        p = pgtype_to_name(stmt, field_type, col_idx, fi && fi->auto_increment);
756
 
                        break;
757
 
 
758
 
                case SQL_COLUMN_UNSIGNED: /* == SQL_DESC_UNSINGED */
759
 
                        value = pgtype_unsigned(conn, field_type);
760
 
                        if (value == -1)        /* non-numeric becomes TRUE (ODBC Doc) */
761
 
                                value = SQL_TRUE;
762
 
 
763
 
                        break;
764
 
 
765
 
                case SQL_COLUMN_UPDATABLE: /* == SQL_DESC_UPDATABLE */
766
 
 
767
 
                        /*
768
 
                         * Neither Access or Borland care about this.
769
 
                         *
770
 
                         * if (field_type == PG_TYPE_OID) pfDesc = SQL_ATTR_READONLY;
771
 
                         * else
772
 
                         */
773
 
                        if (!stmt_updatable)
774
 
                                value = SQL_ATTR_READONLY;
775
 
                        else
776
 
                                value = fi ? (fi->updatable ? SQL_ATTR_WRITE : SQL_ATTR_READONLY) : (QR_get_attid(res, col_idx) > 0 ? SQL_ATTR_WRITE : (PROTOCOL_74(ci) ? SQL_ATTR_READONLY : SQL_ATTR_READWRITE_UNKNOWN));
777
 
                        if (SQL_ATTR_READONLY != value)
778
 
                        {
779
 
                                const char *name = fi ? SAFE_NAME(fi->column_name) : QR_get_fieldname(res, col_idx);
780
 
                                if (stricmp(name, OID_NAME) == 0 ||
781
 
                                    stricmp(name, "ctid") == 0 ||
782
 
                                    stricmp(name, "xmin") == 0)
783
 
                                        value = SQL_ATTR_READONLY;
784
 
                                else if (conn->ms_jet && fi && fi->auto_increment)
785
 
                                        value = SQL_ATTR_READONLY;
786
 
                        }
787
 
 
788
 
                        mylog("%s: UPDATEABLE = %d\n", func, value);
789
 
                        break;
790
 
#if (ODBCVER >= 0x0300)
791
 
                case SQL_DESC_BASE_COLUMN_NAME:
792
 
 
793
 
                        p = fi ? SAFE_NAME(fi->column_name) : QR_get_fieldname(res, col_idx);
794
 
 
795
 
                        mylog("%s: BASE_COLUMN_NAME = '%s'\n", func, p);
796
 
                        break;
797
 
                case SQL_DESC_BASE_TABLE_NAME: /* the same as TABLE_NAME ok ? */
798
 
                        p = ti ? SAFE_NAME(ti->table_name) : NULL_STRING;
799
 
 
800
 
                        mylog("%s: BASE_TABLE_NAME = '%s'\n", func, p);
801
 
                        break;
802
 
                case SQL_DESC_LENGTH: /* different from SQL_COLUMN_LENGTH */
803
 
                        // value = (fi && fi->length > 0) ? fi->length : pgtype_desclength(stmt, field_type, col_idx, unknown_sizes);
804
 
                        value = (fi && column_size > 0) ? column_size : pgtype_desclength(stmt, field_type, col_idx, unknown_sizes);
805
 
                        if (-1 == value)
806
 
                                value = 0;
807
 
 
808
 
                        mylog("%s: col %d, desc_length = %d\n", func, col_idx, value);
809
 
                        break;
810
 
                case SQL_DESC_OCTET_LENGTH:
811
 
                        value = (fi && fi->length > 0) ? fi->length : pgtype_attr_transfer_octet_length(conn, field_type, column_size, unknown_sizes);
812
 
                        if (-1 == value)
813
 
                                value = 0;
814
 
                        mylog("%s: col %d, octet_length = %d\n", func, col_idx, value);
815
 
                        break;
816
 
                case SQL_DESC_PRECISION: /* different from SQL_COLUMN_PRECISION */
817
 
                        if (value = FI_precision(fi), value <= 0)
818
 
                                value = pgtype_precision(stmt, field_type, col_idx, unknown_sizes);
819
 
                        if (value < 0)
820
 
                                value = 0;
821
 
 
822
 
                        mylog("%s: col %d, desc_precision = %d\n", func, col_idx, value);
823
 
                        break;
824
 
                case SQL_DESC_SCALE: /* different from SQL_COLUMN_SCALE */
825
 
                        value = pgtype_scale(stmt, field_type, col_idx);
826
 
                        if (value < 0)
827
 
                                value = 0;
828
 
                        break;
829
 
                case SQL_DESC_LOCAL_TYPE_NAME:
830
 
                        p = pgtype_to_name(stmt, field_type, col_idx, fi && fi->auto_increment);
831
 
                        break;
832
 
                case SQL_DESC_TYPE:
833
 
                        value = pgtype_to_sqldesctype(stmt, field_type, col_idx);
834
 
                        break;
835
 
                case SQL_DESC_NUM_PREC_RADIX:
836
 
                        value = pgtype_radix(conn, field_type);
837
 
                        break;
838
 
                case SQL_DESC_LITERAL_PREFIX:
839
 
                        p = pgtype_literal_prefix(conn, field_type);
840
 
                        break;
841
 
                case SQL_DESC_LITERAL_SUFFIX:
842
 
                        p = pgtype_literal_suffix(conn, field_type);
843
 
                        break;
844
 
                case SQL_DESC_UNNAMED:
845
 
                        value = (fi && NAME_IS_NULL(fi->column_name) && NAME_IS_NULL(fi->column_alias)) ? SQL_UNNAMED : SQL_NAMED;
846
 
                        break;
847
 
#endif /* ODBCVER */
848
 
                case 1211: /* SQL_CA_SS_COLUMN_HIDDEN ? */
849
 
                        value = 0;
850
 
                        break;
851
 
                case 1212: /* SQL_CA_SS_COLUMN_KEY ? */
852
 
                        if (fi)
853
 
                        {
854
 
                                if (fi->columnkey < 0)
855
 
                                {
856
 
                                        SC_set_SS_columnkey(stmt);
857
 
                                }
858
 
                                value = fi->columnkey;
859
 
                                mylog("%s:SS_COLUMN_KEY=%d\n", func, value);
860
 
                                break;
861
 
                        }
862
 
                        SC_set_error(stmt, STMT_OPTION_NOT_FOR_THE_DRIVER, "this request may be for MS SQL Server", func);
863
 
                        return SQL_ERROR;
864
 
                default:
865
 
                        SC_set_error(stmt, STMT_INVALID_OPTION_IDENTIFIER, "ColAttribute for this type not implemented yet", func);
866
 
                        return SQL_ERROR;
867
 
        }
868
 
 
869
 
        result = SQL_SUCCESS;
870
 
 
871
 
        if (p)
872
 
        {                                                       /* char/binary data */
873
 
                size_t len = strlen(p);
874
 
 
875
 
                if (rgbDesc)
876
 
                {
877
 
                        strncpy_null((char *) rgbDesc, p, (size_t) cbDescMax);
878
 
 
879
 
                        if (len >= cbDescMax)
880
 
                        {
881
 
                                result = SQL_SUCCESS_WITH_INFO;
882
 
                                SC_set_error(stmt, STMT_TRUNCATED, "The buffer was too small for the rgbDesc.", func);
883
 
                        }
884
 
                }
885
 
 
886
 
                if (pcbDesc)
887
 
                        *pcbDesc = (SQLSMALLINT) len;
888
 
        }
889
 
        else
890
 
        {
891
 
                /* numeric data */
892
 
                if (pfDesc)
893
 
                        *pfDesc = value;
894
 
        }
895
 
 
896
 
        return result;
897
 
}
898
 
 
899
 
 
900
 
/*      Returns result data for a single column in the current row. */
901
 
RETCODE         SQL_API
902
 
PGAPI_GetData(
903
 
                          HSTMT hstmt,
904
 
                          SQLUSMALLINT icol,
905
 
                          SQLSMALLINT fCType,
906
 
                          PTR rgbValue,
907
 
                          SQLLEN cbValueMax,
908
 
                          SQLLEN FAR * pcbValue)
909
 
{
910
 
        CSTR func = "PGAPI_GetData";
911
 
        QResultClass *res;
912
 
        StatementClass *stmt = (StatementClass *) hstmt;
913
 
        UInt2           num_cols;
914
 
        SQLLEN          num_rows;
915
 
        OID             field_type;
916
 
        int             atttypmod;
917
 
        void       *value = NULL;
918
 
        RETCODE         result = SQL_SUCCESS;
919
 
        char            get_bookmark = FALSE;
920
 
        ConnInfo   *ci;
921
 
        SQLSMALLINT     target_type;
922
 
        int             precision = -1;
923
 
 
924
 
        mylog("%s: enter, stmt=%p icol=%d\n", func, stmt, icol);
925
 
 
926
 
        if (!stmt)
927
 
        {
928
 
                SC_log_error(func, NULL_STRING, NULL);
929
 
                return SQL_INVALID_HANDLE;
930
 
        }
931
 
        ci = &(SC_get_conn(stmt)->connInfo);
932
 
        res = SC_get_Curres(stmt);
933
 
 
934
 
        if (STMT_EXECUTING == stmt->status)
935
 
        {
936
 
                SC_set_error(stmt, STMT_SEQUENCE_ERROR, "Can't get data while statement is still executing.", func);
937
 
                return SQL_ERROR;
938
 
        }
939
 
 
940
 
        if (stmt->status != STMT_FINISHED)
941
 
        {
942
 
                SC_set_error(stmt, STMT_STATUS_ERROR, "GetData can only be called after the successful execution on a SQL statement", func);
943
 
                return SQL_ERROR;
944
 
        }
945
 
 
946
 
#if (ODBCVER >= 0x0300)
947
 
        if (SQL_ARD_TYPE == fCType)
948
 
        {
949
 
                ARDFields       *opts;
950
 
                BindInfoClass   *binfo = NULL;
951
 
 
952
 
                opts = SC_get_ARDF(stmt);
953
 
                if (0 == icol)
954
 
                        binfo = opts->bookmark;
955
 
                else if (icol <= opts->allocated && opts->bindings)
956
 
                        binfo = &opts->bindings[icol - 1];
957
 
                if (binfo)
958
 
                {       
959
 
                        target_type = binfo->returntype;
960
 
                        mylog("SQL_ARD_TYPE=%d\n", target_type);
961
 
                        precision = binfo->precision;
962
 
                }
963
 
                else
964
 
                {
965
 
                        SC_set_error(stmt, STMT_STATUS_ERROR, "GetData can't determine the type via ARD", func);
966
 
                        return SQL_ERROR;
967
 
                }
968
 
        }
969
 
        else
970
 
#endif /* ODBCVER */
971
 
                target_type = fCType;
972
 
        if (icol == 0)
973
 
        {
974
 
                if (stmt->options.use_bookmarks == SQL_UB_OFF)
975
 
                {
976
 
                        SC_set_error(stmt, STMT_COLNUM_ERROR, "Attempt to retrieve bookmark with bookmark usage disabled", func);
977
 
                        return SQL_ERROR;
978
 
                }
979
 
 
980
 
                /* Make sure it is the bookmark data type */
981
 
                switch (target_type)
982
 
                {
983
 
                        case SQL_C_BOOKMARK:
984
 
#if (ODBCVER >= 0x0300)
985
 
                        case SQL_C_VARBOOKMARK:
986
 
#endif /* ODBCVER */
987
 
                                break;
988
 
                        default:
989
 
inolog("GetData Column 0 is type %d not of type SQL_C_BOOKMARK", target_type);
990
 
                                SC_set_error(stmt, STMT_PROGRAM_TYPE_OUT_OF_RANGE, "Column 0 is not of type SQL_C_BOOKMARK", func);
991
 
                                return SQL_ERROR;
992
 
                }
993
 
 
994
 
                get_bookmark = TRUE;
995
 
        }
996
 
        else
997
 
        {
998
 
                /* use zero-based column numbers */
999
 
                icol--;
1000
 
 
1001
 
                /* make sure the column number is valid */
1002
 
                num_cols = QR_NumPublicResultCols(res);
1003
 
                if (icol >= num_cols)
1004
 
                {
1005
 
                        SC_set_error(stmt, STMT_INVALID_COLUMN_NUMBER_ERROR, "Invalid column number.", func);
1006
 
                        return SQL_ERROR;
1007
 
                }
1008
 
        }
1009
 
 
1010
 
#define return  DONT_CALL_RETURN_FROM_HERE???
1011
 
        /* StartRollbackState(stmt); */
1012
 
        if (!SC_is_fetchcursor(stmt))
1013
 
        {
1014
 
                /* make sure we're positioned on a valid row */
1015
 
                num_rows = QR_get_num_total_tuples(res);
1016
 
                if ((stmt->currTuple < 0) ||
1017
 
                        (stmt->currTuple >= num_rows))
1018
 
                {
1019
 
                        SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Not positioned on a valid row for GetData.", func);
1020
 
                        result = SQL_ERROR;
1021
 
                        goto cleanup;
1022
 
                }
1023
 
                mylog("     num_rows = %d\n", num_rows);
1024
 
 
1025
 
                if (!get_bookmark)
1026
 
                {
1027
 
                        SQLLEN  curt = GIdx2CacheIdx(stmt->currTuple, stmt, res);
1028
 
                        value = QR_get_value_backend_row(res, curt, icol);
1029
 
inolog("currT=%d base=%d rowset=%d\n", stmt->currTuple, QR_get_rowstart_in_cache(res), SC_get_rowset_start(stmt)); 
1030
 
                        mylog("     value = '%s'\n", value ? value : "(null)");
1031
 
                }
1032
 
        }
1033
 
        else
1034
 
        {
1035
 
                /* it's a SOCKET result (backend data) */
1036
 
                if (stmt->currTuple == -1 || !res || !res->tupleField)
1037
 
                {
1038
 
                        SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Not positioned on a valid row for GetData.", func);
1039
 
                        result = SQL_ERROR;
1040
 
                        goto cleanup;
1041
 
                }
1042
 
 
1043
 
                if (!get_bookmark)
1044
 
                {
1045
 
                        /** value = QR_get_value_backend(res, icol); maybe thiw doesn't work */
1046
 
                        SQLLEN  curt = GIdx2CacheIdx(stmt->currTuple, stmt, res);
1047
 
                        value = QR_get_value_backend_row(res, curt, icol);
1048
 
                }
1049
 
                mylog("  socket: value = '%s'\n", value ? value : "(null)");
1050
 
        }
1051
 
 
1052
 
        if (get_bookmark)
1053
 
        {
1054
 
                BOOL    contents_get = FALSE;
1055
 
 
1056
 
                if (rgbValue)
1057
 
                {
1058
 
                        if (SQL_C_BOOKMARK == target_type || 4 <= cbValueMax)
1059
 
                        {
1060
 
                                contents_get = TRUE; 
1061
 
                                *((SQLULEN *) rgbValue) = SC_get_bookmark(stmt);
1062
 
                        }
1063
 
                }
1064
 
                if (pcbValue)
1065
 
                        *pcbValue = sizeof(SQLULEN);
1066
 
 
1067
 
                if (contents_get)
1068
 
                        result = SQL_SUCCESS;
1069
 
                else
1070
 
                {
1071
 
                        SC_set_error(stmt, STMT_TRUNCATED, "The buffer was too small for the GetData.", func);
1072
 
                        result = SQL_SUCCESS_WITH_INFO;
1073
 
                }
1074
 
                goto cleanup;
1075
 
        }
1076
 
 
1077
 
        field_type = QR_get_field_type(res, icol);
1078
 
        atttypmod = QR_get_atttypmod(res, icol);
1079
 
 
1080
 
        mylog("**** %s: icol = %d, target_type = %d, field_type = %d, value = '%s'\n", func, icol, target_type, field_type, value ? value : "(null)");
1081
 
 
1082
 
        SC_set_current_col(stmt, icol);
1083
 
 
1084
 
        result = copy_and_convert_field(stmt, field_type, atttypmod, value,
1085
 
                        target_type, precision, rgbValue, cbValueMax, pcbValue, pcbValue);
1086
 
 
1087
 
        switch (result)
1088
 
        {
1089
 
                case COPY_OK:
1090
 
                        result = SQL_SUCCESS;
1091
 
                        break;
1092
 
 
1093
 
                case COPY_UNSUPPORTED_TYPE:
1094
 
                        SC_set_error(stmt, STMT_RESTRICTED_DATA_TYPE_ERROR, "Received an unsupported type from Postgres.", func);
1095
 
                        result = SQL_ERROR;
1096
 
                        break;
1097
 
 
1098
 
                case COPY_UNSUPPORTED_CONVERSION:
1099
 
                        SC_set_error(stmt, STMT_RESTRICTED_DATA_TYPE_ERROR, "Couldn't handle the necessary data type conversion.", func);
1100
 
                        result = SQL_ERROR;
1101
 
                        break;
1102
 
 
1103
 
                case COPY_RESULT_TRUNCATED:
1104
 
                        SC_set_error(stmt, STMT_TRUNCATED, "The buffer was too small for the GetData.", func);
1105
 
                        result = SQL_SUCCESS_WITH_INFO;
1106
 
                        break;
1107
 
 
1108
 
                case COPY_GENERAL_ERROR:                /* error msg already filled in */
1109
 
                        result = SQL_ERROR;
1110
 
                        break;
1111
 
 
1112
 
                case COPY_NO_DATA_FOUND:
1113
 
                        /* SC_log_error(func, "no data found", stmt); */
1114
 
                        result = SQL_NO_DATA_FOUND;
1115
 
                        break;
1116
 
 
1117
 
                default:
1118
 
                        SC_set_error(stmt, STMT_INTERNAL_ERROR, "Unrecognized return value from copy_and_convert_field.", func);
1119
 
                        result = SQL_ERROR;
1120
 
                        break;
1121
 
        }
1122
 
 
1123
 
cleanup:
1124
 
#undef  return
1125
 
        if (stmt->internal)
1126
 
                result = DiscardStatementSvp(stmt, result, FALSE);
1127
 
inolog("%s returning %d\n", __FUNCTION__, result);
1128
 
        return result;
1129
 
}
1130
 
 
1131
 
 
1132
 
/*
1133
 
 *              Returns data for bound columns in the current row ("hstmt->iCursor"),
1134
 
 *              advances the cursor.
1135
 
 */
1136
 
RETCODE         SQL_API
1137
 
PGAPI_Fetch(
1138
 
                        HSTMT hstmt)
1139
 
{
1140
 
        CSTR func = "PGAPI_Fetch";
1141
 
        StatementClass *stmt = (StatementClass *) hstmt;
1142
 
        ARDFields       *opts;
1143
 
        QResultClass *res;
1144
 
        BindInfoClass   *bookmark;
1145
 
        RETCODE         retval = SQL_SUCCESS;
1146
 
 
1147
 
        mylog("%s: stmt = %p, stmt->result= %p\n", func, stmt, stmt ? SC_get_Curres(stmt) : NULL);
1148
 
 
1149
 
        if (!stmt)
1150
 
        {
1151
 
                SC_log_error(func, NULL_STRING, NULL);
1152
 
                return SQL_INVALID_HANDLE;
1153
 
        }
1154
 
 
1155
 
        SC_clear_error(stmt);
1156
 
 
1157
 
        if (!(res = SC_get_Curres(stmt)))
1158
 
        {
1159
 
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in PGAPI_Fetch.", func);
1160
 
                return SQL_ERROR;
1161
 
        }
1162
 
 
1163
 
        /* Not allowed to bind a bookmark column when using SQLFetch. */
1164
 
        opts = SC_get_ARDF(stmt);
1165
 
        if ((bookmark = opts->bookmark) && bookmark->buffer)
1166
 
        {
1167
 
                SC_set_error(stmt, STMT_COLNUM_ERROR, "Not allowed to bind a bookmark column when using PGAPI_Fetch", func);
1168
 
                return SQL_ERROR;
1169
 
        }
1170
 
 
1171
 
        if (stmt->status == STMT_EXECUTING)
1172
 
        {
1173
 
                SC_set_error(stmt, STMT_SEQUENCE_ERROR, "Can't fetch while statement is still executing.", func);
1174
 
                return SQL_ERROR;
1175
 
        }
1176
 
 
1177
 
        if (stmt->status != STMT_FINISHED)
1178
 
        {
1179
 
                SC_set_error(stmt, STMT_SEQUENCE_ERROR, "Fetch can only be called after the successful execution on a SQL statement", func);
1180
 
                return SQL_ERROR;
1181
 
        }
1182
 
 
1183
 
        if (opts->bindings == NULL)
1184
 
        {
1185
 
                if (!SC_may_fetch_rows(stmt))
1186
 
                        return SQL_NO_DATA_FOUND;
1187
 
                /* just to avoid a crash if the user insists on calling this */
1188
 
                /* function even if SQL_ExecDirect has reported an Error */
1189
 
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Bindings were not allocated properly.", func);
1190
 
                return SQL_ERROR;
1191
 
        }
1192
 
 
1193
 
#define return  DONT_CALL_RETURN_FROM_HERE???
1194
 
        /* StartRollbackState(stmt); */
1195
 
        if (stmt->rowset_start < 0)
1196
 
                SC_set_rowset_start(stmt, 0, TRUE);
1197
 
        QR_set_rowset_size(res, 1);
1198
 
        /* QR_inc_rowstart_in_cache(res, stmt->last_fetch_count_include_ommitted); */
1199
 
        SC_inc_rowset_start(stmt, stmt->last_fetch_count_include_ommitted);
1200
 
 
1201
 
        retval = SC_fetch(stmt);
1202
 
#undef  return
1203
 
        if (stmt->internal)
1204
 
                retval = DiscardStatementSvp(stmt, retval, FALSE);
1205
 
        return retval;
1206
 
}
1207
 
 
1208
 
static RETCODE SQL_API
1209
 
SC_pos_reload_needed(StatementClass *stmt, SQLULEN req_size, UDWORD flag);
1210
 
SQLLEN
1211
 
getNthValid(const QResultClass *res, SQLLEN sta, UWORD orientation, SQLULEN nth, SQLLEN *nearest)
1212
 
{
1213
 
        SQLLEN  i, num_tuples = QR_get_num_total_tuples(res), nearp;
1214
 
        SQLULEN count;
1215
 
        KeySet  *keyset;
1216
 
 
1217
 
        if (!QR_once_reached_eof(res))
1218
 
                num_tuples = INT_MAX;
1219
 
        /* Note that the parameter nth is 1-based */
1220
 
inolog("get %dth Valid data from %d to %s [dlt=%d]", nth, sta, orientation == SQL_FETCH_PRIOR ? "backward" : "forward", res->dl_count);
1221
 
        if (0 == res->dl_count)
1222
 
        {
1223
 
                if (SQL_FETCH_PRIOR == orientation)
1224
 
                {       
1225
 
                        if (sta + 1 >= (SQLLEN) nth)
1226
 
                        {
1227
 
                                *nearest = sta + 1 - nth;
1228
 
                                return nth;
1229
 
                        }
1230
 
                        *nearest = -1;
1231
 
                        return -(SQLLEN)(sta + 1);
1232
 
                }
1233
 
                else
1234
 
                {       
1235
 
                        nearp = sta - 1 + nth;
1236
 
                        if (nearp < num_tuples)
1237
 
                        {
1238
 
                                *nearest = nearp;
1239
 
                                return nth;
1240
 
                        }
1241
 
                        *nearest = num_tuples;
1242
 
                        return -(SQLLEN)(num_tuples - sta);
1243
 
                }
1244
 
        }
1245
 
        count = 0;
1246
 
        if (QR_get_cursor(res))
1247
 
        {
1248
 
                SQLLEN  *deleted = res->deleted;
1249
 
 
1250
 
                *nearest = sta - 1 + nth;
1251
 
                if (SQL_FETCH_PRIOR == orientation)
1252
 
                {
1253
 
                        for (i = res->dl_count - 1; i >=0 && *nearest <= (SQLLEN) deleted[i]; i--)
1254
 
                        {
1255
 
inolog("deleted[%d]=%d\n", i, deleted[i]);
1256
 
                                if (sta >= (SQLLEN)deleted[i])
1257
 
                                        (*nearest)--;
1258
 
                        }
1259
 
inolog("nearest=%d\n", *nearest);
1260
 
                        if (*nearest < 0)
1261
 
                        {
1262
 
                                *nearest = -1;
1263
 
                                count = sta + 1;
1264
 
                        }
1265
 
                        else
1266
 
                                return nth;
1267
 
                }
1268
 
                else
1269
 
                {
1270
 
                        if (!QR_once_reached_eof(res))
1271
 
                                num_tuples = INT_MAX;
1272
 
                        for (i = 0; i < res->dl_count && *nearest >= (SQLLEN)deleted[i]; i++)
1273
 
                        {
1274
 
                                if (sta <= (SQLLEN)deleted[i])
1275
 
                                        (*nearest)++;
1276
 
                        }
1277
 
                        if (*nearest >= num_tuples)
1278
 
                        {
1279
 
                                *nearest = num_tuples;
1280
 
                                count = *nearest - sta;
1281
 
                        }
1282
 
                        else
1283
 
                                return nth;
1284
 
                }
1285
 
        }
1286
 
        else if (SQL_FETCH_PRIOR == orientation)
1287
 
        {
1288
 
                for (i = sta, keyset = res->keyset + sta;
1289
 
                        i >= 0; i--, keyset--)
1290
 
                {
1291
 
                        if (0 == (keyset->status & (CURS_SELF_DELETING | CURS_SELF_DELETED | CURS_OTHER_DELETED)))
1292
 
                        {
1293
 
                                *nearest = i;
1294
 
inolog(" nearest=%d\n", *nearest);
1295
 
                                if (++count == nth)
1296
 
                                        return count;
1297
 
                        }
1298
 
                }
1299
 
                *nearest = -1; 
1300
 
        }
1301
 
        else
1302
 
        {
1303
 
                for (i = sta, keyset = res->keyset + sta;
1304
 
                        i < num_tuples; i++, keyset++)
1305
 
                {
1306
 
                        if (0 == (keyset->status & (CURS_SELF_DELETING | CURS_SELF_DELETED | CURS_OTHER_DELETED)))
1307
 
                        {
1308
 
                                *nearest = i;
1309
 
inolog(" nearest=%d\n", *nearest);
1310
 
                                if (++count == nth)
1311
 
                                        return count;
1312
 
                        }
1313
 
                }
1314
 
                *nearest = num_tuples; 
1315
 
        }
1316
 
inolog(" nearest not found\n");
1317
 
        return -(SQLLEN)count;
1318
 
}
1319
 
 
1320
 
static void
1321
 
move_cursor_position_if_needed(StatementClass *self, QResultClass *res)
1322
 
{
1323
 
        SQLLEN  move_offset;
1324
 
        
1325
 
        /*
1326
 
         * The move direction must be initialized to is_not_moving or
1327
 
         * is_moving_from_the_last in advance.
1328
 
         */
1329
 
        if (!QR_get_cursor(res))
1330
 
        {
1331
 
                QR_stop_movement(res); /* for safety */
1332
 
                res->move_offset = 0;
1333
 
                return;
1334
 
        }
1335
 
inolog("BASE=%d numb=%d curr=%d cursT=%d\n", QR_get_rowstart_in_cache(res), res->num_cached_rows, self->currTuple, res->cursTuple);
1336
 
 
1337
 
        /* retrieve "move from the last" case first */
1338
 
        if (QR_is_moving_from_the_last(res))
1339
 
        {
1340
 
                mylog("must MOVE from the last\n");
1341
 
                if (QR_once_reached_eof(res) || self->rowset_start <= QR_get_num_total_tuples(res)) /* this shouldn't happen */
1342
 
                        mylog("strange situation in move from the last\n");
1343
 
                if (0 == res->move_offset)
1344
 
                        res->move_offset = INT_MAX - self->rowset_start;
1345
 
else
1346
 
{
1347
 
inolog("!!move_offset=%d calc=%d\n", res->move_offset, INT_MAX - self->rowset_start);
1348
 
}
1349
 
                return;
1350
 
        }
1351
 
 
1352
 
        /* normal case */
1353
 
        res->move_offset = 0;
1354
 
        move_offset = self->currTuple - res->cursTuple;
1355
 
        if (QR_get_rowstart_in_cache(res) >= 0 &&
1356
 
             QR_get_rowstart_in_cache(res) <= res->num_cached_rows)
1357
 
        {
1358
 
                QR_set_next_in_cache(res, (QR_get_rowstart_in_cache(res) < 0) ? 0 : QR_get_rowstart_in_cache(res));
1359
 
                return;
1360
 
        }
1361
 
        if (0 == move_offset) 
1362
 
                return;
1363
 
        if (move_offset > 0)
1364
 
        {
1365
 
                QR_set_move_forward(res);
1366
 
                res->move_offset = move_offset;
1367
 
        }
1368
 
        else
1369
 
        {
1370
 
                QR_set_move_backward(res);
1371
 
                res->move_offset = -move_offset;
1372
 
        }
1373
 
}
1374
 
/*
1375
 
 *      return NO_DATA_FOUND macros
1376
 
 *        save_rowset_start or num_tuples must be defined 
1377
 
 */
1378
 
#define EXTFETCH_RETURN_BOF(stmt, res) \
1379
 
{ \
1380
 
inolog("RETURN_BOF\n"); \
1381
 
        SC_set_rowset_start(stmt, -1, TRUE); \
1382
 
        stmt->currTuple = -1; \
1383
 
        /* move_cursor_position_if_needed(stmt, res); */ \
1384
 
        return SQL_NO_DATA_FOUND; \
1385
 
}
1386
 
#define EXTFETCH_RETURN_EOF(stmt, res) \
1387
 
{ \
1388
 
inolog("RETURN_EOF\n"); \
1389
 
        SC_set_rowset_start(stmt, num_tuples, TRUE); \
1390
 
        stmt->currTuple = -1; \
1391
 
        /* move_cursor_position_if_needed(stmt, res); */ \
1392
 
        return SQL_NO_DATA_FOUND; \
1393
 
}
1394
 
        
1395
 
/*      This fetchs a block of data (rowset). */
1396
 
RETCODE         SQL_API
1397
 
PGAPI_ExtendedFetch(
1398
 
                                        HSTMT hstmt,
1399
 
                                        SQLUSMALLINT fFetchType,
1400
 
                                        SQLLEN irow,
1401
 
                                        SQLULEN FAR * pcrow,
1402
 
                                        SQLUSMALLINT FAR * rgfRowStatus,
1403
 
                                        SQLLEN bookmark_offset,
1404
 
                                        SQLLEN rowsetSize)
1405
 
{
1406
 
        CSTR func = "PGAPI_ExtendedFetch";
1407
 
        StatementClass *stmt = (StatementClass *) hstmt;
1408
 
        ARDFields       *opts;
1409
 
        QResultClass *res;
1410
 
        BindInfoClass   *bookmark;
1411
 
        SQLLEN          num_tuples, i, fc_io;
1412
 
        SQLLEN          save_rowset_size, progress_size;
1413
 
        SQLLEN          save_rowset_start,
1414
 
                        rowset_start;
1415
 
        RETCODE         result = SQL_SUCCESS;
1416
 
        char            truncated, error, should_set_rowset_start = FALSE; 
1417
 
        ConnInfo   *ci;
1418
 
        SQLLEN          currp;
1419
 
        UWORD           pstatus;
1420
 
        BOOL            currp_is_valid, reached_eof, useCursor;
1421
 
 
1422
 
        mylog("%s: stmt=%p rowsetSize=%d\n", func, stmt, rowsetSize);
1423
 
 
1424
 
        if (!stmt)
1425
 
        {
1426
 
                SC_log_error(func, NULL_STRING, NULL);
1427
 
                return SQL_INVALID_HANDLE;
1428
 
        }
1429
 
        ci = &(SC_get_conn(stmt)->connInfo);
1430
 
 
1431
 
        /* if (SC_is_fetchcursor(stmt) && !stmt->manual_result) */
1432
 
        if (SQL_CURSOR_FORWARD_ONLY == stmt->options.cursor_type)
1433
 
        {
1434
 
                if (fFetchType != SQL_FETCH_NEXT)
1435
 
                {
1436
 
                        SC_set_error(stmt, STMT_FETCH_OUT_OF_RANGE, "The fetch type for PGAPI_ExtendedFetch isn't allowed with ForwardOnly cursor.", func);
1437
 
                        return SQL_ERROR;
1438
 
                }
1439
 
        }
1440
 
 
1441
 
        SC_clear_error(stmt);
1442
 
 
1443
 
        if (!(res = SC_get_Curres(stmt)))
1444
 
        {
1445
 
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in PGAPI_ExtendedFetch.", func);
1446
 
                return SQL_ERROR;
1447
 
        }
1448
 
 
1449
 
        opts = SC_get_ARDF(stmt);
1450
 
        /*
1451
 
         * If a bookmark colunmn is bound but bookmark usage is off, then
1452
 
         * error
1453
 
         */
1454
 
        if ((bookmark = opts->bookmark) && bookmark->buffer && stmt->options.use_bookmarks == SQL_UB_OFF)
1455
 
        {
1456
 
                SC_set_error(stmt, STMT_COLNUM_ERROR, "Attempt to retrieve bookmark with bookmark usage disabled", func);
1457
 
                return SQL_ERROR;
1458
 
        }
1459
 
 
1460
 
        if (stmt->status == STMT_EXECUTING)
1461
 
        {
1462
 
                SC_set_error(stmt, STMT_SEQUENCE_ERROR, "Can't fetch while statement is still executing.", func);
1463
 
                return SQL_ERROR;
1464
 
        }
1465
 
 
1466
 
        if (stmt->status != STMT_FINISHED)
1467
 
        {
1468
 
                SC_set_error(stmt, STMT_STATUS_ERROR, "ExtendedFetch can only be called after the successful execution on a SQL statement", func);
1469
 
                return SQL_ERROR;
1470
 
        }
1471
 
 
1472
 
        if (opts->bindings == NULL)
1473
 
        {
1474
 
                if (!SC_may_fetch_rows(stmt))
1475
 
                        return SQL_NO_DATA_FOUND;
1476
 
                /* just to avoid a crash if the user insists on calling this */
1477
 
                /* function even if SQL_ExecDirect has reported an Error */
1478
 
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Bindings were not allocated properly.", func);
1479
 
                return SQL_ERROR;
1480
 
        }
1481
 
 
1482
 
        /* Initialize to no rows fetched */
1483
 
        if (rgfRowStatus)
1484
 
                for (i = 0; i < rowsetSize; i++)
1485
 
                        *(rgfRowStatus + i) = SQL_ROW_NOROW;
1486
 
 
1487
 
        if (pcrow)
1488
 
                *pcrow = 0;
1489
 
 
1490
 
        useCursor = (SC_is_fetchcursor(stmt) && NULL != QR_get_cursor(res));
1491
 
        num_tuples = QR_get_num_total_tuples(res);
1492
 
        reached_eof = QR_once_reached_eof(res) && QR_get_cursor(res);
1493
 
        if (useCursor && !reached_eof)
1494
 
                num_tuples = INT_MAX;
1495
 
 
1496
 
inolog("num_tuples=%d\n", num_tuples);
1497
 
        /* Save and discard the saved rowset size */
1498
 
        save_rowset_start = SC_get_rowset_start(stmt);
1499
 
        save_rowset_size = stmt->save_rowset_size;
1500
 
        stmt->save_rowset_size = -1;
1501
 
        rowset_start = SC_get_rowset_start(stmt);
1502
 
 
1503
 
        QR_stop_movement(res);
1504
 
        res->move_offset = 0;
1505
 
        switch (fFetchType)
1506
 
        {
1507
 
                case SQL_FETCH_NEXT:
1508
 
 
1509
 
                        /*
1510
 
                         * From the odbc spec... If positioned before the start of the
1511
 
                         * RESULT SET, then this should be equivalent to
1512
 
                         * SQL_FETCH_FIRST.
1513
 
                         */
1514
 
 
1515
 
                        progress_size = (save_rowset_size > 0 ? save_rowset_size : rowsetSize);
1516
 
                        if (rowset_start < 0)
1517
 
                                SC_set_rowset_start(stmt, 0, TRUE);
1518
 
                        else if (res->keyset)
1519
 
                        {
1520
 
                                if (stmt->last_fetch_count <= progress_size)
1521
 
                                {
1522
 
                                        SC_inc_rowset_start(stmt, stmt->last_fetch_count_include_ommitted);
1523
 
                                        progress_size -= stmt->last_fetch_count;
1524
 
                                }
1525
 
                                if (progress_size > 0)
1526
 
                                {
1527
 
                                        if (getNthValid(res, SC_get_rowset_start(stmt),
1528
 
                                                SQL_FETCH_NEXT, progress_size + 1,
1529
 
                                                &rowset_start) <= 0)
1530
 
                                        {
1531
 
                                                EXTFETCH_RETURN_EOF(stmt, res)
1532
 
                                        }
1533
 
                                        else
1534
 
                                                should_set_rowset_start =TRUE;
1535
 
                                }
1536
 
                        }
1537
 
                        else
1538
 
                                SC_inc_rowset_start(stmt, progress_size);
1539
 
                        mylog("SQL_FETCH_NEXT: num_tuples=%d, currtuple=%d, rowst=%d\n", num_tuples, stmt->currTuple, rowset_start);
1540
 
                        break;
1541
 
 
1542
 
                case SQL_FETCH_PRIOR:
1543
 
                        mylog("SQL_FETCH_PRIOR: num_tuples=%d, currtuple=%d\n", num_tuples, stmt->currTuple);
1544
 
 
1545
 
                        /*
1546
 
                         * From the odbc spec... If positioned after the end of the
1547
 
                         * RESULT SET, then this should be equivalent to
1548
 
                         * SQL_FETCH_LAST.
1549
 
                         */
1550
 
                        if (SC_get_rowset_start(stmt) <= 0)
1551
 
                        {
1552
 
                                EXTFETCH_RETURN_BOF(stmt, res)
1553
 
                        }
1554
 
                        if (SC_get_rowset_start(stmt) >= num_tuples)
1555
 
                        {
1556
 
                                if (rowsetSize > num_tuples)
1557
 
                                {
1558
 
                                        SC_set_error(stmt, STMT_POS_BEFORE_RECORDSET, "fetch prior from eof and before the beginning", func);
1559
 
                                }
1560
 
                                SC_set_rowset_start(stmt, num_tuples <= 0 ? 0 : (num_tuples - rowsetSize), TRUE);
1561
 
                        }
1562
 
                        else if (QR_haskeyset(res))
1563
 
                        {
1564
 
                                if (i = getNthValid(res, SC_get_rowset_start(stmt) - 1, SQL_FETCH_PRIOR, rowsetSize, &rowset_start), i < -1)
1565
 
                                {
1566
 
                                        SC_set_error(stmt, STMT_POS_BEFORE_RECORDSET, "fetch prior and before the beggining", func);
1567
 
                                        SC_set_rowset_start(stmt, 0, TRUE);
1568
 
                                }
1569
 
                                else if (i <= 0)
1570
 
                                {
1571
 
                                        EXTFETCH_RETURN_BOF(stmt, res)
1572
 
                                }
1573
 
                                else
1574
 
                                        should_set_rowset_start = TRUE;
1575
 
                        }
1576
 
                        else if (SC_get_rowset_start(stmt) < rowsetSize)
1577
 
                        {
1578
 
                                SC_set_error(stmt, STMT_POS_BEFORE_RECORDSET, "fetch prior from eof and before the beggining", func);
1579
 
                                SC_set_rowset_start(stmt, 0, TRUE);
1580
 
                        }
1581
 
                        else
1582
 
                                SC_inc_rowset_start(stmt, -rowsetSize);
1583
 
                        break;
1584
 
 
1585
 
                case SQL_FETCH_FIRST:
1586
 
                        mylog("SQL_FETCH_FIRST: num_tuples=%d, currtuple=%d\n", num_tuples, stmt->currTuple);
1587
 
 
1588
 
                        SC_set_rowset_start(stmt, 0, TRUE);
1589
 
                        break;
1590
 
 
1591
 
                case SQL_FETCH_LAST:
1592
 
                        mylog("SQL_FETCH_LAST: num_tuples=%d, currtuple=%d\n", num_tuples, stmt->currTuple);
1593
 
 
1594
 
                        if (!reached_eof)
1595
 
                        {
1596
 
                                QR_set_move_from_the_last(res);
1597
 
                                res->move_offset = rowsetSize;
1598
 
                        }
1599
 
                        SC_set_rowset_start(stmt, num_tuples <= 0 ? 0 : (num_tuples - rowsetSize), TRUE);
1600
 
                        break;
1601
 
 
1602
 
                case SQL_FETCH_ABSOLUTE:
1603
 
                        mylog("SQL_FETCH_ABSOLUTE: num_tuples=%d, currtuple=%d, irow=%d\n", num_tuples, stmt->currTuple, irow);
1604
 
 
1605
 
                        /* Position before result set, but dont fetch anything */
1606
 
                        if (irow == 0)
1607
 
                        {
1608
 
                                EXTFETCH_RETURN_BOF(stmt, res)
1609
 
                        }
1610
 
                        /* Position before the desired row */
1611
 
                        else if (irow > 0)
1612
 
                        {
1613
 
                                if (getNthValid(res, 0, SQL_FETCH_NEXT, irow, &rowset_start) <= 0)
1614
 
                                {
1615
 
                                        EXTFETCH_RETURN_EOF(stmt, res)
1616
 
                                }
1617
 
                                else
1618
 
                                        should_set_rowset_start = TRUE;
1619
 
                        }
1620
 
                        /* Position with respect to the end of the result set */
1621
 
                        else
1622
 
                        {
1623
 
                                if (getNthValid(res, num_tuples - 1, SQL_FETCH_PRIOR, -irow, &rowset_start) <= 0)
1624
 
                                {
1625
 
                                        EXTFETCH_RETURN_BOF(stmt, res)
1626
 
                                }
1627
 
                                else
1628
 
                                {
1629
 
                                        if (!reached_eof)
1630
 
                                        {
1631
 
                                                QR_set_move_from_the_last(res);
1632
 
                                                res->move_offset = -irow;
1633
 
                                        }
1634
 
                                        should_set_rowset_start = TRUE;
1635
 
                                }
1636
 
                        }
1637
 
                        break;
1638
 
 
1639
 
                case SQL_FETCH_RELATIVE:
1640
 
 
1641
 
                        /*
1642
 
                         * Refresh the current rowset -- not currently implemented,
1643
 
                         * but lie anyway
1644
 
                         */
1645
 
                        if (irow == 0)
1646
 
                                break;
1647
 
 
1648
 
                        if (irow > 0)
1649
 
                        {
1650
 
                                if (getNthValid(res, SC_get_rowset_start(stmt) + 1, SQL_FETCH_NEXT, irow, &rowset_start) <= 0)
1651
 
                                {
1652
 
                                        EXTFETCH_RETURN_EOF(stmt, res)
1653
 
                                }
1654
 
                                else
1655
 
                                        should_set_rowset_start = TRUE;
1656
 
                        }
1657
 
                        else
1658
 
                        {
1659
 
                                if (getNthValid(res, SC_get_rowset_start(stmt) - 1, SQL_FETCH_PRIOR, -irow, &rowset_start) <= 0)
1660
 
                                {
1661
 
                                        EXTFETCH_RETURN_BOF(stmt, res)
1662
 
                                }
1663
 
                                else
1664
 
                                        should_set_rowset_start = TRUE;
1665
 
                        }
1666
 
                        break;
1667
 
 
1668
 
                case SQL_FETCH_BOOKMARK:
1669
 
                        {
1670
 
                        SQLLEN  bidx = SC_resolve_bookmark(irow);
1671
 
 
1672
 
                        if (bidx < 0)
1673
 
                        {
1674
 
                                if (!reached_eof)
1675
 
                                {
1676
 
                                        QR_set_move_from_the_last(res);
1677
 
                                        res->move_offset = 1 + res->ad_count + bidx;
1678
 
                                }
1679
 
                                bidx = num_tuples - 1 - res->ad_count - bidx;
1680
 
                        } 
1681
 
 
1682
 
                        rowset_start = bidx;
1683
 
                        if (bookmark_offset >= 0)
1684
 
                        {
1685
 
                                if (getNthValid(res, bidx, SQL_FETCH_NEXT, bookmark_offset + 1, &rowset_start) <= 0)
1686
 
                                {
1687
 
                                        EXTFETCH_RETURN_EOF(stmt, res)
1688
 
                                }
1689
 
                                else
1690
 
                                        should_set_rowset_start = TRUE;
1691
 
                        }
1692
 
                        else if (getNthValid(res, bidx, SQL_FETCH_PRIOR, 1 - bookmark_offset, &rowset_start) <= 0)
1693
 
                        {
1694
 
                                stmt->currTuple = -1;
1695
 
                                EXTFETCH_RETURN_BOF(stmt, res)
1696
 
                        }
1697
 
                        else
1698
 
                                should_set_rowset_start = TRUE;
1699
 
                        }
1700
 
                        break;
1701
 
 
1702
 
                default:
1703
 
                        SC_set_error(stmt, STMT_FETCH_OUT_OF_RANGE, "Unsupported PGAPI_ExtendedFetch Direction", func);
1704
 
                        return SQL_ERROR;
1705
 
        }
1706
 
 
1707
 
        /*
1708
 
         * CHECK FOR PROPER CURSOR STATE
1709
 
         */
1710
 
 
1711
 
        /*
1712
 
         * Handle Declare Fetch style specially because the end is not really
1713
 
         * the end...
1714
 
         */
1715
 
        if (!should_set_rowset_start)
1716
 
                rowset_start = SC_get_rowset_start(stmt);
1717
 
        if (useCursor)
1718
 
        {
1719
 
                if (reached_eof &&
1720
 
                    rowset_start >= num_tuples)
1721
 
                {
1722
 
                        EXTFETCH_RETURN_EOF(stmt, res)
1723
 
                }
1724
 
        }
1725
 
        else
1726
 
        {
1727
 
                /* If *new* rowset is after the result_set, return no data found */
1728
 
                if (rowset_start >= num_tuples)
1729
 
                {
1730
 
                        EXTFETCH_RETURN_EOF(stmt, res)
1731
 
                }
1732
 
        }
1733
 
        /* If *new* rowset is prior to result_set, return no data found */
1734
 
        if (rowset_start < 0)
1735
 
        {
1736
 
                if (rowset_start + rowsetSize <= 0)
1737
 
                {
1738
 
                        EXTFETCH_RETURN_BOF(stmt, res)
1739
 
                }
1740
 
                else
1741
 
                {       /* overlap with beginning of result set,
1742
 
                         * so get first rowset */
1743
 
                        SC_set_rowset_start(stmt, 0, TRUE);
1744
 
                }
1745
 
                should_set_rowset_start = FALSE;
1746
 
        }
1747
 
 
1748
 
#define return DONT_CALL_RETURN_FROM_HERE???
1749
 
        /* increment the base row in the tuple cache */
1750
 
        QR_set_rowset_size(res, (Int4) rowsetSize);
1751
 
        /* set the rowset_start if needed */
1752
 
        if (should_set_rowset_start)
1753
 
                SC_set_rowset_start(stmt, rowset_start, TRUE);
1754
 
        /* currTuple is always 1 row prior to the rowset start */
1755
 
        stmt->currTuple = RowIdx2GIdx(-1, stmt);
1756
 
 
1757
 
        if (SC_is_fetchcursor(stmt) ||
1758
 
            SQL_CURSOR_KEYSET_DRIVEN == stmt->options.cursor_type)
1759
 
        {
1760
 
                move_cursor_position_if_needed(stmt, res);
1761
 
        }
1762
 
        else
1763
 
                QR_set_rowstart_in_cache(res, SC_get_rowset_start(stmt));
1764
 
 
1765
 
        if (res->keyset && !QR_get_cursor(res))
1766
 
        {
1767
 
                UDWORD  flag = 0;
1768
 
                SQLLEN  rowset_end, req_size;
1769
 
 
1770
 
                getNthValid(res, rowset_start, SQL_FETCH_NEXT, rowsetSize, &rowset_end);
1771
 
                req_size = rowset_end - rowset_start + 1;
1772
 
                if (SQL_CURSOR_KEYSET_DRIVEN == stmt->options.cursor_type)
1773
 
                {
1774
 
                        if (fFetchType != SQL_FETCH_NEXT ||
1775
 
                                QR_get_rowstart_in_cache(res) + req_size > QR_get_num_cached_tuples(res))
1776
 
                                flag = 1;
1777
 
                }
1778
 
                if (SQL_RD_ON == stmt->options.retrieve_data ||
1779
 
                    flag != 0)
1780
 
                {
1781
 
                        SC_pos_reload_needed(stmt, req_size, flag);
1782
 
                }
1783
 
        }
1784
 
        /* Physical Row advancement occurs for each row fetched below */
1785
 
 
1786
 
        mylog("PGAPI_ExtendedFetch: new currTuple = %d\n", stmt->currTuple);
1787
 
 
1788
 
        truncated = error = FALSE;
1789
 
 
1790
 
        currp = -1;
1791
 
        stmt->bind_row = 0;             /* set the binding location */
1792
 
        result = SC_fetch(stmt);
1793
 
        if (SQL_ERROR == result)
1794
 
                goto cleanup;
1795
 
        if (SQL_NO_DATA_FOUND != result && res->keyset)
1796
 
        {
1797
 
                currp = GIdx2KResIdx(SC_get_rowset_start(stmt), stmt, res);
1798
 
inolog("currp=%d\n", currp);
1799
 
                if (currp < 0)
1800
 
                {
1801
 
                        result = SQL_ERROR;
1802
 
                        mylog("rowset_start=%d but currp=%d\n", SC_get_rowset_start(stmt), currp);
1803
 
                        SC_set_error(stmt, STMT_INTERNAL_ERROR, "rowset_start not in the keyset", func);
1804
 
                        goto cleanup;
1805
 
                }
1806
 
        }
1807
 
        for (i = 0, fc_io = 0; SQL_NO_DATA_FOUND != result && SQL_ERROR != result; currp++)
1808
 
        {
1809
 
                fc_io++;
1810
 
                currp_is_valid = FALSE;
1811
 
                if (res->keyset)
1812
 
                {
1813
 
                        if (currp < res->num_cached_keys)
1814
 
                        {
1815
 
                                currp_is_valid = TRUE;
1816
 
                                res->keyset[currp].status &= ~CURS_IN_ROWSET; /* Off the flag first */
1817
 
                        }
1818
 
                        else
1819
 
                        {
1820
 
                                mylog("Umm current row is out of keyset\n");
1821
 
                                break;
1822
 
                        }
1823
 
                }
1824
 
inolog("ExtFetch result=%d\n", result);
1825
 
                if (currp_is_valid && SQL_SUCCESS_WITH_INFO == result && 0 == stmt->last_fetch_count)
1826
 
                {
1827
 
inolog("just skipping deleted row %d\n", currp);
1828
 
                        QR_set_rowset_size(res, (Int4) (rowsetSize - i + fc_io));
1829
 
                        result = SC_fetch(stmt);
1830
 
                        if (SQL_ERROR == result)
1831
 
                                break;
1832
 
                        continue;
1833
 
                }
1834
 
 
1835
 
                /* Determine Function status */
1836
 
                if (result == SQL_SUCCESS_WITH_INFO)
1837
 
                        truncated = TRUE;
1838
 
                else if (result == SQL_ERROR)
1839
 
                        error = TRUE;
1840
 
 
1841
 
                /* Determine Row Status */
1842
 
                if (rgfRowStatus)
1843
 
                {
1844
 
                        if (result == SQL_ERROR)
1845
 
                                *(rgfRowStatus + i) = SQL_ROW_ERROR;
1846
 
                        else if (currp_is_valid)
1847
 
                        {
1848
 
                                pstatus = (res->keyset[currp].status & KEYSET_INFO_PUBLIC);
1849
 
                                if (pstatus != 0 && pstatus != SQL_ROW_ADDED)
1850
 
                                {
1851
 
                                        rgfRowStatus[i] = pstatus;
1852
 
                                }
1853
 
                                else
1854
 
                                        rgfRowStatus[i] = SQL_ROW_SUCCESS;
1855
 
                                /* refresh the status */
1856
 
                                /* if (SQL_ROW_DELETED != pstatus) */
1857
 
                                res->keyset[currp].status &= (~KEYSET_INFO_PUBLIC);
1858
 
                        }
1859
 
                        else
1860
 
                                *(rgfRowStatus + i) = SQL_ROW_SUCCESS;
1861
 
                }
1862
 
                if (SQL_ERROR != result && currp_is_valid)
1863
 
                        res->keyset[currp].status |= CURS_IN_ROWSET; /* This is the unique place where the CURS_IN_ROWSET bit is turned on */
1864
 
                i++;
1865
 
                if (i >= rowsetSize)
1866
 
                        break;
1867
 
                stmt->bind_row = (SQLSETPOSIROW) i; /* set the binding location */
1868
 
                result = SC_fetch(stmt);
1869
 
        }
1870
 
        if (SQL_ERROR == result)
1871
 
                goto cleanup;
1872
 
 
1873
 
        /* Save the fetch count for SQLSetPos */
1874
 
        stmt->last_fetch_count = i;
1875
 
        stmt->save_rowset_size = rowsetSize;
1876
 
        /*
1877
 
        currp = KResIdx2GIdx(currp, stmt, res);
1878
 
        stmt->last_fetch_count_include_ommitted = GIdx2RowIdx(currp, stmt);
1879
 
        */
1880
 
        stmt->last_fetch_count_include_ommitted = fc_io;
1881
 
 
1882
 
        /* Reset next binding row */
1883
 
        stmt->bind_row = 0;
1884
 
 
1885
 
        /* Move the cursor position to the first row in the result set. */
1886
 
        stmt->currTuple = RowIdx2GIdx(0, stmt);
1887
 
 
1888
 
        /* For declare/fetch, need to reset cursor to beginning of rowset */
1889
 
        if (useCursor)
1890
 
                QR_set_position(res, 0);
1891
 
 
1892
 
        /* Set the number of rows retrieved */
1893
 
        if (pcrow)
1894
 
                *pcrow = i;
1895
 
inolog("pcrow=%d\n", i);
1896
 
 
1897
 
        if (i == 0)
1898
 
                /* Only DeclareFetch should wind up here */
1899
 
                result = SQL_NO_DATA_FOUND;
1900
 
        else if (error)
1901
 
                result = SQL_ERROR;
1902
 
        else if (truncated)
1903
 
                result = SQL_SUCCESS_WITH_INFO;
1904
 
        else if (SC_get_errornumber(stmt) == STMT_POS_BEFORE_RECORDSET)
1905
 
                result = SQL_SUCCESS_WITH_INFO;
1906
 
        else
1907
 
                result = SQL_SUCCESS;
1908
 
 
1909
 
cleanup:
1910
 
#undef  return
1911
 
        if (stmt->internal)
1912
 
                result = DiscardStatementSvp(stmt, result, FALSE);
1913
 
        return result;
1914
 
}
1915
 
 
1916
 
 
1917
 
/*
1918
 
 *              This determines whether there are more results sets available for
1919
 
 *              the "hstmt".
1920
 
 */
1921
 
/* CC: return SQL_NO_DATA_FOUND since we do not support multiple result sets */
1922
 
RETCODE         SQL_API
1923
 
PGAPI_MoreResults(
1924
 
                                  HSTMT hstmt)
1925
 
{
1926
 
        CSTR func = "PGAPI_MoreResults";
1927
 
        StatementClass  *stmt = (StatementClass *) hstmt;
1928
 
        QResultClass    *res;
1929
 
        RETCODE         ret = SQL_SUCCESS;
1930
 
 
1931
 
        mylog("%s: entering...\n", func);
1932
 
        if (stmt && (res = SC_get_Curres(stmt)))
1933
 
                SC_set_Curres(stmt, res->next);
1934
 
        if (res = SC_get_Curres(stmt), res)
1935
 
        {
1936
 
                SQLSMALLINT     num_p;
1937
 
 
1938
 
                if (stmt->multi_statement < 0)
1939
 
                        PGAPI_NumParams(stmt, &num_p);
1940
 
                if (stmt->multi_statement > 0)
1941
 
                { 
1942
 
                        const char *cmdstr;
1943
 
 
1944
 
                        SC_initialize_cols_info(stmt, FALSE, TRUE);
1945
 
                        stmt->statement_type = STMT_TYPE_UNKNOWN;
1946
 
                        if (cmdstr = QR_get_command(res), NULL != cmdstr)
1947
 
                                stmt->statement_type = statement_type(cmdstr);
1948
 
                        stmt->join_info = 0;
1949
 
                        SC_clear_parse_method(stmt);
1950
 
                }
1951
 
                stmt->diag_row_count = res->recent_processed_row_count;
1952
 
                SC_set_rowset_start(stmt, -1, FALSE);
1953
 
                stmt->currTuple = -1;
1954
 
        }
1955
 
        else
1956
 
        {
1957
 
                PGAPI_FreeStmt(hstmt, SQL_CLOSE);
1958
 
                ret = SQL_NO_DATA_FOUND;
1959
 
        }
1960
 
        mylog("%s: returning %d\n", func, ret);
1961
 
        return ret;
1962
 
}
1963
 
 
1964
 
 
1965
 
/*
1966
 
 *      Stuff for updatable cursors.
1967
 
 */
1968
 
static Int2     getNumResultCols(const QResultClass *res)
1969
 
{
1970
 
        Int2    res_cols = QR_NumPublicResultCols(res);
1971
 
        return res_cols;
1972
 
}
1973
 
static OID      getOid(const QResultClass *res, SQLLEN index)
1974
 
{
1975
 
        return res->keyset[index].oid;
1976
 
}
1977
 
static void getTid(const QResultClass *res, SQLLEN index, UInt4 *blocknum, UInt2 *offset)
1978
 
{
1979
 
        *blocknum = res->keyset[index].blocknum;
1980
 
        *offset = res->keyset[index].offset;
1981
 
}
1982
 
static void KeySetSet(const TupleField *tuple, int num_fields, int num_key_fields, KeySet *keyset)
1983
 
{
1984
 
        sscanf(tuple[num_fields - num_key_fields].value, "(%u,%hu)",
1985
 
                        &keyset->blocknum, &keyset->offset);
1986
 
        if (num_key_fields > 1)
1987
 
                sscanf(tuple[num_fields - 1].value, "%u", &keyset->oid);
1988
 
        else
1989
 
                keyset->oid = 0;
1990
 
}
1991
 
 
1992
 
static void AddRollback(StatementClass *stmt, QResultClass *res, SQLLEN index, const KeySet *keyset, Int4 dmlcode)
1993
 
{
1994
 
        ConnectionClass *conn = SC_get_conn(stmt);
1995
 
        Rollback *rollback;
1996
 
 
1997
 
        if (!CC_is_in_trans(conn))
1998
 
                return;
1999
 
inolog("AddRollback %d(%d,%d) %s\n", index, keyset->blocknum, keyset->offset, dmlcode == SQL_ADD ? "ADD" : (dmlcode == SQL_UPDATE ? "UPDATE" : (dmlcode == SQL_DELETE ? "DELETE" : "REFRESH")));
2000
 
        if (!res->rollback)
2001
 
        {
2002
 
                res->rb_count = 0;
2003
 
                res->rb_alloc = 10;
2004
 
                rollback = res->rollback = malloc(sizeof(Rollback) * res->rb_alloc);
2005
 
        }
2006
 
        else
2007
 
        {
2008
 
                if (res->rb_count >= res->rb_alloc)
2009
 
                {
2010
 
                        res->rb_alloc *= 2; 
2011
 
                        if (rollback = realloc(res->rollback, sizeof(Rollback) * res->rb_alloc), !rollback)
2012
 
                        {
2013
 
                                res->rb_alloc = res->rb_count = 0;
2014
 
                                return;
2015
 
                        }
2016
 
                        res->rollback = rollback; 
2017
 
                }
2018
 
                rollback = res->rollback + res->rb_count;
2019
 
        }
2020
 
        rollback->index = index;
2021
 
        rollback->option = dmlcode;
2022
 
        rollback->offset = 0;
2023
 
        rollback->blocknum = 0;
2024
 
        if (keyset)
2025
 
        {
2026
 
                rollback->blocknum = keyset->blocknum;
2027
 
                rollback->offset = keyset->offset;
2028
 
        }
2029
 
 
2030
 
        conn->result_uncommitted = 1;
2031
 
        res->rb_count++;        
2032
 
}
2033
 
 
2034
 
SQLLEN ClearCachedRows(TupleField *tuple, int num_fields, SQLLEN num_rows)
2035
 
{
2036
 
        SQLLEN  i;
2037
 
 
2038
 
        for (i = 0; i < num_fields * num_rows; i++, tuple++)
2039
 
        {
2040
 
                if (tuple->value)
2041
 
                {
2042
 
inolog("freeing tuple[%d][%d].value=%p\n", i / num_fields, i % num_fields, tuple->value);
2043
 
                        free(tuple->value);
2044
 
                        tuple->value = NULL;
2045
 
                }
2046
 
                tuple->len = -1;
2047
 
        }
2048
 
        return i;
2049
 
}
2050
 
SQLLEN ReplaceCachedRows(TupleField *otuple, const TupleField *ituple, int num_fields, SQLLEN num_rows)
2051
 
{
2052
 
        SQLLEN  i;
2053
 
 
2054
 
inolog("ReplaceCachedRows %p num_fields=%d num_rows=%d\n", otuple, num_fields, num_rows);
2055
 
        for (i = 0; i < num_fields * num_rows; i++, ituple++, otuple++)
2056
 
        {
2057
 
                if (otuple->value)
2058
 
                {
2059
 
                        free(otuple->value);
2060
 
                        otuple->value = NULL;
2061
 
                }
2062
 
                if (ituple->value)
2063
 
{
2064
 
                        otuple->value = strdup(ituple->value);
2065
 
inolog("[%d,%d] %s copied\n", i / num_fields, i % num_fields, otuple->value);
2066
 
}
2067
 
                otuple->len = ituple->len;
2068
 
        }
2069
 
        return i;
2070
 
}
2071
 
 
2072
 
static
2073
 
int MoveCachedRows(TupleField *otuple, TupleField *ituple, Int2 num_fields, SQLLEN num_rows)
2074
 
{
2075
 
        int     i;
2076
 
 
2077
 
inolog("MoveCachedRows %p num_fields=%d num_rows=%d\n", otuple, num_fields, num_rows);
2078
 
        for (i = 0; i < num_fields * num_rows; i++, ituple++, otuple++)
2079
 
        {
2080
 
                if (otuple->value)
2081
 
                {
2082
 
                        free(otuple->value);
2083
 
                        otuple->value = NULL;
2084
 
                }
2085
 
                if (ituple->value)
2086
 
                {
2087
 
                        otuple->value = ituple->value;
2088
 
                        ituple->value = NULL;
2089
 
inolog("[%d,%d] %s copied\n", i / num_fields, i % num_fields, otuple->value);
2090
 
                }
2091
 
                otuple->len = ituple->len;
2092
 
                ituple->len = -1;
2093
 
        }
2094
 
        return i;
2095
 
}
2096
 
 
2097
 
static BOOL     tupleExists(const StatementClass *stmt, const KeySet *keyset)
2098
 
{
2099
 
        char    selstr[256];
2100
 
        const TABLE_INFO        *ti = stmt->ti[0];
2101
 
        QResultClass    *res;
2102
 
        RETCODE         ret = FALSE;
2103
 
 
2104
 
        if (NAME_IS_VALID(ti->schema_name))
2105
 
                snprintf(selstr, sizeof(selstr), "select 1 from \"%s\".\"%s\" where ctid = '(%d,%d)'",
2106
 
                        SAFE_NAME(ti->schema_name), SAFE_NAME(ti->table_name), keyset->blocknum, keyset->offset);
2107
 
        else
2108
 
                snprintf(selstr, sizeof(selstr), "select 1 from \"%s\" where ctid = '(%d,%d)'",
2109
 
                        SAFE_NAME(ti->table_name), keyset->blocknum, keyset->offset);
2110
 
        res = CC_send_query(SC_get_conn(stmt), selstr, NULL, 0, NULL);
2111
 
        if (QR_command_maybe_successful(res) && 1 == res->num_cached_rows)
2112
 
                ret = TRUE;
2113
 
        QR_Destructor(res);
2114
 
        return ret;
2115
 
}
2116
 
 
2117
 
static BOOL enlargeAdded(QResultClass *res, UInt4 number, const StatementClass *stmt)
2118
 
{
2119
 
        UInt4   alloc;
2120
 
        int     num_fields = res->num_fields;
2121
 
 
2122
 
        alloc = res->ad_alloc;
2123
 
        if (0 == alloc)
2124
 
                alloc = number > 10 ? number : 10;
2125
 
        else
2126
 
                while (alloc < number)
2127
 
                {
2128
 
                        alloc *= 2;
2129
 
                }
2130
 
 
2131
 
        if (alloc <= res->ad_alloc)
2132
 
                return TRUE;
2133
 
        QR_REALLOC_return_with_error(res->added_keyset, KeySet, sizeof(KeySet) * alloc, res, "enlargeAdded failed", FALSE);
2134
 
        if (SQL_CURSOR_KEYSET_DRIVEN != stmt->options.cursor_type)
2135
 
                QR_REALLOC_return_with_error(res->added_tuples, TupleField, sizeof(TupleField) * num_fields * alloc, res, "enlargeAdded failed 2", FALSE);
2136
 
        res->ad_alloc = alloc;
2137
 
        return TRUE;
2138
 
}
2139
 
static void AddAdded(StatementClass *stmt, QResultClass *res, SQLLEN index, const TupleField *tuple_added)
2140
 
{
2141
 
        KeySet  *added_keyset, *keyset, keys;
2142
 
        TupleField      *added_tuples = NULL, *tuple;
2143
 
        UInt4   ad_count;
2144
 
        Int2    num_fields;
2145
 
 
2146
 
        if (!res)       return;
2147
 
        num_fields = res->num_fields;
2148
 
inolog("AddAdded index=%d, tuple=%p, num_fields=%d\n", index, tuple_added, num_fields);
2149
 
        ad_count = res->ad_count;
2150
 
        res->ad_count++;
2151
 
        if (QR_get_cursor(res))
2152
 
                index = -(SQLLEN)res->ad_count;
2153
 
        if (!tuple_added)
2154
 
                return;
2155
 
        KeySetSet(tuple_added, num_fields + res->num_key_fields, res->num_key_fields, &keys);
2156
 
        keys.status = SQL_ROW_ADDED;
2157
 
        if (CC_is_in_trans(SC_get_conn(stmt)))
2158
 
                keys.status |= CURS_SELF_ADDING;
2159
 
        else
2160
 
                keys.status |= CURS_SELF_ADDED;
2161
 
        AddRollback(stmt, res, index, &keys, SQL_ADD);
2162
 
 
2163
 
        if (!QR_get_cursor(res))
2164
 
                return;
2165
 
        if (ad_count > 0 && 0 == res->ad_alloc)
2166
 
                return;
2167
 
        if (!enlargeAdded(res, ad_count + 1, stmt))
2168
 
                return;
2169
 
        added_keyset = res->added_keyset; 
2170
 
        added_tuples = res->added_tuples;
2171
 
 
2172
 
        keyset = added_keyset + ad_count;
2173
 
        *keyset = keys; 
2174
 
        if (added_tuples)
2175
 
        {
2176
 
                tuple = added_tuples + num_fields * ad_count;
2177
 
                memset(tuple, 0, sizeof(TupleField) * num_fields);
2178
 
                ReplaceCachedRows(tuple, tuple_added, num_fields, 1);
2179
 
        }
2180
 
}
2181
 
 
2182
 
static  void RemoveAdded(QResultClass *, SQLLEN);
2183
 
static  void RemoveUpdated(QResultClass *, SQLLEN);
2184
 
static  void RemoveUpdatedAfterTheKey(QResultClass *, SQLLEN, const KeySet*);
2185
 
static  void RemoveDeleted(QResultClass *, SQLLEN);
2186
 
static  void RemoveAdded(QResultClass *res, SQLLEN index)
2187
 
{
2188
 
        SQLLEN  rmidx, mv_count;
2189
 
        Int2    num_fields = res->num_fields;
2190
 
        KeySet  *added_keyset;
2191
 
        TupleField      *added_tuples;
2192
 
 
2193
 
        mylog("RemoveAdded index=%d\n", index);
2194
 
        if (index < 0)
2195
 
                rmidx = -index - 1;
2196
 
        else
2197
 
                rmidx = index - res->num_total_read;
2198
 
        if (rmidx >= res->ad_count)
2199
 
                return;
2200
 
        added_keyset = res->added_keyset + rmidx;
2201
 
        added_tuples = res->added_tuples + num_fields * rmidx;
2202
 
        ClearCachedRows(added_tuples, num_fields, 1);
2203
 
        mv_count = res->ad_count - rmidx - 1;
2204
 
        if (mv_count > 0)
2205
 
        {
2206
 
                memmove(added_keyset, added_keyset + 1, mv_count * sizeof(KeySet));
2207
 
                memmove(added_tuples, added_tuples + num_fields, mv_count * num_fields * sizeof(TupleField));
2208
 
        }
2209
 
        RemoveDeleted(res, index);
2210
 
        RemoveUpdated(res, index);
2211
 
        res->ad_count--;
2212
 
        mylog("RemoveAdded removed=1 count=%d\n", res->ad_count);
2213
 
}
2214
 
 
2215
 
static void CommitAdded(QResultClass *res)
2216
 
{
2217
 
        KeySet  *added_keyset;
2218
 
        int     i;
2219
 
        UWORD   status;
2220
 
 
2221
 
        mylog("CommitAdded res=%p\n", res);
2222
 
        if (!res || !res->added_keyset) return;
2223
 
        added_keyset = res->added_keyset;
2224
 
        for (i = res->ad_count - 1; i >= 0; i--)
2225
 
        {
2226
 
                status = added_keyset[i].status;
2227
 
                if (0 != (status & CURS_SELF_ADDING))
2228
 
                {
2229
 
                        status |= CURS_SELF_ADDED;
2230
 
                        status &= ~CURS_SELF_ADDING;
2231
 
                }
2232
 
                if (0 != (status & CURS_SELF_UPDATING))
2233
 
                {
2234
 
                        status |= CURS_SELF_UPDATED;
2235
 
                        status &= ~CURS_SELF_UPDATING;
2236
 
                }
2237
 
                if (0 != (status & CURS_SELF_DELETING))
2238
 
                {
2239
 
                        status |= CURS_SELF_DELETED;
2240
 
                        status &= ~CURS_SELF_DELETING;
2241
 
                }
2242
 
                if (status != added_keyset[i].status)
2243
 
                {
2244
 
inolog("!!Commit Added=%d(%d)\n", QR_get_num_total_read(res) + i, i);
2245
 
                        added_keyset[i].status = status;
2246
 
                }
2247
 
        }
2248
 
}
2249
 
 
2250
 
 
2251
 
int AddDeleted(QResultClass *res, SQLULEN index, KeySet *keyset)
2252
 
{
2253
 
        int     i;
2254
 
        Int2    dl_count, new_alloc;
2255
 
        SQLULEN *deleted;
2256
 
        KeySet  *deleted_keyset;
2257
 
        UWORD   status;
2258
 
        Int2    num_fields = res->num_fields;
2259
 
 
2260
 
inolog("AddDeleted %d\n", index);
2261
 
        if (!res)       return FALSE;
2262
 
        dl_count = res->dl_count;
2263
 
        res->dl_count++;
2264
 
        if (!QR_get_cursor(res))
2265
 
                return TRUE;
2266
 
        if (!res->deleted)
2267
 
        {
2268
 
                dl_count = 0;
2269
 
                new_alloc = 10;
2270
 
                QR_MALLOC_return_with_error(res->deleted, SQLULEN, sizeof(SQLULEN) * new_alloc, res, "Deleted index malloc error", FALSE);
2271
 
                QR_MALLOC_return_with_error(res->deleted_keyset, KeySet, sizeof(KeySet) * new_alloc, res, "Deleted keyset malloc error", FALSE);
2272
 
                deleted = res->deleted;
2273
 
                deleted_keyset = res->deleted_keyset;
2274
 
                res->dl_alloc = new_alloc;
2275
 
        }
2276
 
        else
2277
 
        {
2278
 
                if (dl_count >= res->dl_alloc)
2279
 
                {
2280
 
                        new_alloc = res->dl_alloc * 2;
2281
 
                        res->dl_alloc = 0;
2282
 
                        QR_REALLOC_return_with_error(res->deleted, SQLULEN, sizeof(SQLULEN) * new_alloc, res, "Deleted index realloc error", FALSE);
2283
 
                        deleted = res->deleted;
2284
 
                        QR_REALLOC_return_with_error(res->deleted_keyset, KeySet, sizeof(KeySet) * new_alloc, res, "Deleted KeySet realloc error", FALSE);
2285
 
                        deleted_keyset = res->deleted_keyset;
2286
 
                        res->dl_alloc = new_alloc; 
2287
 
                }
2288
 
                /* sort deleted indexes in ascending order */
2289
 
                for (i = 0, deleted = res->deleted, deleted_keyset = res->deleted_keyset; i < dl_count; i++, deleted++, deleted_keyset += num_fields)
2290
 
                {
2291
 
                        if (index < *deleted)
2292
 
                                break;
2293
 
                }
2294
 
                memmove(deleted + 1, deleted, sizeof(SQLLEN) * (dl_count - i)); 
2295
 
                memmove(deleted_keyset + 1, deleted_keyset, sizeof(KeySet) * (dl_count - i)); 
2296
 
        }
2297
 
        *deleted = index;
2298
 
        *deleted_keyset = *keyset;
2299
 
        status = keyset->status;
2300
 
        status &= (~KEYSET_INFO_PUBLIC);
2301
 
        status |= SQL_ROW_DELETED;
2302
 
        if (CC_is_in_trans(QR_get_conn(res)))
2303
 
        {
2304
 
                status |= CURS_SELF_DELETING;
2305
 
                QR_get_conn(res)->result_uncommitted = 1;
2306
 
        }
2307
 
        else
2308
 
        {
2309
 
                status &= ~(CURS_SELF_ADDING | CURS_SELF_UPDATING | CURS_SELF_DELETING);
2310
 
                status |= CURS_SELF_DELETED;
2311
 
        }
2312
 
        deleted_keyset->status = status;
2313
 
        res->dl_count = dl_count + 1;
2314
 
 
2315
 
        return TRUE;
2316
 
}
2317
 
 
2318
 
static void RemoveDeleted(QResultClass *res, SQLLEN index)
2319
 
{
2320
 
        int     i, mv_count, rm_count = 0;
2321
 
        SQLLEN  pidx, midx;
2322
 
        SQLULEN *deleted, num_read = QR_get_num_total_read(res);
2323
 
        KeySet  *deleted_keyset;
2324
 
 
2325
 
        mylog("RemoveDeleted index=%d\n", index);
2326
 
        if (index < 0)
2327
 
        {
2328
 
                midx = index;
2329
 
                pidx = num_read - index - 1;
2330
 
        }
2331
 
        else
2332
 
        {
2333
 
                pidx = index;
2334
 
                if (index >= num_read)
2335
 
                        midx = num_read - index - 1;
2336
 
                else
2337
 
                        midx = index;
2338
 
        }
2339
 
        for (i = 0; i < res->dl_count; i++)
2340
 
        {
2341
 
                if (pidx == res->deleted[i] ||
2342
 
                    midx == res->deleted[i])
2343
 
                {
2344
 
                        mv_count = res->dl_count - i - 1;
2345
 
                        if (mv_count > 0)
2346
 
                        {
2347
 
                                deleted = res->deleted + i;
2348
 
                                deleted_keyset = res->deleted_keyset + i;
2349
 
                                memmove(deleted, deleted + 1, mv_count * sizeof(SQLULEN));
2350
 
                                memmove(deleted_keyset, deleted_keyset + 1, mv_count * sizeof(KeySet));
2351
 
                        }
2352
 
                        res->dl_count--;
2353
 
                        rm_count++;             
2354
 
                }
2355
 
        }
2356
 
        mylog("RemoveDeleted removed count=%d,%d\n", rm_count, res->dl_count);
2357
 
}
2358
 
 
2359
 
static void CommitDeleted(QResultClass *res)
2360
 
{
2361
 
        int     i;
2362
 
        SQLULEN *deleted;
2363
 
        KeySet  *deleted_keyset;
2364
 
        UWORD   status;
2365
 
 
2366
 
        if (!res->deleted)
2367
 
                return;
2368
 
 
2369
 
        for (i = 0, deleted = res->deleted, deleted_keyset = res->deleted_keyset; i < res->dl_count; i++, deleted++, deleted_keyset++)
2370
 
        {
2371
 
                status = deleted_keyset->status;
2372
 
                if (0 != (status & CURS_SELF_ADDING))
2373
 
                {
2374
 
                        status |= CURS_SELF_ADDED;
2375
 
                        status &= ~CURS_SELF_ADDING;
2376
 
                }
2377
 
                if (0 != (status & CURS_SELF_UPDATING))
2378
 
                {
2379
 
                        status |= CURS_SELF_UPDATED;
2380
 
                        status &= ~CURS_SELF_UPDATING;
2381
 
                }
2382
 
                if (0 != (status & CURS_SELF_DELETING))
2383
 
                {
2384
 
                        status |= CURS_SELF_DELETED;
2385
 
                        status &= ~CURS_SELF_DELETING;
2386
 
                }
2387
 
                if (status != deleted_keyset->status)
2388
 
                {
2389
 
inolog("!!Commit Deleted=%d(%d)\n", *deleted, i);
2390
 
                        deleted_keyset->status = status;
2391
 
                }
2392
 
        } 
2393
 
}
2394
 
 
2395
 
static BOOL enlargeUpdated(QResultClass *res, Int4 number, const StatementClass *stmt)
2396
 
{
2397
 
        Int2    alloc;
2398
 
 
2399
 
        alloc = res->up_alloc;
2400
 
        if (0 == alloc)
2401
 
                alloc = number > 10 ? number : 10;
2402
 
        else
2403
 
                while (alloc < number)
2404
 
                {
2405
 
                        alloc *= 2;
2406
 
                }
2407
 
        if (alloc <= res->up_alloc)
2408
 
                return TRUE;
2409
 
 
2410
 
        QR_REALLOC_return_with_error(res->updated, SQLULEN, sizeof(SQLULEN) * alloc, res, "enlargeUpdated failed", FALSE);
2411
 
        QR_REALLOC_return_with_error(res->updated_keyset, KeySet, sizeof(KeySet) * alloc, res, "enlargeUpdated failed 2", FALSE);
2412
 
        if (SQL_CURSOR_KEYSET_DRIVEN != stmt->options.cursor_type)
2413
 
                QR_REALLOC_return_with_error(res->updated_tuples, TupleField, sizeof(TupleField) * res->num_fields * alloc, res, "enlargeUpdated failed 3", FALSE);
2414
 
        res->up_alloc = alloc;
2415
 
 
2416
 
        return TRUE;
2417
 
}
2418
 
 
2419
 
static void AddUpdated(StatementClass *stmt, SQLLEN index)
2420
 
{
2421
 
        QResultClass    *res;
2422
 
        SQLULEN *updated;
2423
 
        KeySet  *updated_keyset, *keyset;
2424
 
        TupleField      *updated_tuples = NULL, *tuple_updated,  *tuple;
2425
 
        SQLULEN kres_ridx;
2426
 
        UInt2   up_count;
2427
 
        BOOL    is_in_trans;
2428
 
        SQLLEN  upd_idx, upd_add_idx;
2429
 
        Int2    num_fields;
2430
 
        int     i;
2431
 
        UWORD   status;
2432
 
 
2433
 
inolog("AddUpdated index=%d\n", index);
2434
 
        if (!stmt)      return;
2435
 
        if (res = SC_get_Curres(stmt), !res)    return;
2436
 
        if (!res->keyset)               return;
2437
 
        kres_ridx = GIdx2KResIdx(index, stmt, res);
2438
 
        if (kres_ridx < 0 || kres_ridx >= res->num_cached_keys)
2439
 
                return;
2440
 
        keyset = res->keyset + kres_ridx;
2441
 
        if (0 != (keyset->status & CURS_SELF_ADDING))
2442
 
                AddRollback(stmt, res, index, res->keyset + kres_ridx, SQL_REFRESH);
2443
 
        if (!QR_get_cursor(res))        return;
2444
 
        up_count = res->up_count;
2445
 
        if (up_count > 0 && 0 == res->up_alloc) return;
2446
 
        num_fields = res->num_fields;
2447
 
        tuple_updated = res->backend_tuples + kres_ridx * num_fields;
2448
 
        if (!tuple_updated)
2449
 
                return;
2450
 
        upd_idx = -1;
2451
 
        upd_add_idx = -1;
2452
 
        updated = res->updated;
2453
 
        is_in_trans = CC_is_in_trans(SC_get_conn(stmt));
2454
 
        updated_keyset = res->updated_keyset;   
2455
 
        status = keyset->status;
2456
 
        status &= (~KEYSET_INFO_PUBLIC);
2457
 
        status |= SQL_ROW_UPDATED;
2458
 
        if (is_in_trans)
2459
 
                status |= CURS_SELF_UPDATING;
2460
 
        else
2461
 
        {
2462
 
                for (i = up_count - 1; i >= 0; i--)
2463
 
                {
2464
 
                        if (updated[i] == index)
2465
 
                                break;
2466
 
                }
2467
 
                if (i >= 0)
2468
 
                        upd_idx = i;
2469
 
                else
2470
 
                {
2471
 
                        SQLLEN  num_totals = QR_get_num_total_tuples(res);
2472
 
                        if (index >= num_totals)
2473
 
                                upd_add_idx = num_totals - index;
2474
 
                }
2475
 
                status |= CURS_SELF_UPDATED;
2476
 
                status &= ~(CURS_SELF_ADDING | CURS_SELF_UPDATING | CURS_SELF_DELETING);
2477
 
        }
2478
 
 
2479
 
        tuple = NULL;
2480
 
        /* update the corresponding add(updat)ed info */
2481
 
        if (upd_add_idx >= 0)
2482
 
        {
2483
 
                res->added_keyset[upd_add_idx].status = status;
2484
 
                if (res->added_tuples)
2485
 
                {
2486
 
                        tuple = res->added_tuples + num_fields * upd_add_idx;
2487
 
                        ClearCachedRows(tuple, num_fields, 1);
2488
 
                }
2489
 
        }
2490
 
        else if (upd_idx >= 0)
2491
 
        {
2492
 
                res->updated_keyset[upd_idx].status = status;
2493
 
                if (res->updated_tuples)
2494
 
                {
2495
 
                        tuple = res->added_tuples + num_fields * upd_add_idx;
2496
 
                        ClearCachedRows(tuple, num_fields, 1);
2497
 
                }
2498
 
        }
2499
 
        else
2500
 
        {
2501
 
                if (!enlargeUpdated(res, res->up_count + 1, stmt))
2502
 
                        return;
2503
 
                updated = res->updated; 
2504
 
                updated_keyset = res->updated_keyset; 
2505
 
                updated_tuples = res->updated_tuples;
2506
 
                upd_idx = up_count;
2507
 
                updated[up_count] = index;
2508
 
                updated_keyset[up_count] = *keyset;
2509
 
                updated_keyset[up_count].status = status;
2510
 
                if (updated_tuples)
2511
 
                {
2512
 
                        tuple = updated_tuples + num_fields * up_count;
2513
 
                        memset(tuple, 0, sizeof(TupleField) * num_fields);
2514
 
                }
2515
 
                res->up_count++;
2516
 
        }
2517
 
 
2518
 
        if (tuple)
2519
 
                ReplaceCachedRows(tuple, tuple_updated, num_fields, 1);
2520
 
        if (is_in_trans)
2521
 
                SC_get_conn(stmt)->result_uncommitted = 1;
2522
 
        mylog("up_count=%d\n", res->up_count);
2523
 
}
2524
 
 
2525
 
static void RemoveUpdated(QResultClass *res, SQLLEN index)
2526
 
{
2527
 
        mylog("RemoveUpdated index=%d\n", index);
2528
 
        RemoveUpdatedAfterTheKey(res, index, NULL);
2529
 
}
2530
 
 
2531
 
static void RemoveUpdatedAfterTheKey(QResultClass *res, SQLLEN index, const KeySet *keyset)
2532
 
{
2533
 
        SQLULEN *updated, num_read = QR_get_num_total_read(res);
2534
 
        KeySet  *updated_keyset;
2535
 
        TupleField      *updated_tuples = NULL;
2536
 
        SQLLEN  pidx, midx, mv_count;
2537
 
        int     i, num_fields = res->num_fields, rm_count = 0;
2538
 
 
2539
 
        mylog("RemoveUpdatedAfterTheKey %d,(%d,%d)\n", index, keyset ? keyset->blocknum : 0, keyset ? keyset->offset : 0);
2540
 
        if (index < 0)
2541
 
        {
2542
 
                midx = index;
2543
 
                pidx = num_read - index - 1;
2544
 
        }
2545
 
        else
2546
 
        {
2547
 
                pidx = index;
2548
 
                if (index >= num_read)
2549
 
                        midx = num_read - index - 1;
2550
 
                else
2551
 
                        midx = index;
2552
 
        }
2553
 
        for (i = 0; i < res->up_count; i++)
2554
 
        {
2555
 
                updated = res->updated + i;
2556
 
                if (pidx == *updated ||
2557
 
                    midx == *updated)
2558
 
                {
2559
 
                        updated_keyset = res->updated_keyset + i;
2560
 
                        if (keyset &&
2561
 
                            updated_keyset->blocknum == keyset->blocknum &&
2562
 
                            updated_keyset->offset == keyset->offset)
2563
 
                                break;
2564
 
                        updated_tuples = NULL;
2565
 
                        if (res->updated_tuples)
2566
 
                        {
2567
 
                                updated_tuples = res->updated_tuples + i * num_fields;
2568
 
                                ClearCachedRows(updated_tuples, num_fields, 1);
2569
 
                        }
2570
 
                        mv_count = res->up_count - i -1;
2571
 
                        if (mv_count > 0)
2572
 
                        {
2573
 
                                memmove(updated, updated + 1, sizeof(SQLULEN) * mv_count); 
2574
 
                                memmove(updated_keyset, updated_keyset + 1, sizeof(KeySet) * mv_count); 
2575
 
                                if (updated_tuples)
2576
 
                                        memmove(updated_tuples, updated_tuples + num_fields, sizeof(TupleField) * num_fields * mv_count);
2577
 
                        }
2578
 
                        res->up_count--;
2579
 
                        rm_count++;
2580
 
                }
2581
 
        }
2582
 
        mylog("RemoveUpdatedAfter removed count=%d,%d\n", rm_count, res->up_count);
2583
 
}
2584
 
 
2585
 
static void CommitUpdated(QResultClass *res)
2586
 
{
2587
 
        KeySet  *updated_keyset;
2588
 
        int     i;
2589
 
        UWORD   status;
2590
 
 
2591
 
        mylog("CommitUpdated res=%p\n", res);
2592
 
        if (!res)       return;
2593
 
        if (!QR_get_cursor(res))
2594
 
                return;
2595
 
        if (res->up_count <= 0)
2596
 
                return;
2597
 
        if (updated_keyset = res->updated_keyset, !updated_keyset)
2598
 
                return;
2599
 
        for (i = res->up_count - 1; i >= 0; i--)
2600
 
        {
2601
 
                status = updated_keyset[i].status;
2602
 
                if (0 != (status & CURS_SELF_UPDATING))
2603
 
                {
2604
 
                        status &= ~CURS_SELF_UPDATING;
2605
 
                        status |= CURS_SELF_UPDATED;
2606
 
                }
2607
 
                if (0 != (status & CURS_SELF_ADDING))
2608
 
                {
2609
 
                        status &= ~CURS_SELF_ADDING;
2610
 
                        status |= CURS_SELF_ADDED;
2611
 
                }
2612
 
                if (0 != (status & CURS_SELF_DELETING))
2613
 
                {
2614
 
                        status &= ~CURS_SELF_DELETING;
2615
 
                        status |= CURS_SELF_DELETED;
2616
 
                }
2617
 
                if (status != updated_keyset[i].status)
2618
 
                {
2619
 
inolog("!!Commit Updated=%d(%d)\n", res->updated[i], i);
2620
 
                        updated_keyset[i].status = status;
2621
 
                }
2622
 
        }
2623
 
}
2624
 
 
2625
 
 
2626
 
static void DiscardRollback(StatementClass *stmt, QResultClass *res)
2627
 
{
2628
 
        int     i;
2629
 
        SQLLEN  index, kres_ridx;
2630
 
        UWORD   status;
2631
 
        Rollback *rollback;
2632
 
        KeySet  *keyset;
2633
 
        BOOL    kres_is_valid;
2634
 
 
2635
 
inolog("DiscardRollback");
2636
 
        if (QR_get_cursor(res))
2637
 
        {
2638
 
                CommitAdded(res);
2639
 
                CommitUpdated(res);
2640
 
                CommitDeleted(res);
2641
 
                return;
2642
 
        }
2643
 
 
2644
 
        if (0 == res->rb_count || NULL == res->rollback)
2645
 
                return;
2646
 
        rollback = res->rollback;
2647
 
        keyset = res->keyset;
2648
 
        for (i = 0; i < res->rb_count; i++)
2649
 
        {
2650
 
                index = rollback[i].index;
2651
 
                status = 0;
2652
 
                kres_is_valid = FALSE;
2653
 
                if (index >= 0)
2654
 
                {
2655
 
                        kres_ridx = GIdx2KResIdx(index, stmt, res);
2656
 
                        if (kres_ridx >= 0 && kres_ridx < res->num_cached_keys)
2657
 
                        {
2658
 
                                kres_is_valid = TRUE;
2659
 
                                status = keyset[kres_ridx].status;
2660
 
                        }
2661
 
                }
2662
 
                if (kres_is_valid)
2663
 
                {
2664
 
                        keyset[kres_ridx].status &= ~(CURS_SELF_DELETING | CURS_SELF_UPDATING | CURS_SELF_ADDING);
2665
 
                        keyset[kres_ridx].status |= ((status & (CURS_SELF_DELETING | CURS_SELF_UPDATING | CURS_SELF_ADDING)) << 3);
2666
 
                }
2667
 
        }
2668
 
        free(rollback);
2669
 
        res->rollback = NULL;
2670
 
        res->rb_count = res->rb_alloc = 0;
2671
 
}
2672
 
 
2673
 
static QResultClass *positioned_load(StatementClass *stmt, UInt4 flag, const UInt4 *oidint, const char *tid);
2674
 
static void UndoRollback(StatementClass *stmt, QResultClass *res, BOOL partial)
2675
 
{
2676
 
        Int4    i, rollbp;
2677
 
        SQLLEN  index, ridx, kres_ridx;
2678
 
        UWORD   status;
2679
 
        Rollback *rollback;
2680
 
        KeySet  *keyset, keys, *wkey = NULL;
2681
 
        BOOL    curs = (NULL != QR_get_cursor(res)), texist, kres_is_valid;
2682
 
 
2683
 
        if (0 == res->rb_count || NULL == res->rollback)
2684
 
                return;
2685
 
        rollback = res->rollback;
2686
 
        keyset = res->keyset;
2687
 
 
2688
 
        rollbp = 0;
2689
 
        if (partial)
2690
 
        {
2691
 
                SQLLEN  pidx, midx;
2692
 
                Int2    doubtp, rollbps;
2693
 
                int     j;
2694
 
 
2695
 
                rollbps = rollbp = res->rb_count;
2696
 
                for (i = 0, doubtp = 0; i < res->rb_count; i++)
2697
 
                {
2698
 
                        index = rollback[i].index;
2699
 
                        keys.blocknum = rollback[i].blocknum;
2700
 
                        keys.offset = rollback[i].offset;
2701
 
                        texist = tupleExists(stmt, &keys);
2702
 
inolog("texist[%d]=%d", i, texist);
2703
 
                        if (SQL_ADD == rollback[i].option)
2704
 
                        {
2705
 
                                if (texist)
2706
 
                                        doubtp = i + 1;
2707
 
                        }
2708
 
                        else if (SQL_REFRESH == rollback[i].option)
2709
 
                        {
2710
 
                                if (texist || doubtp == i)
2711
 
                                        doubtp = i + 1;
2712
 
                        }
2713
 
                        else
2714
 
                        {
2715
 
                                if (texist)
2716
 
                                        break;
2717
 
                                if (doubtp == i)
2718
 
                                        doubtp = i + 1;
2719
 
                        }
2720
 
inolog(" doubtp=%d\n", doubtp);
2721
 
                }
2722
 
                rollbp = i;
2723
 
inolog(" doubtp=%d,rollbp=%d\n", doubtp, rollbp);
2724
 
                if (doubtp < 0)
2725
 
                        doubtp = 0;
2726
 
                do
2727
 
                {
2728
 
                        rollbps = rollbp;
2729
 
                        for (i = doubtp; i < rollbp; i++)
2730
 
                        {
2731
 
                                index = rollback[i].index;
2732
 
                                if (SQL_ADD == rollback[i].option)
2733
 
                                {
2734
 
inolog("index[%d]=%d\n", i, index);
2735
 
                                        if (index < 0)
2736
 
                                        {
2737
 
                                                midx = index;
2738
 
                                                pidx = res->num_total_read - index - 1;
2739
 
                                        }
2740
 
                                        else
2741
 
                                        {
2742
 
                                                pidx = index;
2743
 
                                                midx = res->num_total_read - index - 1;
2744
 
                                        }
2745
 
inolog("pidx=%d,midx=%d\n", pidx, midx); 
2746
 
                                        for (j = rollbp - 1; j > i; j--)
2747
 
                                        {
2748
 
                                                if (rollback[j].index == midx ||
2749
 
                                                    rollback[j].index == pidx)
2750
 
                                                {
2751
 
                                                        if (SQL_DELETE == rollback[j].option)
2752
 
                                                        {
2753
 
inolog("delete[%d].index=%d\n", j, rollback[j].index);
2754
 
                                                                break;
2755
 
                                                        }
2756
 
                                                        /*else if (SQL_UPDATE == rollback[j].option)
2757
 
                                                        {
2758
 
inolog("update[%d].index=%d\n", j, rollback[j].index);
2759
 
                                                                if (IndexExists(stmt, res, rollback + j))
2760
 
                                                                        break;
2761
 
                                                        }*/
2762
 
                                                }
2763
 
                                        }
2764
 
                                        if (j <= i)
2765
 
                                        {
2766
 
                                                rollbp = i;
2767
 
                                                break;
2768
 
                                        }
2769
 
                                }
2770
 
                        }
2771
 
                } while (rollbp < rollbps);
2772
 
        }
2773
 
inolog("rollbp=%d\n", rollbp);
2774
 
 
2775
 
        for (i = res->rb_count - 1; i >= rollbp; i--)
2776
 
        {
2777
 
inolog("UndoRollback %d(%d)\n", i, rollback[i].option);
2778
 
                index = rollback[i].index;
2779
 
                if (curs)
2780
 
                {
2781
 
                        if (SQL_ADD == rollback[i].option)
2782
 
                                RemoveAdded(res, index);
2783
 
                        RemoveDeleted(res, index);
2784
 
                        keys.blocknum = rollback[i].blocknum;
2785
 
                        keys.offset = rollback[i].offset;
2786
 
                        RemoveUpdatedAfterTheKey(res, index, &keys);
2787
 
                }
2788
 
                status = 0;
2789
 
                kres_is_valid = FALSE;
2790
 
                if (index >= 0)
2791
 
                {
2792
 
                        kres_ridx = GIdx2KResIdx(index, stmt, res);
2793
 
                        if (kres_ridx >= 0 && kres_ridx < res->num_cached_keys)
2794
 
                        {
2795
 
                                kres_is_valid = TRUE;
2796
 
                                wkey = keyset + kres_ridx;
2797
 
                                status = wkey->status;
2798
 
                        }
2799
 
                }
2800
 
inolog(" index=%d status=%hx", index, status);
2801
 
                if (kres_is_valid)
2802
 
                {
2803
 
                        QResultClass    *qres;
2804
 
                        Int2            num_fields = res->num_fields;
2805
 
 
2806
 
                        ridx = GIdx2CacheIdx(index, stmt, res);
2807
 
                        if (SQL_ADD == rollback[i].option)
2808
 
                        {
2809
 
                                if (ridx >=0 && ridx < res->num_cached_rows)
2810
 
                                {
2811
 
                                        TupleField *tuple = res->backend_tuples + res->num_fields * ridx;
2812
 
                                        ClearCachedRows(tuple, res->num_fields, 1);
2813
 
                                        res->num_cached_rows--;
2814
 
                                }
2815
 
                                res->num_cached_keys--;
2816
 
                                if (!curs)
2817
 
                                        res->ad_count--;
2818
 
                        }
2819
 
                        else if (SQL_REFRESH == rollback[i].option)
2820
 
                                continue;
2821
 
                        else
2822
 
                        {
2823
 
inolog(" (%u, %u)", wkey->blocknum,  wkey->offset);
2824
 
                                wkey->blocknum = rollback[i].blocknum;
2825
 
                                wkey->offset = rollback[i].offset;
2826
 
inolog("->(%u, %u)\n", wkey->blocknum, wkey->offset);
2827
 
                                wkey->status &= ~KEYSET_INFO_PUBLIC;
2828
 
                                if (SQL_DELETE == rollback[i].option)
2829
 
                                        wkey->status &= ~CURS_SELF_DELETING;
2830
 
                                else if (SQL_UPDATE == rollback[i].option)
2831
 
                                        wkey->status &= ~CURS_SELF_UPDATING;
2832
 
                                wkey->status |= CURS_NEEDS_REREAD;
2833
 
                                if (ridx >=0 && ridx < res->num_cached_rows)
2834
 
                                {
2835
 
                                        char    tidval[32];
2836
 
 
2837
 
                                        sprintf(tidval, "(%d,%d)", wkey->blocknum, wkey->offset);
2838
 
                                        qres = positioned_load(stmt, 0, NULL, tidval);
2839
 
                                        if (QR_command_maybe_successful(qres) &&
2840
 
                                            QR_get_num_cached_tuples(qres) == 1)
2841
 
                                        {
2842
 
                                                MoveCachedRows(res->backend_tuples + num_fields * ridx, qres->backend_tuples, num_fields, 1);
2843
 
                                                wkey->status &= ~CURS_NEEDS_REREAD;
2844
 
                                        }
2845
 
                                        QR_Destructor(qres);
2846
 
                                }
2847
 
                        }
2848
 
                }
2849
 
        }
2850
 
        res->rb_count = rollbp;
2851
 
        if (0 == rollbp)
2852
 
        {
2853
 
                free(rollback);
2854
 
                res->rollback = NULL;
2855
 
                res->rb_alloc = 0;
2856
 
        }
2857
 
}
2858
 
 
2859
 
void    ProcessRollback(ConnectionClass *conn, BOOL undo, BOOL partial) 
2860
 
{
2861
 
        int     i;
2862
 
        StatementClass  *stmt;
2863
 
        QResultClass    *res;
2864
 
 
2865
 
        for (i = 0; i < conn->num_stmts; i++)
2866
 
        {
2867
 
                if (stmt = conn->stmts[i], !stmt)
2868
 
                        continue;
2869
 
                for (res = SC_get_Result(stmt); res; res = res->next)
2870
 
                {
2871
 
                        if (undo)
2872
 
                                UndoRollback(stmt, res, partial);
2873
 
                        else
2874
 
                                DiscardRollback(stmt, res);
2875
 
                }
2876
 
        }
2877
 
}
2878
 
 
2879
 
 
2880
 
#define LATEST_TUPLE_LOAD       1L
2881
 
#define USE_INSERTED_TID        (1L << 1)
2882
 
static QResultClass *
2883
 
positioned_load(StatementClass *stmt, UInt4 flag, const UInt4 *oidint, const char *tidval)
2884
 
{
2885
 
        CSTR    func = "positioned_load";
2886
 
        CSTR    andqual = " and ";
2887
 
        QResultClass *qres = NULL;
2888
 
        char    *selstr, oideqstr[256];
2889
 
        BOOL    latest = ((flag & LATEST_TUPLE_LOAD) != 0);
2890
 
        size_t  len;
2891
 
        TABLE_INFO      *ti = stmt->ti[0];
2892
 
        const char *bestitem = GET_NAME(ti->bestitem);
2893
 
        const char *bestqual = GET_NAME(ti->bestqual);
2894
 
 
2895
 
inolog("%s bestitem=%s bestqual=%s\n", func, SAFE_NAME(ti->bestitem), SAFE_NAME(ti->bestqual));
2896
 
        if (!bestitem || !oidint)
2897
 
                *oideqstr = '\0';
2898
 
        else
2899
 
        {
2900
 
                /*snprintf(oideqstr, sizeof(oideqstr), " and \"%s\" = %u", bestitem, oid);*/
2901
 
                strcpy(oideqstr, andqual);
2902
 
                sprintf(oideqstr + strlen(andqual), bestqual, *oidint);
2903
 
        }
2904
 
        len = strlen(stmt->load_statement);
2905
 
        len += strlen(oideqstr);
2906
 
        if (tidval)
2907
 
                len += 100;
2908
 
        else if ((flag & USE_INSERTED_TID) != 0)
2909
 
                len += 50;
2910
 
        else
2911
 
                len += 20;
2912
 
        selstr = malloc(len);
2913
 
        if (tidval)
2914
 
        {
2915
 
                if (latest)
2916
 
                {
2917
 
                        if (NAME_IS_VALID(ti->schema_name))
2918
 
                                snprintf(selstr, len, "%s where ctid = currtid2('\"%s\".\"%s\"', '%s') %s",
2919
 
                                stmt->load_statement, SAFE_NAME(ti->schema_name),
2920
 
                                SAFE_NAME(ti->table_name), tidval, oideqstr);
2921
 
                        else
2922
 
                                snprintf(selstr, len, "%s where ctid = currtid2('%s', '%s') %s", stmt->load_statement, SAFE_NAME(ti->table_name), tidval, oideqstr);
2923
 
                }
2924
 
                else 
2925
 
                        snprintf(selstr, len, "%s where ctid = '%s' %s", stmt->load_statement, tidval, oideqstr); 
2926
 
        }
2927
 
        else if ((flag & USE_INSERTED_TID) != 0)
2928
 
                snprintf(selstr, len, "%s where ctid = currtid(0, '(0,0)') %s", stmt->load_statement, oideqstr);
2929
 
        else if (bestitem && oidint)
2930
 
        {
2931
 
                /*snprintf(selstr, len, "%s where \"%s\" = %u", stmt->load_statement, bestitem, *oid);*/
2932
 
                snprintf(selstr, len, "%s where ", stmt->load_statement);
2933
 
                snprintf_add(selstr, len, bestqual, *oidint);
2934
 
        }
2935
 
        else
2936
 
        {
2937
 
                SC_set_error(stmt,STMT_INTERNAL_ERROR, "can't find the add and updating row because of the lack of oid", func);
2938
 
                goto cleanup;
2939
 
        } 
2940
 
 
2941
 
        mylog("selstr=%s\n", selstr);
2942
 
        qres = CC_send_query(SC_get_conn(stmt), selstr, NULL, 0, stmt);
2943
 
cleanup:
2944
 
        free(selstr);
2945
 
        return qres;
2946
 
}
2947
 
 
2948
 
static RETCODE
2949
 
SC_pos_reload_with_tid(StatementClass *stmt, SQLULEN global_ridx, UInt2 *count, Int4 logKind, const char *tid)
2950
 
{
2951
 
        CSTR            func = "SC_pos_reload";
2952
 
        int             res_cols;
2953
 
        UInt2           offset;
2954
 
        UInt2           rcnt;
2955
 
        SQLLEN          res_ridx, kres_ridx;
2956
 
        OID             oidint;
2957
 
        UInt4           blocknum;
2958
 
        QResultClass    *res, *qres;
2959
 
        IRDFields       *irdflds = SC_get_IRDF(stmt);
2960
 
        RETCODE         ret = SQL_ERROR;
2961
 
        char            tidval[32];
2962
 
        BOOL            use_ctid = TRUE, data_in_cache = TRUE, key_in_cache = TRUE;
2963
 
 
2964
 
        mylog("positioned load fi=%p ti=%p\n", irdflds->fi, stmt->ti);
2965
 
        rcnt = 0;
2966
 
        if (count)
2967
 
                *count = 0;
2968
 
        if (!(res = SC_get_Curres(stmt)))
2969
 
        {
2970
 
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in SC_pos_reload.", func);
2971
 
                return SQL_ERROR;
2972
 
        }
2973
 
        res_ridx = GIdx2CacheIdx(global_ridx, stmt, res);
2974
 
        if (res_ridx < 0 || res_ridx >= QR_get_num_cached_tuples(res))
2975
 
        {
2976
 
                data_in_cache = FALSE;
2977
 
                SC_set_error(stmt, STMT_ROW_OUT_OF_RANGE, "the target rows is out of the rowset", func);
2978
 
                return SQL_ERROR;
2979
 
        }
2980
 
        kres_ridx = GIdx2KResIdx(global_ridx, stmt, res);
2981
 
        if (kres_ridx < 0 || kres_ridx >= res->num_cached_keys)
2982
 
        {
2983
 
                key_in_cache = FALSE;
2984
 
                SC_set_error(stmt, STMT_ROW_OUT_OF_RANGE, "the target rows is out of the rowset", func);
2985
 
                return SQL_ERROR;
2986
 
        }
2987
 
        else if (0 != (res->keyset[kres_ridx].status & CURS_SELF_ADDING))
2988
 
        {
2989
 
                if (NULL == tid)
2990
 
                {
2991
 
                        use_ctid = FALSE;
2992
 
                        mylog("The tuple is currently being added and can't use ctid\n");
2993
 
                }
2994
 
        }       
2995
 
 
2996
 
        if (SC_update_not_ready(stmt))
2997
 
                parse_statement(stmt, TRUE);    /* not preferable */
2998
 
        if (!SC_is_updatable(stmt))
2999
 
        {
3000
 
                stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY;
3001
 
                SC_set_error(stmt, STMT_INVALID_OPTION_IDENTIFIER, "the statement is read-only", func);
3002
 
                return SQL_ERROR;
3003
 
        }
3004
 
        if (!(oidint = getOid(res, kres_ridx)))
3005
 
        {
3006
 
                if (!strcmp(SAFE_NAME(stmt->ti[0]->bestitem), OID_NAME))
3007
 
                {
3008
 
                        SC_set_error(stmt, STMT_ROW_VERSION_CHANGED, "the row was already deleted ?", func);
3009
 
                        return SQL_SUCCESS_WITH_INFO;
3010
 
                }
3011
 
        }
3012
 
        getTid(res, kres_ridx, &blocknum, &offset);
3013
 
        sprintf(tidval, "(%u, %u)", blocknum, offset);
3014
 
        res_cols = getNumResultCols(res);
3015
 
        if (tid)
3016
 
                qres = positioned_load(stmt, 0, &oidint, tid);
3017
 
        else
3018
 
                qres = positioned_load(stmt, use_ctid ? LATEST_TUPLE_LOAD : 0, &oidint, use_ctid ? tidval : NULL);
3019
 
        if (!QR_command_maybe_successful(qres))
3020
 
        {
3021
 
                ret = SQL_ERROR;
3022
 
                SC_replace_error_with_res(stmt, STMT_ERROR_TAKEN_FROM_BACKEND, "positioned_load failed", qres, TRUE);
3023
 
        }
3024
 
        else
3025
 
        {
3026
 
                TupleField *tuple_old, *tuple_new;
3027
 
                ConnectionClass *conn = SC_get_conn(stmt);
3028
 
 
3029
 
                rcnt = (UInt2) QR_get_num_cached_tuples(qres);
3030
 
                tuple_old = res->backend_tuples + res->num_fields * res_ridx;
3031
 
                if (0 != logKind && CC_is_in_trans(conn))
3032
 
                        AddRollback(stmt, res, global_ridx, res->keyset + kres_ridx, logKind);
3033
 
                if (rcnt == 1)
3034
 
                {
3035
 
                        int     effective_fields = res_cols;
3036
 
 
3037
 
                        QR_set_position(qres, 0);
3038
 
                        tuple_new = qres->tupleField;
3039
 
                        if (res->keyset && key_in_cache)
3040
 
                        {
3041
 
                                if (SQL_CURSOR_KEYSET_DRIVEN == stmt->options.cursor_type &&
3042
 
                                        strcmp(tuple_new[qres->num_fields - res->num_key_fields].value, tidval))
3043
 
                                        res->keyset[kres_ridx].status |= SQL_ROW_UPDATED;
3044
 
                                KeySetSet(tuple_new, qres->num_fields, res->num_key_fields, res->keyset + kres_ridx);
3045
 
                        }
3046
 
                        if (data_in_cache)
3047
 
                                MoveCachedRows(tuple_old, tuple_new, effective_fields, 1); 
3048
 
                        ret = SQL_SUCCESS;
3049
 
                }
3050
 
                else
3051
 
                {
3052
 
                        SC_set_error(stmt, STMT_ROW_VERSION_CHANGED, "the content was deleted after last fetch", func);
3053
 
                        ret = SQL_SUCCESS_WITH_INFO;
3054
 
                        if (stmt->options.cursor_type == SQL_CURSOR_KEYSET_DRIVEN)
3055
 
                        {
3056
 
                                res->keyset[kres_ridx].status |= SQL_ROW_DELETED;
3057
 
                        }
3058
 
                }
3059
 
        }
3060
 
        QR_Destructor(qres);
3061
 
        if (count)
3062
 
                *count = rcnt;
3063
 
        return ret;
3064
 
}
3065
 
 
3066
 
RETCODE
3067
 
SC_pos_reload(StatementClass *stmt, SQLULEN global_ridx, UInt2 *count, Int4 logKind)
3068
 
{
3069
 
        return SC_pos_reload_with_tid(stmt, global_ridx, count, logKind, NULL);
3070
 
}
3071
 
 
3072
 
static  const int       pre_fetch_count = 32;
3073
 
static SQLLEN LoadFromKeyset(StatementClass *stmt, QResultClass * res, int rows_per_fetch, SQLLEN limitrow)
3074
 
{
3075
 
        CSTR    func = "LoadFromKeyset";
3076
 
        ConnectionClass *conn = SC_get_conn(stmt);
3077
 
        SQLLEN  i;
3078
 
        int     j, rowc, rcnt = 0;
3079
 
        BOOL    prepare;
3080
 
        OID     oid;
3081
 
        UInt4   blocknum;
3082
 
        SQLLEN  kres_ridx;
3083
 
        UInt2   offset;
3084
 
        char    *qval = NULL, *sval = NULL;
3085
 
        int     keys_per_fetch = 10;
3086
 
 
3087
 
        prepare = PG_VERSION_GE(conn, 7.3);
3088
 
        for (i = SC_get_rowset_start(stmt), kres_ridx = GIdx2KResIdx(i, stmt, res), rowc = 0;; i++)
3089
 
        {
3090
 
                if (i >= limitrow)
3091
 
                {
3092
 
                        if (!rowc)
3093
 
                                break;
3094
 
                        if (res->reload_count > 0)
3095
 
                        {
3096
 
                                for (j = rowc; j < keys_per_fetch; j++)
3097
 
                                {
3098
 
                                        if (j)
3099
 
                                                strcpy(sval, ",NULL");
3100
 
                                        else
3101
 
                                                strcpy(sval, "NULL");
3102
 
                                        sval = strchr(sval, '\0');
3103
 
                                }
3104
 
                        }
3105
 
                        rowc = -1; /* end of loop */
3106
 
                }
3107
 
                if (rowc < 0 || rowc >= keys_per_fetch)
3108
 
                {
3109
 
                        QResultClass    *qres;
3110
 
 
3111
 
                        strcpy(sval, ")");
3112
 
                        qres = CC_send_query(conn, qval, NULL, CREATE_KEYSET, stmt);
3113
 
                        if (QR_command_maybe_successful(qres))
3114
 
                        {
3115
 
                                SQLLEN          j, k, l;
3116
 
                                Int2            m;
3117
 
                                TupleField      *tuple, *tuplew;
3118
 
 
3119
 
                                for (j = 0; j < QR_get_num_total_read(qres); j++)
3120
 
                                {
3121
 
                                        oid = getOid(qres, j); 
3122
 
                                        getTid(qres, j, &blocknum, &offset);
3123
 
                                        for (k = SC_get_rowset_start(stmt); k < limitrow; k++)
3124
 
                                        {
3125
 
                                                if (oid == getOid(res, k))
3126
 
                                                {
3127
 
                                                        l = GIdx2CacheIdx(k, stmt, res);
3128
 
                                                        tuple = res->backend_tuples + res->num_fields * l;
3129
 
                                                        tuplew = qres->backend_tuples + qres->num_fields * j;
3130
 
                                                        for (m = 0; m < res->num_fields; m++, tuple++, tuplew++)
3131
 
                                                        {
3132
 
                                                                if (tuple->len > 0 && tuple->value)
3133
 
                                                                        free(tuple->value);
3134
 
                                                                tuple->value = tuplew->value;
3135
 
                                                                tuple->len = tuplew->len;
3136
 
                                                                tuplew->value = NULL;
3137
 
                                                                tuplew->len = -1;
3138
 
                                                        }
3139
 
                                                        res->keyset[k].status &= ~CURS_NEEDS_REREAD;
3140
 
                                                        break;
3141
 
                                                }
3142
 
                                        }
3143
 
                                }
3144
 
                        }
3145
 
                        else
3146
 
                        {
3147
 
                                SC_set_error(stmt, STMT_EXEC_ERROR, "Data Load Error", func);
3148
 
                                rcnt = -1;
3149
 
                                QR_Destructor(qres);
3150
 
                                break;
3151
 
                        }
3152
 
                        QR_Destructor(qres);
3153
 
                        if (rowc < 0)
3154
 
                                break;
3155
 
                        rowc = 0;
3156
 
                }
3157
 
                if (!rowc)
3158
 
                {
3159
 
                        size_t lodlen = 0;
3160
 
 
3161
 
                        if (!qval)
3162
 
                        {
3163
 
                                size_t  allen;
3164
 
 
3165
 
                                if (prepare)
3166
 
                                {
3167
 
                                        if (res->reload_count > 0)
3168
 
                                                keys_per_fetch = res->reload_count;
3169
 
                                        else
3170
 
                                        {
3171
 
                                                char    planname[32];
3172
 
                                                int     j;
3173
 
                                                QResultClass    *qres;
3174
 
 
3175
 
                                                if (rows_per_fetch >= pre_fetch_count * 2)
3176
 
                                                        keys_per_fetch = pre_fetch_count;
3177
 
                                                else
3178
 
                                                        keys_per_fetch = rows_per_fetch;
3179
 
                                                if (!keys_per_fetch)
3180
 
                                                        keys_per_fetch = 2;
3181
 
                                                lodlen = strlen(stmt->load_statement);
3182
 
                                                sprintf(planname, "_KEYSET_%p", res);
3183
 
                                                allen = 8 + strlen(planname) +
3184
 
                                                        3 + 4 * keys_per_fetch + 1
3185
 
                                                        + 1 + 2 + lodlen + 20 +
3186
 
                                                        4 * keys_per_fetch + 1;
3187
 
                                                SC_MALLOC_return_with_error(qval, char, allen,
3188
 
                                                        stmt, "Couldn't alloc qval", -1);
3189
 
                                                sprintf(qval, "PREPARE \"%s\"", planname);
3190
 
                                                sval = strchr(qval, '\0');
3191
 
                                                for (j = 0; j < keys_per_fetch; j++)
3192
 
                                                {
3193
 
                                                        if (j == 0)
3194
 
                                                                strcpy(sval, "(tid");
3195
 
                                                        else 
3196
 
                                                                strcpy(sval, ",tid");
3197
 
                                                        sval = strchr(sval, '\0');
3198
 
                                                }
3199
 
                                                sprintf(sval, ") as %s where ctid in ", stmt->load_statement);
3200
 
                                                sval = strchr(sval, '\0'); 
3201
 
                                                for (j = 0; j < keys_per_fetch; j++)
3202
 
                                                {
3203
 
                                                        if (j == 0)
3204
 
                                                                strcpy(sval, "($1");
3205
 
                                                        else 
3206
 
                                                                sprintf(sval, ",$%d", j + 1);
3207
 
                                                        sval = strchr(sval, '\0');
3208
 
                                                }
3209
 
                                                strcpy(sval, ")");
3210
 
                                                qres = CC_send_query(conn, qval, NULL, 0, stmt);
3211
 
                                                if (QR_command_maybe_successful(qres))
3212
 
                                                {
3213
 
                                                        res->reload_count = keys_per_fetch;
3214
 
                                                }
3215
 
                                                else
3216
 
                                                {
3217
 
                                                        SC_set_error(stmt, STMT_EXEC_ERROR, "Prepare for Data Load Error", func);
3218
 
                                                        rcnt = -1;
3219
 
                                                        QR_Destructor(qres);
3220
 
                                                        break;
3221
 
                                                }
3222
 
                                                QR_Destructor(qres);
3223
 
                                        }
3224
 
                                        allen = 25 + 23 * keys_per_fetch;
3225
 
                                }
3226
 
                                else
3227
 
                                {
3228
 
                                        keys_per_fetch = pre_fetch_count;
3229
 
                                        lodlen = strlen(stmt->load_statement);
3230
 
                                        allen = lodlen + 20 + 23 * keys_per_fetch;
3231
 
                                }
3232
 
                                SC_REALLOC_return_with_error(qval, char, allen,
3233
 
                                        stmt, "Couldn't alloc qval", -1);
3234
 
                        }
3235
 
                        if (res->reload_count > 0)
3236
 
                        {
3237
 
                                sprintf(qval, "EXECUTE \"_KEYSET_%p\"(", res);
3238
 
                                sval = qval;
3239
 
                        }
3240
 
                        else
3241
 
                        {
3242
 
                                memcpy(qval, stmt->load_statement, lodlen);
3243
 
                                sval = qval + lodlen;
3244
 
                                sval[0]= '\0';
3245
 
                                strcpy(sval, " where ctid in (");
3246
 
                        }
3247
 
                        sval = strchr(sval, '\0');
3248
 
                }
3249
 
                if (0 != (res->keyset[kres_ridx].status & CURS_NEEDS_REREAD))
3250
 
                {
3251
 
                        getTid(res, i, &blocknum, &offset);
3252
 
                        if (rowc)
3253
 
                                sprintf(sval, ",'(%u,%u)'", blocknum, offset);
3254
 
                        else
3255
 
                                sprintf(sval, "'(%u,%u)'", blocknum, offset);
3256
 
                        sval = strchr(sval, '\0');
3257
 
                        rowc++;
3258
 
                        rcnt++;
3259
 
                }
3260
 
        }
3261
 
        if (qval)
3262
 
                free(qval);
3263
 
        return rcnt;
3264
 
}
3265
 
 
3266
 
static RETCODE  SQL_API
3267
 
SC_pos_reload_needed(StatementClass *stmt, SQLULEN req_size, UDWORD flag)
3268
 
{
3269
 
        CSTR    func = "SC_pos_reload_needed";
3270
 
        Int4            req_rows_size;
3271
 
        SQLLEN          i, limitrow;
3272
 
        UInt2           qcount;
3273
 
        QResultClass    *res;
3274
 
        RETCODE         ret = SQL_ERROR;
3275
 
        SQLLEN          kres_ridx, rowc;
3276
 
        Int4            rows_per_fetch;
3277
 
        BOOL            create_from_scratch = (0 != flag);
3278
 
 
3279
 
        mylog("%s\n", func);
3280
 
        if (!(res = SC_get_Curres(stmt)))
3281
 
        {
3282
 
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in SC_pos_reload_needed.", func);
3283
 
                return SQL_ERROR;
3284
 
        }
3285
 
        if (SC_update_not_ready(stmt))
3286
 
                parse_statement(stmt, TRUE);    /* not preferable */
3287
 
        if (!SC_is_updatable(stmt))
3288
 
        {
3289
 
                stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY;
3290
 
                SC_set_error(stmt, STMT_INVALID_OPTION_IDENTIFIER, "the statement is read-only", func);
3291
 
                return SQL_ERROR;
3292
 
        }
3293
 
        rows_per_fetch = 0;
3294
 
        req_rows_size = QR_get_reqsize(res);
3295
 
        if (req_size > req_rows_size)
3296
 
                req_rows_size = (UInt4) req_size;
3297
 
        if (create_from_scratch)
3298
 
        {
3299
 
                rows_per_fetch = ((pre_fetch_count - 1) / req_rows_size + 1) * req_rows_size;
3300
 
                limitrow = RowIdx2GIdx(rows_per_fetch, stmt);
3301
 
        }
3302
 
        else
3303
 
        {
3304
 
                limitrow = RowIdx2GIdx(req_rows_size, stmt);
3305
 
        }
3306
 
        if (limitrow > res->num_cached_keys)
3307
 
                limitrow = res->num_cached_keys;
3308
 
        if (create_from_scratch ||
3309
 
            !res->dataFilled)
3310
 
        {
3311
 
                ClearCachedRows(res->backend_tuples, res->num_fields, res->num_cached_rows);
3312
 
                res->dataFilled = FALSE;
3313
 
        }
3314
 
        if (!res->dataFilled)
3315
 
        {
3316
 
                SQLLEN  brows = GIdx2RowIdx(limitrow, stmt);
3317
 
                if (brows > res->count_backend_allocated)
3318
 
                {
3319
 
                        QR_REALLOC_return_with_error(res->backend_tuples, TupleField, sizeof(TupleField) * res->num_fields * brows, res, "pos_reload_needed failed", SQL_ERROR);
3320
 
                        res->count_backend_allocated = brows;
3321
 
                }
3322
 
                if (brows > 0)
3323
 
                        memset(res->backend_tuples, 0, sizeof(TupleField) * res->num_fields * brows);
3324
 
                QR_set_num_cached_rows(res, brows);
3325
 
                QR_set_rowstart_in_cache(res, 0);
3326
 
                if (SQL_RD_ON != stmt->options.retrieve_data)
3327
 
                        return SQL_SUCCESS;
3328
 
                for (i = SC_get_rowset_start(stmt), kres_ridx = GIdx2KResIdx(i, stmt,res); i < limitrow; i++, kres_ridx++)
3329
 
                {
3330
 
                        if (0 == (res->keyset[kres_ridx].status & (CURS_SELF_DELETING | CURS_SELF_DELETED | CURS_OTHER_DELETED)))
3331
 
                                res->keyset[kres_ridx].status |= CURS_NEEDS_REREAD;
3332
 
                }
3333
 
        }
3334
 
        if (rowc = LoadFromKeyset(stmt, res, rows_per_fetch, limitrow), rowc < 0)
3335
 
        {
3336
 
                return SQL_ERROR;
3337
 
        }
3338
 
        for (i = SC_get_rowset_start(stmt), kres_ridx = GIdx2KResIdx(i, stmt, res); i < limitrow; i++)
3339
 
        {
3340
 
                if (0 != (res->keyset[kres_ridx].status & CURS_NEEDS_REREAD))
3341
 
                {
3342
 
                        ret = SC_pos_reload(stmt, i, &qcount, 0);
3343
 
                        if (SQL_ERROR == ret)
3344
 
                        {
3345
 
                                break;
3346
 
                        }
3347
 
                        if (SQL_ROW_DELETED == (res->keyset[kres_ridx].status & KEYSET_INFO_PUBLIC))
3348
 
                        {
3349
 
                                res->keyset[kres_ridx].status |= CURS_OTHER_DELETED;
3350
 
                        }
3351
 
                        res->keyset[kres_ridx].status &= ~CURS_NEEDS_REREAD;
3352
 
                }
3353
 
        }
3354
 
        res->dataFilled = TRUE;
3355
 
        return ret;
3356
 
}
3357
 
 
3358
 
static RETCODE  SQL_API
3359
 
SC_pos_newload(StatementClass *stmt, const UInt4 *oidint, BOOL tidRef, const char *tidval)
3360
 
{
3361
 
        CSTR    func = "SC_pos_newload";
3362
 
        int                     i;
3363
 
        QResultClass *res, *qres;
3364
 
        RETCODE         ret = SQL_ERROR;
3365
 
 
3366
 
        mylog("positioned new ti=%p\n", stmt->ti);
3367
 
        if (!(res = SC_get_Curres(stmt)))
3368
 
        {
3369
 
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in SC_pos_newload.", func);
3370
 
                return SQL_ERROR;
3371
 
        }
3372
 
        if (SC_update_not_ready(stmt))
3373
 
                parse_statement(stmt, TRUE);    /* not preferable */
3374
 
        if (!SC_is_updatable(stmt))
3375
 
        {
3376
 
                stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY;
3377
 
                SC_set_error(stmt, STMT_INVALID_OPTION_IDENTIFIER, "the statement is read-only", func);
3378
 
                return SQL_ERROR;
3379
 
        }
3380
 
        qres = positioned_load(stmt, (tidRef && NULL == tidval) ? USE_INSERTED_TID : 0, oidint, tidRef ? tidval : NULL);
3381
 
        if (!qres || !QR_command_maybe_successful(qres))
3382
 
        {
3383
 
                SC_set_error(stmt, STMT_ERROR_TAKEN_FROM_BACKEND, "positioned_load in pos_newload failed", func);
3384
 
        }
3385
 
        else
3386
 
        {
3387
 
                SQLLEN  count = QR_get_num_cached_tuples(qres);
3388
 
 
3389
 
                QR_set_position(qres, 0);
3390
 
                if (count == 1)
3391
 
                {
3392
 
                        int     effective_fields = res->num_fields;
3393
 
                        ssize_t tuple_size;
3394
 
                        SQLLEN  num_total_rows, num_cached_rows, kres_ridx;
3395
 
                        BOOL    appendKey = FALSE, appendData = FALSE;
3396
 
                        TupleField *tuple_old, *tuple_new;
3397
 
 
3398
 
                        tuple_new = qres->tupleField;
3399
 
                        num_total_rows = QR_get_num_total_tuples(res);
3400
 
 
3401
 
                        AddAdded(stmt, res, num_total_rows, tuple_new);
3402
 
                        num_cached_rows = QR_get_num_cached_tuples(res);
3403
 
                        kres_ridx = GIdx2KResIdx(num_total_rows, stmt, res);
3404
 
                        if (QR_haskeyset(res))
3405
 
                        {       if (!QR_get_cursor(res))
3406
 
                                {
3407
 
                                        appendKey = TRUE;
3408
 
                                        if (num_total_rows == CacheIdx2GIdx(num_cached_rows, stmt, res))
3409
 
                                                appendData = TRUE;
3410
 
                                        else
3411
 
                                        {
3412
 
inolog("total %d <> backend %d - base %d + start %d cursor_type=%d\n", 
3413
 
num_total_rows, num_cached_rows,
3414
 
QR_get_rowstart_in_cache(res), SC_get_rowset_start(stmt), stmt->options.cursor_type);
3415
 
                                        }
3416
 
                                }
3417
 
                                else if (kres_ridx >= 0 && kres_ridx < res->cache_size)
3418
 
                                {
3419
 
                                        appendKey = TRUE;
3420
 
                                        appendData = TRUE;
3421
 
                                }
3422
 
                        }
3423
 
                        if (appendKey)
3424
 
                        {
3425
 
                                if (res->num_cached_keys >= res->count_keyset_allocated)
3426
 
                                {
3427
 
                                        if (!res->count_keyset_allocated)
3428
 
                                                tuple_size = TUPLE_MALLOC_INC;
3429
 
                                        else
3430
 
                                                tuple_size = res->count_keyset_allocated * 2;
3431
 
                                        QR_REALLOC_return_with_error(res->keyset, KeySet, sizeof(KeySet) * tuple_size, res, "pos_newload failed", SQL_ERROR);   
3432
 
                                        res->count_keyset_allocated = tuple_size;
3433
 
                                }
3434
 
                                KeySetSet(tuple_new, qres->num_fields, res->num_key_fields, res->keyset + kres_ridx);
3435
 
                                res->num_cached_keys++;
3436
 
                        }
3437
 
                        if (appendData)
3438
 
                        {
3439
 
inolog("total %d == backend %d - base %d + start %d cursor_type=%d\n", 
3440
 
num_total_rows, num_cached_rows,
3441
 
QR_get_rowstart_in_cache(res), SC_get_rowset_start(stmt), stmt->options.cursor_type);
3442
 
                                if (num_cached_rows >= res->count_backend_allocated)
3443
 
                                {
3444
 
                                        if (!res->count_backend_allocated)
3445
 
                                                tuple_size = TUPLE_MALLOC_INC;
3446
 
                                        else
3447
 
                                                tuple_size = res->count_backend_allocated * 2;
3448
 
                                        QR_REALLOC_return_with_error(res->backend_tuples, TupleField, res->num_fields * sizeof(TupleField) * tuple_size, res, "SC_pos_newload failed", SQL_ERROR);
3449
 
                                        /*
3450
 
                                        res->backend_tuples = (TupleField *) realloc(
3451
 
                                                res->backend_tuples,
3452
 
                                                res->num_fields * sizeof(TupleField) * tuple_size);
3453
 
                                        if (!res->backend_tuples)
3454
 
                                        {
3455
 
                                                SC_set_error(stmt, QR_set_rstatus(res, PORES_FATAL_ERROR), "Out of memory while reading tuples.", func);
3456
 
                                                QR_Destructor(qres);
3457
 
                                                return SQL_ERROR;
3458
 
                                        }
3459
 
                                        */
3460
 
                                        res->count_backend_allocated = tuple_size;
3461
 
                                }
3462
 
                                tuple_old = res->backend_tuples + res->num_fields * num_cached_rows;
3463
 
                                for (i = 0; i < effective_fields; i++)
3464
 
                                {
3465
 
                                        tuple_old[i].len = tuple_new[i].len;
3466
 
                                        tuple_new[i].len = -1;
3467
 
                                        tuple_old[i].value = tuple_new[i].value;
3468
 
                                        tuple_new[i].value = NULL;
3469
 
                                }
3470
 
                                res->num_cached_rows++;
3471
 
                        }
3472
 
                        ret = SQL_SUCCESS;
3473
 
                }
3474
 
                else if (0 == count)
3475
 
                        ret = SQL_NO_DATA_FOUND;
3476
 
                else
3477
 
                {
3478
 
                        SC_set_error(stmt, STMT_ROW_VERSION_CHANGED, "the driver cound't identify inserted rows", func);
3479
 
                        ret = SQL_ERROR;
3480
 
                }
3481
 
                /* stmt->currTuple = SC_get_rowset_start(stmt) + ridx; */
3482
 
        }
3483
 
        QR_Destructor(qres);
3484
 
        return ret;
3485
 
}
3486
 
 
3487
 
static RETCODE SQL_API
3488
 
irow_update(RETCODE ret, StatementClass *stmt, StatementClass *ustmt, SQLSETPOSIROW irow, SQLULEN global_ridx)
3489
 
{
3490
 
        CSTR    func = "irow_update";
3491
 
 
3492
 
        if (ret != SQL_ERROR)
3493
 
        {
3494
 
                int                     updcnt;
3495
 
                QResultClass            *tres = SC_get_Curres(ustmt);
3496
 
                const char *cmdstr = QR_get_command(tres);
3497
 
 
3498
 
                if (cmdstr &&
3499
 
                        sscanf(cmdstr, "UPDATE %d", &updcnt) == 1)
3500
 
                {
3501
 
                        if (updcnt == 1)
3502
 
                        {
3503
 
                                const char *tidval = NULL;
3504
 
 
3505
 
                                if (NULL != tres->backend_tuples &&
3506
 
                                    1 == QR_get_num_cached_tuples(tres))
3507
 
                                        tidval = QR_get_value_backend_text(tres, 0, 0);
3508
 
                                ret = SC_pos_reload_with_tid(stmt, global_ridx, (UInt2 *) 0, SQL_UPDATE, tidval);
3509
 
                                if (SQL_ERROR != ret)
3510
 
                                        AddUpdated(stmt, global_ridx);
3511
 
                        }
3512
 
                        else if (updcnt == 0)
3513
 
                        {
3514
 
                                SC_set_error(stmt, STMT_ROW_VERSION_CHANGED, "the content was changed before updation", func);
3515
 
                                ret = SQL_ERROR;
3516
 
                                if (stmt->options.cursor_type == SQL_CURSOR_KEYSET_DRIVEN)
3517
 
                                        SC_pos_reload(stmt, global_ridx, (UInt2 *) 0, 0);
3518
 
                        }
3519
 
                        else
3520
 
                                ret = SQL_ERROR;
3521
 
                }
3522
 
                else
3523
 
                        ret = SQL_ERROR;
3524
 
                if (ret == SQL_ERROR && SC_get_errornumber(stmt) == 0)
3525
 
                {
3526
 
                        SC_set_error(stmt, STMT_ERROR_TAKEN_FROM_BACKEND, "SetPos update return error", func);
3527
 
                }
3528
 
        }
3529
 
        return ret;
3530
 
}
3531
 
 
3532
 
/* SQL_NEED_DATA callback for SC_pos_update */
3533
 
typedef struct
3534
 
{
3535
 
        BOOL            updyes;
3536
 
        QResultClass    *res;
3537
 
        StatementClass  *stmt, *qstmt;
3538
 
        IRDFields       *irdflds;
3539
 
        SQLSETPOSIROW           irow;
3540
 
        SQLULEN         global_ridx;
3541
 
}       pup_cdata;
3542
 
static RETCODE
3543
 
pos_update_callback(RETCODE retcode, void *para)
3544
 
{
3545
 
        CSTR    func = "pos_update_callback";
3546
 
        RETCODE ret = retcode;
3547
 
        pup_cdata *s = (pup_cdata *) para;
3548
 
        SQLLEN  kres_ridx;
3549
 
 
3550
 
        if (s->updyes)
3551
 
        {
3552
 
                mylog("pos_update_callback in\n");
3553
 
                ret = irow_update(ret, s->stmt, s->qstmt, s->irow, s->global_ridx);
3554
 
inolog("irow_update ret=%d,%d\n", ret, SC_get_errornumber(s->qstmt));
3555
 
                if (ret != SQL_SUCCESS)
3556
 
                        SC_error_copy(s->stmt, s->qstmt, TRUE);
3557
 
                PGAPI_FreeStmt(s->qstmt, SQL_DROP);
3558
 
                s->qstmt = NULL;
3559
 
        }
3560
 
        s->updyes = FALSE;
3561
 
        kres_ridx = GIdx2KResIdx(s->global_ridx, s->stmt, s->res);
3562
 
        if (kres_ridx < 0 || kres_ridx >= s->res->num_cached_keys)
3563
 
        {
3564
 
                SC_set_error(s->stmt, STMT_ROW_OUT_OF_RANGE, "the target rows is out of the rowset", func);
3565
 
inolog("gidx=%d num_keys=%d kresidx=%d\n", s->global_ridx, s->res->num_cached_keys, kres_ridx);
3566
 
                return SQL_ERROR;
3567
 
        }
3568
 
        if (SQL_SUCCESS == ret && s->res->keyset)
3569
 
        {
3570
 
                ConnectionClass *conn = SC_get_conn(s->stmt);
3571
 
 
3572
 
                if (CC_is_in_trans(conn))
3573
 
                {
3574
 
                        s->res->keyset[kres_ridx].status |= (SQL_ROW_UPDATED  | CURS_SELF_UPDATING);
3575
 
                }
3576
 
                else
3577
 
                        s->res->keyset[kres_ridx].status |= (SQL_ROW_UPDATED  | CURS_SELF_UPDATED);
3578
 
        }
3579
 
#if (ODBCVER >= 0x0300)
3580
 
        if (s->irdflds->rowStatusArray)
3581
 
        {
3582
 
                switch (ret)
3583
 
                {
3584
 
                        case SQL_SUCCESS:
3585
 
                                s->irdflds->rowStatusArray[s->irow] = SQL_ROW_UPDATED;
3586
 
                                break;
3587
 
                        default:
3588
 
                                s->irdflds->rowStatusArray[s->irow] = ret;
3589
 
                }
3590
 
        }
3591
 
#endif /* ODBCVER */
3592
 
 
3593
 
        return ret;
3594
 
}
3595
 
RETCODE
3596
 
SC_pos_update(StatementClass *stmt,
3597
 
                          SQLSETPOSIROW irow, SQLULEN global_ridx)
3598
 
{
3599
 
        CSTR    func = "SC_pos_update";
3600
 
        int                     i,
3601
 
                                num_cols,
3602
 
                                upd_cols;
3603
 
        pup_cdata       s;
3604
 
        ConnectionClass *conn;
3605
 
        ARDFields       *opts = SC_get_ARDF(stmt);
3606
 
        BindInfoClass *bindings = opts->bindings;
3607
 
        TABLE_INFO      *ti;
3608
 
        FIELD_INFO      **fi;
3609
 
        char            updstr[4096];
3610
 
        RETCODE         ret;
3611
 
        OID     oid;
3612
 
        UInt4   blocknum;
3613
 
        UInt2   pgoffset;
3614
 
        SQLLEN  offset;
3615
 
        SQLLEN  *used, kres_ridx;
3616
 
        Int4    bind_size = opts->bind_size;
3617
 
 
3618
 
        s.stmt = stmt;
3619
 
        s.irow = irow;
3620
 
        s.global_ridx = global_ridx;
3621
 
        s.irdflds = SC_get_IRDF(s.stmt);
3622
 
        fi = s.irdflds->fi;
3623
 
        if (!(s.res = SC_get_Curres(s.stmt)))
3624
 
        {
3625
 
                SC_set_error(s.stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in SC_pos_update.", func);
3626
 
                return SQL_ERROR;
3627
 
        }
3628
 
        mylog("POS UPDATE %d+%d fi=%p ti=%p\n", s.irow, QR_get_rowstart_in_cache(s.res), fi, s.stmt->ti);
3629
 
        if (SC_update_not_ready(stmt))
3630
 
                parse_statement(s.stmt, TRUE);  /* not preferable */
3631
 
        if (!SC_is_updatable(s.stmt))
3632
 
        {
3633
 
                s.stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY;
3634
 
                SC_set_error(s.stmt, STMT_INVALID_OPTION_IDENTIFIER, "the statement is read-only", func);
3635
 
                return SQL_ERROR;
3636
 
        }
3637
 
        kres_ridx = GIdx2KResIdx(s.global_ridx, s.stmt, s.res);
3638
 
        if (kres_ridx < 0 || kres_ridx >= s.res->num_cached_keys)
3639
 
        {
3640
 
                SC_set_error(s.stmt, STMT_ROW_OUT_OF_RANGE, "the target rows is out of the rowset", func);
3641
 
                return SQL_ERROR;
3642
 
        }
3643
 
        if (!(oid = getOid(s.res, kres_ridx)))
3644
 
        {
3645
 
                if (!strcmp(SAFE_NAME(stmt->ti[0]->bestitem), OID_NAME))
3646
 
                {
3647
 
                        SC_set_error(stmt, STMT_ROW_VERSION_CHANGED, "the row was already deleted ?", func);
3648
 
                        return SQL_ERROR;
3649
 
                }
3650
 
        }
3651
 
        getTid(s.res, kres_ridx, &blocknum, &pgoffset);
3652
 
 
3653
 
        ti = s.stmt->ti[0];
3654
 
        if (NAME_IS_VALID(ti->schema_name))
3655
 
                sprintf(updstr, "update \"%s\".\"%s\" set", SAFE_NAME(ti->schema_name), SAFE_NAME(ti->table_name));
3656
 
        else
3657
 
                sprintf(updstr, "update \"%s\" set", SAFE_NAME(ti->table_name));
3658
 
        num_cols = s.irdflds->nfields;
3659
 
        offset = opts->row_offset_ptr ? *opts->row_offset_ptr : 0;
3660
 
        for (i = upd_cols = 0; i < num_cols; i++)
3661
 
        {
3662
 
                if (used = bindings[i].used, used != NULL)
3663
 
                {
3664
 
                        used = LENADDR_SHIFT(used, offset);
3665
 
                        if (bind_size > 0)
3666
 
                                used = LENADDR_SHIFT(used, bind_size * s.irow);
3667
 
                        else    
3668
 
                                used = LENADDR_SHIFT(used, s.irow * sizeof(SQLLEN)); 
3669
 
                        mylog("%d used=%d,%p\n", i, *used, used);
3670
 
                        if (*used != SQL_IGNORE && fi[i]->updatable)
3671
 
                        {
3672
 
                                if (upd_cols)
3673
 
                                        sprintf(updstr, "%s, \"%s\" = ?", updstr, GET_NAME(fi[i]->column_name));
3674
 
                                else
3675
 
                                        sprintf(updstr, "%s \"%s\" = ?", updstr, GET_NAME(fi[i]->column_name));
3676
 
                                upd_cols++;
3677
 
                        }
3678
 
                }
3679
 
                else
3680
 
                        mylog("%d null bind\n", i);
3681
 
        }
3682
 
        conn = SC_get_conn(s.stmt);
3683
 
        s.updyes = FALSE;
3684
 
        if (upd_cols > 0)
3685
 
        {
3686
 
                HSTMT           hstmt;
3687
 
                int                     j;
3688
 
                ConnInfo        *ci = &(conn->connInfo);
3689
 
                APDFields       *apdopts;
3690
 
                IPDFields       *ipdopts;
3691
 
                OID             fieldtype = 0;
3692
 
                const char *bestitem = GET_NAME(ti->bestitem);
3693
 
                const char *bestqual = GET_NAME(ti->bestqual);
3694
 
 
3695
 
                sprintf(updstr, "%s where ctid = '(%u, %u)'", updstr,
3696
 
                                blocknum, pgoffset);
3697
 
                if (bestitem)
3698
 
                {
3699
 
                        /*sprintf(updstr, "%s and \"%s\" = %u", updstr, bestitem, oid);*/
3700
 
                        strcat(updstr, " and ");
3701
 
                        sprintf(updstr + strlen(updstr), bestqual, oid);
3702
 
                }
3703
 
                if (PG_VERSION_GE(conn, 8.2))
3704
 
                        strcat(updstr, " returning ctid");
3705
 
                mylog("updstr=%s\n", updstr);
3706
 
                if (PGAPI_AllocStmt(conn, &hstmt, 0) != SQL_SUCCESS)
3707
 
                {
3708
 
                        SC_set_error(s.stmt, STMT_NO_MEMORY_ERROR, "internal AllocStmt error", func);
3709
 
                        return SQL_ERROR;
3710
 
                }
3711
 
                s.qstmt = (StatementClass *) hstmt;
3712
 
                apdopts = SC_get_APDF(s.qstmt);
3713
 
                apdopts->param_bind_type = opts->bind_size;
3714
 
                apdopts->param_offset_ptr = opts->row_offset_ptr;
3715
 
                ipdopts = SC_get_IPDF(s.qstmt);
3716
 
                SC_set_delegate(s.stmt, s.qstmt);
3717
 
                extend_iparameter_bindings(ipdopts, num_cols);
3718
 
                for (i = j = 0; i < num_cols; i++)
3719
 
                {
3720
 
                        if (used = bindings[i].used, used != NULL)
3721
 
                        {
3722
 
                                used = LENADDR_SHIFT(used, offset);
3723
 
                                if (bind_size > 0)
3724
 
                                        used = LENADDR_SHIFT(used, bind_size * s.irow);
3725
 
                                else
3726
 
                                        used = LENADDR_SHIFT(used, s.irow * sizeof(SQLLEN));
3727
 
                                mylog("%d used=%d\n", i, *used);
3728
 
                                if (*used != SQL_IGNORE && fi[i]->updatable)
3729
 
                                {
3730
 
                                        /* fieldtype = QR_get_field_type(s.res, i); */
3731
 
                                        fieldtype = getEffectiveOid(conn, fi[i]);
3732
 
                                        PIC_set_pgtype(ipdopts->parameters[j], fieldtype);
3733
 
                                        PGAPI_BindParameter(hstmt,
3734
 
                                                (SQLUSMALLINT) ++j,
3735
 
                                                SQL_PARAM_INPUT,
3736
 
                                                bindings[i].returntype,
3737
 
                                                pgtype_to_concise_type(s.stmt, fieldtype, i),
3738
 
                                                                                                                                fi[i]->column_size > 0 ? fi[i]->column_size : pgtype_column_size(s.stmt, fieldtype, i, ci->drivers.unknown_sizes),
3739
 
                                                (SQLSMALLINT) fi[i]->decimal_digits,
3740
 
                                                bindings[i].buffer,
3741
 
                                                bindings[i].buflen,
3742
 
                                                bindings[i].used);
3743
 
                                }
3744
 
                        }
3745
 
                }
3746
 
                s.qstmt->exec_start_row = s.qstmt->exec_end_row = s.irow;
3747
 
                s.updyes = TRUE; 
3748
 
                ret = PGAPI_ExecDirect(hstmt, updstr, SQL_NTS, 0);
3749
 
                if (ret == SQL_NEED_DATA)
3750
 
                {
3751
 
                        pup_cdata *cbdata = (pup_cdata *) malloc(sizeof(pup_cdata));
3752
 
                        memcpy(cbdata, &s, sizeof(pup_cdata));
3753
 
                        if (0 == enqueueNeedDataCallback(s.stmt, pos_update_callback, cbdata))
3754
 
                                ret = SQL_ERROR;
3755
 
                        return ret;
3756
 
                }
3757
 
                /* else if (ret != SQL_SUCCESS) this is unneccesary 
3758
 
                        SC_error_copy(s.stmt, s.qstmt, TRUE); */
3759
 
        }
3760
 
        else
3761
 
        {
3762
 
                ret = SQL_SUCCESS_WITH_INFO;
3763
 
                SC_set_error(s.stmt, STMT_INVALID_CURSOR_STATE_ERROR, "update list null", func);
3764
 
        }
3765
 
 
3766
 
        ret = pos_update_callback(ret, &s);
3767
 
        return ret;
3768
 
}
3769
 
RETCODE
3770
 
SC_pos_delete(StatementClass *stmt,
3771
 
                          SQLSETPOSIROW irow, SQLULEN global_ridx)
3772
 
{
3773
 
        CSTR    func = "SC_pos_update";
3774
 
        UWORD           offset;
3775
 
        QResultClass *res, *qres;
3776
 
        ConnectionClass *conn = SC_get_conn(stmt);
3777
 
        IRDFields       *irdflds = SC_get_IRDF(stmt);
3778
 
        char            dltstr[4096];
3779
 
        RETCODE         ret;
3780
 
        SQLLEN          kres_ridx;
3781
 
        OID             oid;
3782
 
        UInt4           blocknum, qflag;
3783
 
        TABLE_INFO      *ti;
3784
 
        const char      *bestitem;
3785
 
        const char      *bestqual;
3786
 
 
3787
 
        mylog("POS DELETE ti=%p\n", stmt->ti);
3788
 
        if (!(res = SC_get_Curres(stmt)))
3789
 
        {
3790
 
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in SC_pos_delete.", func);
3791
 
                return SQL_ERROR;
3792
 
        }
3793
 
        if (SC_update_not_ready(stmt))
3794
 
                parse_statement(stmt, TRUE);    /* not preferable */
3795
 
        if (!SC_is_updatable(stmt))
3796
 
        {
3797
 
                stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY;
3798
 
                SC_set_error(stmt, STMT_INVALID_OPTION_IDENTIFIER, "the statement is read-only", func);
3799
 
                return SQL_ERROR;
3800
 
        }
3801
 
        kres_ridx = GIdx2KResIdx(global_ridx, stmt, res);
3802
 
        if (kres_ridx < 0 || kres_ridx >= res->num_cached_keys)
3803
 
        {
3804
 
                SC_set_error(stmt, STMT_ROW_OUT_OF_RANGE, "the target rows is out of the rowset", func);
3805
 
                return SQL_ERROR;
3806
 
        }
3807
 
        ti = stmt->ti[0];
3808
 
        bestitem = GET_NAME(ti->bestitem);
3809
 
        if (!(oid = getOid(res, kres_ridx)))
3810
 
        {
3811
 
                if (bestitem && !strcmp(bestitem, OID_NAME))
3812
 
                {
3813
 
                        SC_set_error(stmt, STMT_ROW_VERSION_CHANGED, "the row was already deleted ?", func);
3814
 
                        return SQL_ERROR;
3815
 
                }
3816
 
        }
3817
 
        bestqual = GET_NAME(ti->bestqual);
3818
 
        getTid(res, kres_ridx, &blocknum, &offset);
3819
 
        /*sprintf(dltstr, "delete from \"%s\" where ctid = '%s' and oid = %s",*/
3820
 
        if (NAME_IS_VALID(ti->schema_name))
3821
 
                sprintf(dltstr, "delete from \"%s\".\"%s\" where ctid = '(%u, %u)'",
3822
 
                SAFE_NAME(ti->schema_name), SAFE_NAME(ti->table_name), blocknum, offset);
3823
 
        else
3824
 
                sprintf(dltstr, "delete from \"%s\" where ctid = '(%u, %u)'",
3825
 
                        SAFE_NAME(ti->table_name), blocknum, offset);
3826
 
        if (bestitem)
3827
 
        {
3828
 
                /*sprintf(dltstr, "%s and \"%s\" = %u", dltstr, bestitem, oid);*/
3829
 
                strcat(dltstr, " and ");
3830
 
                sprintf(dltstr + strlen(dltstr), bestqual, oid);
3831
 
        }
3832
 
 
3833
 
        mylog("dltstr=%s\n", dltstr);
3834
 
        qflag = 0;
3835
 
        if (!stmt->internal && !CC_is_in_trans(conn) &&
3836
 
                 (!CC_does_autocommit(conn)))
3837
 
                qflag |= GO_INTO_TRANSACTION;
3838
 
        qres = CC_send_query(conn, dltstr, NULL, qflag, stmt);
3839
 
        ret = SQL_SUCCESS;
3840
 
        if (QR_command_maybe_successful(qres))
3841
 
        {
3842
 
                int                     dltcnt;
3843
 
                const char *cmdstr = QR_get_command(qres);
3844
 
 
3845
 
                if (cmdstr &&
3846
 
                        sscanf(cmdstr, "DELETE %d", &dltcnt) == 1)
3847
 
                {
3848
 
                        if (dltcnt == 1)
3849
 
                        {
3850
 
                                RETCODE tret = SC_pos_reload(stmt, global_ridx, (UInt2 *) 0, SQL_DELETE);
3851
 
                                if (!SQL_SUCCEEDED(tret))
3852
 
                                        ret = tret;
3853
 
                        }
3854
 
                        else if (dltcnt == 0)
3855
 
                        {
3856
 
                                SC_set_error(stmt, STMT_ROW_VERSION_CHANGED, "the content was changed before deletion", func);
3857
 
                                ret = SQL_ERROR;
3858
 
                                if (stmt->options.cursor_type == SQL_CURSOR_KEYSET_DRIVEN)
3859
 
                                        SC_pos_reload(stmt, global_ridx, (UInt2 *) 0, 0);
3860
 
                        }
3861
 
                        else
3862
 
                                ret = SQL_ERROR;
3863
 
                }
3864
 
                else
3865
 
                        ret = SQL_ERROR;
3866
 
        }
3867
 
        else
3868
 
        {
3869
 
                ret = SQL_ERROR;
3870
 
                strcpy(res->sqlstate, qres->sqlstate);
3871
 
                res->message = qres->message;
3872
 
                qres->message = NULL;
3873
 
        }
3874
 
        if (ret == SQL_ERROR && SC_get_errornumber(stmt) == 0)
3875
 
        {
3876
 
                SC_set_error(stmt, STMT_ERROR_TAKEN_FROM_BACKEND, "SetPos delete return error", func);
3877
 
        }
3878
 
        if (qres)
3879
 
                QR_Destructor(qres);
3880
 
        if (SQL_SUCCESS == ret && res->keyset)
3881
 
        {
3882
 
                AddDeleted(res, global_ridx, res->keyset + kres_ridx);
3883
 
                res->keyset[kres_ridx].status &= (~KEYSET_INFO_PUBLIC);
3884
 
                if (CC_is_in_trans(conn))
3885
 
                {
3886
 
                        res->keyset[kres_ridx].status |= (SQL_ROW_DELETED | CURS_SELF_DELETING);
3887
 
                }
3888
 
                else
3889
 
                        res->keyset[kres_ridx].status |= (SQL_ROW_DELETED | CURS_SELF_DELETED);
3890
 
inolog(".status[%d]=%x\n", global_ridx, res->keyset[kres_ridx].status);
3891
 
        }
3892
 
#if (ODBCVER >= 0x0300)
3893
 
        if (irdflds->rowStatusArray)
3894
 
        {
3895
 
                switch (ret)
3896
 
                {
3897
 
                        case SQL_SUCCESS:
3898
 
                                irdflds->rowStatusArray[irow] = SQL_ROW_DELETED;
3899
 
                                break;
3900
 
                        default:
3901
 
                                irdflds->rowStatusArray[irow] = ret;
3902
 
                }
3903
 
        }
3904
 
#endif /* ODBCVER */
3905
 
        return ret;
3906
 
}
3907
 
 
3908
 
static RETCODE SQL_API
3909
 
irow_insert(RETCODE ret, StatementClass *stmt, StatementClass *istmt, SQLLEN addpos)
3910
 
{
3911
 
        CSTR    func = "irow_insert";
3912
 
 
3913
 
        if (ret != SQL_ERROR)
3914
 
        {
3915
 
                int             addcnt;
3916
 
                OID             oid, *poid = NULL;
3917
 
                ARDFields       *opts = SC_get_ARDF(stmt);
3918
 
                QResultClass    *ires = SC_get_Curres(istmt), *tres;
3919
 
                const char *cmdstr;
3920
 
                BindInfoClass   *bookmark;
3921
 
 
3922
 
                tres = (ires->next ? ires->next : ires);
3923
 
                cmdstr = QR_get_command(tres);
3924
 
                if (cmdstr &&
3925
 
                        sscanf(cmdstr, "INSERT %u %d", &oid, &addcnt) == 2 &&
3926
 
                        addcnt == 1)
3927
 
                {
3928
 
                        ConnectionClass *conn = SC_get_conn(stmt);
3929
 
                        RETCODE qret;
3930
 
 
3931
 
                        if (0 != oid)
3932
 
                                poid = &oid;
3933
 
                        qret = SQL_NO_DATA_FOUND;
3934
 
                        if (PG_VERSION_GE(conn, 7.2))
3935
 
                        {
3936
 
                                const char * tidval = NULL;
3937
 
 
3938
 
                                if (NULL != tres->backend_tuples &&
3939
 
                                    1 == QR_get_num_cached_tuples(tres))
3940
 
                                        tidval = QR_get_value_backend_text(tres, 0, 0);
3941
 
                                qret = SC_pos_newload(stmt, poid, TRUE, tidval);
3942
 
                                if (SQL_ERROR == qret)
3943
 
                                        return qret;
3944
 
                        }
3945
 
                        if (SQL_NO_DATA_FOUND == qret)
3946
 
                        {
3947
 
                                qret = SC_pos_newload(stmt, poid, FALSE, NULL);
3948
 
                                if (SQL_ERROR == qret)
3949
 
                                        return qret;
3950
 
                        }
3951
 
                        bookmark = opts->bookmark;
3952
 
                        if (bookmark && bookmark->buffer)
3953
 
                        {
3954
 
                                char    buf[32];
3955
 
                                SQLULEN offset = opts->row_offset_ptr ? *opts->row_offset_ptr : 0;
3956
 
 
3957
 
                                snprintf(buf, sizeof(buf), FORMAT_LEN, SC_make_bookmark(addpos));
3958
 
                                SC_set_current_col(stmt, -1);
3959
 
                                copy_and_convert_field(stmt,
3960
 
                                        PG_TYPE_INT4,
3961
 
                                        PG_UNSPECIFIED,
3962
 
                                        buf,
3963
 
                                        bookmark->returntype,
3964
 
                                        0,
3965
 
                                        bookmark->buffer + offset,
3966
 
                                        bookmark->buflen,
3967
 
                                        LENADDR_SHIFT(bookmark->used, offset),
3968
 
                                        LENADDR_SHIFT(bookmark->used, offset));
3969
 
                        }
3970
 
                }
3971
 
                else
3972
 
                {
3973
 
                        SC_set_error(stmt, STMT_ERROR_TAKEN_FROM_BACKEND, "SetPos insert return error", func);
3974
 
                }
3975
 
        }
3976
 
        return ret;
3977
 
}
3978
 
 
3979
 
/* SQL_NEED_DATA callback for SC_pos_add */
3980
 
typedef struct
3981
 
{
3982
 
        BOOL            updyes;
3983
 
        QResultClass    *res;
3984
 
        StatementClass  *stmt, *qstmt;
3985
 
        IRDFields       *irdflds;
3986
 
        SQLSETPOSIROW           irow;
3987
 
}       padd_cdata;
3988
 
 
3989
 
static RETCODE
3990
 
pos_add_callback(RETCODE retcode, void *para)
3991
 
{
3992
 
        RETCODE ret = retcode;
3993
 
        padd_cdata *s = (padd_cdata *) para;
3994
 
        SQLLEN  addpos;
3995
 
 
3996
 
        if (s->updyes)
3997
 
        {
3998
 
                SQLSETPOSIROW   brow_save;
3999
 
                
4000
 
                mylog("pos_add_callback in ret=%d\n", ret);
4001
 
                brow_save = s->stmt->bind_row; 
4002
 
                s->stmt->bind_row = s->irow;
4003
 
                if (QR_get_cursor(s->res))
4004
 
                        addpos = -(SQLLEN)(s->res->ad_count + 1);
4005
 
                else
4006
 
                        addpos = QR_get_num_total_tuples(s->res); 
4007
 
                ret = irow_insert(ret, s->stmt, s->qstmt, addpos);
4008
 
                s->stmt->bind_row = brow_save;
4009
 
        }
4010
 
        s->updyes = FALSE;
4011
 
        SC_setInsertedTable(s->qstmt, ret);
4012
 
        if (ret != SQL_SUCCESS)
4013
 
                SC_error_copy(s->stmt, s->qstmt, TRUE);
4014
 
        PGAPI_FreeStmt((HSTMT) s->qstmt, SQL_DROP);
4015
 
        s->qstmt = NULL;
4016
 
        if (SQL_SUCCESS == ret && s->res->keyset)
4017
 
        {
4018
 
                SQLLEN  global_ridx = QR_get_num_total_tuples(s->res) - 1;
4019
 
                ConnectionClass *conn = SC_get_conn(s->stmt);
4020
 
                SQLLEN  kres_ridx;
4021
 
                UWORD   status = SQL_ROW_ADDED;
4022
 
 
4023
 
                if (CC_is_in_trans(conn))
4024
 
                        status |= CURS_SELF_ADDING;
4025
 
                else
4026
 
                        status |= CURS_SELF_ADDED;
4027
 
                kres_ridx = GIdx2KResIdx(global_ridx, s->stmt, s->res);
4028
 
                if (kres_ridx >= 0 || kres_ridx < s->res->num_cached_keys)
4029
 
                {
4030
 
                        s->res->keyset[kres_ridx].status = status;
4031
 
                }
4032
 
        }
4033
 
#if (ODBCVER >= 0x0300)
4034
 
        if (s->irdflds->rowStatusArray)
4035
 
        {
4036
 
                switch (ret)
4037
 
                {
4038
 
                        case SQL_SUCCESS:
4039
 
                                s->irdflds->rowStatusArray[s->irow] = SQL_ROW_ADDED;
4040
 
                                break;
4041
 
                        default:
4042
 
                                s->irdflds->rowStatusArray[s->irow] = ret;
4043
 
                }
4044
 
        }
4045
 
#endif /* ODBCVER */
4046
 
 
4047
 
        return ret;
4048
 
}
4049
 
 
4050
 
RETCODE
4051
 
SC_pos_add(StatementClass *stmt,
4052
 
                   SQLSETPOSIROW irow)
4053
 
{
4054
 
        CSTR    func = "SC_pos_add";
4055
 
        int                     num_cols,
4056
 
                                add_cols,
4057
 
                                i;
4058
 
        HSTMT           hstmt;
4059
 
 
4060
 
        padd_cdata      s;
4061
 
        ConnectionClass *conn;
4062
 
        ConnInfo        *ci;
4063
 
        ARDFields       *opts = SC_get_ARDF(stmt);
4064
 
        APDFields       *apdopts;
4065
 
        IPDFields       *ipdopts;
4066
 
        BindInfoClass *bindings = opts->bindings;
4067
 
        FIELD_INFO      **fi = SC_get_IRDF(stmt)->fi;
4068
 
        char            addstr[4096];
4069
 
        RETCODE         ret;
4070
 
        SQLULEN         offset;
4071
 
        SQLLEN          *used;
4072
 
        Int4            bind_size = opts->bind_size;
4073
 
        OID             fieldtype;
4074
 
        int             func_cs_count = 0;
4075
 
 
4076
 
        mylog("POS ADD fi=%p ti=%p\n", fi, stmt->ti);
4077
 
        s.stmt = stmt;
4078
 
        s.irow = irow;
4079
 
        if (!(s.res = SC_get_Curres(s.stmt)))
4080
 
        {
4081
 
                SC_set_error(s.stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in SC_pos_add.", func);
4082
 
                return SQL_ERROR;
4083
 
        }
4084
 
        if (SC_update_not_ready(stmt))
4085
 
                parse_statement(s.stmt, TRUE);  /* not preferable */
4086
 
        if (!SC_is_updatable(s.stmt))
4087
 
        {
4088
 
                s.stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY;
4089
 
                SC_set_error(s.stmt, STMT_INVALID_OPTION_IDENTIFIER, "the statement is read-only", func);
4090
 
                return SQL_ERROR;
4091
 
        }
4092
 
        s.irdflds = SC_get_IRDF(s.stmt);
4093
 
        num_cols = s.irdflds->nfields;
4094
 
        conn = SC_get_conn(s.stmt);
4095
 
        if (NAME_IS_VALID(s.stmt->ti[0]->schema_name))
4096
 
                sprintf(addstr, "insert into \"%s\".\"%s\" (", SAFE_NAME(s.stmt->ti[0]->schema_name), SAFE_NAME(s.stmt->ti[0]->table_name));
4097
 
        else
4098
 
                sprintf(addstr, "insert into \"%s\" (", SAFE_NAME(s.stmt->ti[0]->table_name));
4099
 
        if (PGAPI_AllocStmt(conn, &hstmt, 0) != SQL_SUCCESS)
4100
 
        {
4101
 
                SC_set_error(s.stmt, STMT_NO_MEMORY_ERROR, "internal AllocStmt error", func);
4102
 
                return SQL_ERROR;
4103
 
        }
4104
 
        if (opts->row_offset_ptr)
4105
 
                offset = *opts->row_offset_ptr;
4106
 
        else
4107
 
                offset = 0;
4108
 
        s.qstmt = (StatementClass *) hstmt;
4109
 
        apdopts = SC_get_APDF(s.qstmt);
4110
 
        apdopts->param_bind_type = opts->bind_size;
4111
 
        apdopts->param_offset_ptr = opts->row_offset_ptr;
4112
 
        ipdopts = SC_get_IPDF(s.qstmt);
4113
 
        SC_set_delegate(s.stmt, s.qstmt);
4114
 
        ci = &(conn->connInfo);
4115
 
        extend_iparameter_bindings(ipdopts, num_cols);
4116
 
        for (i = add_cols = 0; i < num_cols; i++)
4117
 
        {
4118
 
                if (used = bindings[i].used, used != NULL)
4119
 
                {
4120
 
                        used = LENADDR_SHIFT(used, offset);
4121
 
                        if (bind_size > 0)
4122
 
                                used = LENADDR_SHIFT(used, bind_size * s.irow);
4123
 
                        else
4124
 
                                used = LENADDR_SHIFT(used, s.irow * sizeof(SQLLEN));
4125
 
                        mylog("%d used=%d\n", i, *used);
4126
 
                        if (*used != SQL_IGNORE && fi[i]->updatable)
4127
 
                        {
4128
 
                                /* fieldtype = QR_get_field_type(s.res, i); */
4129
 
                                fieldtype = getEffectiveOid(conn, fi[i]);
4130
 
                                if (add_cols)
4131
 
                                        sprintf(addstr, "%s, \"%s\"", addstr, GET_NAME(fi[i]->column_name));
4132
 
                                else
4133
 
                                        sprintf(addstr, "%s\"%s\"", addstr, GET_NAME(fi[i]->column_name));
4134
 
                                PIC_set_pgtype(ipdopts->parameters[add_cols], fieldtype);
4135
 
                                PGAPI_BindParameter(hstmt,
4136
 
                                        (SQLUSMALLINT) ++add_cols,
4137
 
                                        SQL_PARAM_INPUT,
4138
 
                                        bindings[i].returntype,
4139
 
                                        pgtype_to_concise_type(s.stmt, fieldtype, i),
4140
 
                                                                                                                        fi[i]->column_size > 0 ? fi[i]->column_size : pgtype_column_size(s.stmt, fieldtype, i, ci->drivers.unknown_sizes),
4141
 
                                        (SQLSMALLINT) fi[i]->decimal_digits,
4142
 
                                        bindings[i].buffer,
4143
 
                                        bindings[i].buflen,
4144
 
                                        bindings[i].used);
4145
 
                        }
4146
 
                }
4147
 
                else
4148
 
                        mylog("%d null bind\n", i);
4149
 
        }
4150
 
        s.updyes = FALSE;
4151
 
#define return  DONT_CALL_RETURN_FROM_HERE???
4152
 
        ENTER_INNER_CONN_CS(conn, func_cs_count); 
4153
 
        if (add_cols > 0)
4154
 
        {
4155
 
                sprintf(addstr, "%s) values (", addstr);
4156
 
                for (i = 0; i < add_cols; i++)
4157
 
                {
4158
 
                        if (i)
4159
 
                                strcat(addstr, ", ?");
4160
 
                        else
4161
 
                                strcat(addstr, "?");
4162
 
                }
4163
 
                strcat(addstr, ")");
4164
 
                if (PG_VERSION_GE(conn, 8.2))
4165
 
                        strcat(addstr, " returning ctid");
4166
 
                mylog("addstr=%s\n", addstr);
4167
 
                s.qstmt->exec_start_row = s.qstmt->exec_end_row = s.irow;
4168
 
                s.updyes = TRUE;
4169
 
                ret = PGAPI_ExecDirect(hstmt, addstr, SQL_NTS, 0);
4170
 
                if (ret == SQL_NEED_DATA)
4171
 
                {
4172
 
                        padd_cdata *cbdata = (padd_cdata *) malloc(sizeof(padd_cdata));
4173
 
                        memcpy(cbdata, &s, sizeof(padd_cdata));
4174
 
                        if (0 == enqueueNeedDataCallback(s.stmt, pos_add_callback, cbdata))
4175
 
                                ret = SQL_ERROR;
4176
 
                        goto cleanup;
4177
 
                }
4178
 
                /* else if (ret != SQL_SUCCESS) this is unneccesary
4179
 
                        SC_error_copy(s.stmt, s.qstmt, TRUE); */
4180
 
        }
4181
 
        else
4182
 
        {
4183
 
                ret = SQL_SUCCESS_WITH_INFO;
4184
 
                SC_set_error(s.stmt, STMT_INVALID_CURSOR_STATE_ERROR, "insert list null", func);
4185
 
        }
4186
 
 
4187
 
        ret = pos_add_callback(ret, &s);
4188
 
 
4189
 
cleanup:
4190
 
#undef  return
4191
 
        CLEANUP_FUNC_CONN_CS(func_cs_count, conn);
4192
 
        return ret;
4193
 
}
4194
 
 
4195
 
/*
4196
 
 *      Stuff for updatable cursors end.
4197
 
 */
4198
 
 
4199
 
RETCODE
4200
 
SC_pos_refresh(StatementClass *stmt, SQLSETPOSIROW irow , SQLULEN global_ridx)
4201
 
{
4202
 
        RETCODE ret;
4203
 
#if (ODBCVER >= 0x0300)
4204
 
        IRDFields       *irdflds = SC_get_IRDF(stmt);
4205
 
#endif /* ODBCVER */
4206
 
        /* save the last_fetch_count */
4207
 
        SQLLEN          last_fetch = stmt->last_fetch_count;
4208
 
        SQLLEN          last_fetch2 = stmt->last_fetch_count_include_ommitted;
4209
 
        SQLSETPOSIROW   bind_save = stmt->bind_row;
4210
 
        BOOL            tuple_reload = FALSE;
4211
 
 
4212
 
        if (stmt->options.cursor_type == SQL_CURSOR_KEYSET_DRIVEN)
4213
 
                tuple_reload = TRUE;
4214
 
        else 
4215
 
        {
4216
 
                QResultClass    *res = SC_get_Curres(stmt);
4217
 
                if (res && res->keyset)
4218
 
                {
4219
 
                        SQLLEN kres_ridx = GIdx2KResIdx(global_ridx, stmt, res);
4220
 
                        if (kres_ridx >= 0 && kres_ridx < QR_get_num_cached_tuples(res))
4221
 
                        {
4222
 
                                if (0 != (CURS_NEEDS_REREAD & res->keyset[kres_ridx].status))
4223
 
                                        tuple_reload = TRUE;
4224
 
                        }
4225
 
                }
4226
 
        }
4227
 
        if (tuple_reload)
4228
 
                SC_pos_reload(stmt, global_ridx, (UInt2 *) 0, 0);
4229
 
        stmt->bind_row = irow;
4230
 
        ret = SC_fetch(stmt);
4231
 
        /* restore the last_fetch_count */
4232
 
        stmt->last_fetch_count = last_fetch;
4233
 
        stmt->last_fetch_count_include_ommitted = last_fetch2;
4234
 
        stmt->bind_row = bind_save;
4235
 
#if (ODBCVER >= 0x0300)
4236
 
        if (irdflds->rowStatusArray)
4237
 
        {
4238
 
                switch (ret)
4239
 
                {
4240
 
                        case SQL_ERROR:
4241
 
                                irdflds->rowStatusArray[irow] = SQL_ROW_ERROR;
4242
 
                                break;
4243
 
                        case SQL_SUCCESS:
4244
 
                                irdflds->rowStatusArray[irow] = SQL_ROW_SUCCESS;
4245
 
                                break;
4246
 
                        case SQL_SUCCESS_WITH_INFO:
4247
 
                        default:
4248
 
                                irdflds->rowStatusArray[irow] = ret;
4249
 
                                break;
4250
 
                }
4251
 
        }
4252
 
#endif /* ODBCVER */
4253
 
 
4254
 
        return SQL_SUCCESS;
4255
 
}
4256
 
 
4257
 
/*      SQL_NEED_DATA callback for PGAPI_SetPos */
4258
 
typedef struct
4259
 
{
4260
 
        BOOL            need_data_callback, auto_commit_needed;
4261
 
        QResultClass    *res;
4262
 
        StatementClass  *stmt;
4263
 
        ARDFields       *opts;
4264
 
        GetDataInfo     *gdata;
4265
 
        SQLLEN  idx, start_row, end_row, ridx;
4266
 
        UWORD   fOption;
4267
 
        SQLSETPOSIROW   irow, nrow, processed;
4268
 
}       spos_cdata;
4269
 
static 
4270
 
RETCODE spos_callback(RETCODE retcode, void *para)
4271
 
{
4272
 
        CSTR    func = "spos_callback";
4273
 
        RETCODE ret;
4274
 
        spos_cdata *s = (spos_cdata *) para;
4275
 
        QResultClass    *res;
4276
 
        ARDFields       *opts;
4277
 
        ConnectionClass *conn;
4278
 
        SQLULEN global_ridx;
4279
 
        SQLLEN  kres_ridx, pos_ridx = 0;
4280
 
 
4281
 
        ret = retcode;
4282
 
        mylog("%s: %d in\n", func, s->need_data_callback);
4283
 
        if (s->need_data_callback)
4284
 
        {
4285
 
                s->processed++;
4286
 
                if (SQL_ERROR != retcode)
4287
 
                {
4288
 
                        s->nrow++;
4289
 
                        s->idx++;
4290
 
                }
4291
 
        }
4292
 
        else
4293
 
        {
4294
 
                s->ridx = -1;
4295
 
                s->idx = s->nrow = s->processed = 0;
4296
 
        }
4297
 
        res = s->res;
4298
 
        opts = s->opts;
4299
 
        if (!res || !opts)
4300
 
        {
4301
 
                SC_set_error(s->stmt, STMT_SEQUENCE_ERROR, "Passed res or opts for spos_callback is NULL", func);
4302
 
                return SQL_ERROR;
4303
 
        }
4304
 
        s->need_data_callback = FALSE;
4305
 
        for (; SQL_ERROR != ret && s->nrow <= s->end_row; s->idx++)
4306
 
        {
4307
 
                global_ridx = RowIdx2GIdx(s->idx, s->stmt);
4308
 
                if (SQL_ADD != s->fOption)
4309
 
                {
4310
 
                        if ((int) global_ridx >= QR_get_num_total_tuples(res))
4311
 
                                break;
4312
 
                        if (res->keyset)
4313
 
                        {
4314
 
                                kres_ridx = GIdx2KResIdx(global_ridx, s->stmt, res);
4315
 
                                if (kres_ridx >= res->num_cached_keys)
4316
 
                                        break;
4317
 
                                if (kres_ridx >= 0) /* the row may be deleted and not in the rowset */
4318
 
                                {
4319
 
                                        if (0 == (res->keyset[kres_ridx].status & CURS_IN_ROWSET))
4320
 
                                                continue;
4321
 
                                }
4322
 
                        }
4323
 
                }
4324
 
                if (s->nrow < s->start_row)
4325
 
                {
4326
 
                        s->nrow++;
4327
 
                        continue;
4328
 
                }       
4329
 
                s->ridx = s->nrow;
4330
 
                pos_ridx = s->idx;
4331
 
#if (ODBCVER >= 0x0300)
4332
 
                if (0 != s->irow || !opts->row_operation_ptr || opts->row_operation_ptr[s->nrow] == SQL_ROW_PROCEED)
4333
 
                {
4334
 
#endif /* ODBCVER */
4335
 
                        switch (s->fOption)
4336
 
                        {
4337
 
                                case SQL_UPDATE:
4338
 
                                        ret = SC_pos_update(s->stmt, s->nrow, global_ridx);
4339
 
                                        break;
4340
 
                                case SQL_DELETE:
4341
 
                                        ret = SC_pos_delete(s->stmt, s->nrow, global_ridx);
4342
 
                                        break;
4343
 
                                case SQL_ADD:
4344
 
                                        ret = SC_pos_add(s->stmt, s->nrow);
4345
 
                                        break;
4346
 
                                case SQL_REFRESH:
4347
 
                                        ret = SC_pos_refresh(s->stmt, s->nrow, global_ridx);
4348
 
                                        break;
4349
 
                        }
4350
 
                        if (SQL_NEED_DATA == ret)
4351
 
                        {
4352
 
                                spos_cdata *cbdata = (spos_cdata *) malloc(sizeof(spos_cdata));
4353
 
 
4354
 
                                memcpy(cbdata, s, sizeof(spos_cdata));
4355
 
                                cbdata->need_data_callback = TRUE;
4356
 
                                if (0 == enqueueNeedDataCallback(s->stmt, spos_callback, cbdata))
4357
 
                                        ret = SQL_ERROR;
4358
 
                                return ret;
4359
 
                        }
4360
 
                        s->processed++;
4361
 
#if (ODBCVER >= 0x0300)
4362
 
                }
4363
 
#endif /* ODBCVER */
4364
 
                if (SQL_ERROR != ret)
4365
 
                        s->nrow++;
4366
 
        }
4367
 
        conn = SC_get_conn(s->stmt);
4368
 
        if (s->auto_commit_needed)
4369
 
                CC_set_autocommit(conn, TRUE);
4370
 
        if (s->irow > 0)
4371
 
        {
4372
 
                if (SQL_ADD != s->fOption && s->ridx >= 0) /* for SQLGetData */
4373
 
                {
4374
 
                        s->stmt->currTuple = RowIdx2GIdx(pos_ridx, s->stmt);
4375
 
                        QR_set_position(res, pos_ridx);
4376
 
                }
4377
 
        }
4378
 
        else if (SC_get_IRDF(s->stmt)->rowsFetched)
4379
 
                *(SC_get_IRDF(s->stmt)->rowsFetched) = s->processed;
4380
 
        res->recent_processed_row_count = s->stmt->diag_row_count = s->processed;
4381
 
if (opts)
4382
 
{
4383
 
inolog("processed=%d ret=%d rowset=%d", s->processed, ret, opts->size_of_rowset_odbc2);
4384
 
#if (ODBCVER >= 0x0300)
4385
 
inolog(",%d\n", opts->size_of_rowset);
4386
 
#else
4387
 
inolog("\n");
4388
 
#endif /* ODBCVER */
4389
 
}
4390
 
 
4391
 
        return ret;
4392
 
}
4393
 
 
4394
 
/*
4395
 
 *      This positions the cursor within a rowset, that was positioned using SQLExtendedFetch.
4396
 
 *      This will be useful (so far) only when using SQLGetData after SQLExtendedFetch.
4397
 
 */
4398
 
RETCODE         SQL_API
4399
 
PGAPI_SetPos(
4400
 
                         HSTMT hstmt,
4401
 
                         SQLSETPOSIROW irow,
4402
 
                         SQLUSMALLINT fOption,
4403
 
                         SQLUSMALLINT fLock)
4404
 
{
4405
 
        CSTR func = "PGAPI_SetPos";
4406
 
        RETCODE ret;
4407
 
        ConnectionClass *conn;
4408
 
        SQLLEN          rowsetSize;
4409
 
        int             i;
4410
 
        UInt2           gdata_allocated;
4411
 
        GetDataInfo     *gdata_info;
4412
 
        GetDataClass    *gdata = NULL;
4413
 
        spos_cdata      s;
4414
 
 
4415
 
        s.stmt = (StatementClass *) hstmt;
4416
 
        if (!s.stmt)
4417
 
        {
4418
 
                SC_log_error(func, NULL_STRING, NULL);
4419
 
                return SQL_INVALID_HANDLE;
4420
 
        }
4421
 
 
4422
 
        s.irow = irow;
4423
 
        s.fOption = fOption;
4424
 
        s.auto_commit_needed = FALSE;
4425
 
        s.opts = SC_get_ARDF(s.stmt);
4426
 
        gdata_info = SC_get_GDTI(s.stmt);
4427
 
        gdata = gdata_info->gdata;
4428
 
        mylog("%s fOption=%d irow=%d lock=%d currt=%d\n", func, s.fOption, s.irow, fLock, s.stmt->currTuple);
4429
 
        if (s.stmt->options.scroll_concurrency != SQL_CONCUR_READ_ONLY)
4430
 
                ;
4431
 
        else if (s.fOption != SQL_POSITION && s.fOption != SQL_REFRESH)
4432
 
        {
4433
 
                SC_set_error(s.stmt, STMT_NOT_IMPLEMENTED_ERROR, "Only SQL_POSITION/REFRESH is supported for PGAPI_SetPos", func);
4434
 
                return SQL_ERROR;
4435
 
        }
4436
 
 
4437
 
        if (!(s.res = SC_get_Curres(s.stmt)))
4438
 
        {
4439
 
                SC_set_error(s.stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in PGAPI_SetPos.", func);
4440
 
                return SQL_ERROR;
4441
 
        }
4442
 
 
4443
 
#if (ODBCVER >= 0x0300)
4444
 
        rowsetSize = (s.stmt->transition_status == STMT_TRANSITION_EXTENDED_FETCH ? s.opts->size_of_rowset_odbc2 : s.opts->size_of_rowset);
4445
 
#else
4446
 
        rowsetSize = s.opts->size_of_rowset_odbc2;
4447
 
#endif /* ODBCVER */
4448
 
        if (s.irow == 0) /* bulk operation */
4449
 
        {
4450
 
                if (SQL_POSITION == s.fOption)
4451
 
                {
4452
 
                        SC_set_error(s.stmt, STMT_INVALID_CURSOR_POSITION, "Bulk Position operations not allowed.", func);
4453
 
                        return SQL_ERROR;
4454
 
                }
4455
 
                s.start_row = 0;
4456
 
                s.end_row = rowsetSize - 1;
4457
 
        }
4458
 
        else
4459
 
        {
4460
 
                if (SQL_ADD != s.fOption && s.irow > s.stmt->last_fetch_count)
4461
 
                {
4462
 
                        SC_set_error(s.stmt, STMT_ROW_OUT_OF_RANGE, "Row value out of range", func);
4463
 
                        return SQL_ERROR;
4464
 
                }
4465
 
                s.start_row = s.end_row = s.irow - 1;
4466
 
        }
4467
 
 
4468
 
        gdata_allocated = gdata_info->allocated;
4469
 
mylog("num_cols=%d gdatainfo=%d\n", QR_NumPublicResultCols(s.res), gdata_allocated);
4470
 
        /* Reset for SQLGetData */
4471
 
        if (gdata)
4472
 
        {
4473
 
                for (i = 0; i < gdata_allocated; i++)
4474
 
                        gdata[i].data_left = -1;
4475
 
        }
4476
 
        ret = SQL_SUCCESS;
4477
 
        conn = SC_get_conn(s.stmt);
4478
 
        switch (s.fOption)
4479
 
        {
4480
 
                case SQL_UPDATE:
4481
 
                case SQL_DELETE:
4482
 
                case SQL_ADD:
4483
 
                        if (s.auto_commit_needed = CC_does_autocommit(conn), s.auto_commit_needed)
4484
 
                                CC_set_autocommit(conn, FALSE);
4485
 
                        break;
4486
 
                case SQL_POSITION:
4487
 
                        break;
4488
 
        }
4489
 
 
4490
 
        s.need_data_callback = FALSE;
4491
 
#define return  DONT_CALL_RETURN_FROM_HERE???
4492
 
        /* StartRollbackState(s.stmt); */
4493
 
        ret = spos_callback(SQL_SUCCESS, &s);
4494
 
#undef  return
4495
 
        if (s.stmt->internal)
4496
 
                ret = DiscardStatementSvp(s.stmt, ret, FALSE);
4497
 
        mylog("%s returning %d\n", func, ret);
4498
 
        return ret;
4499
 
}
4500
 
 
4501
 
 
4502
 
/*              Sets options that control the behavior of cursors. */
4503
 
RETCODE         SQL_API
4504
 
PGAPI_SetScrollOptions( HSTMT hstmt,
4505
 
                                SQLUSMALLINT fConcurrency,
4506
 
                                SQLLEN crowKeyset,
4507
 
                                SQLUSMALLINT crowRowset)
4508
 
{
4509
 
        CSTR func = "PGAPI_SetScrollOptions";
4510
 
        StatementClass *stmt = (StatementClass *) hstmt;
4511
 
 
4512
 
        mylog("%s: fConcurrency=%d crowKeyset=%d crowRowset=%d\n",
4513
 
                  func, fConcurrency, crowKeyset, crowRowset);
4514
 
        SC_set_error(stmt, STMT_NOT_IMPLEMENTED_ERROR, "SetScroll option not implemeted", func);
4515
 
 
4516
 
        return SQL_ERROR;
4517
 
}
4518
 
 
4519
 
 
4520
 
/*      Set the cursor name on a statement handle */
4521
 
RETCODE         SQL_API
4522
 
PGAPI_SetCursorName(
4523
 
                                HSTMT hstmt,
4524
 
                                const SQLCHAR FAR * szCursor,
4525
 
                                SQLSMALLINT cbCursor)
4526
 
{
4527
 
        CSTR func = "PGAPI_SetCursorName";
4528
 
        StatementClass *stmt = (StatementClass *) hstmt;
4529
 
 
4530
 
        mylog("%s: hstmt=%p, szCursor=%p, cbCursorMax=%d\n", func, hstmt, szCursor, cbCursor);
4531
 
 
4532
 
        if (!stmt)
4533
 
        {
4534
 
                SC_log_error(func, NULL_STRING, NULL);
4535
 
                return SQL_INVALID_HANDLE;
4536
 
        }
4537
 
 
4538
 
        SET_NAME(stmt->cursor_name, make_string(szCursor, cbCursor, NULL, 0));
4539
 
        return SQL_SUCCESS;
4540
 
}
4541
 
 
4542
 
 
4543
 
/*      Return the cursor name for a statement handle */
4544
 
RETCODE         SQL_API
4545
 
PGAPI_GetCursorName(
4546
 
                                        HSTMT hstmt,
4547
 
                                        SQLCHAR FAR * szCursor,
4548
 
                                        SQLSMALLINT cbCursorMax,
4549
 
                                        SQLSMALLINT FAR * pcbCursor)
4550
 
{
4551
 
        CSTR func = "PGAPI_GetCursorName";
4552
 
        StatementClass *stmt = (StatementClass *) hstmt;
4553
 
        size_t          len = 0;
4554
 
        RETCODE         result;
4555
 
 
4556
 
        mylog("%s: hstmt=%p, szCursor=%p, cbCursorMax=%d, pcbCursor=%p\n", func, hstmt, szCursor, cbCursorMax, pcbCursor);
4557
 
 
4558
 
        if (!stmt)
4559
 
        {
4560
 
                SC_log_error(func, NULL_STRING, NULL);
4561
 
                return SQL_INVALID_HANDLE;
4562
 
        }
4563
 
        result = SQL_SUCCESS;
4564
 
        len = strlen(SC_cursor_name(stmt));
4565
 
 
4566
 
        if (szCursor)
4567
 
        {
4568
 
                strncpy_null(szCursor, SC_cursor_name(stmt), cbCursorMax);
4569
 
 
4570
 
                if (len >= cbCursorMax)
4571
 
                {
4572
 
                        result = SQL_SUCCESS_WITH_INFO;
4573
 
                        SC_set_error(stmt, STMT_TRUNCATED, "The buffer was too small for the GetCursorName.", func);
4574
 
                }
4575
 
        }
4576
 
 
4577
 
        if (pcbCursor)
4578
 
                *pcbCursor = (SQLSMALLINT) len;
4579
 
 
4580
 
        /*
4581
 
         * Because this function causes no db-access, there's
4582
 
         * no need to call DiscardStatementSvp()
4583
 
         */
4584
 
 
4585
 
        return result;
4586
 
}
 
1
/*
 
2
 * Module:                      results.c
 
3
 *
 
4
 * Description:         This module contains functions related to
 
5
 *                                      retrieving result information through the ODBC API.
 
6
 *
 
7
 * Classes:                     n/a
 
8
 *
 
9
 * API functions:       SQLRowCount, SQLNumResultCols, SQLDescribeCol,
 
10
 *                                      SQLColAttributes, SQLGetData, SQLFetch, SQLExtendedFetch,
 
11
 *                                      SQLMoreResults, SQLSetPos, SQLSetScrollOptions(NI),
 
12
 *                                      SQLSetCursorName, SQLGetCursorName
 
13
 *
 
14
 * Comments:            See "readme.txt" for copyright and license information.
 
15
 *-------
 
16
 */
 
17
 
 
18
#include "psqlodbc.h"
 
19
 
 
20
#include <string.h>
 
21
#include "psqlodbc.h"
 
22
#include "dlg_specific.h"
 
23
#include "environ.h"
 
24
#include "connection.h"
 
25
#include "statement.h"
 
26
#include "bind.h"
 
27
#include "qresult.h"
 
28
#include "convert.h"
 
29
#include "pgtypes.h"
 
30
 
 
31
#include <stdio.h>
 
32
#include <limits.h>
 
33
 
 
34
#include "pgapifunc.h"
 
35
 
 
36
/*      Helper macro */
 
37
#define getEffectiveOid(conn, fi) pg_true_type((conn), (fi)->columntype, FI_type(fi))
 
38
#define NULL_IF_NULL(a) ((a) ? ((const char *)(a)) : "(null)")
 
39
 
 
40
 
 
41
RETCODE         SQL_API
 
42
PGAPI_RowCount(
 
43
                           HSTMT hstmt,
 
44
                           SQLLEN FAR * pcrow)
 
45
{
 
46
        CSTR func = "PGAPI_RowCount";
 
47
        StatementClass *stmt = (StatementClass *) hstmt;
 
48
        QResultClass *res;
 
49
 
 
50
        mylog("%s: entering...\n", func);
 
51
        if (!stmt)
 
52
        {
 
53
                SC_log_error(func, NULL_STRING, NULL);
 
54
                return SQL_INVALID_HANDLE;
 
55
        }
 
56
        if (stmt->proc_return > 0)
 
57
        {
 
58
                if (pcrow)
 
59
{
 
60
                        *pcrow = 0;
 
61
inolog("returning RowCount=%d\n", *pcrow);
 
62
}
 
63
                return SQL_SUCCESS;
 
64
        }
 
65
 
 
66
        res = SC_get_Curres(stmt);
 
67
        if (res && pcrow)
 
68
        {
 
69
                if (stmt->status != STMT_FINISHED)
 
70
                {
 
71
                        SC_set_error(stmt, STMT_SEQUENCE_ERROR, "Can't get row count while statement is still executing.", func);
 
72
                        return  SQL_ERROR;
 
73
                }
 
74
                if (res->recent_processed_row_count >= 0)
 
75
                {
 
76
                        *pcrow = res->recent_processed_row_count;
 
77
                        mylog("**** %s: THE ROWS: *pcrow = %d\n", func, *pcrow);
 
78
 
 
79
                        return SQL_SUCCESS;
 
80
                }
 
81
                else if (QR_NumResultCols(res) > 0)
 
82
                {
 
83
                        *pcrow = QR_get_cursor(res) ? -1 : QR_get_num_total_tuples(res) - res->dl_count;
 
84
                        mylog("RowCount=%d\n", *pcrow);
 
85
                        return SQL_SUCCESS;
 
86
                }
 
87
        }
 
88
 
 
89
        *pcrow = -1;
 
90
        return SQL_SUCCESS;
 
91
}
 
92
 
 
93
static BOOL SC_pre_execute_ok(StatementClass *stmt, BOOL build_fi, int col_idx, const char *func)
 
94
{
 
95
        Int2 num_fields = SC_pre_execute(stmt);
 
96
        QResultClass    *result = SC_get_Curres(stmt);
 
97
        BOOL    exec_ok = TRUE;
 
98
 
 
99
        mylog("%s: result = %p, status = %d, numcols = %d\n", func, result, stmt->status, result != NULL ? QR_NumResultCols(result) : -1);
 
100
        /****if ((!result) || ((stmt->status != STMT_FINISHED) && (stmt->status != STMT_PREMATURE))) ****/
 
101
        if (!QR_command_maybe_successful(result) || num_fields < 0)
 
102
        {
 
103
                /* no query has been executed on this statement */
 
104
                SC_set_error(stmt, STMT_EXEC_ERROR, "No query has been executed with that handle", func);
 
105
                exec_ok = FALSE;
 
106
        }
 
107
        else if (col_idx >= 0 && col_idx < num_fields)
 
108
        {
 
109
                OID     reloid = QR_get_relid(result, col_idx);
 
110
                IRDFields       *irdflds = SC_get_IRDF(stmt);
 
111
                FIELD_INFO      *fi;
 
112
                TABLE_INFO      *ti = NULL;
 
113
 
 
114
inolog("build_fi=%d reloid=%u\n", build_fi, reloid);
 
115
                if (build_fi && 0 != QR_get_attid(result, col_idx))
 
116
                        getCOLIfromTI(func, NULL, stmt, reloid, &ti);
 
117
inolog("nfields=%d\n", irdflds->nfields);
 
118
                if (irdflds->fi && col_idx < (int) irdflds->nfields)
 
119
                {
 
120
                        fi = irdflds->fi[col_idx];
 
121
                        if (fi)
 
122
                        {
 
123
                                if (ti)
 
124
                                {
 
125
                                        if (NULL == fi->ti)
 
126
                                                fi->ti = ti;
 
127
                                        if (!FI_is_applicable(fi)
 
128
                                            && 0 != (ti->flags & TI_COLATTRIBUTE))
 
129
                                                fi->flag |= FIELD_COL_ATTRIBUTE;
 
130
                                }
 
131
                                fi->basetype = QR_get_field_type(result, col_idx);
 
132
                                if (0 == fi->columntype)
 
133
                                        fi->columntype = fi->basetype;
 
134
                        }
 
135
                }
 
136
        }
 
137
        return exec_ok;
 
138
}
 
139
 
 
140
/*
 
141
 *      This returns the number of columns associated with the database
 
142
 *      attached to "hstmt".
 
143
 */
 
144
RETCODE         SQL_API
 
145
PGAPI_NumResultCols(
 
146
                                        HSTMT hstmt,
 
147
                                        SQLSMALLINT FAR * pccol)
 
148
{
 
149
        CSTR func = "PGAPI_NumResultCols";
 
150
        StatementClass *stmt = (StatementClass *) hstmt;
 
151
        QResultClass *result;
 
152
        char            parse_ok;
 
153
        RETCODE         ret = SQL_SUCCESS;
 
154
 
 
155
        mylog("%s: entering...\n", func);
 
156
        if (!stmt)
 
157
        {
 
158
                SC_log_error(func, NULL_STRING, NULL);
 
159
                return SQL_INVALID_HANDLE;
 
160
        }
 
161
 
 
162
        SC_clear_error(stmt);
 
163
#define return  DONT_CALL_RETURN_FROM_HERE???
 
164
        /* StartRollbackState(stmt); */
 
165
 
 
166
        if (stmt->proc_return > 0)
 
167
        {
 
168
                *pccol = 0;
 
169
                goto cleanup;
 
170
        }
 
171
        parse_ok = FALSE;
 
172
        if (!stmt->catalog_result && SC_is_parse_forced(stmt) && SC_can_parse_statement(stmt))
 
173
        {
 
174
                if (SC_parsed_status(stmt) == STMT_PARSE_NONE)
 
175
                {
 
176
                        mylog("%s: calling parse_statement on stmt=%p\n", func, stmt);
 
177
                        parse_statement(stmt, FALSE);
 
178
                }
 
179
 
 
180
                if (SC_parsed_status(stmt) != STMT_PARSE_FATAL)
 
181
                {
 
182
                        parse_ok = TRUE;
 
183
                        *pccol = SC_get_IRDF(stmt)->nfields;
 
184
                        mylog("PARSE: %s: *pccol = %d\n", func, *pccol);
 
185
                }
 
186
        }
 
187
 
 
188
        if (!parse_ok)
 
189
        {
 
190
                if (!SC_pre_execute_ok(stmt, FALSE, -1, func))
 
191
                {
 
192
                        ret = SQL_ERROR;
 
193
                        goto cleanup;
 
194
                }
 
195
 
 
196
                result = SC_get_Curres(stmt);
 
197
                *pccol = QR_NumPublicResultCols(result);
 
198
        }
 
199
 
 
200
cleanup:
 
201
#undef  return
 
202
        if (stmt->internal)
 
203
                ret = DiscardStatementSvp(stmt, ret, FALSE);
 
204
        return ret;
 
205
}
 
206
 
 
207
 
 
208
/*
 
209
 *      Return information about the database column the user wants
 
210
 *      information about.
 
211
 */
 
212
RETCODE         SQL_API
 
213
PGAPI_DescribeCol(
 
214
                                  HSTMT hstmt,
 
215
                                  SQLUSMALLINT icol,
 
216
                                  SQLCHAR FAR * szColName,
 
217
                                  SQLSMALLINT cbColNameMax,
 
218
                                  SQLSMALLINT FAR * pcbColName,
 
219
                                  SQLSMALLINT FAR * pfSqlType,
 
220
                                  SQLULEN FAR * pcbColDef,
 
221
                                  SQLSMALLINT FAR * pibScale,
 
222
                                  SQLSMALLINT FAR * pfNullable)
 
223
{
 
224
        CSTR func = "PGAPI_DescribeCol";
 
225
 
 
226
        /* gets all the information about a specific column */
 
227
        StatementClass *stmt = (StatementClass *) hstmt;
 
228
        ConnectionClass *conn;
 
229
        IRDFields       *irdflds;
 
230
        QResultClass    *res = NULL;
 
231
        char            *col_name = NULL;
 
232
        OID             fieldtype = 0;
 
233
        SQLLEN          column_size = 0;
 
234
        SQLINTEGER      decimal_digits = 0;
 
235
        ConnInfo   *ci;
 
236
        FIELD_INFO      *fi;
 
237
        char            buf[255];
 
238
        int                     len = 0;
 
239
        RETCODE         result = SQL_SUCCESS;
 
240
 
 
241
        mylog("%s: entering.%d..\n", func, icol);
 
242
 
 
243
        if (!stmt)
 
244
        {
 
245
                SC_log_error(func, NULL_STRING, NULL);
 
246
                return SQL_INVALID_HANDLE;
 
247
        }
 
248
 
 
249
        conn = SC_get_conn(stmt);
 
250
        ci = &(conn->connInfo);
 
251
 
 
252
        SC_clear_error(stmt);
 
253
 
 
254
#define return  DONT_CALL_RETURN_FROM_HERE???
 
255
        irdflds = SC_get_IRDF(stmt);
 
256
#if (ODBCVER >= 0x0300)
 
257
        if (0 == icol) /* bookmark column */
 
258
        {
 
259
                SQLSMALLINT     fType = stmt->options.use_bookmarks == SQL_UB_VARIABLE ? SQL_BINARY : SQL_INTEGER;
 
260
 
 
261
inolog("answering bookmark info\n");
 
262
                if (szColName && cbColNameMax > 0)
 
263
                        *szColName = '\0';
 
264
                if (pcbColName)
 
265
                        *pcbColName = 0;
 
266
                if (pfSqlType)
 
267
                        *pfSqlType = fType;
 
268
                if (pcbColDef)
 
269
                        *pcbColDef = 10;
 
270
                if (pibScale)
 
271
                        *pibScale = 0;
 
272
                if (pfNullable)
 
273
                        *pfNullable = SQL_NO_NULLS;
 
274
                result = SQL_SUCCESS;
 
275
                goto cleanup;
 
276
        }
 
277
#endif /* ODBCVER */
 
278
        /*
 
279
         * Dont check for bookmark column. This is the responsibility of the
 
280
         * driver manager.
 
281
         */
 
282
 
 
283
        icol--;                                         /* use zero based column numbers */
 
284
 
 
285
        fi = NULL;
 
286
        if (icol < irdflds->nfields && irdflds->fi)
 
287
                fi = irdflds->fi[icol];
 
288
        if (!FI_is_applicable(fi) && !stmt->catalog_result && SC_is_parse_forced(stmt) && SC_can_parse_statement(stmt))
 
289
        {
 
290
                if (SC_parsed_status(stmt) == STMT_PARSE_NONE)
 
291
                {
 
292
                        mylog("%s: calling parse_statement on stmt=%p\n", func, stmt);
 
293
                        parse_statement(stmt, FALSE);
 
294
                }
 
295
 
 
296
                mylog("PARSE: DescribeCol: icol=%d, stmt=%p, stmt->nfld=%d, stmt->fi=%p\n", icol, stmt, irdflds->nfields, irdflds->fi);
 
297
 
 
298
                if (SC_parsed_status(stmt) != STMT_PARSE_FATAL && irdflds->fi)
 
299
                {
 
300
                        if (icol < irdflds->nfields)
 
301
                                fi = irdflds->fi[icol];
 
302
                        else
 
303
                        {
 
304
                                SC_set_error(stmt, STMT_INVALID_COLUMN_NUMBER_ERROR, "Invalid column number in DescribeCol.", func);
 
305
                                result = SQL_ERROR;
 
306
                                goto cleanup;
 
307
                        }
 
308
                        mylog("DescribeCol: getting info for icol=%d\n", icol);
 
309
                }
 
310
        }
 
311
 
 
312
        if (!FI_is_applicable(fi))
 
313
        {
 
314
                /*
 
315
                 * If couldn't parse it OR the field being described was not parsed
 
316
                 * (i.e., because it was a function or expression, etc, then do it the
 
317
                 * old fashioned way.
 
318
                 */
 
319
                BOOL    build_fi = PROTOCOL_74(ci) && (NULL != pfNullable || NULL != pfSqlType);
 
320
                fi = NULL;
 
321
                if (!SC_pre_execute_ok(stmt, build_fi, icol, func))
 
322
                {
 
323
                        result = SQL_ERROR;
 
324
                        goto cleanup;
 
325
                }
 
326
 
 
327
                res = SC_get_Curres(stmt);
 
328
                if (icol >= QR_NumPublicResultCols(res))
 
329
                {
 
330
                        SC_set_error(stmt, STMT_INVALID_COLUMN_NUMBER_ERROR, "Invalid column number in DescribeCol.", NULL);
 
331
                        snprintf(buf, sizeof(buf), "Col#=%d, #Cols=%d,%d keys=%d", icol, QR_NumResultCols(res), QR_NumPublicResultCols(res), res->num_key_fields);
 
332
                        SC_log_error(func, buf, stmt);
 
333
                        result = SQL_ERROR;
 
334
                        goto cleanup;
 
335
                }
 
336
                if (icol < irdflds->nfields && irdflds->fi)
 
337
                        fi = irdflds->fi[icol];
 
338
        }
 
339
        if (FI_is_applicable(fi))
 
340
        {
 
341
                fieldtype = getEffectiveOid(conn, fi);
 
342
                if (NAME_IS_VALID(fi->column_alias))
 
343
                        col_name = GET_NAME(fi->column_alias);
 
344
                else
 
345
                        col_name = GET_NAME(fi->column_name);
 
346
                column_size = fi->column_size;
 
347
                decimal_digits = fi->decimal_digits;
 
348
 
 
349
                mylog("PARSE: fieldtype=%d, col_name='%s', column_size=%d\n", fieldtype, NULL_IF_NULL(col_name), column_size);
 
350
        }               
 
351
        else
 
352
        {
 
353
                col_name = QR_get_fieldname(res, icol);
 
354
                fieldtype = QR_get_field_type(res, icol);
 
355
 
 
356
                /* atoi(ci->unknown_sizes) */
 
357
                column_size = pgtype_column_size(stmt, fieldtype, icol, ci->drivers.unknown_sizes);
 
358
                decimal_digits = pgtype_decimal_digits(stmt, fieldtype, icol);
 
359
        }
 
360
 
 
361
        mylog("describeCol: col %d fieldname = '%s'\n", icol, NULL_IF_NULL(col_name));
 
362
        mylog("describeCol: col %d fieldtype = %d\n", icol, fieldtype);
 
363
        mylog("describeCol: col %d column_size = %d\n", icol, column_size);
 
364
 
 
365
        result = SQL_SUCCESS;
 
366
 
 
367
        /*
 
368
         * COLUMN NAME
 
369
         */
 
370
        len = col_name ? (int) strlen(col_name) : 0;
 
371
 
 
372
        if (pcbColName)
 
373
                *pcbColName = len;
 
374
 
 
375
        if (szColName && cbColNameMax > 0)
 
376
        {
 
377
                if (NULL != col_name)
 
378
                        strncpy_null(szColName, col_name, cbColNameMax);
 
379
                else
 
380
                        szColName[0] = '\0';
 
381
 
 
382
                if (len >= cbColNameMax)
 
383
                {
 
384
                        result = SQL_SUCCESS_WITH_INFO;
 
385
                        SC_set_error(stmt, STMT_TRUNCATED, "The buffer was too small for the colName.", func);
 
386
                }
 
387
        }
 
388
 
 
389
        /*
 
390
         * CONCISE(SQL) TYPE
 
391
         */
 
392
        if (pfSqlType)
 
393
        {
 
394
                *pfSqlType = pgtype_to_concise_type(stmt, fieldtype, icol);
 
395
 
 
396
                mylog("describeCol: col %d *pfSqlType = %d\n", icol, *pfSqlType);
 
397
        }
 
398
 
 
399
        /*
 
400
         * COLUMN SIZE(PRECISION in 2.x)
 
401
         */
 
402
        if (pcbColDef)
 
403
        {
 
404
                if (column_size < 0)
 
405
                        column_size = 0;                /* "I dont know" */
 
406
 
 
407
                *pcbColDef = column_size;
 
408
 
 
409
                mylog("describeCol: col %d  *pcbColDef = %d\n", icol, *pcbColDef);
 
410
        }
 
411
 
 
412
        /*
 
413
         * DECIMAL DIGITS(SCALE in 2.x)
 
414
         */
 
415
        if (pibScale)
 
416
        {
 
417
                if (decimal_digits < 0)
 
418
                        decimal_digits = 0;
 
419
 
 
420
                *pibScale = (SQLSMALLINT) decimal_digits;
 
421
                mylog("describeCol: col %d  *pibScale = %d\n", icol, *pibScale);
 
422
        }
 
423
 
 
424
        /*
 
425
         * NULLABILITY
 
426
         */
 
427
        if (pfNullable)
 
428
        {
 
429
                if (SC_has_outer_join(stmt))
 
430
                        *pfNullable = TRUE;
 
431
                else
 
432
                        *pfNullable = fi ? fi->nullable : pgtype_nullable(conn, fieldtype);
 
433
 
 
434
                mylog("describeCol: col %d  *pfNullable = %d\n", icol, *pfNullable);
 
435
        }
 
436
 
 
437
cleanup:
 
438
#undef  return
 
439
        if (stmt->internal)
 
440
                result = DiscardStatementSvp(stmt, result, FALSE);
 
441
        return result;
 
442
}
 
443
 
 
444
 
 
445
/*              Returns result column descriptor information for a result set. */
 
446
RETCODE         SQL_API
 
447
PGAPI_ColAttributes(
 
448
                                        HSTMT hstmt,
 
449
                                        SQLUSMALLINT icol,
 
450
                                        SQLUSMALLINT fDescType,
 
451
                                        PTR rgbDesc,
 
452
                                        SQLSMALLINT cbDescMax,
 
453
                                        SQLSMALLINT FAR * pcbDesc,
 
454
                                        SQLLEN FAR * pfDesc)
 
455
{
 
456
        CSTR func = "PGAPI_ColAttributes";
 
457
        StatementClass *stmt = (StatementClass *) hstmt;
 
458
        IRDFields       *irdflds;
 
459
        OID             field_type = 0;
 
460
        Int2            col_idx;
 
461
        ConnectionClass *conn;
 
462
        ConnInfo        *ci;
 
463
        int             column_size, unknown_sizes;
 
464
        int                     cols = 0;
 
465
        RETCODE         result;
 
466
        const char   *p = NULL;
 
467
        SQLLEN          value = 0;
 
468
        const   FIELD_INFO      *fi = NULL;
 
469
        const   TABLE_INFO      *ti = NULL;
 
470
        QResultClass    *res;
 
471
        BOOL            stmt_updatable;
 
472
 
 
473
        mylog("%s: entering..col=%d %d len=%d.\n", func, icol, fDescType,
 
474
                                cbDescMax);
 
475
 
 
476
        if (!stmt)
 
477
        {
 
478
                SC_log_error(func, NULL_STRING, NULL);
 
479
                return SQL_INVALID_HANDLE;
 
480
        }
 
481
        stmt_updatable = SC_is_updatable(stmt)
 
482
                /* The following doesn't seem appropriate for client side cursors
 
483
                  && stmt->options.scroll_concurrency != SQL_CONCUR_READ_ONLY
 
484
                 */
 
485
                ;
 
486
 
 
487
        if (pcbDesc)
 
488
                *pcbDesc = 0;
 
489
        irdflds = SC_get_IRDF(stmt);
 
490
        conn = SC_get_conn(stmt);
 
491
        ci = &(conn->connInfo);
 
492
 
 
493
        /*
 
494
         * Dont check for bookmark column.      This is the responsibility of the
 
495
         * driver manager.      For certain types of arguments, the column number
 
496
         * is ignored anyway, so it may be 0.
 
497
         */
 
498
 
 
499
        res = SC_get_Curres(stmt);
 
500
#if (ODBCVER >= 0x0300)
 
501
        if (0 == icol && SQL_DESC_COUNT != fDescType) /* bookmark column */
 
502
        {
 
503
inolog("answering bookmark info\n");
 
504
                switch (fDescType)
 
505
                {
 
506
                        case SQL_DESC_OCTET_LENGTH:
 
507
                                if (pfDesc)
 
508
                                        *pfDesc = 4;
 
509
                                break;
 
510
                        case SQL_DESC_TYPE:
 
511
                                if (pfDesc)
 
512
                                        *pfDesc = stmt->options.use_bookmarks == SQL_UB_VARIABLE ? SQL_BINARY : SQL_INTEGER;
 
513
                                break;
 
514
                }
 
515
                return SQL_SUCCESS;
 
516
        }
 
517
#endif /* ODBCVER */
 
518
        col_idx = icol - 1;
 
519
 
 
520
        /* atoi(ci->unknown_sizes); */
 
521
        unknown_sizes = ci->drivers.unknown_sizes;
 
522
 
 
523
        /* not appropriate for SQLColAttributes() */
 
524
        if (stmt->catalog_result)
 
525
                unknown_sizes = UNKNOWNS_AS_CATALOG;
 
526
        else if (unknown_sizes == UNKNOWNS_AS_DONTKNOW)
 
527
                unknown_sizes = UNKNOWNS_AS_MAX;
 
528
 
 
529
        if (!stmt->catalog_result && SC_is_parse_forced(stmt) && SC_can_parse_statement(stmt))
 
530
        {
 
531
                if (SC_parsed_status(stmt) == STMT_PARSE_NONE)
 
532
                {
 
533
                        mylog("%s: calling parse_statement\n", func);
 
534
                        parse_statement(stmt, FALSE);
 
535
                }
 
536
 
 
537
                cols = irdflds->nfields;
 
538
 
 
539
                /*
 
540
                 * Column Count is a special case.      The Column number is ignored
 
541
                 * in this case.
 
542
                 */
 
543
#if (ODBCVER >= 0x0300)
 
544
                if (fDescType == SQL_DESC_COUNT)
 
545
#else
 
546
                if (fDescType == SQL_COLUMN_COUNT)
 
547
#endif /* ODBCVER */
 
548
                {
 
549
                        if (pfDesc)
 
550
                                *pfDesc = cols;
 
551
 
 
552
                        return SQL_SUCCESS;
 
553
                }
 
554
 
 
555
                if (SC_parsed_status(stmt) != STMT_PARSE_FATAL && irdflds->fi)
 
556
                {
 
557
                        if (col_idx >= cols)
 
558
                        {
 
559
                                SC_set_error(stmt, STMT_INVALID_COLUMN_NUMBER_ERROR, "Invalid column number in ColAttributes.", func);
 
560
                                return SQL_ERROR;
 
561
                        }
 
562
                }
 
563
        }
 
564
 
 
565
        if (col_idx < irdflds->nfields && irdflds->fi)
 
566
                fi = irdflds->fi[col_idx];
 
567
        if (FI_is_applicable(fi))
 
568
                field_type = getEffectiveOid(conn, fi);
 
569
        else
 
570
        {
 
571
                BOOL    build_fi = FALSE;
 
572
 
 
573
                fi = NULL;
 
574
                if (PROTOCOL_74(ci))
 
575
                {
 
576
                        switch (fDescType)
 
577
                        {
 
578
                                case SQL_COLUMN_OWNER_NAME:
 
579
                                case SQL_COLUMN_TABLE_NAME:
 
580
                                case SQL_COLUMN_TYPE:
 
581
                                case SQL_COLUMN_TYPE_NAME:
 
582
                                case SQL_COLUMN_AUTO_INCREMENT:
 
583
#if (ODBCVER >= 0x0300)
 
584
                                case SQL_DESC_NULLABLE:
 
585
                                case SQL_DESC_BASE_TABLE_NAME:
 
586
                                case SQL_DESC_BASE_COLUMN_NAME:
 
587
#else
 
588
                                case SQL_COLUMN_NULLABLE:
 
589
#endif /* ODBCVER */
 
590
                                case SQL_COLUMN_UPDATABLE:
 
591
                                case 1212: /* SQL_CA_SS_COLUMN_KEY ? */
 
592
                                        build_fi = TRUE;
 
593
                                        break;
 
594
                        }
 
595
                }
 
596
                if (!SC_pre_execute_ok(stmt, build_fi, col_idx, func))
 
597
                        return SQL_ERROR;
 
598
 
 
599
                res = SC_get_Curres(stmt);
 
600
                cols = QR_NumPublicResultCols(res);
 
601
 
 
602
                /*
 
603
                 * Column Count is a special case.      The Column number is ignored
 
604
                 * in this case.
 
605
                 */
 
606
#if (ODBCVER >= 0x0300)
 
607
                if (fDescType == SQL_DESC_COUNT)
 
608
#else
 
609
                if (fDescType == SQL_COLUMN_COUNT)
 
610
#endif /* ODBCVER */
 
611
                {
 
612
                        if (pfDesc)
 
613
                                *pfDesc = cols;
 
614
 
 
615
                        return SQL_SUCCESS;
 
616
                }
 
617
 
 
618
                if (col_idx >= cols)
 
619
                {
 
620
                        SC_set_error(stmt, STMT_INVALID_COLUMN_NUMBER_ERROR, "Invalid column number in ColAttributes.", func);
 
621
                        return SQL_ERROR;
 
622
                }
 
623
 
 
624
                field_type = QR_get_field_type(res, col_idx);
 
625
                if (col_idx < irdflds->nfields && irdflds->fi)
 
626
                        fi = irdflds->fi[col_idx];
 
627
        }
 
628
        if (FI_is_applicable(fi))
 
629
        {
 
630
                ti = fi->ti;
 
631
                field_type = getEffectiveOid(conn, fi);
 
632
        }
 
633
 
 
634
        mylog("colAttr: col %d field_type=%d fi,ti=%p,%p\n", col_idx, field_type, fi, ti);
 
635
 
 
636
        column_size = (fi != NULL && fi->column_size > 0) ? fi->column_size : pgtype_column_size(stmt, field_type, col_idx, unknown_sizes);
 
637
        switch (fDescType)
 
638
        {
 
639
                case SQL_COLUMN_AUTO_INCREMENT: /* == SQL_DESC_AUTO_UNIQUE_VALUE */
 
640
                        if (fi && fi->auto_increment)
 
641
                                value = TRUE;
 
642
                        else
 
643
                                value = pgtype_auto_increment(conn, field_type);
 
644
                        if (value == -1)        /* non-numeric becomes FALSE (ODBC Doc) */
 
645
                                value = FALSE;
 
646
                        mylog("AUTO_INCREMENT=%d\n", value);
 
647
 
 
648
                        break;
 
649
 
 
650
                case SQL_COLUMN_CASE_SENSITIVE: /* == SQL_DESC_CASE_SENSITIVE */
 
651
                        value = pgtype_case_sensitive(conn, field_type);
 
652
                        break;
 
653
 
 
654
                        /*
 
655
                         * This special case is handled above.
 
656
                         *
 
657
                         * case SQL_COLUMN_COUNT:
 
658
                         */
 
659
                case SQL_COLUMN_DISPLAY_SIZE: /* == SQL_DESC_DISPLAY_SIZE */
 
660
                        value = (fi && 0 != fi->display_size) ? fi->display_size : pgtype_display_size(stmt, field_type, col_idx, unknown_sizes);
 
661
 
 
662
                        mylog("%s: col %d, display_size= %d\n", func, col_idx, value);
 
663
 
 
664
                        break;
 
665
 
 
666
                case SQL_COLUMN_LABEL: /* == SQL_DESC_LABEL */
 
667
                        if (fi && (NAME_IS_VALID(fi->column_alias)))
 
668
                        {
 
669
                                p = GET_NAME(fi->column_alias);
 
670
 
 
671
                                mylog("%s: COLUMN_LABEL = '%s'\n", func, p);
 
672
                                break;
 
673
                        }
 
674
                        /* otherwise same as column name -- FALL THROUGH!!! */
 
675
 
 
676
#if (ODBCVER >= 0x0300)
 
677
                case SQL_DESC_NAME:
 
678
#else
 
679
                case SQL_COLUMN_NAME:
 
680
#endif /* ODBCVER */
 
681
inolog("fi=%p", fi);
 
682
if (fi)
 
683
inolog(" (%s,%s)", PRINT_NAME(fi->column_alias), PRINT_NAME(fi->column_name));
 
684
                        p = fi ? (NAME_IS_NULL(fi->column_alias) ? SAFE_NAME(fi->column_name) : GET_NAME(fi->column_alias)) : QR_get_fieldname(res, col_idx);
 
685
 
 
686
                        mylog("%s: COLUMN_NAME = '%s'\n", func, p);
 
687
                        break;
 
688
 
 
689
                case SQL_COLUMN_LENGTH:
 
690
                        value = (fi && fi->length > 0) ? fi->length : pgtype_buffer_length(stmt, field_type, col_idx, unknown_sizes);
 
691
                        if (0 > value)
 
692
                        /* if (-1 == value)  I'm not sure which is right */
 
693
                                value = 0;
 
694
 
 
695
                        mylog("%s: col %d, column_length = %d\n", func, col_idx, value);
 
696
                        break;
 
697
 
 
698
                case SQL_COLUMN_MONEY: /* == SQL_DESC_FIXED_PREC_SCALE */
 
699
                        value = pgtype_money(conn, field_type);
 
700
inolog("COLUMN_MONEY=%d\n", value);
 
701
                        break;
 
702
 
 
703
#if (ODBCVER >= 0x0300)
 
704
                case SQL_DESC_NULLABLE:
 
705
#else
 
706
                case SQL_COLUMN_NULLABLE:
 
707
#endif /* ODBCVER */
 
708
                        if (SC_has_outer_join(stmt))
 
709
                                value = TRUE;
 
710
                        else
 
711
                                value = fi ? fi->nullable : pgtype_nullable(conn, field_type);
 
712
inolog("COLUMN_NULLABLE=%d\n", value);
 
713
                        break;
 
714
 
 
715
                case SQL_COLUMN_OWNER_NAME: /* == SQL_DESC_SCHEMA_NAME */
 
716
                        p = ti ? SAFE_NAME(ti->schema_name) : NULL_STRING;
 
717
                        mylog("%s: SCHEMA_NAME = '%s'\n", func, p);
 
718
                        break;
 
719
 
 
720
                case SQL_COLUMN_PRECISION: /* in 2.x */
 
721
                        value = column_size;
 
722
                        if (value < 0)
 
723
                                value = 0;
 
724
 
 
725
                        mylog("%s: col %d, column_size = %d\n", func, col_idx, value);
 
726
                        break;
 
727
 
 
728
                case SQL_COLUMN_QUALIFIER_NAME: /* == SQL_DESC_CATALOG_NAME */
 
729
                        p = ti ? CurrCatString(conn) : NULL_STRING;     /* empty string means *not supported* */
 
730
                        break;
 
731
 
 
732
                case SQL_COLUMN_SCALE: /* in 2.x */
 
733
                        value = pgtype_decimal_digits(stmt, field_type, col_idx);
 
734
inolog("COLUMN_SCALE=%d\n", value);
 
735
                        if (value < 0)
 
736
                                value = 0;
 
737
                        break;
 
738
 
 
739
                case SQL_COLUMN_SEARCHABLE: /* == SQL_DESC_SEARCHABLE */
 
740
                        value = pgtype_searchable(conn, field_type);
 
741
                        break;
 
742
 
 
743
                case SQL_COLUMN_TABLE_NAME: /* == SQL_DESC_TABLE_NAME */
 
744
                        p = ti ? SAFE_NAME(ti->table_name) : NULL_STRING;
 
745
 
 
746
                        mylog("%s: TABLE_NAME = '%s'\n", func, p);
 
747
                        break;
 
748
 
 
749
                case SQL_COLUMN_TYPE: /* == SQL_DESC_CONCISE_TYPE */
 
750
                        value = pgtype_to_concise_type(stmt, field_type, col_idx);
 
751
                        mylog("COLUMN_TYPE=%d\n", value);
 
752
                        break;
 
753
 
 
754
                case SQL_COLUMN_TYPE_NAME: /* == SQL_DESC_TYPE_NAME */
 
755
                        p = pgtype_to_name(stmt, field_type, col_idx, fi && fi->auto_increment);
 
756
                        break;
 
757
 
 
758
                case SQL_COLUMN_UNSIGNED: /* == SQL_DESC_UNSINGED */
 
759
                        value = pgtype_unsigned(conn, field_type);
 
760
                        if (value == -1)        /* non-numeric becomes TRUE (ODBC Doc) */
 
761
                                value = SQL_TRUE;
 
762
 
 
763
                        break;
 
764
 
 
765
                case SQL_COLUMN_UPDATABLE: /* == SQL_DESC_UPDATABLE */
 
766
 
 
767
                        /*
 
768
                         * Neither Access or Borland care about this.
 
769
                         *
 
770
                         * if (field_type == PG_TYPE_OID) pfDesc = SQL_ATTR_READONLY;
 
771
                         * else
 
772
                         */
 
773
                        if (!stmt_updatable)
 
774
                                value = SQL_ATTR_READONLY;
 
775
                        else
 
776
                                value = fi ? (fi->updatable ? SQL_ATTR_WRITE : SQL_ATTR_READONLY) : (QR_get_attid(res, col_idx) > 0 ? SQL_ATTR_WRITE : (PROTOCOL_74(ci) ? SQL_ATTR_READONLY : SQL_ATTR_READWRITE_UNKNOWN));
 
777
                        if (SQL_ATTR_READONLY != value)
 
778
                        {
 
779
                                const char *name = fi ? SAFE_NAME(fi->column_name) : QR_get_fieldname(res, col_idx);
 
780
                                if (stricmp(name, OID_NAME) == 0 ||
 
781
                                    stricmp(name, "ctid") == 0 ||
 
782
                                    stricmp(name, "xmin") == 0)
 
783
                                        value = SQL_ATTR_READONLY;
 
784
                                else if (conn->ms_jet && fi && fi->auto_increment)
 
785
                                        value = SQL_ATTR_READONLY;
 
786
                        }
 
787
 
 
788
                        mylog("%s: UPDATEABLE = %d\n", func, value);
 
789
                        break;
 
790
#if (ODBCVER >= 0x0300)
 
791
                case SQL_DESC_BASE_COLUMN_NAME:
 
792
 
 
793
                        p = fi ? SAFE_NAME(fi->column_name) : QR_get_fieldname(res, col_idx);
 
794
 
 
795
                        mylog("%s: BASE_COLUMN_NAME = '%s'\n", func, p);
 
796
                        break;
 
797
                case SQL_DESC_BASE_TABLE_NAME: /* the same as TABLE_NAME ok ? */
 
798
                        p = ti ? SAFE_NAME(ti->table_name) : NULL_STRING;
 
799
 
 
800
                        mylog("%s: BASE_TABLE_NAME = '%s'\n", func, p);
 
801
                        break;
 
802
                case SQL_DESC_LENGTH: /* different from SQL_COLUMN_LENGTH */
 
803
                        // value = (fi && fi->length > 0) ? fi->length : pgtype_desclength(stmt, field_type, col_idx, unknown_sizes);
 
804
                        value = (fi && column_size > 0) ? column_size : pgtype_desclength(stmt, field_type, col_idx, unknown_sizes);
 
805
                        if (-1 == value)
 
806
                                value = 0;
 
807
 
 
808
                        mylog("%s: col %d, desc_length = %d\n", func, col_idx, value);
 
809
                        break;
 
810
                case SQL_DESC_OCTET_LENGTH:
 
811
                        value = (fi && fi->length > 0) ? fi->length : pgtype_attr_transfer_octet_length(conn, field_type, column_size, unknown_sizes);
 
812
                        if (-1 == value)
 
813
                                value = 0;
 
814
                        mylog("%s: col %d, octet_length = %d\n", func, col_idx, value);
 
815
                        break;
 
816
                case SQL_DESC_PRECISION: /* different from SQL_COLUMN_PRECISION */
 
817
                        if (value = FI_precision(fi), value <= 0)
 
818
                                value = pgtype_precision(stmt, field_type, col_idx, unknown_sizes);
 
819
                        if (value < 0)
 
820
                                value = 0;
 
821
 
 
822
                        mylog("%s: col %d, desc_precision = %d\n", func, col_idx, value);
 
823
                        break;
 
824
                case SQL_DESC_SCALE: /* different from SQL_COLUMN_SCALE */
 
825
                        value = pgtype_scale(stmt, field_type, col_idx);
 
826
                        if (value < 0)
 
827
                                value = 0;
 
828
                        break;
 
829
                case SQL_DESC_LOCAL_TYPE_NAME:
 
830
                        p = pgtype_to_name(stmt, field_type, col_idx, fi && fi->auto_increment);
 
831
                        break;
 
832
                case SQL_DESC_TYPE:
 
833
                        value = pgtype_to_sqldesctype(stmt, field_type, col_idx);
 
834
                        break;
 
835
                case SQL_DESC_NUM_PREC_RADIX:
 
836
                        value = pgtype_radix(conn, field_type);
 
837
                        break;
 
838
                case SQL_DESC_LITERAL_PREFIX:
 
839
                        p = pgtype_literal_prefix(conn, field_type);
 
840
                        break;
 
841
                case SQL_DESC_LITERAL_SUFFIX:
 
842
                        p = pgtype_literal_suffix(conn, field_type);
 
843
                        break;
 
844
                case SQL_DESC_UNNAMED:
 
845
                        value = (fi && NAME_IS_NULL(fi->column_name) && NAME_IS_NULL(fi->column_alias)) ? SQL_UNNAMED : SQL_NAMED;
 
846
                        break;
 
847
#endif /* ODBCVER */
 
848
                case 1211: /* SQL_CA_SS_COLUMN_HIDDEN ? */
 
849
                        value = 0;
 
850
                        break;
 
851
                case 1212: /* SQL_CA_SS_COLUMN_KEY ? */
 
852
                        if (fi)
 
853
                        {
 
854
                                if (fi->columnkey < 0)
 
855
                                {
 
856
                                        SC_set_SS_columnkey(stmt);
 
857
                                }
 
858
                                value = fi->columnkey;
 
859
                                mylog("%s:SS_COLUMN_KEY=%d\n", func, value);
 
860
                                break;
 
861
                        }
 
862
                        SC_set_error(stmt, STMT_OPTION_NOT_FOR_THE_DRIVER, "this request may be for MS SQL Server", func);
 
863
                        return SQL_ERROR;
 
864
                default:
 
865
                        SC_set_error(stmt, STMT_INVALID_OPTION_IDENTIFIER, "ColAttribute for this type not implemented yet", func);
 
866
                        return SQL_ERROR;
 
867
        }
 
868
 
 
869
        result = SQL_SUCCESS;
 
870
 
 
871
        if (p)
 
872
        {                                                       /* char/binary data */
 
873
                size_t len = strlen(p);
 
874
 
 
875
                if (rgbDesc)
 
876
                {
 
877
                        strncpy_null((char *) rgbDesc, p, (size_t) cbDescMax);
 
878
 
 
879
                        if (len >= cbDescMax)
 
880
                        {
 
881
                                result = SQL_SUCCESS_WITH_INFO;
 
882
                                SC_set_error(stmt, STMT_TRUNCATED, "The buffer was too small for the rgbDesc.", func);
 
883
                        }
 
884
                }
 
885
 
 
886
                if (pcbDesc)
 
887
                        *pcbDesc = (SQLSMALLINT) len;
 
888
        }
 
889
        else
 
890
        {
 
891
                /* numeric data */
 
892
                if (pfDesc)
 
893
                        *pfDesc = value;
 
894
        }
 
895
 
 
896
        return result;
 
897
}
 
898
 
 
899
 
 
900
/*      Returns result data for a single column in the current row. */
 
901
RETCODE         SQL_API
 
902
PGAPI_GetData(
 
903
                          HSTMT hstmt,
 
904
                          SQLUSMALLINT icol,
 
905
                          SQLSMALLINT fCType,
 
906
                          PTR rgbValue,
 
907
                          SQLLEN cbValueMax,
 
908
                          SQLLEN FAR * pcbValue)
 
909
{
 
910
        CSTR func = "PGAPI_GetData";
 
911
        QResultClass *res;
 
912
        StatementClass *stmt = (StatementClass *) hstmt;
 
913
        UInt2           num_cols;
 
914
        SQLLEN          num_rows;
 
915
        OID             field_type;
 
916
        int             atttypmod;
 
917
        void       *value = NULL;
 
918
        RETCODE         result = SQL_SUCCESS;
 
919
        char            get_bookmark = FALSE;
 
920
        SQLSMALLINT     target_type;
 
921
        int             precision = -1;
 
922
 
 
923
        mylog("%s: enter, stmt=%p icol=%d\n", func, stmt, icol);
 
924
 
 
925
        if (!stmt)
 
926
        {
 
927
                SC_log_error(func, NULL_STRING, NULL);
 
928
                return SQL_INVALID_HANDLE;
 
929
        }
 
930
        res = SC_get_Curres(stmt);
 
931
 
 
932
        if (STMT_EXECUTING == stmt->status)
 
933
        {
 
934
                SC_set_error(stmt, STMT_SEQUENCE_ERROR, "Can't get data while statement is still executing.", func);
 
935
                return SQL_ERROR;
 
936
        }
 
937
 
 
938
        if (stmt->status != STMT_FINISHED)
 
939
        {
 
940
                SC_set_error(stmt, STMT_STATUS_ERROR, "GetData can only be called after the successful execution on a SQL statement", func);
 
941
                return SQL_ERROR;
 
942
        }
 
943
 
 
944
#if (ODBCVER >= 0x0300)
 
945
        if (SQL_ARD_TYPE == fCType)
 
946
        {
 
947
                ARDFields       *opts;
 
948
                BindInfoClass   *binfo = NULL;
 
949
 
 
950
                opts = SC_get_ARDF(stmt);
 
951
                if (0 == icol)
 
952
                        binfo = opts->bookmark;
 
953
                else if (icol <= opts->allocated && opts->bindings)
 
954
                        binfo = &opts->bindings[icol - 1];
 
955
                if (binfo)
 
956
                {       
 
957
                        target_type = binfo->returntype;
 
958
                        mylog("SQL_ARD_TYPE=%d\n", target_type);
 
959
                        precision = binfo->precision;
 
960
                }
 
961
                else
 
962
                {
 
963
                        SC_set_error(stmt, STMT_STATUS_ERROR, "GetData can't determine the type via ARD", func);
 
964
                        return SQL_ERROR;
 
965
                }
 
966
        }
 
967
        else
 
968
#endif /* ODBCVER */
 
969
                target_type = fCType;
 
970
        if (icol == 0)
 
971
        {
 
972
                if (stmt->options.use_bookmarks == SQL_UB_OFF)
 
973
                {
 
974
                        SC_set_error(stmt, STMT_COLNUM_ERROR, "Attempt to retrieve bookmark with bookmark usage disabled", func);
 
975
                        return SQL_ERROR;
 
976
                }
 
977
 
 
978
                /* Make sure it is the bookmark data type */
 
979
                switch (target_type)
 
980
                {
 
981
                        case SQL_C_BOOKMARK:
 
982
#if (ODBCVER >= 0x0300)
 
983
                        case SQL_C_VARBOOKMARK:
 
984
#endif /* ODBCVER */
 
985
                                break;
 
986
                        default:
 
987
inolog("GetData Column 0 is type %d not of type SQL_C_BOOKMARK", target_type);
 
988
                                SC_set_error(stmt, STMT_PROGRAM_TYPE_OUT_OF_RANGE, "Column 0 is not of type SQL_C_BOOKMARK", func);
 
989
                                return SQL_ERROR;
 
990
                }
 
991
 
 
992
                get_bookmark = TRUE;
 
993
        }
 
994
        else
 
995
        {
 
996
                /* use zero-based column numbers */
 
997
                icol--;
 
998
 
 
999
                /* make sure the column number is valid */
 
1000
                num_cols = QR_NumPublicResultCols(res);
 
1001
                if (icol >= num_cols)
 
1002
                {
 
1003
                        SC_set_error(stmt, STMT_INVALID_COLUMN_NUMBER_ERROR, "Invalid column number.", func);
 
1004
                        return SQL_ERROR;
 
1005
                }
 
1006
        }
 
1007
 
 
1008
#define return  DONT_CALL_RETURN_FROM_HERE???
 
1009
        /* StartRollbackState(stmt); */
 
1010
        if (!SC_is_fetchcursor(stmt))
 
1011
        {
 
1012
                /* make sure we're positioned on a valid row */
 
1013
                num_rows = QR_get_num_total_tuples(res);
 
1014
                if ((stmt->currTuple < 0) ||
 
1015
                        (stmt->currTuple >= num_rows))
 
1016
                {
 
1017
                        SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Not positioned on a valid row for GetData.", func);
 
1018
                        result = SQL_ERROR;
 
1019
                        goto cleanup;
 
1020
                }
 
1021
                mylog("     num_rows = %d\n", num_rows);
 
1022
 
 
1023
                if (!get_bookmark)
 
1024
                {
 
1025
                        SQLLEN  curt = GIdx2CacheIdx(stmt->currTuple, stmt, res);
 
1026
                        value = QR_get_value_backend_row(res, curt, icol);
 
1027
inolog("currT=%d base=%d rowset=%d\n", stmt->currTuple, QR_get_rowstart_in_cache(res), SC_get_rowset_start(stmt)); 
 
1028
                        mylog("     value = '%s'\n", NULL_IF_NULL(value));
 
1029
                }
 
1030
        }
 
1031
        else
 
1032
        {
 
1033
                /* it's a SOCKET result (backend data) */
 
1034
                if (stmt->currTuple == -1 || !res || !res->tupleField)
 
1035
                {
 
1036
                        SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Not positioned on a valid row for GetData.", func);
 
1037
                        result = SQL_ERROR;
 
1038
                        goto cleanup;
 
1039
                }
 
1040
 
 
1041
                if (!get_bookmark)
 
1042
                {
 
1043
                        /** value = QR_get_value_backend(res, icol); maybe thiw doesn't work */
 
1044
                        SQLLEN  curt = GIdx2CacheIdx(stmt->currTuple, stmt, res);
 
1045
                        value = QR_get_value_backend_row(res, curt, icol);
 
1046
                }
 
1047
                mylog("  socket: value = '%s'\n", NULL_IF_NULL(value));
 
1048
        }
 
1049
 
 
1050
        if (get_bookmark)
 
1051
        {
 
1052
                BOOL    contents_get = FALSE;
 
1053
 
 
1054
                if (rgbValue)
 
1055
                {
 
1056
                        if (SQL_C_BOOKMARK == target_type || 4 <= cbValueMax)
 
1057
                        {
 
1058
                                contents_get = TRUE; 
 
1059
                                *((SQLULEN *) rgbValue) = SC_get_bookmark(stmt);
 
1060
                        }
 
1061
                }
 
1062
                if (pcbValue)
 
1063
                        *pcbValue = sizeof(SQLULEN);
 
1064
 
 
1065
                if (contents_get)
 
1066
                        result = SQL_SUCCESS;
 
1067
                else
 
1068
                {
 
1069
                        SC_set_error(stmt, STMT_TRUNCATED, "The buffer was too small for the GetData.", func);
 
1070
                        result = SQL_SUCCESS_WITH_INFO;
 
1071
                }
 
1072
                goto cleanup;
 
1073
        }
 
1074
 
 
1075
        field_type = QR_get_field_type(res, icol);
 
1076
        atttypmod = QR_get_atttypmod(res, icol);
 
1077
 
 
1078
        mylog("**** %s: icol = %d, target_type = %d, field_type = %d, value = '%s'\n", func, icol, target_type, field_type, NULL_IF_NULL(value));
 
1079
 
 
1080
        SC_set_current_col(stmt, icol);
 
1081
 
 
1082
        result = copy_and_convert_field(stmt, field_type, atttypmod, value,
 
1083
                        target_type, precision, rgbValue, cbValueMax, pcbValue, pcbValue);
 
1084
 
 
1085
        switch (result)
 
1086
        {
 
1087
                case COPY_OK:
 
1088
                        result = SQL_SUCCESS;
 
1089
                        break;
 
1090
 
 
1091
                case COPY_UNSUPPORTED_TYPE:
 
1092
                        SC_set_error(stmt, STMT_RESTRICTED_DATA_TYPE_ERROR, "Received an unsupported type from Postgres.", func);
 
1093
                        result = SQL_ERROR;
 
1094
                        break;
 
1095
 
 
1096
                case COPY_UNSUPPORTED_CONVERSION:
 
1097
                        SC_set_error(stmt, STMT_RESTRICTED_DATA_TYPE_ERROR, "Couldn't handle the necessary data type conversion.", func);
 
1098
                        result = SQL_ERROR;
 
1099
                        break;
 
1100
 
 
1101
                case COPY_RESULT_TRUNCATED:
 
1102
                        SC_set_error(stmt, STMT_TRUNCATED, "The buffer was too small for the GetData.", func);
 
1103
                        result = SQL_SUCCESS_WITH_INFO;
 
1104
                        break;
 
1105
 
 
1106
                case COPY_GENERAL_ERROR:                /* error msg already filled in */
 
1107
                        result = SQL_ERROR;
 
1108
                        break;
 
1109
 
 
1110
                case COPY_NO_DATA_FOUND:
 
1111
                        /* SC_log_error(func, "no data found", stmt); */
 
1112
                        result = SQL_NO_DATA_FOUND;
 
1113
                        break;
 
1114
 
 
1115
                default:
 
1116
                        SC_set_error(stmt, STMT_INTERNAL_ERROR, "Unrecognized return value from copy_and_convert_field.", func);
 
1117
                        result = SQL_ERROR;
 
1118
                        break;
 
1119
        }
 
1120
 
 
1121
cleanup:
 
1122
#undef  return
 
1123
        if (stmt->internal)
 
1124
                result = DiscardStatementSvp(stmt, result, FALSE);
 
1125
inolog("%s returning %d\n", __FUNCTION__, result);
 
1126
        return result;
 
1127
}
 
1128
 
 
1129
 
 
1130
/*
 
1131
 *              Returns data for bound columns in the current row ("hstmt->iCursor"),
 
1132
 *              advances the cursor.
 
1133
 */
 
1134
RETCODE         SQL_API
 
1135
PGAPI_Fetch(
 
1136
                        HSTMT hstmt)
 
1137
{
 
1138
        CSTR func = "PGAPI_Fetch";
 
1139
        StatementClass *stmt = (StatementClass *) hstmt;
 
1140
        ARDFields       *opts;
 
1141
        QResultClass *res;
 
1142
        BindInfoClass   *bookmark;
 
1143
        RETCODE         retval = SQL_SUCCESS;
 
1144
 
 
1145
        mylog("%s: stmt = %p, stmt->result= %p\n", func, stmt, stmt ? SC_get_Curres(stmt) : NULL);
 
1146
 
 
1147
        if (!stmt)
 
1148
        {
 
1149
                SC_log_error(func, NULL_STRING, NULL);
 
1150
                return SQL_INVALID_HANDLE;
 
1151
        }
 
1152
 
 
1153
        SC_clear_error(stmt);
 
1154
 
 
1155
        if (!(res = SC_get_Curres(stmt)))
 
1156
        {
 
1157
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in PGAPI_Fetch.", func);
 
1158
                return SQL_ERROR;
 
1159
        }
 
1160
 
 
1161
        /* Not allowed to bind a bookmark column when using SQLFetch. */
 
1162
        opts = SC_get_ARDF(stmt);
 
1163
        if ((bookmark = opts->bookmark) && bookmark->buffer)
 
1164
        {
 
1165
                SC_set_error(stmt, STMT_COLNUM_ERROR, "Not allowed to bind a bookmark column when using PGAPI_Fetch", func);
 
1166
                return SQL_ERROR;
 
1167
        }
 
1168
 
 
1169
        if (stmt->status == STMT_EXECUTING)
 
1170
        {
 
1171
                SC_set_error(stmt, STMT_SEQUENCE_ERROR, "Can't fetch while statement is still executing.", func);
 
1172
                return SQL_ERROR;
 
1173
        }
 
1174
 
 
1175
        if (stmt->status != STMT_FINISHED)
 
1176
        {
 
1177
                SC_set_error(stmt, STMT_SEQUENCE_ERROR, "Fetch can only be called after the successful execution on a SQL statement", func);
 
1178
                return SQL_ERROR;
 
1179
        }
 
1180
 
 
1181
        if (opts->bindings == NULL)
 
1182
        {
 
1183
                if (!SC_may_fetch_rows(stmt))
 
1184
                        return SQL_NO_DATA_FOUND;
 
1185
                /* just to avoid a crash if the user insists on calling this */
 
1186
                /* function even if SQL_ExecDirect has reported an Error */
 
1187
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Bindings were not allocated properly.", func);
 
1188
                return SQL_ERROR;
 
1189
        }
 
1190
 
 
1191
#define return  DONT_CALL_RETURN_FROM_HERE???
 
1192
        /* StartRollbackState(stmt); */
 
1193
        if (stmt->rowset_start < 0)
 
1194
                SC_set_rowset_start(stmt, 0, TRUE);
 
1195
        QR_set_rowset_size(res, 1);
 
1196
        /* QR_inc_rowstart_in_cache(res, stmt->last_fetch_count_include_ommitted); */
 
1197
        SC_inc_rowset_start(stmt, stmt->last_fetch_count_include_ommitted);
 
1198
 
 
1199
        retval = SC_fetch(stmt);
 
1200
#undef  return
 
1201
        if (stmt->internal)
 
1202
                retval = DiscardStatementSvp(stmt, retval, FALSE);
 
1203
        return retval;
 
1204
}
 
1205
 
 
1206
static RETCODE SQL_API
 
1207
SC_pos_reload_needed(StatementClass *stmt, SQLULEN req_size, UDWORD flag);
 
1208
SQLLEN
 
1209
getNthValid(const QResultClass *res, SQLLEN sta, UWORD orientation, SQLULEN nth, SQLLEN *nearest)
 
1210
{
 
1211
        SQLLEN  i, num_tuples = QR_get_num_total_tuples(res), nearp;
 
1212
        SQLULEN count;
 
1213
        KeySet  *keyset;
 
1214
 
 
1215
        if (!QR_once_reached_eof(res))
 
1216
                num_tuples = INT_MAX;
 
1217
        /* Note that the parameter nth is 1-based */
 
1218
inolog("get %dth Valid data from %d to %s [dlt=%d]", nth, sta, orientation == SQL_FETCH_PRIOR ? "backward" : "forward", res->dl_count);
 
1219
        if (0 == res->dl_count)
 
1220
        {
 
1221
                if (SQL_FETCH_PRIOR == orientation)
 
1222
                {       
 
1223
                        if (sta + 1 >= (SQLLEN) nth)
 
1224
                        {
 
1225
                                *nearest = sta + 1 - nth;
 
1226
                                return nth;
 
1227
                        }
 
1228
                        *nearest = -1;
 
1229
                        return -(SQLLEN)(sta + 1);
 
1230
                }
 
1231
                else
 
1232
                {       
 
1233
                        nearp = sta - 1 + nth;
 
1234
                        if (nearp < num_tuples)
 
1235
                        {
 
1236
                                *nearest = nearp;
 
1237
                                return nth;
 
1238
                        }
 
1239
                        *nearest = num_tuples;
 
1240
                        return -(SQLLEN)(num_tuples - sta);
 
1241
                }
 
1242
        }
 
1243
        count = 0;
 
1244
        if (QR_get_cursor(res))
 
1245
        {
 
1246
                SQLLEN  *deleted = res->deleted;
 
1247
 
 
1248
                *nearest = sta - 1 + nth;
 
1249
                if (SQL_FETCH_PRIOR == orientation)
 
1250
                {
 
1251
                        for (i = res->dl_count - 1; i >=0 && *nearest <= (SQLLEN) deleted[i]; i--)
 
1252
                        {
 
1253
inolog("deleted[%d]=%d\n", i, deleted[i]);
 
1254
                                if (sta >= (SQLLEN)deleted[i])
 
1255
                                        (*nearest)--;
 
1256
                        }
 
1257
inolog("nearest=%d\n", *nearest);
 
1258
                        if (*nearest < 0)
 
1259
                        {
 
1260
                                *nearest = -1;
 
1261
                                count = sta + 1;
 
1262
                        }
 
1263
                        else
 
1264
                                return nth;
 
1265
                }
 
1266
                else
 
1267
                {
 
1268
                        if (!QR_once_reached_eof(res))
 
1269
                                num_tuples = INT_MAX;
 
1270
                        for (i = 0; i < res->dl_count && *nearest >= (SQLLEN)deleted[i]; i++)
 
1271
                        {
 
1272
                                if (sta <= (SQLLEN)deleted[i])
 
1273
                                        (*nearest)++;
 
1274
                        }
 
1275
                        if (*nearest >= num_tuples)
 
1276
                        {
 
1277
                                *nearest = num_tuples;
 
1278
                                count = *nearest - sta;
 
1279
                        }
 
1280
                        else
 
1281
                                return nth;
 
1282
                }
 
1283
        }
 
1284
        else if (SQL_FETCH_PRIOR == orientation)
 
1285
        {
 
1286
                for (i = sta, keyset = res->keyset + sta;
 
1287
                        i >= 0; i--, keyset--)
 
1288
                {
 
1289
                        if (0 == (keyset->status & (CURS_SELF_DELETING | CURS_SELF_DELETED | CURS_OTHER_DELETED)))
 
1290
                        {
 
1291
                                *nearest = i;
 
1292
inolog(" nearest=%d\n", *nearest);
 
1293
                                if (++count == nth)
 
1294
                                        return count;
 
1295
                        }
 
1296
                }
 
1297
                *nearest = -1; 
 
1298
        }
 
1299
        else
 
1300
        {
 
1301
                for (i = sta, keyset = res->keyset + sta;
 
1302
                        i < num_tuples; i++, keyset++)
 
1303
                {
 
1304
                        if (0 == (keyset->status & (CURS_SELF_DELETING | CURS_SELF_DELETED | CURS_OTHER_DELETED)))
 
1305
                        {
 
1306
                                *nearest = i;
 
1307
inolog(" nearest=%d\n", *nearest);
 
1308
                                if (++count == nth)
 
1309
                                        return count;
 
1310
                        }
 
1311
                }
 
1312
                *nearest = num_tuples; 
 
1313
        }
 
1314
inolog(" nearest not found\n");
 
1315
        return -(SQLLEN)count;
 
1316
}
 
1317
 
 
1318
static void
 
1319
move_cursor_position_if_needed(StatementClass *self, QResultClass *res)
 
1320
{
 
1321
        SQLLEN  move_offset;
 
1322
        
 
1323
        /*
 
1324
         * The move direction must be initialized to is_not_moving or
 
1325
         * is_moving_from_the_last in advance.
 
1326
         */
 
1327
        if (!QR_get_cursor(res))
 
1328
        {
 
1329
                QR_stop_movement(res); /* for safety */
 
1330
                res->move_offset = 0;
 
1331
                return;
 
1332
        }
 
1333
inolog("BASE=%d numb=%d curr=%d cursT=%d\n", QR_get_rowstart_in_cache(res), res->num_cached_rows, self->currTuple, res->cursTuple);
 
1334
 
 
1335
        /* retrieve "move from the last" case first */
 
1336
        if (QR_is_moving_from_the_last(res))
 
1337
        {
 
1338
                mylog("must MOVE from the last\n");
 
1339
                if (QR_once_reached_eof(res) || self->rowset_start <= QR_get_num_total_tuples(res)) /* this shouldn't happen */
 
1340
                        mylog("strange situation in move from the last\n");
 
1341
                if (0 == res->move_offset)
 
1342
                        res->move_offset = INT_MAX - self->rowset_start;
 
1343
else
 
1344
{
 
1345
inolog("!!move_offset=%d calc=%d\n", res->move_offset, INT_MAX - self->rowset_start);
 
1346
}
 
1347
                return;
 
1348
        }
 
1349
 
 
1350
        /* normal case */
 
1351
        res->move_offset = 0;
 
1352
        move_offset = self->currTuple - res->cursTuple;
 
1353
        if (QR_get_rowstart_in_cache(res) >= 0 &&
 
1354
             QR_get_rowstart_in_cache(res) <= res->num_cached_rows)
 
1355
        {
 
1356
                QR_set_next_in_cache(res, (QR_get_rowstart_in_cache(res) < 0) ? 0 : QR_get_rowstart_in_cache(res));
 
1357
                return;
 
1358
        }
 
1359
        if (0 == move_offset) 
 
1360
                return;
 
1361
        if (move_offset > 0)
 
1362
        {
 
1363
                QR_set_move_forward(res);
 
1364
                res->move_offset = move_offset;
 
1365
        }
 
1366
        else
 
1367
        {
 
1368
                QR_set_move_backward(res);
 
1369
                res->move_offset = -move_offset;
 
1370
        }
 
1371
}
 
1372
/*
 
1373
 *      return NO_DATA_FOUND macros
 
1374
 *        save_rowset_start or num_tuples must be defined 
 
1375
 */
 
1376
#define EXTFETCH_RETURN_BOF(stmt, res) \
 
1377
{ \
 
1378
inolog("RETURN_BOF\n"); \
 
1379
        SC_set_rowset_start(stmt, -1, TRUE); \
 
1380
        stmt->currTuple = -1; \
 
1381
        /* move_cursor_position_if_needed(stmt, res); */ \
 
1382
        return SQL_NO_DATA_FOUND; \
 
1383
}
 
1384
#define EXTFETCH_RETURN_EOF(stmt, res) \
 
1385
{ \
 
1386
inolog("RETURN_EOF\n"); \
 
1387
        SC_set_rowset_start(stmt, num_tuples, TRUE); \
 
1388
        stmt->currTuple = -1; \
 
1389
        /* move_cursor_position_if_needed(stmt, res); */ \
 
1390
        return SQL_NO_DATA_FOUND; \
 
1391
}
 
1392
        
 
1393
/*      This fetchs a block of data (rowset). */
 
1394
RETCODE         SQL_API
 
1395
PGAPI_ExtendedFetch(
 
1396
                                        HSTMT hstmt,
 
1397
                                        SQLUSMALLINT fFetchType,
 
1398
                                        SQLLEN irow,
 
1399
                                        SQLULEN FAR * pcrow,
 
1400
                                        SQLUSMALLINT FAR * rgfRowStatus,
 
1401
                                        SQLLEN bookmark_offset,
 
1402
                                        SQLLEN rowsetSize)
 
1403
{
 
1404
        CSTR func = "PGAPI_ExtendedFetch";
 
1405
        StatementClass *stmt = (StatementClass *) hstmt;
 
1406
        ARDFields       *opts;
 
1407
        QResultClass *res;
 
1408
        BindInfoClass   *bookmark;
 
1409
        SQLLEN          num_tuples, i, fc_io;
 
1410
        SQLLEN          save_rowset_size, progress_size;
 
1411
        SQLLEN          rowset_start;
 
1412
        RETCODE         result = SQL_SUCCESS;
 
1413
        char            truncated, error, should_set_rowset_start = FALSE; 
 
1414
        SQLLEN          currp;
 
1415
        UWORD           pstatus;
 
1416
        BOOL            currp_is_valid, reached_eof, useCursor;
 
1417
 
 
1418
        mylog("%s: stmt=%p rowsetSize=%d\n", func, stmt, rowsetSize);
 
1419
 
 
1420
        if (!stmt)
 
1421
        {
 
1422
                SC_log_error(func, NULL_STRING, NULL);
 
1423
                return SQL_INVALID_HANDLE;
 
1424
        }
 
1425
 
 
1426
        /* if (SC_is_fetchcursor(stmt) && !stmt->manual_result) */
 
1427
        if (SQL_CURSOR_FORWARD_ONLY == stmt->options.cursor_type)
 
1428
        {
 
1429
                if (fFetchType != SQL_FETCH_NEXT)
 
1430
                {
 
1431
                        SC_set_error(stmt, STMT_FETCH_OUT_OF_RANGE, "The fetch type for PGAPI_ExtendedFetch isn't allowed with ForwardOnly cursor.", func);
 
1432
                        return SQL_ERROR;
 
1433
                }
 
1434
        }
 
1435
 
 
1436
        SC_clear_error(stmt);
 
1437
 
 
1438
        if (!(res = SC_get_Curres(stmt)))
 
1439
        {
 
1440
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in PGAPI_ExtendedFetch.", func);
 
1441
                return SQL_ERROR;
 
1442
        }
 
1443
 
 
1444
        opts = SC_get_ARDF(stmt);
 
1445
        /*
 
1446
         * If a bookmark colunmn is bound but bookmark usage is off, then
 
1447
         * error
 
1448
         */
 
1449
        if ((bookmark = opts->bookmark) && bookmark->buffer && stmt->options.use_bookmarks == SQL_UB_OFF)
 
1450
        {
 
1451
                SC_set_error(stmt, STMT_COLNUM_ERROR, "Attempt to retrieve bookmark with bookmark usage disabled", func);
 
1452
                return SQL_ERROR;
 
1453
        }
 
1454
 
 
1455
        if (stmt->status == STMT_EXECUTING)
 
1456
        {
 
1457
                SC_set_error(stmt, STMT_SEQUENCE_ERROR, "Can't fetch while statement is still executing.", func);
 
1458
                return SQL_ERROR;
 
1459
        }
 
1460
 
 
1461
        if (stmt->status != STMT_FINISHED)
 
1462
        {
 
1463
                SC_set_error(stmt, STMT_STATUS_ERROR, "ExtendedFetch can only be called after the successful execution on a SQL statement", func);
 
1464
                return SQL_ERROR;
 
1465
        }
 
1466
 
 
1467
        if (opts->bindings == NULL)
 
1468
        {
 
1469
                if (!SC_may_fetch_rows(stmt))
 
1470
                        return SQL_NO_DATA_FOUND;
 
1471
                /* just to avoid a crash if the user insists on calling this */
 
1472
                /* function even if SQL_ExecDirect has reported an Error */
 
1473
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Bindings were not allocated properly.", func);
 
1474
                return SQL_ERROR;
 
1475
        }
 
1476
 
 
1477
        /* Initialize to no rows fetched */
 
1478
        if (rgfRowStatus)
 
1479
                for (i = 0; i < rowsetSize; i++)
 
1480
                        *(rgfRowStatus + i) = SQL_ROW_NOROW;
 
1481
 
 
1482
        if (pcrow)
 
1483
                *pcrow = 0;
 
1484
 
 
1485
        useCursor = (SC_is_fetchcursor(stmt) && NULL != QR_get_cursor(res));
 
1486
        num_tuples = QR_get_num_total_tuples(res);
 
1487
        reached_eof = QR_once_reached_eof(res) && QR_get_cursor(res);
 
1488
        if (useCursor && !reached_eof)
 
1489
                num_tuples = INT_MAX;
 
1490
 
 
1491
inolog("num_tuples=%d\n", num_tuples);
 
1492
        /* Save and discard the saved rowset size */
 
1493
        save_rowset_size = stmt->save_rowset_size;
 
1494
        stmt->save_rowset_size = -1;
 
1495
        rowset_start = SC_get_rowset_start(stmt);
 
1496
 
 
1497
        QR_stop_movement(res);
 
1498
        res->move_offset = 0;
 
1499
        switch (fFetchType)
 
1500
        {
 
1501
                case SQL_FETCH_NEXT:
 
1502
 
 
1503
                        /*
 
1504
                         * From the odbc spec... If positioned before the start of the
 
1505
                         * RESULT SET, then this should be equivalent to
 
1506
                         * SQL_FETCH_FIRST.
 
1507
                         */
 
1508
 
 
1509
                        progress_size = (save_rowset_size > 0 ? save_rowset_size : rowsetSize);
 
1510
                        if (rowset_start < 0)
 
1511
                                SC_set_rowset_start(stmt, 0, TRUE);
 
1512
                        else if (res->keyset)
 
1513
                        {
 
1514
                                if (stmt->last_fetch_count <= progress_size)
 
1515
                                {
 
1516
                                        SC_inc_rowset_start(stmt, stmt->last_fetch_count_include_ommitted);
 
1517
                                        progress_size -= stmt->last_fetch_count;
 
1518
                                }
 
1519
                                if (progress_size > 0)
 
1520
                                {
 
1521
                                        if (getNthValid(res, SC_get_rowset_start(stmt),
 
1522
                                                SQL_FETCH_NEXT, progress_size + 1,
 
1523
                                                &rowset_start) <= 0)
 
1524
                                        {
 
1525
                                                EXTFETCH_RETURN_EOF(stmt, res)
 
1526
                                        }
 
1527
                                        else
 
1528
                                                should_set_rowset_start =TRUE;
 
1529
                                }
 
1530
                        }
 
1531
                        else
 
1532
                                SC_inc_rowset_start(stmt, progress_size);
 
1533
                        mylog("SQL_FETCH_NEXT: num_tuples=%d, currtuple=%d, rowst=%d\n", num_tuples, stmt->currTuple, rowset_start);
 
1534
                        break;
 
1535
 
 
1536
                case SQL_FETCH_PRIOR:
 
1537
                        mylog("SQL_FETCH_PRIOR: num_tuples=%d, currtuple=%d\n", num_tuples, stmt->currTuple);
 
1538
 
 
1539
                        /*
 
1540
                         * From the odbc spec... If positioned after the end of the
 
1541
                         * RESULT SET, then this should be equivalent to
 
1542
                         * SQL_FETCH_LAST.
 
1543
                         */
 
1544
                        if (SC_get_rowset_start(stmt) <= 0)
 
1545
                        {
 
1546
                                EXTFETCH_RETURN_BOF(stmt, res)
 
1547
                        }
 
1548
                        if (SC_get_rowset_start(stmt) >= num_tuples)
 
1549
                        {
 
1550
                                if (rowsetSize > num_tuples)
 
1551
                                {
 
1552
                                        SC_set_error(stmt, STMT_POS_BEFORE_RECORDSET, "fetch prior from eof and before the beginning", func);
 
1553
                                }
 
1554
                                SC_set_rowset_start(stmt, num_tuples <= 0 ? 0 : (num_tuples - rowsetSize), TRUE);
 
1555
                        }
 
1556
                        else if (QR_haskeyset(res))
 
1557
                        {
 
1558
                                if (i = getNthValid(res, SC_get_rowset_start(stmt) - 1, SQL_FETCH_PRIOR, rowsetSize, &rowset_start), i < -1)
 
1559
                                {
 
1560
                                        SC_set_error(stmt, STMT_POS_BEFORE_RECORDSET, "fetch prior and before the beggining", func);
 
1561
                                        SC_set_rowset_start(stmt, 0, TRUE);
 
1562
                                }
 
1563
                                else if (i <= 0)
 
1564
                                {
 
1565
                                        EXTFETCH_RETURN_BOF(stmt, res)
 
1566
                                }
 
1567
                                else
 
1568
                                        should_set_rowset_start = TRUE;
 
1569
                        }
 
1570
                        else if (SC_get_rowset_start(stmt) < rowsetSize)
 
1571
                        {
 
1572
                                SC_set_error(stmt, STMT_POS_BEFORE_RECORDSET, "fetch prior from eof and before the beggining", func);
 
1573
                                SC_set_rowset_start(stmt, 0, TRUE);
 
1574
                        }
 
1575
                        else
 
1576
                                SC_inc_rowset_start(stmt, -rowsetSize);
 
1577
                        break;
 
1578
 
 
1579
                case SQL_FETCH_FIRST:
 
1580
                        mylog("SQL_FETCH_FIRST: num_tuples=%d, currtuple=%d\n", num_tuples, stmt->currTuple);
 
1581
 
 
1582
                        SC_set_rowset_start(stmt, 0, TRUE);
 
1583
                        break;
 
1584
 
 
1585
                case SQL_FETCH_LAST:
 
1586
                        mylog("SQL_FETCH_LAST: num_tuples=%d, currtuple=%d\n", num_tuples, stmt->currTuple);
 
1587
 
 
1588
                        if (!reached_eof)
 
1589
                        {
 
1590
                                QR_set_move_from_the_last(res);
 
1591
                                res->move_offset = rowsetSize;
 
1592
                        }
 
1593
                        SC_set_rowset_start(stmt, num_tuples <= 0 ? 0 : (num_tuples - rowsetSize), TRUE);
 
1594
                        break;
 
1595
 
 
1596
                case SQL_FETCH_ABSOLUTE:
 
1597
                        mylog("SQL_FETCH_ABSOLUTE: num_tuples=%d, currtuple=%d, irow=%d\n", num_tuples, stmt->currTuple, irow);
 
1598
 
 
1599
                        /* Position before result set, but dont fetch anything */
 
1600
                        if (irow == 0)
 
1601
                        {
 
1602
                                EXTFETCH_RETURN_BOF(stmt, res)
 
1603
                        }
 
1604
                        /* Position before the desired row */
 
1605
                        else if (irow > 0)
 
1606
                        {
 
1607
                                if (getNthValid(res, 0, SQL_FETCH_NEXT, irow, &rowset_start) <= 0)
 
1608
                                {
 
1609
                                        EXTFETCH_RETURN_EOF(stmt, res)
 
1610
                                }
 
1611
                                else
 
1612
                                        should_set_rowset_start = TRUE;
 
1613
                        }
 
1614
                        /* Position with respect to the end of the result set */
 
1615
                        else
 
1616
                        {
 
1617
                                if (getNthValid(res, num_tuples - 1, SQL_FETCH_PRIOR, -irow, &rowset_start) <= 0)
 
1618
                                {
 
1619
                                        EXTFETCH_RETURN_BOF(stmt, res)
 
1620
                                }
 
1621
                                else
 
1622
                                {
 
1623
                                        if (!reached_eof)
 
1624
                                        {
 
1625
                                                QR_set_move_from_the_last(res);
 
1626
                                                res->move_offset = -irow;
 
1627
                                        }
 
1628
                                        should_set_rowset_start = TRUE;
 
1629
                                }
 
1630
                        }
 
1631
                        break;
 
1632
 
 
1633
                case SQL_FETCH_RELATIVE:
 
1634
 
 
1635
                        /*
 
1636
                         * Refresh the current rowset -- not currently implemented,
 
1637
                         * but lie anyway
 
1638
                         */
 
1639
                        if (irow == 0)
 
1640
                                break;
 
1641
 
 
1642
                        if (irow > 0)
 
1643
                        {
 
1644
                                if (getNthValid(res, SC_get_rowset_start(stmt) + 1, SQL_FETCH_NEXT, irow, &rowset_start) <= 0)
 
1645
                                {
 
1646
                                        EXTFETCH_RETURN_EOF(stmt, res)
 
1647
                                }
 
1648
                                else
 
1649
                                        should_set_rowset_start = TRUE;
 
1650
                        }
 
1651
                        else
 
1652
                        {
 
1653
                                if (getNthValid(res, SC_get_rowset_start(stmt) - 1, SQL_FETCH_PRIOR, -irow, &rowset_start) <= 0)
 
1654
                                {
 
1655
                                        EXTFETCH_RETURN_BOF(stmt, res)
 
1656
                                }
 
1657
                                else
 
1658
                                        should_set_rowset_start = TRUE;
 
1659
                        }
 
1660
                        break;
 
1661
 
 
1662
                case SQL_FETCH_BOOKMARK:
 
1663
                        {
 
1664
                        SQLLEN  bidx = SC_resolve_bookmark(irow);
 
1665
 
 
1666
                        if (bidx < 0)
 
1667
                        {
 
1668
                                if (!reached_eof)
 
1669
                                {
 
1670
                                        QR_set_move_from_the_last(res);
 
1671
                                        res->move_offset = 1 + res->ad_count + bidx;
 
1672
                                }
 
1673
                                bidx = num_tuples - 1 - res->ad_count - bidx;
 
1674
                        } 
 
1675
 
 
1676
                        rowset_start = bidx;
 
1677
                        if (bookmark_offset >= 0)
 
1678
                        {
 
1679
                                if (getNthValid(res, bidx, SQL_FETCH_NEXT, bookmark_offset + 1, &rowset_start) <= 0)
 
1680
                                {
 
1681
                                        EXTFETCH_RETURN_EOF(stmt, res)
 
1682
                                }
 
1683
                                else
 
1684
                                        should_set_rowset_start = TRUE;
 
1685
                        }
 
1686
                        else if (getNthValid(res, bidx, SQL_FETCH_PRIOR, 1 - bookmark_offset, &rowset_start) <= 0)
 
1687
                        {
 
1688
                                stmt->currTuple = -1;
 
1689
                                EXTFETCH_RETURN_BOF(stmt, res)
 
1690
                        }
 
1691
                        else
 
1692
                                should_set_rowset_start = TRUE;
 
1693
                        }
 
1694
                        break;
 
1695
 
 
1696
                default:
 
1697
                        SC_set_error(stmt, STMT_FETCH_OUT_OF_RANGE, "Unsupported PGAPI_ExtendedFetch Direction", func);
 
1698
                        return SQL_ERROR;
 
1699
        }
 
1700
 
 
1701
        /*
 
1702
         * CHECK FOR PROPER CURSOR STATE
 
1703
         */
 
1704
 
 
1705
        /*
 
1706
         * Handle Declare Fetch style specially because the end is not really
 
1707
         * the end...
 
1708
         */
 
1709
        if (!should_set_rowset_start)
 
1710
                rowset_start = SC_get_rowset_start(stmt);
 
1711
        if (useCursor)
 
1712
        {
 
1713
                if (reached_eof &&
 
1714
                    rowset_start >= num_tuples)
 
1715
                {
 
1716
                        EXTFETCH_RETURN_EOF(stmt, res)
 
1717
                }
 
1718
        }
 
1719
        else
 
1720
        {
 
1721
                /* If *new* rowset is after the result_set, return no data found */
 
1722
                if (rowset_start >= num_tuples)
 
1723
                {
 
1724
                        EXTFETCH_RETURN_EOF(stmt, res)
 
1725
                }
 
1726
        }
 
1727
        /* If *new* rowset is prior to result_set, return no data found */
 
1728
        if (rowset_start < 0)
 
1729
        {
 
1730
                if (rowset_start + rowsetSize <= 0)
 
1731
                {
 
1732
                        EXTFETCH_RETURN_BOF(stmt, res)
 
1733
                }
 
1734
                else
 
1735
                {       /* overlap with beginning of result set,
 
1736
                         * so get first rowset */
 
1737
                        SC_set_rowset_start(stmt, 0, TRUE);
 
1738
                }
 
1739
                should_set_rowset_start = FALSE;
 
1740
        }
 
1741
 
 
1742
#define return DONT_CALL_RETURN_FROM_HERE???
 
1743
        /* increment the base row in the tuple cache */
 
1744
        QR_set_rowset_size(res, (Int4) rowsetSize);
 
1745
        /* set the rowset_start if needed */
 
1746
        if (should_set_rowset_start)
 
1747
                SC_set_rowset_start(stmt, rowset_start, TRUE);
 
1748
        /* currTuple is always 1 row prior to the rowset start */
 
1749
        stmt->currTuple = RowIdx2GIdx(-1, stmt);
 
1750
 
 
1751
        if (SC_is_fetchcursor(stmt) ||
 
1752
            SQL_CURSOR_KEYSET_DRIVEN == stmt->options.cursor_type)
 
1753
        {
 
1754
                move_cursor_position_if_needed(stmt, res);
 
1755
        }
 
1756
        else
 
1757
                QR_set_rowstart_in_cache(res, SC_get_rowset_start(stmt));
 
1758
 
 
1759
        if (res->keyset && !QR_get_cursor(res))
 
1760
        {
 
1761
                UDWORD  flag = 0;
 
1762
                SQLLEN  rowset_end, req_size;
 
1763
 
 
1764
                getNthValid(res, rowset_start, SQL_FETCH_NEXT, rowsetSize, &rowset_end);
 
1765
                req_size = rowset_end - rowset_start + 1;
 
1766
                if (SQL_CURSOR_KEYSET_DRIVEN == stmt->options.cursor_type)
 
1767
                {
 
1768
                        if (fFetchType != SQL_FETCH_NEXT ||
 
1769
                                QR_get_rowstart_in_cache(res) + req_size > QR_get_num_cached_tuples(res))
 
1770
                                flag = 1;
 
1771
                }
 
1772
                if (SQL_RD_ON == stmt->options.retrieve_data ||
 
1773
                    flag != 0)
 
1774
                {
 
1775
                        SC_pos_reload_needed(stmt, req_size, flag);
 
1776
                }
 
1777
        }
 
1778
        /* Physical Row advancement occurs for each row fetched below */
 
1779
 
 
1780
        mylog("PGAPI_ExtendedFetch: new currTuple = %d\n", stmt->currTuple);
 
1781
 
 
1782
        truncated = error = FALSE;
 
1783
 
 
1784
        currp = -1;
 
1785
        stmt->bind_row = 0;             /* set the binding location */
 
1786
        result = SC_fetch(stmt);
 
1787
        if (SQL_ERROR == result)
 
1788
                goto cleanup;
 
1789
        if (SQL_NO_DATA_FOUND != result && res->keyset)
 
1790
        {
 
1791
                currp = GIdx2KResIdx(SC_get_rowset_start(stmt), stmt, res);
 
1792
inolog("currp=%d\n", currp);
 
1793
                if (currp < 0)
 
1794
                {
 
1795
                        result = SQL_ERROR;
 
1796
                        mylog("rowset_start=%d but currp=%d\n", SC_get_rowset_start(stmt), currp);
 
1797
                        SC_set_error(stmt, STMT_INTERNAL_ERROR, "rowset_start not in the keyset", func);
 
1798
                        goto cleanup;
 
1799
                }
 
1800
        }
 
1801
        for (i = 0, fc_io = 0; SQL_NO_DATA_FOUND != result && SQL_ERROR != result; currp++)
 
1802
        {
 
1803
                fc_io++;
 
1804
                currp_is_valid = FALSE;
 
1805
                if (res->keyset)
 
1806
                {
 
1807
                        if (currp < res->num_cached_keys)
 
1808
                        {
 
1809
                                currp_is_valid = TRUE;
 
1810
                                res->keyset[currp].status &= ~CURS_IN_ROWSET; /* Off the flag first */
 
1811
                        }
 
1812
                        else
 
1813
                        {
 
1814
                                mylog("Umm current row is out of keyset\n");
 
1815
                                break;
 
1816
                        }
 
1817
                }
 
1818
inolog("ExtFetch result=%d\n", result);
 
1819
                if (currp_is_valid && SQL_SUCCESS_WITH_INFO == result && 0 == stmt->last_fetch_count)
 
1820
                {
 
1821
inolog("just skipping deleted row %d\n", currp);
 
1822
                        QR_set_rowset_size(res, (Int4) (rowsetSize - i + fc_io));
 
1823
                        result = SC_fetch(stmt);
 
1824
                        if (SQL_ERROR == result)
 
1825
                                break;
 
1826
                        continue;
 
1827
                }
 
1828
 
 
1829
                /* Determine Function status */
 
1830
                if (result == SQL_SUCCESS_WITH_INFO)
 
1831
                        truncated = TRUE;
 
1832
                else if (result == SQL_ERROR)
 
1833
                        error = TRUE;
 
1834
 
 
1835
                /* Determine Row Status */
 
1836
                if (rgfRowStatus)
 
1837
                {
 
1838
                        if (result == SQL_ERROR)
 
1839
                                *(rgfRowStatus + i) = SQL_ROW_ERROR;
 
1840
                        else if (currp_is_valid)
 
1841
                        {
 
1842
                                pstatus = (res->keyset[currp].status & KEYSET_INFO_PUBLIC);
 
1843
                                if (pstatus != 0 && pstatus != SQL_ROW_ADDED)
 
1844
                                {
 
1845
                                        rgfRowStatus[i] = pstatus;
 
1846
                                }
 
1847
                                else
 
1848
                                        rgfRowStatus[i] = SQL_ROW_SUCCESS;
 
1849
                                /* refresh the status */
 
1850
                                /* if (SQL_ROW_DELETED != pstatus) */
 
1851
                                res->keyset[currp].status &= (~KEYSET_INFO_PUBLIC);
 
1852
                        }
 
1853
                        else
 
1854
                                *(rgfRowStatus + i) = SQL_ROW_SUCCESS;
 
1855
                }
 
1856
                if (SQL_ERROR != result && currp_is_valid)
 
1857
                        res->keyset[currp].status |= CURS_IN_ROWSET; /* This is the unique place where the CURS_IN_ROWSET bit is turned on */
 
1858
                i++;
 
1859
                if (i >= rowsetSize)
 
1860
                        break;
 
1861
                stmt->bind_row = (SQLSETPOSIROW) i; /* set the binding location */
 
1862
                result = SC_fetch(stmt);
 
1863
        }
 
1864
        if (SQL_ERROR == result)
 
1865
                goto cleanup;
 
1866
 
 
1867
        /* Save the fetch count for SQLSetPos */
 
1868
        stmt->last_fetch_count = i;
 
1869
        stmt->save_rowset_size = rowsetSize;
 
1870
        /*
 
1871
        currp = KResIdx2GIdx(currp, stmt, res);
 
1872
        stmt->last_fetch_count_include_ommitted = GIdx2RowIdx(currp, stmt);
 
1873
        */
 
1874
        stmt->last_fetch_count_include_ommitted = fc_io;
 
1875
 
 
1876
        /* Reset next binding row */
 
1877
        stmt->bind_row = 0;
 
1878
 
 
1879
        /* Move the cursor position to the first row in the result set. */
 
1880
        stmt->currTuple = RowIdx2GIdx(0, stmt);
 
1881
 
 
1882
        /* For declare/fetch, need to reset cursor to beginning of rowset */
 
1883
        if (useCursor)
 
1884
                QR_set_position(res, 0);
 
1885
 
 
1886
        /* Set the number of rows retrieved */
 
1887
        if (pcrow)
 
1888
                *pcrow = i;
 
1889
inolog("pcrow=%d\n", i);
 
1890
 
 
1891
        if (i == 0)
 
1892
                /* Only DeclareFetch should wind up here */
 
1893
                result = SQL_NO_DATA_FOUND;
 
1894
        else if (error)
 
1895
                result = SQL_ERROR;
 
1896
        else if (truncated)
 
1897
                result = SQL_SUCCESS_WITH_INFO;
 
1898
        else if (SC_get_errornumber(stmt) == STMT_POS_BEFORE_RECORDSET)
 
1899
                result = SQL_SUCCESS_WITH_INFO;
 
1900
        else
 
1901
                result = SQL_SUCCESS;
 
1902
 
 
1903
cleanup:
 
1904
#undef  return
 
1905
        if (stmt->internal)
 
1906
                result = DiscardStatementSvp(stmt, result, FALSE);
 
1907
        return result;
 
1908
}
 
1909
 
 
1910
 
 
1911
/*
 
1912
 *              This determines whether there are more results sets available for
 
1913
 *              the "hstmt".
 
1914
 */
 
1915
/* CC: return SQL_NO_DATA_FOUND since we do not support multiple result sets */
 
1916
RETCODE         SQL_API
 
1917
PGAPI_MoreResults(
 
1918
                                  HSTMT hstmt)
 
1919
{
 
1920
        CSTR func = "PGAPI_MoreResults";
 
1921
        StatementClass  *stmt = (StatementClass *) hstmt;
 
1922
        QResultClass    *res;
 
1923
        RETCODE         ret = SQL_SUCCESS;
 
1924
 
 
1925
        mylog("%s: entering...\n", func);
 
1926
        if (stmt && (res = SC_get_Curres(stmt)))
 
1927
                SC_set_Curres(stmt, res->next);
 
1928
        if (res = SC_get_Curres(stmt), res)
 
1929
        {
 
1930
                SQLSMALLINT     num_p;
 
1931
 
 
1932
                if (stmt->multi_statement < 0)
 
1933
                        PGAPI_NumParams(stmt, &num_p);
 
1934
                if (stmt->multi_statement > 0)
 
1935
                { 
 
1936
                        const char *cmdstr;
 
1937
 
 
1938
                        SC_initialize_cols_info(stmt, FALSE, TRUE);
 
1939
                        stmt->statement_type = STMT_TYPE_UNKNOWN;
 
1940
                        if (cmdstr = QR_get_command(res), NULL != cmdstr)
 
1941
                                stmt->statement_type = statement_type(cmdstr);
 
1942
                        stmt->join_info = 0;
 
1943
                        SC_clear_parse_method(stmt);
 
1944
                }
 
1945
                stmt->diag_row_count = res->recent_processed_row_count;
 
1946
                SC_set_rowset_start(stmt, -1, FALSE);
 
1947
                stmt->currTuple = -1;
 
1948
        }
 
1949
        else
 
1950
        {
 
1951
                PGAPI_FreeStmt(hstmt, SQL_CLOSE);
 
1952
                ret = SQL_NO_DATA_FOUND;
 
1953
        }
 
1954
        mylog("%s: returning %d\n", func, ret);
 
1955
        return ret;
 
1956
}
 
1957
 
 
1958
 
 
1959
/*
 
1960
 *      Stuff for updatable cursors.
 
1961
 */
 
1962
static Int2     getNumResultCols(const QResultClass *res)
 
1963
{
 
1964
        Int2    res_cols = QR_NumPublicResultCols(res);
 
1965
        return res_cols;
 
1966
}
 
1967
static OID      getOid(const QResultClass *res, SQLLEN index)
 
1968
{
 
1969
        return res->keyset[index].oid;
 
1970
}
 
1971
static void getTid(const QResultClass *res, SQLLEN index, UInt4 *blocknum, UInt2 *offset)
 
1972
{
 
1973
        *blocknum = res->keyset[index].blocknum;
 
1974
        *offset = res->keyset[index].offset;
 
1975
}
 
1976
static void KeySetSet(const TupleField *tuple, int num_fields, int num_key_fields, KeySet *keyset)
 
1977
{
 
1978
        sscanf(tuple[num_fields - num_key_fields].value, "(%u,%hu)",
 
1979
                        &keyset->blocknum, &keyset->offset);
 
1980
        if (num_key_fields > 1)
 
1981
                sscanf(tuple[num_fields - 1].value, "%u", &keyset->oid);
 
1982
        else
 
1983
                keyset->oid = 0;
 
1984
}
 
1985
 
 
1986
static void AddRollback(StatementClass *stmt, QResultClass *res, SQLLEN index, const KeySet *keyset, Int4 dmlcode)
 
1987
{
 
1988
        ConnectionClass *conn = SC_get_conn(stmt);
 
1989
        Rollback *rollback;
 
1990
 
 
1991
        if (!CC_is_in_trans(conn))
 
1992
                return;
 
1993
inolog("AddRollback %d(%d,%d) %s\n", index, keyset->blocknum, keyset->offset, dmlcode == SQL_ADD ? "ADD" : (dmlcode == SQL_UPDATE ? "UPDATE" : (dmlcode == SQL_DELETE ? "DELETE" : "REFRESH")));
 
1994
        if (!res->rollback)
 
1995
        {
 
1996
                res->rb_count = 0;
 
1997
                res->rb_alloc = 10;
 
1998
                rollback = res->rollback = malloc(sizeof(Rollback) * res->rb_alloc);
 
1999
        }
 
2000
        else
 
2001
        {
 
2002
                if (res->rb_count >= res->rb_alloc)
 
2003
                {
 
2004
                        res->rb_alloc *= 2; 
 
2005
                        if (rollback = realloc(res->rollback, sizeof(Rollback) * res->rb_alloc), !rollback)
 
2006
                        {
 
2007
                                res->rb_alloc = res->rb_count = 0;
 
2008
                                return;
 
2009
                        }
 
2010
                        res->rollback = rollback; 
 
2011
                }
 
2012
                rollback = res->rollback + res->rb_count;
 
2013
        }
 
2014
        rollback->index = index;
 
2015
        rollback->option = dmlcode;
 
2016
        rollback->offset = 0;
 
2017
        rollback->blocknum = 0;
 
2018
        if (keyset)
 
2019
        {
 
2020
                rollback->blocknum = keyset->blocknum;
 
2021
                rollback->offset = keyset->offset;
 
2022
        }
 
2023
 
 
2024
        conn->result_uncommitted = 1;
 
2025
        res->rb_count++;        
 
2026
}
 
2027
 
 
2028
SQLLEN ClearCachedRows(TupleField *tuple, int num_fields, SQLLEN num_rows)
 
2029
{
 
2030
        SQLLEN  i;
 
2031
 
 
2032
        for (i = 0; i < num_fields * num_rows; i++, tuple++)
 
2033
        {
 
2034
                if (tuple->value)
 
2035
                {
 
2036
inolog("freeing tuple[%d][%d].value=%p\n", i / num_fields, i % num_fields, tuple->value);
 
2037
                        free(tuple->value);
 
2038
                        tuple->value = NULL;
 
2039
                }
 
2040
                tuple->len = -1;
 
2041
        }
 
2042
        return i;
 
2043
}
 
2044
SQLLEN ReplaceCachedRows(TupleField *otuple, const TupleField *ituple, int num_fields, SQLLEN num_rows)
 
2045
{
 
2046
        SQLLEN  i;
 
2047
 
 
2048
inolog("ReplaceCachedRows %p num_fields=%d num_rows=%d\n", otuple, num_fields, num_rows);
 
2049
        for (i = 0; i < num_fields * num_rows; i++, ituple++, otuple++)
 
2050
        {
 
2051
                if (otuple->value)
 
2052
                {
 
2053
                        free(otuple->value);
 
2054
                        otuple->value = NULL;
 
2055
                }
 
2056
                if (ituple->value)
 
2057
{
 
2058
                        otuple->value = strdup(ituple->value);
 
2059
inolog("[%d,%d] %s copied\n", i / num_fields, i % num_fields, otuple->value);
 
2060
}
 
2061
                otuple->len = ituple->len;
 
2062
        }
 
2063
        return i;
 
2064
}
 
2065
 
 
2066
static
 
2067
int MoveCachedRows(TupleField *otuple, TupleField *ituple, Int2 num_fields, SQLLEN num_rows)
 
2068
{
 
2069
        int     i;
 
2070
 
 
2071
inolog("MoveCachedRows %p num_fields=%d num_rows=%d\n", otuple, num_fields, num_rows);
 
2072
        for (i = 0; i < num_fields * num_rows; i++, ituple++, otuple++)
 
2073
        {
 
2074
                if (otuple->value)
 
2075
                {
 
2076
                        free(otuple->value);
 
2077
                        otuple->value = NULL;
 
2078
                }
 
2079
                if (ituple->value)
 
2080
                {
 
2081
                        otuple->value = ituple->value;
 
2082
                        ituple->value = NULL;
 
2083
inolog("[%d,%d] %s copied\n", i / num_fields, i % num_fields, otuple->value);
 
2084
                }
 
2085
                otuple->len = ituple->len;
 
2086
                ituple->len = -1;
 
2087
        }
 
2088
        return i;
 
2089
}
 
2090
 
 
2091
static BOOL     tupleExists(const StatementClass *stmt, const KeySet *keyset)
 
2092
{
 
2093
        char    selstr[256];
 
2094
        const TABLE_INFO        *ti = stmt->ti[0];
 
2095
        QResultClass    *res;
 
2096
        RETCODE         ret = FALSE;
 
2097
 
 
2098
        if (NAME_IS_VALID(ti->schema_name))
 
2099
                snprintf(selstr, sizeof(selstr), "select 1 from \"%s\".\"%s\" where ctid = '(%d,%d)'",
 
2100
                        SAFE_NAME(ti->schema_name), SAFE_NAME(ti->table_name), keyset->blocknum, keyset->offset);
 
2101
        else
 
2102
                snprintf(selstr, sizeof(selstr), "select 1 from \"%s\" where ctid = '(%d,%d)'",
 
2103
                        SAFE_NAME(ti->table_name), keyset->blocknum, keyset->offset);
 
2104
        res = CC_send_query(SC_get_conn(stmt), selstr, NULL, 0, NULL);
 
2105
        if (QR_command_maybe_successful(res) && 1 == res->num_cached_rows)
 
2106
                ret = TRUE;
 
2107
        QR_Destructor(res);
 
2108
        return ret;
 
2109
}
 
2110
 
 
2111
static BOOL enlargeAdded(QResultClass *res, UInt4 number, const StatementClass *stmt)
 
2112
{
 
2113
        UInt4   alloc;
 
2114
        int     num_fields = res->num_fields;
 
2115
 
 
2116
        alloc = res->ad_alloc;
 
2117
        if (0 == alloc)
 
2118
                alloc = number > 10 ? number : 10;
 
2119
        else
 
2120
                while (alloc < number)
 
2121
                {
 
2122
                        alloc *= 2;
 
2123
                }
 
2124
 
 
2125
        if (alloc <= res->ad_alloc)
 
2126
                return TRUE;
 
2127
        QR_REALLOC_return_with_error(res->added_keyset, KeySet, sizeof(KeySet) * alloc, res, "enlargeAdded failed", FALSE);
 
2128
        if (SQL_CURSOR_KEYSET_DRIVEN != stmt->options.cursor_type)
 
2129
                QR_REALLOC_return_with_error(res->added_tuples, TupleField, sizeof(TupleField) * num_fields * alloc, res, "enlargeAdded failed 2", FALSE);
 
2130
        res->ad_alloc = alloc;
 
2131
        return TRUE;
 
2132
}
 
2133
static void AddAdded(StatementClass *stmt, QResultClass *res, SQLLEN index, const TupleField *tuple_added)
 
2134
{
 
2135
        KeySet  *added_keyset, *keyset, keys;
 
2136
        TupleField      *added_tuples = NULL, *tuple;
 
2137
        UInt4   ad_count;
 
2138
        Int2    num_fields;
 
2139
 
 
2140
        if (!res)       return;
 
2141
        num_fields = res->num_fields;
 
2142
inolog("AddAdded index=%d, tuple=%p, num_fields=%d\n", index, tuple_added, num_fields);
 
2143
        ad_count = res->ad_count;
 
2144
        res->ad_count++;
 
2145
        if (QR_get_cursor(res))
 
2146
                index = -(SQLLEN)res->ad_count;
 
2147
        if (!tuple_added)
 
2148
                return;
 
2149
        KeySetSet(tuple_added, num_fields + res->num_key_fields, res->num_key_fields, &keys);
 
2150
        keys.status = SQL_ROW_ADDED;
 
2151
        if (CC_is_in_trans(SC_get_conn(stmt)))
 
2152
                keys.status |= CURS_SELF_ADDING;
 
2153
        else
 
2154
                keys.status |= CURS_SELF_ADDED;
 
2155
        AddRollback(stmt, res, index, &keys, SQL_ADD);
 
2156
 
 
2157
        if (!QR_get_cursor(res))
 
2158
                return;
 
2159
        if (ad_count > 0 && 0 == res->ad_alloc)
 
2160
                return;
 
2161
        if (!enlargeAdded(res, ad_count + 1, stmt))
 
2162
                return;
 
2163
        added_keyset = res->added_keyset; 
 
2164
        added_tuples = res->added_tuples;
 
2165
 
 
2166
        keyset = added_keyset + ad_count;
 
2167
        *keyset = keys; 
 
2168
        if (added_tuples)
 
2169
        {
 
2170
                tuple = added_tuples + num_fields * ad_count;
 
2171
                memset(tuple, 0, sizeof(TupleField) * num_fields);
 
2172
                ReplaceCachedRows(tuple, tuple_added, num_fields, 1);
 
2173
        }
 
2174
}
 
2175
 
 
2176
static  void RemoveAdded(QResultClass *, SQLLEN);
 
2177
static  void RemoveUpdated(QResultClass *, SQLLEN);
 
2178
static  void RemoveUpdatedAfterTheKey(QResultClass *, SQLLEN, const KeySet*);
 
2179
static  void RemoveDeleted(QResultClass *, SQLLEN);
 
2180
static  void RemoveAdded(QResultClass *res, SQLLEN index)
 
2181
{
 
2182
        SQLLEN  rmidx, mv_count;
 
2183
        Int2    num_fields = res->num_fields;
 
2184
        KeySet  *added_keyset;
 
2185
        TupleField      *added_tuples;
 
2186
 
 
2187
        mylog("RemoveAdded index=%d\n", index);
 
2188
        if (index < 0)
 
2189
                rmidx = -index - 1;
 
2190
        else
 
2191
                rmidx = index - res->num_total_read;
 
2192
        if (rmidx >= res->ad_count)
 
2193
                return;
 
2194
        added_keyset = res->added_keyset + rmidx;
 
2195
        added_tuples = res->added_tuples + num_fields * rmidx;
 
2196
        ClearCachedRows(added_tuples, num_fields, 1);
 
2197
        mv_count = res->ad_count - rmidx - 1;
 
2198
        if (mv_count > 0)
 
2199
        {
 
2200
                memmove(added_keyset, added_keyset + 1, mv_count * sizeof(KeySet));
 
2201
                memmove(added_tuples, added_tuples + num_fields, mv_count * num_fields * sizeof(TupleField));
 
2202
        }
 
2203
        RemoveDeleted(res, index);
 
2204
        RemoveUpdated(res, index);
 
2205
        res->ad_count--;
 
2206
        mylog("RemoveAdded removed=1 count=%d\n", res->ad_count);
 
2207
}
 
2208
 
 
2209
static void CommitAdded(QResultClass *res)
 
2210
{
 
2211
        KeySet  *added_keyset;
 
2212
        int     i;
 
2213
        UWORD   status;
 
2214
 
 
2215
        mylog("CommitAdded res=%p\n", res);
 
2216
        if (!res || !res->added_keyset) return;
 
2217
        added_keyset = res->added_keyset;
 
2218
        for (i = res->ad_count - 1; i >= 0; i--)
 
2219
        {
 
2220
                status = added_keyset[i].status;
 
2221
                if (0 != (status & CURS_SELF_ADDING))
 
2222
                {
 
2223
                        status |= CURS_SELF_ADDED;
 
2224
                        status &= ~CURS_SELF_ADDING;
 
2225
                }
 
2226
                if (0 != (status & CURS_SELF_UPDATING))
 
2227
                {
 
2228
                        status |= CURS_SELF_UPDATED;
 
2229
                        status &= ~CURS_SELF_UPDATING;
 
2230
                }
 
2231
                if (0 != (status & CURS_SELF_DELETING))
 
2232
                {
 
2233
                        status |= CURS_SELF_DELETED;
 
2234
                        status &= ~CURS_SELF_DELETING;
 
2235
                }
 
2236
                if (status != added_keyset[i].status)
 
2237
                {
 
2238
inolog("!!Commit Added=%d(%d)\n", QR_get_num_total_read(res) + i, i);
 
2239
                        added_keyset[i].status = status;
 
2240
                }
 
2241
        }
 
2242
}
 
2243
 
 
2244
 
 
2245
int AddDeleted(QResultClass *res, SQLULEN index, KeySet *keyset)
 
2246
{
 
2247
        int     i;
 
2248
        Int2    dl_count, new_alloc;
 
2249
        SQLULEN *deleted;
 
2250
        KeySet  *deleted_keyset;
 
2251
        UWORD   status;
 
2252
        Int2    num_fields = res->num_fields;
 
2253
 
 
2254
inolog("AddDeleted %d\n", index);
 
2255
        if (!res)       return FALSE;
 
2256
        dl_count = res->dl_count;
 
2257
        res->dl_count++;
 
2258
        if (!QR_get_cursor(res))
 
2259
                return TRUE;
 
2260
        if (!res->deleted)
 
2261
        {
 
2262
                dl_count = 0;
 
2263
                new_alloc = 10;
 
2264
                QR_MALLOC_return_with_error(res->deleted, SQLULEN, sizeof(SQLULEN) * new_alloc, res, "Deleted index malloc error", FALSE);
 
2265
                QR_MALLOC_return_with_error(res->deleted_keyset, KeySet, sizeof(KeySet) * new_alloc, res, "Deleted keyset malloc error", FALSE);
 
2266
                deleted = res->deleted;
 
2267
                deleted_keyset = res->deleted_keyset;
 
2268
                res->dl_alloc = new_alloc;
 
2269
        }
 
2270
        else
 
2271
        {
 
2272
                if (dl_count >= res->dl_alloc)
 
2273
                {
 
2274
                        new_alloc = res->dl_alloc * 2;
 
2275
                        res->dl_alloc = 0;
 
2276
                        QR_REALLOC_return_with_error(res->deleted, SQLULEN, sizeof(SQLULEN) * new_alloc, res, "Deleted index realloc error", FALSE);
 
2277
                        deleted = res->deleted;
 
2278
                        QR_REALLOC_return_with_error(res->deleted_keyset, KeySet, sizeof(KeySet) * new_alloc, res, "Deleted KeySet realloc error", FALSE);
 
2279
                        deleted_keyset = res->deleted_keyset;
 
2280
                        res->dl_alloc = new_alloc; 
 
2281
                }
 
2282
                /* sort deleted indexes in ascending order */
 
2283
                for (i = 0, deleted = res->deleted, deleted_keyset = res->deleted_keyset; i < dl_count; i++, deleted++, deleted_keyset += num_fields)
 
2284
                {
 
2285
                        if (index < *deleted)
 
2286
                                break;
 
2287
                }
 
2288
                memmove(deleted + 1, deleted, sizeof(SQLLEN) * (dl_count - i)); 
 
2289
                memmove(deleted_keyset + 1, deleted_keyset, sizeof(KeySet) * (dl_count - i)); 
 
2290
        }
 
2291
        *deleted = index;
 
2292
        *deleted_keyset = *keyset;
 
2293
        status = keyset->status;
 
2294
        status &= (~KEYSET_INFO_PUBLIC);
 
2295
        status |= SQL_ROW_DELETED;
 
2296
        if (CC_is_in_trans(QR_get_conn(res)))
 
2297
        {
 
2298
                status |= CURS_SELF_DELETING;
 
2299
                QR_get_conn(res)->result_uncommitted = 1;
 
2300
        }
 
2301
        else
 
2302
        {
 
2303
                status &= ~(CURS_SELF_ADDING | CURS_SELF_UPDATING | CURS_SELF_DELETING);
 
2304
                status |= CURS_SELF_DELETED;
 
2305
        }
 
2306
        deleted_keyset->status = status;
 
2307
        res->dl_count = dl_count + 1;
 
2308
 
 
2309
        return TRUE;
 
2310
}
 
2311
 
 
2312
static void RemoveDeleted(QResultClass *res, SQLLEN index)
 
2313
{
 
2314
        int     i, mv_count, rm_count = 0;
 
2315
        SQLLEN  pidx, midx;
 
2316
        SQLULEN *deleted, num_read = QR_get_num_total_read(res);
 
2317
        KeySet  *deleted_keyset;
 
2318
 
 
2319
        mylog("RemoveDeleted index=%d\n", index);
 
2320
        if (index < 0)
 
2321
        {
 
2322
                midx = index;
 
2323
                pidx = num_read - index - 1;
 
2324
        }
 
2325
        else
 
2326
        {
 
2327
                pidx = index;
 
2328
                if (index >= num_read)
 
2329
                        midx = num_read - index - 1;
 
2330
                else
 
2331
                        midx = index;
 
2332
        }
 
2333
        for (i = 0; i < res->dl_count; i++)
 
2334
        {
 
2335
                if (pidx == res->deleted[i] ||
 
2336
                    midx == res->deleted[i])
 
2337
                {
 
2338
                        mv_count = res->dl_count - i - 1;
 
2339
                        if (mv_count > 0)
 
2340
                        {
 
2341
                                deleted = res->deleted + i;
 
2342
                                deleted_keyset = res->deleted_keyset + i;
 
2343
                                memmove(deleted, deleted + 1, mv_count * sizeof(SQLULEN));
 
2344
                                memmove(deleted_keyset, deleted_keyset + 1, mv_count * sizeof(KeySet));
 
2345
                        }
 
2346
                        res->dl_count--;
 
2347
                        rm_count++;             
 
2348
                }
 
2349
        }
 
2350
        mylog("RemoveDeleted removed count=%d,%d\n", rm_count, res->dl_count);
 
2351
}
 
2352
 
 
2353
static void CommitDeleted(QResultClass *res)
 
2354
{
 
2355
        int     i;
 
2356
        SQLULEN *deleted;
 
2357
        KeySet  *deleted_keyset;
 
2358
        UWORD   status;
 
2359
 
 
2360
        if (!res->deleted)
 
2361
                return;
 
2362
 
 
2363
        for (i = 0, deleted = res->deleted, deleted_keyset = res->deleted_keyset; i < res->dl_count; i++, deleted++, deleted_keyset++)
 
2364
        {
 
2365
                status = deleted_keyset->status;
 
2366
                if (0 != (status & CURS_SELF_ADDING))
 
2367
                {
 
2368
                        status |= CURS_SELF_ADDED;
 
2369
                        status &= ~CURS_SELF_ADDING;
 
2370
                }
 
2371
                if (0 != (status & CURS_SELF_UPDATING))
 
2372
                {
 
2373
                        status |= CURS_SELF_UPDATED;
 
2374
                        status &= ~CURS_SELF_UPDATING;
 
2375
                }
 
2376
                if (0 != (status & CURS_SELF_DELETING))
 
2377
                {
 
2378
                        status |= CURS_SELF_DELETED;
 
2379
                        status &= ~CURS_SELF_DELETING;
 
2380
                }
 
2381
                if (status != deleted_keyset->status)
 
2382
                {
 
2383
inolog("!!Commit Deleted=%d(%d)\n", *deleted, i);
 
2384
                        deleted_keyset->status = status;
 
2385
                }
 
2386
        } 
 
2387
}
 
2388
 
 
2389
static BOOL enlargeUpdated(QResultClass *res, Int4 number, const StatementClass *stmt)
 
2390
{
 
2391
        Int2    alloc;
 
2392
 
 
2393
        alloc = res->up_alloc;
 
2394
        if (0 == alloc)
 
2395
                alloc = number > 10 ? number : 10;
 
2396
        else
 
2397
                while (alloc < number)
 
2398
                {
 
2399
                        alloc *= 2;
 
2400
                }
 
2401
        if (alloc <= res->up_alloc)
 
2402
                return TRUE;
 
2403
 
 
2404
        QR_REALLOC_return_with_error(res->updated, SQLULEN, sizeof(SQLULEN) * alloc, res, "enlargeUpdated failed", FALSE);
 
2405
        QR_REALLOC_return_with_error(res->updated_keyset, KeySet, sizeof(KeySet) * alloc, res, "enlargeUpdated failed 2", FALSE);
 
2406
        if (SQL_CURSOR_KEYSET_DRIVEN != stmt->options.cursor_type)
 
2407
                QR_REALLOC_return_with_error(res->updated_tuples, TupleField, sizeof(TupleField) * res->num_fields * alloc, res, "enlargeUpdated failed 3", FALSE);
 
2408
        res->up_alloc = alloc;
 
2409
 
 
2410
        return TRUE;
 
2411
}
 
2412
 
 
2413
static void AddUpdated(StatementClass *stmt, SQLLEN index)
 
2414
{
 
2415
        QResultClass    *res;
 
2416
        SQLULEN *updated;
 
2417
        KeySet  *updated_keyset, *keyset;
 
2418
        TupleField      *updated_tuples = NULL, *tuple_updated,  *tuple;
 
2419
        SQLULEN kres_ridx;
 
2420
        UInt2   up_count;
 
2421
        BOOL    is_in_trans;
 
2422
        SQLLEN  upd_idx, upd_add_idx;
 
2423
        Int2    num_fields;
 
2424
        int     i;
 
2425
        UWORD   status;
 
2426
 
 
2427
inolog("AddUpdated index=%d\n", index);
 
2428
        if (!stmt)      return;
 
2429
        if (res = SC_get_Curres(stmt), !res)    return;
 
2430
        if (!res->keyset)               return;
 
2431
        kres_ridx = GIdx2KResIdx(index, stmt, res);
 
2432
        if (kres_ridx < 0 || kres_ridx >= res->num_cached_keys)
 
2433
                return;
 
2434
        keyset = res->keyset + kres_ridx;
 
2435
        if (0 != (keyset->status & CURS_SELF_ADDING))
 
2436
                AddRollback(stmt, res, index, res->keyset + kres_ridx, SQL_REFRESH);
 
2437
        if (!QR_get_cursor(res))        return;
 
2438
        up_count = res->up_count;
 
2439
        if (up_count > 0 && 0 == res->up_alloc) return;
 
2440
        num_fields = res->num_fields;
 
2441
        tuple_updated = res->backend_tuples + kres_ridx * num_fields;
 
2442
        if (!tuple_updated)
 
2443
                return;
 
2444
        upd_idx = -1;
 
2445
        upd_add_idx = -1;
 
2446
        updated = res->updated;
 
2447
        is_in_trans = CC_is_in_trans(SC_get_conn(stmt));
 
2448
        updated_keyset = res->updated_keyset;   
 
2449
        status = keyset->status;
 
2450
        status &= (~KEYSET_INFO_PUBLIC);
 
2451
        status |= SQL_ROW_UPDATED;
 
2452
        if (is_in_trans)
 
2453
                status |= CURS_SELF_UPDATING;
 
2454
        else
 
2455
        {
 
2456
                for (i = up_count - 1; i >= 0; i--)
 
2457
                {
 
2458
                        if (updated[i] == index)
 
2459
                                break;
 
2460
                }
 
2461
                if (i >= 0)
 
2462
                        upd_idx = i;
 
2463
                else
 
2464
                {
 
2465
                        SQLLEN  num_totals = QR_get_num_total_tuples(res);
 
2466
                        if (index >= num_totals)
 
2467
                                upd_add_idx = num_totals - index;
 
2468
                }
 
2469
                status |= CURS_SELF_UPDATED;
 
2470
                status &= ~(CURS_SELF_ADDING | CURS_SELF_UPDATING | CURS_SELF_DELETING);
 
2471
        }
 
2472
 
 
2473
        tuple = NULL;
 
2474
        /* update the corresponding add(updat)ed info */
 
2475
        if (upd_add_idx >= 0)
 
2476
        {
 
2477
                res->added_keyset[upd_add_idx].status = status;
 
2478
                if (res->added_tuples)
 
2479
                {
 
2480
                        tuple = res->added_tuples + num_fields * upd_add_idx;
 
2481
                        ClearCachedRows(tuple, num_fields, 1);
 
2482
                }
 
2483
        }
 
2484
        else if (upd_idx >= 0)
 
2485
        {
 
2486
                res->updated_keyset[upd_idx].status = status;
 
2487
                if (res->updated_tuples)
 
2488
                {
 
2489
                        tuple = res->added_tuples + num_fields * upd_add_idx;
 
2490
                        ClearCachedRows(tuple, num_fields, 1);
 
2491
                }
 
2492
        }
 
2493
        else
 
2494
        {
 
2495
                if (!enlargeUpdated(res, res->up_count + 1, stmt))
 
2496
                        return;
 
2497
                updated = res->updated; 
 
2498
                updated_keyset = res->updated_keyset; 
 
2499
                updated_tuples = res->updated_tuples;
 
2500
                upd_idx = up_count;
 
2501
                updated[up_count] = index;
 
2502
                updated_keyset[up_count] = *keyset;
 
2503
                updated_keyset[up_count].status = status;
 
2504
                if (updated_tuples)
 
2505
                {
 
2506
                        tuple = updated_tuples + num_fields * up_count;
 
2507
                        memset(tuple, 0, sizeof(TupleField) * num_fields);
 
2508
                }
 
2509
                res->up_count++;
 
2510
        }
 
2511
 
 
2512
        if (tuple)
 
2513
                ReplaceCachedRows(tuple, tuple_updated, num_fields, 1);
 
2514
        if (is_in_trans)
 
2515
                SC_get_conn(stmt)->result_uncommitted = 1;
 
2516
        mylog("up_count=%d\n", res->up_count);
 
2517
}
 
2518
 
 
2519
static void RemoveUpdated(QResultClass *res, SQLLEN index)
 
2520
{
 
2521
        mylog("RemoveUpdated index=%d\n", index);
 
2522
        RemoveUpdatedAfterTheKey(res, index, NULL);
 
2523
}
 
2524
 
 
2525
static void RemoveUpdatedAfterTheKey(QResultClass *res, SQLLEN index, const KeySet *keyset)
 
2526
{
 
2527
        SQLULEN *updated, num_read = QR_get_num_total_read(res);
 
2528
        KeySet  *updated_keyset;
 
2529
        TupleField      *updated_tuples = NULL;
 
2530
        SQLLEN  pidx, midx, mv_count;
 
2531
        int     i, num_fields = res->num_fields, rm_count = 0;
 
2532
 
 
2533
        mylog("RemoveUpdatedAfterTheKey %d,(%d,%d)\n", index, keyset ? keyset->blocknum : 0, keyset ? keyset->offset : 0);
 
2534
        if (index < 0)
 
2535
        {
 
2536
                midx = index;
 
2537
                pidx = num_read - index - 1;
 
2538
        }
 
2539
        else
 
2540
        {
 
2541
                pidx = index;
 
2542
                if (index >= num_read)
 
2543
                        midx = num_read - index - 1;
 
2544
                else
 
2545
                        midx = index;
 
2546
        }
 
2547
        for (i = 0; i < res->up_count; i++)
 
2548
        {
 
2549
                updated = res->updated + i;
 
2550
                if (pidx == *updated ||
 
2551
                    midx == *updated)
 
2552
                {
 
2553
                        updated_keyset = res->updated_keyset + i;
 
2554
                        if (keyset &&
 
2555
                            updated_keyset->blocknum == keyset->blocknum &&
 
2556
                            updated_keyset->offset == keyset->offset)
 
2557
                                break;
 
2558
                        updated_tuples = NULL;
 
2559
                        if (res->updated_tuples)
 
2560
                        {
 
2561
                                updated_tuples = res->updated_tuples + i * num_fields;
 
2562
                                ClearCachedRows(updated_tuples, num_fields, 1);
 
2563
                        }
 
2564
                        mv_count = res->up_count - i -1;
 
2565
                        if (mv_count > 0)
 
2566
                        {
 
2567
                                memmove(updated, updated + 1, sizeof(SQLULEN) * mv_count); 
 
2568
                                memmove(updated_keyset, updated_keyset + 1, sizeof(KeySet) * mv_count); 
 
2569
                                if (updated_tuples)
 
2570
                                        memmove(updated_tuples, updated_tuples + num_fields, sizeof(TupleField) * num_fields * mv_count);
 
2571
                        }
 
2572
                        res->up_count--;
 
2573
                        rm_count++;
 
2574
                }
 
2575
        }
 
2576
        mylog("RemoveUpdatedAfter removed count=%d,%d\n", rm_count, res->up_count);
 
2577
}
 
2578
 
 
2579
static void CommitUpdated(QResultClass *res)
 
2580
{
 
2581
        KeySet  *updated_keyset;
 
2582
        int     i;
 
2583
        UWORD   status;
 
2584
 
 
2585
        mylog("CommitUpdated res=%p\n", res);
 
2586
        if (!res)       return;
 
2587
        if (!QR_get_cursor(res))
 
2588
                return;
 
2589
        if (res->up_count <= 0)
 
2590
                return;
 
2591
        if (updated_keyset = res->updated_keyset, !updated_keyset)
 
2592
                return;
 
2593
        for (i = res->up_count - 1; i >= 0; i--)
 
2594
        {
 
2595
                status = updated_keyset[i].status;
 
2596
                if (0 != (status & CURS_SELF_UPDATING))
 
2597
                {
 
2598
                        status &= ~CURS_SELF_UPDATING;
 
2599
                        status |= CURS_SELF_UPDATED;
 
2600
                }
 
2601
                if (0 != (status & CURS_SELF_ADDING))
 
2602
                {
 
2603
                        status &= ~CURS_SELF_ADDING;
 
2604
                        status |= CURS_SELF_ADDED;
 
2605
                }
 
2606
                if (0 != (status & CURS_SELF_DELETING))
 
2607
                {
 
2608
                        status &= ~CURS_SELF_DELETING;
 
2609
                        status |= CURS_SELF_DELETED;
 
2610
                }
 
2611
                if (status != updated_keyset[i].status)
 
2612
                {
 
2613
inolog("!!Commit Updated=%d(%d)\n", res->updated[i], i);
 
2614
                        updated_keyset[i].status = status;
 
2615
                }
 
2616
        }
 
2617
}
 
2618
 
 
2619
 
 
2620
static void DiscardRollback(StatementClass *stmt, QResultClass *res)
 
2621
{
 
2622
        int     i;
 
2623
        SQLLEN  index, kres_ridx;
 
2624
        UWORD   status;
 
2625
        Rollback *rollback;
 
2626
        KeySet  *keyset;
 
2627
        BOOL    kres_is_valid;
 
2628
 
 
2629
inolog("DiscardRollback");
 
2630
        if (QR_get_cursor(res))
 
2631
        {
 
2632
                CommitAdded(res);
 
2633
                CommitUpdated(res);
 
2634
                CommitDeleted(res);
 
2635
                return;
 
2636
        }
 
2637
 
 
2638
        if (0 == res->rb_count || NULL == res->rollback)
 
2639
                return;
 
2640
        rollback = res->rollback;
 
2641
        keyset = res->keyset;
 
2642
        for (i = 0; i < res->rb_count; i++)
 
2643
        {
 
2644
                index = rollback[i].index;
 
2645
                status = 0;
 
2646
                kres_is_valid = FALSE;
 
2647
                if (index >= 0)
 
2648
                {
 
2649
                        kres_ridx = GIdx2KResIdx(index, stmt, res);
 
2650
                        if (kres_ridx >= 0 && kres_ridx < res->num_cached_keys)
 
2651
                        {
 
2652
                                kres_is_valid = TRUE;
 
2653
                                status = keyset[kres_ridx].status;
 
2654
                        }
 
2655
                }
 
2656
                if (kres_is_valid)
 
2657
                {
 
2658
                        keyset[kres_ridx].status &= ~(CURS_SELF_DELETING | CURS_SELF_UPDATING | CURS_SELF_ADDING);
 
2659
                        keyset[kres_ridx].status |= ((status & (CURS_SELF_DELETING | CURS_SELF_UPDATING | CURS_SELF_ADDING)) << 3);
 
2660
                }
 
2661
        }
 
2662
        free(rollback);
 
2663
        res->rollback = NULL;
 
2664
        res->rb_count = res->rb_alloc = 0;
 
2665
}
 
2666
 
 
2667
static QResultClass *positioned_load(StatementClass *stmt, UInt4 flag, const UInt4 *oidint, const char *tid);
 
2668
static void UndoRollback(StatementClass *stmt, QResultClass *res, BOOL partial)
 
2669
{
 
2670
        Int4    i, rollbp;
 
2671
        SQLLEN  index, ridx, kres_ridx;
 
2672
        UWORD   status;
 
2673
        Rollback *rollback;
 
2674
        KeySet  *keyset, keys, *wkey = NULL;
 
2675
        BOOL    curs = (NULL != QR_get_cursor(res)), texist, kres_is_valid;
 
2676
 
 
2677
        if (0 == res->rb_count || NULL == res->rollback)
 
2678
                return;
 
2679
        rollback = res->rollback;
 
2680
        keyset = res->keyset;
 
2681
 
 
2682
        rollbp = 0;
 
2683
        if (partial)
 
2684
        {
 
2685
                SQLLEN  pidx, midx;
 
2686
                Int2    doubtp, rollbps;
 
2687
                int     j;
 
2688
 
 
2689
                rollbps = rollbp = res->rb_count;
 
2690
                for (i = 0, doubtp = 0; i < res->rb_count; i++)
 
2691
                {
 
2692
                        index = rollback[i].index;
 
2693
                        keys.blocknum = rollback[i].blocknum;
 
2694
                        keys.offset = rollback[i].offset;
 
2695
                        texist = tupleExists(stmt, &keys);
 
2696
inolog("texist[%d]=%d", i, texist);
 
2697
                        if (SQL_ADD == rollback[i].option)
 
2698
                        {
 
2699
                                if (texist)
 
2700
                                        doubtp = i + 1;
 
2701
                        }
 
2702
                        else if (SQL_REFRESH == rollback[i].option)
 
2703
                        {
 
2704
                                if (texist || doubtp == i)
 
2705
                                        doubtp = i + 1;
 
2706
                        }
 
2707
                        else
 
2708
                        {
 
2709
                                if (texist)
 
2710
                                        break;
 
2711
                                if (doubtp == i)
 
2712
                                        doubtp = i + 1;
 
2713
                        }
 
2714
inolog(" doubtp=%d\n", doubtp);
 
2715
                }
 
2716
                rollbp = i;
 
2717
inolog(" doubtp=%d,rollbp=%d\n", doubtp, rollbp);
 
2718
                if (doubtp < 0)
 
2719
                        doubtp = 0;
 
2720
                do
 
2721
                {
 
2722
                        rollbps = rollbp;
 
2723
                        for (i = doubtp; i < rollbp; i++)
 
2724
                        {
 
2725
                                index = rollback[i].index;
 
2726
                                if (SQL_ADD == rollback[i].option)
 
2727
                                {
 
2728
inolog("index[%d]=%d\n", i, index);
 
2729
                                        if (index < 0)
 
2730
                                        {
 
2731
                                                midx = index;
 
2732
                                                pidx = res->num_total_read - index - 1;
 
2733
                                        }
 
2734
                                        else
 
2735
                                        {
 
2736
                                                pidx = index;
 
2737
                                                midx = res->num_total_read - index - 1;
 
2738
                                        }
 
2739
inolog("pidx=%d,midx=%d\n", pidx, midx); 
 
2740
                                        for (j = rollbp - 1; j > i; j--)
 
2741
                                        {
 
2742
                                                if (rollback[j].index == midx ||
 
2743
                                                    rollback[j].index == pidx)
 
2744
                                                {
 
2745
                                                        if (SQL_DELETE == rollback[j].option)
 
2746
                                                        {
 
2747
inolog("delete[%d].index=%d\n", j, rollback[j].index);
 
2748
                                                                break;
 
2749
                                                        }
 
2750
                                                        /*else if (SQL_UPDATE == rollback[j].option)
 
2751
                                                        {
 
2752
inolog("update[%d].index=%d\n", j, rollback[j].index);
 
2753
                                                                if (IndexExists(stmt, res, rollback + j))
 
2754
                                                                        break;
 
2755
                                                        }*/
 
2756
                                                }
 
2757
                                        }
 
2758
                                        if (j <= i)
 
2759
                                        {
 
2760
                                                rollbp = i;
 
2761
                                                break;
 
2762
                                        }
 
2763
                                }
 
2764
                        }
 
2765
                } while (rollbp < rollbps);
 
2766
        }
 
2767
inolog("rollbp=%d\n", rollbp);
 
2768
 
 
2769
        for (i = res->rb_count - 1; i >= rollbp; i--)
 
2770
        {
 
2771
inolog("UndoRollback %d(%d)\n", i, rollback[i].option);
 
2772
                index = rollback[i].index;
 
2773
                if (curs)
 
2774
                {
 
2775
                        if (SQL_ADD == rollback[i].option)
 
2776
                                RemoveAdded(res, index);
 
2777
                        RemoveDeleted(res, index);
 
2778
                        keys.blocknum = rollback[i].blocknum;
 
2779
                        keys.offset = rollback[i].offset;
 
2780
                        RemoveUpdatedAfterTheKey(res, index, &keys);
 
2781
                }
 
2782
                status = 0;
 
2783
                kres_is_valid = FALSE;
 
2784
                if (index >= 0)
 
2785
                {
 
2786
                        kres_ridx = GIdx2KResIdx(index, stmt, res);
 
2787
                        if (kres_ridx >= 0 && kres_ridx < res->num_cached_keys)
 
2788
                        {
 
2789
                                kres_is_valid = TRUE;
 
2790
                                wkey = keyset + kres_ridx;
 
2791
                                status = wkey->status;
 
2792
                        }
 
2793
                }
 
2794
inolog(" index=%d status=%hx", index, status);
 
2795
                if (kres_is_valid)
 
2796
                {
 
2797
                        QResultClass    *qres;
 
2798
                        Int2            num_fields = res->num_fields;
 
2799
 
 
2800
                        ridx = GIdx2CacheIdx(index, stmt, res);
 
2801
                        if (SQL_ADD == rollback[i].option)
 
2802
                        {
 
2803
                                if (ridx >=0 && ridx < res->num_cached_rows)
 
2804
                                {
 
2805
                                        TupleField *tuple = res->backend_tuples + res->num_fields * ridx;
 
2806
                                        ClearCachedRows(tuple, res->num_fields, 1);
 
2807
                                        res->num_cached_rows--;
 
2808
                                }
 
2809
                                res->num_cached_keys--;
 
2810
                                if (!curs)
 
2811
                                        res->ad_count--;
 
2812
                        }
 
2813
                        else if (SQL_REFRESH == rollback[i].option)
 
2814
                                continue;
 
2815
                        else
 
2816
                        {
 
2817
inolog(" (%u, %u)", wkey->blocknum,  wkey->offset);
 
2818
                                wkey->blocknum = rollback[i].blocknum;
 
2819
                                wkey->offset = rollback[i].offset;
 
2820
inolog("->(%u, %u)\n", wkey->blocknum, wkey->offset);
 
2821
                                wkey->status &= ~KEYSET_INFO_PUBLIC;
 
2822
                                if (SQL_DELETE == rollback[i].option)
 
2823
                                        wkey->status &= ~CURS_SELF_DELETING;
 
2824
                                else if (SQL_UPDATE == rollback[i].option)
 
2825
                                        wkey->status &= ~CURS_SELF_UPDATING;
 
2826
                                wkey->status |= CURS_NEEDS_REREAD;
 
2827
                                if (ridx >=0 && ridx < res->num_cached_rows)
 
2828
                                {
 
2829
                                        char    tidval[32];
 
2830
 
 
2831
                                        sprintf(tidval, "(%d,%d)", wkey->blocknum, wkey->offset);
 
2832
                                        qres = positioned_load(stmt, 0, NULL, tidval);
 
2833
                                        if (QR_command_maybe_successful(qres) &&
 
2834
                                            QR_get_num_cached_tuples(qres) == 1)
 
2835
                                        {
 
2836
                                                MoveCachedRows(res->backend_tuples + num_fields * ridx, qres->backend_tuples, num_fields, 1);
 
2837
                                                wkey->status &= ~CURS_NEEDS_REREAD;
 
2838
                                        }
 
2839
                                        QR_Destructor(qres);
 
2840
                                }
 
2841
                        }
 
2842
                }
 
2843
        }
 
2844
        res->rb_count = rollbp;
 
2845
        if (0 == rollbp)
 
2846
        {
 
2847
                free(rollback);
 
2848
                res->rollback = NULL;
 
2849
                res->rb_alloc = 0;
 
2850
        }
 
2851
}
 
2852
 
 
2853
void    ProcessRollback(ConnectionClass *conn, BOOL undo, BOOL partial) 
 
2854
{
 
2855
        int     i;
 
2856
        StatementClass  *stmt;
 
2857
        QResultClass    *res;
 
2858
 
 
2859
        for (i = 0; i < conn->num_stmts; i++)
 
2860
        {
 
2861
                if (stmt = conn->stmts[i], !stmt)
 
2862
                        continue;
 
2863
                for (res = SC_get_Result(stmt); res; res = res->next)
 
2864
                {
 
2865
                        if (undo)
 
2866
                                UndoRollback(stmt, res, partial);
 
2867
                        else
 
2868
                                DiscardRollback(stmt, res);
 
2869
                }
 
2870
        }
 
2871
}
 
2872
 
 
2873
 
 
2874
#define LATEST_TUPLE_LOAD       1L
 
2875
#define USE_INSERTED_TID        (1L << 1)
 
2876
static QResultClass *
 
2877
positioned_load(StatementClass *stmt, UInt4 flag, const UInt4 *oidint, const char *tidval)
 
2878
{
 
2879
        CSTR    func = "positioned_load";
 
2880
        CSTR    andqual = " and ";
 
2881
        QResultClass *qres = NULL;
 
2882
        char    *selstr, oideqstr[256];
 
2883
        BOOL    latest = ((flag & LATEST_TUPLE_LOAD) != 0);
 
2884
        size_t  len;
 
2885
        TABLE_INFO      *ti = stmt->ti[0];
 
2886
        const char *bestitem = GET_NAME(ti->bestitem);
 
2887
        const char *bestqual = GET_NAME(ti->bestqual);
 
2888
 
 
2889
inolog("%s bestitem=%s bestqual=%s\n", func, SAFE_NAME(ti->bestitem), SAFE_NAME(ti->bestqual));
 
2890
        if (!bestitem || !oidint)
 
2891
                *oideqstr = '\0';
 
2892
        else
 
2893
        {
 
2894
                /*snprintf(oideqstr, sizeof(oideqstr), " and \"%s\" = %u", bestitem, oid);*/
 
2895
                strcpy(oideqstr, andqual);
 
2896
                sprintf(oideqstr + strlen(andqual), bestqual, *oidint);
 
2897
        }
 
2898
        len = strlen(stmt->load_statement);
 
2899
        len += strlen(oideqstr);
 
2900
        if (tidval)
 
2901
                len += 100;
 
2902
        else if ((flag & USE_INSERTED_TID) != 0)
 
2903
                len += 50;
 
2904
        else
 
2905
                len += 20;
 
2906
        selstr = malloc(len);
 
2907
        if (tidval)
 
2908
        {
 
2909
                if (latest)
 
2910
                {
 
2911
                        if (NAME_IS_VALID(ti->schema_name))
 
2912
                                snprintf(selstr, len, "%s where ctid = currtid2('\"%s\".\"%s\"', '%s') %s",
 
2913
                                stmt->load_statement, SAFE_NAME(ti->schema_name),
 
2914
                                SAFE_NAME(ti->table_name), tidval, oideqstr);
 
2915
                        else
 
2916
                                snprintf(selstr, len, "%s where ctid = currtid2('%s', '%s') %s", stmt->load_statement, SAFE_NAME(ti->table_name), tidval, oideqstr);
 
2917
                }
 
2918
                else 
 
2919
                        snprintf(selstr, len, "%s where ctid = '%s' %s", stmt->load_statement, tidval, oideqstr); 
 
2920
        }
 
2921
        else if ((flag & USE_INSERTED_TID) != 0)
 
2922
                snprintf(selstr, len, "%s where ctid = currtid(0, '(0,0)') %s", stmt->load_statement, oideqstr);
 
2923
        else if (bestitem && oidint)
 
2924
        {
 
2925
                /*snprintf(selstr, len, "%s where \"%s\" = %u", stmt->load_statement, bestitem, *oid);*/
 
2926
                snprintf(selstr, len, "%s where ", stmt->load_statement);
 
2927
                snprintf_add(selstr, len, bestqual, *oidint);
 
2928
        }
 
2929
        else
 
2930
        {
 
2931
                SC_set_error(stmt,STMT_INTERNAL_ERROR, "can't find the add and updating row because of the lack of oid", func);
 
2932
                goto cleanup;
 
2933
        } 
 
2934
 
 
2935
        mylog("selstr=%s\n", selstr);
 
2936
        qres = CC_send_query(SC_get_conn(stmt), selstr, NULL, 0, stmt);
 
2937
cleanup:
 
2938
        free(selstr);
 
2939
        return qres;
 
2940
}
 
2941
 
 
2942
static RETCODE
 
2943
SC_pos_reload_with_tid(StatementClass *stmt, SQLULEN global_ridx, UInt2 *count, Int4 logKind, const char *tid)
 
2944
{
 
2945
        CSTR            func = "SC_pos_reload";
 
2946
        int             res_cols;
 
2947
        UInt2           offset;
 
2948
        UInt2           rcnt;
 
2949
        SQLLEN          res_ridx, kres_ridx;
 
2950
        OID             oidint;
 
2951
        UInt4           blocknum;
 
2952
        QResultClass    *res, *qres;
 
2953
        IRDFields       *irdflds = SC_get_IRDF(stmt);
 
2954
        RETCODE         ret = SQL_ERROR;
 
2955
        char            tidval[32];
 
2956
        BOOL            use_ctid = TRUE, data_in_cache = TRUE, key_in_cache = TRUE;
 
2957
 
 
2958
        mylog("positioned load fi=%p ti=%p\n", irdflds->fi, stmt->ti);
 
2959
        rcnt = 0;
 
2960
        if (count)
 
2961
                *count = 0;
 
2962
        if (!(res = SC_get_Curres(stmt)))
 
2963
        {
 
2964
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in SC_pos_reload.", func);
 
2965
                return SQL_ERROR;
 
2966
        }
 
2967
        res_ridx = GIdx2CacheIdx(global_ridx, stmt, res);
 
2968
        if (res_ridx < 0 || res_ridx >= QR_get_num_cached_tuples(res))
 
2969
        {
 
2970
                data_in_cache = FALSE;
 
2971
                SC_set_error(stmt, STMT_ROW_OUT_OF_RANGE, "the target rows is out of the rowset", func);
 
2972
                return SQL_ERROR;
 
2973
        }
 
2974
        kres_ridx = GIdx2KResIdx(global_ridx, stmt, res);
 
2975
        if (kres_ridx < 0 || kres_ridx >= res->num_cached_keys)
 
2976
        {
 
2977
                key_in_cache = FALSE;
 
2978
                SC_set_error(stmt, STMT_ROW_OUT_OF_RANGE, "the target rows is out of the rowset", func);
 
2979
                return SQL_ERROR;
 
2980
        }
 
2981
        else if (0 != (res->keyset[kres_ridx].status & CURS_SELF_ADDING))
 
2982
        {
 
2983
                if (NULL == tid)
 
2984
                {
 
2985
                        use_ctid = FALSE;
 
2986
                        mylog("The tuple is currently being added and can't use ctid\n");
 
2987
                }
 
2988
        }       
 
2989
 
 
2990
        if (SC_update_not_ready(stmt))
 
2991
                parse_statement(stmt, TRUE);    /* not preferable */
 
2992
        if (!SC_is_updatable(stmt))
 
2993
        {
 
2994
                stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY;
 
2995
                SC_set_error(stmt, STMT_INVALID_OPTION_IDENTIFIER, "the statement is read-only", func);
 
2996
                return SQL_ERROR;
 
2997
        }
 
2998
        if (!(oidint = getOid(res, kres_ridx)))
 
2999
        {
 
3000
                if (!strcmp(SAFE_NAME(stmt->ti[0]->bestitem), OID_NAME))
 
3001
                {
 
3002
                        SC_set_error(stmt, STMT_ROW_VERSION_CHANGED, "the row was already deleted ?", func);
 
3003
                        return SQL_SUCCESS_WITH_INFO;
 
3004
                }
 
3005
        }
 
3006
        getTid(res, kres_ridx, &blocknum, &offset);
 
3007
        sprintf(tidval, "(%u, %u)", blocknum, offset);
 
3008
        res_cols = getNumResultCols(res);
 
3009
        if (tid)
 
3010
                qres = positioned_load(stmt, 0, &oidint, tid);
 
3011
        else
 
3012
                qres = positioned_load(stmt, use_ctid ? LATEST_TUPLE_LOAD : 0, &oidint, use_ctid ? tidval : NULL);
 
3013
        if (!QR_command_maybe_successful(qres))
 
3014
        {
 
3015
                ret = SQL_ERROR;
 
3016
                SC_replace_error_with_res(stmt, STMT_ERROR_TAKEN_FROM_BACKEND, "positioned_load failed", qres, TRUE);
 
3017
        }
 
3018
        else
 
3019
        {
 
3020
                TupleField *tuple_old, *tuple_new;
 
3021
                ConnectionClass *conn = SC_get_conn(stmt);
 
3022
 
 
3023
                rcnt = (UInt2) QR_get_num_cached_tuples(qres);
 
3024
                tuple_old = res->backend_tuples + res->num_fields * res_ridx;
 
3025
                if (0 != logKind && CC_is_in_trans(conn))
 
3026
                        AddRollback(stmt, res, global_ridx, res->keyset + kres_ridx, logKind);
 
3027
                if (rcnt == 1)
 
3028
                {
 
3029
                        int     effective_fields = res_cols;
 
3030
 
 
3031
                        QR_set_position(qres, 0);
 
3032
                        tuple_new = qres->tupleField;
 
3033
                        if (res->keyset && key_in_cache)
 
3034
                        {
 
3035
                                if (SQL_CURSOR_KEYSET_DRIVEN == stmt->options.cursor_type &&
 
3036
                                        strcmp(tuple_new[qres->num_fields - res->num_key_fields].value, tidval))
 
3037
                                        res->keyset[kres_ridx].status |= SQL_ROW_UPDATED;
 
3038
                                KeySetSet(tuple_new, qres->num_fields, res->num_key_fields, res->keyset + kres_ridx);
 
3039
                        }
 
3040
                        if (data_in_cache)
 
3041
                                MoveCachedRows(tuple_old, tuple_new, effective_fields, 1); 
 
3042
                        ret = SQL_SUCCESS;
 
3043
                }
 
3044
                else
 
3045
                {
 
3046
                        SC_set_error(stmt, STMT_ROW_VERSION_CHANGED, "the content was deleted after last fetch", func);
 
3047
                        ret = SQL_SUCCESS_WITH_INFO;
 
3048
                        if (stmt->options.cursor_type == SQL_CURSOR_KEYSET_DRIVEN)
 
3049
                        {
 
3050
                                res->keyset[kres_ridx].status |= SQL_ROW_DELETED;
 
3051
                        }
 
3052
                }
 
3053
        }
 
3054
        QR_Destructor(qres);
 
3055
        if (count)
 
3056
                *count = rcnt;
 
3057
        return ret;
 
3058
}
 
3059
 
 
3060
RETCODE
 
3061
SC_pos_reload(StatementClass *stmt, SQLULEN global_ridx, UInt2 *count, Int4 logKind)
 
3062
{
 
3063
        return SC_pos_reload_with_tid(stmt, global_ridx, count, logKind, NULL);
 
3064
}
 
3065
 
 
3066
static  const int       pre_fetch_count = 32;
 
3067
static SQLLEN LoadFromKeyset(StatementClass *stmt, QResultClass * res, int rows_per_fetch, SQLLEN limitrow)
 
3068
{
 
3069
        CSTR    func = "LoadFromKeyset";
 
3070
        ConnectionClass *conn = SC_get_conn(stmt);
 
3071
        SQLLEN  i;
 
3072
        int     j, rowc, rcnt = 0;
 
3073
        BOOL    prepare;
 
3074
        OID     oid;
 
3075
        UInt4   blocknum;
 
3076
        SQLLEN  kres_ridx;
 
3077
        UInt2   offset;
 
3078
        char    *qval = NULL, *sval = NULL;
 
3079
        int     keys_per_fetch = 10;
 
3080
 
 
3081
        prepare = PG_VERSION_GE(conn, 7.3);
 
3082
        for (i = SC_get_rowset_start(stmt), kres_ridx = GIdx2KResIdx(i, stmt, res), rowc = 0;; i++)
 
3083
        {
 
3084
                if (i >= limitrow)
 
3085
                {
 
3086
                        if (!rowc)
 
3087
                                break;
 
3088
                        if (res->reload_count > 0)
 
3089
                        {
 
3090
                                for (j = rowc; j < keys_per_fetch; j++)
 
3091
                                {
 
3092
                                        if (j)
 
3093
                                                strcpy(sval, ",NULL");
 
3094
                                        else
 
3095
                                                strcpy(sval, "NULL");
 
3096
                                        sval = strchr(sval, '\0');
 
3097
                                }
 
3098
                        }
 
3099
                        rowc = -1; /* end of loop */
 
3100
                }
 
3101
                if (rowc < 0 || rowc >= keys_per_fetch)
 
3102
                {
 
3103
                        QResultClass    *qres;
 
3104
 
 
3105
                        strcpy(sval, ")");
 
3106
                        qres = CC_send_query(conn, qval, NULL, CREATE_KEYSET, stmt);
 
3107
                        if (QR_command_maybe_successful(qres))
 
3108
                        {
 
3109
                                SQLLEN          j, k, l;
 
3110
                                Int2            m;
 
3111
                                TupleField      *tuple, *tuplew;
 
3112
 
 
3113
                                for (j = 0; j < QR_get_num_total_read(qres); j++)
 
3114
                                {
 
3115
                                        oid = getOid(qres, j); 
 
3116
                                        getTid(qres, j, &blocknum, &offset);
 
3117
                                        for (k = SC_get_rowset_start(stmt); k < limitrow; k++)
 
3118
                                        {
 
3119
                                                if (oid == getOid(res, k))
 
3120
                                                {
 
3121
                                                        l = GIdx2CacheIdx(k, stmt, res);
 
3122
                                                        tuple = res->backend_tuples + res->num_fields * l;
 
3123
                                                        tuplew = qres->backend_tuples + qres->num_fields * j;
 
3124
                                                        for (m = 0; m < res->num_fields; m++, tuple++, tuplew++)
 
3125
                                                        {
 
3126
                                                                if (tuple->len > 0 && tuple->value)
 
3127
                                                                        free(tuple->value);
 
3128
                                                                tuple->value = tuplew->value;
 
3129
                                                                tuple->len = tuplew->len;
 
3130
                                                                tuplew->value = NULL;
 
3131
                                                                tuplew->len = -1;
 
3132
                                                        }
 
3133
                                                        res->keyset[k].status &= ~CURS_NEEDS_REREAD;
 
3134
                                                        break;
 
3135
                                                }
 
3136
                                        }
 
3137
                                }
 
3138
                        }
 
3139
                        else
 
3140
                        {
 
3141
                                SC_set_error(stmt, STMT_EXEC_ERROR, "Data Load Error", func);
 
3142
                                rcnt = -1;
 
3143
                                QR_Destructor(qres);
 
3144
                                break;
 
3145
                        }
 
3146
                        QR_Destructor(qres);
 
3147
                        if (rowc < 0)
 
3148
                                break;
 
3149
                        rowc = 0;
 
3150
                }
 
3151
                if (!rowc)
 
3152
                {
 
3153
                        size_t lodlen = 0;
 
3154
 
 
3155
                        if (!qval)
 
3156
                        {
 
3157
                                size_t  allen;
 
3158
 
 
3159
                                if (prepare)
 
3160
                                {
 
3161
                                        if (res->reload_count > 0)
 
3162
                                                keys_per_fetch = res->reload_count;
 
3163
                                        else
 
3164
                                        {
 
3165
                                                char    planname[32];
 
3166
                                                int     j;
 
3167
                                                QResultClass    *qres;
 
3168
 
 
3169
                                                if (rows_per_fetch >= pre_fetch_count * 2)
 
3170
                                                        keys_per_fetch = pre_fetch_count;
 
3171
                                                else
 
3172
                                                        keys_per_fetch = rows_per_fetch;
 
3173
                                                if (!keys_per_fetch)
 
3174
                                                        keys_per_fetch = 2;
 
3175
                                                lodlen = strlen(stmt->load_statement);
 
3176
                                                sprintf(planname, "_KEYSET_%p", res);
 
3177
                                                allen = 8 + strlen(planname) +
 
3178
                                                        3 + 4 * keys_per_fetch + 1
 
3179
                                                        + 1 + 2 + lodlen + 20 +
 
3180
                                                        4 * keys_per_fetch + 1;
 
3181
                                                SC_MALLOC_return_with_error(qval, char, allen,
 
3182
                                                        stmt, "Couldn't alloc qval", -1);
 
3183
                                                sprintf(qval, "PREPARE \"%s\"", planname);
 
3184
                                                sval = strchr(qval, '\0');
 
3185
                                                for (j = 0; j < keys_per_fetch; j++)
 
3186
                                                {
 
3187
                                                        if (j == 0)
 
3188
                                                                strcpy(sval, "(tid");
 
3189
                                                        else 
 
3190
                                                                strcpy(sval, ",tid");
 
3191
                                                        sval = strchr(sval, '\0');
 
3192
                                                }
 
3193
                                                sprintf(sval, ") as %s where ctid in ", stmt->load_statement);
 
3194
                                                sval = strchr(sval, '\0'); 
 
3195
                                                for (j = 0; j < keys_per_fetch; j++)
 
3196
                                                {
 
3197
                                                        if (j == 0)
 
3198
                                                                strcpy(sval, "($1");
 
3199
                                                        else 
 
3200
                                                                sprintf(sval, ",$%d", j + 1);
 
3201
                                                        sval = strchr(sval, '\0');
 
3202
                                                }
 
3203
                                                strcpy(sval, ")");
 
3204
                                                qres = CC_send_query(conn, qval, NULL, 0, stmt);
 
3205
                                                if (QR_command_maybe_successful(qres))
 
3206
                                                {
 
3207
                                                        res->reload_count = keys_per_fetch;
 
3208
                                                }
 
3209
                                                else
 
3210
                                                {
 
3211
                                                        SC_set_error(stmt, STMT_EXEC_ERROR, "Prepare for Data Load Error", func);
 
3212
                                                        rcnt = -1;
 
3213
                                                        QR_Destructor(qres);
 
3214
                                                        break;
 
3215
                                                }
 
3216
                                                QR_Destructor(qres);
 
3217
                                        }
 
3218
                                        allen = 25 + 23 * keys_per_fetch;
 
3219
                                }
 
3220
                                else
 
3221
                                {
 
3222
                                        keys_per_fetch = pre_fetch_count;
 
3223
                                        lodlen = strlen(stmt->load_statement);
 
3224
                                        allen = lodlen + 20 + 23 * keys_per_fetch;
 
3225
                                }
 
3226
                                SC_REALLOC_return_with_error(qval, char, allen,
 
3227
                                        stmt, "Couldn't alloc qval", -1);
 
3228
                        }
 
3229
                        if (res->reload_count > 0)
 
3230
                        {
 
3231
                                sprintf(qval, "EXECUTE \"_KEYSET_%p\"(", res);
 
3232
                                sval = qval;
 
3233
                        }
 
3234
                        else
 
3235
                        {
 
3236
                                memcpy(qval, stmt->load_statement, lodlen);
 
3237
                                sval = qval + lodlen;
 
3238
                                sval[0]= '\0';
 
3239
                                strcpy(sval, " where ctid in (");
 
3240
                        }
 
3241
                        sval = strchr(sval, '\0');
 
3242
                }
 
3243
                if (0 != (res->keyset[kres_ridx].status & CURS_NEEDS_REREAD))
 
3244
                {
 
3245
                        getTid(res, i, &blocknum, &offset);
 
3246
                        if (rowc)
 
3247
                                sprintf(sval, ",'(%u,%u)'", blocknum, offset);
 
3248
                        else
 
3249
                                sprintf(sval, "'(%u,%u)'", blocknum, offset);
 
3250
                        sval = strchr(sval, '\0');
 
3251
                        rowc++;
 
3252
                        rcnt++;
 
3253
                }
 
3254
        }
 
3255
        if (qval)
 
3256
                free(qval);
 
3257
        return rcnt;
 
3258
}
 
3259
 
 
3260
static RETCODE  SQL_API
 
3261
SC_pos_reload_needed(StatementClass *stmt, SQLULEN req_size, UDWORD flag)
 
3262
{
 
3263
        CSTR    func = "SC_pos_reload_needed";
 
3264
        Int4            req_rows_size;
 
3265
        SQLLEN          i, limitrow;
 
3266
        UInt2           qcount;
 
3267
        QResultClass    *res;
 
3268
        RETCODE         ret = SQL_ERROR;
 
3269
        SQLLEN          kres_ridx, rowc;
 
3270
        Int4            rows_per_fetch;
 
3271
        BOOL            create_from_scratch = (0 != flag);
 
3272
 
 
3273
        mylog("%s\n", func);
 
3274
        if (!(res = SC_get_Curres(stmt)))
 
3275
        {
 
3276
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in SC_pos_reload_needed.", func);
 
3277
                return SQL_ERROR;
 
3278
        }
 
3279
        if (SC_update_not_ready(stmt))
 
3280
                parse_statement(stmt, TRUE);    /* not preferable */
 
3281
        if (!SC_is_updatable(stmt))
 
3282
        {
 
3283
                stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY;
 
3284
                SC_set_error(stmt, STMT_INVALID_OPTION_IDENTIFIER, "the statement is read-only", func);
 
3285
                return SQL_ERROR;
 
3286
        }
 
3287
        rows_per_fetch = 0;
 
3288
        req_rows_size = QR_get_reqsize(res);
 
3289
        if (req_size > req_rows_size)
 
3290
                req_rows_size = (UInt4) req_size;
 
3291
        if (create_from_scratch)
 
3292
        {
 
3293
                rows_per_fetch = ((pre_fetch_count - 1) / req_rows_size + 1) * req_rows_size;
 
3294
                limitrow = RowIdx2GIdx(rows_per_fetch, stmt);
 
3295
        }
 
3296
        else
 
3297
        {
 
3298
                limitrow = RowIdx2GIdx(req_rows_size, stmt);
 
3299
        }
 
3300
        if (limitrow > res->num_cached_keys)
 
3301
                limitrow = res->num_cached_keys;
 
3302
        if (create_from_scratch ||
 
3303
            !res->dataFilled)
 
3304
        {
 
3305
                ClearCachedRows(res->backend_tuples, res->num_fields, res->num_cached_rows);
 
3306
                res->dataFilled = FALSE;
 
3307
        }
 
3308
        if (!res->dataFilled)
 
3309
        {
 
3310
                SQLLEN  brows = GIdx2RowIdx(limitrow, stmt);
 
3311
                if (brows > res->count_backend_allocated)
 
3312
                {
 
3313
                        QR_REALLOC_return_with_error(res->backend_tuples, TupleField, sizeof(TupleField) * res->num_fields * brows, res, "pos_reload_needed failed", SQL_ERROR);
 
3314
                        res->count_backend_allocated = brows;
 
3315
                }
 
3316
                if (brows > 0)
 
3317
                        memset(res->backend_tuples, 0, sizeof(TupleField) * res->num_fields * brows);
 
3318
                QR_set_num_cached_rows(res, brows);
 
3319
                QR_set_rowstart_in_cache(res, 0);
 
3320
                if (SQL_RD_ON != stmt->options.retrieve_data)
 
3321
                        return SQL_SUCCESS;
 
3322
                for (i = SC_get_rowset_start(stmt), kres_ridx = GIdx2KResIdx(i, stmt,res); i < limitrow; i++, kres_ridx++)
 
3323
                {
 
3324
                        if (0 == (res->keyset[kres_ridx].status & (CURS_SELF_DELETING | CURS_SELF_DELETED | CURS_OTHER_DELETED)))
 
3325
                                res->keyset[kres_ridx].status |= CURS_NEEDS_REREAD;
 
3326
                }
 
3327
        }
 
3328
        if (rowc = LoadFromKeyset(stmt, res, rows_per_fetch, limitrow), rowc < 0)
 
3329
        {
 
3330
                return SQL_ERROR;
 
3331
        }
 
3332
        for (i = SC_get_rowset_start(stmt), kres_ridx = GIdx2KResIdx(i, stmt, res); i < limitrow; i++)
 
3333
        {
 
3334
                if (0 != (res->keyset[kres_ridx].status & CURS_NEEDS_REREAD))
 
3335
                {
 
3336
                        ret = SC_pos_reload(stmt, i, &qcount, 0);
 
3337
                        if (SQL_ERROR == ret)
 
3338
                        {
 
3339
                                break;
 
3340
                        }
 
3341
                        if (SQL_ROW_DELETED == (res->keyset[kres_ridx].status & KEYSET_INFO_PUBLIC))
 
3342
                        {
 
3343
                                res->keyset[kres_ridx].status |= CURS_OTHER_DELETED;
 
3344
                        }
 
3345
                        res->keyset[kres_ridx].status &= ~CURS_NEEDS_REREAD;
 
3346
                }
 
3347
        }
 
3348
        res->dataFilled = TRUE;
 
3349
        return ret;
 
3350
}
 
3351
 
 
3352
static RETCODE  SQL_API
 
3353
SC_pos_newload(StatementClass *stmt, const UInt4 *oidint, BOOL tidRef, const char *tidval)
 
3354
{
 
3355
        CSTR    func = "SC_pos_newload";
 
3356
        int                     i;
 
3357
        QResultClass *res, *qres;
 
3358
        RETCODE         ret = SQL_ERROR;
 
3359
 
 
3360
        mylog("positioned new ti=%p\n", stmt->ti);
 
3361
        if (!(res = SC_get_Curres(stmt)))
 
3362
        {
 
3363
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in SC_pos_newload.", func);
 
3364
                return SQL_ERROR;
 
3365
        }
 
3366
        if (SC_update_not_ready(stmt))
 
3367
                parse_statement(stmt, TRUE);    /* not preferable */
 
3368
        if (!SC_is_updatable(stmt))
 
3369
        {
 
3370
                stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY;
 
3371
                SC_set_error(stmt, STMT_INVALID_OPTION_IDENTIFIER, "the statement is read-only", func);
 
3372
                return SQL_ERROR;
 
3373
        }
 
3374
        qres = positioned_load(stmt, (tidRef && NULL == tidval) ? USE_INSERTED_TID : 0, oidint, tidRef ? tidval : NULL);
 
3375
        if (!qres || !QR_command_maybe_successful(qres))
 
3376
        {
 
3377
                SC_set_error(stmt, STMT_ERROR_TAKEN_FROM_BACKEND, "positioned_load in pos_newload failed", func);
 
3378
        }
 
3379
        else
 
3380
        {
 
3381
                SQLLEN  count = QR_get_num_cached_tuples(qres);
 
3382
 
 
3383
                QR_set_position(qres, 0);
 
3384
                if (count == 1)
 
3385
                {
 
3386
                        int     effective_fields = res->num_fields;
 
3387
                        ssize_t tuple_size;
 
3388
                        SQLLEN  num_total_rows, num_cached_rows, kres_ridx;
 
3389
                        BOOL    appendKey = FALSE, appendData = FALSE;
 
3390
                        TupleField *tuple_old, *tuple_new;
 
3391
 
 
3392
                        tuple_new = qres->tupleField;
 
3393
                        num_total_rows = QR_get_num_total_tuples(res);
 
3394
 
 
3395
                        AddAdded(stmt, res, num_total_rows, tuple_new);
 
3396
                        num_cached_rows = QR_get_num_cached_tuples(res);
 
3397
                        kres_ridx = GIdx2KResIdx(num_total_rows, stmt, res);
 
3398
                        if (QR_haskeyset(res))
 
3399
                        {       if (!QR_get_cursor(res))
 
3400
                                {
 
3401
                                        appendKey = TRUE;
 
3402
                                        if (num_total_rows == CacheIdx2GIdx(num_cached_rows, stmt, res))
 
3403
                                                appendData = TRUE;
 
3404
                                        else
 
3405
                                        {
 
3406
inolog("total %d <> backend %d - base %d + start %d cursor_type=%d\n", 
 
3407
num_total_rows, num_cached_rows,
 
3408
QR_get_rowstart_in_cache(res), SC_get_rowset_start(stmt), stmt->options.cursor_type);
 
3409
                                        }
 
3410
                                }
 
3411
                                else if (kres_ridx >= 0 && kres_ridx < res->cache_size)
 
3412
                                {
 
3413
                                        appendKey = TRUE;
 
3414
                                        appendData = TRUE;
 
3415
                                }
 
3416
                        }
 
3417
                        if (appendKey)
 
3418
                        {
 
3419
                                if (res->num_cached_keys >= res->count_keyset_allocated)
 
3420
                                {
 
3421
                                        if (!res->count_keyset_allocated)
 
3422
                                                tuple_size = TUPLE_MALLOC_INC;
 
3423
                                        else
 
3424
                                                tuple_size = res->count_keyset_allocated * 2;
 
3425
                                        QR_REALLOC_return_with_error(res->keyset, KeySet, sizeof(KeySet) * tuple_size, res, "pos_newload failed", SQL_ERROR);   
 
3426
                                        res->count_keyset_allocated = tuple_size;
 
3427
                                }
 
3428
                                KeySetSet(tuple_new, qres->num_fields, res->num_key_fields, res->keyset + kres_ridx);
 
3429
                                res->num_cached_keys++;
 
3430
                        }
 
3431
                        if (appendData)
 
3432
                        {
 
3433
inolog("total %d == backend %d - base %d + start %d cursor_type=%d\n", 
 
3434
num_total_rows, num_cached_rows,
 
3435
QR_get_rowstart_in_cache(res), SC_get_rowset_start(stmt), stmt->options.cursor_type);
 
3436
                                if (num_cached_rows >= res->count_backend_allocated)
 
3437
                                {
 
3438
                                        if (!res->count_backend_allocated)
 
3439
                                                tuple_size = TUPLE_MALLOC_INC;
 
3440
                                        else
 
3441
                                                tuple_size = res->count_backend_allocated * 2;
 
3442
                                        QR_REALLOC_return_with_error(res->backend_tuples, TupleField, res->num_fields * sizeof(TupleField) * tuple_size, res, "SC_pos_newload failed", SQL_ERROR);
 
3443
                                        /*
 
3444
                                        res->backend_tuples = (TupleField *) realloc(
 
3445
                                                res->backend_tuples,
 
3446
                                                res->num_fields * sizeof(TupleField) * tuple_size);
 
3447
                                        if (!res->backend_tuples)
 
3448
                                        {
 
3449
                                                SC_set_error(stmt, QR_set_rstatus(res, PORES_FATAL_ERROR), "Out of memory while reading tuples.", func);
 
3450
                                                QR_Destructor(qres);
 
3451
                                                return SQL_ERROR;
 
3452
                                        }
 
3453
                                        */
 
3454
                                        res->count_backend_allocated = tuple_size;
 
3455
                                }
 
3456
                                tuple_old = res->backend_tuples + res->num_fields * num_cached_rows;
 
3457
                                for (i = 0; i < effective_fields; i++)
 
3458
                                {
 
3459
                                        tuple_old[i].len = tuple_new[i].len;
 
3460
                                        tuple_new[i].len = -1;
 
3461
                                        tuple_old[i].value = tuple_new[i].value;
 
3462
                                        tuple_new[i].value = NULL;
 
3463
                                }
 
3464
                                res->num_cached_rows++;
 
3465
                        }
 
3466
                        ret = SQL_SUCCESS;
 
3467
                }
 
3468
                else if (0 == count)
 
3469
                        ret = SQL_NO_DATA_FOUND;
 
3470
                else
 
3471
                {
 
3472
                        SC_set_error(stmt, STMT_ROW_VERSION_CHANGED, "the driver cound't identify inserted rows", func);
 
3473
                        ret = SQL_ERROR;
 
3474
                }
 
3475
                /* stmt->currTuple = SC_get_rowset_start(stmt) + ridx; */
 
3476
        }
 
3477
        QR_Destructor(qres);
 
3478
        return ret;
 
3479
}
 
3480
 
 
3481
static RETCODE SQL_API
 
3482
irow_update(RETCODE ret, StatementClass *stmt, StatementClass *ustmt, SQLSETPOSIROW irow, SQLULEN global_ridx)
 
3483
{
 
3484
        CSTR    func = "irow_update";
 
3485
 
 
3486
        if (ret != SQL_ERROR)
 
3487
        {
 
3488
                int                     updcnt;
 
3489
                QResultClass            *tres = SC_get_Curres(ustmt);
 
3490
                const char *cmdstr = QR_get_command(tres);
 
3491
 
 
3492
                if (cmdstr &&
 
3493
                        sscanf(cmdstr, "UPDATE %d", &updcnt) == 1)
 
3494
                {
 
3495
                        if (updcnt == 1)
 
3496
                        {
 
3497
                                const char *tidval = NULL;
 
3498
 
 
3499
                                if (NULL != tres->backend_tuples &&
 
3500
                                    1 == QR_get_num_cached_tuples(tres))
 
3501
                                        tidval = QR_get_value_backend_text(tres, 0, 0);
 
3502
                                ret = SC_pos_reload_with_tid(stmt, global_ridx, (UInt2 *) 0, SQL_UPDATE, tidval);
 
3503
                                if (SQL_ERROR != ret)
 
3504
                                        AddUpdated(stmt, global_ridx);
 
3505
                        }
 
3506
                        else if (updcnt == 0)
 
3507
                        {
 
3508
                                SC_set_error(stmt, STMT_ROW_VERSION_CHANGED, "the content was changed before updation", func);
 
3509
                                ret = SQL_ERROR;
 
3510
                                if (stmt->options.cursor_type == SQL_CURSOR_KEYSET_DRIVEN)
 
3511
                                        SC_pos_reload(stmt, global_ridx, (UInt2 *) 0, 0);
 
3512
                        }
 
3513
                        else
 
3514
                                ret = SQL_ERROR;
 
3515
                }
 
3516
                else
 
3517
                        ret = SQL_ERROR;
 
3518
                if (ret == SQL_ERROR && SC_get_errornumber(stmt) == 0)
 
3519
                {
 
3520
                        SC_set_error(stmt, STMT_ERROR_TAKEN_FROM_BACKEND, "SetPos update return error", func);
 
3521
                }
 
3522
        }
 
3523
        return ret;
 
3524
}
 
3525
 
 
3526
/* SQL_NEED_DATA callback for SC_pos_update */
 
3527
typedef struct
 
3528
{
 
3529
        BOOL            updyes;
 
3530
        QResultClass    *res;
 
3531
        StatementClass  *stmt, *qstmt;
 
3532
        IRDFields       *irdflds;
 
3533
        SQLSETPOSIROW           irow;
 
3534
        SQLULEN         global_ridx;
 
3535
}       pup_cdata;
 
3536
static RETCODE
 
3537
pos_update_callback(RETCODE retcode, void *para)
 
3538
{
 
3539
        CSTR    func = "pos_update_callback";
 
3540
        RETCODE ret = retcode;
 
3541
        pup_cdata *s = (pup_cdata *) para;
 
3542
        SQLLEN  kres_ridx;
 
3543
 
 
3544
        if (s->updyes)
 
3545
        {
 
3546
                mylog("pos_update_callback in\n");
 
3547
                ret = irow_update(ret, s->stmt, s->qstmt, s->irow, s->global_ridx);
 
3548
inolog("irow_update ret=%d,%d\n", ret, SC_get_errornumber(s->qstmt));
 
3549
                if (ret != SQL_SUCCESS)
 
3550
                        SC_error_copy(s->stmt, s->qstmt, TRUE);
 
3551
                PGAPI_FreeStmt(s->qstmt, SQL_DROP);
 
3552
                s->qstmt = NULL;
 
3553
        }
 
3554
        s->updyes = FALSE;
 
3555
        kres_ridx = GIdx2KResIdx(s->global_ridx, s->stmt, s->res);
 
3556
        if (kres_ridx < 0 || kres_ridx >= s->res->num_cached_keys)
 
3557
        {
 
3558
                SC_set_error(s->stmt, STMT_ROW_OUT_OF_RANGE, "the target rows is out of the rowset", func);
 
3559
inolog("gidx=%d num_keys=%d kresidx=%d\n", s->global_ridx, s->res->num_cached_keys, kres_ridx);
 
3560
                return SQL_ERROR;
 
3561
        }
 
3562
        if (SQL_SUCCESS == ret && s->res->keyset)
 
3563
        {
 
3564
                ConnectionClass *conn = SC_get_conn(s->stmt);
 
3565
 
 
3566
                if (CC_is_in_trans(conn))
 
3567
                {
 
3568
                        s->res->keyset[kres_ridx].status |= (SQL_ROW_UPDATED  | CURS_SELF_UPDATING);
 
3569
                }
 
3570
                else
 
3571
                        s->res->keyset[kres_ridx].status |= (SQL_ROW_UPDATED  | CURS_SELF_UPDATED);
 
3572
        }
 
3573
#if (ODBCVER >= 0x0300)
 
3574
        if (s->irdflds->rowStatusArray)
 
3575
        {
 
3576
                switch (ret)
 
3577
                {
 
3578
                        case SQL_SUCCESS:
 
3579
                                s->irdflds->rowStatusArray[s->irow] = SQL_ROW_UPDATED;
 
3580
                                break;
 
3581
                        default:
 
3582
                                s->irdflds->rowStatusArray[s->irow] = ret;
 
3583
                }
 
3584
        }
 
3585
#endif /* ODBCVER */
 
3586
 
 
3587
        return ret;
 
3588
}
 
3589
RETCODE
 
3590
SC_pos_update(StatementClass *stmt,
 
3591
                          SQLSETPOSIROW irow, SQLULEN global_ridx)
 
3592
{
 
3593
        CSTR    func = "SC_pos_update";
 
3594
        int                     i,
 
3595
                                num_cols,
 
3596
                                upd_cols;
 
3597
        pup_cdata       s;
 
3598
        ConnectionClass *conn;
 
3599
        ARDFields       *opts = SC_get_ARDF(stmt);
 
3600
        BindInfoClass *bindings = opts->bindings;
 
3601
        TABLE_INFO      *ti;
 
3602
        FIELD_INFO      **fi;
 
3603
        char            updstr[4096];
 
3604
        RETCODE         ret;
 
3605
        OID     oid;
 
3606
        UInt4   blocknum;
 
3607
        UInt2   pgoffset;
 
3608
        SQLLEN  offset;
 
3609
        SQLLEN  *used, kres_ridx;
 
3610
        Int4    bind_size = opts->bind_size;
 
3611
 
 
3612
        s.stmt = stmt;
 
3613
        s.irow = irow;
 
3614
        s.global_ridx = global_ridx;
 
3615
        s.irdflds = SC_get_IRDF(s.stmt);
 
3616
        fi = s.irdflds->fi;
 
3617
        if (!(s.res = SC_get_Curres(s.stmt)))
 
3618
        {
 
3619
                SC_set_error(s.stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in SC_pos_update.", func);
 
3620
                return SQL_ERROR;
 
3621
        }
 
3622
        mylog("POS UPDATE %d+%d fi=%p ti=%p\n", s.irow, QR_get_rowstart_in_cache(s.res), fi, s.stmt->ti);
 
3623
        if (SC_update_not_ready(stmt))
 
3624
                parse_statement(s.stmt, TRUE);  /* not preferable */
 
3625
        if (!SC_is_updatable(s.stmt))
 
3626
        {
 
3627
                s.stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY;
 
3628
                SC_set_error(s.stmt, STMT_INVALID_OPTION_IDENTIFIER, "the statement is read-only", func);
 
3629
                return SQL_ERROR;
 
3630
        }
 
3631
        kres_ridx = GIdx2KResIdx(s.global_ridx, s.stmt, s.res);
 
3632
        if (kres_ridx < 0 || kres_ridx >= s.res->num_cached_keys)
 
3633
        {
 
3634
                SC_set_error(s.stmt, STMT_ROW_OUT_OF_RANGE, "the target rows is out of the rowset", func);
 
3635
                return SQL_ERROR;
 
3636
        }
 
3637
        if (!(oid = getOid(s.res, kres_ridx)))
 
3638
        {
 
3639
                if (!strcmp(SAFE_NAME(stmt->ti[0]->bestitem), OID_NAME))
 
3640
                {
 
3641
                        SC_set_error(stmt, STMT_ROW_VERSION_CHANGED, "the row was already deleted ?", func);
 
3642
                        return SQL_ERROR;
 
3643
                }
 
3644
        }
 
3645
        getTid(s.res, kres_ridx, &blocknum, &pgoffset);
 
3646
 
 
3647
        ti = s.stmt->ti[0];
 
3648
        if (NAME_IS_VALID(ti->schema_name))
 
3649
                sprintf(updstr, "update \"%s\".\"%s\" set", SAFE_NAME(ti->schema_name), SAFE_NAME(ti->table_name));
 
3650
        else
 
3651
                sprintf(updstr, "update \"%s\" set", SAFE_NAME(ti->table_name));
 
3652
        num_cols = s.irdflds->nfields;
 
3653
        offset = opts->row_offset_ptr ? *opts->row_offset_ptr : 0;
 
3654
        for (i = upd_cols = 0; i < num_cols; i++)
 
3655
        {
 
3656
                if (used = bindings[i].used, used != NULL)
 
3657
                {
 
3658
                        used = LENADDR_SHIFT(used, offset);
 
3659
                        if (bind_size > 0)
 
3660
                                used = LENADDR_SHIFT(used, bind_size * s.irow);
 
3661
                        else    
 
3662
                                used = LENADDR_SHIFT(used, s.irow * sizeof(SQLLEN)); 
 
3663
                        mylog("%d used=%d,%p\n", i, *used, used);
 
3664
                        if (*used != SQL_IGNORE && fi[i]->updatable)
 
3665
                        {
 
3666
                                if (upd_cols)
 
3667
                                        sprintf(updstr, "%s, \"%s\" = ?", updstr, GET_NAME(fi[i]->column_name));
 
3668
                                else
 
3669
                                        sprintf(updstr, "%s \"%s\" = ?", updstr, GET_NAME(fi[i]->column_name));
 
3670
                                upd_cols++;
 
3671
                        }
 
3672
                }
 
3673
                else
 
3674
                        mylog("%d null bind\n", i);
 
3675
        }
 
3676
        conn = SC_get_conn(s.stmt);
 
3677
        s.updyes = FALSE;
 
3678
        if (upd_cols > 0)
 
3679
        {
 
3680
                HSTMT           hstmt;
 
3681
                int                     j;
 
3682
                ConnInfo        *ci = &(conn->connInfo);
 
3683
                APDFields       *apdopts;
 
3684
                IPDFields       *ipdopts;
 
3685
                OID             fieldtype = 0;
 
3686
                const char *bestitem = GET_NAME(ti->bestitem);
 
3687
                const char *bestqual = GET_NAME(ti->bestqual);
 
3688
 
 
3689
                sprintf(updstr, "%s where ctid = '(%u, %u)'", updstr,
 
3690
                                blocknum, pgoffset);
 
3691
                if (bestitem)
 
3692
                {
 
3693
                        /*sprintf(updstr, "%s and \"%s\" = %u", updstr, bestitem, oid);*/
 
3694
                        strcat(updstr, " and ");
 
3695
                        sprintf(updstr + strlen(updstr), bestqual, oid);
 
3696
                }
 
3697
                if (PG_VERSION_GE(conn, 8.2))
 
3698
                        strcat(updstr, " returning ctid");
 
3699
                mylog("updstr=%s\n", updstr);
 
3700
                if (PGAPI_AllocStmt(conn, &hstmt, 0) != SQL_SUCCESS)
 
3701
                {
 
3702
                        SC_set_error(s.stmt, STMT_NO_MEMORY_ERROR, "internal AllocStmt error", func);
 
3703
                        return SQL_ERROR;
 
3704
                }
 
3705
                s.qstmt = (StatementClass *) hstmt;
 
3706
                apdopts = SC_get_APDF(s.qstmt);
 
3707
                apdopts->param_bind_type = opts->bind_size;
 
3708
                apdopts->param_offset_ptr = opts->row_offset_ptr;
 
3709
                ipdopts = SC_get_IPDF(s.qstmt);
 
3710
                SC_set_delegate(s.stmt, s.qstmt);
 
3711
                extend_iparameter_bindings(ipdopts, num_cols);
 
3712
                for (i = j = 0; i < num_cols; i++)
 
3713
                {
 
3714
                        if (used = bindings[i].used, used != NULL)
 
3715
                        {
 
3716
                                used = LENADDR_SHIFT(used, offset);
 
3717
                                if (bind_size > 0)
 
3718
                                        used = LENADDR_SHIFT(used, bind_size * s.irow);
 
3719
                                else
 
3720
                                        used = LENADDR_SHIFT(used, s.irow * sizeof(SQLLEN));
 
3721
                                mylog("%d used=%d\n", i, *used);
 
3722
                                if (*used != SQL_IGNORE && fi[i]->updatable)
 
3723
                                {
 
3724
                                        /* fieldtype = QR_get_field_type(s.res, i); */
 
3725
                                        fieldtype = getEffectiveOid(conn, fi[i]);
 
3726
                                        PIC_set_pgtype(ipdopts->parameters[j], fieldtype);
 
3727
                                        PGAPI_BindParameter(hstmt,
 
3728
                                                (SQLUSMALLINT) ++j,
 
3729
                                                SQL_PARAM_INPUT,
 
3730
                                                bindings[i].returntype,
 
3731
                                                pgtype_to_concise_type(s.stmt, fieldtype, i),
 
3732
                                                                                                                                fi[i]->column_size > 0 ? fi[i]->column_size : pgtype_column_size(s.stmt, fieldtype, i, ci->drivers.unknown_sizes),
 
3733
                                                (SQLSMALLINT) fi[i]->decimal_digits,
 
3734
                                                bindings[i].buffer,
 
3735
                                                bindings[i].buflen,
 
3736
                                                bindings[i].used);
 
3737
                                }
 
3738
                        }
 
3739
                }
 
3740
                s.qstmt->exec_start_row = s.qstmt->exec_end_row = s.irow;
 
3741
                s.updyes = TRUE; 
 
3742
                ret = PGAPI_ExecDirect(hstmt, updstr, SQL_NTS, 0);
 
3743
                if (ret == SQL_NEED_DATA)
 
3744
                {
 
3745
                        pup_cdata *cbdata = (pup_cdata *) malloc(sizeof(pup_cdata));
 
3746
                        memcpy(cbdata, &s, sizeof(pup_cdata));
 
3747
                        if (0 == enqueueNeedDataCallback(s.stmt, pos_update_callback, cbdata))
 
3748
                                ret = SQL_ERROR;
 
3749
                        return ret;
 
3750
                }
 
3751
                /* else if (ret != SQL_SUCCESS) this is unneccesary 
 
3752
                        SC_error_copy(s.stmt, s.qstmt, TRUE); */
 
3753
        }
 
3754
        else
 
3755
        {
 
3756
                ret = SQL_SUCCESS_WITH_INFO;
 
3757
                SC_set_error(s.stmt, STMT_INVALID_CURSOR_STATE_ERROR, "update list null", func);
 
3758
        }
 
3759
 
 
3760
        ret = pos_update_callback(ret, &s);
 
3761
        return ret;
 
3762
}
 
3763
RETCODE
 
3764
SC_pos_delete(StatementClass *stmt,
 
3765
                          SQLSETPOSIROW irow, SQLULEN global_ridx)
 
3766
{
 
3767
        CSTR    func = "SC_pos_update";
 
3768
        UWORD           offset;
 
3769
        QResultClass *res, *qres;
 
3770
        ConnectionClass *conn = SC_get_conn(stmt);
 
3771
        IRDFields       *irdflds = SC_get_IRDF(stmt);
 
3772
        char            dltstr[4096];
 
3773
        RETCODE         ret;
 
3774
        SQLLEN          kres_ridx;
 
3775
        OID             oid;
 
3776
        UInt4           blocknum, qflag;
 
3777
        TABLE_INFO      *ti;
 
3778
        const char      *bestitem;
 
3779
        const char      *bestqual;
 
3780
 
 
3781
        mylog("POS DELETE ti=%p\n", stmt->ti);
 
3782
        if (!(res = SC_get_Curres(stmt)))
 
3783
        {
 
3784
                SC_set_error(stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in SC_pos_delete.", func);
 
3785
                return SQL_ERROR;
 
3786
        }
 
3787
        if (SC_update_not_ready(stmt))
 
3788
                parse_statement(stmt, TRUE);    /* not preferable */
 
3789
        if (!SC_is_updatable(stmt))
 
3790
        {
 
3791
                stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY;
 
3792
                SC_set_error(stmt, STMT_INVALID_OPTION_IDENTIFIER, "the statement is read-only", func);
 
3793
                return SQL_ERROR;
 
3794
        }
 
3795
        kres_ridx = GIdx2KResIdx(global_ridx, stmt, res);
 
3796
        if (kres_ridx < 0 || kres_ridx >= res->num_cached_keys)
 
3797
        {
 
3798
                SC_set_error(stmt, STMT_ROW_OUT_OF_RANGE, "the target rows is out of the rowset", func);
 
3799
                return SQL_ERROR;
 
3800
        }
 
3801
        ti = stmt->ti[0];
 
3802
        bestitem = GET_NAME(ti->bestitem);
 
3803
        if (!(oid = getOid(res, kres_ridx)))
 
3804
        {
 
3805
                if (bestitem && !strcmp(bestitem, OID_NAME))
 
3806
                {
 
3807
                        SC_set_error(stmt, STMT_ROW_VERSION_CHANGED, "the row was already deleted ?", func);
 
3808
                        return SQL_ERROR;
 
3809
                }
 
3810
        }
 
3811
        bestqual = GET_NAME(ti->bestqual);
 
3812
        getTid(res, kres_ridx, &blocknum, &offset);
 
3813
        /*sprintf(dltstr, "delete from \"%s\" where ctid = '%s' and oid = %s",*/
 
3814
        if (NAME_IS_VALID(ti->schema_name))
 
3815
                sprintf(dltstr, "delete from \"%s\".\"%s\" where ctid = '(%u, %u)'",
 
3816
                SAFE_NAME(ti->schema_name), SAFE_NAME(ti->table_name), blocknum, offset);
 
3817
        else
 
3818
                sprintf(dltstr, "delete from \"%s\" where ctid = '(%u, %u)'",
 
3819
                        SAFE_NAME(ti->table_name), blocknum, offset);
 
3820
        if (bestitem)
 
3821
        {
 
3822
                /*sprintf(dltstr, "%s and \"%s\" = %u", dltstr, bestitem, oid);*/
 
3823
                strcat(dltstr, " and ");
 
3824
                sprintf(dltstr + strlen(dltstr), bestqual, oid);
 
3825
        }
 
3826
 
 
3827
        mylog("dltstr=%s\n", dltstr);
 
3828
        qflag = 0;
 
3829
        if (!stmt->internal && !CC_is_in_trans(conn) &&
 
3830
                 (!CC_does_autocommit(conn)))
 
3831
                qflag |= GO_INTO_TRANSACTION;
 
3832
        qres = CC_send_query(conn, dltstr, NULL, qflag, stmt);
 
3833
        ret = SQL_SUCCESS;
 
3834
        if (QR_command_maybe_successful(qres))
 
3835
        {
 
3836
                int                     dltcnt;
 
3837
                const char *cmdstr = QR_get_command(qres);
 
3838
 
 
3839
                if (cmdstr &&
 
3840
                        sscanf(cmdstr, "DELETE %d", &dltcnt) == 1)
 
3841
                {
 
3842
                        if (dltcnt == 1)
 
3843
                        {
 
3844
                                RETCODE tret = SC_pos_reload(stmt, global_ridx, (UInt2 *) 0, SQL_DELETE);
 
3845
                                if (!SQL_SUCCEEDED(tret))
 
3846
                                        ret = tret;
 
3847
                        }
 
3848
                        else if (dltcnt == 0)
 
3849
                        {
 
3850
                                SC_set_error(stmt, STMT_ROW_VERSION_CHANGED, "the content was changed before deletion", func);
 
3851
                                ret = SQL_ERROR;
 
3852
                                if (stmt->options.cursor_type == SQL_CURSOR_KEYSET_DRIVEN)
 
3853
                                        SC_pos_reload(stmt, global_ridx, (UInt2 *) 0, 0);
 
3854
                        }
 
3855
                        else
 
3856
                                ret = SQL_ERROR;
 
3857
                }
 
3858
                else
 
3859
                        ret = SQL_ERROR;
 
3860
        }
 
3861
        else
 
3862
        {
 
3863
                ret = SQL_ERROR;
 
3864
                strcpy(res->sqlstate, qres->sqlstate);
 
3865
                res->message = qres->message;
 
3866
                qres->message = NULL;
 
3867
        }
 
3868
        if (ret == SQL_ERROR && SC_get_errornumber(stmt) == 0)
 
3869
        {
 
3870
                SC_set_error(stmt, STMT_ERROR_TAKEN_FROM_BACKEND, "SetPos delete return error", func);
 
3871
        }
 
3872
        if (qres)
 
3873
                QR_Destructor(qres);
 
3874
        if (SQL_SUCCESS == ret && res->keyset)
 
3875
        {
 
3876
                AddDeleted(res, global_ridx, res->keyset + kres_ridx);
 
3877
                res->keyset[kres_ridx].status &= (~KEYSET_INFO_PUBLIC);
 
3878
                if (CC_is_in_trans(conn))
 
3879
                {
 
3880
                        res->keyset[kres_ridx].status |= (SQL_ROW_DELETED | CURS_SELF_DELETING);
 
3881
                }
 
3882
                else
 
3883
                        res->keyset[kres_ridx].status |= (SQL_ROW_DELETED | CURS_SELF_DELETED);
 
3884
inolog(".status[%d]=%x\n", global_ridx, res->keyset[kres_ridx].status);
 
3885
        }
 
3886
#if (ODBCVER >= 0x0300)
 
3887
        if (irdflds->rowStatusArray)
 
3888
        {
 
3889
                switch (ret)
 
3890
                {
 
3891
                        case SQL_SUCCESS:
 
3892
                                irdflds->rowStatusArray[irow] = SQL_ROW_DELETED;
 
3893
                                break;
 
3894
                        default:
 
3895
                                irdflds->rowStatusArray[irow] = ret;
 
3896
                }
 
3897
        }
 
3898
#endif /* ODBCVER */
 
3899
        return ret;
 
3900
}
 
3901
 
 
3902
static RETCODE SQL_API
 
3903
irow_insert(RETCODE ret, StatementClass *stmt, StatementClass *istmt, SQLLEN addpos)
 
3904
{
 
3905
        CSTR    func = "irow_insert";
 
3906
 
 
3907
        if (ret != SQL_ERROR)
 
3908
        {
 
3909
                int             addcnt;
 
3910
                OID             oid, *poid = NULL;
 
3911
                ARDFields       *opts = SC_get_ARDF(stmt);
 
3912
                QResultClass    *ires = SC_get_Curres(istmt), *tres;
 
3913
                const char *cmdstr;
 
3914
                BindInfoClass   *bookmark;
 
3915
 
 
3916
                tres = (ires->next ? ires->next : ires);
 
3917
                cmdstr = QR_get_command(tres);
 
3918
                if (cmdstr &&
 
3919
                        sscanf(cmdstr, "INSERT %u %d", &oid, &addcnt) == 2 &&
 
3920
                        addcnt == 1)
 
3921
                {
 
3922
                        ConnectionClass *conn = SC_get_conn(stmt);
 
3923
                        RETCODE qret;
 
3924
 
 
3925
                        if (0 != oid)
 
3926
                                poid = &oid;
 
3927
                        qret = SQL_NO_DATA_FOUND;
 
3928
                        if (PG_VERSION_GE(conn, 7.2))
 
3929
                        {
 
3930
                                const char * tidval = NULL;
 
3931
 
 
3932
                                if (NULL != tres->backend_tuples &&
 
3933
                                    1 == QR_get_num_cached_tuples(tres))
 
3934
                                        tidval = QR_get_value_backend_text(tres, 0, 0);
 
3935
                                qret = SC_pos_newload(stmt, poid, TRUE, tidval);
 
3936
                                if (SQL_ERROR == qret)
 
3937
                                        return qret;
 
3938
                        }
 
3939
                        if (SQL_NO_DATA_FOUND == qret)
 
3940
                        {
 
3941
                                qret = SC_pos_newload(stmt, poid, FALSE, NULL);
 
3942
                                if (SQL_ERROR == qret)
 
3943
                                        return qret;
 
3944
                        }
 
3945
                        bookmark = opts->bookmark;
 
3946
                        if (bookmark && bookmark->buffer)
 
3947
                        {
 
3948
                                char    buf[32];
 
3949
                                SQLULEN offset = opts->row_offset_ptr ? *opts->row_offset_ptr : 0;
 
3950
 
 
3951
                                snprintf(buf, sizeof(buf), FORMAT_LEN, SC_make_bookmark(addpos));
 
3952
                                SC_set_current_col(stmt, -1);
 
3953
                                copy_and_convert_field(stmt,
 
3954
                                        PG_TYPE_INT4,
 
3955
                                        PG_UNSPECIFIED,
 
3956
                                        buf,
 
3957
                                        bookmark->returntype,
 
3958
                                        0,
 
3959
                                        bookmark->buffer + offset,
 
3960
                                        bookmark->buflen,
 
3961
                                        LENADDR_SHIFT(bookmark->used, offset),
 
3962
                                        LENADDR_SHIFT(bookmark->used, offset));
 
3963
                        }
 
3964
                }
 
3965
                else
 
3966
                {
 
3967
                        SC_set_error(stmt, STMT_ERROR_TAKEN_FROM_BACKEND, "SetPos insert return error", func);
 
3968
                }
 
3969
        }
 
3970
        return ret;
 
3971
}
 
3972
 
 
3973
/* SQL_NEED_DATA callback for SC_pos_add */
 
3974
typedef struct
 
3975
{
 
3976
        BOOL            updyes;
 
3977
        QResultClass    *res;
 
3978
        StatementClass  *stmt, *qstmt;
 
3979
        IRDFields       *irdflds;
 
3980
        SQLSETPOSIROW           irow;
 
3981
}       padd_cdata;
 
3982
 
 
3983
static RETCODE
 
3984
pos_add_callback(RETCODE retcode, void *para)
 
3985
{
 
3986
        RETCODE ret = retcode;
 
3987
        padd_cdata *s = (padd_cdata *) para;
 
3988
        SQLLEN  addpos;
 
3989
 
 
3990
        if (s->updyes)
 
3991
        {
 
3992
                SQLSETPOSIROW   brow_save;
 
3993
                
 
3994
                mylog("pos_add_callback in ret=%d\n", ret);
 
3995
                brow_save = s->stmt->bind_row; 
 
3996
                s->stmt->bind_row = s->irow;
 
3997
                if (QR_get_cursor(s->res))
 
3998
                        addpos = -(SQLLEN)(s->res->ad_count + 1);
 
3999
                else
 
4000
                        addpos = QR_get_num_total_tuples(s->res); 
 
4001
                ret = irow_insert(ret, s->stmt, s->qstmt, addpos);
 
4002
                s->stmt->bind_row = brow_save;
 
4003
        }
 
4004
        s->updyes = FALSE;
 
4005
        SC_setInsertedTable(s->qstmt, ret);
 
4006
        if (ret != SQL_SUCCESS)
 
4007
                SC_error_copy(s->stmt, s->qstmt, TRUE);
 
4008
        PGAPI_FreeStmt((HSTMT) s->qstmt, SQL_DROP);
 
4009
        s->qstmt = NULL;
 
4010
        if (SQL_SUCCESS == ret && s->res->keyset)
 
4011
        {
 
4012
                SQLLEN  global_ridx = QR_get_num_total_tuples(s->res) - 1;
 
4013
                ConnectionClass *conn = SC_get_conn(s->stmt);
 
4014
                SQLLEN  kres_ridx;
 
4015
                UWORD   status = SQL_ROW_ADDED;
 
4016
 
 
4017
                if (CC_is_in_trans(conn))
 
4018
                        status |= CURS_SELF_ADDING;
 
4019
                else
 
4020
                        status |= CURS_SELF_ADDED;
 
4021
                kres_ridx = GIdx2KResIdx(global_ridx, s->stmt, s->res);
 
4022
                if (kres_ridx >= 0 || kres_ridx < s->res->num_cached_keys)
 
4023
                {
 
4024
                        s->res->keyset[kres_ridx].status = status;
 
4025
                }
 
4026
        }
 
4027
#if (ODBCVER >= 0x0300)
 
4028
        if (s->irdflds->rowStatusArray)
 
4029
        {
 
4030
                switch (ret)
 
4031
                {
 
4032
                        case SQL_SUCCESS:
 
4033
                                s->irdflds->rowStatusArray[s->irow] = SQL_ROW_ADDED;
 
4034
                                break;
 
4035
                        default:
 
4036
                                s->irdflds->rowStatusArray[s->irow] = ret;
 
4037
                }
 
4038
        }
 
4039
#endif /* ODBCVER */
 
4040
 
 
4041
        return ret;
 
4042
}
 
4043
 
 
4044
RETCODE
 
4045
SC_pos_add(StatementClass *stmt,
 
4046
                   SQLSETPOSIROW irow)
 
4047
{
 
4048
        CSTR    func = "SC_pos_add";
 
4049
        int                     num_cols,
 
4050
                                add_cols,
 
4051
                                i;
 
4052
        HSTMT           hstmt;
 
4053
 
 
4054
        padd_cdata      s;
 
4055
        ConnectionClass *conn;
 
4056
        ConnInfo        *ci;
 
4057
        ARDFields       *opts = SC_get_ARDF(stmt);
 
4058
        APDFields       *apdopts;
 
4059
        IPDFields       *ipdopts;
 
4060
        BindInfoClass *bindings = opts->bindings;
 
4061
        FIELD_INFO      **fi = SC_get_IRDF(stmt)->fi;
 
4062
        char            addstr[4096];
 
4063
        RETCODE         ret;
 
4064
        SQLULEN         offset;
 
4065
        SQLLEN          *used;
 
4066
        Int4            bind_size = opts->bind_size;
 
4067
        OID             fieldtype;
 
4068
        int             func_cs_count = 0;
 
4069
 
 
4070
        mylog("POS ADD fi=%p ti=%p\n", fi, stmt->ti);
 
4071
        s.stmt = stmt;
 
4072
        s.irow = irow;
 
4073
        if (!(s.res = SC_get_Curres(s.stmt)))
 
4074
        {
 
4075
                SC_set_error(s.stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in SC_pos_add.", func);
 
4076
                return SQL_ERROR;
 
4077
        }
 
4078
        if (SC_update_not_ready(stmt))
 
4079
                parse_statement(s.stmt, TRUE);  /* not preferable */
 
4080
        if (!SC_is_updatable(s.stmt))
 
4081
        {
 
4082
                s.stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY;
 
4083
                SC_set_error(s.stmt, STMT_INVALID_OPTION_IDENTIFIER, "the statement is read-only", func);
 
4084
                return SQL_ERROR;
 
4085
        }
 
4086
        s.irdflds = SC_get_IRDF(s.stmt);
 
4087
        num_cols = s.irdflds->nfields;
 
4088
        conn = SC_get_conn(s.stmt);
 
4089
        if (NAME_IS_VALID(s.stmt->ti[0]->schema_name))
 
4090
                sprintf(addstr, "insert into \"%s\".\"%s\" (", SAFE_NAME(s.stmt->ti[0]->schema_name), SAFE_NAME(s.stmt->ti[0]->table_name));
 
4091
        else
 
4092
                sprintf(addstr, "insert into \"%s\" (", SAFE_NAME(s.stmt->ti[0]->table_name));
 
4093
        if (PGAPI_AllocStmt(conn, &hstmt, 0) != SQL_SUCCESS)
 
4094
        {
 
4095
                SC_set_error(s.stmt, STMT_NO_MEMORY_ERROR, "internal AllocStmt error", func);
 
4096
                return SQL_ERROR;
 
4097
        }
 
4098
        if (opts->row_offset_ptr)
 
4099
                offset = *opts->row_offset_ptr;
 
4100
        else
 
4101
                offset = 0;
 
4102
        s.qstmt = (StatementClass *) hstmt;
 
4103
        apdopts = SC_get_APDF(s.qstmt);
 
4104
        apdopts->param_bind_type = opts->bind_size;
 
4105
        apdopts->param_offset_ptr = opts->row_offset_ptr;
 
4106
        ipdopts = SC_get_IPDF(s.qstmt);
 
4107
        SC_set_delegate(s.stmt, s.qstmt);
 
4108
        ci = &(conn->connInfo);
 
4109
        extend_iparameter_bindings(ipdopts, num_cols);
 
4110
        for (i = add_cols = 0; i < num_cols; i++)
 
4111
        {
 
4112
                if (used = bindings[i].used, used != NULL)
 
4113
                {
 
4114
                        used = LENADDR_SHIFT(used, offset);
 
4115
                        if (bind_size > 0)
 
4116
                                used = LENADDR_SHIFT(used, bind_size * s.irow);
 
4117
                        else
 
4118
                                used = LENADDR_SHIFT(used, s.irow * sizeof(SQLLEN));
 
4119
                        mylog("%d used=%d\n", i, *used);
 
4120
                        if (*used != SQL_IGNORE && fi[i]->updatable)
 
4121
                        {
 
4122
                                /* fieldtype = QR_get_field_type(s.res, i); */
 
4123
                                fieldtype = getEffectiveOid(conn, fi[i]);
 
4124
                                if (add_cols)
 
4125
                                        sprintf(addstr, "%s, \"%s\"", addstr, GET_NAME(fi[i]->column_name));
 
4126
                                else
 
4127
                                        sprintf(addstr, "%s\"%s\"", addstr, GET_NAME(fi[i]->column_name));
 
4128
                                PIC_set_pgtype(ipdopts->parameters[add_cols], fieldtype);
 
4129
                                PGAPI_BindParameter(hstmt,
 
4130
                                        (SQLUSMALLINT) ++add_cols,
 
4131
                                        SQL_PARAM_INPUT,
 
4132
                                        bindings[i].returntype,
 
4133
                                        pgtype_to_concise_type(s.stmt, fieldtype, i),
 
4134
                                                                                                                        fi[i]->column_size > 0 ? fi[i]->column_size : pgtype_column_size(s.stmt, fieldtype, i, ci->drivers.unknown_sizes),
 
4135
                                        (SQLSMALLINT) fi[i]->decimal_digits,
 
4136
                                        bindings[i].buffer,
 
4137
                                        bindings[i].buflen,
 
4138
                                        bindings[i].used);
 
4139
                        }
 
4140
                }
 
4141
                else
 
4142
                        mylog("%d null bind\n", i);
 
4143
        }
 
4144
        s.updyes = FALSE;
 
4145
#define return  DONT_CALL_RETURN_FROM_HERE???
 
4146
        ENTER_INNER_CONN_CS(conn, func_cs_count); 
 
4147
        if (add_cols > 0)
 
4148
        {
 
4149
                sprintf(addstr, "%s) values (", addstr);
 
4150
                for (i = 0; i < add_cols; i++)
 
4151
                {
 
4152
                        if (i)
 
4153
                                strcat(addstr, ", ?");
 
4154
                        else
 
4155
                                strcat(addstr, "?");
 
4156
                }
 
4157
                strcat(addstr, ")");
 
4158
                if (PG_VERSION_GE(conn, 8.2))
 
4159
                        strcat(addstr, " returning ctid");
 
4160
                mylog("addstr=%s\n", addstr);
 
4161
                s.qstmt->exec_start_row = s.qstmt->exec_end_row = s.irow;
 
4162
                s.updyes = TRUE;
 
4163
                ret = PGAPI_ExecDirect(hstmt, addstr, SQL_NTS, 0);
 
4164
                if (ret == SQL_NEED_DATA)
 
4165
                {
 
4166
                        padd_cdata *cbdata = (padd_cdata *) malloc(sizeof(padd_cdata));
 
4167
                        memcpy(cbdata, &s, sizeof(padd_cdata));
 
4168
                        if (0 == enqueueNeedDataCallback(s.stmt, pos_add_callback, cbdata))
 
4169
                                ret = SQL_ERROR;
 
4170
                        goto cleanup;
 
4171
                }
 
4172
                /* else if (ret != SQL_SUCCESS) this is unneccesary
 
4173
                        SC_error_copy(s.stmt, s.qstmt, TRUE); */
 
4174
        }
 
4175
        else
 
4176
        {
 
4177
                ret = SQL_SUCCESS_WITH_INFO;
 
4178
                SC_set_error(s.stmt, STMT_INVALID_CURSOR_STATE_ERROR, "insert list null", func);
 
4179
        }
 
4180
 
 
4181
        ret = pos_add_callback(ret, &s);
 
4182
 
 
4183
cleanup:
 
4184
#undef  return
 
4185
        CLEANUP_FUNC_CONN_CS(func_cs_count, conn);
 
4186
        return ret;
 
4187
}
 
4188
 
 
4189
/*
 
4190
 *      Stuff for updatable cursors end.
 
4191
 */
 
4192
 
 
4193
RETCODE
 
4194
SC_pos_refresh(StatementClass *stmt, SQLSETPOSIROW irow , SQLULEN global_ridx)
 
4195
{
 
4196
        RETCODE ret;
 
4197
#if (ODBCVER >= 0x0300)
 
4198
        IRDFields       *irdflds = SC_get_IRDF(stmt);
 
4199
#endif /* ODBCVER */
 
4200
        /* save the last_fetch_count */
 
4201
        SQLLEN          last_fetch = stmt->last_fetch_count;
 
4202
        SQLLEN          last_fetch2 = stmt->last_fetch_count_include_ommitted;
 
4203
        SQLSETPOSIROW   bind_save = stmt->bind_row;
 
4204
        BOOL            tuple_reload = FALSE;
 
4205
 
 
4206
        if (stmt->options.cursor_type == SQL_CURSOR_KEYSET_DRIVEN)
 
4207
                tuple_reload = TRUE;
 
4208
        else 
 
4209
        {
 
4210
                QResultClass    *res = SC_get_Curres(stmt);
 
4211
                if (res && res->keyset)
 
4212
                {
 
4213
                        SQLLEN kres_ridx = GIdx2KResIdx(global_ridx, stmt, res);
 
4214
                        if (kres_ridx >= 0 && kres_ridx < QR_get_num_cached_tuples(res))
 
4215
                        {
 
4216
                                if (0 != (CURS_NEEDS_REREAD & res->keyset[kres_ridx].status))
 
4217
                                        tuple_reload = TRUE;
 
4218
                        }
 
4219
                }
 
4220
        }
 
4221
        if (tuple_reload)
 
4222
                SC_pos_reload(stmt, global_ridx, (UInt2 *) 0, 0);
 
4223
        stmt->bind_row = irow;
 
4224
        ret = SC_fetch(stmt);
 
4225
        /* restore the last_fetch_count */
 
4226
        stmt->last_fetch_count = last_fetch;
 
4227
        stmt->last_fetch_count_include_ommitted = last_fetch2;
 
4228
        stmt->bind_row = bind_save;
 
4229
#if (ODBCVER >= 0x0300)
 
4230
        if (irdflds->rowStatusArray)
 
4231
        {
 
4232
                switch (ret)
 
4233
                {
 
4234
                        case SQL_ERROR:
 
4235
                                irdflds->rowStatusArray[irow] = SQL_ROW_ERROR;
 
4236
                                break;
 
4237
                        case SQL_SUCCESS:
 
4238
                                irdflds->rowStatusArray[irow] = SQL_ROW_SUCCESS;
 
4239
                                break;
 
4240
                        case SQL_SUCCESS_WITH_INFO:
 
4241
                        default:
 
4242
                                irdflds->rowStatusArray[irow] = ret;
 
4243
                                break;
 
4244
                }
 
4245
        }
 
4246
#endif /* ODBCVER */
 
4247
 
 
4248
        return SQL_SUCCESS;
 
4249
}
 
4250
 
 
4251
/*      SQL_NEED_DATA callback for PGAPI_SetPos */
 
4252
typedef struct
 
4253
{
 
4254
        BOOL            need_data_callback, auto_commit_needed;
 
4255
        QResultClass    *res;
 
4256
        StatementClass  *stmt;
 
4257
        ARDFields       *opts;
 
4258
        GetDataInfo     *gdata;
 
4259
        SQLLEN  idx, start_row, end_row, ridx;
 
4260
        UWORD   fOption;
 
4261
        SQLSETPOSIROW   irow, nrow, processed;
 
4262
}       spos_cdata;
 
4263
static 
 
4264
RETCODE spos_callback(RETCODE retcode, void *para)
 
4265
{
 
4266
        CSTR    func = "spos_callback";
 
4267
        RETCODE ret;
 
4268
        spos_cdata *s = (spos_cdata *) para;
 
4269
        QResultClass    *res;
 
4270
        ARDFields       *opts;
 
4271
        ConnectionClass *conn;
 
4272
        SQLULEN global_ridx;
 
4273
        SQLLEN  kres_ridx, pos_ridx = 0;
 
4274
 
 
4275
        ret = retcode;
 
4276
        mylog("%s: %d in\n", func, s->need_data_callback);
 
4277
        if (s->need_data_callback)
 
4278
        {
 
4279
                s->processed++;
 
4280
                if (SQL_ERROR != retcode)
 
4281
                {
 
4282
                        s->nrow++;
 
4283
                        s->idx++;
 
4284
                }
 
4285
        }
 
4286
        else
 
4287
        {
 
4288
                s->ridx = -1;
 
4289
                s->idx = s->nrow = s->processed = 0;
 
4290
        }
 
4291
        res = s->res;
 
4292
        opts = s->opts;
 
4293
        if (!res || !opts)
 
4294
        {
 
4295
                SC_set_error(s->stmt, STMT_SEQUENCE_ERROR, "Passed res or opts for spos_callback is NULL", func);
 
4296
                return SQL_ERROR;
 
4297
        }
 
4298
        s->need_data_callback = FALSE;
 
4299
        for (; SQL_ERROR != ret && s->nrow <= s->end_row; s->idx++)
 
4300
        {
 
4301
                global_ridx = RowIdx2GIdx(s->idx, s->stmt);
 
4302
                if (SQL_ADD != s->fOption)
 
4303
                {
 
4304
                        if ((int) global_ridx >= QR_get_num_total_tuples(res))
 
4305
                                break;
 
4306
                        if (res->keyset)
 
4307
                        {
 
4308
                                kres_ridx = GIdx2KResIdx(global_ridx, s->stmt, res);
 
4309
                                if (kres_ridx >= res->num_cached_keys)
 
4310
                                        break;
 
4311
                                if (kres_ridx >= 0) /* the row may be deleted and not in the rowset */
 
4312
                                {
 
4313
                                        if (0 == (res->keyset[kres_ridx].status & CURS_IN_ROWSET))
 
4314
                                                continue;
 
4315
                                }
 
4316
                        }
 
4317
                }
 
4318
                if (s->nrow < s->start_row)
 
4319
                {
 
4320
                        s->nrow++;
 
4321
                        continue;
 
4322
                }       
 
4323
                s->ridx = s->nrow;
 
4324
                pos_ridx = s->idx;
 
4325
#if (ODBCVER >= 0x0300)
 
4326
                if (0 != s->irow || !opts->row_operation_ptr || opts->row_operation_ptr[s->nrow] == SQL_ROW_PROCEED)
 
4327
                {
 
4328
#endif /* ODBCVER */
 
4329
                        switch (s->fOption)
 
4330
                        {
 
4331
                                case SQL_UPDATE:
 
4332
                                        ret = SC_pos_update(s->stmt, s->nrow, global_ridx);
 
4333
                                        break;
 
4334
                                case SQL_DELETE:
 
4335
                                        ret = SC_pos_delete(s->stmt, s->nrow, global_ridx);
 
4336
                                        break;
 
4337
                                case SQL_ADD:
 
4338
                                        ret = SC_pos_add(s->stmt, s->nrow);
 
4339
                                        break;
 
4340
                                case SQL_REFRESH:
 
4341
                                        ret = SC_pos_refresh(s->stmt, s->nrow, global_ridx);
 
4342
                                        break;
 
4343
                        }
 
4344
                        if (SQL_NEED_DATA == ret)
 
4345
                        {
 
4346
                                spos_cdata *cbdata = (spos_cdata *) malloc(sizeof(spos_cdata));
 
4347
 
 
4348
                                memcpy(cbdata, s, sizeof(spos_cdata));
 
4349
                                cbdata->need_data_callback = TRUE;
 
4350
                                if (0 == enqueueNeedDataCallback(s->stmt, spos_callback, cbdata))
 
4351
                                        ret = SQL_ERROR;
 
4352
                                return ret;
 
4353
                        }
 
4354
                        s->processed++;
 
4355
#if (ODBCVER >= 0x0300)
 
4356
                }
 
4357
#endif /* ODBCVER */
 
4358
                if (SQL_ERROR != ret)
 
4359
                        s->nrow++;
 
4360
        }
 
4361
        conn = SC_get_conn(s->stmt);
 
4362
        if (s->auto_commit_needed)
 
4363
                CC_set_autocommit(conn, TRUE);
 
4364
        if (s->irow > 0)
 
4365
        {
 
4366
                if (SQL_ADD != s->fOption && s->ridx >= 0) /* for SQLGetData */
 
4367
                {
 
4368
                        s->stmt->currTuple = RowIdx2GIdx(pos_ridx, s->stmt);
 
4369
                        QR_set_position(res, pos_ridx);
 
4370
                }
 
4371
        }
 
4372
        else if (SC_get_IRDF(s->stmt)->rowsFetched)
 
4373
                *(SC_get_IRDF(s->stmt)->rowsFetched) = s->processed;
 
4374
        res->recent_processed_row_count = s->stmt->diag_row_count = s->processed;
 
4375
if (opts)
 
4376
{
 
4377
inolog("processed=%d ret=%d rowset=%d", s->processed, ret, opts->size_of_rowset_odbc2);
 
4378
#if (ODBCVER >= 0x0300)
 
4379
inolog(",%d\n", opts->size_of_rowset);
 
4380
#else
 
4381
inolog("\n");
 
4382
#endif /* ODBCVER */
 
4383
}
 
4384
 
 
4385
        return ret;
 
4386
}
 
4387
 
 
4388
/*
 
4389
 *      This positions the cursor within a rowset, that was positioned using SQLExtendedFetch.
 
4390
 *      This will be useful (so far) only when using SQLGetData after SQLExtendedFetch.
 
4391
 */
 
4392
RETCODE         SQL_API
 
4393
PGAPI_SetPos(
 
4394
                         HSTMT hstmt,
 
4395
                         SQLSETPOSIROW irow,
 
4396
                         SQLUSMALLINT fOption,
 
4397
                         SQLUSMALLINT fLock)
 
4398
{
 
4399
        CSTR func = "PGAPI_SetPos";
 
4400
        RETCODE ret;
 
4401
        ConnectionClass *conn;
 
4402
        SQLLEN          rowsetSize;
 
4403
        int             i;
 
4404
        UInt2           gdata_allocated;
 
4405
        GetDataInfo     *gdata_info;
 
4406
        GetDataClass    *gdata = NULL;
 
4407
        spos_cdata      s;
 
4408
 
 
4409
        s.stmt = (StatementClass *) hstmt;
 
4410
        if (!s.stmt)
 
4411
        {
 
4412
                SC_log_error(func, NULL_STRING, NULL);
 
4413
                return SQL_INVALID_HANDLE;
 
4414
        }
 
4415
 
 
4416
        s.irow = irow;
 
4417
        s.fOption = fOption;
 
4418
        s.auto_commit_needed = FALSE;
 
4419
        s.opts = SC_get_ARDF(s.stmt);
 
4420
        gdata_info = SC_get_GDTI(s.stmt);
 
4421
        gdata = gdata_info->gdata;
 
4422
        mylog("%s fOption=%d irow=%d lock=%d currt=%d\n", func, s.fOption, s.irow, fLock, s.stmt->currTuple);
 
4423
        if (s.stmt->options.scroll_concurrency != SQL_CONCUR_READ_ONLY)
 
4424
                ;
 
4425
        else if (s.fOption != SQL_POSITION && s.fOption != SQL_REFRESH)
 
4426
        {
 
4427
                SC_set_error(s.stmt, STMT_NOT_IMPLEMENTED_ERROR, "Only SQL_POSITION/REFRESH is supported for PGAPI_SetPos", func);
 
4428
                return SQL_ERROR;
 
4429
        }
 
4430
 
 
4431
        if (!(s.res = SC_get_Curres(s.stmt)))
 
4432
        {
 
4433
                SC_set_error(s.stmt, STMT_INVALID_CURSOR_STATE_ERROR, "Null statement result in PGAPI_SetPos.", func);
 
4434
                return SQL_ERROR;
 
4435
        }
 
4436
 
 
4437
#if (ODBCVER >= 0x0300)
 
4438
        rowsetSize = (s.stmt->transition_status == STMT_TRANSITION_EXTENDED_FETCH ? s.opts->size_of_rowset_odbc2 : s.opts->size_of_rowset);
 
4439
#else
 
4440
        rowsetSize = s.opts->size_of_rowset_odbc2;
 
4441
#endif /* ODBCVER */
 
4442
        if (s.irow == 0) /* bulk operation */
 
4443
        {
 
4444
                if (SQL_POSITION == s.fOption)
 
4445
                {
 
4446
                        SC_set_error(s.stmt, STMT_INVALID_CURSOR_POSITION, "Bulk Position operations not allowed.", func);
 
4447
                        return SQL_ERROR;
 
4448
                }
 
4449
                s.start_row = 0;
 
4450
                s.end_row = rowsetSize - 1;
 
4451
        }
 
4452
        else
 
4453
        {
 
4454
                if (SQL_ADD != s.fOption && s.irow > s.stmt->last_fetch_count)
 
4455
                {
 
4456
                        SC_set_error(s.stmt, STMT_ROW_OUT_OF_RANGE, "Row value out of range", func);
 
4457
                        return SQL_ERROR;
 
4458
                }
 
4459
                s.start_row = s.end_row = s.irow - 1;
 
4460
        }
 
4461
 
 
4462
        gdata_allocated = gdata_info->allocated;
 
4463
mylog("num_cols=%d gdatainfo=%d\n", QR_NumPublicResultCols(s.res), gdata_allocated);
 
4464
        /* Reset for SQLGetData */
 
4465
        if (gdata)
 
4466
        {
 
4467
                for (i = 0; i < gdata_allocated; i++)
 
4468
                        gdata[i].data_left = -1;
 
4469
        }
 
4470
        ret = SQL_SUCCESS;
 
4471
        conn = SC_get_conn(s.stmt);
 
4472
        switch (s.fOption)
 
4473
        {
 
4474
                case SQL_UPDATE:
 
4475
                case SQL_DELETE:
 
4476
                case SQL_ADD:
 
4477
                        if (s.auto_commit_needed = CC_does_autocommit(conn), s.auto_commit_needed)
 
4478
                                CC_set_autocommit(conn, FALSE);
 
4479
                        break;
 
4480
                case SQL_POSITION:
 
4481
                        break;
 
4482
        }
 
4483
 
 
4484
        s.need_data_callback = FALSE;
 
4485
#define return  DONT_CALL_RETURN_FROM_HERE???
 
4486
        /* StartRollbackState(s.stmt); */
 
4487
        ret = spos_callback(SQL_SUCCESS, &s);
 
4488
#undef  return
 
4489
        if (s.stmt->internal)
 
4490
                ret = DiscardStatementSvp(s.stmt, ret, FALSE);
 
4491
        mylog("%s returning %d\n", func, ret);
 
4492
        return ret;
 
4493
}
 
4494
 
 
4495
 
 
4496
/*              Sets options that control the behavior of cursors. */
 
4497
RETCODE         SQL_API
 
4498
PGAPI_SetScrollOptions( HSTMT hstmt,
 
4499
                                SQLUSMALLINT fConcurrency,
 
4500
                                SQLLEN crowKeyset,
 
4501
                                SQLUSMALLINT crowRowset)
 
4502
{
 
4503
        CSTR func = "PGAPI_SetScrollOptions";
 
4504
        StatementClass *stmt = (StatementClass *) hstmt;
 
4505
 
 
4506
        mylog("%s: fConcurrency=%d crowKeyset=%d crowRowset=%d\n",
 
4507
                  func, fConcurrency, crowKeyset, crowRowset);
 
4508
        SC_set_error(stmt, STMT_NOT_IMPLEMENTED_ERROR, "SetScroll option not implemeted", func);
 
4509
 
 
4510
        return SQL_ERROR;
 
4511
}
 
4512
 
 
4513
 
 
4514
/*      Set the cursor name on a statement handle */
 
4515
RETCODE         SQL_API
 
4516
PGAPI_SetCursorName(
 
4517
                                HSTMT hstmt,
 
4518
                                const SQLCHAR FAR * szCursor,
 
4519
                                SQLSMALLINT cbCursor)
 
4520
{
 
4521
        CSTR func = "PGAPI_SetCursorName";
 
4522
        StatementClass *stmt = (StatementClass *) hstmt;
 
4523
 
 
4524
        mylog("%s: hstmt=%p, szCursor=%p, cbCursorMax=%d\n", func, hstmt, szCursor, cbCursor);
 
4525
 
 
4526
        if (!stmt)
 
4527
        {
 
4528
                SC_log_error(func, NULL_STRING, NULL);
 
4529
                return SQL_INVALID_HANDLE;
 
4530
        }
 
4531
 
 
4532
        SET_NAME(stmt->cursor_name, make_string(szCursor, cbCursor, NULL, 0));
 
4533
        return SQL_SUCCESS;
 
4534
}
 
4535
 
 
4536
 
 
4537
/*      Return the cursor name for a statement handle */
 
4538
RETCODE         SQL_API
 
4539
PGAPI_GetCursorName(
 
4540
                                        HSTMT hstmt,
 
4541
                                        SQLCHAR FAR * szCursor,
 
4542
                                        SQLSMALLINT cbCursorMax,
 
4543
                                        SQLSMALLINT FAR * pcbCursor)
 
4544
{
 
4545
        CSTR func = "PGAPI_GetCursorName";
 
4546
        StatementClass *stmt = (StatementClass *) hstmt;
 
4547
        size_t          len = 0;
 
4548
        RETCODE         result;
 
4549
 
 
4550
        mylog("%s: hstmt=%p, szCursor=%p, cbCursorMax=%d, pcbCursor=%p\n", func, hstmt, szCursor, cbCursorMax, pcbCursor);
 
4551
 
 
4552
        if (!stmt)
 
4553
        {
 
4554
                SC_log_error(func, NULL_STRING, NULL);
 
4555
                return SQL_INVALID_HANDLE;
 
4556
        }
 
4557
        result = SQL_SUCCESS;
 
4558
        len = strlen(SC_cursor_name(stmt));
 
4559
 
 
4560
        if (szCursor)
 
4561
        {
 
4562
                strncpy_null(szCursor, SC_cursor_name(stmt), cbCursorMax);
 
4563
 
 
4564
                if (len >= cbCursorMax)
 
4565
                {
 
4566
                        result = SQL_SUCCESS_WITH_INFO;
 
4567
                        SC_set_error(stmt, STMT_TRUNCATED, "The buffer was too small for the GetCursorName.", func);
 
4568
                }
 
4569
        }
 
4570
 
 
4571
        if (pcbCursor)
 
4572
                *pcbCursor = (SQLSMALLINT) len;
 
4573
 
 
4574
        /*
 
4575
         * Because this function causes no db-access, there's
 
4576
         * no need to call DiscardStatementSvp()
 
4577
         */
 
4578
 
 
4579
        return result;
 
4580
}