Module: PgConn

Defined in:
lib/pg_conn.rb,
lib/pg_conn/version.rb,
lib/pg_conn/role_methods.rb,
lib/pg_conn/rdbms_methods.rb,
lib/pg_conn/schema_methods.rb,
lib/pg_conn/session_methods.rb

Defined Under Namespace

Classes: Connection, Error, Literal, PsqlError, RdbmsMethods, RoleMethods, Rollback, SchemaMethods, SessionMethods

Constant Summary collapse

VERSION =
"0.56.1"

Class Method Summary collapse

Class Method Details

.===(element) ⇒ Object

Make the PgConn module pretend it has PgConn instances



22
# File 'lib/pg_conn.rb', line 22

def self.===(element) element.is_a?(PgConn::Connection) or super end

.ensure(*args) ⇒ Object

Returns a PgConn::Connection object (aka. a PgConn object). It’s arguments can be an existing connection that will just be returned or a set of PgConn::Connection#initialize arguments that will be used to create a new PgConn::Connection object



28
29
30
31
32
33
34
# File 'lib/pg_conn.rb', line 28

def self.ensure(*args)
  if args.size == 1 && args.first.is_a?(PgConn::Connection)
    args.first
  else
    PgConn::Connection.new(*args)
  end
end

.escape_identifier(s) ⇒ Object

Same as postgres PG#escape_identifier but do not need a connection



1618
1619
1620
1621
1622
1623
# File 'lib/pg_conn.rb', line 1618

def self.escape_identifier(s)
  !s.nil? or raise TypeError, "Identifier can't be nil"
  s.is_a?(String) or raise TypeError, "Identifier can't be a #{s.class}"
  s !~ /\A\s*\z/ or raise TypeError, "Identifier be blank"
  %("#{s.gsub('"', '""')}")
end

.escape_literal(s) ⇒ Object

Same as postgres PG#escape_literal but do not need a connection



1615
# File 'lib/pg_conn.rb', line 1615

def self.escape_literal(s) = s.nil? ? 'NULL' : "'#{s.gsub("'", "''")}'"

.new(*args, &block) ⇒ Object

Return a PgConn::Connection object. TODO: A block argument



19
# File 'lib/pg_conn.rb', line 19

def self.new(*args, &block) Connection.new(*args, &block) end

.quote(value, elem_type: nil, json_type: nil) ⇒ Object

Simple shorthand for ::quote_value



98
99
100
# File 'lib/pg_conn.rb', line 98

def self.quote(value, elem_type: nil, json_type: nil)
  quote_value(value, elem_type: nil, json_type: nil)
end

.quote_identifier(s) ⇒ Object

Quote argument as an identifier. The argument should be a non-nil string or a symbol

Quote metods are both defined as PgConn class methods and as Connection member methods



42
43
44
45
# File 'lib/pg_conn.rb', line 42

def self.quote_identifier(s)
  s = s.to_s if s.is_a?(Symbol)
  Literal.new escape_identifier(s).gsub(/\./, '"."').sub(/"\*"/, "*")
end

.quote_identifiers(idents) ⇒ Object

Quote identifiers and concatenate them using ‘,’ as separator



48
# File 'lib/pg_conn.rb', line 48

def self.quote_identifiers(idents) = Literal.new idents.map { |ident| quote_identifier(ident) }.join(", ")

.quote_list(array, **opts) ⇒ Object

Quote array elements as a comma separated list of values and enclosed in parenthesis. Eg. [1, 2] => “(1, 2)”



134
# File 'lib/pg_conn.rb', line 134

def self.quote_list(array, **opts) = quote_tuples([array], **opts)

.quote_row(row, **opts) ⇒ Object

Quote array as a row - ‘(value, value, …)’



108
# File 'lib/pg_conn.rb', line 108

def self.quote_row(row, **opts) = quote_list(row, **opts)

.quote_rows(rows, **opts) ⇒ Object

Quote values as a list of rows - ‘(value, value), (value, value), …’



111
# File 'lib/pg_conn.rb', line 111

def self.quote_rows(rows, **opts) = rows.map { quote_list(_1, **opts) }.join(', ')

.quote_tuple(tuple, elem_types: nil, **opts) ⇒ Object

Quote an array of values as a tuple. The element types should be in the same order as the array arguments. #quote_tuples is same as #quote_values except the values may have different types (this makes no difference except in the case when the tuple may contain empty array(s))

Note that it is :elem_types (plural) and not :elem_type



119
120
121
122
123
124
125
# File 'lib/pg_conn.rb', line 119

def self.quote_tuple(tuple, elem_types: nil, **opts)
  elem_types = Array(elem_types)
  Literal.new tuple.map { |value|
    elem_type = value.is_a?(Array) ? elem_types&.shift : nil
    quote_value(value, **opts, elem_type: elem_type)
  }.join(", ")
end

.quote_tuples(tuples, **opts) ⇒ Object

Quote an array of tuples



128
129
130
# File 'lib/pg_conn.rb', line 128

def self.quote_tuples(tuples, **opts)
  Literal.new tuples.map { |tuple| "(#{quote_tuple(tuple, **opts)})" }.join(", ")
end

.quote_value(value, elem_type: nil, json_type: nil) ⇒ Object

Quote the value as a string. Returns a Literal object

The value can be of any type but is converted to a string using #to_s before quoting. This works by default for the regular types Integer, true/false, Time/Date/DateTime, and arrays. Other types may require special handling

Arrays are quoted using the Array constructor (Array) so the quoted string can’t be used in ‘VALUE in (LIST)’ constructs, use #quote_values instead

Hashes are quoted as a literal JSON expression converted into the given :json_type. If :json_type is nil, it is the application’s responsibility to cast the value to either ‘json’ or ‘jsonb’

Note that a tuple value (an array) must be quoted using #quote_tuple because #quote_value would quote the tuple as an array value instead of a list of values

The :elem_type option can be a postgres type name (String or Symbol) or an array of type names. It is used as the required explicit element type when the argument is an empty array. It is not needed if the array is guaranteed to be non-empty. Nested arrays are not supported



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pg_conn.rb', line 74

def self.quote_value(value, elem_type: nil, json_type: nil)
  Literal.new \
      case value
        when Literal; value
        when String; escape_literal(value)
        when Integer, Float; value.to_s
        when true, false; value.to_s
        when nil; 'null'
        when DateTime, Time; "'#{value.to_time.strftime("%FT%T.%6N%:z")}'"
        when Date; "'#{value}'"
        when Array
          if value.empty?
            elem_type or raise Error, "Empty array without elem_type"
            "array[]::#{elem_type}[]"
          else
            "array[#{value.map { |elem| quote_value(elem) }.join(', ')}]"
          end
        when Hash; ["'#{value.to_json}'", json_type].compact.join('::')
      else
        escape_literal(value.to_s)
      end
end

.quote_values(values, **opts) ⇒ Object

Quote values and concatenate them using ‘,’ as separator



103
104
105
# File 'lib/pg_conn.rb', line 103

def self.quote_values(values, **opts)
  Literal.new values.map { |value| quote_value(value, **opts) }.join(", ")
end

.sql_idents(values) ⇒ Object



1612
# File 'lib/pg_conn.rb', line 1612

def self.sql_idents(values) '"' + values.join('", "') + '"' end

.sql_values(values) ⇒ Object



1611
# File 'lib/pg_conn.rb', line 1611

def self.sql_values(values) "'" + values.join("', '") + "'" end