Class: RTable

Inherits:
Gtk::EventBox
  • Object
show all
Includes:
ManqodCommon
Defined in:
lib/RelationBuilder/RTable.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, td) ⇒ RTable

Returns a new instance of RTable.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/RelationBuilder/RTable.rb', line 7

def initialize(parentM,td)
	@parentM=parentM
	@shown=false
	super()
	visible_window=false
	@dragging=false
	@widget=Gtk::Frame.new
	@widget.set_label_widget(Gtk::HBox.new(false,1))
	@widget.label_widget.pack_start(@label=Gtk::Label.new)
	@widget.label_widget.pack_start(@frame_button=Gtk::Button.new.set_relief(Gtk::ReliefStyle::NONE))
	add(@widget)
	@widget.add(Gtk::VButtonBox.new)
	@widget.set_label_xalign(0.5)
	@fields=Hash.new
	update(td)
	
	signal_connect('button-press-event'){|me,ev|
		parentM.set_focused_item(self)
		@parentM.clear
		case ev.button
			when 2
				@xshift=ev.x
				@yshift=ev.y
				@dragging=true
			when 3
				menu=Gtk::Menu.new
				menu.append(rent=Gtk::ImageMenuItem.new("rename table").set_image(Gtk::Image.new(Gtk::Stock::EDIT,Gtk::IconSize::MENU)))
				rent.signal_connect("activate",self){|me,table|
					dialog=Gtk::MessageDialog.new(get_ancestor(Gtk::Window),Gtk::Dialog::Flags::MODAL,Gtk::MessageDialog::QUESTION,Gtk::MessageDialog::ButtonsType::YES_NO,"rename table?")
					dialog.vbox.pack_start(new_table=Gtk::Entry.new.set_text(@table))
					dialog.show_all
					if dialog.run == Gtk::Dialog::ResponseType::YES
						rename_me(new_table.text)
					end
					dialog.destroy
				}

				menu.append(rt=Gtk::ImageMenuItem.new("remove table").set_image(Gtk::Image.new(Gtk::Stock::REMOVE,Gtk::IconSize::MENU)))
				rt.signal_connect("activate",self){|me,table|
					dialog=Gtk::MessageDialog.new(get_ancestor(Gtk::Window),Gtk::Dialog::Flags::MODAL,Gtk::MessageDialog::QUESTION,Gtk::MessageDialog::ButtonsType::YES_NO,"remove table?")
					dialog.vbox.pack_start(remove_from_db=Gtk::ToggleButton.new("remove from database",false).set_draw_indicator(false))
					dialog.show_all
					if dialog.run == Gtk::Dialog::ResponseType::YES
						parentM.remove_table(@table,remove_from_db.active?)
					end
					dialog.destroy
				}

				menu.append(tt=Gtk::ImageMenuItem.new("truncate table").set_image(Gtk::Image.new(Gtk::Stock::DELETE,Gtk::IconSize::MENU)))
				tt.signal_connect("activate",self){|me,table|
						truncate_table
				}

			menu.show_all
			menu.popup(nil,nil,ev.button,ev.time)
			menu.signal_connect("deactivate",self){|menu,table|
				table.parentM.set_focused_item(nil)
			}
		end
		true
	}
	signal_connect('motion-notify-event',parentM){|me,event,parentM|
		if @dragging
			@x=parentM.widget.pointer[0]-@xshift 
			@y=parentM.widget.pointer[1]-@yshift 
			move_me
		end
		true
	}
	signal_connect('button-release-event'){|me,event|
		move_me
		save_me
		parentM.set_focused_item(nil)
		true
	}
end

Instance Attribute Details

#draggingObject

Returns the value of attribute dragging.



84
85
86
# File 'lib/RelationBuilder/RTable.rb', line 84

def dragging
  @dragging
end

#fieldsObject (readonly)

Returns the value of attribute fields.



83
84
85
# File 'lib/RelationBuilder/RTable.rb', line 83

def fields
  @fields
end

#indexesObject (readonly)

Returns the value of attribute indexes.



83
84
85
# File 'lib/RelationBuilder/RTable.rb', line 83

def indexes
  @indexes
end

#parentMObject (readonly)

Returns the value of attribute parentM.



83
84
85
# File 'lib/RelationBuilder/RTable.rb', line 83

def parentM
  @parentM
end

#tableObject (readonly)

Returns the value of attribute table.



83
84
85
# File 'lib/RelationBuilder/RTable.rb', line 83

def table
  @table
end

#table_idObject (readonly)

Returns the value of attribute table_id.



83
84
85
# File 'lib/RelationBuilder/RTable.rb', line 83

def table_id
  @table_id
end

#widgetObject (readonly)

Returns the value of attribute widget.



83
84
85
# File 'lib/RelationBuilder/RTable.rb', line 83

def widget
  @widget
end

#xObject (readonly)

Returns the value of attribute x.



83
84
85
# File 'lib/RelationBuilder/RTable.rb', line 83

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



83
84
85
# File 'lib/RelationBuilder/RTable.rb', line 83

def y
  @y
end

Instance Method Details

#alter_field(alter_field = nil, after_field = nil) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/RelationBuilder/RTable.rb', line 222

def alter_field(alter_field=nil,after_field=nil)
	dialog=Gtk::MessageDialog.new(get_ancestor(Gtk::Window),Gtk::Dialog::Flags::MODAL,Gtk::MessageDialog::QUESTION,Gtk::MessageDialog::ButtonsType::OK_CANCEL,if after_field then "add after `#{after_field}`" else "alter `#{alter_field["Field"]}`" end).set_title("Field preferences")
	i=0
	dialog.vbox.pack_start(Gtk::Table.new(2,2).
		attach_defaults(Gtk::Label.new("name"),0,1,i,i+1).attach_defaults(field_name=Gtk::Entry.new,1,2,i,i+=1).
		attach_defaults(Gtk::Label.new("type"),0,1,i,i+1).attach_defaults(field_type=Gtk::Combo.new(["TEXT","TINYINT","INT","BIGINT","REAL","DOUBLE","FLOAT","NUMERIC","DATE","TIME","TIMESTAMP","DATETIME","YEAR","CHAR","VARCHAR","BINARY","VARBiNARY","BLOB","TINYBLOB","MEDIUMBLOB","LONGBLOB","TINYTEXT"]).set_allow_empty(false),1,2,i,i+=1).
		attach_defaults(Gtk::Label.new("size"),0,1,i,i+1).attach_defaults(field_size=Gtk::Entry.new,1,2,i,i+=1).
		attach_defaults(Gtk::Label.new("default"),0,1,i,i+1).attach_defaults(field_default=Gtk::Entry.new,1,2,i,i+=1).
		attach_defaults(bb=Gtk::HButtonBox.new,0,2,i,i+=1)
	)
	bb.
		pack_start(field_null=Gtk::ToggleButton.new("NOT NULL",false).set_image(Gtk::Image.new(Gtk::Stock::CLEAR,Gtk::IconSize::MENU))).
		pack_start(field_index=Gtk::ToggleButton.new("NOT INDEX",false).set_image(Gtk::Image.new(Gtk::Stock::INDEX,Gtk::IconSize::MENU)))
	field_null.signal_connect("toggled"){|me| me.set_label(me.active? ? "NULL" : "NOT NULL")}
	field_index.signal_connect("toggled"){|me| me.set_label(me.active? ? "INDEX" : "NOT INDEX")}
	if alter_field
		field_name.set_text(alter_field["Field"]) 
		field_type.entry.set_text(alter_field["Type"])
		field_size.set_text(alter_field['Size'] || "")
		field_default.set_text(alter_field['Default'] || "")
		field_null.set_active(alter_field["Null"] == "YES")
		field_index.set_active(alter_field["Key"] == "MUL")
	end
	dialog.show_all
	if dialog.run == Gtk::Dialog::ResponseType::OK
		if after_field
			sql="ALTER TABLE `#{@table}` ADD `#{field_name.text}` 
			#{field_type.entry.text}#{field_size.text!="" ? "(#{field_size.text})" : ""}
			#{field_null.active? ? "NULL" : "NOT NULL"} 
			#{field_default.text!="" ? "DEFAULT '#{field_default.text}' " : ""} 
			AFTER `#{after_field}`";
			sql2=field_index.active? ? "alter table `#{@table}` add index(`#{field_name.text}`)" : nil
			else
			if alter_field["Field"] != field_name.text
				sql="ALTER TABLE `#{@table}` CHANGE `#{alter_field["Field"]}` `#{field_name.text}` 
				#{field_type.entry.text}#{field_size.text!="" ? "(#{field_size.text})":""}
				#{field_null.active? ? "NULL" : "NOT NULL"} 
				#{field_default.text!="" ? "DEFAULT '#{field_default.text}' ":""}"
			sql2=field_index.active? != (alter_field["Key"] == "MUL") ? field_index.active? ? "alter table `#{@table}` add index(`#{field_name.text}`)" : "alter table `#{@table}` drop index `#{field_name.text}`" : nil
				@fields[alter_field["Field"]].destroy
				@fields.delete(alter_field["Field"])
			else
				sql="ALTER TABLE `#{@table}` MODIFY `#{field_name.text}` 
				#{field_type.entry.text}#{field_size.text!="" ? "(#{field_size.text})":""}
				#{field_null.active? ? "NULL" : "NOT NULL"} 
				#{field_default.text!="" ? "DEFAULT '#{field_default.text}' ":""}"
			sql2=field_index.active? != (alter_field["Key"] == "MUL") ? field_index.active? ? "alter table `#{@table}` add index(`#{field_name.text}`)" : "alter table `#{@table}` drop index `#{field_name.text}`" : nil
			end
		end
		
		begin
			client.query(sql)
			client.query(sql2) if sql2
		rescue =>err
			retry if tell_exception("error altering the table",backtrace_to_debug(err),"server","error",false,true,err)
		end
		parentM.reload_server_rb(true,@table)
	end
	dialog.destroy
end

#center_on_meObject



289
290
291
292
293
294
295
296
# File 'lib/RelationBuilder/RTable.rb', line 289

def center_on_me
	show_neighbours
	widget.realize unless widget.realized?
	sx,sy = widget.translate_coordinates(@parentM.widget,-@parentM.allocation.width/3,-@parentM.allocation.height/3)
	sx1,sy1 = widget.translate_coordinates(@parentM.widget,widget.allocation.width+@parentM.allocation.height/3,widget.allocation.height+@parentM.allocation.height/3)
	@parentM.hadjustment.clamp_page(sx,sx1)
	@parentM.vadjustment.clamp_page(sy,sy1)
end

#hide_meObject



164
165
166
167
168
# File 'lib/RelationBuilder/RTable.rb', line 164

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

#inspectObject



283
284
285
# File 'lib/RelationBuilder/RTable.rb', line 283

def inspect
	"RTable:#{table}"
end

#is_view?Boolean

Returns:

  • (Boolean)


297
298
299
# File 'lib/RelationBuilder/RTable.rb', line 297

def is_view?
	@is_view
end

#move_me(newx = @x, newy = @y) ⇒ Object



131
132
133
134
135
# File 'lib/RelationBuilder/RTable.rb', line 131

def move_me(newx=@x,newy=@y)
	@x=newx
	@y=newy
	parent.move(self,@x,@y) if parent
end

#reloadObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/RelationBuilder/RTable.rb', line 111

def reload
=begin
	@indexes.clear
	client_rows("show indexes from #{table}"){|index|
		@indexes.push({"name"=>index["Key_name"],"unique"=>index["Non_unique"]=="0", "field"=>index["Column_name"]})
	}
=end
	i=0
	@fields_data.each_pair{|field_name,field|
		if @fields.has_key?(field["Field"])
			@fields[field["Field"]].set_field(field)
			else
			@widget.child.pack_start_defaults(@fields[field["Field"]]=RField.new(self,field)) 
		end
		@widget.child.reorder_child(@fields[field["Field"]],i)
		i+=1
	}
	show_all
end

#remove_field(field) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/RelationBuilder/RTable.rb', line 193

def remove_field(field)
	dialog=Gtk::MessageDialog.new(nil,Gtk::Dialog::Flags::MODAL,Gtk::MessageDialog::QUESTION,Gtk::MessageDialog::ButtonsType::YES_NO,"remove field `#{field}`?")
	dialog.show_all
	if dialog.run == Gtk::Dialog::ResponseType::YES
		parentM.relations.each_value{|rel|
			if rel.rfield1 == @fields[field] or rel.rfield2 == @fields[field]
				edebug("removing #{rel}","relation-builder","debug")
				rel.remove
			end
		}

		client.query("alter table `#{table}` drop `#{field}`")
		@fields[field].destroy
		@fields.delete(field)
		parentM.clear
		parentM.reload_server_rb(true,@table)
	end
	dialog.destroy
end

#rename_me(new_table) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/RelationBuilder/RTable.rb', line 137

def rename_me(new_table)
	client_query("rename table `#{@table}` to `#{new_table}`")
	@table=new_table
	@fields.each{|field,field_wg|
		parentM.relations.each_value{|rel|
			if rel.rfield1 == field_wg or rel.rfield2 == field_wg
				rel.save
			end
		}
	}

	save_me
	set_table(@table)
	parentM.reload_server_rb(true,@table)
end

#save_meObject



153
154
155
156
# File 'lib/RelationBuilder/RTable.rb', line 153

def save_me
	query("update tables set rbx='#{x}', rby='#{y}', name='#{@table}' where id='#{table_id}'")
	@parentM.reload_server_rb(false,@table)
end

#set_table(table) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/RelationBuilder/RTable.rb', line 86

def set_table(table)
	@table=table
	@label.set_label(table)
	unless table.nil?
		if is_view?
			@frame_button.set_image(Gtk::Image.new(Gtk::Stock::ABOUT,Gtk::IconSize::MENU))
			@widget.set_shadow_type(Gtk::ShadowType::IN).modify_bg(Gtk::StateType::NORMAL,Gdk::Color.parse('#E0D130'))
			else
			@frame_button.set_image(Gtk::Image.new(Gtk::Stock::FILE,Gtk::IconSize::MENU))
			@widget.set_shadow_type(Gtk::ShadowType::OUT)
		end
		reload
	end
end

#set_visibility(vis) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/RelationBuilder/RTable.rb', line 170

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

#show_meObject



158
159
160
161
162
# File 'lib/RelationBuilder/RTable.rb', line 158

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

#show_neighboursObject



182
183
184
185
186
187
188
189
190
191
# File 'lib/RelationBuilder/RTable.rb', line 182

def show_neighbours
	show_me
	@parentM.relations.each_value{|rel|
		if rel.rfield1.table == self || rel.rfield2.table == self
			rel.rfield1.table.show_me
			rel.rfield2.table.show_me
			rel.show_me
		end
	}
end

#shown?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/RelationBuilder/RTable.rb', line 178

def shown?
	@shown
end

#to_sObject



286
287
288
# File 'lib/RelationBuilder/RTable.rb', line 286

def to_s
	inspect
end

#truncate_tableObject



213
214
215
216
217
218
219
220
# File 'lib/RelationBuilder/RTable.rb', line 213

def truncate_table
	dialog=Gtk::MessageDialog.new(nil,Gtk::Dialog::Flags::MODAL,Gtk::MessageDialog::QUESTION,Gtk::MessageDialog::ButtonsType::YES_NO,"truncate table?")
	dialog.show_all
	if dialog.run == Gtk::Dialog::ResponseType::YES
		client.query("truncate table `#{table}`")
	end
	dialog.destroy
end

#update(td) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/RelationBuilder/RTable.rb', line 101

def update(td)
	@table_id=(td["id"] ? td["id"].to_i : nil)
	@x=td["rbx"]
	@y=td["rby"]
	@fields_data=td["fields"]
	@indexes=td["indexes"]
	@is_view=td["view"]
	set_table(td["name"])
end