Class: Relation

Inherits:
Object
  • Object
show all
Includes:
ManqodCommon
Defined in:
lib/RelationBuilder/Relation.rb

Overview

this file is part of manqod manqod is distributed under the CDDL licence the author of manqod is Dobai-Pataky Balint([email protected])

Constant Summary

Constants included from ManqodCommon

ManqodCommon::CRITICAL, ManqodCommon::DEBUG, ManqodCommon::ERROR, ManqodCommon::INFO, ManqodCommon::NORMAL, ManqodCommon::WARNING

Constants included from Eprint

Eprint::DOMAIN, Eprint::LEVEL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ManqodCommon

#add_where, #admin, #admin_cache, #admin_qrow, #admin_rows, #backtrace_to_debug, #cache, #changed_ids_of_base, #client, #client_fields, #client_image_of_id, #client_qrow, #client_query, #client_rows, #eeval, #escape_string, #getBinding, #guess_base, #guess_table, #image_of_id, #lzero, #manqod_db, #measure, #myexec, #nick, #nick_id, #number_format, #qrow, #query, #reconnect_manqod_db, #rows, #run_events, #send_message, #sendmail, #set_manqod_db_uri, #set_nick

Methods included from Eprint

#ecode, #edebug, #eerror, #einfo, #enormal, #eprint, #ewarn, #gtk_set_edebug, #set_edebug, #tell_exception

Constructor Details

#initialize(parentM, rel_data) ⇒ Relation

Returns a new instance of Relation.



8
9
10
11
12
13
# File 'lib/RelationBuilder/Relation.rb', line 8

def initialize(parentM,rel_data)
	super()
	@parentM=parentM
	reload(rel_data)
	@shown=false
end

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



14
15
16
# File 'lib/RelationBuilder/Relation.rb', line 14

def handle
  @handle
end

#parentMObject (readonly)

Returns the value of attribute parentM.



14
15
16
# File 'lib/RelationBuilder/Relation.rb', line 14

def parentM
  @parentM
end

#rel_customObject (readonly)

Returns the value of attribute rel_custom.



14
15
16
# File 'lib/RelationBuilder/Relation.rb', line 14

def rel_custom
  @rel_custom
end

#rel_nameObject (readonly)

Returns the value of attribute rel_name.



14
15
16
# File 'lib/RelationBuilder/Relation.rb', line 14

def rel_name
  @rel_name
end

#rel_typeObject (readonly)

Returns the value of attribute rel_type.



14
15
16
# File 'lib/RelationBuilder/Relation.rb', line 14

def rel_type
  @rel_type
end

#relation_idObject (readonly)

Returns the value of attribute relation_id.



14
15
16
# File 'lib/RelationBuilder/Relation.rb', line 14

def relation_id
  @relation_id
end

#rfield1Object (readonly)

Returns the value of attribute rfield1.



14
15
16
# File 'lib/RelationBuilder/Relation.rb', line 14

def rfield1
  @rfield1
end

#rfield2Object (readonly)

Returns the value of attribute rfield2.



14
15
16
# File 'lib/RelationBuilder/Relation.rb', line 14

def rfield2
  @rfield2
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/RelationBuilder/Relation.rb', line 60

def active?
	@handle.widget.active?
end

#editObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/RelationBuilder/Relation.rb', line 99

def edit
	dialog=Gtk::Dialog.new("relation properties",
		@handle.get_ancestor(Gtk::Window),
		Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
		[Gtk::Stock::OK, Gtk::Dialog::RESPONSE_ACCEPT] #,
#			[Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_REJECT]
		)
	dialog.vbox.
		pack_start_defaults(many2one=Gtk::ToggleButton.new("Many to One")).
		pack_start_defaults(rel_dir=Gtk::Button.new("change direction")).
		pack_start_defaults(description=Gtk::Label.new).
		pack_start_defaults(Gtk::HBox.new.pack_start_defaults(Gtk::Label.new("alias")).pack_start_defaults(rel_name_widget=Gtk::Entry.new.set_text(@rel_name))).
		pack_start_defaults(Gtk::HBox.new.pack_start_defaults(Gtk::Label.new("and join condition")).pack_start_defaults(rel_custom_widget=Gtk::Entry.new.set_text(@rel_custom))).
		pack_start_defaults(remove_rel=Gtk::ToggleButton.new("Remove"))
		
	rel_dir.signal_connect('pressed'){|me|
		a=@rfield1
		@rfield1=@rfield2
		@rfield2=a
		description.set_label("#{if many2one.active? then "Many" else "One" end} #{@rfield1.table_name} to One #{@rfield2.table_name}") unless @rfield1.nil? or @rfield2.nil?
	}
	many2one.signal_connect("toggled"){|me|
		description.set_label("#{if many2one.active? then "Many" else "One" end} #{@rfield1.table_name} to One #{@rfield2.table_name}") unless @rfield1.nil? or @rfield2.nil?
	}
	many2one.set_active(rel_type=="m")

	description.set_label("#{if many2one.active? then "Many" else "One" end} #{@rfield1.table_name} to One #{@rfield2.table_name}") unless @rfield1.nil? or @rfield2.nil?
	dialog.signal_connect('response'){|dialog,response|
#			rfield1.set_active(false)
#			rfield2.set_active(false)
		if response == Gtk::Dialog::RESPONSE_ACCEPT
			if remove_rel.active?
				remove
				else
				@rel_name=rel_name_widget.text
				@rel_custom=rel_custom_widget.text
				set_rel_type(if many2one.active? then "m"; else "o";end)
				save
				show_me
			end
		end
		dialog.destroy
	}
	dialog.show_all
end

#hide_meObject



43
44
45
46
47
# File 'lib/RelationBuilder/Relation.rb', line 43

def hide_me
	parentM.widget.remove(@handle) if @shown
	@shown=false
	self
end

#inspectObject



161
162
163
# File 'lib/RelationBuilder/Relation.rb', line 161

def inspect
	"relation #{@rel_name}(#{relation_id}) #{rfield1.table.table}.#{rfield1.field_name} <-> #{rfield2.table.table}.#{rfield2.field_name}"
end

#other_field(rfield) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/RelationBuilder/Relation.rb', line 145

def other_field(rfield)
	if rfield == rfield1
		rfield2
		else
		if rfield == rfield2
			rfield1
			else
			nil
		end
	end
end

#reload(rel) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/RelationBuilder/Relation.rb', line 23

def reload(rel)
	@rfield1=parentM.tables[rel["src_table"]].fields[rel["src_field"]] if parentM.tables.has_key?(rel["src_table"])
	@rfield2=parentM.tables[rel["dst_table"]].fields[rel["dst_field"]] if parentM.tables.has_key?(rel["dst_table"])
	@rel_type=rel["rel_type"]
	@rel_name=rel['rel_name']
	@rel_custom=rel['rel_custom']
	@relation_id=if rel["id"] then rel["id"].to_i else nil;end
	if @handle then
		@handle.move_me(rel["rbx"].to_f,rel["rby"].to_f)
	else
		@handle=RelationHandle.new(self,rel["rbx"].to_f,rel["rby"].to_f)
	end
end

#removeObject



91
92
93
94
95
96
97
# File 'lib/RelationBuilder/Relation.rb', line 91

def remove
	query("delete from relations where id='#{@relation_id}'") if !@relation_id.nil?
	parentM.relations.delete(self)
	parentM.clear
	@handle.destroy unless @handle.destroyed?
	@parentM.reload_server_rb(true,nil,@relation_id)
end

#saveObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/RelationBuilder/Relation.rb', line 71

def save
	if @relation_id.nil?
		query("insert into relations set rel_type='#{@rel_type}'")
		@relation_id=qrow("select id from `relations` order by id desc limit 1")["id"]
		parentM.clear
		einfo("inserted","relation-builder")
	end
	if rfield1.nil? or rfield2.nil?
		ewarn("id:#{@relation_id} has a lost table","relation-builder")
	else
		query("update relations set src_table = '#{@rfield1.table.table}', dst_table = '#{@rfield2.table.table}', src_field='#{@rfield1.field["Field"]}', dst_field='#{@rfield2.field["Field"]}', rel_type='#{@rel_type}', rbx='#{@handle.x}', rby='#{@handle.y}', rel_name='#{@rel_name}', rel_custom='#{@rel_custom}' where id='#{@relation_id}'")
		einfo("updated","relation-builder")
		parentM.reload_server_rb(true,nil,@relation_id)
	end
end

#set_active(new_a) ⇒ Object



63
64
65
# File 'lib/RelationBuilder/Relation.rb', line 63

def set_active(new_a)
	@handle.widget.set_active(new_a)
end

#set_rel_type(rel_type) ⇒ Object



67
68
69
# File 'lib/RelationBuilder/Relation.rb', line 67

def set_rel_type(rel_type)
	@rel_type=rel_type
end

#set_rfield2(new_rfield2 = rfield2) ⇒ Object



87
88
89
# File 'lib/RelationBuilder/Relation.rb', line 87

def set_rfield2(new_rfield2=rfield2)
	@rfield2=new_rfield2
end

#set_visibility(vis) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/RelationBuilder/Relation.rb', line 48

def set_visibility(vis)
	if vis
		show_me
		else
		hide_me
	end
end

#show_meObject



37
38
39
40
41
42
# File 'lib/RelationBuilder/Relation.rb', line 37

def show_me
	parentM.widget.put(@handle,@handle.x,@handle.y) unless @shown
	@handle.show_all
	@shown=true
	self
end

#shown?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/RelationBuilder/Relation.rb', line 56

def shown?
	@shown
end

#to_sObject



164
165
166
# File 'lib/RelationBuilder/Relation.rb', line 164

def to_s
	"relation #{rel_type} of #{rfield1} and #{rfield2}"
end

#visible?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/RelationBuilder/Relation.rb', line 157

def visible?
	@handle && !@handle.destroyed? && @handle.visible?
end

#xObject



16
17
18
# File 'lib/RelationBuilder/Relation.rb', line 16

def x
	@handle.x
end

#yObject



19
20
21
# File 'lib/RelationBuilder/Relation.rb', line 19

def y
	@handle.y
end