LCOV - code coverage report
Current view: top level - src/dblib/unittests - t0012.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 79 81 97.5 %
Date: 2025-01-18 12:13:41 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* 
       2             :  * Purpose: Test datetime conversion as well as dbdata() & dbdatlen()
       3             :  * Functions: dbcmd dbdata dbdatecrack dbdatlen dbnextrow dbresults dbsqlexec
       4             :  */
       5             : 
       6             : #include "common.h"
       7             : 
       8             : static int failed = 0;
       9             : static void set_failed(int line)
      10             : {
      11           0 :         failed = 1;
      12           0 :         fprintf(stderr, "Failed check at line %d\n", line);
      13             : }
      14             : #define set_failed() set_failed(__LINE__)
      15             : 
      16             : #ifdef MSDBLIB
      17             : #define dateyear year
      18             : #define datemonth month
      19             : #define datedmonth day
      20             : #define datedyear dayofyear
      21             : #define datedweek weekday
      22             : #define datehour hour
      23             : #define dateminute minute
      24             : #define datesecond second
      25             : #define datemsecond millisecond
      26             : #define datensecond nanosecond
      27             : #endif
      28             : 
      29             : static int
      30           6 : ignore_msg_handler(DBPROCESS * dbproc, DBINT msgno, int state, int severity, char *text, char *server, char *proc, int line)
      31             : {
      32           6 :         return 0;
      33             : }
      34             : 
      35             : int
      36           8 : main(int argc, char *argv[])
      37             : {
      38             :         LOGINREC *login;
      39             :         DBPROCESS *dbproc;
      40             :         char datestring[256];
      41             :         DBDATEREC  dateinfo;
      42             : #ifdef SYBMSDATETIME2
      43             :         DBDATEREC2 dateinfo2;
      44             : #endif
      45             :         DBDATETIME mydatetime;
      46           8 :         int output_count = 0;
      47             : 
      48           8 :         set_malloc_options();
      49             : 
      50           8 :         read_login_info(argc, argv);
      51           8 :         printf("Starting %s\n", argv[0]);
      52           8 :         dbinit();
      53             : 
      54           8 :         dberrhandle(syb_err_handler);
      55           8 :         dbmsghandle(syb_msg_handler);
      56             : 
      57           8 :         printf("About to logon\n");
      58             : 
      59           8 :         login = dblogin();
      60           8 :         DBSETLPWD(login, PASSWORD);
      61           8 :         DBSETLUSER(login, USER);
      62           8 :         DBSETLAPP(login, "t0012");
      63             : 
      64           8 :         dbproc = dbopen(login, SERVER);
      65           8 :         if (strlen(DATABASE)) {
      66           8 :                 dbuse(dbproc, DATABASE);
      67             :         }
      68           8 :         dbloginfree(login);
      69           8 :         printf("After logon\n");
      70             : 
      71           8 :         printf("creating table\n");
      72           8 :         sql_cmd(dbproc);
      73           8 :         dbsqlexec(dbproc);
      74           8 :         while (dbresults(dbproc) == SUCCEED) {
      75             :                 /* nop */
      76             :         }
      77             : 
      78             :         /* insert */
      79           8 :         sql_cmd(dbproc);
      80           8 :         dbsqlexec(dbproc);
      81           8 :         while (dbresults(dbproc) == SUCCEED) {
      82             :                 /* nop */
      83             :         }
      84             : 
      85             :         /* insert */
      86           8 :         sql_cmd(dbproc);
      87           8 :         dbsqlexec(dbproc);
      88           8 :         while (dbresults(dbproc) == SUCCEED) {
      89             :                 /* nop */
      90             :         }
      91             : 
      92             :         /* select */
      93           8 :         sql_cmd(dbproc);
      94           8 :         dbsqlexec(dbproc);
      95           8 :         dbresults(dbproc);
      96             : 
      97          32 :         while (dbnextrow(dbproc) != NO_MORE_ROWS) {
      98          16 :                 ++output_count;
      99             :                 /* Print the date info  */
     100          16 :                 dbconvert(dbproc, dbcoltype(dbproc, 1), dbdata(dbproc, 1), dbdatlen(dbproc, 1), SYBCHAR, (BYTE*) datestring, -1);
     101             : 
     102          16 :                 printf("%s\n", datestring);
     103             : 
     104             :                 /* Break up the creation date into its constituent parts */
     105          16 :                 memcpy(&mydatetime, (DBDATETIME *) (dbdata(dbproc, 1)), sizeof(DBDATETIME));
     106          16 :                 dbdatecrack(dbproc, &dateinfo, &mydatetime);
     107             : 
     108             :                 /* Print the parts of the creation date */
     109          16 :                 printf("\tYear = %d.\n", dateinfo.dateyear);
     110          16 :                 printf("\tMonth = %d.\n", dateinfo.datemonth);
     111          16 :                 printf("\tDay of month = %d.\n", dateinfo.datedmonth);
     112          16 :                 printf("\tDay of year = %d.\n", dateinfo.datedyear);
     113          16 :                 printf("\tDay of week = %d.\n", dateinfo.datedweek);
     114          16 :                 printf("\tHour = %d.\n", dateinfo.datehour);
     115          16 :                 printf("\tMinute = %d.\n", dateinfo.dateminute);
     116          16 :                 printf("\tSecond = %d.\n", dateinfo.datesecond);
     117          16 :                 printf("\tMillisecond = %d.\n", dateinfo.datemsecond);
     118          16 :                 if (dateinfo.dateyear != 1898 && dateinfo.dateyear != 2001)
     119             :                         set_failed();
     120          16 :                 if (dateinfo.dateminute != 24 && dateinfo.dateminute != 30)
     121             :                         set_failed();
     122          16 :                 if (dateinfo.datehour != 19 && dateinfo.datehour != 10)
     123             :                         set_failed();
     124             :         }
     125           8 :         dbresults(dbproc);
     126             : 
     127           8 :         if (output_count != 2)
     128             :                 set_failed();
     129             : 
     130             : #ifdef SYBMSDATETIME2
     131           8 :         dbmsghandle(ignore_msg_handler);
     132             : 
     133             :         /* select */
     134           8 :         sql_cmd(dbproc);
     135           8 :         dbsqlexec(dbproc);
     136           8 :         dbresults(dbproc);
     137             : 
     138          18 :         while (dbnextrow(dbproc) != NO_MORE_ROWS) {
     139           2 :                 int type = dbcoltype(dbproc, 1);
     140             : 
     141           2 :                 ++output_count;
     142             :                 /* Print the date info  */
     143           2 :                 dbconvert(dbproc, type, dbdata(dbproc, 1), dbdatlen(dbproc, 1), SYBCHAR, (BYTE*) datestring, -1);
     144             : 
     145           2 :                 printf("%s\n", datestring);
     146             : 
     147             :                 /* network not high enough for this type ! */
     148           2 :                 if (type == SYBCHAR || type == SYBVARCHAR)
     149             :                         break;
     150             : 
     151             :                 /* Break up the creation date into its constituent parts */
     152           2 :                 if (dbanydatecrack(dbproc, &dateinfo2, dbcoltype(dbproc, 1), dbdata(dbproc, 1)) != SUCCEED)
     153             :                         set_failed();
     154             : 
     155             :                 /* Print the parts of the creation date */
     156           2 :                 printf("\tYear = %d.\n", dateinfo2.dateyear);
     157           2 :                 printf("\tMonth = %d.\n", dateinfo2.datemonth);
     158           2 :                 printf("\tDay of month = %d.\n", dateinfo2.datedmonth);
     159           2 :                 printf("\tDay of year = %d.\n", dateinfo2.datedyear);
     160           2 :                 printf("\tDay of week = %d.\n", dateinfo2.datedweek);
     161           2 :                 printf("\tHour = %d.\n", dateinfo2.datehour);
     162           2 :                 printf("\tMinute = %d.\n", dateinfo2.dateminute);
     163           2 :                 printf("\tSecond = %d.\n", dateinfo2.datesecond);
     164           2 :                 printf("\tNanosecond = %d.\n", dateinfo2.datensecond);
     165           2 :                 if (dateinfo2.dateyear != 1898 || dateinfo2.datensecond != 567000000)
     166             :                         set_failed();
     167             :         }
     168             : #endif
     169             : 
     170           8 :         dbclose(dbproc);
     171           8 :         dbexit();
     172             : 
     173           8 :         if (output_count < 2 || output_count > 3)
     174             :                 set_failed();
     175             : 
     176           8 :         printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
     177           8 :         return failed ? 1 : 0;
     178             : }

Generated by: LCOV version 1.13