class DrbRelationBuilder

this file is part of manqod manqod is distributed under the CDDL licence the owner of manqod is Dobai-Pataky Balint(dpblnt@gmail.com)

Attributes

moditem[R]

Public Class Methods

new(drbdb,my_id) click to toggle source
# File lib/DrbDB/DrbRelationBuilder.rb, line 9
def initialize(drbdb,my_id)
        @my_id=my_id.to_i
        @drbdb=drbdb
        @title="Relation Builder"
        @relations=Hash.new
        @tables=Hash.new

end

Public Instance Methods

mod_type() click to toggle source
# File lib/DrbDB/DrbRelationBuilder.rb, line 121
def mod_type
        "relation_builder"
end
to_s() click to toggle source
# File lib/DrbDB/DrbRelationBuilder.rb, line 125
def to_s
        "#{@drbdb}.RelationBuilder"
end
update(table=nil,relation=nil) click to toggle source
# File lib/DrbDB/DrbRelationBuilder.rb, line 19
def update(table=nil,relation=nil)
        #specify one table or one relation or nothing

        #clear related relations
        if table.nil? && relation.nil?
                @relations.clear
        else
                #reload table related relations
                @relations.delete_if{|key,val|
                        val["src_table"] == table || val["dst_table"] == table
                } if table
                #reload specified relation
                @relations.delete(relation) if relation
        end
        #clear related tables
        if relation.nil?
                if table.nil?
                        @tables.clear
                else
                        @tables.delete_if{|key,val|
                                val["name"] == table
                        }
                end
        end
        
        #load table or talbes
        if relation.nil?
                @drbdb.rows("select * from tables " + (table ? "where name='#{table}'" : "") ).each{|row|
                        @tables[row["name"]]={
                                "id" => row["id"],
                                "name" => row["name"],
                                "rbx" => row["rbx"].to_f,
                                "rby" => row["rby"].to_f
                        }
                        #indexes
                        begin
                                @tables[row["name"]]["indexes"]=Array.new
                                @drbdb.client.rows("show indexes from #{row['name']}").each{|index|
                                        @tables[row["name"]]["indexes"].push({
                                                "name"=>index["Key_name"],
                                                "unique"=>index["Non_unique"]=="0", 
                                                "field"=>index["Column_name"]
                                        }
                                        )
                                }
                        rescue => err
                                eerror(err)
                        end

                        #is view?
                        is_view=false
                        begin
                                @drbdb.client.fields("show create table `#{row['name']}`").each{|f| is_view=true if f["name"].include?("View")}
                                @tables[row["name"]]["view"]=is_view
                        rescue => err
                                eerror(err)
                        end
                        
                        #fields
                        @tables[row["name"]]["fields"]=Hash.new
                        begin
                                @drbdb.client.rows("show fields from #{row['name']}").each{|field|
                                        fi=field.rehash
                                        if f=field["Type"].index("(")
                                                fi["Size"]=field["Type"][f+1 .. field["Type"].index(")") -1]
                                                fi["Type"]=field["Type"][0 .. f-1]
                                        end
                                        @tables[row["name"]]["fields"][field["Field"]]=fi
                                }
                        rescue => err
                                eerror(err)
                        end

                }
        end
        
        #relations
        if table.nil?
                @drbdb.rows("select * from relations "  + (relation ? "where id='#{relation}'" : "") ).each{|row|
                        @relations[row["id"]]={
                                "id" => row["id"],
                                "src_table" => row["src_table"],
                                "src_field" => row["src_field"],
                                "dst_table" => row["dst_table"],
                                "dst_field" => row["dst_field"],
                                "rel_name" => row['rel_name'],
                                "rel_type" => row["rel_type"],
                                "rbx" => row["rbx"].to_f,
                                "rby" => row["rby"].to_f,
                                "rel_custom" => row['rel_custom']
                        }
                }
        end

        #store in the cache
        @drbdb.cache.set("tables",@tables)
        einfo("#{@tables.size} tables")
        @drbdb.cache.set("relations",@relations)
        einfo("#{@relations.size} relations")
        self
end