require "tempfile" module FixtureFox class Idr # Qualified names of controlled tables def tables() @tables_hash.keys end # List of materialized views that depends on the tables. Assigned by the analyzer # FIXME: Is this in use? attr_accessor :materialized_views # Data as a hash from schema to table to id to record to field to value. # Ie. { "schema" => { "table" => { 1 => { id: 1, name: "Alice" } } } } attr_reader :data # Map from qualified table name to last used ID. FIXME: Unused? attr_reader :ids def initialize @tables_hash = {} @data = {} end # Number of records def count() count = 0 @data.each { |_, tables| tables.each { |_, records| count += records.size } } count end # Add a field to the data representation. If field and value are nil, an # empty record will be created. If id is also nil, an empty table is # created # def put(schema, table, id = nil, field = nil, value = nil) # puts "Idr.put(#{schema}, #{table}, #{id.inspect}, #{field.inspect}, #{value.inspect})" !id.nil? || field.nil? or raise ArgumentError raise if table.to_s =~ /[A-Z]/ uid = "#{schema}.#{table}" @tables_hash[uid] = true return if id.nil? tuple = ((@data[schema] ||= {})[table] ||= {})[id] ||= {id: id} tuple[field.to_sym] = value if field end alias_method :to_h, :data def dump data.sort_by(&:first).each { |schema, tables| puts schema tables.each { |table, records| puts " #{table}" records.each { |id, fields| puts " #{fields.inspect}" } } } end end end