1 : /*
2 : * Purpose: Log in, create a table, insert a few rows, select them, and log out.
3 : * Functions: dbbind dbcmd dbcolname dberrhandle dbisopt dbmsghandle dbnextrow dbnumcols dbopen dbresults dbsetlogintime dbsqlexec dbuse
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: t0001.c,v 1.20 2005/05/23 08:06:25 freddy77 Exp $";
26 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
27 :
28 :
29 :
30 : int failed = 0;
31 :
32 :
33 : int
34 : main(int argc, char **argv)
35 2 : {
36 2 : const int rows_to_add = 50;
37 : LOGINREC *login;
38 : DBPROCESS *dbproc;
39 : int i;
40 : char teststr[1024];
41 : DBINT testint;
42 :
43 2 : set_malloc_options();
44 :
45 2 : read_login_info(argc, argv);
46 :
47 2 : fprintf(stdout, "Start\n");
48 2 : add_bread_crumb();
49 :
50 : /* Fortify_EnterScope(); */
51 2 : dbinit();
52 :
53 2 : add_bread_crumb();
54 2 : dberrhandle(syb_err_handler);
55 2 : dbmsghandle(syb_msg_handler);
56 :
57 2 : fprintf(stdout, "About to logon\n");
58 :
59 2 : add_bread_crumb();
60 2 : login = dblogin();
61 2 : DBSETLPWD(login, PASSWORD);
62 2 : DBSETLUSER(login, USER);
63 2 : DBSETLAPP(login, "t0001");
64 :
65 2 : if (argc > 2) {
66 0 : strcpy(SERVER, argv[1]);
67 0 : i = atoi(argv[2]);
68 0 : if (i) {
69 0 : i = dbsetlogintime(i);
70 0 : if (i == SUCCEED)
71 0 : printf("login timeout set to %s.\n", argv[2]);
72 : }
73 : }
74 :
75 2 : fprintf(stdout, "About to open \"%s\"\n", SERVER);
76 :
77 2 : add_bread_crumb();
78 :
79 2 : dbproc = dbopen(login, SERVER);
80 2 : if (!dbproc) {
81 0 : fprintf(stderr, "Unable to connect to %s\n", SERVER);
82 0 : return 1;
83 : }
84 2 : add_bread_crumb();
85 2 : dbloginfree(login);
86 2 : add_bread_crumb();
87 :
88 2 : if (strlen(DATABASE))
89 2 : dbuse(dbproc, DATABASE);
90 2 : add_bread_crumb();
91 :
92 2 : fprintf(stdout, "QUOTED_IDENTIFIER is %s\n", (dbisopt(dbproc, DBQUOTEDIDENT, NULL))? "ON":"OFF");
93 :
94 2 : fprintf(stdout, "creating table\n");
95 2 : dbcmd(dbproc, "create table #dblib0001 (i int not null, s char(10) not null)");
96 2 : dbsqlexec(dbproc);
97 4 : while (dbresults(dbproc) == SUCCEED) {
98 : /* nop */
99 : }
100 :
101 2 : fprintf(stdout, "insert\n");
102 102 : for (i = 0; i < rows_to_add; i++) {
103 : char cmd[1024];
104 :
105 100 : if(dbisopt(dbproc, DBQUOTEDIDENT, NULL))
106 0 : sprintf(cmd, "insert into #dblib0001 values (%d, 'row %03d')", i, i);
107 : else
108 100 : sprintf(cmd, "insert into #dblib0001 values (%d, \"row %03d\")", i, i);
109 100 : fprintf(stdout, "%s\n", cmd);
110 100 : dbcmd(dbproc, cmd);
111 100 : dbsqlexec(dbproc);
112 200 : while (dbresults(dbproc) == SUCCEED) {
113 : /* nop */
114 : }
115 : }
116 :
117 2 : fprintf(stdout, "select\n");
118 2 : dbcmd(dbproc, "select * from #dblib0001 order by i");
119 2 : dbsqlexec(dbproc);
120 2 : add_bread_crumb();
121 :
122 :
123 2 : if (dbresults(dbproc) != SUCCEED) {
124 0 : add_bread_crumb();
125 0 : failed = 1;
126 0 : fprintf(stdout, "Was expecting a result set.\n");
127 0 : exit(1);
128 : }
129 2 : add_bread_crumb();
130 :
131 6 : for (i = 1; i <= dbnumcols(dbproc); i++) {
132 4 : add_bread_crumb();
133 4 : printf("col %d is %s\n", i, dbcolname(dbproc, i));
134 4 : add_bread_crumb();
135 : }
136 :
137 2 : add_bread_crumb();
138 2 : if (SUCCEED != dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint)) {
139 0 : failed = 1;
140 0 : fprintf(stderr, "Had problem with bind\n");
141 0 : abort();
142 : }
143 2 : add_bread_crumb();
144 2 : if (SUCCEED != dbbind(dbproc, 2, STRINGBIND, -1, (BYTE *) teststr)) {
145 0 : failed = 1;
146 0 : fprintf(stderr, "Had problem with bind\n");
147 0 : abort();
148 : }
149 2 : add_bread_crumb();
150 :
151 2 : add_bread_crumb();
152 :
153 102 : for (i = 0; i < rows_to_add; i++) {
154 : char expected[1024];
155 :
156 100 : sprintf(expected, "row %03d", i);
157 :
158 100 : add_bread_crumb();
159 100 : memset(teststr, 'x', sizeof(teststr));
160 100 : teststr[0] = 0;
161 100 : teststr[sizeof(teststr) - 1] = 0;
162 100 : if (REG_ROW != dbnextrow(dbproc)) {
163 0 : failed = 1;
164 0 : fprintf(stderr, "Failed. Expected a row\n");
165 0 : exit(1);
166 : }
167 100 : add_bread_crumb();
168 100 : if (testint != i) {
169 0 : failed = 1;
170 0 : fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, (int) testint);
171 0 : abort();
172 : }
173 100 : if (0 != strncmp(teststr, expected, strlen(expected))) {
174 0 : failed = 1;
175 0 : fprintf(stdout, "Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
176 0 : abort();
177 : }
178 100 : printf("Read a row of data -> %d |%s|\n", (int) testint, teststr);
179 : }
180 :
181 :
182 2 : add_bread_crumb();
183 2 : if (dbnextrow(dbproc) != NO_MORE_ROWS) {
184 0 : failed = 1;
185 0 : fprintf(stderr, "Was expecting no more rows\n");
186 0 : exit(1);
187 : }
188 :
189 2 : add_bread_crumb();
190 2 : dbexit();
191 2 : add_bread_crumb();
192 :
193 2 : fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
194 2 : free_bread_crumb();
195 2 : return failed ? 1 : 0;
196 : }
|