Use postgres instead of pg for the module name

This commit is contained in:
Ryan Leckey 2019-08-21 12:20:06 -07:00
parent 9ed276d454
commit aefcdd23fa
47 changed files with 126 additions and 123 deletions

View File

@ -10,13 +10,13 @@ use fake::{
Dummy, Fake, Faker,
};
use futures::{channel::oneshot::channel, future, stream::TryStreamExt};
use sqlx::{pg::Pg, Pool};
use sqlx::{Postgres, Pool};
use std::{
io,
time::{Duration, Instant},
};
type PgPool = Pool<Pg>;
type PostgresPool = Pool<Postgres>;
#[derive(Debug, Dummy)]
struct Contact {
@ -40,7 +40,7 @@ struct Contact {
async fn main() -> Fallible<()> {
env_logger::try_init()?;
let pool = PgPool::new("postgres://postgres@127.0.0.1/sqlx__dev", 85);
let pool = PostgresPool::new("postgres://postgres@127.0.0.1/sqlx__dev", 85);
ensure_schema(&pool).await?;
insert(&pool, 50_000).await?;
@ -49,7 +49,7 @@ async fn main() -> Fallible<()> {
Ok(())
}
async fn ensure_schema(pool: &PgPool) -> io::Result<()> {
async fn ensure_schema(pool: &PostgresPool) -> io::Result<()> {
sqlx::query(
r#"
CREATE TABLE IF NOT EXISTS contacts (
@ -71,7 +71,7 @@ CREATE TABLE IF NOT EXISTS contacts (
Ok(())
}
async fn insert(pool: &PgPool, count: usize) -> io::Result<()> {
async fn insert(pool: &PostgresPool, count: usize) -> io::Result<()> {
let start_at = Instant::now();
let mut handles = vec![];
@ -111,7 +111,7 @@ async fn insert(pool: &PgPool, count: usize) -> io::Result<()> {
Ok(())
}
async fn select(pool: &PgPool, iterations: usize) -> io::Result<()> {
async fn select(pool: &PostgresPool, iterations: usize) -> io::Result<()> {
let start_at = Instant::now();
let mut rows: usize = 0;

View File

@ -2,7 +2,7 @@
use failure::Fallible;
use futures::{future, TryStreamExt};
use sqlx::{pg::Pg, Connection};
use sqlx::{Postgres, Connection};
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
@ -26,7 +26,7 @@ async fn main() -> Fallible<()> {
let opt = Options::from_args();
let mut conn = Connection::<Pg>::establish("postgres://postgres@127.0.0.1/sqlx__dev").await?;
let mut conn = Connection::<Postgres>::establish("postgres://postgres@127.0.0.1/sqlx__dev").await?;
ensure_schema(&mut conn).await?;
@ -47,7 +47,7 @@ async fn main() -> Fallible<()> {
Ok(())
}
async fn ensure_schema(conn: &mut Connection<Pg>) -> Fallible<()> {
async fn ensure_schema(conn: &mut Connection<Postgres>) -> Fallible<()> {
sqlx::query("BEGIN").execute(conn).await?;
// language=sql
@ -69,7 +69,7 @@ CREATE TABLE IF NOT EXISTS tasks (
Ok(())
}
async fn print_all_tasks(conn: &mut Connection<Pg>) -> Fallible<()> {
async fn print_all_tasks(conn: &mut Connection<Postgres>) -> Fallible<()> {
// language=sql
sqlx::query(
r#"
@ -90,7 +90,7 @@ WHERE done_at IS NULL
Ok(())
}
async fn add_task(conn: &mut Connection<Pg>, text: &str) -> Fallible<()> {
async fn add_task(conn: &mut Connection<Postgres>, text: &str) -> Fallible<()> {
// language=sql
sqlx::query(
r#"
@ -105,7 +105,7 @@ VALUES ( $1 )
Ok(())
}
async fn mark_task_as_done(conn: &mut Connection<Pg>, id: i64) -> Fallible<()> {
async fn mark_task_as_done(conn: &mut Connection<Postgres>, id: i64) -> Fallible<()> {
// language=sql
sqlx::query(
r#"

View File

@ -24,7 +24,10 @@ pub mod types;
pub mod mariadb;
#[cfg(feature = "postgres")]
pub mod pg;
pub mod postgres;
#[cfg(feature = "postgres")]
pub use self::postgres::Postgres;
mod connection;
mod executor;

View File

@ -1,15 +0,0 @@
use crate::backend::{Backend, BackendAssocRawQuery};
pub struct Pg;
impl<'q> BackendAssocRawQuery<'q, Pg> for Pg {
type RawQuery = super::PgRawQuery<'q>;
}
impl Backend for Pg {
type RawConnection = super::PgRawConnection;
type Row = super::PgRow;
}
// Generates tuple FromSqlRow impls for this backend
impl_from_sql_row_tuples_for_backend!(Pg);

15
src/postgres/backend.rs Normal file
View File

@ -0,0 +1,15 @@
use crate::backend::{Backend, BackendAssocRawQuery};
pub struct Postgres;
impl<'q> BackendAssocRawQuery<'q, Postgres> for Postgres {
type RawQuery = super::PostgresRawQuery<'q>;
}
impl Backend for Postgres {
type RawConnection = super::PostgresRawConnection;
type Row = super::PostgresRow;
}
// Generates tuple FromSqlRow impls for this backend
impl_from_sql_row_tuples_for_backend!(Postgres);

View File

@ -1,9 +1,9 @@
use super::PgRawConnection;
use crate::pg::protocol::{Authentication, Message, PasswordMessage, StartupMessage};
use super::PostgresRawConnection;
use crate::postgres::protocol::{Authentication, Message, PasswordMessage, StartupMessage};
use std::io;
use url::Url;
pub async fn establish<'a, 'b: 'a>(conn: &'a mut PgRawConnection, url: &'b Url) -> io::Result<()> {
pub async fn establish<'a, 'b: 'a>(conn: &'a mut PostgresRawConnection, url: &'b Url) -> io::Result<()> {
let user = url.username();
let password = url.password().unwrap_or("");
let database = url.path().trim_start_matches('/');

View File

@ -1,8 +1,8 @@
use super::PgRawConnection;
use crate::pg::protocol::Message;
use super::PostgresRawConnection;
use crate::postgres::protocol::Message;
use std::io;
pub async fn execute(conn: &mut PgRawConnection) -> io::Result<u64> {
pub async fn execute(conn: &mut PostgresRawConnection) -> io::Result<u64> {
conn.flush().await?;
let mut rows = 0;

View File

@ -1,11 +1,11 @@
use super::{PgRawConnection, PgRow};
use crate::pg::protocol::Message;
use super::{PostgresRawConnection, PostgresRow};
use crate::postgres::protocol::Message;
use futures_core::stream::Stream;
use std::io;
pub fn fetch<'a>(
conn: &'a mut PgRawConnection,
) -> impl Stream<Item = Result<PgRow, io::Error>> + 'a {
conn: &'a mut PostgresRawConnection,
) -> impl Stream<Item = Result<PostgresRow, io::Error>> + 'a {
async_stream::try_stream! {
conn.flush().await?;
@ -18,7 +18,7 @@ pub fn fetch<'a>(
| Message::CommandComplete(_) => {}
Message::DataRow(body) => {
yield PgRow(body);
yield PostgresRow(body);
}
Message::ReadyForQuery(_) => {

View File

@ -1,11 +1,11 @@
use super::{PgRawConnection, PgRow};
use crate::pg::protocol::Message;
use super::{PostgresRawConnection, PostgresRow};
use crate::postgres::protocol::Message;
use std::io;
pub async fn fetch_optional<'a>(conn: &'a mut PgRawConnection) -> io::Result<Option<PgRow>> {
pub async fn fetch_optional<'a>(conn: &'a mut PostgresRawConnection) -> io::Result<Option<PostgresRow>> {
conn.flush().await?;
let mut row: Option<PgRow> = None;
let mut row: Option<PostgresRow> = None;
while let Some(message) = conn.receive().await? {
match message {
@ -16,7 +16,7 @@ pub async fn fetch_optional<'a>(conn: &'a mut PgRawConnection) -> io::Result<Opt
| Message::CommandComplete(_) => {}
Message::DataRow(body) => {
row = Some(PgRow(body));
row = Some(PostgresRow(body));
}
Message::ReadyForQuery(_) => {

View File

@ -1,6 +1,6 @@
use super::{
protocol::{Encode, Message, Terminate},
Pg, PgRow,
Postgres, PostgresRow,
};
use crate::{connection::RawConnection, query::RawQuery};
use bytes::{BufMut, BytesMut};
@ -20,7 +20,7 @@ mod execute;
mod fetch;
mod fetch_optional;
pub struct PgRawConnection {
pub struct PostgresRawConnection {
stream: TcpStream,
// Do we think that there is data in the read buffer to be decoded
@ -43,7 +43,7 @@ pub struct PgRawConnection {
secret_key: u32,
}
impl PgRawConnection {
impl PostgresRawConnection {
async fn establish(url: &str) -> io::Result<Self> {
// TODO: Handle errors
let url = Url::parse(url).unwrap();
@ -146,12 +146,12 @@ impl PgRawConnection {
}
}
impl RawConnection for PgRawConnection {
type Backend = Pg;
impl RawConnection for PostgresRawConnection {
type Backend = Postgres;
#[inline]
fn establish(url: &str) -> BoxFuture<io::Result<Self>> {
Box::pin(PgRawConnection::establish(url))
Box::pin(PostgresRawConnection::establish(url))
}
#[inline]
@ -168,7 +168,7 @@ impl RawConnection for PgRawConnection {
Box::pin(execute::execute(self))
}
fn fetch<'c, 'q, Q: 'q>(&'c mut self, query: Q) -> BoxStream<'c, io::Result<PgRow>>
fn fetch<'c, 'q, Q: 'q>(&'c mut self, query: Q) -> BoxStream<'c, io::Result<PostgresRow>>
where
Q: RawQuery<'q, Backend = Self::Backend>,
{
@ -180,7 +180,7 @@ impl RawConnection for PgRawConnection {
fn fetch_optional<'c, 'q, Q: 'q>(
&'c mut self,
query: Q,
) -> BoxFuture<'c, io::Result<Option<PgRow>>>
) -> BoxFuture<'c, io::Result<Option<PostgresRow>>>
where
Q: RawQuery<'q, Backend = Self::Backend>,
{

View File

@ -6,4 +6,4 @@ mod query;
mod row;
pub mod types;
pub use self::{backend::Pg, connection::PgRawConnection, query::PgRawQuery, row::PgRow};
pub use self::{backend::Postgres, connection::PostgresRawConnection, query::PostgresRawQuery, row::PostgresRow};

View File

@ -1,6 +1,6 @@
use super::{
protocol::{self, BufMut},
Pg, PgRawConnection,
Postgres, PostgresRawConnection,
};
use crate::{
query::RawQuery,
@ -9,7 +9,7 @@ use crate::{
};
use byteorder::{BigEndian, ByteOrder};
pub struct PgRawQuery<'q> {
pub struct PostgresRawQuery<'q> {
limit: i32,
query: &'q str,
// OIDs of the bind parameters
@ -18,8 +18,8 @@ pub struct PgRawQuery<'q> {
buf: Vec<u8>,
}
impl<'q> RawQuery<'q> for PgRawQuery<'q> {
type Backend = Pg;
impl<'q> RawQuery<'q> for PostgresRawQuery<'q> {
type Backend = Postgres;
fn new(query: &'q str) -> Self {
Self {
@ -41,7 +41,7 @@ impl<'q> RawQuery<'q> for PgRawQuery<'q> {
// TODO: When/if we receive types that do _not_ support BINARY, we need to check here
// TODO: There is no need to be explicit unless we are expecting mixed BINARY / TEXT
self.types.push(<Pg as HasSqlType<T>>::metadata().oid);
self.types.push(<Postgres as HasSqlType<T>>::metadata().oid);
let pos = self.buf.len();
self.buf.put_int_32(0);
@ -60,7 +60,7 @@ impl<'q> RawQuery<'q> for PgRawQuery<'q> {
self
}
fn finish(self, conn: &mut PgRawConnection) {
fn finish(self, conn: &mut PostgresRawConnection) {
conn.write(protocol::Parse {
portal: "",
query: self.query,

View File

@ -1,10 +1,10 @@
use super::{protocol::DataRow, Pg};
use super::{protocol::DataRow, Postgres};
use crate::row::Row;
pub struct PgRow(pub(crate) Box<DataRow>);
pub struct PostgresRow(pub(crate) Box<DataRow>);
impl Row for PgRow {
type Backend = Pg;
impl Row for PostgresRow {
type Backend = Postgres;
#[inline]
fn is_empty(&self) -> bool {

View File

@ -1,21 +1,21 @@
use super::{Pg, PgTypeMetadata, PgTypeFormat};
use super::{Postgres, PostgresTypeMetadata, PostgresTypeFormat};
use crate::{
deserialize::FromSql,
serialize::{IsNull, ToSql},
types::HasSqlType,
};
impl HasSqlType<bool> for Pg {
fn metadata() -> PgTypeMetadata {
PgTypeMetadata {
format: PgTypeFormat::Binary,
impl HasSqlType<bool> for Postgres {
fn metadata() -> PostgresTypeMetadata {
PostgresTypeMetadata {
format: PostgresTypeFormat::Binary,
oid: 16,
array_oid: 1000,
}
}
}
impl ToSql<Pg> for bool {
impl ToSql<Postgres> for bool {
#[inline]
fn to_sql(self, buf: &mut Vec<u8>) -> IsNull {
buf.push(self as u8);
@ -24,7 +24,7 @@ impl ToSql<Pg> for bool {
}
}
impl FromSql<Pg> for bool {
impl FromSql<Postgres> for bool {
#[inline]
fn from_sql(buf: Option<&[u8]>) -> Self {
// TODO: Handle optionals

View File

@ -1,4 +1,4 @@
use super::{Pg, PgTypeMetadata, PgTypeFormat};
use super::{Postgres, PostgresTypeMetadata, PostgresTypeFormat};
use crate::{
deserialize::FromSql,
serialize::{IsNull, ToSql},
@ -6,25 +6,25 @@ use crate::{
};
use std::str;
impl HasSqlType<&'_ str> for Pg {
impl HasSqlType<&'_ str> for Postgres {
#[inline]
fn metadata() -> PgTypeMetadata {
PgTypeMetadata {
format: PgTypeFormat::Binary,
fn metadata() -> PostgresTypeMetadata {
PostgresTypeMetadata {
format: PostgresTypeFormat::Binary,
oid: 25,
array_oid: 1009,
}
}
}
impl HasSqlType<String> for Pg {
impl HasSqlType<String> for Postgres {
#[inline]
fn metadata() -> PgTypeMetadata {
<Pg as HasSqlType<&str>>::metadata()
fn metadata() -> PostgresTypeMetadata {
<Postgres as HasSqlType<&str>>::metadata()
}
}
impl ToSql<Pg> for &'_ str {
impl ToSql<Postgres> for &'_ str {
#[inline]
fn to_sql(self, buf: &mut Vec<u8>) -> IsNull {
buf.extend_from_slice(self.as_bytes());
@ -33,14 +33,14 @@ impl ToSql<Pg> for &'_ str {
}
}
impl ToSql<Pg> for String {
impl ToSql<Postgres> for String {
#[inline]
fn to_sql(self, buf: &mut Vec<u8>) -> IsNull {
self.as_str().to_sql(buf)
}
}
impl FromSql<Pg> for String {
impl FromSql<Postgres> for String {
#[inline]
fn from_sql(buf: Option<&[u8]>) -> Self {
// TODO: Handle nulls

View File

@ -27,14 +27,14 @@
//! | `IpAddr` | INET |
//!
use super::Pg;
use super::Postgres;
use crate::types::TypeMetadata;
mod boolean;
mod character;
mod numeric;
pub enum PgTypeFormat {
pub enum PostgresTypeFormat {
Text = 0,
Binary = 1,
}
@ -44,12 +44,12 @@ pub enum PgTypeFormat {
///
/// While the BINARY format is preferred in most cases, there are scenarios
/// where only the TEXT format may be available for a type.
pub struct PgTypeMetadata {
pub format: PgTypeFormat,
pub struct PostgresTypeMetadata {
pub format: PostgresTypeFormat,
pub oid: u32,
pub array_oid: u32,
}
impl TypeMetadata for Pg {
type TypeMetadata = PgTypeMetadata;
impl TypeMetadata for Postgres {
type TypeMetadata = PostgresTypeMetadata;
}

View File

@ -1,4 +1,4 @@
use super::{Pg, PgTypeMetadata, PgTypeFormat};
use super::{Postgres, PostgresTypeMetadata, PostgresTypeFormat};
use crate::{
deserialize::FromSql,
serialize::{IsNull, ToSql},
@ -6,18 +6,18 @@ use crate::{
};
use byteorder::{BigEndian, ByteOrder};
impl HasSqlType<i16> for Pg {
impl HasSqlType<i16> for Postgres {
#[inline]
fn metadata() -> PgTypeMetadata {
PgTypeMetadata {
format: PgTypeFormat::Binary,
fn metadata() -> PostgresTypeMetadata {
PostgresTypeMetadata {
format: PostgresTypeFormat::Binary,
oid: 21,
array_oid: 1005,
}
}
}
impl ToSql<Pg> for i16 {
impl ToSql<Postgres> for i16 {
#[inline]
fn to_sql(self, buf: &mut Vec<u8>) -> IsNull {
buf.extend_from_slice(&self.to_be_bytes());
@ -26,25 +26,25 @@ impl ToSql<Pg> for i16 {
}
}
impl FromSql<Pg> for i16 {
impl FromSql<Postgres> for i16 {
#[inline]
fn from_sql(buf: Option<&[u8]>) -> Self {
BigEndian::read_i16(buf.unwrap())
}
}
impl HasSqlType<i32> for Pg {
impl HasSqlType<i32> for Postgres {
#[inline]
fn metadata() -> PgTypeMetadata {
PgTypeMetadata {
format: PgTypeFormat::Binary,
fn metadata() -> PostgresTypeMetadata {
PostgresTypeMetadata {
format: PostgresTypeFormat::Binary,
oid: 23,
array_oid: 1007,
}
}
}
impl ToSql<Pg> for i32 {
impl ToSql<Postgres> for i32 {
#[inline]
fn to_sql(self, buf: &mut Vec<u8>) -> IsNull {
buf.extend_from_slice(&self.to_be_bytes());
@ -53,25 +53,25 @@ impl ToSql<Pg> for i32 {
}
}
impl FromSql<Pg> for i32 {
impl FromSql<Postgres> for i32 {
#[inline]
fn from_sql(buf: Option<&[u8]>) -> Self {
BigEndian::read_i32(buf.unwrap())
}
}
impl HasSqlType<i64> for Pg {
impl HasSqlType<i64> for Postgres {
#[inline]
fn metadata() -> PgTypeMetadata {
PgTypeMetadata {
format: PgTypeFormat::Binary,
fn metadata() -> PostgresTypeMetadata {
PostgresTypeMetadata {
format: PostgresTypeFormat::Binary,
oid: 20,
array_oid: 1016,
}
}
}
impl ToSql<Pg> for i64 {
impl ToSql<Postgres> for i64 {
#[inline]
fn to_sql(self, buf: &mut Vec<u8>) -> IsNull {
buf.extend_from_slice(&self.to_be_bytes());
@ -80,57 +80,57 @@ impl ToSql<Pg> for i64 {
}
}
impl FromSql<Pg> for i64 {
impl FromSql<Postgres> for i64 {
#[inline]
fn from_sql(buf: Option<&[u8]>) -> Self {
BigEndian::read_i64(buf.unwrap())
}
}
impl HasSqlType<f32> for Pg {
impl HasSqlType<f32> for Postgres {
#[inline]
fn metadata() -> PgTypeMetadata {
PgTypeMetadata {
format: PgTypeFormat::Binary,
fn metadata() -> PostgresTypeMetadata {
PostgresTypeMetadata {
format: PostgresTypeFormat::Binary,
oid: 700,
array_oid: 1021,
}
}
}
impl ToSql<Pg> for f32 {
impl ToSql<Postgres> for f32 {
#[inline]
fn to_sql(self, buf: &mut Vec<u8>) -> IsNull {
(self.to_bits() as i32).to_sql(buf)
}
}
impl FromSql<Pg> for f32 {
impl FromSql<Postgres> for f32 {
#[inline]
fn from_sql(buf: Option<&[u8]>) -> Self {
f32::from_bits(i32::from_sql(buf) as u32)
}
}
impl HasSqlType<f64> for Pg {
impl HasSqlType<f64> for Postgres {
#[inline]
fn metadata() -> PgTypeMetadata {
PgTypeMetadata {
format: PgTypeFormat::Binary,
fn metadata() -> PostgresTypeMetadata {
PostgresTypeMetadata {
format: PostgresTypeFormat::Binary,
oid: 701,
array_oid: 1022,
}
}
}
impl ToSql<Pg> for f64 {
impl ToSql<Postgres> for f64 {
#[inline]
fn to_sql(self, buf: &mut Vec<u8>) -> IsNull {
(self.to_bits() as i64).to_sql(buf)
}
}
impl FromSql<Pg> for f64 {
impl FromSql<Postgres> for f64 {
#[inline]
fn from_sql(buf: Option<&[u8]>) -> Self {
f64::from_bits(i64::from_sql(buf) as u64)

View File

@ -33,21 +33,21 @@ pub trait HasSqlType<A>: TypeMetadata {
// Example of what that derive should generate
// impl HasSqlType<Bool> for Pg {
// impl HasSqlType<Bool> for Postgres {
// #[inline]
// fn metadata() -> PgTypeMetadata {
// <Pg as HasSqlType<bool>>::metadata()
// fn metadata() -> PostgresTypeMetadata {
// <Postgres as HasSqlType<bool>>::metadata()
// }
// }
// impl ToSql<Pg> for Bool {
// impl ToSql<Postgres> for Bool {
// #[inline]
// fn to_sql(self, buf: &mut Vec<u8>) -> IsNull {
// self.0.to_sql(buf)
// }
// }
// impl FromSql<Pg> for bool {
// impl FromSql<Postgres> for bool {
// #[inline]
// fn from_sql(buf: Option<&[u8]>) -> Self {
// Self(bool::from_sql(buf))