1 # (c) Yves Fischer yvesf - xapek org, 2007, License: GPL-2
5 from pgw
.Plugin
import SidebarPlugin
;
8 class RowedListBox(gtk
.ScrolledWindow
):
10 gtk
.ScrolledWindow
.__init
__(self
)
11 self
.set_policy(gtk
.POLICY_AUTOMATIC
, gtk
.POLICY_ALWAYS
)
12 self
.set_shadow_type(gtk
.SHADOW_NONE
)
13 self
.view
= gtk
.TreeView()
16 # [[CAPTION1, CAPTION2, ...] [DATA1, DATA2, ...] ]
17 def set_array(self
, array
):
18 if len(array
) == 0 or len(array
[0]) == 0:
20 #XXX !!!1!!11!!!11111111 dont look at this, it works but...
21 function
= gtk
.ListStore
22 self
.store
= apply(function
, (str,) * len(array
[0]) )
26 self
.view
.set_model(self
.store
)
29 array
.remove(array
[0])
30 col
= caption
[0]; caption
.remove(caption
[0])
31 cell
= gtk
.CellRendererText()
32 column
= gtk
.TreeViewColumn(str(col
).replace("_", " "))
33 column
.pack_start(cell
, True)
34 column
.add_attribute(cell
, 'text', 0)
35 column
.set_sort_column_id(0)
36 self
.view
.append_column(column
)
38 for col
in caption
: #pop caption
39 cell
= gtk
.CellRendererText()
40 column
= gtk
.TreeViewColumn(str(col
).replace("_", " "))
41 column
.pack_start(cell
, True)
42 column
.add_attribute(cell
, 'text', 0)
43 self
.view
.append_column(column
)
53 self
.store
.append(row
)
56 tuple, treeview
= self
.view
.get_cursor()
59 return self
.store
[tuple[0]]
61 class TextWidget(gtk
.ScrolledWindow
):
63 gtk
.ScrolledWindow
.__init
__(self
)
64 self
.set_policy(gtk
.POLICY_AUTOMATIC
, gtk
.POLICY_AUTOMATIC
)
65 self
.set_shadow_type(gtk
.SHADOW_NONE
)
67 self
.buffer = gtk
.TextBuffer()
68 self
.syntax
= pgw
.Syntax
.Syntax(self
.buffer)
70 self
.buffer.connect('delete-range', self
.syntax
.text_deleted
)
71 self
.buffer.connect('insert-text', self
.syntax
.text_inserted
)
72 self
.buffer.connect('changed', self
.syntax
.text_changed
)
74 self
.view
= gtk
.TextView(self
.buffer)
79 def set_text(self
, text
):
80 self
.buffer.set_text(text
)
82 return self
.buffer.get_text( \
83 self
.buffer.get_start_iter(), self
.buffer.get_end_iter() )
86 class Dropdown(gtk
.ComboBox
):
88 self
.store
= gtk
.ListStore( str )
89 gtk
.ComboBox
.__init
__(self
, self
.store
)
91 cell
= gtk
.CellRendererText()
92 self
.pack_start(cell
, True)
93 self
.add_attribute(cell
, 'text', 0)
95 def refresh(self
, array
):
96 active_text
= self
.get_active_text()
101 self
.store
.append([ line
])
102 if line
== active_text
:
109 class PgStats(SidebarPlugin
):
112 SidebarPlugin
.__init
__(self
)
113 self
.vpaned
= gtk
.VPaned()
115 self
.vcontainer
= gtk
.VBox()
118 self
.combobox_table
= Dropdown()
119 self
.combobox_table
.set_property("height-request", 30)
120 self
.combobox_table
.connect("changed", self
.refresh_data
)
122 self
.button_refresh
= gtk
.Button("Refresh")
123 self
.button_refresh
.set_property("width-request", 60)
124 self
.button_refresh
.set_property("height-request", 30)
125 self
.button_refresh
.connect("clicked", self
.refresh
)
127 self
.filterbox1
= gtk
.HBox()
128 self
.filterbox1
.pack_start(self
.combobox_table
, True, True)
129 self
.pack_start(self
.filterbox1
, False, True)
131 self
.filterbox2
= gtk
.HBox()
132 self
.filterbox2
.add(self
.button_refresh
)
133 self
.pack_start(self
.filterbox2
, False, True)
135 self
.listbox
= RowedListBox()
136 self
.vcontainer
.add(self
.listbox
)
138 self
.add(self
.vcontainer
)
143 def on_connection(self
):
146 def refresh(self
,widget
=None):
147 self
.refresh_table() #=> Calls refresh_data via event from table-column ding
149 def refresh_table(self
):
151 for row
in self
.query("SELECT DISTINCT(tablename) FROM pg_stats;"):
152 tables
.append(row
[0])
153 self
.combobox_table
.refresh(tables
)
156 def refresh_data(self
, widget
=None):
157 if self
.combobox_table
.get_active_text() == _("All"):
160 filter_table
= self
.combobox_table
.get_active_text()
162 data
= [ ["tablename", "attname", "null_frac", "avg_width", "n_distinct", "most_common_vals", "most_common_freqs", "histogram_bounds", "correlation"] ]
163 columns
= ", ".join(data
[0])
165 sql
= "SELECT %s FROM pg_stats WHERE tablename LIKE '%s';"%(columns
, filter_table
)
168 for row
in self
.query(sql
):
170 self
.listbox
.set_array(data
)
172 def query(self
, sql
):
173 if not vars(self
.app
).has_key("db") or not self
.app
.db
.is_connected
:
175 q
= self
.app
.db
.query(sql
)
176 if not q
.has_key("error") and q
.has_key("cursor"):
177 return q
['cursor'].fetchall()
182 def cursor(self, widget, data=None):
183 name = self.listbox.get()[0]
184 if not vars(self.app).has_key("db") or not self.app.db.is_connected:
185 print "FunctionBrowser: No Connection!"
187 query = self.app.db.query("SELECT prosrc AS sprache FROM pg_catalog.pg_proc WHERE proname = '%s'" %(name))
188 result = query['cursor'].fetchall()
189 self.text.set_text(result[0][0])
191 def refresh(self, widget=None):
192 if not vars(self.app).has_key("db") or not self.app.db.is_connected:
194 self.button.set_relief( gtk.RELIEF_NONE )
197 # sql = """SELECT DISTINCT(lanname)
198 # FROM pg_catalog.pg_language AS L, pg_catalog.pg_proc AS P
199 # WHERE int4(L.lanplcallfoid)+1 = int4(P.prolang);"""
200 """ query = self.app.db.query(sql);
202 results = query['cursor'].fetchall()
203 active = self.combobox.get_active_text()
204 self.liststore.clear()
205 self.liststore.append([_("All")])
208 for result in results:
210 self.liststore.append([result[0]])
211 if result[0] == active: #take-over old selection
215 print "FunctionBrowser: Error, unexpected result" + str(result)
216 self.combobox.set_active(j)
219 filter = self.combobox.get_active_text()
220 if filter == _("All") or filter == None:
222 """# sql = """SELECT proname
223 # FROM pg_catalog.pg_language AS L, pg_catalog.pg_proc AS P
225 # int4(L.lanplcallfoid)+1 = int4(P.prolang)
226 # AND L.lanname LIKE '%s';""" % filter
227 x
= """ got = self.app.db.query(sql)
228 cursor = got['cursor']
229 results = cursor.fetchall()
233 store.append([line[0]])
235 self.listbox.set_array(store)
237 self.button.set_relief( gtk.RELIEF_NORMAL )