LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLExecDirect.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 32 67 47.8 %
Date: 2021-10-13 02:24:04 Functions: 2 2 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             :  * SQLExecDirect()
      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             : static SQLRETURN
      33           4 : ODBCExecDirect(ODBCStmt *stmt, const SQLCHAR *StatementText, SQLINTEGER TextLength)
      34             : {
      35             :         char *query;
      36             :         const char *err;
      37             :         MapiMsg ret;
      38             :         MapiHdl hdl;
      39             : 
      40           4 :         hdl = stmt->hdl;
      41             : 
      42           4 :         if (stmt->State >= EXECUTED1 ||
      43           1 :             (stmt->State == EXECUTED0 && mapi_more_results(hdl))) {
      44             :                 /* Invalid cursor state */
      45           0 :                 addStmtError(stmt, "24000", NULL, 0);
      46           0 :                 return SQL_ERROR;
      47             :         }
      48             : 
      49           4 :         if (stmt->qtimeout != stmt->Dbc->qtimeout) {
      50             :                 char buf[48];
      51           0 :                 snprintf(buf, sizeof(buf), "call sys.settimeout(%" PRIu64 ")",
      52             :                          (uint64_t) stmt->qtimeout);
      53           0 :                 if (mapi_query_handle(hdl, buf) == MOK)
      54           0 :                         stmt->Dbc->qtimeout = stmt->qtimeout;
      55             :         }
      56             : 
      57           4 :         query = ODBCTranslateSQL(stmt->Dbc, StatementText, (size_t) TextLength,
      58             :                                  stmt->noScan);
      59           4 :         if (query == NULL) {
      60             :                 /* Memory allocation error */
      61           0 :                 addStmtError(stmt, "HY001", NULL, 0);
      62           0 :                 return SQL_ERROR;
      63             :         }
      64             : 
      65           4 :         ODBCResetStmt(stmt);
      66             : 
      67             : #ifdef ODBCDEBUG
      68           4 :         ODBCLOG("SQLExecDirect: \"%s\"\n", query);
      69             : #endif
      70             : 
      71           4 :         if (stmt->next == NULL &&
      72           3 :             stmt->Dbc->FirstStmt == stmt &&
      73           3 :             stmt->cursorType == SQL_CURSOR_FORWARD_ONLY) {
      74             :                 /* we're the only Stmt handle, and we're only going forward */
      75           3 :                 if (stmt->Dbc->cachelimit != 10000)
      76           2 :                         mapi_cache_limit(stmt->Dbc->mid, 10000);
      77           3 :                 stmt->Dbc->cachelimit = 10000;
      78             :         } else {
      79           1 :                 if (stmt->Dbc->cachelimit != 100)
      80           1 :                         mapi_cache_limit(stmt->Dbc->mid, 100);
      81           1 :                 stmt->Dbc->cachelimit = 100;
      82             :         }
      83           4 :         ret = mapi_query_handle(hdl, query);
      84           4 :         free(query);
      85           4 :         switch (ret) {
      86             :         case MOK:
      87             :                 break;
      88           0 :         case MTIMEOUT:
      89             :                 /* Timeout expired / Communication link failure */
      90           0 :                 addStmtError(stmt, stmt->Dbc->sql_attr_connection_timeout ? "HYT00" : "08S01", mapi_error_str(stmt->Dbc->mid), 0);
      91           0 :                 return SQL_ERROR;
      92           0 :         default:
      93           0 :                 err = mapi_result_error(hdl);
      94           0 :                 if (err == NULL)
      95           0 :                         err = mapi_error_str(stmt->Dbc->mid);
      96           0 :                 if (err != NULL) {
      97           0 :                         const char *e = mapi_result_errorcode(hdl);
      98             : 
      99           0 :                         if (e) {
     100           0 :                                 addStmtError(stmt, e, err, 0);
     101           0 :                                 return SQL_ERROR;
     102             :                         }
     103             :                 }
     104             :                 /* General error */
     105           0 :                 addStmtError(stmt, "HY000", err, 0);
     106           0 :                 return SQL_ERROR;
     107             :         }
     108             : 
     109             :         /* now get the result data and store it to our internal data
     110             :          * structure */
     111             : 
     112           4 :         return ODBCInitResult(stmt);
     113             : }
     114             : 
     115             : SQLRETURN
     116           4 : MNDBExecDirect(ODBCStmt *stmt,
     117             :                const SQLCHAR *StatementText,
     118             :                SQLINTEGER TextLength)
     119             : {
     120             :         SQLRETURN ret;
     121             :         SQLINTEGER i;
     122             : 
     123             :         /* check input parameter */
     124           4 :         if (StatementText == NULL) {
     125             :                 /* Invalid use of null pointer */
     126           0 :                 addStmtError(stmt, "HY009", NULL, 0);
     127           0 :                 return SQL_ERROR;
     128             :         }
     129             : 
     130           4 :         fixODBCstring(StatementText, TextLength, SQLINTEGER,
     131             :                       addStmtError, stmt, return SQL_ERROR);
     132        6156 :         for (i = 0; i < TextLength; i++)
     133             :                 /* TODO FIX: only when the statement starts with PREPARE the
     134             :                    questions marks have a special meaning */
     135        6152 :                 if (StatementText[i] == '?') {
     136             :                         /* query may have parameters, take the long route */
     137           0 :                         ret = MNDBPrepare(stmt, StatementText, TextLength);
     138           0 :                         if (ret == SQL_SUCCESS)
     139           0 :                                 ret = MNDBExecute(stmt);
     140           0 :                         return ret;
     141             :                 }
     142             : 
     143             :         /* no parameters, take the direct route */
     144           4 :         return ODBCExecDirect(stmt, StatementText, TextLength);
     145             : }
     146             : 
     147             : SQLRETURN SQL_API
     148             : SQLExecDirect(SQLHSTMT StatementHandle,
     149             :               SQLCHAR *StatementText,
     150             :               SQLINTEGER TextLength)
     151             : {
     152             : #ifdef ODBCDEBUG
     153           2 :         ODBCLOG("SQLExecDirect %p\n", StatementHandle);
     154             : #endif
     155             : 
     156           2 :         if (!isValidStmt((ODBCStmt *) StatementHandle))
     157             :                 return SQL_INVALID_HANDLE;
     158             : 
     159           2 :         clearStmtErrors((ODBCStmt *) StatementHandle);
     160             : 
     161           2 :         return MNDBExecDirect((ODBCStmt *) StatementHandle,
     162             :                               StatementText,
     163             :                               TextLength);
     164             : }
     165             : 
     166             : SQLRETURN SQL_API
     167             : SQLExecDirectA(SQLHSTMT StatementHandle,
     168             :                SQLCHAR *StatementText,
     169             :                SQLINTEGER TextLength)
     170             : {
     171           0 :         return SQLExecDirect(StatementHandle, StatementText, TextLength);
     172             : }
     173             : 
     174             : SQLRETURN SQL_API
     175             : SQLExecDirectW(SQLHSTMT StatementHandle,
     176             :                SQLWCHAR *StatementText,
     177             :                SQLINTEGER TextLength)
     178             : {
     179             :         ODBCStmt *stmt = (ODBCStmt *) StatementHandle;
     180             :         SQLRETURN rc;
     181             :         SQLCHAR *sql;
     182             : 
     183             : #ifdef ODBCDEBUG
     184           0 :         ODBCLOG("SQLExecDirectW %p\n", StatementHandle);
     185             : #endif
     186             : 
     187           0 :         if (!isValidStmt(stmt))
     188             :                  return SQL_INVALID_HANDLE;
     189             : 
     190           0 :         clearStmtErrors(stmt);
     191             : 
     192           0 :         fixWcharIn(StatementText, TextLength, SQLCHAR, sql,
     193             :                    addStmtError, stmt, return SQL_ERROR);
     194             : 
     195           0 :         rc = MNDBExecDirect((ODBCStmt *) StatementHandle, sql, SQL_NTS);
     196             : 
     197           0 :         if (sql)
     198           0 :                 free(sql);
     199             : 
     200             :         return rc;
     201             : }

Generated by: LCOV version 1.14