-
Notifications
You must be signed in to change notification settings - Fork 2
/
tdspp.cc
161 lines (145 loc) · 4.55 KB
/
tdspp.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* $Id: tdspp.cc,v 2.7 2005/10/20 11:24:12 martin Exp $
* Copyright (c) 2004, 2005, Voidsoft AB
*/
#include "tdspp.hh"
#include <iostream>
TDSPP::TDSPP() {
verbose = 0;
if (init() != CS_SUCCEED) {
throw Exception("TDSPP: Init failed");
return;
}
}
TDSPP::~TDSPP() {
disconnect();
ct_con_drop(conn);
ct_exit(ctx, CS_UNUSED);
cs_ctx_drop(ctx);
}
CS_RETCODE TDSPP::init(void) {
CS_RETCODE ret;
ret = cs_ctx_alloc(CS_VERSION_100, &ctx);
if (ret != CS_SUCCEED) {
throw Exception("TDSPP::init: Context allocation failed!");
return ret;
}
ret = ct_init(ctx, CS_VERSION_100);
if (ret != CS_SUCCEED) {
throw Exception("TDSPP::init: Library init failed!");
return ret;
}
ret = ct_con_alloc(ctx, &conn);
if (ret != CS_SUCCEED) {
throw Exception("TDSPP::init: Connect allocation failed!");
return ret;
}
return CS_SUCCEED;
}
void TDSPP::connect(const string &server,
const string &username,
const string &password) {
this->server = server;
this->username = username;
this->password = password;
login();
}
void TDSPP::login(void) {
if (ct_con_props(conn, CS_SET, CS_USERNAME,
(CS_VOID*)username.c_str(), CS_NULLTERM,
(CS_INT*)NULL) != CS_SUCCEED) {
throw Exception("TDSPP::login: ct_con_props() SET USERNAME failed!");
return;
}
if (ct_con_props(conn, CS_SET, CS_PASSWORD,
(CS_VOID*)password.c_str(), CS_NULLTERM,
(CS_INT*)NULL) != CS_SUCCEED) {
throw Exception("TDSPP::login: ct_con_props() SET PASSWORD failed!");
return;
}
if (ct_con_props(conn, CS_SET, CS_LOGIN_TIMEOUT,
(CS_VOID*)&timeval, CS_NULLTERM,
(CS_INT*)NULL) != CS_SUCCEED) {
throw Exception("TDSPP::login: ct_con_props() SET LOGIN_TIMEOUT failed!");
return;
}
if (ct_con_props(conn, CS_SET, CS_TIMEOUT,
(CS_VOID*)&timeval, CS_NULLTERM,
(CS_INT*)NULL) != CS_SUCCEED) {
throw Exception("TDSPP::login: ct_con_props() SET TIMEOUT failed!");
return;
}
if (ct_con_props(conn, CS_SET, CS_TDS_VERSION,
(CS_VOID*)&tdsversion, CS_NULLTERM,
(CS_INT*)NULL) != CS_SUCCEED) {
throw Exception("TDSPP::login: ct_con_props() SET TDS_VERSION failed!");
return;
}
conn_retcode = ct_connect(conn, (CS_CHAR*)server.c_str(),
CS_NULLTERM);
if (conn_retcode != CS_SUCCEED) {
throw Exception("TDSPP::login: Connection failed!");
return;
}
if (ct_cmd_alloc(conn, &cmd) != CS_SUCCEED) {
throw Exception("TDSPP::login: Command allocation failed!");
return;
}
}
void TDSPP::disconnect(void) {
if (ct_cancel(conn, NULL, CS_CANCEL_ALL) != CS_SUCCEED) {
throw Exception("TDSPP::disconnect: ct_cancel() failed!");
return;
}
if (conn_retcode != CS_SUCCEED)
return;
ct_cmd_drop(cmd);
ct_close(conn, CS_UNUSED);
}
void TDSPP::execute(const string& sql) {
CS_RETCODE results_ret;
CS_INT result_type;
if (cmd == NULL) throw Exception("TDSPP::execute: cmd is not allocated");
if (ct_command(cmd, CS_LANG_CMD,
sql.c_str(), CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
throw Exception("TDSPP::execute: ct_command() failed");
return;
}
if (ct_send(cmd) != CS_SUCCEED) {
throw Exception("TDSPP::execute: ct_send() failed");
return;
}
while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
switch ((int) result_type) {
case CS_CMD_SUCCEED:
break;
case CS_CMD_DONE:
break;
case CS_CMD_FAIL:
throw Exception(string("TDSPP::execute: Failed on ")+sql);
// return CS_FAIL;
break;
default:
throw Exception("TDSPP::execute: ct_results() unexpected result_type.");
return;
}
}
switch ((int) results_ret) {
case CS_END_RESULTS:
break;
case CS_FAIL:
throw Exception("TDSPP::execute: ct_results() failed.");
return;
default:
throw Exception("TDSPP::execute: ct_results() unexpected return.");
return;
}
}
Query* TDSPP::sql(const string& sql) {
Query* q = new Query(this);
q->command = sql;
return q;
}
CS_RETCODE TDSPP::connection_state() {
return conn_retcode;
}