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.
-
#truncate ⇒ Object
readonly
Which data to truncate: none - don’t delete any data touched - delete data for tables in the fox file all - delete data from the whole database.
Instance Method Summary collapse
-
#initialize(database, format, ids: {}, delete: :all, truncate: :none, 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, truncate: :none, 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
truncate
acts as delete
that has the major drawback that it doesn’t delete records in dependency order (FIXME This is an error). You can use the SQL truncate statement instead of delete using this option but note that ids
should be empty for this to work
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?)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/pg_graph/data/render.rb', line 42 def initialize(database, format, ids: {}, delete: :all, truncate: :none, 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 } constrain ids.empty? || truncate == :none, true constrain delete == :none || truncate == :none, true @database = database self.format = format (@ids = ids.dup).default = 0 @delete = delete @truncate = truncate @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 |
#truncate ⇒ Object (readonly)
Which data to truncate:
none - don't delete any data
touched - delete data for tables in the fox file
all - delete data from the whole database
24 25 26 |
# File 'lib/pg_graph/data/render.rb', line 24 def truncate @truncate end |
Instance Method Details
#to_a(files = @files) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/pg_graph/data/render.rb', line 76 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
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/pg_graph/data/render.rb', line 92 def to_h @to_h ||= { disable: render_triggers(:disable), delete: render_deletes(delete), truncate: render_truncates(truncate), 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
84 85 86 87 88 89 90 |
# File 'lib/pg_graph/data/render.rb', line 84 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 |