1 : /*
2 : * Purpose: Test binding of string types
3 : * Functions: dbbind dbcmd dbcolname dbnextrow dbnumcols dbopen dbresults dbsqlexec
4 :
5 : */
6 :
7 : #if HAVE_CONFIG_H
8 : #include <config.h>
9 : #endif /* HAVE_CONFIG_H */
10 :
11 : #include <stdio.h>
12 :
13 : #if HAVE_STDLIB_H
14 : #include <stdlib.h>
15 : #endif /* HAVE_STDLIB_H */
16 :
17 : #if HAVE_STRING_H
18 : #include <string.h>
19 : #endif /* HAVE_STRING_H */
20 :
21 : #include <sqlfront.h>
22 : #include <sqldb.h>
23 :
24 : #include "common.h"
25 :
26 : static char software_version[] = "$Id: t0011.c,v 1.10 2005/04/19 03:51:04 jklowden Exp $";
27 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
28 :
29 : int failed = 0;
30 : const char long_column[] = "This is a really long column to ensure that the next row ends properly.";
31 : const char short_column[] = "Short column";
32 :
33 : void insert_row(DBPROCESS * dbproc, char *cmd);
34 : int select_rows(DBPROCESS * dbproc, int bind_type);
35 :
36 : int
37 : main(int argc, char **argv)
38 2 : {
39 : LOGINREC *login;
40 : DBPROCESS *dbproc;
41 : char cmd[2048];
42 :
43 2 : read_login_info(argc, argv);
44 2 : fprintf(stdout, "Start\n");
45 :
46 2 : dbinit();
47 :
48 2 : fprintf(stdout, "About to logon\n");
49 :
50 2 : login = dblogin();
51 2 : DBSETLPWD(login, PASSWORD);
52 2 : DBSETLUSER(login, USER);
53 2 : DBSETLAPP(login, "t0011");
54 :
55 2 : fprintf(stdout, "About to open\n");
56 :
57 2 : dbproc = dbopen(login, SERVER);
58 2 : if (strlen(DATABASE))
59 2 : dbuse(dbproc, DATABASE);
60 2 : dbloginfree(login);
61 :
62 2 : fprintf(stdout, "Dropping table\n");
63 2 : dbcmd(dbproc, "drop table #dblib0011");
64 2 : dbsqlexec(dbproc);
65 2 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
66 : /* nop */
67 : }
68 :
69 2 : fprintf(stdout, "creating table\n");
70 2 : dbcmd(dbproc, "create table #dblib0011 (i int not null, c1 char(200) not null, c2 char(200) null, vc varchar(200) null)");
71 2 : dbsqlexec(dbproc);
72 4 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
73 : /* nop */
74 : }
75 :
76 2 : fprintf(stdout, "insert\n");
77 :
78 2 : sprintf(cmd, "insert into #dblib0011 values (1, '%s','%s','%s')", long_column, long_column, long_column);
79 2 : insert_row(dbproc, cmd);
80 2 : sprintf(cmd, "insert into #dblib0011 values (2, '%s','%s','%s')", short_column, short_column, short_column);
81 2 : insert_row(dbproc, cmd);
82 2 : sprintf(cmd, "insert into #dblib0011 values (3, '%s',NULL,NULL)", short_column);
83 2 : insert_row(dbproc, cmd);
84 :
85 2 : failed = select_rows(dbproc, STRINGBIND);
86 :
87 2 : dbexit();
88 :
89 2 : fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
90 2 : return failed ? 1 : 0;
91 : }
92 :
93 : int
94 : select_rows(DBPROCESS * dbproc, int bind_type)
95 2 : {
96 : char teststr[1024];
97 : char teststr2[1024];
98 : char testvstr[1024];
99 : DBINT testint;
100 : DBINT i;
101 :
102 :
103 2 : fprintf(stdout, "select\n");
104 2 : dbcmd(dbproc, "select * from #dblib0011 order by i");
105 2 : dbsqlexec(dbproc);
106 :
107 :
108 2 : if (dbresults(dbproc) != SUCCEED) {
109 0 : failed = 1;
110 0 : fprintf(stdout, "Was expecting a result set.");
111 0 : exit(1);
112 : }
113 :
114 10 : for (i = 1; i <= dbnumcols(dbproc); i++) {
115 8 : printf("col %d is %s\n", i, dbcolname(dbproc, i));
116 : }
117 :
118 2 : if (SUCCEED != dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint)) {
119 0 : fprintf(stderr, "Had problem with bind\n");
120 0 : return 1;
121 : }
122 2 : if (SUCCEED != dbbind(dbproc, 2, bind_type, -1, (BYTE *) teststr)) {
123 0 : fprintf(stderr, "Had problem with bind\n");
124 0 : return 1;
125 : }
126 2 : if (SUCCEED != dbbind(dbproc, 3, bind_type, -1, (BYTE *) teststr2)) {
127 0 : fprintf(stderr, "Had problem with bind\n");
128 0 : return 1;
129 : }
130 2 : if (SUCCEED != dbbind(dbproc, 4, bind_type, -1, (BYTE *) testvstr)) {
131 0 : fprintf(stderr, "Had problem with bind\n");
132 0 : return 1;
133 : }
134 :
135 2 : i = 0;
136 10 : while (dbnextrow(dbproc) == REG_ROW) {
137 6 : i++;
138 6 : if (testint != i) {
139 0 : fprintf(stdout, "Failed. Expected i to be |%d|, was |%d|\n", testint, i);
140 0 : return 1;
141 : }
142 6 : printf("c: %s$\n", teststr);
143 6 : printf("c2: %s$\n", teststr2);
144 6 : printf("vc: %s$\n", testvstr);
145 : if (bind_type == STRINGBIND) {
146 : } else {
147 : }
148 : }
149 2 : return 0;
150 : }
151 :
152 : void
153 : insert_row(DBPROCESS * dbproc, char *cmd)
154 6 : {
155 6 : fprintf(stdout, "%s\n", cmd);
156 6 : dbcmd(dbproc, cmd);
157 6 : dbsqlexec(dbproc);
158 12 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
159 : /* nop */
160 : }
161 6 : }
|