Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split api feature #177

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
tests-integration/test-server/target
tests-integration/rust-client/target
node_modules
.DS_Store
65 changes: 40 additions & 25 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,52 @@ readme = "README.md"
rust-version = "1.67"

[dependencies]
log = "0.4"
derive-new = "0.6"
bytes = "1.1.0"
time = "0.3"
futures = "0.3"
async-trait = "0.1"
rand = "0.8"
thiserror = "1"
postgres-types = { version = "0.2", features = ["with-chrono-0_4", "array-impls"]}
md5 = "0.7"
hex = "0.4"
## scram libraries
base64 = "0.22"
ring = "0.17"
stringprep = "0.1.2"
x509-certificate = "0.23"

tokio = { version = "1.19", features = ["net", "rt", "io-util"], optional = true}
## api
tokio = { version = "1.19", features = [
"net",
"rt",
"io-util",
], optional = true }
tokio-util = { version = "0.7.3", features = ["codec", "io"], optional = true }
tokio-rustls = { version = "0.26", optional = true }
futures = { version = "0.3", optional = true }
async-trait = { version = "0.1", optional = true }
rand = { version = "0.8", optional = true }
md5 = { version = "0.7", optional = true }
hex = { version = "0.4", optional = true }
## scram libraries
base64 = { version = "0.22", optional = true }
ring = { version = "0.17", optional = true }
stringprep = { version = "0.1.2", optional = true }
x509-certificate = { version = "0.23", optional = true }
## types
postgres-types = { version = "0.2", features = [
"with-chrono-0_4",
"array-impls",
], optional = true }
chrono = { version = "0.4", features = ["std"], optional = true }

chrono = { version = "0.4", optional = true, features = ["std"] }
[features]
default = ["api"]
api = [
"dep:tokio",
"dep:tokio-util",
"dep:tokio-rustls",
"dep:futures",
"dep:async-trait",
"dep:rand",
"dep:md5",
"dep:hex",
"dep:base64",
"dep:ring",
"dep:stringprep",
"dep:x509-certificate",
"dep:postgres-types",
"dep:chrono",
]

[dev-dependencies]
tokio = { version = "1.19", features = ["rt-multi-thread", "net", "macros"]}
Expand All @@ -50,15 +74,6 @@ rustls-pki-types = "1.0"
## rustls-native-certs loads roots from current system
gluesql = { version = "0.15", default-features = false, features = ["memory-storage"] }

[features]
default = ["tokio", "time-format"]
tokio = ["dep:tokio", "dep:tokio-util", "dep:tokio-rustls"]
time-format = ["dep:chrono"]

[[example]]
name = "server"
required-features = ["tokio"]

[workspace]
members = [
".",
Expand Down
10 changes: 4 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::messages::response::{ErrorResponse, NoticeResponse};
use std::io::{Error as IOError, ErrorKind};

use postgres_types::Oid;
use thiserror::Error;

use crate::messages::response::{ErrorResponse, NoticeResponse};

#[derive(Error, Debug)]
pub enum PgWireError {
#[error("Invalid protocol version, received {0}")]
Expand All @@ -23,8 +20,9 @@ pub enum PgWireError {
PortalNotFound(String),
#[error("Statement not found for name: {0:?}")]
StatementNotFound(String),
#[error("Unknown type: {0:?}")]
UnknownTypeId(Oid),
#[cfg(feature = "api")]
#[error("Unknown type: {0}")]
UnknownTypeId(u32),
#[error("Parameter index out of bound: {0:?}")]
ParameterIndexOutOfBound(usize),
#[error("Cannot convert postgre type {0:?} to given rust type")]
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@
extern crate derive_new;

/// handler layer and high-level API layer.
#[cfg(feature = "api")]
pub mod api;
/// error types.
pub mod error;
/// the protocol layer.
pub mod messages;
/// server entry-point for tokio based application.
#[cfg(feature = "tokio")]
#[cfg(feature = "api")]
pub mod tokio;
/// types and encoding related helper
#[cfg(feature = "api")]
pub mod types;
11 changes: 5 additions & 6 deletions src/messages/data.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use bytes::{Buf, BufMut, BytesMut};
use postgres_types::Oid;

use super::codec;
use super::Message;
use crate::error::PgWireResult;

pub(crate) const FORMAT_CODE_TEXT: i16 = 0;
pub(crate) const FORMAT_CODE_BINARY: i16 = 1;
pub const FORMAT_CODE_TEXT: i16 = 0;
pub const FORMAT_CODE_BINARY: i16 = 1;

#[non_exhaustive]
#[derive(PartialEq, Eq, Debug, Default, new)]
Expand All @@ -18,7 +17,7 @@ pub struct FieldDescription {
// the attribute number of the column, default to 0 if not a column from table
pub column_id: i16,
// the object ID of the data type
pub type_id: Oid,
pub type_id: u32,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pub type Oid = u32;

Oid is just an alias for u32, to avoid introducing postgres_types, we restore it here.

// the size of data type, negative values denote variable-width types
pub type_size: i16,
// the type modifier
Expand Down Expand Up @@ -92,7 +91,7 @@ impl Message for RowDescription {
#[derive(PartialEq, Eq, Debug, Default, new, Clone)]
pub struct ParameterDescription {
/// parameter types
pub types: Vec<Oid>,
pub types: Vec<u32>,
}

pub const MESSAGE_TYPE_BYTE_PARAMETER_DESCRITION: u8 = b't';
Expand Down Expand Up @@ -121,7 +120,7 @@ impl Message for ParameterDescription {
let mut types = Vec::with_capacity(types_len as usize);

for _ in 0..types_len {
types.push(buf.get_i32() as Oid);
types.push(buf.get_i32() as u32);
}

Ok(ParameterDescription { types })
Expand Down
3 changes: 1 addition & 2 deletions src/messages/extendedquery.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bytes::{Buf, BufMut, Bytes};
use postgres_types::Oid;

use super::{codec, Message};
use crate::error::PgWireResult;
Expand All @@ -10,7 +9,7 @@ use crate::error::PgWireResult;
pub struct Parse {
pub name: Option<String>,
pub query: String,
pub type_oids: Vec<Oid>,
pub type_oids: Vec<u32>,
}

pub const MESSAGE_TYPE_BYTE_PARSE: u8 = b'P';
Expand Down
Loading