Module: PGI::Dataset::Utils
- Defined in:
- lib/pgi/dataset/utils.rb
Class Method Summary collapse
-
.sanitize_column(col, table = nil) ⇒ Array
Get a sanitized column name.
-
.sanitize_columns(columns, table = nil) ⇒ Array
Get a sanitized column name(s).
-
.stmt_name(table, sql) ⇒ String
Get a unique statement name for the Query.
-
.strip_uninsertable(attr) ⇒ Hash
Strips the fields to not insert off a hash.
-
.strip_unupdateable(attr) ⇒ Hash
Strips the fields to not update off a hash.
-
.valid_column?(column) ⇒ Boolean
Validates a column name.
Class Method Details
.sanitize_column(col, table = nil) ⇒ Array
Get a sanitized column name
48 49 50 51 52 53 54 |
# File 'lib/pgi/dataset/utils.rb', line 48 def sanitize_column(col, table = nil) raise "invalid column name: #{col.inspect}" unless valid_column?(col) return "*" if col == "*" table ? %("#{table}"."#{col}") : %("#{col}") end |
.sanitize_columns(columns, table = nil) ⇒ Array
Get a sanitized column name(s)
37 38 39 40 41 |
# File 'lib/pgi/dataset/utils.rb', line 37 def sanitize_columns(columns, table = nil) Array(columns).map do |col| sanitize_column(col, table) end end |
.stmt_name(table, sql) ⇒ String
Get a unique statement name for the Query
28 29 30 |
# File 'lib/pgi/dataset/utils.rb', line 28 def stmt_name(table, sql) "#{table}_#{Digest::MD5.hexdigest(sql)}" end |
.strip_uninsertable(attr) ⇒ Hash
Strips the fields to not insert off a hash
11 12 13 |
# File 'lib/pgi/dataset/utils.rb', line 11 def strip_uninsertable(attr) attr.except(:id, :created_at, :updated_at) end |
.strip_unupdateable(attr) ⇒ Hash
Strips the fields to not update off a hash
19 20 21 |
# File 'lib/pgi/dataset/utils.rb', line 19 def strip_unupdateable(attr) attr.except(:created_at, :updated_at) end |
.valid_column?(column) ⇒ Boolean
Validates a column name
60 61 62 63 |
# File 'lib/pgi/dataset/utils.rb', line 60 def valid_column?(column) col = column.to_s col == "*" || col =~ /\A[a-z_][a-z0-9_]*\z/i end |