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.53.0"
Class Method Summary collapse
-
.===(element) ⇒ Object
Make the PgConn module pretend it has PgConn instances.
-
.ensure(*args) ⇒ Object
Returns a PgConn::Connection object (aka. a PgConn object).
-
.escape_identifier(s) ⇒ Object
Same as postgres PG#escape_identifier but do not need a connection.
-
.escape_literal(s) ⇒ Object
Same as postgres PG#escape_literal but do not need a connection.
-
.new(*args, &block) ⇒ Object
Return a PgConn::Connection object.
-
.quote(value, elem_type: nil, json_type: nil) ⇒ Object
Simple shorthand for ::quote_value.
-
.quote_identifier(s) ⇒ Object
Quote argument as an identifier.
-
.quote_identifiers(idents) ⇒ Object
Quote identifiers and concatenate them using ‘,’ as separator.
-
.quote_list(array, **opts) ⇒ Object
Quote array elements as a comma separated list of values and enclosed in parenthesis.
-
.quote_row(row, **opts) ⇒ Object
Quote array as a row - ‘(value, value, …)’.
-
.quote_rows(rows, **opts) ⇒ Object
Quote values as a list of rows - ‘(value, value), (value, value), …’.
-
.quote_tuple(tuple, elem_types: nil, **opts) ⇒ Object
Quote an array of values as a tuple.
-
.quote_tuples(tuples, **opts) ⇒ Object
Quote an array of tuples.
-
.quote_value(value, elem_type: nil, json_type: nil) ⇒ Object
Quote the value as a string.
-
.quote_values(values, **opts) ⇒ Object
Quote values and concatenate them using ‘,’ as separator.
- .sql_idents(values) ⇒ Object
- .sql_values(values) ⇒ Object
Class Method Details
.===(element) ⇒ Object
Make the PgConn module pretend it has PgConn instances
21 |
# File 'lib/pg_conn.rb', line 21 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
27 28 29 30 31 32 33 |
# File 'lib/pg_conn.rb', line 27 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
1580 1581 1582 1583 1584 1585 |
# File 'lib/pg_conn.rb', line 1580 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
1577 |
# File 'lib/pg_conn.rb', line 1577 def self.escape_literal(s) = s.nil? ? 'NULL' : "'#{s.gsub("'", "''")}'" |
.new(*args, &block) ⇒ Object
Return a PgConn::Connection object. TODO: A block argument
18 |
# File 'lib/pg_conn.rb', line 18 def self.new(*args, &block) Connection.new(*args, &block) end |
.quote(value, elem_type: nil, json_type: nil) ⇒ Object
Simple shorthand for ::quote_value
97 98 99 |
# File 'lib/pg_conn.rb', line 97 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
41 42 43 44 |
# File 'lib/pg_conn.rb', line 41 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
47 |
# File 'lib/pg_conn.rb', line 47 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)”
133 |
# File 'lib/pg_conn.rb', line 133 def self.quote_list(array, **opts) = quote_tuples([array], **opts) |
.quote_row(row, **opts) ⇒ Object
Quote array as a row - ‘(value, value, …)’
107 |
# File 'lib/pg_conn.rb', line 107 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), …’
110 |
# File 'lib/pg_conn.rb', line 110 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
118 119 120 121 122 123 124 |
# File 'lib/pg_conn.rb', line 118 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
127 128 129 |
# File 'lib/pg_conn.rb', line 127 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
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/pg_conn.rb', line 73 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
102 103 104 |
# File 'lib/pg_conn.rb', line 102 def self.quote_values(values, **opts) Literal.new values.map { |value| quote_value(value, **opts) }.join(", ") end |
.sql_idents(values) ⇒ Object
1574 |
# File 'lib/pg_conn.rb', line 1574 def self.sql_idents(values) '"' + values.join('", "') + '"' end |
.sql_values(values) ⇒ Object
1573 |
# File 'lib/pg_conn.rb', line 1573 def self.sql_values(values) "'" + values.join("', '") + "'" end |