Class: PgGraph::Data::Database

Inherits:
DatabaseObject show all
Defined in:
lib/pg_graph.rb,
lib/pg_graph/data/data.rb,
lib/pg_graph/data/read.rb

Instance Attribute Summary

Attributes inherited from Node

#dimension, #type

Instance Method Summary collapse

Methods inherited from DatabaseObject

#<=>, #guid, #name, #uid

Methods inherited from Node

#inspect, #object, #value, #value_type

Constructor Details

#initialize(type, data_source = nil) ⇒ Database

data_source can be a PgConn object, a Hash, or nil



85
86
87
88
89
90
91
92
93
# File 'lib/pg_graph/data/data.rb', line 85

def initialize(type, data_source = nil)
  constrain type, PgGraph::Type::Database
  constrain data_source, PgConn, Hash, NilClass
  @queued_objects = [] # has to go before super
  @objects = nil # Object cache. Maps from UID to object. Has to go before super
  super(type)
  initialize_impl
  read(data_source) if data_source
end

Instance Method Details

#==(other) ⇒ Object



118
# File 'lib/pg_graph/data/data.rb', line 118

def ==(other) to_h == other.to_h end

#dataObject Also known as: to_h

Return database as a hash from schema name to schema hash



107
# File 'lib/pg_graph/data/data.rb', line 107

def data() @impl.map { |k,v| [k, v.data] }.to_h end

#databaseObject

Redefine DatabaseObject#database to return self



79
# File 'lib/pg_graph/data/data.rb', line 79

def database() self end

#dot(uid) ⇒ Object

Return object by the given UID



110
# File 'lib/pg_graph/data/data.rb', line 110

def dot(uid) @objects ||= map_objects[uid] end

#dupObject



95
# File 'lib/pg_graph/data/data.rb', line 95

def dup() Database.new(type, to_h) end

#each(&block) ⇒ Object

Iterate schemas



104
# File 'lib/pg_graph/data/data.rb', line 104

def each(&block) schemas.each { |schema| yield schema } end

#is_a?(klass) ⇒ Boolean

Make Database pretend it is a PgGraph::Data object

Returns:

  • (Boolean)


111
# File 'lib/pg_graph.rb', line 111

def is_a?(arg) arg == PgGraph::Data || super end

#lookup(path, id = nil) ⇒ Object

TODO: Maybe use this instead of #dot



113
114
115
116
# File 'lib/pg_graph/data/data.rb', line 113

def lookup(path, id = nil)
  object = (@objects ||= map_objects)[path]
  id.nil? ? object : object[id]
end

#read(arg) ⇒ Object

:call-seq:

read(connection)
read(hash)
read(yaml)

Read data from a source. Returns self



147
148
149
150
151
152
153
154
# File 'lib/pg_graph/data/data.rb', line 147

def read(arg)
  constrain arg, PgConn, Hash
  case arg
    when PgConn; read_connection(arg)
    when Hash; read_hash(arg)
  end
  self
end

#read_connection(conn) ⇒ Object

load data from connection



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pg_graph/data/read.rb', line 7

def read_connection(conn)
  constrain conn, PgConn::Connection
  initialize_impl
  for schema in type.schemas
    for table in schema.tables
      for record in conn.records("select * from #{table.uid}")
        Record.new(self[schema.name][table.name], record)
      end
    end
  end
  self
end

#read_hash(hash) ⇒ Object

load data from hash. The hash can be produced by #to_h or #to_yaml

TODO: Check for unknown keys



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pg_graph/data/read.rb', line 23

def read_hash(hash)
  constrain hash, Hash
  initialize_impl

  # Works on to #to_h and #to_yaml hashes because it is easier to handle
  # the two formats dynamically than detecting which format to use
  for schema in type.schemas
    next if !hash.key?(schema.name)
    hash_schema = hash[schema.name]
    for table in schema.tables
      next if !hash[schema.name].key?(table.name)
      hash_table = hash_schema[table.name]
      records = hash_table.is_a?(Hash) ? hash_table.values : hash_table
      for record in records
        Record.new(self[schema.name][table.name], record)
      end
    end
  end
  self
end

#schemasObject

List of Schema objects



82
# File 'lib/pg_graph/data/data.rb', line 82

def schemas() @impl.values end

#to_exec_sql(ids: {}, delete: :all) ⇒ Object



132
133
134
# File 'lib/pg_graph/data/data.rb', line 132

def to_exec_sql(ids: {}, delete: :all)
  to_sql(format: :exec, ids: ids, delete: delete)
end

#to_psql_sql(files = [], ids: {}, delete: :all) ⇒ Object



136
137
138
# File 'lib/pg_graph/data/data.rb', line 136

def to_psql_sql(files = [], ids: {}, delete: :all)
  to_sql(format: :psql, ids: ids, delete: delete, files: files)
end

#to_sql(format: :sql, ids: {}, delete: :all, truncate: :none, files: []) ⇒ Object

Note that #to_sql in derived classes should be deleted FIXME



127
128
129
130
# File 'lib/pg_graph/data/data.rb', line 127

def to_sql(format: :sql, ids: {}, delete: :all, truncate: :none, files: [])
  render = SqlRender.new(self, format, ids: ids, delete: delete, truncate: truncate, files: files)
  render.to_s
end

#to_yamlObject

The #to_yaml format is like #to_h but with records formatted as an array instead of as a map from ID to record



124
# File 'lib/pg_graph/data/data.rb', line 124

def to_yaml() @impl.map { |k,v| [k, v.to_yaml] }.to_h end

#write(connection, ids: {}, delete: :all) ⇒ Object

Write data to database. ids is a hash from table UID to id. If a record has an ID equal to or less than the corresponding ID in ids, the SQL will be rendered as an ‘update’ statement instead of an ‘insert’ statement. Returns self



160
161
162
163
164
165
# File 'lib/pg_graph/data/data.rb', line 160

def write(connection, ids: {}, delete: :all)
  constrain connection, PgConn
  constrain ids, { String => Integer }
  connection.exec(to_exec_sql(ids: ids, delete: delete))
  self
end