Module: DBI::DBD::Pg

Defined in:
lib/dbd/Pg.rb,
lib/dbd/pg/exec.rb

Overview

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.

Defined Under Namespace

Modules: Type Classes: Database, Driver, PgExecutor, PgExecutorAsync, Statement, Tuples

Constant Summary

VERSION =
"0.3.9"
DESCRIPTION =
"PostgreSQL DBI DBD"

Class Method Summary collapse

Class Method Details

.driver_nameObject

returns 'Pg'

See DBI::TypeUtil#convert for more information.



60
61
62
# File 'lib/dbd/Pg.rb', line 60

def self.driver_name
    "Pg"
end

.generate_array(obj) ⇒ Object

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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) ⇒ Object

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.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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) ⇒ Object

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



95
96
97
# File 'lib/dbd/Pg.rb', line 95

def self.quote(value)
    "E'#{ value.gsub(/\\/){ '\\\\' }.gsub(/'/){ '\\\'' } }'"
end