01: package net.sourceforge.squirrel_sql.plugins.postgres.tab;
02:
03: import java.sql.ResultSet;
04: import java.sql.SQLException;
05: import java.sql.Statement;
06:
07: import net.sourceforge.squirrel_sql.client.session.ISession;
08: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.BaseDataSetTab;
09: import net.sourceforge.squirrel_sql.fw.datasetviewer.DataSetException;
10: import net.sourceforge.squirrel_sql.fw.datasetviewer.IDataSet;
11: import net.sourceforge.squirrel_sql.fw.datasetviewer.ResultSetDataSet;
12: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
13: import net.sourceforge.squirrel_sql.fw.util.StringManager;
14: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
15:
16: /**
17: * Detail tab providing info about database wide locks.
18: *
19: * This was adapted from Rocco Rutte's PostgreSQL LockTab in EclipseSQL project.
20: */
21: public class LockTab extends BaseDataSetTab {
22:
23: private static final StringManager s_stringMgr = StringManagerFactory
24: .getStringManager(LockTab.class);
25:
26: static interface i18n {
27: //i18n[LockDetailsTab.hint=Display Locks]
28: String HINT = s_stringMgr.getString("LockDetailsTab.hint");
29: //i18n[LockDetailsTab.title=Locks]
30: String TITLE = s_stringMgr.getString("LockDetailsTab.title");
31:
32: }
33:
34: private static final String QUERY = "SELECT "
35: + " pgl.relation::regclass AS \"Class\", "
36: + " pg_get_userbyid(pg_stat_get_backend_userid(svrid)) AS \"User\", "
37: + " pgl.transaction AS \"Transaction\", "
38: + " pg_stat_get_backend_pid(svrid) AS \"Pid\", "
39: + " pgl.mode AS \"Mode\", "
40: + " pgl.granted AS \"Granted\", "
41: + " translate(pg_stat_get_backend_activity(svrid),E'\n',' ') AS \"Query\", "
42: + " pg_stat_get_backend_activity_start(svrid) AS \"Running since\" "
43: + "FROM "
44: + " pg_stat_get_backend_idset() svrid, pg_locks pgl, pg_database db "
45: + "WHERE "
46: + " datname = current_database() AND pgl.pid = pg_stat_get_backend_pid(svrid) AND db.oid = pgl.database "
47: + "ORDER BY " + " user,pid";;
48:
49: /**
50: * @see net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.BaseDataSetTab#createDataSet()
51: */
52: @Override
53: protected IDataSet createDataSet() throws DataSetException {
54: final ISession session = getSession();
55: try {
56: ISQLConnection con = session.getSQLConnection();
57: Statement stmt = con.createStatement();
58: ResultSet rs = stmt.executeQuery(QUERY);
59: ResultSetDataSet rsds = new ResultSetDataSet();
60: rsds.setResultSet(rs);
61: return rsds;
62: } catch (SQLException ex) {
63: throw new DataSetException(ex);
64: }
65: }
66:
67: /**
68: * @see net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.IObjectTab#getHint()
69: */
70: public String getHint() {
71: return i18n.HINT;
72: }
73:
74: /**
75: * @see net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.IObjectTab#getTitle()
76: */
77: public String getTitle() {
78: return i18n.TITLE;
79: }
80:
81: }
|