Skip to content

Commit

Permalink
feat: add sqlparser::ast::Ident (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
iajoiner authored Oct 30, 2024
2 parents a9821ba + eeaf042 commit 26372d8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/proof-of-sql-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ chrono = { workspace = true, features = ["serde"] }
lalrpop-util = { workspace = true, features = ["lexer", "unicode"] }
serde = { workspace = true, features = ["serde_derive", "alloc"] }
snafu = { workspace = true }
sqlparser = { workspace = true }

[build-dependencies]
lalrpop = { workspace = true }
Expand Down
21 changes: 21 additions & 0 deletions crates/proof-of-sql-parser/src/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{sql::IdentifierParser, ParseError, ParseResult};
use alloc::{format, string::ToString};
use arrayvec::ArrayString;
use core::{cmp::Ordering, fmt, ops::Deref, str::FromStr};
use sqlparser::ast::Ident;

/// Top-level unique identifier.
#[derive(Debug, PartialEq, Eq, Clone, Hash, Ord, PartialOrd, Copy)]
Expand Down Expand Up @@ -71,6 +72,16 @@ impl fmt::Display for Identifier {
}
}

// TryFrom<Ident> for Identifier
impl TryFrom<Ident> for Identifier {
type Error = ParseError;

fn try_from(ident: Ident) -> ParseResult<Self> {
// Convert Ident's value to Identifier
Identifier::try_new(ident.value)
}
}

impl PartialEq<str> for Identifier {
fn eq(&self, other: &str) -> bool {
other.eq_ignore_ascii_case(&self.name)
Expand Down Expand Up @@ -278,4 +289,14 @@ mod tests {
Identifier::new("t".repeat(64));
Identifier::new("茶".repeat(21));
}

#[test]
fn try_from_ident() {
let ident = Ident::new("ValidIdentifier");
let identifier = Identifier::try_from(ident).unwrap();
assert_eq!(identifier.name(), "valididentifier");

let invalid_ident = Ident::new("INVALID$IDENTIFIER");
assert!(Identifier::try_from(invalid_ident).is_err());
}
}
29 changes: 29 additions & 0 deletions crates/proof-of-sql-parser/src/resource_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use crate::{impl_serde_from_str, sql::ResourceIdParser, Identifier, ParseError,
use alloc::{
format,
string::{String, ToString},
vec::Vec,
};
use core::{
fmt::{self, Display},
str::FromStr,
};
use sqlparser::ast::Ident;

/// Unique resource identifier, like `schema.object_name`.
#[derive(Debug, PartialEq, Eq, Clone, Hash, Copy)]
Expand Down Expand Up @@ -110,6 +112,22 @@ impl FromStr for ResourceId {
}
impl_serde_from_str!(ResourceId);

impl TryFrom<Vec<Ident>> for ResourceId {
type Error = ParseError;

fn try_from(identifiers: Vec<Ident>) -> ParseResult<Self> {
if identifiers.len() != 2 {
return Err(ParseError::ResourceIdParseError {
error: "Expected exactly two identifiers for ResourceId".to_string(),
});
}

let schema = Identifier::try_from(identifiers[0].clone())?;
let object_name = Identifier::try_from(identifiers[1].clone())?;
Ok(ResourceId::new(schema, object_name))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -233,4 +251,15 @@ mod tests {
serde_json::from_str(r#""good_identifier.bad!identifier"#);
assert!(deserialized.is_err());
}

#[test]
fn test_try_from_vec_ident() {
let identifiers = alloc::vec![Ident::new("schema_name"), Ident::new("object_name")];
let resource_id = ResourceId::try_from(identifiers).unwrap();
assert_eq!(resource_id.schema().name(), "schema_name");
assert_eq!(resource_id.object_name().name(), "object_name");

let invalid_identifiers = alloc::vec![Ident::new("only_one_ident")];
assert!(ResourceId::try_from(invalid_identifiers).is_err());
}
}

0 comments on commit 26372d8

Please sign in to comment.