Class: PgGraph::Data::SqlRender
- Inherits:
-
Object
- Object
- PgGraph::Data::SqlRender
- Defined in:
- lib/pg_graph/data/render.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#delete ⇒ Object
readonly
Which data to delete: none - don’t delete any data touched - delete data for tables in the fox file recursive - delete data for table in the fox file including recursively depending tables all - delete data from the whole database.
-
#format ⇒ Object
Returns the value of attribute format.
Instance Method Summary collapse
-
#initialize(database, format, ids: {}, delete: :all, files: []) ⇒ SqlRender
constructor
ids
is a map from table UID to ID. - #to_a(files = @files) ⇒ Object
- #to_h ⇒ Object
- #to_s(files = @files) ⇒ Object
Constructor Details
#initialize(database, format, ids: {}, delete: :all, files: []) ⇒ SqlRender
ids
is a map from table UID to ID. Records with larger IDs will be emitted as insert statements, records with IDs less or equal to the given ID is emitted as update statements
delete
control which tables are deleted. It can be :none, :touched, :recursive, :all Only records with an ID greater than the corresponding ID from ids
will be deleted
files
is a list of source file names to be included in the psql SQL header as documentation. It can be set explicitly when #to_a or #to_h is called (FIXME: is this used?)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/pg_graph/data/render.rb', line 31 def initialize(database, format, ids: {}, delete: :all, files: []) # puts "SqlRender#initialize" # puts " format: #{format.inspect}" # puts " ids: #{ids.inspect}" # puts " delete: #{delete.inspect}" # puts " files: #{files.inspect}" constrain database, Database constrain ids, { String => Integer } @database = database self.format = format (@ids = ids.dup).default = 0 @delete = delete @files = files @tables = database.schemas.map(&:tables).flatten.sort @insert_tables = [] @update_tables = [] @insert_records = {} @update_records = [] @tables.each { |table| next if table.empty? @insert_tables << table if table.max_id > @ids[table.uid] @update_tables << table if table.ids.min || 0 <= @ids[table.uid] inserts, updates = table.records.partition { |record| record.id > @ids[table.uid] } @insert_records[table] = inserts if !inserts.empty? @update_records += updates } @table_uids = @tables.select { |table| !table.empty? }.map(&:uid) @materialized_views = @tables.map(&:type).map(&:depending_materialized_views).flatten.uniq end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
5 6 7 |
# File 'lib/pg_graph/data/render.rb', line 5 def database @database end |
#delete ⇒ Object (readonly)
Which data to delete:
none - don't delete any data
touched - delete data for tables in the fox file
recursive - delete data for table in the fox file including recursively depending tables
all - delete data from the whole database
18 19 20 |
# File 'lib/pg_graph/data/render.rb', line 18 def delete @delete end |
#format ⇒ Object
Returns the value of attribute format.
7 8 9 |
# File 'lib/pg_graph/data/render.rb', line 7 def format @format end |
Instance Method Details
#to_a(files = @files) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/pg_graph/data/render.rb', line 62 def to_a(files = @files) case format when :sql; to_sql.flatten when :exec; to_exec.flatten when :psql; to_psql(files).flatten.compact end end |
#to_h ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/pg_graph/data/render.rb', line 78 def to_h @to_h ||= { disable: render_triggers(:disable), delete: render_deletes(delete), update: render_updates, insert: render_inserts, restart: render_restart_sequences, enable: render_triggers(:enable), refresh: render_refresh_materialized_views } end |
#to_s(files = @files) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/pg_graph/data/render.rb', line 70 def to_s(files = @files) case format when :sql; to_a.join("\n") when :exec; to_a.join("\n") when :psql; to_psql(files).map { |group| group.join("\n") }.join("\n\n") end end |