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 || !tds_set_app(*login, basename(appname_copy))
56 140 : || !tds_set_host(*login, "myhost")
57 140 : || !tds_set_library(*login, "TDS-Library")
58 140 : || !tds_set_server(*login, common_pwd.server)
59 140 : || !tds_set_client_charset(*login, charset)
60 140 : || !tds_set_language(*login, "us_english")) {
61 0 : free(appname_copy);
62 0 : fprintf(stderr, "tds_alloc_login() failed.\n");
63 0 : return TDS_FAIL;
64 : }
65 140 : free(appname_copy);
66 :
67 140 : if (verbose) {
68 10 : printf("Connecting to database\n");
69 : }
70 140 : test_context = tds_alloc_context(NULL);
71 140 : *tds = tds_alloc_socket(test_context, 512);
72 140 : tds_set_parent(*tds, NULL);
73 140 : connection = tds_read_config_info(*tds, *login, test_context->locale);
74 140 : if (!connection || tds_connect_and_login(*tds, connection) != TDS_SUCCESS) {
75 0 : if (connection) {
76 0 : tds_free_socket(*tds);
77 0 : *tds = NULL;
78 0 : tds_free_login(connection);
79 : }
80 0 : fprintf(stderr, "tds_connect_and_login() failed\n");
81 0 : return TDS_FAIL;
82 : }
83 140 : tds_free_login(connection);
84 :
85 140 : return TDS_SUCCESS;
86 : }
87 :
88 :
89 : /* Note that this always suceeds */
90 : int
91 140 : try_tds_logout(TDSLOGIN *login, TDSSOCKET *tds, int verbose)
92 : {
93 140 : if (verbose) {
94 10 : printf("Entered tds_try_logout()\n");
95 : }
96 140 : tds_close_socket(tds);
97 140 : tds_free_socket(tds);
98 140 : tds_free_login(login);
99 140 : tds_free_context(test_context);
100 140 : test_context = NULL;
101 140 : return TDS_SUCCESS;
102 : }
103 :
104 : /* Run query for which there should be no return results */
105 : int
106 730 : run_query(TDSSOCKET *tds, const char *query)
107 : {
108 : int rc;
109 : int result_type;
110 :
111 730 : rc = tds_submit_query(tds, query);
112 730 : if (rc != TDS_SUCCESS) {
113 0 : fprintf(stderr, "tds_submit_query() failed for query '%s'\n", query);
114 0 : return TDS_FAIL;
115 : }
116 :
117 1532 : while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) {
118 :
119 802 : switch (result_type) {
120 : case TDS_DONE_RESULT:
121 : case TDS_DONEPROC_RESULT:
122 : case TDS_DONEINPROC_RESULT:
123 : /* ignore possible spurious result (TDS7+ send it) */
124 : case TDS_STATUS_RESULT:
125 : break;
126 0 : default:
127 0 : fprintf(stderr, "Error: query should not return results\n");
128 0 : return TDS_FAIL;
129 : }
130 : }
131 730 : if (rc == TDS_FAIL) {
132 0 : fprintf(stderr, "tds_process_tokens() returned TDS_FAIL for '%s'\n", query);
133 0 : return TDS_FAIL;
134 730 : } else if (rc != TDS_NO_MORE_RESULTS) {
135 0 : fprintf(stderr, "tds_process_tokens() unexpected return\n");
136 0 : return TDS_FAIL;
137 : }
138 :
139 : return TDS_SUCCESS;
140 : }
|