Module: DBI::Type

### Home Classes Methods

### In Files

- lib/dbi/types.rb

### Namespace

- CLASS DBI::Type::Boolean
- CLASS DBI::Type::Decimal
- CLASS DBI::Type::Float
- CLASS DBI::Type::Integer
- CLASS DBI::Type::Null
- CLASS DBI::Type::Timestamp
- CLASS DBI::Type::Varchar

### Files

- ChangeLog
- LICENSE
- README

### Class/Module Index

Quicksearch

- DBI
- DBI::BaseDatabase
- DBI::BaseDriver
- DBI::BaseStatement
- DBI::Binary
- DBI::ColumnInfo
- DBI::DataError
- DBI::DatabaseError
- DBI::DatabaseHandle
- DBI::Date
- DBI::DriverHandle
- DBI::Error
- DBI::Handle
- DBI::HandleTracer
- DBI::IntegrityError
- DBI::InterfaceError
- DBI::InternalError
- DBI::NotImplementedError
- DBI::NotSupportedError
- DBI::OperationalError
- DBI::ProgrammingError
- DBI::Row
- DBI::SQL
- DBI::SQL::PreparedStatement
- DBI::StatementHandle
- DBI::Time
- DBI::Timestamp
- DBI::Type
- DBI::Type::Boolean
- DBI::Type::Decimal
- DBI::Type::Float
- DBI::Type::Integer
- DBI::Type::Null
- DBI::Type::Timestamp
- DBI::Type::Varchar
- DBI::TypeUtil
- DBI::Utils
- DBI::Utils::ConvParam
- DBI::Utils::TableFormatter
- DBI::Utils::XMLFormatter
- DBI::Warning
- Class

No matching classes.

# DBI::Type

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.

[Validate]

Generated with the Darkfish Rdoc Generator 2.