Skip to content

Commit 90eb58d

Browse files
committed
Some prep for custom type lookup
1 parent f9e4651 commit 90eb58d

4 files changed

Lines changed: 59 additions & 23 deletions

File tree

tokio-postgres/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ with-serde_json-1 = ["serde-1", "serde_json-1"]
3131
"with-uuid-0_7" = ["uuid-07"]
3232

3333
[dependencies]
34-
antidote = "1.0"
3534
bytes = "0.4"
3635
fallible-iterator = "0.2"
3736
futures-preview = { version = "0.3.0-alpha.17", features = ["nightly", "async-await"] }
3837
log = "0.4"
38+
parking_lot = "0.9"
3939
percent-encoding = "1.0"
4040
phf = "0.7.23"
4141
postgres-protocol = { version = "0.4.1", path = "../postgres-protocol" }

tokio-postgres/src/client.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use crate::codec::BackendMessages;
22
use crate::connection::{Request, RequestMessages};
33
use crate::prepare::prepare;
4-
use crate::types::Type;
4+
use crate::types::{Oid, Type};
55
use crate::{Error, Statement};
66
use fallible_iterator::FallibleIterator;
77
use futures::channel::mpsc;
88
use futures::{Stream, StreamExt};
9+
use parking_lot::Mutex;
910
use postgres_protocol::message::backend::Message;
11+
use std::collections::HashMap;
1012
use std::future::Future;
1113
use std::pin::Pin;
1214
use std::sync::Arc;
@@ -34,8 +36,16 @@ impl Responses {
3436
}
3537
}
3638

39+
struct State {
40+
has_typeinfo: bool,
41+
has_typeinfo_composite: bool,
42+
has_typeinfo_enum: bool,
43+
types: HashMap<Oid, Type>,
44+
}
45+
3746
pub struct InnerClient {
3847
sender: mpsc::UnboundedSender<Request>,
48+
state: Mutex<State>,
3949
}
4050

4151
impl InnerClient {
@@ -51,6 +61,38 @@ impl InnerClient {
5161
cur: BackendMessages::empty(),
5262
})
5363
}
64+
65+
pub fn has_typeinfo(&self) -> bool {
66+
self.state.lock().has_typeinfo
67+
}
68+
69+
pub fn set_has_typeinfo(&self) {
70+
self.state.lock().has_typeinfo = true;
71+
}
72+
73+
pub fn has_typeinfo_composite(&self) -> bool {
74+
self.state.lock().has_typeinfo_composite
75+
}
76+
77+
pub fn set_has_typeinfo_composite(&self) {
78+
self.state.lock().has_typeinfo_composite = true;
79+
}
80+
81+
pub fn has_typeinfo_enum(&self) -> bool {
82+
self.state.lock().has_typeinfo_enum
83+
}
84+
85+
pub fn set_has_typeinfo_enum(&self) {
86+
self.state.lock().has_typeinfo_enum = true;
87+
}
88+
89+
pub fn type_(&self, oid: Oid) -> Option<Type> {
90+
self.state.lock().types.get(&oid).cloned()
91+
}
92+
93+
pub fn set_type(&self, oid: Oid, type_: Type) {
94+
self.state.lock().types.insert(oid, type_);
95+
}
5496
}
5597

5698
pub struct Client {
@@ -66,7 +108,15 @@ impl Client {
66108
secret_key: i32,
67109
) -> Client {
68110
Client {
69-
inner: Arc::new(InnerClient { sender }),
111+
inner: Arc::new(InnerClient {
112+
sender,
113+
state: Mutex::new(State {
114+
has_typeinfo: false,
115+
has_typeinfo_composite: false,
116+
has_typeinfo_enum: false,
117+
types: HashMap::new(),
118+
}),
119+
}),
70120
process_id,
71121
secret_key,
72122
}

tokio-postgres/src/prepare.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,14 @@ pub async fn prepare(
6262
Ok(Statement::new(&client, name, parameters, columns))
6363
}
6464

65-
async fn get_type(client: &InnerClient, oid: Oid) -> Result<Type, Error> {
65+
async fn get_type(client: &Arc<InnerClient>, oid: Oid) -> Result<Type, Error> {
6666
if let Some(type_) = Type::from_oid(oid) {
6767
return Ok(type_);
6868
}
6969

70+
if let Some(type_) = client.type_(oid) {
71+
return Ok(type_);
72+
}
73+
7074
unimplemented!()
7175
}

tokio-postgres/tests/test/main.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use futures::{try_join, FutureExt};
55
use tokio::net::TcpStream;
66
use tokio_postgres::error::SqlState;
77
use tokio_postgres::tls::{NoTls, NoTlsStream};
8-
use tokio_postgres::{Client, Config, Connection, Error};
98
use tokio_postgres::types::Type;
9+
use tokio_postgres::{Client, Config, Connection, Error};
1010

1111
mod parse;
1212
#[cfg(feature = "runtime")]
@@ -113,24 +113,6 @@ async fn pipelined_prepare() {
113113
}
114114

115115
/*
116-
#[test]
117-
fn pipelined_prepare() {
118-
let _ = env_logger::try_init();
119-
let mut runtime = Runtime::new().unwrap();
120-
121-
let (mut client, connection) = runtime.block_on(connect("user=postgres")).unwrap();
122-
let connection = connection.map_err(|e| panic!("{}", e));
123-
runtime.handle().spawn(connection).unwrap();
124-
125-
let prepare1 = client.prepare("SELECT $1::HSTORE[]");
126-
let prepare2 = client.prepare("SELECT $1::HSTORE[]");
127-
let prepare = prepare1.join(prepare2);
128-
runtime.block_on(prepare).unwrap();
129-
130-
drop(client);
131-
runtime.run().unwrap();
132-
}
133-
134116
#[test]
135117
fn insert_select() {
136118
let _ = env_logger::try_init();

0 commit comments

Comments
 (0)