Line data Source code
1 : #define TDS_DONT_DEFINE_DEFAULT_FUNCTIONS
2 : #include "common.h"
3 : #include <freetds/replacements.h>
4 :
5 : int read_login_info(void);
6 :
7 : int
8 0 : read_login_info(void)
9 : {
10 : const char *s;
11 :
12 140 : s = read_login_info_base(&common_pwd, DEFAULT_PWD_PATH);
13 0 : return s ? TDS_SUCCESS : TDS_FAIL;
14 : }
15 :
16 : TDSCONTEXT *test_context = NULL;
17 :
18 : int
19 140 : try_tds_login(TDSLOGIN ** login, TDSSOCKET ** tds, const char *appname, int verbose)
20 : {
21 : TDSLOGIN *connection;
22 : char *appname_copy;
23 140 : const char *charset = "ISO-8859-1";
24 :
25 140 : if (verbose) {
26 10 : printf("Entered tds_try_login()\n");
27 : }
28 140 : if (!login) {
29 0 : fprintf(stderr, "Invalid TDSLOGIN**\n");
30 0 : return TDS_FAIL;
31 : }
32 140 : if (!tds) {
33 0 : fprintf(stderr, "Invalid TDSSOCKET**\n");
34 0 : return TDS_FAIL;
35 : }
36 :
37 140 : if (verbose) {
38 10 : printf("Trying read_login_info()\n");
39 : }
40 140 : read_login_info();
41 :
42 140 : if (verbose) {
43 10 : printf("Setting login parameters\n");
44 : }
45 140 : *login = tds_alloc_login(true);
46 140 : if (!*login) {
47 0 : fprintf(stderr, "tds_alloc_login() failed.\n");
48 0 : return TDS_FAIL;
49 : }
50 140 : appname_copy = strdup(appname);
51 140 : if (common_pwd.charset[0])
52 30 : charset = common_pwd.charset;
53 140 : if (!tds_set_passwd(*login, common_pwd.password)
54 140 : || !tds_set_user(*login, common_pwd.user)
55 140 : || !appname_copy
56 140 : || !tds_set_app(*login, basename(appname_copy))
57 140 : || !tds_set_host(*login, "myhost")
58 140 : || !tds_set_library(*login, "TDS-Library")
59 140 : || !tds_set_server(*login, common_pwd.server)
60 140 : || !tds_set_client_charset(*login, charset)
61 140 : || !tds_set_language(*login, "us_english")) {
62 0 : free(appname_copy);
63 0 : fprintf(stderr, "tds_alloc_login() failed.\n");
64 0 : return TDS_FAIL;
65 : }
66 140 : free(appname_copy);
67 :
68 140 : if (verbose) {
69 10 : printf("Connecting to database\n");
70 : }
71 140 : test_context = tds_alloc_context(NULL);
72 140 : *tds = tds_alloc_socket(test_context, 512);
73 140 : tds_set_parent(*tds, NULL);
74 140 : connection = tds_read_config_info(*tds, *login, test_context->locale);
75 140 : if (!connection || tds_connect_and_login(*tds, connection) != TDS_SUCCESS) {
76 0 : if (connection) {
77 0 : tds_free_socket(*tds);
78 0 : *tds = NULL;
79 0 : tds_free_login(connection);
80 : }
81 0 : fprintf(stderr, "tds_connect_and_login() failed\n");
82 0 : return TDS_FAIL;
83 : }
84 140 : tds_free_login(connection);
85 :
86 140 : return TDS_SUCCESS;
87 : }
88 :
89 :
90 : /* Note that this always suceeds */
91 : int
92 140 : try_tds_logout(TDSLOGIN * login, TDSSOCKET * tds, int verbose)
93 : {
94 140 : if (verbose) {
95 10 : printf("Entered tds_try_logout()\n");
96 : }
97 140 : tds_close_socket(tds);
98 140 : tds_free_socket(tds);
99 140 : tds_free_login(login);
100 140 : tds_free_context(test_context);
101 140 : test_context = NULL;
102 140 : return TDS_SUCCESS;
103 : }
104 :
105 : /* Run query for which there should be no return results */
106 : int
107 730 : run_query(TDSSOCKET * tds, const char *query)
108 : {
109 : int rc;
110 : int result_type;
111 :
112 730 : rc = tds_submit_query(tds, query);
113 730 : if (rc != TDS_SUCCESS) {
114 0 : fprintf(stderr, "tds_submit_query() failed for query '%s'\n", query);
115 0 : return TDS_FAIL;
116 : }
117 :
118 1532 : while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) {
119 :
120 802 : switch (result_type) {
121 : case TDS_DONE_RESULT:
122 : case TDS_DONEPROC_RESULT:
123 : case TDS_DONEINPROC_RESULT:
124 : /* ignore possible spurious result (TDS7+ send it) */
125 : case TDS_STATUS_RESULT:
126 : break;
127 0 : default:
128 0 : fprintf(stderr, "Error: query should not return results\n");
129 0 : return TDS_FAIL;
130 : }
131 : }
132 730 : if (rc == TDS_FAIL) {
133 0 : fprintf(stderr, "tds_process_tokens() returned TDS_FAIL for '%s'\n", query);
134 0 : return TDS_FAIL;
135 730 : } else if (rc != TDS_NO_MORE_RESULTS) {
136 0 : fprintf(stderr, "tds_process_tokens() unexpected return\n");
137 0 : return TDS_FAIL;
138 : }
139 :
140 : return TDS_SUCCESS;
141 : }
|