Hi all,

please find attached a small patch for pgadmin based on a personal need. On
our server we have approximately 400-500 databases that is a problem when
trying to select one from the drop down list of the "Connect to Server"
dialog when using the query tool. The list is huge and you have to scroll
down a lot. Also you may not know the actual database name but you want to
search based on a part of the name. As a solution, i have added a textbox
in the dialog which filters the available databases based on the user
input.

Please note that my C/C++ skills are very poor and it is the first time i
work with wxWidgets so feel free to rewrite the code or even drop the patch
in case you think this is not a general need.

Thank you,
Dimitris
commit 62d029d7a7a6432853a9739059b1c3b51adba4ec
Author: Dimitris Koukis <dimcook...@gmail.com>
Date:   Wed Jan 21 15:46:57 2015 +0200

    Add filtering of available databases in "Connect to Server" dialog

diff --git a/pgadmin/dlg/dlgSelectConnection.cpp b/pgadmin/dlg/dlgSelectConnection.cpp
index 79b27ea..9512495 100644
--- a/pgadmin/dlg/dlgSelectConnection.cpp
+++ b/pgadmin/dlg/dlgSelectConnection.cpp
@@ -27,10 +27,14 @@
 #define CTRLID_CBDATABASE 4243
 #define CTRLID_CBUSERNAME 4244
 #define CTRLID_CBROLENAME 4245
+#define CTRLID_CBDBFILTER 4246
+
+#define DBFILTER_MAX_LENGTH 100
 
 BEGIN_EVENT_TABLE(dlgSelectConnection, DialogWithHelp)
 	EVT_COMBOBOX(CTRLID_CBSERVER,      dlgSelectConnection::OnChangeServer)
 	EVT_COMBOBOX(CTRLID_CBDATABASE,    dlgSelectConnection::OnChangeDatabase)
+	EVT_TEXT(CTRLID_CBDBFILTER,        dlgSelectConnection::OnChangeDbFilter)
 	EVT_TEXT(CTRLID_CBSERVER,          dlgSelectConnection::OnTextChange)
 	EVT_TEXT(CTRLID_CBDATABASE,        dlgSelectConnection::OnTextChange)
 	EVT_TEXT(CTRLID_CBUSERNAME,        dlgSelectConnection::OnTextChange)
@@ -57,9 +61,10 @@ dlgSelectConnection::dlgSelectConnection(wxWindow *parent, frmMain *form) :
 		style |= wxCB_READONLY;
 
 	cbServer = new ctlComboBoxFix(this, CTRLID_CBSERVER, ConvertDialogToPixels(wxPoint(65, 5)), ConvertDialogToPixels(wxSize(135, 12)), style);
-	cbDatabase = new wxComboBox(this, CTRLID_CBDATABASE, wxEmptyString, ConvertDialogToPixels(wxPoint(65, 20)), ConvertDialogToPixels(wxSize(135, 12)), wxArrayString(), style);
-	cbUsername = new wxComboBox(this, CTRLID_CBUSERNAME, wxEmptyString, ConvertDialogToPixels(wxPoint(65, 35)), ConvertDialogToPixels(wxSize(135, 12)), wxArrayString(), style);
-	cbRolename = new wxComboBox(this, CTRLID_CBROLENAME, wxEmptyString, ConvertDialogToPixels(wxPoint(65, 50)), ConvertDialogToPixels(wxSize(135, 12)), wxArrayString(), style);
+	cbDbFilter = new wxTextCtrl(this, CTRLID_CBDBFILTER, wxString::FromAscii(""), ConvertDialogToPixels(wxPoint(65, 22)), ConvertDialogToPixels(wxSize(120, 12)), wxTE_DONTWRAP , wxDefaultValidator, wxTextCtrlNameStr);
+	cbDatabase = new wxComboBox(this, CTRLID_CBDATABASE, wxEmptyString, ConvertDialogToPixels(wxPoint(65, 37)), ConvertDialogToPixels(wxSize(135, 12)), wxArrayString(), style);
+	cbUsername = new wxComboBox(this, CTRLID_CBUSERNAME, wxEmptyString, ConvertDialogToPixels(wxPoint(65, 54)), ConvertDialogToPixels(wxSize(135, 12)), wxArrayString(), style);
+	cbRolename = new wxComboBox(this, CTRLID_CBROLENAME, wxEmptyString, ConvertDialogToPixels(wxPoint(65, 71)), ConvertDialogToPixels(wxSize(135, 12)), wxArrayString(), style);
 
 	if (form == NULL)
 	{
@@ -99,6 +104,7 @@ void dlgSelectConnection::OnChangeServer(wxCommandEvent &ev)
 
 	// Clear the comboboxes
 	cbDatabase->Clear();
+	cbDbFilter->Clear();
 	cbUsername->Clear();
 	cbRolename->Clear();
 
@@ -185,6 +191,41 @@ wxString dlgSelectConnection::GetServerName()
 	return cbServer->GetValue();
 }
 
+void dlgSelectConnection::OnChangeDbFilter(wxCommandEvent &ev)
+{
+	if (ev.GetString().length() > DBFILTER_MAX_LENGTH)
+		return;
+
+	cbDatabase->Clear();
+	int sel = cbServer->GetCurrentSelection();
+	if (sel >= 0)
+	{
+		remoteServer = (pgServer *)cbServer->wxItemContainer::GetClientData(sel);
+		if (!remoteServer->GetConnected())
+		{
+			remoteServer->Connect(mainForm, remoteServer->GetStorePwd());
+			if (!remoteServer->GetConnected())
+			{
+				wxLogError(wxT("%s"), remoteServer->GetLastError().c_str());
+				return;
+			}
+		}
+		if (remoteServer->GetConnected())
+		{
+			char buffer[256];
+			sprintf(buffer,  "SELECT DISTINCT datname FROM pg_database db where datname ~ '%s' and datallowconn ORDER BY datname", static_cast<const char*>(ev.GetString().mb_str()));
+			pgSetIterator set1(remoteServer->GetConnection(),wxString::FromAscii(buffer));
+
+			int item = 0;
+			while(set1.RowsLeft())
+				cbDatabase->Append(set1.GetVal(wxT("datname")));
+
+			if (cbDatabase->GetCount())
+				cbDatabase->SetSelection(item);
+		}
+	}
+}
+
 void dlgSelectConnection::OnChangeDatabase(wxCommandEvent &ev)
 {
 	btnOK->Enable(cbServer->GetValue().Length() > 0 && cbDatabase->GetValue().Length() > 0 && cbUsername->GetValue().Length() > 0);
diff --git a/pgadmin/include/dlg/dlgSelectConnection.h b/pgadmin/include/dlg/dlgSelectConnection.h
index 896e91d..c6f83a2 100644
--- a/pgadmin/include/dlg/dlgSelectConnection.h
+++ b/pgadmin/include/dlg/dlgSelectConnection.h
@@ -37,6 +37,7 @@ public:
 private:
 	void OnChangeServer(wxCommandEvent &ev);
 	void OnChangeDatabase(wxCommandEvent &ev);
+	void OnChangeDbFilter(wxCommandEvent &ev);
 	void OnTextChange(wxCommandEvent &ev);
 	void OnOK(wxCommandEvent &ev);
 	void OnCancel(wxCommandEvent &ev);
@@ -45,6 +46,7 @@ private:
 	wxBitmapComboBox *cbConnection;
 	ctlComboBoxFix *cbServer;
 	wxComboBox     *cbDatabase, *cbUsername, *cbRolename;
+	wxTextCtrl  *cbDbFilter;
 
 	DECLARE_EVENT_TABLE()
 };
diff --git a/pgadmin/ui/dlgSelectConnection.xrc b/pgadmin/ui/dlgSelectConnection.xrc
index 0424936..0896566 100644
--- a/pgadmin/ui/dlgSelectConnection.xrc
+++ b/pgadmin/ui/dlgSelectConnection.xrc
@@ -14,27 +14,27 @@
     </object>
     <object class="wxStaticText" name="stUsername">
       <label>Username</label>
-      <pos>5,37d</pos>
+      <pos>5,54d</pos>
     </object>
     <object class="wxStaticText" name="stRolename">
       <label>Rolename</label>
-      <pos>5,54d</pos>
+      <pos>5,71d</pos>
     </object>
     <object class="wxButton" name="wxID_HELP">
       <label>&amp;Help</label>
-      <pos>2,80d</pos>
+      <pos>2,90d</pos>
       <style></style>
     </object>
     <object class="wxButton" name="wxID_OK">
       <label>&amp;OK</label>
       <default>1</default>
-      <pos>97,80d</pos>
+      <pos>97,90d</pos>
       <style></style>
     </object>
     <object class="wxButton" name="wxID_CANCEL">
       <label>&amp;Cancel</label>
       <default>0</default>
-      <pos>150,80d</pos>
+      <pos>150,90d</pos>
     </object>
   </object>
 </resource>
-- 
Sent via pgadmin-hackers mailing list (pgadmin-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers

Reply via email to