Line data Source code
1 : #include "common.h"
2 :
3 : #if HAVE_UNISTD_H
4 : #include <unistd.h>
5 : #endif /* HAVE_UNISTD_H */
6 :
7 : #if HAVE_SYS_SOCKET_H
8 : #include <sys/socket.h>
9 : #endif /* HAVE_SYS_SOCKET_H */
10 :
11 : #if HAVE_SYS_STAT_H
12 : #include <sys/stat.h>
13 : #endif /* HAVE_SYS_STAT_H */
14 :
15 : #if HAVE_SYS_IOCTL_H
16 : #include <sys/ioctl.h>
17 : #endif /* HAVE_SYS_IOCTL_H */
18 :
19 : #if HAVE_SYS_WAIT_H
20 : #include <sys/wait.h>
21 : #endif /* HAVE_SYS_WAIT_H */
22 :
23 : #include <freetds/utils.h>
24 :
25 : /* Test for SQLEndTran */
26 :
27 : static void
28 10 : ReadErrorConn(void)
29 : {
30 10 : ODBC_BUF *odbc_buf = NULL;
31 10 : SQLTCHAR *err = (SQLTCHAR *) ODBC_GET(sizeof(odbc_err)*sizeof(SQLTCHAR));
32 10 : SQLTCHAR *state = (SQLTCHAR *) ODBC_GET(sizeof(odbc_sqlstate)*sizeof(SQLTCHAR));
33 :
34 10 : memset(odbc_err, 0, sizeof(odbc_err));
35 10 : memset(odbc_sqlstate, 0, sizeof(odbc_sqlstate));
36 10 : CHKGetDiagRec(SQL_HANDLE_DBC, odbc_conn, 1, state, NULL, err, sizeof(odbc_err), NULL, "SI");
37 10 : strcpy(odbc_err, C(err));
38 10 : strcpy(odbc_sqlstate, C(state));
39 10 : ODBC_FREE();
40 10 : printf("Message: '%s' %s\n", odbc_sqlstate, odbc_err);
41 10 : }
42 :
43 : #ifdef _WIN32
44 : #define SHUT_RDWR SD_BOTH
45 : #endif
46 :
47 : static int
48 10 : close_last_socket(void)
49 : {
50 10 : TDS_SYS_SOCKET max_socket = odbc_find_last_socket();
51 :
52 10 : if (max_socket < 0) {
53 0 : fprintf(stderr, "Error finding last socket\n");
54 0 : return 0;
55 : }
56 :
57 : /* close connection */
58 10 : shutdown(max_socket, SHUT_RDWR);
59 :
60 10 : return 1;
61 : }
62 :
63 10 : TEST_MAIN()
64 : {
65 10 : odbc_use_version3 = 1;
66 :
67 10 : odbc_mark_sockets_opened();
68 10 : odbc_connect();
69 :
70 10 : CHKSetConnectAttr(SQL_ATTR_AUTOCOMMIT, (void *) SQL_AUTOCOMMIT_OFF, 0, "S");
71 :
72 10 : odbc_command("SELECT 1");
73 10 : CHKMoreResults("No");
74 :
75 10 : if (!close_last_socket()) {
76 0 : fprintf(stderr, "Error closing connection\n");
77 0 : return 1;
78 : }
79 10 : CHKEndTran(SQL_HANDLE_DBC, odbc_conn, SQL_ROLLBACK, "E");
80 :
81 : /* the error should be written in the connection, not in the statement */
82 10 : ReadErrorConn();
83 10 : if (strcmp(odbc_sqlstate, "08S01") != 0 || strstr(odbc_err, "Write to the server") == NULL) {
84 0 : odbc_disconnect();
85 0 : fprintf(stderr, "Unexpected error message %s %s\n", odbc_sqlstate, odbc_err);
86 0 : return 1;
87 : }
88 :
89 10 : odbc_disconnect();
90 10 : return 0;
91 : }
|