1 //////////////////////////////////////////////////////////////////////////
2 //
3 // pgAdmin III - PostgreSQL Tools
4 //
5 // Copyright (C) 2002 - 2016, The pgAdmin Development Team
6 // This software is released under the PostgreSQL Licence
7 //
8 // dlgRepListen.cpp - PostgreSQL Slony-I Listen Property
9 //
10 //////////////////////////////////////////////////////////////////////////
11 
12 // wxWindows headers
13 #include <wx/wx.h>
14 
15 // App headers
16 #include "pgAdmin3.h"
17 
18 #include "slony/dlgRepListen.h"
19 #include "slony/slCluster.h"
20 #include "slony/slNode.h"
21 #include "slony/slListen.h"
22 #include "schema/pgDatatype.h"
23 
24 // pointer to controls
25 
26 #define cbOrigin        CTRL_COMBOBOX("cbOrigin")
27 #define txtReceiver     CTRL_TEXT("txtReceiver")
28 #define cbProvider      CTRL_COMBOBOX("cbProvider")
29 
30 
31 
32 
33 BEGIN_EVENT_TABLE(dlgRepListen, dlgProperty)
34 	EVT_COMBOBOX(XRCID("cbOrigin"),       dlgRepListen::OnChange)
35 	EVT_COMBOBOX(XRCID("cbProvider"),     dlgRepListen::OnChange)
36 END_EVENT_TABLE();
37 
38 
CreateDialog(frmMain * frame,pgObject * node,pgObject * parent)39 dlgProperty *slListenFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
40 {
41 	return new dlgRepListen(this, frame, (slListen *)node, (slNode *)parent);
42 }
43 
dlgRepListen(pgaFactory * f,frmMain * frame,slListen * l,slNode * n)44 dlgRepListen::dlgRepListen(pgaFactory *f, frmMain *frame, slListen *l, slNode *n)
45 	: dlgRepProperty(f, frame, n->GetCluster(), wxT("dlgRepListen"))
46 {
47 	listen = l;
48 	node = n;
49 }
50 
51 
GetObject()52 pgObject *dlgRepListen::GetObject()
53 {
54 	return listen;
55 }
56 
57 
Go(bool modal)58 int dlgRepListen::Go(bool modal)
59 {
60 	txtReceiver->SetValue(IdAndName(node->GetSlId(), node->GetName()));
61 	txtReceiver->Disable();
62 
63 	if (listen)
64 	{
65 		// edit mode
66 		cbOrigin->Append(IdAndName(listen->GetSlId(), listen->GetName()));
67 		cbOrigin->SetSelection(0);
68 		cbProvider->Append(IdAndName(listen->GetProviderId(), listen->GetProviderName()));
69 		cbProvider->SetSelection(0);
70 		cbOrigin->Disable();
71 		cbProvider->Disable();
72 	}
73 	else
74 	{
75 		// create mode
76 
77 		pgSet *nodes = connection->ExecuteSet(
78 		                   wxT("SELECT no_id, no_comment, pa_server\n")
79 		                   wxT("  FROM ") + cluster->GetSchemaPrefix() + wxT("sl_node\n")
80 		                   wxT("  LEFT JOIN ") + cluster->GetSchemaPrefix() + wxT("sl_path ON pa_server=no_id")
81 		                   wxT(" AND pa_client=") + NumToStr(node->GetSlId()) + wxT("\n")
82 		                   wxT(" WHERE no_id <> ") + NumToStr(node->GetSlId()) + wxT("\n")
83 		                   wxT(" ORDER BY no_id")
84 		               );
85 
86 		if (nodes)
87 		{
88 			while (!nodes->Eof())
89 			{
90 				long id = nodes->GetLong(wxT("no_id"));
91 				wxString name = IdAndName(id, nodes->GetVal(wxT("no_comment")));
92 
93 				cbOrigin->Append(name, (void *)id);
94 
95 				if (nodes->GetLong(wxT("pa_server")) > 0)
96 					cbProvider->Append(name, (void *)id);
97 
98 				nodes->MoveNext();
99 			}
100 			delete nodes;
101 		}
102 	}
103 
104 	return dlgProperty::Go(modal);
105 }
106 
107 
CreateObject(pgCollection * collection)108 pgObject *dlgRepListen::CreateObject(pgCollection *collection)
109 {
110 	pgObject *obj = listenFactory.CreateObjects(collection, 0,
111 	                wxT(" WHERE li_origin = ") + NumToStr((OID)cbOrigin->wxItemContainer::GetClientData(cbOrigin->GetCurrentSelection())) +
112 	                wxT("   AND li_receiver = ") + NumToStr(node->GetSlId()) +
113 	                wxT("   AND li_provider = ") + NumToStr((OID)cbProvider->wxItemContainer::GetClientData(cbProvider->GetCurrentSelection()))
114 	                                           );
115 
116 	return obj;
117 }
118 
119 
120 
121 
CheckChange()122 void dlgRepListen::CheckChange()
123 {
124 	if (listen)
125 	{
126 	}
127 	else
128 	{
129 		bool enable = true;
130 		CheckValid(enable, cbProvider->GetCount() > 0, _("No path to any provider node; add proper path."));
131 		CheckValid(enable, cbOrigin->GetCurrentSelection() >= 0, _("Please select origin node of replication events."));
132 		CheckValid(enable, cbProvider->GetCurrentSelection() >= 0, _("Please select provider node for replication events."));
133 
134 		EnableOK(enable);
135 	}
136 }
137 
138 
139 
GetSql()140 wxString dlgRepListen::GetSql()
141 {
142 	wxString sql;
143 
144 	if (listen)
145 	{
146 		// edit mode
147 	}
148 	else
149 	{
150 		// create mode
151 
152 		sql = wxT("SELECT ") + cluster->GetSchemaPrefix() + wxT("storelisten(")
153 		      + NumToStr((OID)cbOrigin->wxItemContainer::GetClientData(cbOrigin->GetCurrentSelection())) + wxT(", ")
154 		      + NumToStr((OID)cbProvider->wxItemContainer::GetClientData(cbProvider->GetCurrentSelection())) + wxT(", ")
155 		      + NumToStr(node->GetSlId()) + wxT(");");
156 	}
157 
158 	return sql;
159 }
160