Line data Source code
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 : #include "common.h"
7 :
8 : int failed = 0;
9 :
10 :
11 : int
12 8 : main(int argc, char **argv)
13 : {
14 8 : const int rows_to_add = 50;
15 : LOGINREC *login;
16 : DBPROCESS *dbproc;
17 : int i;
18 : char teststr[1024];
19 : DBINT testint, erc;
20 :
21 8 : set_malloc_options();
22 :
23 8 : read_login_info(argc, argv);
24 8 : if (argc > 1) {
25 0 : argc -= optind;
26 0 : argv += optind;
27 : }
28 :
29 8 : printf("Starting %s\n", argv[0]);
30 :
31 : /* Fortify_EnterScope(); */
32 8 : dbinit();
33 :
34 8 : dberrhandle(syb_err_handler);
35 8 : dbmsghandle(syb_msg_handler);
36 :
37 8 : printf("About to logon as \"%s\"\n", USER);
38 :
39 8 : login = dblogin();
40 8 : DBSETLPWD(login, PASSWORD);
41 8 : DBSETLUSER(login, USER);
42 8 : DBSETLAPP(login, "t0001");
43 :
44 8 : if (argc > 1) {
45 0 : printf("server and login timeout overrides (%s and %s) detected\n", argv[0], argv[1]);
46 0 : strcpy(SERVER, argv[0]);
47 0 : i = atoi(argv[1]);
48 0 : if (i) {
49 0 : i = dbsetlogintime(i);
50 0 : printf("dbsetlogintime returned %s.\n", (i == SUCCEED)? "SUCCEED" : "FAIL");
51 : }
52 : }
53 :
54 8 : printf("About to open \"%s\"\n", SERVER);
55 :
56 8 : dbproc = dbopen(login, SERVER);
57 8 : if (!dbproc) {
58 0 : fprintf(stderr, "Unable to connect to %s\n", SERVER);
59 0 : return 1;
60 : }
61 8 : dbloginfree(login);
62 :
63 8 : printf("Using database \"%s\"\n", DATABASE);
64 8 : if (strlen(DATABASE)) {
65 8 : erc = dbuse(dbproc, DATABASE);
66 8 : assert(erc == SUCCEED);
67 : }
68 :
69 : #ifdef DBQUOTEDIDENT
70 8 : printf("QUOTED_IDENTIFIER is %s\n", (dbisopt(dbproc, DBQUOTEDIDENT, NULL))? "ON":"OFF");
71 : #endif
72 8 : sql_cmd(dbproc);
73 8 : dbsqlexec(dbproc);
74 8 : while (dbresults(dbproc) == SUCCEED) {
75 : /* nop */
76 : }
77 :
78 400 : for (i = 0; i < rows_to_add && sql_cmd(dbproc) == SUCCEED; i++) {
79 400 : dbsqlexec(dbproc);
80 400 : while (dbresults(dbproc) == SUCCEED) {
81 : /* nop */
82 : }
83 : }
84 :
85 8 : sql_cmd(dbproc);
86 8 : dbsqlexec(dbproc);
87 :
88 8 : if (dbresults(dbproc) != SUCCEED) {
89 0 : failed = 1;
90 0 : fprintf(stderr, "error: expected a result set, none returned.\n");
91 0 : exit(1);
92 : }
93 :
94 16 : for (i = 1; i <= dbnumcols(dbproc); i++)
95 16 : printf("col %d is %s\n", i, dbcolname(dbproc, i));
96 :
97 8 : if (SUCCEED != dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint)) {
98 0 : failed = 1;
99 0 : fprintf(stderr, "Had problem with bind\n");
100 0 : abort();
101 : }
102 8 : if (SUCCEED != dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr)) {
103 0 : failed = 1;
104 0 : fprintf(stderr, "Had problem with bind\n");
105 0 : abort();
106 : }
107 :
108 400 : for (i = 0; i < rows_to_add; i++) {
109 : char expected[1024];
110 :
111 400 : sprintf(expected, "row %03d", i);
112 :
113 400 : memset(teststr, 'x', sizeof(teststr));
114 400 : teststr[0] = 0;
115 400 : teststr[sizeof(teststr) - 1] = 0;
116 400 : if (REG_ROW != dbnextrow(dbproc)) {
117 0 : failed = 1;
118 0 : fprintf(stderr, "Failed. Expected a row\n");
119 0 : exit(1);
120 : }
121 400 : if (testint != i) {
122 0 : failed = 1;
123 0 : fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, (int) testint);
124 0 : abort();
125 : }
126 400 : if (0 != strncmp(teststr, expected, strlen(expected))) {
127 0 : failed = 1;
128 0 : printf("Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
129 0 : abort();
130 : }
131 400 : printf("Read a row of data -> %d |%s|\n", (int) testint, teststr);
132 : }
133 :
134 8 : if (dbnextrow(dbproc) != NO_MORE_ROWS) {
135 0 : failed = 1;
136 0 : fprintf(stderr, "Was expecting no more rows\n");
137 0 : exit(1);
138 : }
139 :
140 8 : dbexit();
141 :
142 8 : printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
143 8 : return failed ? 1 : 0;
144 : }
|