Skip to content
This repository has been archived by the owner on Apr 13, 2021. It is now read-only.

Commit

Permalink
adjust code extraction for 0.4.0 version (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-dukhno authored Nov 5, 2020
1 parent 6775c33 commit adea868
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pg_wire"
version = "0.3.1"
version = "0.4.0"
description = "Server Side implementation of PostgreSQL Wire Protocol"
license = "Apache-2.0"
repository = "https://github.com/alex-dukhno/pg_wire"
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use hand_shake::{Process as HandShakeProcess, Request as HandShakeRequest, S
pub use message_decoder::{MessageDecoder, Status as MessageDecoderStatus};
pub use messages::{BackendMessage, ColumnMetadata, FrontendMessage};
pub use result::{Error, Result};
pub use types::{NotSupportedOid, PgType, Value};
pub use types::{PgType, Value};

mod cursor;
mod format;
Expand Down
14 changes: 7 additions & 7 deletions src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub enum FrontendMessage {
sql: String,
/// The number of specified parameter data types can be less than the
/// number of parameters specified in the query.
param_types: Vec<u32>,
param_types: Vec<PgType>,
},

/// Describe an existing prepared statement.
Expand Down Expand Up @@ -375,11 +375,11 @@ pub struct ColumnMetadata {

impl ColumnMetadata {
/// Creates new column metadata
pub fn new<S: ToString>(name: S, type_id: u32, type_size: i16) -> ColumnMetadata {
pub fn new<S: ToString>(name: S, pg_type: PgType) -> ColumnMetadata {
Self {
name: name.to_string(),
type_id,
type_size,
type_id: pg_type.type_oid(),
type_size: pg_type.type_len(),
}
}
}
Expand Down Expand Up @@ -470,7 +470,7 @@ fn decode_parse(mut cursor: Cursor) -> Result<FrontendMessage> {

let mut param_types = vec![];
for _ in 0..cursor.read_i16()? {
let oid = cursor.read_u32()?;
let oid = PgType::try_from(cursor.read_u32()?)?;
log::trace!("OID {:?}", oid);
param_types.push(oid);
}
Expand Down Expand Up @@ -613,7 +613,7 @@ mod decoding_frontend_messages {
Ok(FrontendMessage::Parse {
statement_name: "".to_owned(),
sql: "select * from schema_name.table_name where si_column = $1;".to_owned(),
param_types: vec![23],
param_types: vec![PgType::Integer],
})
);
}
Expand Down Expand Up @@ -726,7 +726,7 @@ mod serializing_backend_messages {
#[test]
fn row_description() {
assert_eq!(
BackendMessage::RowDescription(vec![ColumnMetadata::new("c1".to_owned(), 23, 4)]).as_vec(),
BackendMessage::RowDescription(vec![ColumnMetadata::new("c1".to_owned(), PgType::Integer)]).as_vec(),
vec![
ROW_DESCRIPTION,
0,
Expand Down
8 changes: 8 additions & 0 deletions src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::types::NotSupportedOid;

/// Protocol operation result
pub type Result<T> = std::result::Result<T, Error>;

Expand All @@ -37,3 +39,9 @@ pub enum Error {
/// Indicates that connection verification is failed
VerificationFailed,
}

impl From<NotSupportedOid> for Error {
fn from(error: NotSupportedOid) -> Error {
Error::InvalidInput(error.to_string())
}
}
8 changes: 7 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::{
};

/// Represents PostgreSQL data type and methods to send over wire
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum PgType {
/// Represents PostgreSQL `smallint` data type
SmallInt,
Expand Down Expand Up @@ -132,6 +132,12 @@ impl TryFrom<Oid> for PgType {
}
}

impl Display for NotSupportedOid {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{} OID is not supported", self.0)
}
}

fn parse_bigint_from_binary(buf: &mut Cursor) -> Result<Value, String> {
let v = match buf.read_i64() {
Ok(v) => v,
Expand Down

0 comments on commit adea868

Please sign in to comment.