removing decimal with precision and decimal count
This commit is contained in:
@@ -37,17 +37,12 @@ pub fn parse_decimal_exact(value: &str) -> Result<Decimal, String> {
|
||||
Decimal::from_str_exact(value).map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
/// True for the `data_type` spellings `GetTableStructure` reports for a decimal
|
||||
/// column: `NUMERIC` (from `numeric` and `money`), `NUMERIC(p)` and
|
||||
/// `NUMERIC(p,s)` (from `decimal(p,s)`).
|
||||
/// True for the `NUMERIC` data type reported by `GetTableStructure`.
|
||||
pub fn is_decimal_data_type(data_type: &str) -> bool {
|
||||
data_type
|
||||
.trim()
|
||||
.to_ascii_uppercase()
|
||||
.starts_with(DECIMAL_DATA_TYPE_PREFIX)
|
||||
data_type.trim().eq_ignore_ascii_case(DECIMAL_DATA_TYPE)
|
||||
}
|
||||
|
||||
const DECIMAL_DATA_TYPE_PREFIX: &str = "NUMERIC";
|
||||
const DECIMAL_DATA_TYPE: &str = "NUMERIC";
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
@@ -101,11 +96,10 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decimal_data_type_covers_every_numeric_spelling() {
|
||||
for data_type in ["NUMERIC", "NUMERIC(12)", "NUMERIC(12,3)", "numeric(12,3)"] {
|
||||
assert!(is_decimal_data_type(data_type), "missed {data_type}");
|
||||
}
|
||||
for data_type in ["TEXT", "INT8", "TIMESTAMPTZ", "VARCHAR(255)", ""] {
|
||||
fn decimal_data_type_matches_only_unconstrained_numeric() {
|
||||
assert!(is_decimal_data_type("NUMERIC"));
|
||||
assert!(is_decimal_data_type("numeric"));
|
||||
for data_type in ["NUMERIC(12)", "NUMERIC(12,3)", "TEXT", "INT8", ""] {
|
||||
assert!(!is_decimal_data_type(data_type), "matched {data_type}");
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -239,15 +239,12 @@ pub struct ColumnDefinition {
|
||||
/// DATE
|
||||
/// DURATION
|
||||
/// PERIOD
|
||||
/// DECIMAL(p,s) → NUMERIC(p,s)
|
||||
/// LINK(table) → BIGINT referencing that table in the same profile, indexed
|
||||
/// automatically. A table may hold several links to the same
|
||||
/// target as long as the columns are named differently.
|
||||
/// FROM(link.source) → stored, read-only copy of an atomic column reached
|
||||
/// through another column in this definition whose type is
|
||||
/// LINK(...). SQL type and money metadata are inferred.
|
||||
/// DECIMAL args must be integers (no sign, no dot, no leading zeros);
|
||||
/// s ≤ p and p ≥ 1.
|
||||
#[prost(string, tag = "2")]
|
||||
pub field_type: ::prost::alloc::string::String,
|
||||
/// MONEY rounding applied before a value is stored.
|
||||
@@ -584,8 +581,7 @@ pub mod list_column_types_response {
|
||||
#[prost(string, tag = "1")]
|
||||
pub name: ::prost::alloc::string::String,
|
||||
/// Underlying PostgreSQL type the logical type maps to (e.g. "NUMERIC").
|
||||
/// Empty when `compound` is true. For COLUMN_TYPE_SPELLING_DECIMAL this is
|
||||
/// the unparameterised type; the declared precision and scale are appended.
|
||||
/// Empty when `compound` is true.
|
||||
#[prost(string, tag = "2")]
|
||||
pub sql_type: ::prost::alloc::string::String,
|
||||
/// False for types the server generates on its own and rejects when a client
|
||||
@@ -682,10 +678,6 @@ impl MoneyRounding {
|
||||
pub enum ColumnTypeSpelling {
|
||||
/// The name is the whole spelling: "text", "money", "gtin_13".
|
||||
Bare = 0,
|
||||
/// The name takes a precision and a scale: "decimal(12,3)". Precision must be
|
||||
/// at least 1 and scale no greater than precision; neither may carry a sign,
|
||||
/// a decimal point, or leading zeros.
|
||||
Decimal = 1,
|
||||
/// The name takes the name of another table in the same profile:
|
||||
/// "link(adresar)". The column holds that table's id, and the server creates
|
||||
/// the foreign key and its index. A picker offers the profile's other tables
|
||||
@@ -700,7 +692,6 @@ impl ColumnTypeSpelling {
|
||||
pub fn as_str_name(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Bare => "COLUMN_TYPE_SPELLING_BARE",
|
||||
Self::Decimal => "COLUMN_TYPE_SPELLING_DECIMAL",
|
||||
Self::Link => "COLUMN_TYPE_SPELLING_LINK",
|
||||
}
|
||||
}
|
||||
@@ -708,7 +699,6 @@ impl ColumnTypeSpelling {
|
||||
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
|
||||
match value {
|
||||
"COLUMN_TYPE_SPELLING_BARE" => Some(Self::Bare),
|
||||
"COLUMN_TYPE_SPELLING_DECIMAL" => Some(Self::Decimal),
|
||||
"COLUMN_TYPE_SPELLING_LINK" => Some(Self::Link),
|
||||
_ => None,
|
||||
}
|
||||
|
||||
@@ -93,9 +93,7 @@ pub fn normalize_exact(input: &str) -> String {
|
||||
/// names, so this deliberately matches the catalog vocabulary.
|
||||
pub fn canonical_exact_search_value(input: &str, field_type: &str) -> Result<String, String> {
|
||||
let normalized_type = field_type.trim().to_ascii_lowercase();
|
||||
if matches!(normalized_type.as_str(), "numeric" | "money")
|
||||
|| normalized_type.starts_with("decimal(")
|
||||
{
|
||||
if matches!(normalized_type.as_str(), "numeric" | "money") {
|
||||
return input
|
||||
.parse::<rust_decimal::Decimal>()
|
||||
.map(|value| value.normalize().to_string())
|
||||
@@ -288,10 +286,6 @@ mod tests {
|
||||
assert_eq!(canonical_exact_search_value("001", "link(adresar)").unwrap(), "1");
|
||||
assert_eq!(canonical_exact_search_value("10.50", "numeric").unwrap(), "10.5");
|
||||
assert_eq!(canonical_exact_search_value("10.50", "money").unwrap(), "10.5");
|
||||
assert_eq!(
|
||||
canonical_exact_search_value("10.50", "decimal(12, 2)").unwrap(),
|
||||
"10.5"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,9 +41,7 @@ pub fn parse_archived_search_row_key(row_key: &str) -> Option<(i64, i64, i64)> {
|
||||
|
||||
pub fn canonical_exact_search_value(input: &str, field_type: &str) -> Result<String, String> {
|
||||
let normalized_type = field_type.trim().to_ascii_lowercase();
|
||||
if matches!(normalized_type.as_str(), "numeric" | "money")
|
||||
|| normalized_type.starts_with("decimal(")
|
||||
{
|
||||
if matches!(normalized_type.as_str(), "numeric" | "money") {
|
||||
return input
|
||||
.parse::<rust_decimal::Decimal>()
|
||||
.map(|value| value.normalize().to_string())
|
||||
@@ -100,9 +98,5 @@ mod tests {
|
||||
canonical_exact_search_value("10.50", "money").unwrap(),
|
||||
"10.5"
|
||||
);
|
||||
assert_eq!(
|
||||
canonical_exact_search_value("10.50", "decimal(12, 2)").unwrap(),
|
||||
"10.5"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user