forked from transact-rs/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_complete.rs
More file actions
63 lines (49 loc) 路 1.68 KB
/
Copy pathcommand_complete.rs
File metadata and controls
63 lines (49 loc) 路 1.68 KB
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
use crate::io::Buf;
#[derive(Debug)]
pub(crate) struct CommandComplete {
pub(crate) affected_rows: u64,
}
impl CommandComplete {
pub(crate) fn read(mut buf: &[u8]) -> crate::Result<Self> {
// Attempt to parse the last word in the command tag as an integer
// If it can't be parsed, the tag is probably "CREATE TABLE" or something
// and we should return 0 rows
let rows = buf
.get_str_nul()?
.rsplit(' ')
.next()
.and_then(|s| s.parse().ok())
.unwrap_or(0);
Ok(Self {
affected_rows: rows,
})
}
}
#[cfg(test)]
mod tests {
use super::CommandComplete;
const COMMAND_COMPLETE_INSERT: &[u8] = b"INSERT 0 1\0";
const COMMAND_COMPLETE_UPDATE: &[u8] = b"UPDATE 512\0";
const COMMAND_COMPLETE_CREATE_TABLE: &[u8] = b"CREATE TABLE\0";
const COMMAND_COMPLETE_BEGIN: &[u8] = b"BEGIN\0";
#[test]
fn it_reads_command_complete_for_insert() {
let message = CommandComplete::read(COMMAND_COMPLETE_INSERT).unwrap();
assert_eq!(message.affected_rows, 1);
}
#[test]
fn it_reads_command_complete_for_update() {
let message = CommandComplete::read(COMMAND_COMPLETE_UPDATE).unwrap();
assert_eq!(message.affected_rows, 512);
}
#[test]
fn it_reads_command_complete_for_begin() {
let message = CommandComplete::read(COMMAND_COMPLETE_BEGIN).unwrap();
assert_eq!(message.affected_rows, 0);
}
#[test]
fn it_reads_command_complete_for_create_table() {
let message = CommandComplete::read(COMMAND_COMPLETE_CREATE_TABLE).unwrap();
assert_eq!(message.affected_rows, 0);
}
}