Module: PGB

Defined in:
lib/pgb.rb,
lib/pgb.rb,
lib/pgb/model.rb,
lib/pgb/query.rb,
lib/pgb/casting.rb,
lib/pgb/command.rb,
lib/pgb/keyword.rb,
lib/pgb/literal.rb,
lib/pgb/mutable.rb,
lib/pgb/version.rb,
lib/pgb/shortcuts.rb,
lib/pgb/type_cast.rb,
lib/pgb/executable.rb,
lib/pgb/expression.rb,
lib/pgb/identifier.rb,
lib/pgb/function_call.rb,
lib/pgb/operator_call.rb,
lib/pgb/sql_displayable.rb,
lib/pgb/column_reference.rb,
lib/pgb/evaluation_context.rb

Defined Under Namespace

Modules: Casting, Executable, Mutable, SQLDisplayable, Shortcuts Classes: ColumnReference, Command, Error, EvaluationContext, Expression, FunctionCall, Identifier, Keyword, Literal, Model, OperatorCall, Query, TypeCast

Constant Summary collapse

VERSION =
'0.1.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loaderObject

Returns the value of attribute loader.


29
30
31
# File 'lib/pgb.rb', line 29

def loader
  @loader
end

Class Method Details

.connectionObject

TODO Connection lost? Reconnect? Different version?


67
68
69
70
71
72
# File 'lib/pgb.rb', line 67

def connection
  @connection ||= begin
    connection = PG.connect(dbname: 'hubstaff_development')
    connection
  end
end

.evaluate(&block) ⇒ Object


45
46
47
# File 'lib/pgb.rb', line 45

def evaluate(&block)
  EvaluationContext.new.instance_exec(&block)
end

.execute(query) ⇒ Object

TODO Log TODO Connection pool TODO DB connection params TODO Extract TODO Wrap errors in PGB::Error TODO Support block evaluation TODO Log runtime TODO Log source of query TODO Check postgres version on first call TODO Look through everything TODO Enable all rubocop cops


60
61
62
63
64
# File 'lib/pgb.rb', line 60

def execute(query)
  sql = query.is_a?(Query) || query.is_a?(Command) ? query.to_sql : query
  puts sql
  connection.exec(sql).to_a
end

.schema(table_name) ⇒ Object

TODO Extract TODO Model is not a table name (e.g. query)? TODO Do not select all columns TODO Request as PGB query TODO Support getting schema for all tables TODO Remove line breaks for raw queries?


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pgb.rb', line 80

def schema(table_name)
  table_name = table_name.to_s
  @schema ||= begin
    result = execute <<~SQL
      SELECT *
      FROM information_schema.columns
      WHERE table_schema = 'public'
    SQL
    result.group_by { _1['table_name'] }
  end

  schema = @schema.fetch(table_name) do
    raise Error, "Failed to load schema for #{table_name.inspect}"
  end

  schema.group_by { _1['column_name'] }.transform_values(&:first)
end