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