| 113 | |
| 114 | impl PgConn { |
| 115 | fn new<'a>( |
| 116 | addr: &str, |
| 117 | user: &'a str, |
| 118 | timeout: Duration, |
| 119 | verbose: bool, |
| 120 | mut options: Vec<(&'a str, &'a str)>, |
| 121 | ) -> anyhow::Result<Self> { |
| 122 | let mut conn = Self { |
| 123 | stream: TcpStream::connect(addr)?, |
| 124 | recv_buf: BytesMut::new(), |
| 125 | send_buf: BytesMut::new(), |
| 126 | timeout, |
| 127 | verbose, |
| 128 | }; |
| 129 | |
| 130 | conn.stream.set_read_timeout(Some(timeout))?; |
| 131 | options.insert(0, ("user", user)); |
| 132 | options.insert(0, ("welcome_message", "off")); |
| 133 | conn.send(|buf| frontend::startup_message(options, buf).unwrap())?; |
| 134 | match conn.recv()?.1 { |
| 135 | Message::AuthenticationOk => {} |
| 136 | _ => bail!("expected AuthenticationOk"), |
| 137 | }; |
| 138 | conn.until(vec!["ReadyForQuery"], vec!['C', 'S', 'M'], BTreeSet::new())?; |
| 139 | Ok(conn) |
| 140 | } |
| 141 | |
| 142 | fn send<F: FnOnce(&mut BytesMut)>(&mut self, f: F) -> anyhow::Result<()> { |
| 143 | self.send_buf.clear(); |