LCOV - code coverage report
Current view: top level - clients/odbc/tests - ODBCtester.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 106 150 70.7 %
Date: 2024-04-26 00:35:57 Functions: 5 6 83.3 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : #ifdef _MSC_VER
      14             : #include <WTypes.h>
      15             : #endif
      16             : #include <stdio.h>
      17             : #include <stdlib.h>
      18             : #include <stdint.h>
      19             : #include <string.h>
      20             : #include <sql.h>
      21             : #include <sqlext.h>
      22             : #include <ctype.h>
      23             : #include <inttypes.h>
      24             : #include <wchar.h>
      25             : 
      26             : static void
      27           0 : prerr(SQLSMALLINT tpe, SQLHANDLE hnd, const char *func, const char *pref)
      28             : {
      29           0 :         SQLCHAR state[6];
      30           0 :         SQLINTEGER errnr;
      31           0 :         SQLCHAR msg[256];
      32           0 :         SQLSMALLINT msglen;
      33           0 :         SQLRETURN ret;
      34             : 
      35           0 :         ret = SQLGetDiagRec(tpe, hnd, 1, state, &errnr, msg, sizeof(msg), &msglen);
      36           0 :         switch (ret) {
      37           0 :         case SQL_SUCCESS_WITH_INFO:
      38           0 :                 if (msglen >= (signed int) sizeof(msg))
      39           0 :                         fprintf(stderr, "(message truncated)\n");
      40             :                 /* fall through */
      41             :         case SQL_SUCCESS:
      42           0 :                 fprintf(stderr, "%s: %s: SQLstate %s, Errnr %d, Message %s\n", func, pref, (char*)state, (int)errnr, (char*)msg);
      43           0 :                 break;
      44           0 :         case SQL_INVALID_HANDLE:
      45           0 :                 fprintf(stderr, "%s: %s, invalid handle passed to error function\n", func, pref);
      46           0 :                 break;
      47           0 :         case SQL_ERROR:
      48           0 :                 fprintf(stderr, "%s: %s, unexpected error from SQLGetDiagRec\n", func, pref);
      49           0 :                 break;
      50             :         case SQL_NO_DATA:
      51             :                 break;
      52           0 :         default:
      53           0 :                 fprintf(stderr, "%s: %s, weird return value from SQLGetDiagRec\n", func, pref);
      54           0 :                 break;
      55             :         }
      56           0 : }
      57             : 
      58             : static void
      59          21 : check(SQLRETURN ret, SQLSMALLINT tpe, SQLHANDLE hnd, const char *func)
      60             : {
      61          21 :         switch (ret) {
      62             :         case SQL_SUCCESS:
      63             :                 break;
      64           0 :         case SQL_SUCCESS_WITH_INFO:
      65           0 :                 prerr(tpe, hnd, func, "Info");
      66           0 :                 break;
      67           0 :         case SQL_ERROR:
      68           0 :                 prerr(tpe, hnd, func, "Error");
      69           0 :                 break;
      70             :         case SQL_NO_DATA:
      71             :                 break;
      72           0 :         case SQL_INVALID_HANDLE:
      73           0 :                 fprintf(stderr, "%s: Error: invalid handle\n", func);
      74           0 :                 break;
      75           0 :         default:
      76           0 :                 fprintf(stderr, "%s: Unexpected return value\n", func);
      77           0 :                 break;
      78             :         }
      79          21 : }
      80             : 
      81             : static size_t
      82           3 : retrieveDiagMsg(SQLHANDLE stmt, char * outp, size_t outp_len)
      83             : {
      84           3 :         SQLCHAR state[6];
      85           3 :         SQLINTEGER errnr = 0;
      86           3 :         char msg[256];
      87           3 :         SQLSMALLINT msglen = 0;
      88           3 :         SQLRETURN ret = SQLGetDiagRec(SQL_HANDLE_STMT, stmt, 1, state, &errnr, (SQLCHAR *) msg, sizeof(msg), &msglen);
      89           3 :         if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
      90             :                 /* The message layout is: "[MonetDB][ODBC Driver 11.46.0][MonetDB-Test]error/warning text".
      91             :                    The ODBC driver version numbers changes in time. Overwrite it to get a stable output */
      92           3 :                 if (strncmp(msg, "[MonetDB][ODBC Driver 11.", 25) == 0) {
      93           3 :                         return snprintf(outp, outp_len, "SQLstate %s, Errnr %d, Message [MonetDB][ODBC Driver 11.##.#]%s\n", (char*)state, (int)errnr, strchr(msg + 25, ']') + 1);
      94             :                 }
      95           0 :                 return snprintf(outp, outp_len, "SQLstate %s, Errnr %d, Message %s\n", (char*)state, (int)errnr, (char*)msg);
      96             :         }
      97             :         return 0;
      98             : }
      99             : 
     100             : static void
     101           2 : compareResult(char * testname, char * testresult, char * expected)
     102             : {
     103           2 :         if (strcmp(expected, testresult) != 0) {
     104           0 :                 fprintf(stderr, "Testing %s\nGotten:\n%s\nExpected:\n%s\n", testname, testresult, expected);
     105             :         }
     106           2 : }
     107             : 
     108             : #define LLFMT                   "%" PRId64
     109             : 
     110             : static SQLRETURN
     111           2 : testGetDataTruncatedString(SQLHANDLE stmt, SWORD ctype)
     112             : {
     113           2 :         SQLRETURN ret;
     114           2 :         SQLLEN RowCount = 0;
     115           2 :         SWORD NumResultCols = 0;
     116             : 
     117           2 :         size_t outp_len = 800;
     118           2 :         char * outp = malloc(outp_len);
     119           2 :         size_t pos = 0;
     120             : 
     121             : //      char * sql = "select tag as \"qtag\", sessionid as \"sessionid\", username as \"username\", started as \"started\", status as \"status\", query as \"query\", finished as \"finished\", maxworkers as \"maxworkers\", footprint as \"footprint\" from sys.queue() where query like '/*e6dbd251960c49bbbeff9784fa70c86d*/%';";
     122           2 :         char * sql = "select cast('12345678901234567890 abcdefghijklmnopqrstuvwxyz' as clob) as val;";
     123           2 :         ret = SQLExecDirect(stmt, (SQLCHAR *) sql, SQL_NTS);
     124           2 :         pos += snprintf(outp + pos, outp_len - pos, "SQLExecDirect\n");
     125           2 :         check(ret, SQL_HANDLE_STMT, stmt, "SQLExecDirect");
     126             : 
     127           2 :         ret = SQLRowCount(stmt, &RowCount);
     128           2 :         pos += snprintf(outp + pos, outp_len - pos, "SQLRowCount is " LLFMT "\n", (int64_t) RowCount);
     129           2 :         check(ret, SQL_HANDLE_STMT, stmt, "SQLRowCount");
     130             : 
     131           2 :         ret = SQLNumResultCols(stmt, &NumResultCols);
     132           2 :         pos += snprintf(outp + pos, outp_len - pos, "SQLNumResultCols is %d\n", NumResultCols);
     133           2 :         check(ret, SQL_HANDLE_STMT, stmt, "SQLNumResultCols");
     134             : 
     135           2 :         ret = SQLFetch(stmt);
     136           2 :         pos += snprintf(outp + pos, outp_len - pos, "SQLFetch\n");
     137           2 :         check(ret, SQL_HANDLE_STMT, stmt, "SQLFetch");
     138             : 
     139           4 :         for (SWORD col = 1; col <= NumResultCols; col++) {
     140           2 :                 char buf[99];
     141           2 :                 wchar_t wbuf[99];
     142           2 :                 char buf2[99];
     143           2 :                 wchar_t wbuf2[99];
     144           2 :                 SQLLEN vallen = 0;
     145           2 :                 SQLLEN NumAttr = 0;
     146           2 :                 char * ctype_str = (ctype == SQL_C_CHAR ? "SQL_C_CHAR" : ctype == SQL_C_WCHAR ? "SQL_C_WCHAR" : "NYI");
     147             : 
     148             :                 /* retrieve query result column metadata */
     149           2 :                 ret = SQLColAttribute(stmt, (UWORD)col, SQL_DESC_CONCISE_TYPE, (PTR)&buf, (SQLLEN)20, NULL, &NumAttr);
     150           2 :                 pos += snprintf(outp + pos, outp_len - pos, "SQLColAttribute(%d, SQL_DESC_CONCISE_TYPE) returns %d, NumAttr " LLFMT "\n", col, ret, (int64_t) NumAttr);
     151           2 :                 ret = SQLColAttribute(stmt, (UWORD)col, SQL_DESC_LENGTH, (PTR)&buf, (SQLLEN)20, NULL, &NumAttr);
     152           2 :                 pos += snprintf(outp + pos, outp_len - pos, "SQLColAttribute(%d, SQL_DESC_LENGTH) returns %d, NumAttr " LLFMT "\n", col, ret, (int64_t) NumAttr);
     153           2 :                 ret = SQLColAttribute(stmt, (UWORD)col, SQL_DESC_DISPLAY_SIZE, (PTR)&buf, (SQLLEN)20, NULL, &NumAttr);
     154           2 :                 pos += snprintf(outp + pos, outp_len - pos, "SQLColAttribute(%d, SQL_DESC_DISPLAY_SIZE) returns %d, NumAttr " LLFMT "\n", col, ret, (int64_t) NumAttr);
     155             : 
     156             :                 /* test SQLGetData(SQL_C_(W)CHAR, 20) with a restricted buffer size (20) for the queried string value (47) */
     157           3 :                 ret = SQLGetData(stmt, (UWORD)col, (SWORD)ctype, ctype == SQL_C_WCHAR ? (PTR)&wbuf : (PTR)&buf, (SQLLEN)20, &vallen);
     158           2 :                 if (ctype == SQL_C_WCHAR) {
     159             :                         /* snprintf does not allow printing wchar strings. convert it to a char string */
     160             :                         /* tried: wcstombs(buf, wbuf, 99); but it doesn't work */
     161             :                         /* workaround: just empty the buffer to get a stable output on all platforms (power8 gives a different output) */
     162           1 :                         buf[0] = 0;
     163             :                 }
     164           2 :                 pos += snprintf(outp + pos, outp_len - pos, "SQLGetData(%d, %s, 20) returns %d, vallen " LLFMT ", buf: '%s'\n", col, ctype_str, ret, (int64_t) vallen, buf);
     165             :                 /* we expect SQL_SUCCESS_WITH_INFO with warning msg set, fetch them */
     166           2 :                 if (ret == SQL_SUCCESS_WITH_INFO) {
     167           2 :                         pos += retrieveDiagMsg(stmt, outp + pos, outp_len - pos);
     168             : 
     169             :                         /* get the next data part of the value (this is how SQLGetData is intended to be used to get large data in chunks) */
     170           3 :                         ret = SQLGetData(stmt, (UWORD)col, (SWORD)ctype, ctype == SQL_C_WCHAR ? (PTR)&wbuf2 : (PTR)&buf2, (SQLLEN)30, &vallen);
     171           2 :                         if (ctype == SQL_C_WCHAR) {
     172             :                                 /* tried: wcstombs(buf2, wbuf2, 99); but it doesn't work */
     173             :                                 /* workaround: just empty the buffer to get a stable output on all platforms (power8 gives a different output) */
     174           1 :                                 buf2[0] = 0;
     175             :                         }
     176           2 :                         pos += snprintf(outp + pos, outp_len - pos, "SQLGetData(%d, %s, 30) returns %d, vallen " LLFMT ", buf: '%s'\n", col, ctype_str, ret, (int64_t) vallen, buf2);
     177           2 :                         if (ret == SQL_SUCCESS_WITH_INFO) {
     178           1 :                                 pos += retrieveDiagMsg(stmt, outp + pos, outp_len - pos);
     179           1 :                                 ret = SQL_SUCCESS;
     180             :                         }
     181             :                 }
     182           2 :                 check(ret, SQL_HANDLE_STMT, stmt, "SQLGetData(col)");
     183             :         }
     184             : 
     185           2 :         if (ctype == SQL_C_CHAR) {
     186           1 :                 compareResult("testGetDataTruncatedString(SQL_C_CHAR)", outp,
     187             :                         "SQLExecDirect\nSQLRowCount is 1\nSQLNumResultCols is 1\nSQLFetch\n"
     188             :                         "SQLColAttribute(1, SQL_DESC_CONCISE_TYPE) returns 0, NumAttr -9\n"   /* -9 = SQL_WVARCHAR */
     189             :                         "SQLColAttribute(1, SQL_DESC_LENGTH) returns 0, NumAttr 47\n"
     190             :                         "SQLColAttribute(1, SQL_DESC_DISPLAY_SIZE) returns 0, NumAttr 47\n"
     191             :                         "SQLGetData(1, SQL_C_CHAR, 20) returns 1, vallen 47, buf: '1234567890123456789'\n"
     192             :                         "SQLstate 01004, Errnr 0, Message [MonetDB][ODBC Driver 11.##.#][MonetDB-Test]String data, right truncated\n"
     193             :                         "SQLGetData(1, SQL_C_CHAR, 30) returns 0, vallen 28, buf: '0 abcdefghijklmnopqrstuvwxyz'\n");
     194             :         } else
     195           1 :         if (ctype == SQL_C_WCHAR) {
     196           1 :                 compareResult("testGetDataTruncatedString(SQL_C_WCHAR)", outp,
     197             :                         "SQLExecDirect\nSQLRowCount is 1\nSQLNumResultCols is 1\nSQLFetch\n"
     198             :                         "SQLColAttribute(1, SQL_DESC_CONCISE_TYPE) returns 0, NumAttr -9\n"   /* -9 = SQL_WVARCHAR */
     199             :                         "SQLColAttribute(1, SQL_DESC_LENGTH) returns 0, NumAttr 47\n"
     200             :                         "SQLColAttribute(1, SQL_DESC_DISPLAY_SIZE) returns 0, NumAttr 47\n"
     201             :                         "SQLGetData(1, SQL_C_WCHAR, 20) returns 1, vallen 94, buf: ''\n"
     202             :                         "SQLstate 01004, Errnr 0, Message [MonetDB][ODBC Driver 11.##.#][MonetDB-Test]String data, right truncated\n"
     203             :                         "SQLGetData(1, SQL_C_WCHAR, 30) returns 1, vallen 76, buf: ''\n"
     204             :                         "SQLstate 01004, Errnr 0, Message [MonetDB][ODBC Driver 11.##.#][MonetDB-Test]String data, right truncated\n");
     205             :         }
     206             : 
     207             :         /* cleanup */
     208           2 :         free(outp);
     209           2 :         return ret;
     210             : }
     211             : 
     212             : int
     213           1 : main(int argc, char **argv)
     214             : {
     215           1 :         SQLRETURN ret;
     216           1 :         SQLHANDLE env;
     217           1 :         SQLHANDLE dbc;
     218           1 :         SQLHANDLE stmt;
     219           1 :         char *dsn = "MonetDB";
     220           1 :         char *user = "monetdb";
     221           1 :         char *pass = "monetdb";
     222             : 
     223           1 :         if (argc > 1)
     224           1 :                 dsn = argv[1];
     225           1 :         if (argc > 2)
     226           0 :                 user = argv[2];
     227           0 :         if (argc > 3)
     228           0 :                 pass = argv[3];
     229           1 :         if (argc > 4 || *dsn == '-') {
     230           0 :                 fprintf(stderr, "Wrong arguments. Usage: %s [datasource [user [password]]]\n", argv[0]);
     231           0 :                 exit(1);
     232             :         }
     233             : 
     234           1 :         ret = SQLAllocHandle(SQL_HANDLE_ENV, NULL, &env);
     235           1 :         if (ret != SQL_SUCCESS) {
     236           0 :                 fprintf(stderr, "Cannot allocate ODBC environment handle!\n");
     237           0 :                 exit(1);
     238             :         }
     239             : 
     240           1 :         ret = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) (uintptr_t) SQL_OV_ODBC3, 0);
     241           1 :         check(ret, SQL_HANDLE_ENV, env, "SQLSetEnvAttr (SQL_ATTR_ODBC_VERSION ODBC3)");
     242             : 
     243           1 :         ret = SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
     244           1 :         check(ret, SQL_HANDLE_ENV, env, "SQLAllocHandle (DBC)");
     245             : 
     246           1 :         ret = SQLConnect(dbc, (SQLCHAR *) dsn, SQL_NTS, (SQLCHAR *) user, SQL_NTS, (SQLCHAR *) pass, SQL_NTS);
     247           1 :         check(ret, SQL_HANDLE_DBC, dbc, "SQLConnect");
     248             : 
     249           1 :         ret = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
     250           1 :         check(ret, SQL_HANDLE_DBC, dbc, "SQLAllocHandle (STMT)");
     251             : 
     252             :         /* run tests */
     253           1 :         ret = testGetDataTruncatedString(stmt, SQL_C_CHAR);
     254           1 :         check(ret, SQL_HANDLE_STMT, stmt, "testGetDataTruncatedString(STMT, SQL_C_CHAR)");
     255             : 
     256           1 :         ret = SQLCloseCursor(stmt);
     257           1 :         check(ret, SQL_HANDLE_STMT, stmt, "SQLCloseCursor");
     258             : 
     259           1 :         ret = testGetDataTruncatedString(stmt, SQL_C_WCHAR);
     260           1 :         check(ret, SQL_HANDLE_STMT, stmt, "testGetDataTruncatedString(STMT, SQL_C_WCHAR)");
     261             : 
     262             :         /* cleanup */
     263           1 :         ret = SQLFreeHandle(SQL_HANDLE_STMT, stmt);
     264           1 :         check(ret, SQL_HANDLE_STMT, stmt, "SQLFreeHandle (STMT)");
     265             : 
     266           1 :         ret = SQLDisconnect(dbc);
     267           1 :         check(ret, SQL_HANDLE_DBC, dbc, "SQLDisconnect");
     268             : 
     269           1 :         ret = SQLFreeHandle(SQL_HANDLE_DBC, dbc);
     270           1 :         check(ret, SQL_HANDLE_DBC, dbc, "SQLFreeHandle (DBC)");
     271             : 
     272           1 :         ret = SQLFreeHandle(SQL_HANDLE_ENV, env);
     273           1 :         check(ret, SQL_HANDLE_ENV, env, "SQLFreeHandle (ENV)");
     274             : 
     275           1 :         return 0;
     276             : }

Generated by: LCOV version 1.14