Module: PGI::Dataset::Utils

Defined in:
lib/pgi/dataset/utils.rb

Class Method Summary collapse

Class Method Details

.sanitize_column(col, table = nil) ⇒ Array

Get a sanitized column name

Parameters:

  • columns (String|Array)

    the column name(s) to sanitize

  • table (Symbol) (defaults to: nil)

    the table name

Returns:

  • (Array)

    list of sanitized column names



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)

Parameters:

  • columns (String|Array)

    the column name(s) to sanitize

  • table (Symbol) (defaults to: nil)

    the table name

Returns:

  • (Array)

    list of sanitized column names



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

Parameters:

  • table (String)

    table name

  • sql (String)

    SQL query

Returns:

  • (String)

    a statement name



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

Parameters:

  • attr (Hash)

    input hash

Returns:

  • (Hash)

    stripped 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

Parameters:

  • attr (Hash)

    input hash

Returns:

  • (Hash)

    stripped 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

Parameters:

  • columns (Symbol)

    the column name(s) to sanitize

Returns:

  • (Boolean)

    true if valid, otherwise false



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