Module: PGB

Defined in:
lib/pgb.rb,
lib/pgb.rb,
lib/pgb/user.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, User

Constant Summary collapse

SUPPORTED_PG_VERSIONS =
(11..15).freeze
WORKING_DIR =
File.expand_path(__dir__)
VERSION =
'0.1.1'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loaderObject

Returns the value of attribute loader.



31
32
33
# File 'lib/pgb.rb', line 31

def loader
  @loader
end

Class Method Details

.connectionObject

TODO Connection lost? Reconnect? Different version?



75
76
77
78
79
80
81
# File 'lib/pgb.rb', line 75

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

.ensure_pg_version_supported(connection) ⇒ Object

TODO Check expected response format



99
100
101
102
103
104
105
106
# File 'lib/pgb.rb', line 99

def ensure_pg_version_supported(connection)
  sql = 'SELECT version()'
  log(sql)
  version = Integer(connection.exec(sql).first.fetch('version').match(/PostgreSQL (\d+)\./)[1])
  unless SUPPORTED_PG_VERSIONS.include?(version)
    raise Error, "Unsupported Postgres version: #{version}, supported: #{SUPPORTED_PG_VERSIONS}"
  end
end

.evaluate(&block) ⇒ Object



50
51
52
# File 'lib/pgb.rb', line 50

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 TODO Coloring if supported



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

def execute(query)
  sql = query.is_a?(Query) || query.is_a?(Command) ? query.to_sql : query.strip.tr("\n", ' ')
  # TODO Weird
  connection
  log(sql)
  connection.exec(sql).to_a
end

.log(sql) ⇒ Object

TODO Different backtrace order in different versions? TODO Disable sql logging? TODO Disable caller logging?



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pgb.rb', line 86

def log(sql)
  log_line = sql

  frame = caller.find { !_1['/lib/ruby/gems/'] }
  if frame
    frame = frame.gsub("#{WORKING_DIR}/", '').strip
    log_line += " -- #{frame}" unless frame.empty?
  end

  puts log_line
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? TODO Check expected response format



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/pgb.rb', line 115

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