Skip to content

struct Athena::ORM::Types::Decimal
inherits Athena::ORM::Types::Type #

Holds decimal values as strings, so no precision is lost. Map a BigDecimal field with Types::Number instead to work with the values numerically.

Values are stored in a NUMERIC/DECIMAL column. Fields are only mapped to this type explicitly, e.g. @[AORMA::Column(type: "decimal", precision: 10, scale: 2)] on a String property.

Warning

MySQL and MariaDB read DECIMAL columns as Float64, so on those databases values are limited to Float64 precision and lose their scale, e.g. "12.3400" reads back as "12.34". SQLite stores them with REAL affinity, with the same limitation.

Methods#

#sql_declaration(column : Schema::Column, platform : AORM::Platforms::Platform) : ::String#

Returns the SQL used to declare column as this type on platform, e.g. VARCHAR(255).

Built-in types delegate to the matching declaration method of AORM::Platforms::Platform, which custom types can reuse.

Todo

Nothing in the ORM generates schema yet, so this isn't called by the ORM itself.

View source

#to_crystal_value(value : DB::ResultSet, platform : Platforms::Platform) : ::String | Nil#

Reads the next column from value and converts it into the Crystal value this type represents. Returns nil for a NULL value.

Used while hydrating entities. Advances the cursor by exactly one column.

View source

#to_crystal_value(value : _, platform : Platforms::Platform) : ::String | Nil#

Converts value, as read from the database, into the Crystal value this type represents. Returns nil for a NULL value, and raises if value can't be converted.

Used for values that have already been read, such as a database-generated identifier. Each type defines this once, as the place for its translation logic (parsing, narrowing, decoding, etc.). It accepts any input, since drivers disagree on the Crystal type of some columns, and validates it inside the body, e.g. with case value.

View source