Skip to content

Commit f00ed42

Browse files
committed
Merge remote-tracking branch 'origin/master' into exograph
2 parents 74eb4db + ded5e7d commit f00ed42

File tree

12 files changed

+82
-50
lines changed

12 files changed

+82
-50
lines changed

postgres-protocol/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ macro_rules! from_usize {
6060
impl FromUsize for $t {
6161
#[inline]
6262
fn from_usize(x: usize) -> io::Result<$t> {
63-
if x > <$t>::max_value() as usize {
63+
if x > <$t>::MAX as usize {
6464
Err(io::Error::new(
6565
io::ErrorKind::InvalidInput,
6666
"value too large to transmit",

postgres-protocol/src/message/backend.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ impl CopyOutResponseBody {
524524
}
525525
}
526526

527-
#[derive(Debug)]
527+
#[derive(Debug, Clone)]
528528
pub struct DataRowBody {
529529
storage: Bytes,
530530
len: u16,
@@ -633,7 +633,7 @@ impl<'a> FallibleIterator for ErrorFields<'a> {
633633
}
634634

635635
let value_end = find_null(self.buf, 0)?;
636-
let value = get_str(&self.buf[..value_end])?;
636+
let value = &self.buf[..value_end];
637637
self.buf = &self.buf[value_end + 1..];
638638

639639
Ok(Some(ErrorField { type_, value }))
@@ -642,7 +642,7 @@ impl<'a> FallibleIterator for ErrorFields<'a> {
642642

643643
pub struct ErrorField<'a> {
644644
type_: u8,
645-
value: &'a str,
645+
value: &'a [u8],
646646
}
647647

648648
impl<'a> ErrorField<'a> {
@@ -652,7 +652,13 @@ impl<'a> ErrorField<'a> {
652652
}
653653

654654
#[inline]
655+
#[deprecated(note = "use value_bytes instead", since = "0.6.7")]
655656
pub fn value(&self) -> &str {
657+
str::from_utf8(self.value).expect("error field value contained non-UTF8 bytes")
658+
}
659+
660+
#[inline]
661+
pub fn value_bytes(&self) -> &[u8] {
656662
self.value
657663
}
658664
}

postgres-types/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ categories = ["database"]
1313
[features]
1414
derive = ["postgres-derive"]
1515
array-impls = ["array-init"]
16+
js = ["postgres-protocol/js"]
1617
with-bit-vec-0_6 = ["bit-vec-06"]
1718
with-cidr-0_2 = ["cidr-02"]
1819
with-chrono-0_4 = ["chrono-04"]

postgres-types/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ impl ToSql for IpAddr {
12221222
}
12231223

12241224
fn downcast(len: usize) -> Result<i32, Box<dyn Error + Sync + Send>> {
1225-
if len > i32::max_value() as usize {
1225+
if len > i32::MAX as usize {
12261226
Err("value too large to transmit".into())
12271227
} else {
12281228
Ok(len as i32)

postgres-types/src/special.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use bytes::BytesMut;
22
use postgres_protocol::types;
33
use std::error::Error;
4-
use std::{i32, i64};
54

65
use crate::{FromSql, IsNull, ToSql, Type};
76

tokio-postgres/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Disable `rustc-serialize` compatibility of `eui48-1` dependency
66
* Remove tests for `eui48-04`
77
* Add `table_oid` and `field_id` fields to `Columns` struct of prepared statements.
8+
* Add `GenericClient::simple_query`.
89

910
## v0.7.10 - 2023-08-25
1011

tokio-postgres/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ with-uuid-0_8 = ["postgres-types/with-uuid-0_8"]
4040
with-uuid-1 = ["postgres-types/with-uuid-1"]
4141
with-time-0_2 = ["postgres-types/with-time-0_2"]
4242
with-time-0_3 = ["postgres-types/with-time-0_3"]
43-
js = ["postgres-protocol/js"]
43+
js = ["postgres-protocol/js", "postgres-types/js"]
4444

4545
[dependencies]
4646
async-trait = "0.1"

tokio-postgres/src/config.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ impl Config {
248248
/// Sets the user to authenticate with.
249249
///
250250
/// Defaults to the user executing this process.
251-
pub fn user(&mut self, user: &str) -> &mut Config {
252-
self.user = Some(user.to_string());
251+
pub fn user(&mut self, user: impl Into<String>) -> &mut Config {
252+
self.user = Some(user.into());
253253
self
254254
}
255255

@@ -277,8 +277,8 @@ impl Config {
277277
/// Sets the name of the database to connect to.
278278
///
279279
/// Defaults to the user.
280-
pub fn dbname(&mut self, dbname: &str) -> &mut Config {
281-
self.dbname = Some(dbname.to_string());
280+
pub fn dbname(&mut self, dbname: impl Into<String>) -> &mut Config {
281+
self.dbname = Some(dbname.into());
282282
self
283283
}
284284

@@ -289,8 +289,8 @@ impl Config {
289289
}
290290

291291
/// Sets command line options used to configure the server.
292-
pub fn options(&mut self, options: &str) -> &mut Config {
293-
self.options = Some(options.to_string());
292+
pub fn options(&mut self, options: impl Into<String>) -> &mut Config {
293+
self.options = Some(options.into());
294294
self
295295
}
296296

@@ -301,8 +301,8 @@ impl Config {
301301
}
302302

303303
/// Sets the value of the `application_name` runtime parameter.
304-
pub fn application_name(&mut self, application_name: &str) -> &mut Config {
305-
self.application_name = Some(application_name.to_string());
304+
pub fn application_name(&mut self, application_name: impl Into<String>) -> &mut Config {
305+
self.application_name = Some(application_name.into());
306306
self
307307
}
308308

@@ -330,15 +330,17 @@ impl Config {
330330
/// Multiple hosts can be specified by calling this method multiple times, and each will be tried in order. On Unix
331331
/// systems, a host starting with a `/` is interpreted as a path to a directory containing Unix domain sockets.
332332
/// There must be either no hosts, or the same number of hosts as hostaddrs.
333-
pub fn host(&mut self, host: &str) -> &mut Config {
333+
pub fn host(&mut self, host: impl Into<String>) -> &mut Config {
334+
let host = host.into();
335+
334336
#[cfg(unix)]
335337
{
336338
if host.starts_with('/') {
337339
return self.host_path(host);
338340
}
339341
}
340342

341-
self.host.push(Host::Tcp(host.to_string()));
343+
self.host.push(Host::Tcp(host));
342344
self
343345
}
344346

@@ -990,7 +992,7 @@ impl<'a> UrlParser<'a> {
990992

991993
let mut it = creds.splitn(2, ':');
992994
let user = self.decode(it.next().unwrap())?;
993-
self.config.user(&user);
995+
self.config.user(user);
994996

995997
if let Some(password) = it.next() {
996998
let password = Cow::from(percent_encoding::percent_decode(password.as_bytes()));
@@ -1053,7 +1055,7 @@ impl<'a> UrlParser<'a> {
10531055
};
10541056

10551057
if !dbname.is_empty() {
1056-
self.config.dbname(&self.decode(dbname)?);
1058+
self.config.dbname(self.decode(dbname)?);
10571059
}
10581060

10591061
Ok(())

tokio-postgres/src/error/mod.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,47 +107,48 @@ impl DbError {
107107
let mut routine = None;
108108

109109
while let Some(field) = fields.next()? {
110+
let value = String::from_utf8_lossy(field.value_bytes());
110111
match field.type_() {
111-
b'S' => severity = Some(field.value().to_owned()),
112-
b'C' => code = Some(SqlState::from_code(field.value())),
113-
b'M' => message = Some(field.value().to_owned()),
114-
b'D' => detail = Some(field.value().to_owned()),
115-
b'H' => hint = Some(field.value().to_owned()),
112+
b'S' => severity = Some(value.into_owned()),
113+
b'C' => code = Some(SqlState::from_code(&value)),
114+
b'M' => message = Some(value.into_owned()),
115+
b'D' => detail = Some(value.into_owned()),
116+
b'H' => hint = Some(value.into_owned()),
116117
b'P' => {
117-
normal_position = Some(field.value().parse::<u32>().map_err(|_| {
118+
normal_position = Some(value.parse::<u32>().map_err(|_| {
118119
io::Error::new(
119120
io::ErrorKind::InvalidInput,
120121
"`P` field did not contain an integer",
121122
)
122123
})?);
123124
}
124125
b'p' => {
125-
internal_position = Some(field.value().parse::<u32>().map_err(|_| {
126+
internal_position = Some(value.parse::<u32>().map_err(|_| {
126127
io::Error::new(
127128
io::ErrorKind::InvalidInput,
128129
"`p` field did not contain an integer",
129130
)
130131
})?);
131132
}
132-
b'q' => internal_query = Some(field.value().to_owned()),
133-
b'W' => where_ = Some(field.value().to_owned()),
134-
b's' => schema = Some(field.value().to_owned()),
135-
b't' => table = Some(field.value().to_owned()),
136-
b'c' => column = Some(field.value().to_owned()),
137-
b'd' => datatype = Some(field.value().to_owned()),
138-
b'n' => constraint = Some(field.value().to_owned()),
139-
b'F' => file = Some(field.value().to_owned()),
133+
b'q' => internal_query = Some(value.into_owned()),
134+
b'W' => where_ = Some(value.into_owned()),
135+
b's' => schema = Some(value.into_owned()),
136+
b't' => table = Some(value.into_owned()),
137+
b'c' => column = Some(value.into_owned()),
138+
b'd' => datatype = Some(value.into_owned()),
139+
b'n' => constraint = Some(value.into_owned()),
140+
b'F' => file = Some(value.into_owned()),
140141
b'L' => {
141-
line = Some(field.value().parse::<u32>().map_err(|_| {
142+
line = Some(value.parse::<u32>().map_err(|_| {
142143
io::Error::new(
143144
io::ErrorKind::InvalidInput,
144145
"`L` field did not contain an integer",
145146
)
146147
})?);
147148
}
148-
b'R' => routine = Some(field.value().to_owned()),
149+
b'R' => routine = Some(value.into_owned()),
149150
b'V' => {
150-
parsed_severity = Some(Severity::from_str(field.value()).ok_or_else(|| {
151+
parsed_severity = Some(Severity::from_str(&value).ok_or_else(|| {
151152
io::Error::new(
152153
io::ErrorKind::InvalidInput,
153154
"`V` field contained an invalid value",

tokio-postgres/src/generic_client.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::query::RowStream;
22
use crate::types::{BorrowToSql, ToSql, Type};
3-
use crate::{Client, Error, Row, Statement, ToStatement, Transaction};
3+
use crate::{Client, Error, Row, SimpleQueryMessage, Statement, ToStatement, Transaction};
44
use async_trait::async_trait;
55

66
mod private {
@@ -12,25 +12,25 @@ mod private {
1212
/// This trait is "sealed", and cannot be implemented outside of this crate.
1313
#[async_trait]
1414
pub trait GenericClient: private::Sealed {
15-
/// Like `Client::execute`.
15+
/// Like [`Client::execute`].
1616
async fn execute<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
1717
where
1818
T: ?Sized + ToStatement + Sync + Send;
1919

20-
/// Like `Client::execute_raw`.
20+
/// Like [`Client::execute_raw`].
2121
async fn execute_raw<P, I, T>(&self, statement: &T, params: I) -> Result<u64, Error>
2222
where
2323
T: ?Sized + ToStatement + Sync + Send,
2424
P: BorrowToSql,
2525
I: IntoIterator<Item = P> + Sync + Send,
2626
I::IntoIter: ExactSizeIterator;
2727

28-
/// Like `Client::query`.
28+
/// Like [`Client::query`].
2929
async fn query<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<Vec<Row>, Error>
3030
where
3131
T: ?Sized + ToStatement + Sync + Send;
3232

33-
/// Like `Client::query_one`.
33+
/// Like [`Client::query_one`].
3434
async fn query_one<T>(
3535
&self,
3636
statement: &T,
@@ -39,7 +39,7 @@ pub trait GenericClient: private::Sealed {
3939
where
4040
T: ?Sized + ToStatement + Sync + Send;
4141

42-
/// Like `Client::query_opt`.
42+
/// Like [`Client::query_opt`].
4343
async fn query_opt<T>(
4444
&self,
4545
statement: &T,
@@ -48,38 +48,41 @@ pub trait GenericClient: private::Sealed {
4848
where
4949
T: ?Sized + ToStatement + Sync + Send;
5050

51-
/// Like `Client::query_raw`.
51+
/// Like [`Client::query_raw`].
5252
async fn query_raw<T, P, I>(&self, statement: &T, params: I) -> Result<RowStream, Error>
5353
where
5454
T: ?Sized + ToStatement + Sync + Send,
5555
P: BorrowToSql,
5656
I: IntoIterator<Item = P> + Sync + Send,
5757
I::IntoIter: ExactSizeIterator;
5858

59-
/// Like `Client::query_with_param_types`
59+
/// Like [`Client::query_with_param_types`]
6060
async fn query_with_param_types(
6161
&self,
6262
statement: &str,
6363
params: &[(&(dyn ToSql + Sync), Type)],
6464
) -> Result<Vec<Row>, Error>;
6565

66-
/// Like `Client::prepare`.
66+
/// Like [`Client::prepare`].
6767
async fn prepare(&self, query: &str) -> Result<Statement, Error>;
6868

69-
/// Like `Client::prepare_typed`.
69+
/// Like [`Client::prepare_typed`].
7070
async fn prepare_typed(
7171
&self,
7272
query: &str,
7373
parameter_types: &[Type],
7474
) -> Result<Statement, Error>;
7575

76-
/// Like `Client::transaction`.
76+
/// Like [`Client::transaction`].
7777
async fn transaction(&mut self) -> Result<Transaction<'_>, Error>;
7878

79-
/// Like `Client::batch_execute`.
79+
/// Like [`Client::batch_execute`].
8080
async fn batch_execute(&self, query: &str) -> Result<(), Error>;
8181

82-
/// Returns a reference to the underlying `Client`.
82+
/// Like [`Client::simple_query`].
83+
async fn simple_query(&self, query: &str) -> Result<Vec<SimpleQueryMessage>, Error>;
84+
85+
/// Returns a reference to the underlying [`Client`].
8386
fn client(&self) -> &Client;
8487
}
8588

@@ -171,6 +174,10 @@ impl GenericClient for Client {
171174
self.batch_execute(query).await
172175
}
173176

177+
async fn simple_query(&self, query: &str) -> Result<Vec<SimpleQueryMessage>, Error> {
178+
self.simple_query(query).await
179+
}
180+
174181
fn client(&self) -> &Client {
175182
self
176183
}
@@ -266,6 +273,10 @@ impl GenericClient for Transaction<'_> {
266273
self.batch_execute(query).await
267274
}
268275

276+
async fn simple_query(&self, query: &str) -> Result<Vec<SimpleQueryMessage>, Error> {
277+
self.simple_query(query).await
278+
}
279+
269280
fn client(&self) -> &Client {
270281
self.client()
271282
}

tokio-postgres/src/row.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ where
9595
}
9696

9797
/// A row of data returned from the database by a query.
98+
#[derive(Clone)]
9899
pub struct Row {
99100
statement: Statement,
100101
body: DataRowBody,

tokio-postgres/src/statement.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ impl Statement {
7474
}
7575
}
7676

77+
impl std::fmt::Debug for Statement {
78+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
79+
f.debug_struct("Statement")
80+
.field("name", &self.0.name)
81+
.field("params", &self.0.params)
82+
.field("columns", &self.0.columns)
83+
.finish_non_exhaustive()
84+
}
85+
}
86+
7787
/// Information about a column of a query.
7888
#[derive(Debug)]
7989
pub struct Column {

0 commit comments

Comments
 (0)