-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpsql.rs
More file actions
126 lines (109 loc) · 4.33 KB
/
Copy pathsimpsql.rs
File metadata and controls
126 lines (109 loc) · 4.33 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
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
use bytes::{BufMut, BytesMut};
use std::cmp::Ordering;
use std::io::{self, Write};
use std::net::Ipv4Addr;
use std::str::FromStr;
use std::time::Duration;
use pg_rusted_wire::auth::StartupMsg;
use pg_rusted_wire::wire::*;
const DEFAULT_USER: &str = "postgres";
const DEFAULT_PORT: u16 = 5432;
const DEFAULT_IP: &str = "127.0.0.1";
const DEFAULT_PASS: &str = "pass12234";
const DEFAULT_DATABASE: &str = "postgres";
fn main() {
let mut startup_msg = StartupMsg::new(
String::from(DEFAULT_USER),
Some(String::from(DEFAULT_DATABASE)),
None,
None,
);
let client = Client::new(
Ipv4Addr::from_str(DEFAULT_IP).expect("IPV4 address error"),
DEFAULT_PORT,
);
let mut state = QueryState::default();
match client.connect() {
Ok(mut stream) => {
if let Err(e) = client.authenticate(&mut stream, &mut startup_msg, DEFAULT_PASS) {
eprintln!("Client Authentication: {}", e);
return;
}
println!("Client connection\nUse Ctrl+c or type \"exit\" to end\n");
let mut result_buf = [0; 4096];
let mut row_descr = BytesMut::new();
loop {
// Okay to use stream to send queries at this point
print!("psql> ");
let _ = io::stdout().flush(); // Flush immediately so we can have query in single line
let mut msg = String::new();
io::stdin().read_line(&mut msg).expect("Error in read line");
msg = msg.trim().to_string();
if msg.len() <= 1 {
// Ignore empty. Account for newline char
continue;
}
match msg.cmp(&String::from("exit")) {
Ordering::Equal => break,
_ => (),
}
match send_simple_query(&mut stream, &msg) {
Some(e) => {
eprintln!("Query Error: {}", e);
return;
}
_ => (),
}
let buf_is_cleared = false;
loop {
stream
.set_read_timeout(Some(Duration::from_millis(100)))
.unwrap(); // Set a timeout to avoid blocking indefinitely
state.data_buf_off = 0;
match process_simple_query(
&mut stream,
&mut result_buf,
&mut row_descr,
&mut state,
) {
Ok(done) => {
if done {
row_descr.put_u8(b'\n');
if let Err(e) = io::stdout().write_all(&row_descr) {
eprintln!(
"Error when writing to stdout for RowDescription: {}",
e
);
}
if let Err(e) = io::stdout().write_all(&result_buf) {
eprintln!("Error when writing to stdout for DataRow: {}", e);
}
break;
}
}
Err(e) => {
eprintln!("Error processing simple query: {}", e);
return;
}
}
row_descr.put_u8(b'\n');
if let Err(e) = io::stdout().write_all(&row_descr) {
eprintln!("Error when writing to stdout for RowDescription: {}", e);
}
if let Err(e) = io::stdout().write_all(&result_buf[..state.data_buf_off]) {
eprintln!("Error when writing to stdout for DataRow: {}", e);
}
result_buf.fill(0);
if !buf_is_cleared {
// Clear only once
row_descr.clear();
}
}
}
}
Err(e) => {
eprintln!("No stream available for client: {}", e);
return;
}
}
}