LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLFetch.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 41 80 51.2 %
Date: 2021-10-13 02:24:04 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * This code was created by Peter Harvey (mostly during Christmas 98/99).
      11             :  * This code is LGPL. Please ensure that this message remains in future
      12             :  * distributions and uses of this code (thats about all I get out of it).
      13             :  * - Peter Harvey pharvey@codebydesign.com
      14             :  *
      15             :  * This file has been modified for the MonetDB project.  See the file
      16             :  * Copyright in this directory for more information.
      17             :  */
      18             : 
      19             : /**********************************************************************
      20             :  * SQLFetch()
      21             :  * CLI Compliance: ISO 92
      22             :  *
      23             :  * Author: Martin van Dinther, Sjoerd Mullender
      24             :  * Date  : 30 aug 2002
      25             :  *
      26             :  **********************************************************************/
      27             : 
      28             : #include "ODBCGlobal.h"
      29             : #include "ODBCStmt.h"
      30             : #include "ODBCUtil.h"
      31             : 
      32             : SQLRETURN
      33        2007 : MNDBFetch(ODBCStmt *stmt, SQLUSMALLINT *RowStatusArray)
      34             : {
      35             :         ODBCDesc *ard, *ird;
      36             :         ODBCDescRec *rec;
      37             :         int i;
      38             :         SQLULEN row;
      39             :         SQLLEN offset;
      40             : 
      41             :         /* stmt->startRow is the (0 based) index of the first row we
      42             :          * stmt->need to fetch */
      43             : 
      44        2007 :         ard = stmt->ApplRowDescr;
      45        2007 :         ird = stmt->ImplRowDescr;
      46             : 
      47        2007 :         stmt->retrieved = 0;
      48        2007 :         stmt->currentCol = 0;
      49             : 
      50        2007 :         stmt->rowSetSize = 0;
      51        2007 :         stmt->currentRow = stmt->startRow + 1;
      52        2007 :         if (mapi_seek_row(stmt->hdl, stmt->startRow, MAPI_SEEK_SET) != MOK) {
      53             :                 /* Row value out of range */
      54           0 :                 addStmtError(stmt, "HY107", mapi_error_str(stmt->Dbc->mid), 0);
      55           0 :                 return SQL_ERROR;
      56             :         }
      57             : 
      58        2007 :         stmt->State = FETCHED;
      59             : 
      60        2007 :         if (stmt->retrieveData == SQL_RD_OFF) {
      61             :                 /* don't really retrieve the data, just do as if,
      62             :                    updating the SQL_DESC_ARRAY_STATUS_PTR */
      63           0 :                 stmt->rowSetSize = ard->sql_desc_array_size;
      64             : 
      65           0 :                 if (stmt->startRow + stmt->rowSetSize > (SQLLEN) stmt->rowcount)
      66           0 :                         stmt->rowSetSize = stmt->rowcount - stmt->startRow;
      67             : 
      68           0 :                 if (stmt->rowSetSize <= 0) {
      69           0 :                         stmt->rowSetSize = 0;
      70           0 :                         return SQL_NO_DATA;
      71             :                 }
      72           0 :                 if (RowStatusArray) {
      73           0 :                         for (row = 0; (SQLLEN) row < stmt->rowSetSize; row++) {
      74           0 :                                 WriteValue(RowStatusArray, SQL_ROW_SUCCESS);
      75           0 :                                 RowStatusArray++;
      76             :                         }
      77           0 :                         for (; row < ard->sql_desc_array_size; row++) {
      78           0 :                                 WriteValue(RowStatusArray, SQL_ROW_NOROW);
      79           0 :                                 RowStatusArray++;
      80             :                         }
      81             :                 }
      82           0 :                 return SQL_SUCCESS;
      83             :         }
      84             : 
      85        2007 :         if (ard->sql_desc_bind_offset_ptr)
      86           0 :                 offset = *ard->sql_desc_bind_offset_ptr;
      87             :         else
      88             :                 offset = 0;
      89        4012 :         for (row = 0; row < ard->sql_desc_array_size; row++) {
      90        2007 :                 if (mapi_fetch_row(stmt->hdl) == 0) {
      91           2 :                         switch (mapi_error(stmt->Dbc->mid)) {
      92           2 :                         case MOK:
      93           2 :                                 if (row == 0)
      94             :                                         return SQL_NO_DATA;
      95             :                                 break;
      96           0 :                         case MTIMEOUT:
      97           0 :                                 if (RowStatusArray)
      98           0 :                                         WriteValue(RowStatusArray, SQL_ROW_ERROR);
      99             :                                 /* Timeout expired / Communication
     100             :                                  * link failure */
     101           0 :                                 addStmtError(stmt, stmt->Dbc->sql_attr_connection_timeout ? "HYT00" : "08S01", mapi_error_str(stmt->Dbc->mid), 0);
     102           0 :                                 return SQL_ERROR;
     103           0 :                         default:
     104           0 :                                 if (RowStatusArray)
     105           0 :                                         WriteValue(RowStatusArray, SQL_ROW_ERROR);
     106             :                                 /* General error */
     107           0 :                                 addStmtError(stmt, "HY000", mapi_error_str(stmt->Dbc->mid), 0);
     108           0 :                                 return SQL_ERROR;
     109             :                         }
     110             :                         break;
     111             :                 }
     112        2005 :                 if (RowStatusArray)
     113           0 :                         WriteValue(RowStatusArray, SQL_ROW_SUCCESS);
     114             : 
     115        2005 :                 stmt->rowSetSize++;
     116             : 
     117       12095 :                 for (i = 1; i <= ird->sql_desc_count; i++)
     118       10090 :                         ird->descRec[i].already_returned = -1;
     119             : 
     120       12090 :                 for (i = 1; i <= ard->sql_desc_count; i++) {
     121       10085 :                         rec = &ard->descRec[i];
     122       10085 :                         if (rec->sql_desc_data_ptr == NULL)
     123          75 :                                 continue;
     124       10010 :                         stmt->retrieved = 0;
     125       10010 :                         if (ODBCFetch(stmt, i,
     126       10010 :                                       rec->sql_desc_concise_type,
     127             :                                       rec->sql_desc_data_ptr,
     128             :                                       rec->sql_desc_octet_length,
     129             :                                       rec->sql_desc_octet_length_ptr,
     130             :                                       rec->sql_desc_indicator_ptr,
     131       10010 :                                       rec->sql_desc_precision,
     132       10010 :                                       rec->sql_desc_scale,
     133             :                                       rec->sql_desc_datetime_interval_precision,
     134             :                                       offset, row) == SQL_ERROR) {
     135           0 :                                 if (RowStatusArray)
     136           0 :                                         WriteValue(RowStatusArray, SQL_ROW_SUCCESS_WITH_INFO);
     137             :                         }
     138             :                 }
     139        2005 :                 if (RowStatusArray)
     140           0 :                         RowStatusArray++;
     141             :         }
     142        2005 :         if (ird->sql_desc_rows_processed_ptr)
     143           0 :                 *ird->sql_desc_rows_processed_ptr = (SQLULEN) stmt->rowSetSize;
     144             : 
     145        2005 :         if (RowStatusArray)
     146           0 :                 while (row++ < ard->sql_desc_array_size) {
     147           0 :                         WriteValue(RowStatusArray, SQL_ROW_NOROW);
     148           0 :                         RowStatusArray++;
     149             :                 }
     150             : 
     151        2005 :         return stmt->Error ? SQL_SUCCESS_WITH_INFO : SQL_SUCCESS;
     152             : }
     153             : 
     154             : SQLRETURN SQL_API
     155             : SQLFetch(SQLHSTMT StatementHandle)
     156             : {
     157             :         ODBCStmt *stmt = (ODBCStmt *) StatementHandle;
     158             : 
     159             : #ifdef ODBCDEBUG
     160        1006 :         ODBCLOG("SQLFetch %p\n", StatementHandle);
     161             : #endif
     162             : 
     163        1006 :         if (!isValidStmt(stmt))
     164             :                  return SQL_INVALID_HANDLE;
     165             : 
     166        1006 :         clearStmtErrors(stmt);
     167             : 
     168        1006 :         assert(stmt->hdl);
     169             : 
     170             :         /* check statement cursor state, query should be executed */
     171        1006 :         if (stmt->State < EXECUTED0 || stmt->State == EXTENDEDFETCHED) {
     172             :                 /* Function sequence error */
     173           0 :                 addStmtError(stmt, "HY010", NULL, 0);
     174           0 :                 return SQL_ERROR;
     175             :         }
     176        1006 :         if (stmt->State == EXECUTED0) {
     177             :                 /* Invalid cursor state */
     178           0 :                 addStmtError(stmt, "24000", NULL, 0);
     179           0 :                 return SQL_ERROR;
     180             :         }
     181             : 
     182        1006 :         stmt->startRow += stmt->rowSetSize;
     183             : 
     184        1006 :         return MNDBFetch(stmt, stmt->ImplRowDescr->sql_desc_array_status_ptr);
     185             : }

Generated by: LCOV version 1.14