-
Notifications
You must be signed in to change notification settings - Fork 29
/
libs.rs
353 lines (311 loc) · 11.5 KB
/
libs.rs
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
extern crate odbc;
extern crate odbc_safe;
use odbc::*;
use odbc_safe::AutocommitOn;
#[test]
fn list_tables() {
let env = create_environment_v3().unwrap();
let ds = env.connect("TestDataSource", "", "").unwrap();
// scope is required (for now) to close statement before disconnecting
{
let statement = Statement::with_parent(&ds).unwrap();
let mut statement = statement.tables_str("%", "%", "MOV%", "TABLE").unwrap();
assert_eq!(statement.num_result_cols().unwrap(), 5);
let rs = statement.fetch().unwrap();
assert!(rs.is_some());
let mut cur = rs.unwrap();
assert_eq!(cur.get_data::<String>(1).unwrap(), None);
assert_eq!(cur.get_data::<String>(2).unwrap(), None);
assert_eq!(cur.get_data::<String>(3).unwrap(), Some("MOVIES".to_owned()));
assert_eq!(cur.get_data::<String>(4).unwrap(), Some("TABLE".to_owned()));
assert_eq!(cur.get_data::<String>(5).unwrap(), None);
}
ds.disconnect().unwrap();
}
#[test]
fn not_read_only() {
let env = create_environment_v3().unwrap();
let mut conn = env.connect("TestDataSource", "", "").unwrap();
assert!(!conn.is_read_only().unwrap());
conn.disconnect().unwrap();
}
#[test]
fn implicit_disconnect() {
let env = create_environment_v3().unwrap();
env.connect("TestDataSource", "", "").unwrap();
// if there would be no implicit disconnect, all the drops would panic with function sequence
// error
}
#[test]
fn invalid_connection_string() {
let expected = if cfg!(target_os = "windows") {
"State: IM002, Native error: 0, Message: [Microsoft][ODBC Driver Manager] Data source \
name not found and no default driver specified"
} else {
"State: IM002, Native error: 0, Message: [unixODBC][Driver Manager]Data source name not \
found, and no default driver specified"
};
let environment = create_environment_v3().unwrap();
let result = environment.connect_with_connection_string("bla");
let message = format!("{}", result.err().unwrap());
assert_eq!(expected, message);
}
#[test]
fn test_connection_string() {
let environment = create_environment_v3().unwrap();
let conn = environment
.connect_with_connection_string("dsn=TestDataSource;Uid=;Pwd=;")
.unwrap();
conn.disconnect().unwrap();
}
#[test]
fn test_direct_select() {
let env = create_environment_v3().unwrap();
let conn = env.connect("TestDataSource", "", "").unwrap();
let stmt = Statement::with_parent(&conn).unwrap();
let mut stmt = match stmt.exec_direct("SELECT TITLE, YEAR FROM MOVIES ORDER BY YEAR")
.unwrap() {
Data(stmt) => stmt,
NoData(_) => panic!("SELECT statement did not return result set!"),
};
assert_eq!(stmt.num_result_cols().unwrap(), 2);
#[derive(PartialEq, Debug)]
struct Movie {
title: String,
year: i16,
}
let mut actual = Vec::new();
while let Some(mut cursor) = stmt.fetch().unwrap() {
actual.push(Movie {
title: cursor.get_data(1).unwrap().unwrap(),
year: cursor.get_data(2).unwrap().unwrap(),
})
}
let check = actual ==
vec![
Movie {
title: "2001: A Space Odyssey".to_owned(),
year: 1968,
},
Movie {
title: "Jurassic Park".to_owned(),
year: 1993,
},
];
println!("test_direct_select query result: {:?}", actual);
assert!(check);
}
#[test]
fn reuse_statement() {
let env = create_environment_v3().unwrap();
let conn = env.connect("TestDataSource", "", "").unwrap();
let stmt = Statement::with_parent(&conn).unwrap();
let stmt = match stmt.exec_direct("CREATE TABLE STAGE (A VARCHAR, B VARCHAR);")
.unwrap() {
Data(stmt) => stmt.close_cursor().unwrap(), //A result set has been returned, we need to close it.
NoData(stmt) => stmt,
};
let stmt = match stmt.exec_direct("INSERT INTO STAGE (A, B) VALUES ('Hello', 'World');")
.unwrap() {
Data(stmt) => stmt.close_cursor().unwrap(),
NoData(stmt) => stmt,
};
if let Data(mut stmt) = stmt.exec_direct("SELECT A, B FROM STAGE;").unwrap() {
{
let mut cursor = stmt.fetch().unwrap().unwrap();
assert_eq!(cursor.get_data::<String>(1).unwrap().unwrap(), "Hello");
assert_eq!(cursor.get_data::<String>(2).unwrap().unwrap(), "World");
}
let stmt = stmt.close_cursor().unwrap();
stmt.exec_direct("DROP TABLE STAGE;").unwrap();
} else {
panic!("SELECT statement returned no result set")
};
}
#[test]
fn execution_with_parameter() {
let env = create_environment_v3().unwrap();
let conn = env.connect("TestDataSource", "", "").unwrap();
let stmt = Statement::with_parent(&conn).unwrap();
let param = 1968;
let stmt = stmt.bind_parameter(1, ¶m).unwrap();
if let Data(mut stmt) = stmt.exec_direct("SELECT TITLE FROM MOVIES WHERE YEAR = ?")
.unwrap()
{
let mut cursor = stmt.fetch().unwrap().unwrap();
assert_eq!(
cursor.get_data::<String>(1).unwrap().unwrap(),
"2001: A Space Odyssey"
);
} else {
panic!("SELECT statement returned no result set")
};
}
#[test]
fn prepared_execution() {
let env = create_environment_v3().unwrap();
let conn = env.connect("TestDataSource", "", "").unwrap();
let stmt = Statement::with_parent(&conn).unwrap();
let stmt = stmt.prepare("SELECT TITLE FROM MOVIES WHERE YEAR = ?")
.unwrap();
fn execute_query<'a>(
year: u16,
expected: &str,
stmt: Statement<'a, 'a, Prepared, NoResult, AutocommitOn>,
) -> Result<Statement<'a, 'a, Prepared, NoResult, AutocommitOn>> {
let stmt = stmt.bind_parameter(1, &year)?;
let stmt = if let Data(mut stmt) = stmt.execute()? {
{
let mut cursor = stmt.fetch()?.unwrap();
assert_eq!(cursor.get_data::<String>(1)?.unwrap(), expected);
}
stmt.close_cursor()?
} else {
panic!("SELECT statement returned no result set");
};
stmt.reset_parameters()
};
let stmt = execute_query(1968, "2001: A Space Odyssey", stmt).unwrap();
execute_query(1993, "Jurassic Park", stmt).unwrap();
}
// These tests query the results of catalog functions. These results are only likely to match the
// expectation on the travis ci build on linux. Therefore we limit compilation and execution of
// these tests to this platform.
#[cfg_attr(not(feature = "travis"), ignore)]
#[test]
fn list_drivers() {
let mut environment = create_environment_v3().unwrap();
let drivers = environment.drivers().expect("Drivers can be iterated over");
println!("{:?}", drivers);
let expected = ["PostgreSQL ANSI", "PostgreSQL Unicode", "SQLite", "SQLite3"];
let mut actual: Vec<_> = drivers.iter().map(|d| &d.description).collect();
actual.sort();
assert_eq!(actual, expected);
}
#[cfg_attr(not(feature = "travis"), ignore)]
#[test]
fn list_data_sources() {
let mut environment = create_environment_v3().unwrap();
let sources = environment.data_sources().expect(
"Data sources can be iterated over",
);
println!("{:?}", sources);
let expected = [
DataSourceInfo {
server_name: "PostgreSQL".to_owned(),
driver: "PostgreSQL Unicode".to_owned(),
},
DataSourceInfo {
server_name: "TestDataSource".to_owned(),
driver: "SQLite3".to_owned(),
},
];
assert!(sources.iter().eq(expected.iter()));
}
#[cfg_attr(not(feature = "travis"), ignore)]
#[test]
fn list_user_data_sources() {
let mut environment = create_environment_v3().unwrap();
let sources = environment.user_data_sources().expect(
"Data sources can be iterated over",
);
println!("{:?}", sources);
let expected = [
DataSourceInfo {
server_name: "PostgreSQL".to_owned(),
driver: "PostgreSQL Unicode".to_owned(),
},
DataSourceInfo {
server_name: "TestDataSource".to_owned(),
driver: "SQLite3".to_owned(),
},
];
assert!(sources.iter().eq(expected.iter()));
}
#[cfg_attr(not(feature = "travis"), ignore)]
#[test]
fn list_system_data_sources() {
let mut environment = create_environment_v3().unwrap();
let sources = environment.system_data_sources().expect(
"Data sources can be iterated over",
);
println!("{:?}", sources);
let expected: [DataSourceInfo; 0] = [];
assert!(sources.iter().eq(expected.iter()));
}
#[test]
fn read_big_string() {
let env = create_environment_v3().unwrap();
let conn = env.connect("TestDataSource", "", "").unwrap();
let stmt = Statement::with_parent(&conn).unwrap();
let stmt = match stmt.exec_direct("CREATE TABLE READ_BIG_STRING (DATA TEXT)").unwrap() {
Data(stmt) => stmt.close_cursor().unwrap(),
NoData(stmt) => stmt,
};
let data = "Hello, World".repeat(43);
assert!(data.len() > 512);
stmt
.prepare("INSERT INTO READ_BIG_STRING VALUES (?)")
.unwrap()
.bind_parameter(1, &data)
.unwrap()
.execute()
.unwrap();
let stmt = Statement::with_parent(&conn).unwrap();
let sel_query = "SELECT DATA FROM READ_BIG_STRING";
let stmt = if let Data(mut stmt) = stmt.exec_direct(sel_query).unwrap() {
{
let mut cursor = stmt.fetch().unwrap().unwrap();
// Do read with bytes buffer
let data0 = cursor.get_data::<Vec<u8>>(1).unwrap().unwrap();
assert_eq!(data0, data.as_bytes());
}
stmt.close_cursor().unwrap()
} else {
panic!("SELECT statement returned no result set")
};
let stmt = if let Data(mut stmt) = stmt.exec_direct(sel_query).unwrap() {
{
let mut cursor = stmt.fetch().unwrap().unwrap();
// Do read with String buffer
let data0 = cursor.get_data::<String>(1).unwrap().unwrap();
assert_eq!(data0, data);
}
stmt.close_cursor().unwrap()
} else {
panic!("SELECT statement returned no result set")
};
stmt.exec_direct("DROP TABLE READ_BIG_STRING").unwrap();
}
#[test]
fn zero_truncation_bug() {
let env = create_environment_v3().unwrap();
let conn = env.connect("TestDataSource", "", "").unwrap();
let stmt = Statement::with_parent(&conn).unwrap();
let stmt = match stmt.exec_direct("CREATE TABLE ZERO_TRUNCATION_BUG (DATA BLOB);").unwrap() {
Data(stmt) => stmt.close_cursor().unwrap(),
NoData(stmt) => stmt,
};
// Reproduction of zeroes truncation bug. Until now there is no chance to query binary data
// with zero at 512 byte border.
let data = vec![0;513];
stmt
.prepare("INSERT INTO ZERO_TRUNCATION_BUG VALUES (?)")
.unwrap()
.bind_parameter(1, &data)
.unwrap()
.execute()
.unwrap();
let stmt = Statement::with_parent(&conn).unwrap();
if let Data(mut stmt) = stmt.exec_direct("SELECT DATA FROM ZERO_TRUNCATION_BUG").unwrap() {
{
let mut cursor = stmt.fetch().unwrap().unwrap();
let data0 = cursor.get_data::<Vec<u8>>(1).unwrap().unwrap();
assert_eq!(data0, data);
}
let stmt = stmt.close_cursor().unwrap();
stmt.exec_direct("DROP TABLE ZERO_TRUNCATION_BUG").unwrap();
} else {
panic!("SELECT statement returned no result set")
};
}