Struct sqlx::postgres::types::PgMoney[][src]

pub struct PgMoney(pub i64);
This is supported 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

Convert the money value into a BigDecimal using locale_frac_digits.

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

Convert the money value into a Decimal using locale_frac_digits.

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

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.

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

Trait Implementations

Adds two monetary values.

Panics

Panics if overflowing the i64::MAX.

The resulting type after applying the + operator.

An assigning add for two monetary values.

Panics

Panics if overflowing the i64::MAX.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

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

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

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Subtracts two monetary values.

Panics

Panics if underflowing the i64::MIN.

The resulting type after applying the - operator.

An assigning subtract for two monetary values.

Panics

Panics if underflowing the i64::MIN.

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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Converts self into T using Into<T>. Read more

Converts self into a target type. Read more

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

Causes self to use its Binary implementation when Debug-formatted.

Causes self to use its Display implementation when Debug-formatted. Read more

Causes self to use its LowerExp implementation when Debug-formatted. Read more

Causes self to use its LowerHex implementation when Debug-formatted. Read more

Causes self to use its Octal implementation when Debug-formatted.

Causes self to use its Pointer implementation when Debug-formatted. Read more

Causes self to use its UpperExp implementation when Debug-formatted. Read more

Causes self to use its UpperHex implementation when Debug-formatted. Read more

Performs the conversion.

Performs the conversion.

Pipes by value. This is generally the method you want to use. Read more

Borrows self and passes that borrow into the pipe function. Read more

Mutably borrows self and passes that borrow into the pipe function. Read more

Borrows self, then passes self.borrow() into the pipe function. Read more

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more

Borrows self, then passes self.as_ref() into the pipe function.

Mutably borrows self, then passes self.as_mut() into the pipe function. Read more

Borrows self, then passes self.deref() into the pipe function.

Mutably borrows self, then passes self.deref_mut() into the pipe function. Read more

Pipes a value into a function that cannot ordinarily be called in suffix position. Read more

Pipes a trait borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait mutable borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait mutable borrow into a function that cannot normally be called in suffix position. Read more

Pipes a dereference into a function that cannot normally be called in suffix position. Read more

Pipes a mutable dereference into a function that cannot normally be called in suffix position. Read more

Pipes a reference into a function that cannot ordinarily be called in suffix position. Read more

Pipes a mutable reference into a function that cannot ordinarily be called in suffix position. Read more

Should always be Self

Immutable access to a value. Read more

Mutable access to a value. Read more

Immutable access to the Borrow<B> of a value. Read more

Mutable access to the BorrowMut<B> of a value. Read more

Immutable access to the AsRef<R> view of a value. Read more

Mutable access to the AsMut<R> view of a value. Read more

Immutable access to the Deref::Target of a value. Read more

Mutable access to the Deref::Target of a value. Read more

Calls .tap() only in debug builds, and is erased in release builds.

Calls .tap_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref_mut() only in debug builds, and is erased in release builds. Read more

Provides immutable access for inspection. Read more

Calls tap in debug builds, and does nothing in release builds.

Provides mutable access for modification. Read more

Calls tap_mut in debug builds, and does nothing in release builds.

Provides immutable access to the reference for inspection.

Calls tap_ref in debug builds, and does nothing in release builds.

Provides mutable access to the reference for modification.

Calls tap_ref_mut in debug builds, and does nothing in release builds.

Provides immutable access to the borrow for inspection. Read more

Calls tap_borrow in debug builds, and does nothing in release builds.

Provides mutable access to the borrow for modification.

Calls tap_borrow_mut in debug builds, and does nothing in release builds. Read more

Immutably dereferences self for inspection.

Calls tap_deref in debug builds, and does nothing in release builds.

Mutably dereferences self for modification.

Calls tap_deref_mut in debug builds, and does nothing in release builds. Read more

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

Attempts to convert self into T using TryInto<T>. Read more

Attempts to convert self into a target type. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.