LCOV - code coverage report
Current view: top level - src/dblib/unittests - null2.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 86 106 81.1 %
Date: 2024-03-23 09:12:27 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /* 
       2             :  * Purpose: Test NULL behavior using dbbind
       3             :  */
       4             : 
       5             : #include "common.h"
       6             : 
       7             : #if HAVE_UNISTD_H
       8             : #include <unistd.h>
       9             : #endif /* HAVE_UNISTD_H */
      10             : 
      11             : static DBPROCESS *dbproc = NULL;
      12             : static int failed = 0;
      13             : 
      14             : static int
      15           6 : ignore_msg_handler(DBPROCESS * dbproc, DBINT msgno, int state, int severity, char *text, char *server, char *proc, int line)
      16             : {
      17           6 :         return 0;
      18             : }
      19             : 
      20             : static int
      21           6 : ignore_err_handler(DBPROCESS * dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
      22             : {
      23           6 :         return INT_CANCEL;
      24             : }
      25             : 
      26             : static void
      27         282 : query(const char *query)
      28             : {
      29         282 :         printf("query: %s\n", query);
      30         282 :         dbcmd(dbproc, (char *) query);
      31         282 :         dbsqlexec(dbproc);
      32         282 :         while (dbresults(dbproc) == SUCCEED) {
      33             :                 /* nop */
      34             :         }
      35         282 : }
      36             : 
      37             : static int use_nullbind = 0;
      38             : 
      39             : static void
      40         368 : test0(int n,  const char * expected)
      41             : {
      42             :         DBINT ind, expected_ind;
      43             :         char text_buf[16];
      44             : 
      45         368 :         dbfcmd(dbproc, "select c from #null where n = %d", n);
      46             : 
      47         368 :         dbsqlexec(dbproc);
      48             : 
      49         368 :         if (dbresults(dbproc) != SUCCEED) {
      50           0 :                 fprintf(stderr, "Was expecting a row.\n");
      51           0 :                 failed = 1;
      52           0 :                 dbcancel(dbproc);
      53           0 :                 return;
      54             :         }
      55             : 
      56         368 :         dbbind(dbproc, 1, NTBSTRINGBIND, 0, (BYTE *)text_buf);
      57         368 :         if (use_nullbind)
      58         184 :                 dbnullbind(dbproc, 1, &ind);
      59             : 
      60         368 :         memset(text_buf, 'a', sizeof(text_buf));
      61         368 :         ind = -5;
      62             : 
      63         368 :         if (dbnextrow(dbproc) != REG_ROW) {
      64           0 :                 fprintf(stderr, "Was expecting a row.\n");
      65           0 :                 failed = 1;
      66           0 :                 dbcancel(dbproc);
      67           0 :                 return;
      68             :         }
      69             : 
      70         368 :         text_buf[sizeof(text_buf) - 1] = 0;
      71         368 :         printf("ind %d text_buf -%s-\n", (int) ind, text_buf);
      72             : 
      73         368 :         expected_ind = 0;
      74         368 :         if (strcmp(expected, "aaaaaaaaaaaaaaa") == 0)
      75          46 :                 expected_ind = -1;
      76             : 
      77             :         /* do not check indicator if not bound */
      78         368 :         if (!use_nullbind)
      79         184 :                 ind = expected_ind;
      80         368 :         if (ind != expected_ind || strcmp(expected, text_buf) != 0) {
      81           0 :                 fprintf(stderr, "expected_ind %d expected -%s-\n", (int) expected_ind, expected);
      82           0 :                 failed = 1;
      83           0 :                 dbcancel(dbproc);
      84           0 :                 return;
      85             :         }
      86             : 
      87         368 :         if (dbnextrow(dbproc) != NO_MORE_ROWS) {
      88           0 :                 fprintf(stderr, "Only one row expected\n");
      89           0 :                 dbcancel(dbproc);
      90           0 :                 failed = 1;
      91             :         }
      92             : 
      93         368 :         while (dbresults(dbproc) == SUCCEED) {
      94             :                 /* nop */
      95             :         }
      96             : }
      97             : 
      98             : 
      99             : static void
     100          52 : test(const char *type, int give_err)
     101             : {
     102             :         RETCODE ret;
     103             : 
     104          52 :         query("if object_id('#null') is not NULL drop table #null");
     105             : 
     106          52 :         dberrhandle(ignore_err_handler);
     107          52 :         dbmsghandle(ignore_msg_handler);
     108             : 
     109          52 :         printf("create table #null (n int, c %s NULL)\n", type);
     110          52 :         dbfcmd(dbproc, "create table #null (n int, c %s NULL)", type);
     111          52 :         dbsqlexec(dbproc);
     112             : 
     113          52 :         ret = dbresults(dbproc);
     114             : 
     115          52 :         dberrhandle(syb_err_handler);
     116          52 :         dbmsghandle(syb_msg_handler);
     117             : 
     118          52 :         if (ret != SUCCEED) {
     119           6 :                 dbcancel(dbproc);
     120           6 :                 if (!give_err)
     121             :                         return;
     122           0 :                 printf("Was expecting a result set.\n");
     123           0 :                 failed = 1;
     124           0 :                 return;
     125             :         }
     126             : 
     127          46 :         query("insert into #null values(1, '')");
     128          46 :         query("insert into #null values(2, NULL)");
     129          46 :         query("insert into #null values(3, ' ')");
     130          46 :         query("insert into #null values(4, 'foo')");
     131             : 
     132          46 :         use_nullbind = 1;
     133          46 :         test0(1, "");
     134          46 :         test0(2, "aaaaaaaaaaaaaaa");
     135          46 :         test0(3, "");
     136          46 :         test0(4, "foo");
     137             : 
     138          46 :         use_nullbind = 0;
     139          46 :         test0(1, "");
     140          46 :         test0(2, "");
     141          46 :         test0(3, "");
     142          46 :         test0(4, "foo");
     143             : 
     144          46 :         query("drop table #null");
     145             : }
     146             : 
     147             : int
     148           8 : main(int argc, char **argv)
     149             : {
     150             :         LOGINREC *login;
     151             : 
     152           8 :         read_login_info(argc, argv);
     153             : 
     154           8 :         printf("Starting %s\n", argv[0]);
     155             : 
     156           8 :         dbinit();
     157             : 
     158           8 :         dberrhandle(syb_err_handler);
     159           8 :         dbmsghandle(syb_msg_handler);
     160             : 
     161           8 :         printf("About to logon\n");
     162             : 
     163           8 :         login = dblogin();
     164           8 :         DBSETLPWD(login, PASSWORD);
     165           8 :         DBSETLUSER(login, USER);
     166           8 :         DBSETLAPP(login, "thread");
     167             : 
     168           8 :         printf("About to open \"%s\"\n", SERVER);
     169             : 
     170           8 :         dbproc = dbopen(login, SERVER);
     171           8 :         if (!dbproc) {
     172           0 :                 fprintf(stderr, "Unable to connect to %s\n", SERVER);
     173           0 :                 return 1;
     174             :         }
     175             : 
     176           8 :         dbloginfree(login);
     177             : 
     178           8 :         if (strlen(DATABASE))
     179           8 :                 dbuse(dbproc, DATABASE);
     180             : 
     181           8 :         test("VARCHAR(10)", 1);
     182           8 :         test("CHAR(10)", 1);
     183           8 :         test("TEXT", 1);
     184             : 
     185           8 :         test("NVARCHAR(10)", 0);
     186             : #ifndef DBNTWIN32
     187           8 :         if (dbtds(dbproc) >= DBTDS_7_0)
     188           6 :                 test("NTEXT", 0);
     189             : #endif
     190             : 
     191           8 :         test("VARCHAR(MAX)", 0);
     192             : #ifndef DBNTWIN32
     193           8 :         if (dbtds(dbproc) >= DBTDS_7_0)
     194           6 :                 test("NVARCHAR(MAX)", 0);
     195             : #endif
     196             : 
     197           8 :         dbexit();
     198             : 
     199           8 :         return failed ? 1 : 0;
     200             : }
     201             : 

Generated by: LCOV version 1.13