postgres_sync 0.1.2

A synchronous PostgreSQL client using std::net, with an API compatible with the popular postgres crate.
Documentation
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! A synchronous, drop-in replacement for the `postgres` crate.
//!
//! This crate provides a compatible API but uses standard library networking instead of `tokio`.
//!
//! For detailed documentation on individual functions and types, please refer to the
//! [original `postgres` crate documentation](https://docs.rs/postgres/latest/postgres/).
//!
//! **Note:** `postgres_sync` implements a *subset* of the `postgres` API. If you find a
//! feature in the `postgres` docs, it may not yet be implemented in this crate.

use std::error::Error as StdError;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::time::Duration;

use bytes::BytesMut;
use fallible_iterator::FallibleIterator;
use postgres_protocol::Oid;
use postgres_protocol::authentication::{
    md5_hash,
    sasl::{self, ChannelBinding, ScramSha256},
};
use postgres_protocol::message::backend;
use postgres_protocol::message::frontend;
use postgres_types::{IsNull, Type};
use socket2::{SockRef, TcpKeepalive};

pub use fallible_iterator;
pub use postgres_types::{BorrowToSql, FromSql, ToSql};
pub use postgres_types as types;

pub use crate::transaction::Transaction;
pub use crate::config::Config;

mod config;
mod transaction;

pub type Error = Box<dyn StdError + Send + Sync>;

#[derive(Debug)]
pub struct DbError {
    severity: String,
    code: String,
    message: String,
    detail: Option<String>,
    hint: Option<String>,
    position: Option<ErrorPosition>,
}

impl std::fmt::Display for DbError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {} ({})", self.severity, self.message, self.code)?;
        if let Some(detail) = &self.detail {
            write!(f, "\nDETAIL: {detail}")?;
        }
        if let Some(hint) = &self.hint {
            write!(f, "\nHINT: {hint}")?;
        }
        if let Some(pos) = &self.position {
            write!(f, "\nPOSITION: {pos:?}")?;
        }
        Ok(())
    }
}

impl StdError for DbError {}

impl DbError {
    fn parse(mut fields: backend::ErrorFields<'_>) -> Self {
    let mut severity = String::new();
    let mut code = String::new();
    let mut message = String::new();
    let mut detail = None;
    let mut hint = None;
    let mut normal_position = None;
    let mut internal_position = None;
    let mut internal_query = None;
    while let Some(field) = fields.next().unwrap() {
        match field.type_() {
            b'S' => severity = String::from_utf8_lossy(field.value_bytes()).into_owned(),
            b'C' => code = String::from_utf8_lossy(field.value_bytes()).into_owned(),
            b'M' => message = String::from_utf8_lossy(field.value_bytes()).into_owned(),
            b'D' => detail = Some(String::from_utf8_lossy(field.value_bytes()).into_owned()),
            b'H' => hint = Some(String::from_utf8_lossy(field.value_bytes()).into_owned()),
            b'P' => normal_position = String::from_utf8_lossy(field.value_bytes()).parse().ok(),
            b'p' => internal_position = String::from_utf8_lossy(field.value_bytes()).parse().ok(),
            b'q' => internal_query = Some(String::from_utf8_lossy(field.value_bytes()).into_owned()),
            _ => {}
        }
    }
    let position = match normal_position {
        Some(pos) => Some(ErrorPosition::Original(pos)),
        None => internal_position.map(|pos| ErrorPosition::Internal {
            position: pos,
            query: internal_query.unwrap_or_default(),
        }),
    };
    Self { severity, code, message, detail, hint, position }
    }
}

#[derive(Debug)]
pub enum ErrorPosition {
    Original(u32),
    Internal { position: u32, query: String },
}

#[derive(Debug, Clone, Copy)]
pub struct NoTls;

pub struct Client {
    stream: TcpStream,
    read_buf: BytesMut,
    write_buf: BytesMut,
}

impl Client {
    pub fn connect(s: &str, _tls: NoTls) -> Result<Client, Error> {
        let config = config::Config::parse(s)?;
        Self::connect_config(&config, _tls)
    }

    fn connect_config(config: &config::Config, _tls: NoTls) -> Result<Client, Error> {
        let stream = TcpStream::connect((config.host.as_str(), config.port))?;

        let sock_ref = SockRef::from(&stream);
        let keepalive = TcpKeepalive::new().with_time(Duration::from_secs(50));
        sock_ref.set_tcp_keepalive(&keepalive)?;

        let user = &config.user;
        let db = &config.db;

        let mut this = Client {
            stream,
            read_buf: BytesMut::with_capacity(8192),
            write_buf: BytesMut::with_capacity(8192),
        };

        let mut params: Vec<(&str, &str)> = Vec::new();
        params.push(("user", user));
        if !db.is_empty() {
            params.push(("database", db));
        }
        params.push(("client_encoding", "UTF8"));

        frontend::startup_message(params.iter().copied(), &mut this.write_buf)?;
        this.flush()?;

        this.handle_auth(user.as_bytes(), &config.password)?;

        loop {
            match this.read_message()? {
                backend::Message::ReadyForQuery(_) => break,
                backend::Message::BackendKeyData(_) => {}
                backend::Message::ParameterStatus(_) => {}
                backend::Message::ErrorResponse(body) => return Err(DbError::parse(body.fields()).into()),
                _ => return Err("unexpected message".into()),
            }
        }

        Ok(this)
    }

    fn handle_auth(&mut self, user: &[u8], password: &str) -> Result<(), Error> {
        loop {
            match self.read_message()? {
                backend::Message::AuthenticationOk => break,
                backend::Message::AuthenticationCleartextPassword => {
                    // TODO: untested
                    frontend::password_message(password.as_bytes(), &mut self.write_buf)?;
                    self.flush()?;
                }
                backend::Message::AuthenticationMd5Password(body) => {
                    // TODO: untested
                    let output = md5_hash(user, password.as_bytes(), body.salt());
                    frontend::password_message(output.as_bytes(), &mut self.write_buf)?;
                    self.flush()?;
                }
                backend::Message::AuthenticationSasl(body) => {
                    let mut has_scram = false;
                    let mut mechs = body.mechanisms();
                    while let Some(mech) = mechs.next()? {
                        if mech == sasl::SCRAM_SHA_256 {
                            has_scram = true;
                        }
                    }
                    if !has_scram {
                        return Err("unsupported authentication".into());
                    }

                    let mut scram =
                        ScramSha256::new(password.as_bytes(), ChannelBinding::unsupported());

                    frontend::sasl_initial_response(
                        sasl::SCRAM_SHA_256,
                        scram.message(),
                        &mut self.write_buf,
                    )?;
                    self.flush()?;

                    let body = match self.read_message()? {
                        backend::Message::AuthenticationSaslContinue(body) => body,
                        backend::Message::ErrorResponse(body) => return Err(DbError::parse(body.fields()).into()),
                        _ => return Err("unexpected message".into()),
                    };

                    scram.update(body.data())?;

                    frontend::sasl_response(scram.message(), &mut self.write_buf)?;
                    self.flush()?;

                    let body = match self.read_message()? {
                        backend::Message::AuthenticationSaslFinal(body) => body,
                        backend::Message::ErrorResponse(body) => return Err(DbError::parse(body.fields()).into()),
                        _ => return Err("unexpected message".into()),
                    };

                    scram.finish(body.data())?;
                }
                backend::Message::ErrorResponse(body) => {
                    return Err(DbError::parse(body.fields()).into());
                }
                _ => return Err("unsupported authentication".into()),
            }
        }
        Ok(())
    }

    fn flush(&mut self) -> Result<(), Error> {
        self.stream.write_all(&self.write_buf)?;
        self.stream.flush()?;
        self.write_buf.clear();
        Ok(())
    }

    fn read_message(&mut self) -> Result<backend::Message, Error> {
        loop {
            if let Some(message) = backend::Message::parse(&mut self.read_buf)? {
                if let backend::Message::NoticeResponse(body) = &message {
                    log::info!("postgres notice: {:?}", DbError::parse(body.fields()));
                    continue;
                }
                return Ok(message);
            }
            let mut buf = [0u8; 8192];
            let n = self.stream.read(&mut buf)?;
            if n == 0 {
                return Err("unexpected EOF".into());
            }
            self.read_buf.extend_from_slice(&buf[..n]);
        }
    }

    fn drain_ready(&mut self) -> Result<(), Error> {
        loop {
            match self.read_message()? {
                backend::Message::ReadyForQuery(_) => return Ok(()),
                backend::Message::ErrorResponse(body) => {
                    return Err(DbError::parse(body.fields()).into())
                }
                _ => {}
            }
        }
    }

    #[allow(clippy::type_complexity)]
    fn prepare_query(
        &mut self,
        query: &str,
        params_len: usize,
    ) -> Result<(Vec<Type>, Vec<(String, Oid)>), Error> {
        let param_oids = vec![0; params_len];
        frontend::parse("", query, param_oids.iter().copied(), &mut self.write_buf)?;
        frontend::describe(b'S', "", &mut self.write_buf)?;
        frontend::sync(&mut self.write_buf);
        self.flush()?;

        let mut param_types = Vec::new();
        let mut columns = Vec::new();
        loop {
            match self.read_message()? {
                backend::Message::ParseComplete => {}
                backend::Message::ParameterDescription(body) => {
                    let mut it = body.parameters();
                    while let Some(oid) = it.next()? {
                        let ty = Type::from_oid(oid).unwrap_or(Type::TEXT);
                        param_types.push(ty);
                    }
                }
                backend::Message::RowDescription(body) => {
                    let mut fields = body.fields();
                    while let Some(field) = fields.next()? {
                        columns.push((field.name().to_string(), field.type_oid()));
                    }
                }
                backend::Message::NoData => {}
                backend::Message::ReadyForQuery(_) => break,
                backend::Message::ErrorResponse(body) => {
                    let err = DbError::parse(body.fields());
                    self.drain_ready()?;
                    return Err(err.into());
                }
                _ => return Err("unexpected message".into()),
            }
        }

        Ok((param_types, columns))
    }

    fn bind_execute<P, I>(
        &mut self,
        params: I,
        param_types: &[Type],
        mut rows: Option<&mut Vec<Vec<Option<Vec<u8>>>>>,
    ) -> Result<u64, Error>
    where
        P: BorrowToSql,
        I: IntoIterator<Item = P>,
        I::IntoIter: ExactSizeIterator,
    {
        let params: Vec<P> = params.into_iter().collect();
        assert_eq!(param_types.len(), params.len());
        let param_formats: Vec<i16> = params
            .iter()
            .zip(param_types)
            .map(|(p, t)| p.borrow_to_sql().encode_format(t) as i16)
            .collect();

        frontend::bind(
            "",
            "",
            param_formats,
            params.iter().zip(param_types.iter()),
            |(param, ty), buf| match param.borrow_to_sql().to_sql_checked(ty, buf)? {
                IsNull::No => Ok(postgres_protocol::IsNull::No),
                IsNull::Yes => Ok(postgres_protocol::IsNull::Yes),
            },
            Some(1),
            &mut self.write_buf,
        )
        .map_err(|e| match e {
            frontend::BindError::Conversion(e) => e,
            frontend::BindError::Serialization(e) => Box::new(e) as Error,
        })?;
        frontend::execute("", 0, &mut self.write_buf)?;
        frontend::sync(&mut self.write_buf);
        self.flush()?;

        let mut rows_affected = 0;
        loop {
            match self.read_message()? {
                backend::Message::BindComplete => {}
                backend::Message::DataRow(body) => {
                    if let Some(out) = rows.as_mut() {
                        out.push(self.parse_data_row(body)?);
                    }
                }
                backend::Message::CommandComplete(body) => {
                    let tag = body.tag().map_err(|e| Box::new(e) as Error)?;
                    rows_affected = tag
                        .rsplit(' ')
                        .next()
                        .and_then(|s| s.parse().ok())
                        .unwrap_or(0);
                }
                backend::Message::EmptyQueryResponse => rows_affected = 0,
                backend::Message::ReadyForQuery(_) => return Ok(rows_affected),
                backend::Message::ErrorResponse(body) => {
                    let err = DbError::parse(body.fields());
                    self.drain_ready()?;
                    return Err(err.into());
                }
                _ => return Err("unexpected message".into()),
            }
        }
    }

    pub fn query_raw<P, I>(&mut self, query: &str, params: I) -> Result<RowIter, Error>
    where
        P: BorrowToSql,
        I: IntoIterator<Item = P>,
        I::IntoIter: ExactSizeIterator,
    {
        let params = params.into_iter();
        let (param_types, columns) = self.prepare_query(query, params.len())?;
        let params: Vec<P> = params.collect();
        let mut rows = Vec::new();
        self.bind_execute(params, &param_types, Some(&mut rows))?;

        Ok(RowIter {
            columns,
            rows: rows.into_iter(),
        })
    }

    pub fn execute(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error> {
        let (param_types, _) = self.prepare_query(query, params.len())?;
        self.bind_execute(params.iter().copied(), &param_types, None)
    }

    pub fn query(
        &mut self,
        query: &str,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<Vec<Row>, Error> {
        self.query_raw(query, params.iter().copied())?.collect()
    }

    pub fn query_one(&mut self, query: &str, params: &[&(dyn ToSql + Sync)]) -> Result<Row, Error> {
        let mut it = self.query_raw(query, params.iter().copied())?;
        let first = it.next()?.ok_or("no rows returned")?;
        if it.next()?.is_some() {
            return Err("more than one row returned".into());
        }
        Ok(first)
    }

    pub fn batch_execute(&mut self, query: &str) -> Result<(), Error> {
        frontend::query(query, &mut self.write_buf)?;
        self.flush()?;

        loop {
            match self.read_message()? {
                backend::Message::ReadyForQuery(_) => return Ok(()),
                backend::Message::CommandComplete(_)
                | backend::Message::EmptyQueryResponse
                | backend::Message::RowDescription(_)
                | backend::Message::DataRow(_) => {}
                backend::Message::ErrorResponse(body) => {
                    let err = DbError::parse(body.fields());
                    self.drain_ready()?;
                    return Err(err.into());
                }
                _ => return Err("unexpected message".into()),
            }
        }
    }

    fn parse_data_row(&self, body: backend::DataRowBody) -> Result<Vec<Option<Vec<u8>>>, Error> {
        let mut out = Vec::new();
        let mut ranges = body.ranges();
        let buf = body.buffer();
        while let Some(range) = ranges.next()? {
            match range {
                Some(r) => out.push(Some(buf[r].to_vec())),
                None => out.push(None),
            }
        }
        Ok(out)
    }
}

pub struct Row {
    columns: Vec<(String, Oid)>,
    values: Vec<Option<Vec<u8>>>,
}

pub trait RowIndex {
    fn idx(&self, columns: &[(String, Oid)]) -> Option<usize>;
}

impl RowIndex for usize {
    fn idx(&self, columns: &[(String, Oid)]) -> Option<usize> {
        if *self < columns.len() { Some(*self) } else { None }
    }
}

impl RowIndex for &str {
    fn idx(&self, columns: &[(String, Oid)]) -> Option<usize> {
        columns.iter()
            .position(|(name, _)| name == self)
        .or_else(|| columns.iter()
            .position(|(name, _)| name.eq_ignore_ascii_case(self)))
    }
}


impl Row {
    pub fn get<'a, T>(&'a self, idx: impl RowIndex) -> T
    where
        T: FromSql<'a>,
    {
        let idx = idx.idx(&self.columns).expect("invalid column");
        let (_, oid) = &self.columns[idx];
        let ty = Type::from_oid(*oid).unwrap_or(Type::TEXT);
        let raw = self.values[idx].as_deref();
        FromSql::from_sql_nullable(&ty, raw).unwrap()
    }
}

pub struct RowIter {
    columns: Vec<(String, Oid)>,
    rows: std::vec::IntoIter<Vec<Option<Vec<u8>>>>,
}

impl FallibleIterator for RowIter {
    type Item = Row;
    type Error = Error;

    fn next(&mut self) -> Result<Option<Row>, Error> {
        Ok(self.rows.next().map(|values| Row {
            columns: self.columns.clone(),
            values,
        }))
    }
}