### Home Classes Methods

### In Files

- lib/dbi/typeutil.rb

### Parent

Object

### Methods

- ::convert
- ::register_conversion
- ::type_name_to_module

### Files

- 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::TypeUtil

TypeUtil is a series of utility methods for type management.

### Public Class Methods

convert(driver_name, obj) click to toggle source

Convert object for `driver_name`. See register_conversion for a complete explanation of how type conversion is performed.

If the conversion is instructed to cascade, it will go to the special "default" conversion, which is a pre-defined common case (and mutable) ruleset for native types. Note that it will use the result from the first conversion, not what was originally passed. Be sure to leave the object untouched if that is your intent. E.g., if your DBD converts an Integer to String and tells it to cascade, the "default" conversion will get a String and quote it, not an Integer (which has different rules).

    # File lib/dbi/typeutil.rb, line 48
    def self.convert(driver_name, obj)
        if @@conversions[driver_name]
            newobj, cascade = @@conversions[driver_name].call(obj)
            if cascade
                return @@conversions["default"].call(newobj)
            end
            return newobj
        end

        return @@conversions["default"].call(obj)
    end

register_conversion(driver_name, &block) click to toggle source

Register a conversion for a DBD. This applies to bound parameters for outgoing statements; please look at DBI::Type for result sets.

Conversions are given a driver_name, which is then used to look up the conversion to perform on the object. Please see convert for more information. Driver names are typically provided by the DBD, but may be overridden at any stage temporarily by assigning to the `driver_name` attribute for the various handles.

A conversion block is normally a `case` statement that identifies various native ruby types and converts them to string, but ultimately the result type is dependent on low-level driver. The resulting object will be fed to the query as the bound value.

The result of the block is two arguments, the first being the result object, and the second being a `cascade` flag, which if true instructs convert to run the result through the `default` conversion as well and use its result. This is advantageous when you just need to convert everything to string, and allow `default` to properly escape it.

    # File lib/dbi/typeutil.rb, line 30
    def self.register_conversion(driver_name, &block)
        raise "Must provide a block" unless block_given?
        @@conversions[driver_name] = block
    end

type_name_to_module(type_name) click to toggle source

Convenience method to match many SQL named types to DBI::Type classes. If none can be matched, returns DBI::Type::Varchar.

    # File lib/dbi/typeutil.rb, line 64
    def self.type_name_to_module(type_name)
        case type_name
        when /^int(?:\d+|eger)?$/
            DBI::Type::Integer
        when /^varchar$/, /^character varying$/
            DBI::Type::Varchar
        when /^(?:float|real)$/
            DBI::Type::Float
        when /^bool(?:ean)?$/, /^tinyint$/
            DBI::Type::Boolean
        when /^time(?:stamp(?:tz)?)?$/
            DBI::Type::Timestamp
        when /^(?:decimal|numeric)$/
            DBI::Type::Decimal
        else
            DBI::Type::Varchar
        end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.