Module: DBI::Type
- Defined in:
- lib/dbi/types.rb
Overview
Interface to convert SQL result sets to native Ruby types.
Type is used to convert result sets, which differ from bound variables (which generally go in the opposite direction). For those, see DBI::TypeUtil#convert and DBI::TypeUtil#register_conversion.
Type objects have a simple interface: they implement a parse method
which takes the result from the DBD and attempts to convert it to the
native type. In the event that they do not do this successfully, they are
expected to return the object in its original form.
As a result, many of the built-in Type classes fallback to simpler forms: Float falls back to Integer, Integer to Varchar, etc. It's questionable at this point if it's desirable to do this, but testing has so far proven it a non-issue.
To reiterate, it is never acceptable to return nil or some other
placeholder when an object will not successfully parse. Return the object
handed to you.
Types must also handle nil as a result to parse. In this case, the
advisable solution is to just let the nil pass through, as it's usually
indicative of a SQL NULL result.
DBI::Row handles delegation of these objects as a converter for the
results. Typically, the type object is a class inferred from
DBI::TypeUtil#type_name_to_module ran against the ColumnInfo field
type_name. However, the the dbi_type field can be used in its place
to directly associate a Type object with the column in the DBD, and
end-users can leverage StatementHandle#bind_coltype to manually tweak
this transformation.
As stated before, Type objects are objects. These objects may be Modules
or Classes (and typically are), but there is no reason a traditional
constructed object with a parse method cannot be returned; in fact, it
is used in a few spots to emulate complex types such as PostgreSQL
arrays. Look into the dbi_type ColumnInfo field to pass these types
around.
Defined Under Namespace
Classes: Boolean, Decimal, Float, Integer, Null, Timestamp, Varchar