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

Add ColumnType::Inet support for the prepared statements #80

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Changes from all 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
29 changes: 29 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io;
use std::io::{BufRead, BufReader, ErrorKind, Read};
use std::net::IpAddr;
use std::str::FromStr;
use std::sync::Arc;

use anyhow::anyhow;
Expand Down Expand Up @@ -197,6 +199,7 @@ pub enum CassErrorKind {
ValueOutOfRange(String, ColumnType),
InvalidNumberOfQueryParams,
InvalidQueryParamsObject(TypeInfo),
WrongDataStructure(String),
Prepare(String, QueryError),
Overloaded(QueryInfo, QueryError),
QueryExecution(QueryInfo, QueryError),
Expand Down Expand Up @@ -233,6 +236,9 @@ impl CassError {
CassErrorKind::InvalidQueryParamsObject(t) => {
write!(buf, "Value of type {t} cannot by used as query parameters; expected a list or object")
}
CassErrorKind::WrongDataStructure(s) => {
write!(buf, "Wrong data structure: {s}")
}
CassErrorKind::Prepare(q, e) => {
write!(buf, "Failed to prepare query \"{q}\": {e}")
}
Expand Down Expand Up @@ -590,6 +596,29 @@ mod bind {
(Value::String(v), ColumnType::Text | ColumnType::Ascii) => {
Ok(CqlValue::Text(v.borrow_ref().unwrap().as_str().to_string()))
}
(Value::StaticString(v), ColumnType::Inet) => {
let ipaddr = IpAddr::from_str(v);
match ipaddr {
Ok(ipaddr) => Ok(CqlValue::Inet(ipaddr)),
Err(e) => {
Err(CassError(CassErrorKind::WrongDataStructure(
format!("Failed to parse '{}' StaticString as IP address: {}", v.as_str(), e),
)))
}
}
}
(Value::String(v), ColumnType::Inet) => {
let ipaddr_str = v.borrow_ref().unwrap();
let ipaddr = IpAddr::from_str(ipaddr_str.as_str());
match ipaddr {
Ok(ipaddr) => Ok(CqlValue::Inet(ipaddr)),
Err(e) => {
Err(CassError(CassErrorKind::WrongDataStructure(
format!("Failed to parse '{}' String as IP address: {}", ipaddr_str.as_str(), e),
)))
}
}
}

(Value::Bytes(v), ColumnType::Blob) => {
Ok(CqlValue::Blob(v.borrow_ref().unwrap().to_vec()))
Expand Down