Parent

SQLite3::Translator

The Translator class encapsulates the logic and callbacks necessary for converting string data to a value of some specified type. Every Database instance may have a Translator instance, in order to assist in type translation (Database#type_translation).

Further, applications may define their own custom type translation logic by registering translator blocks with the corresponding database’s translator instance (Database#translator).

Public Class Methods

new() click to toggle source

Create a new Translator instance. It will be preinitialized with default translators for most SQL data types.

    # File lib/sqlite3/translator.rb, line 18
18:     def initialize
19:       @translators = Hash.new( proc { |type,value| value } )
20:       @type_name_cache = {}
21:       register_default_translators
22:     end

Public Instance Methods

add_translator( type ) click to toggle source

Add a new translator block, which will be invoked to process type translations to the given type. The type should be an SQL datatype, and may include parentheses (i.e., “VARCHAR(30)”). However, any parenthetical information is stripped off and discarded, so type translation decisions are made solely on the “base” type name.

The translator block itself should accept two parameters, “type” and “value”. In this case, the “type” is the full type name (including parentheses), so the block itself may include logic for changing how a type is translated based on the additional data. The “value” parameter is the (string) data to convert.

The block should return the translated value.

    # File lib/sqlite3/translator.rb, line 37
37:     def add_translator( type, &block ) # :yields: type, value
38:       @translators[ type_name( type ) ] = block
39:     end
translate( type, value ) click to toggle source

Translate the given string value to a value of the given type. In the absense of an installed translator block for the given type, the value itself is always returned. Further, nil values are never translated, and are always passed straight through regardless of the type parameter.

    # File lib/sqlite3/translator.rb, line 45
45:     def translate( type, value )
46:       unless value.nil?
47:         # FIXME: this is a hack to support Sequel
48:         if type && %{ datetime timestamp }.include?(type.downcase)
49:           @translators[ type_name( type ) ].call( type, value.to_s )
50:         else
51:           @translators[ type_name( type ) ].call( type, value )
52:         end
53:       end
54:     end

Private Instance Methods

register_default_translators() click to toggle source

Register the default translators for the current Translator instance. This includes translators for most major SQL data types.

     # File lib/sqlite3/translator.rb, line 69
 69:     def register_default_translators
 70:       [ "time",
 71:         "timestamp" ].each { |type| add_translator( type ) { |t, v| Time.parse( v ) } }
 72: 
 73:       add_translator( "date" ) { |t,v| Date.parse(v) }
 74:       add_translator( "datetime" ) { |t,v| DateTime.parse(v) }
 75: 
 76:       [ "decimal",
 77:         "float",
 78:         "numeric",
 79:         "double",
 80:         "real",
 81:         "dec",
 82:         "fixed" ].each { |type| add_translator( type ) { |t,v| v.to_f } }
 83: 
 84:       [ "integer",
 85:         "smallint",
 86:         "mediumint",
 87:         "int",
 88:         "bigint" ].each { |type| add_translator( type ) { |t,v| v.to_i } }
 89: 
 90:       [ "bit",
 91:         "bool",
 92:         "boolean" ].each do |type|
 93:         add_translator( type ) do |t,v|
 94:           !( v.strip.gsub(/00+/,"0") == "0" ||
 95:              v.downcase == "false" ||
 96:              v.downcase == "f" ||
 97:              v.downcase == "no" ||
 98:              v.downcase == "n" )
 99:         end
100:       end
101: 
102:       add_translator( "tinyint" ) do |type, value|
103:         if type =~ /\(\s*1\s*\)/
104:           value.to_i == 1
105:         else
106:           value.to_i
107:         end
108:       end
109:     end
type_name( type ) click to toggle source

A convenience method for working with type names. This returns the “base” type name, without any parenthetical data.

    # File lib/sqlite3/translator.rb, line 58
58:     def type_name( type )
59:       @type_name_cache[type] ||= begin
60:         type = "" if type.nil?
61:         type = $1 if type =~ /^(.*?)\(/
62:         type.upcase
63:       end
64:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.