module DBI::DBD::Pg

DBD::Pg - Database Driver for the PostgreSQL database system.

Requires DBI and the 'pg' gem or package to work.

Only things that extend DBI's results are documented.

Constants

DESCRIPTION
VERSION

Public Class Methods

driver_name() click to toggle source

returns 'Pg'

See DBI::TypeUtil#convert for more information.

# File lib/dbd/Pg.rb, line 60
def self.driver_name
    "Pg"
end
generate_array(obj) click to toggle source

This method takes a ruby Array and converts it to PostgreSQL array syntax.

# File lib/dbd/Pg.rb, line 67
def self.generate_array(obj)
    # XXX yarr, there be recursion here, and it's probably not a good idea.
    output = "{"
    obj.each do |item|
        case item
        when ::Array
            output += generate_array(item)
        else
            generated = DBI::TypeUtil.convert(driver_name, item)
            generated = case item
                        when String
                            # in strings, escapes are doubled and the quotes are different.
                            # this gets *really* ugly and needs to be well-tested
                            "\"#{generated.gsub(/\\/) { "\\\\" }}\""
                        when Fixnum
                            generated.to_s
                        end
            output += generated
        end
        output += "," # FIXME technically, delimiters are variable
    end

    output.sub(/,$/, '}')
end
parse_type(ftype) click to toggle source

Parse a postgresql type. Returns a hash with these fields (as Symbol)

  • ftype: the full type, as passed in to this method.

  • type: the type stripped of all attribute information.

  • size: the LHS of the attribute information, typically the precision.

  • decimal: the RHS of the attribute information, typically the scale.

  • array: true if this type is actually an array of that type.

# File lib/dbd/Pg.rb, line 108
def self.parse_type(ftype)
    type = ftype
    pos = ftype.index('(')
    decimal = nil
    size = nil
    array_of_type = nil

    if pos != nil
        type = ftype[0..pos-1]
        size = ftype[pos+1..-2]
        pos = size.index(',')
        if pos != nil
            size, decimal = size.split(',', 2)
            size = size.to_i
            decimal = decimal.to_i
        else
            size = size.to_i
        end
    end

    if type =~ /\[\]$/
        type.sub!(/\[\]$/, '')
        array_of_type = true
    end

    return {
        :ftype   => ftype.dup,
        :type    => type,
        :size    => size,
        :decimal => decimal,
        :array   => array_of_type
    }
end
quote(value) click to toggle source

A quote helper, this uses the new syntax in PostgreSQL 8.2 and up.

# File lib/dbd/Pg.rb, line 95
def self.quote(value)
    "E'#{ value.gsub(/\\/){ '\\\\' }.gsub(/'/){ '\\\'' } }'"
end