Struct sqlx::postgres::types::PgMoney

source ·
pub struct PgMoney(pub i64);
Available on crate feature postgres only.
Expand description

The PostgreSQL MONEY type stores a currency amount with a fixed fractional precision. The fractional precision is determined by the database’s lc_monetary setting.

Data is read and written as 64-bit signed integers, and conversion into a decimal should be done using the right precision.

Reading MONEY value in text format is not supported and will cause an error.

§locale_frac_digits

This parameter corresponds to the number of digits after the decimal separator.

This value must match what Postgres is expecting for the locale set in the database or else the decimal value you see on the client side will not match the money value on the server side.

For most locales, this value is 2.

If you’re not sure what locale your database is set to or how many decimal digits it specifies, you can execute SHOW lc_monetary; to get the locale name, and then look it up in this list (you can ignore the .utf8 prefix): https://lh.2xlibre.net/values/frac_digits/

If that link is dead and you’re on a POSIX-compliant system (Unix, FreeBSD) you can also execute:

$ LC_MONETARY=<value returned by `SHOW lc_monetary`> locale -k frac_digits

And the value you want is N in frac_digits=N. If you have shell access to the database server you should execute it there as available locales may differ between machines.

Note that if frac_digits for the locale is outside the range [0, 10], Postgres assumes it’s a sentinel value and defaults to 2: https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/cash.c#L114-L123

Tuple Fields§

§0: i64

The raw integer value sent over the wire; for locales with frac_digits=2 (i.e. most of them), this will be the value in whole cents.

E.g. for select '$123.45'::money with a locale of en_US (frac_digits=2), this will be 12345.

If the currency of your locale does not have fractional units, e.g. Yen, then this will just be the units of the currency.

See the type-level docs for an explanation of locale_frac_units.

Implementations§

source§

impl PgMoney

source

pub fn to_bigdecimal(self, locale_frac_digits: i64) -> BigDecimal

Convert the money value into a BigDecimal using locale_frac_digits.

See the type-level docs for an explanation of locale_frac_digits.

source

pub fn to_decimal(self, locale_frac_digits: u32) -> Decimal

Convert the money value into a Decimal using locale_frac_digits.

See the type-level docs for an explanation of locale_frac_digits.

source

pub fn from_decimal(decimal: Decimal, locale_frac_digits: u32) -> PgMoney

Convert a Decimal value into money using locale_frac_digits.

See the type-level docs for an explanation of locale_frac_digits.

Note that Decimal has 96 bits of precision, but PgMoney only has 63 plus the sign bit. If the value is larger than 63 bits it will be truncated.

source

pub fn from_bigdecimal( decimal: BigDecimal, locale_frac_digits: u32, ) -> Result<PgMoney, Box<dyn Error + Sync + Send>>

Convert a BigDecimal value into money using the correct precision defined in the PostgreSQL settings. The default precision is two.

Trait Implementations§

source§

impl Add for PgMoney

source§

fn add(self, rhs: PgMoney) -> <PgMoney as Add>::Output

Adds two monetary values.

§Panics

Panics if overflowing the i64::MAX.

source§

type Output = PgMoney

The resulting type after applying the + operator.
source§

impl AddAssign for PgMoney

source§

fn add_assign(&mut self, rhs: PgMoney)

An assigning add for two monetary values.

§Panics

Panics if overflowing the i64::MAX.

source§

impl Clone for PgMoney

source§

fn clone(&self) -> PgMoney

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for PgMoney

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Decode<'_, Postgres> for PgMoney

source§

fn decode( value: PgValueRef<'_>, ) -> Result<PgMoney, Box<dyn Error + Sync + Send>>

Decode a new value of this type using a raw value from the database.
source§

impl Default for PgMoney

source§

fn default() -> PgMoney

Returns the “default value” for a type. Read more
source§

impl Encode<'_, Postgres> for PgMoney

source§

fn encode_by_ref( &self, buf: &mut PgArgumentBuffer, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>

Writes the value of self into buf without moving self. Read more
source§

fn encode( self, buf: &mut <DB as Database>::ArgumentBuffer<'q>, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
where Self: Sized,

Writes the value of self into buf in the expected format for the database.
source§

fn produces(&self) -> Option<<DB as Database>::TypeInfo>

source§

fn size_hint(&self) -> usize

source§

impl<T> From<T> for PgMoney
where T: Into<i64>,

source§

fn from(num: T) -> PgMoney

Converts to this type from the input type.
source§

impl PartialEq for PgMoney

source§

fn eq(&self, other: &PgMoney) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PgHasArrayType for PgMoney

source§

impl Sub for PgMoney

source§

fn sub(self, rhs: PgMoney) -> <PgMoney as Sub>::Output

Subtracts two monetary values.

§Panics

Panics if underflowing the i64::MIN.

source§

type Output = PgMoney

The resulting type after applying the - operator.
source§

impl SubAssign for PgMoney

source§

fn sub_assign(&mut self, rhs: PgMoney)

An assigning subtract for two monetary values.

§Panics

Panics if underflowing the i64::MIN.

source§

impl Type<Postgres> for PgMoney

source§

fn type_info() -> PgTypeInfo

Returns the canonical SQL type for this Rust type. Read more
source§

fn compatible(ty: &<DB as Database>::TypeInfo) -> bool

Determines if this Rust type is compatible with the given SQL type. Read more
source§

impl Copy for PgMoney

source§

impl Eq for PgMoney

source§

impl StructuralPartialEq for PgMoney

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more