1 : /*
2 : * Purpose: Test datetime conversion as well as dbdata() & dbdatlen()
3 : * Functions: dbcmd dbdata dbdatecrack dbdatlen dbnextrow dbresults dbsqlexec
4 : */
5 :
6 : #if HAVE_CONFIG_H
7 : #include <config.h>
8 : #endif /* HAVE_CONFIG_H */
9 :
10 : #include <stdio.h>
11 :
12 : #if HAVE_STDLIB_H
13 : #include <stdlib.h>
14 : #endif /* HAVE_STDLIB_H */
15 :
16 : #if HAVE_STRING_H
17 : #include <string.h>
18 : #endif /* HAVE_STRING_H */
19 :
20 : #include <sqlfront.h>
21 : #include <sqldb.h>
22 :
23 : #include "common.h"
24 :
25 : static char software_version[] = "$Id: t0012.c,v 1.19 2005/05/23 08:06:25 freddy77 Exp $";
26 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
27 : int failed = 0;
28 :
29 :
30 : int
31 : main(int argc, char *argv[])
32 2 : {
33 : LOGINREC *login;
34 : DBPROCESS *dbproc;
35 : char cmd[512];
36 : char sqlCmd[256];
37 : char datestring[256];
38 : DBDATEREC dateinfo;
39 : DBDATETIME mydatetime;
40 :
41 2 : set_malloc_options();
42 :
43 2 : read_login_info(argc, argv);
44 2 : fprintf(stdout, "Start\n");
45 2 : dbinit();
46 :
47 2 : dberrhandle(syb_err_handler);
48 2 : dbmsghandle(syb_msg_handler);
49 :
50 2 : fprintf(stdout, "About to logon\n");
51 :
52 2 : login = dblogin();
53 2 : DBSETLPWD(login, PASSWORD);
54 2 : DBSETLUSER(login, USER);
55 2 : DBSETLAPP(login, "t0012");
56 :
57 2 : fprintf(stdout, "About to open, PASSWORD: %s, USER: %s, SERVER: %s\n", "", "", ""); /* PASSWORD, USER, SERVER); */
58 :
59 2 : dbproc = dbopen(login, SERVER);
60 2 : if (strlen(DATABASE)) {
61 2 : dbuse(dbproc, DATABASE);
62 : }
63 2 : dbloginfree(login);
64 2 : fprintf(stdout, "After logon\n");
65 :
66 2 : fprintf(stdout, "creating table\n");
67 2 : dbcmd(dbproc, "create table #dblib0012 (dt datetime not null)");
68 2 : dbsqlexec(dbproc);
69 4 : while (dbresults(dbproc) == SUCCEED) {
70 : /* nop */
71 : }
72 :
73 2 : sprintf(cmd, "insert into #dblib0012 values ('Feb 27 2001 10:24:35:056AM')");
74 2 : fprintf(stdout, "%s\n", cmd);
75 2 : dbcmd(dbproc, cmd);
76 2 : dbsqlexec(dbproc);
77 4 : while (dbresults(dbproc) == SUCCEED) {
78 : /* nop */
79 : }
80 :
81 2 : sprintf(cmd, "insert into #dblib0012 values ('Dec 25 1898 07:30:00:567PM')");
82 2 : fprintf(stdout, "%s\n", cmd);
83 2 : dbcmd(dbproc, cmd);
84 2 : dbsqlexec(dbproc);
85 4 : while (dbresults(dbproc) == SUCCEED) {
86 : /* nop */
87 : }
88 2 : sprintf(sqlCmd, "SELECT dt FROM #dblib0012");
89 2 : dbcmd(dbproc, sqlCmd);
90 2 : dbsqlexec(dbproc);
91 2 : dbresults(dbproc);
92 :
93 8 : while (dbnextrow(dbproc) != NO_MORE_ROWS) {
94 : /* Print the date info */
95 4 : dbconvert(dbproc, dbcoltype(dbproc, 1), dbdata(dbproc, 1), dbdatlen(dbproc, 1), SYBCHAR, (BYTE*) datestring, -1);
96 :
97 4 : printf("%s\n", datestring);
98 :
99 : /* Break up the creation date into its constituent parts */
100 4 : memcpy(&mydatetime, (DBDATETIME *) (dbdata(dbproc, 1)), sizeof(DBDATETIME));
101 4 : dbdatecrack(dbproc, &dateinfo, &mydatetime);
102 :
103 : /* Print the parts of the creation date */
104 : #ifdef MSDBLIB
105 : printf("\tYear = %d.\n", dateinfo.year);
106 : printf("\tMonth = %d.\n", dateinfo.month);
107 : printf("\tDay of month = %d.\n", dateinfo.day);
108 : printf("\tDay of year = %d.\n", dateinfo.dayofyear);
109 : printf("\tDay of week = %d.\n", dateinfo.weekday);
110 : printf("\tHour = %d.\n", dateinfo.hour);
111 : printf("\tMinute = %d.\n", dateinfo.minute);
112 : printf("\tSecond = %d.\n", dateinfo.second);
113 : printf("\tMillisecond = %d.\n", dateinfo.millisecond);
114 : #else
115 4 : printf("\tYear = %d.\n", dateinfo.dateyear);
116 4 : printf("\tMonth = %d.\n", dateinfo.datemonth);
117 4 : printf("\tDay of month = %d.\n", dateinfo.datedmonth);
118 4 : printf("\tDay of year = %d.\n", dateinfo.datedyear);
119 4 : printf("\tDay of week = %d.\n", dateinfo.datedweek);
120 4 : printf("\tHour = %d.\n", dateinfo.datehour);
121 4 : printf("\tMinute = %d.\n", dateinfo.dateminute);
122 4 : printf("\tSecond = %d.\n", dateinfo.datesecond);
123 4 : printf("\tMillisecond = %d.\n", dateinfo.datemsecond);
124 : #endif
125 : }
126 :
127 2 : dbclose(dbproc);
128 2 : dbexit();
129 :
130 2 : fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
131 2 : free_bread_crumb();
132 2 : return failed ? 1 : 0;
133 : }
|