Class: DBI::TypeUtil
- Inherits:
-
Object
- Object
- DBI::TypeUtil
- Defined in:
- lib/dbi/typeutil.rb
Overview
TypeUtil is a series of utility methods for type management.
Constant Summary collapse
- @@conversions =
{ }
Class Method Summary collapse
-
.convert(driver_name, obj) ⇒ Object
Convert object for
driver_name
. -
.register_conversion(driver_name, &block) ⇒ Object
Register a conversion for a DBD.
-
.type_name_to_module(type_name) ⇒ Object
Convenience method to match many SQL named types to DBI::Type classes.
Class Method Details
.convert(driver_name, obj) ⇒ Object
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).
48 49 50 51 52 53 54 55 56 57 58 |
# 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) ⇒ Object
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.
30 31 32 33 |
# 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) ⇒ Object
Convenience method to match many SQL named types to DBI::Type classes. If none can be matched, returns DBI::Type::Varchar.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/dbi/typeutil.rb', line 64 def self.type_name_to_module(type_name) case type_name when /^int(?:\d+|eger)?$/i DBI::Type::Integer when /^varchar$/i, /^character varying$/i DBI::Type::Varchar when /^(?:float|real)$/i DBI::Type::Float when /^bool(?:ean)?$/i, /^tinyint$/i DBI::Type::Boolean when /^time(?:stamp(?:tz)?)?$/i DBI::Type::Timestamp when /^(?:decimal|numeric)$/i DBI::Type::Decimal else DBI::Type::Varchar end end |