use std::collections::BTreeSet;
use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Tier {
Exact,
Emulated,
Conditional,
Refused,
}
impl Tier {
pub fn name(self) -> &'static str {
match self {
Tier::Exact => "exact",
Tier::Emulated => "emulated",
Tier::Conditional => "conditional",
Tier::Refused => "refused",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Allowance {
NowAsText,
TimestampAsText,
EpochWholeSeconds,
TimestampWholeSeconds,
TimestampMillisecondResolution,
ReplaceIsUpsert,
SourceCollationNotBytewise,
PipesAsLogicalOr,
IgnoreCoversOnlyUniqueConflicts,
SubstringStartAssumedPositive,
DateAsText,
LikeCaseSensitivityIsConnectionSetting,
AsciiOnlyCaseFolding,
LenientCast,
DivisionByZeroIsNull,
TextCollation,
BooleanAsInteger,
TimestampArithmeticAsText,
JsonScalarAffinity,
JsonRepresentationAsText,
DecimalDivisionAsFloat,
ArrayNullSemantics,
IntegerDivisionAssumed,
UpsertAnyUniqueKey,
AggregateStringTruncation,
RegexpFlavor,
BitwiseNonNegativeIntegers,
XorOperandsBoolean,
}
impl Allowance {
pub const ALL: &[Allowance] = &[
Allowance::NowAsText,
Allowance::TimestampAsText,
Allowance::EpochWholeSeconds,
Allowance::TimestampWholeSeconds,
Allowance::TimestampMillisecondResolution,
Allowance::ReplaceIsUpsert,
Allowance::SourceCollationNotBytewise,
Allowance::PipesAsLogicalOr,
Allowance::IgnoreCoversOnlyUniqueConflicts,
Allowance::SubstringStartAssumedPositive,
Allowance::DateAsText,
Allowance::LikeCaseSensitivityIsConnectionSetting,
Allowance::AsciiOnlyCaseFolding,
Allowance::LenientCast,
Allowance::DivisionByZeroIsNull,
Allowance::TextCollation,
Allowance::BooleanAsInteger,
Allowance::TimestampArithmeticAsText,
Allowance::JsonScalarAffinity,
Allowance::JsonRepresentationAsText,
Allowance::IntegerDivisionAssumed,
Allowance::UpsertAnyUniqueKey,
Allowance::AggregateStringTruncation,
Allowance::RegexpFlavor,
Allowance::BitwiseNonNegativeIntegers,
Allowance::XorOperandsBoolean,
Allowance::ArrayNullSemantics,
Allowance::DecimalDivisionAsFloat,
];
pub fn name(self) -> &'static str {
match self {
Allowance::NowAsText => "now_as_text",
Allowance::TimestampAsText => "timestamp_as_text",
Allowance::EpochWholeSeconds => "epoch_whole_seconds",
Allowance::TimestampWholeSeconds => "timestamp_whole_seconds",
Allowance::TimestampMillisecondResolution => "timestamp_millisecond_resolution",
Allowance::ReplaceIsUpsert => "replace_is_upsert",
Allowance::SourceCollationNotBytewise => "source_collation_not_bytewise",
Allowance::PipesAsLogicalOr => "pipes_as_logical_or",
Allowance::IgnoreCoversOnlyUniqueConflicts => "ignore_covers_only_unique_conflicts",
Allowance::SubstringStartAssumedPositive => "substring_start_assumed_positive",
Allowance::DateAsText => "date_as_text",
Allowance::LikeCaseSensitivityIsConnectionSetting => {
"like_case_sensitivity_is_connection_setting"
}
Allowance::AsciiOnlyCaseFolding => "ascii_only_case_folding",
Allowance::LenientCast => "lenient_cast",
Allowance::DivisionByZeroIsNull => "division_by_zero_is_null",
Allowance::TextCollation => "text_collation",
Allowance::BooleanAsInteger => "boolean_as_integer",
Allowance::TimestampArithmeticAsText => "timestamp_arithmetic_as_text",
Allowance::JsonScalarAffinity => "json_scalar_affinity",
Allowance::JsonRepresentationAsText => "json_representation_as_text",
Allowance::ArrayNullSemantics => "array_null_semantics",
Allowance::DecimalDivisionAsFloat => "decimal_division_as_float",
Allowance::IntegerDivisionAssumed => "integer_division_assumed",
Allowance::UpsertAnyUniqueKey => "upsert_any_unique_key",
Allowance::AggregateStringTruncation => "aggregate_string_truncation",
Allowance::RegexpFlavor => "regexp_flavor",
Allowance::BitwiseNonNegativeIntegers => "bitwise_non_negative_integers",
Allowance::XorOperandsBoolean => "xor_operands_boolean",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Entry {
pub construct: String,
pub tier: Tier,
pub note: String,
pub allowance: Option<Allowance>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Report {
pub entries: Vec<Entry>,
}
impl Report {
pub fn push(&mut self, e: Entry) {
if !self.entries.contains(&e) {
self.entries.push(e);
}
}
pub fn worst(&self) -> Tier {
self.entries
.iter()
.map(|e| e.tier)
.max()
.unwrap_or(Tier::Exact)
}
pub fn constructs(&self) -> BTreeSet<&str> {
self.entries.iter().map(|e| e.construct.as_str()).collect()
}
pub fn tier_of(&self, construct: &str) -> Option<Tier> {
self.entries
.iter()
.filter(|e| e.construct == construct)
.map(|e| e.tier)
.max()
}
}
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct Options {
pub allow: BTreeSet<Allowance>,
pub schema: Option<crate::schema::Schema>,
}
impl Options {
pub fn with_schema(mut self, schema: crate::schema::Schema) -> Self {
self.schema = Some(schema);
self
}
pub fn with(mut self, a: Allowance) -> Self {
self.allow.insert(a);
self
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Untranslatable {
pub construct: String,
pub source: String,
pub target: String,
pub reason: String,
pub alternatives: Vec<String>,
}
impl Untranslatable {
pub fn subject(&self) -> String {
self.construct.clone()
}
}
impl fmt::Display for Untranslatable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"untranslatable `{}` ({} -> {}): {}",
self.construct, self.source, self.target, self.reason
)?;
if !self.alternatives.is_empty() {
write!(f, " [alternatives: {}]", self.alternatives.join("; "))?;
}
Ok(())
}
}
impl std::error::Error for Untranslatable {}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ReadError {
Syntax(String),
NotInIr { construct: String, detail: String },
}
impl fmt::Display for ReadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ReadError::Syntax(s) => write!(f, "syntax error: {s}"),
ReadError::NotInIr { construct, detail } => {
write!(f, "construct `{construct}` is not in the IR: {detail}")
}
}
}
}
impl std::error::Error for ReadError {}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ParamBinding {
Scalar,
NativeArray,
JsonArrayText,
}
impl ParamBinding {
pub fn name(self) -> &'static str {
match self {
ParamBinding::Scalar => "scalar",
ParamBinding::NativeArray => "native_array",
ParamBinding::JsonArrayText => "json_array_text",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Translation {
pub sql: String,
pub param_count: u32,
pub param_order: Vec<u32>,
pub param_bindings: Vec<ParamBinding>,
pub report: Report,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReadNote {
pub construct: String,
pub note: String,
pub allowance: Allowance,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Read {
pub stmt: crate::ast::Statement,
pub notes: Vec<ReadNote>,
}