del net-oscar
[learning-git.git] / pgworksheet_yvesf / plugins / sidebar / PgStats.py
blob4bd55e5ed0babdbd8bef76a705bfa162ad51b677
1 # (c) Yves Fischer yvesf - xapek org, 2007, License: GPL-2
3 import pygtk;
4 import gtk;
5 from pgw.Plugin import SidebarPlugin;
6 import pgw.Syntax;
8 class RowedListBox(gtk.ScrolledWindow):
9 def __init__(self):
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()
14 self.add(self.view)
16 # [[CAPTION1, CAPTION2, ...] [DATA1, DATA2, ...] ]
17 def set_array(self, array):
18 if len(array) == 0 or len(array[0]) == 0:
19 return
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]) )
23 #XXX its done
24 self.store.clear()
25 self.view.set_model()
26 self.view.set_model(self.store)
28 caption = array[0]
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)
45 data = []
46 for row in array:
47 x = []
48 for col in row:
49 x.append(str(col))
50 data.append(x)
52 for row in data:
53 self.store.append(row)
55 def get(self):
56 tuple, treeview = self.view.get_cursor()
57 if tuple == None:
58 return None
59 return self.store[tuple[0]]
61 class TextWidget(gtk.ScrolledWindow):
62 def __init__(self):
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)
76 self.add(self.view)
79 def set_text(self, text):
80 self.buffer.set_text(text)
81 def get_text(self):
82 return self.buffer.get_text( \
83 self.buffer.get_start_iter(), self.buffer.get_end_iter() )
86 class Dropdown(gtk.ComboBox):
87 def __init__(self):
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()
97 self.store.clear()
98 n=0
99 j=0
100 for line in array:
101 self.store.append([ line ])
102 if line == active_text:
103 j = n
104 n += 1
105 self.set_active(j)
109 class PgStats(SidebarPlugin):
110 name = "PgStats"
111 def __init__(self):
112 SidebarPlugin.__init__(self)
113 self.vpaned = gtk.VPaned()
115 self.vcontainer = gtk.VBox()
117 #Filter
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)
134 #Ergebnisse
135 self.listbox = RowedListBox()
136 self.vcontainer.add(self.listbox)
138 self.add(self.vcontainer)
140 def on_init(self):
141 pass
143 def on_connection(self):
144 self.refresh()
146 def refresh(self,widget=None):
147 self.refresh_table() #=> Calls refresh_data via event from table-column ding
149 def refresh_table(self):
150 tables = [_("All")]
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"):
158 filter_table = '%'
159 else:
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)
166 print sql
168 for row in self.query(sql):
169 data.append(row)
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:
174 return
175 q = self.app.db.query(sql)
176 if not q.has_key("error") and q.has_key("cursor"):
177 return q['cursor'].fetchall()
178 else:
179 print q['error']
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!"
186 return
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:
193 return
194 self.button.set_relief( gtk.RELIEF_NONE )
196 #dropdown
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:
209 if len(result) == 1:
210 self.liststore.append([result[0]])
211 if result[0] == active: #take-over old selection
212 j = n
213 n+=1
214 else:
215 print "FunctionBrowser: Error, unexpected result" + str(result)
216 self.combobox.set_active(j)
217 #list
219 filter = self.combobox.get_active_text()
220 if filter == _("All") or filter == None:
221 filter = '%';
222 """# sql = """SELECT proname
223 # FROM pg_catalog.pg_language AS L, pg_catalog.pg_proc AS P
224 # WHERE
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()
231 store = []
232 for line in results:
233 store.append([line[0]])
235 self.listbox.set_array(store)
237 self.button.set_relief( gtk.RELIEF_NORMAL )
238 return
241 #EXPORTS=[PgStats]