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

Generated by: LCOV version 1.13