forked from levinsv/pgadmin3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlgRepCluster.cpp
1628 lines (1375 loc) · 51.2 KB
/
dlgRepCluster.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2016, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// dlgRepCluster.cpp - PostgreSQL Slony-I Cluster Property
//
//////////////////////////////////////////////////////////////////////////
// wxWindows headers
#include <wx/wx.h>
// App headers
#include "pgAdmin3.h"
#include "utils/pgDefs.h"
#include <wx/textbuf.h>
#include <wx/file.h>
#include "frm/frmMain.h"
#include "slony/dlgRepCluster.h"
#include "slony/slCluster.h"
#include "slony/slSet.h"
#include "slony/slCluster.h"
#include "schema/pgDatatype.h"
#include "utils/sysProcess.h"
#define cbServer CTRL_COMBOBOX("cbServer")
#define cbDatabase CTRL_COMBOBOX("cbDatabase")
#define cbClusterName CTRL_COMBOBOX("cbClusterName")
BEGIN_EVENT_TABLE(dlgRepClusterBase, dlgProperty)
EVT_COMBOBOX(XRCID("cbServer"), dlgRepClusterBase::OnChangeServer)
EVT_COMBOBOX(XRCID("cbDatabase"), dlgRepClusterBase::OnChangeDatabase)
END_EVENT_TABLE();
dlgRepClusterBase::dlgRepClusterBase(pgaFactory *f, frmMain *frame, const wxString &dlgName, slCluster *node, pgDatabase *db)
: dlgProperty(f, frame, dlgName)
{
cluster = node;
remoteServer = 0;
remoteConn = 0;
pgObject *obj = db;
servers = obj->GetId();
while (obj && obj != frame->GetServerCollection())
{
servers = frame->GetBrowser()->GetItemParent(servers);
if (servers)
obj = frame->GetBrowser()->GetObject(servers);
}
}
dlgRepClusterBase::~dlgRepClusterBase()
{
if (remoteConn)
{
delete remoteConn;
remoteConn = 0;
}
}
pgObject *dlgRepClusterBase::GetObject()
{
return cluster;
}
bool dlgRepClusterBase::AddScript(wxString &sql, const wxString &fn)
{
wxFileName filename;
filename.Assign(settings->GetSlonyPath(), fn);
if (!wxFile::Exists(filename.GetFullPath()))
return false;
wxFile file(filename.GetFullPath(), wxFile::read);
if (!file.IsOpened())
return false;
char *buffer;
size_t done;
buffer = new char[file.Length() + 1];
done = file.Read(buffer, file.Length());
buffer[done] = 0;
sql += wxTextBuffer::Translate(wxString::FromAscii(buffer), wxTextFileType_Unix);
delete[] buffer;
return done > 0;
}
int dlgRepClusterBase::Go(bool modal)
{
return dlgProperty::Go(modal);
}
void dlgRepClusterBase::OnChangeServer(wxCommandEvent &ev)
{
cbDatabase->Clear();
if (remoteConn)
{
delete remoteConn;
remoteConn = 0;
}
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(remoteServer->GetLastError());
return;
}
}
if (remoteServer->GetConnected())
{
pgSet *set = remoteServer->ExecuteSet(
wxT("SELECT DISTINCT datname\n")
wxT(" FROM pg_database db\n")
wxT(" WHERE datallowconn ORDER BY datname"));
if (set)
{
while (!set->Eof())
{
cbDatabase->Append(set->GetVal(wxT("datname")));
set->MoveNext();
}
delete set;
if (cbDatabase->GetCount())
cbDatabase->SetSelection(0);
}
}
}
OnChangeDatabase(ev);
}
void dlgRepClusterBase::OnChangeDatabase(wxCommandEvent &ev)
{
cbClusterName->Clear();
int sel = cbDatabase->GetCurrentSelection();
if (remoteServer && sel >= 0)
{
if (remoteConn)
{
delete remoteConn;
remoteConn = 0;
}
remoteConn = remoteServer->CreateConn(cbDatabase->GetValue());
if (remoteConn)
{
pgSet *set = remoteConn->ExecuteSet(
wxT("SELECT substr(nspname, 2) as clustername\n")
wxT(" FROM pg_namespace nsp\n")
wxT(" JOIN pg_proc pro ON pronamespace=nsp.oid AND proname = 'slonyversion'\n")
wxT(" ORDER BY nspname"));
if (set)
{
while (!set->Eof())
{
cbClusterName->Append(set->GetVal(wxT("clustername")));
set->MoveNext();
}
delete set;
}
if (cbClusterName->GetCount())
cbClusterName->SetSelection(0);
}
}
OnChangeCluster(ev);
}
////////////////////////////////////////////////////////////////////////////////7
// pointer to controls
#define chkJoinCluster CTRL_CHECKBOX("chkJoinCluster")
#define txtClusterName CTRL_TEXT("txtClusterName")
#define txtNodeID CTRL_TEXT("txtNodeID")
#define txtNodeName CTRL_TEXT("txtNodeName")
#define txtAdminNodeID CTRL_TEXT("txtAdminNodeID")
#define txtAdminNodeName CTRL_TEXT("txtAdminNodeName")
#define cbAdminNode CTRL_COMBOBOX("cbAdminNode")
BEGIN_EVENT_TABLE(dlgRepCluster, dlgRepClusterBase)
EVT_BUTTON(wxID_OK, dlgRepCluster::OnOK)
EVT_CHECKBOX(XRCID("chkJoinCluster"), dlgRepCluster::OnChangeJoin)
EVT_COMBOBOX(XRCID("cbClusterName"), dlgRepCluster::OnChangeCluster)
EVT_TEXT(XRCID("txtClusterName"), dlgRepCluster::OnChange)
EVT_TEXT(XRCID("txtNodeID"), dlgRepCluster::OnChange)
EVT_TEXT(XRCID("txtNodeName"), dlgRepCluster::OnChange)
EVT_COMBOBOX(XRCID("cbAdminNode"), dlgRepCluster::OnChange)
EVT_END_PROCESS(-1, dlgRepCluster::OnEndProcess)
END_EVENT_TABLE();
dlgProperty *pgaSlClusterFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
return new dlgRepCluster(this, frame, (slCluster *)node, (pgDatabase *)parent);
}
dlgRepCluster::dlgRepCluster(pgaFactory *f, frmMain *frame, slCluster *node, pgDatabase *db)
: dlgRepClusterBase(f, frame, wxT("dlgRepCluster"), node, db)
{
process = 0;
}
wxString dlgRepCluster::GetHelpPage() const
{
wxString page = wxT("slony-install");
if (chkJoinCluster->GetValue())
page += wxT("#join");
return page;
}
bool dlgRepCluster::SlonyMaximumVersion(const wxString &series, long minor)
{
wxString slonySeries;
long slonyMinorVersion;
slonySeries = slonyVersion.BeforeLast('.');
slonyVersion.AfterLast('.').ToLong(&slonyMinorVersion);
return slonySeries == series && slonyMinorVersion <= minor;
}
int dlgRepCluster::Go(bool modal)
{
chkJoinCluster->SetValue(false);
if (cluster)
{
// edit mode
txtClusterName->SetValue(cluster->GetName());
txtNodeID->SetValue(NumToStr(cluster->GetLocalNodeID()));
txtClusterName->Disable();
txtNodeID->Disable();
txtNodeName->SetValue(cluster->GetLocalNodeName());
txtNodeName->Disable();
chkJoinCluster->Disable();
txtAdminNodeID->Hide();
txtAdminNodeName->Hide();
wxString sql =
wxT("SELECT no_id, no_comment\n")
wxT(" FROM ") + cluster->GetSchemaPrefix() + wxT("sl_node\n")
wxT(" JOIN ") + cluster->GetSchemaPrefix() + wxT("sl_path ON no_id = pa_client\n")
wxT(" WHERE pa_server = ") + NumToStr(cluster->GetLocalNodeID()) +
wxT(" AND pa_conninfo LIKE ") + qtDbString(wxT("%host=") + cluster->GetServer()->GetName() + wxT("%")) +
wxT(" AND pa_conninfo LIKE ") + qtDbString(wxT("%dbname=") + cluster->GetDatabase()->GetName() + wxT("%"));
if (cluster->GetServer()->GetPort() != 5432)
sql += wxT(" AND pa_conninfo LIKE ") + qtDbString(wxT("%port=") + NumToStr((long)cluster->GetServer()->GetPort()) + wxT("%"));
sql += wxT(" ORDER BY no_id");
pgSet *set = connection->ExecuteSet(sql);
if (set)
{
while (!set->Eof())
{
long id = set->GetLong(wxT("no_id"));
cbAdminNode->Append(IdAndName(id, set->GetVal(wxT("no_comment"))), (void *)id);
if (id == cluster->GetAdminNodeID())
cbAdminNode->SetSelection(cbAdminNode->GetCount() - 1);
set->MoveNext();
}
delete set;
}
if (!cbAdminNode->GetCount())
{
cbAdminNode->Append(_("<none>"), (void *) - 1);
cbAdminNode->SetSelection(0);
}
cbServer->Append(cluster->GetServer()->GetName());
cbServer->SetSelection(0);
cbDatabase->Append(cluster->GetDatabase()->GetName());
cbDatabase->SetSelection(0);
cbClusterName->Append(cluster->GetName());
cbClusterName->SetSelection(0);
}
else
{
// create mode
cbAdminNode->Hide();
wxString scriptVersion = wxEmptyString;
wxString xxidVersion = wxEmptyString;
txtNodeID->SetValidator(numericValidator);
txtAdminNodeID->SetValidator(numericValidator);
txtClusterName->Hide();
//We need to find the exact Slony Version.
//NOTE: We are not supporting Slony versions less than 1.2.0
wxString tempScript = wxEmptyString;
bool isSlonyVersionBefore2_2_0 = false;
if(!AddScript(tempScript, wxT("slony1_funcs.sql")))
{
if(!AddScript(tempScript, wxT("slony1_funcs.2.2.0.sql")))
{
isSlonyVersionBefore2_2_0 = true;
}
else
{
isSlonyVersionBefore2_2_0 = false;
}
}
else
{
isSlonyVersionBefore2_2_0 = true;
}
if (tempScript.Contains(wxT("@MODULEVERSION@")) && slonyVersion.IsEmpty())
{
bool hasVerFunc = false;
this->database->ExecuteVoid(wxT("RESET SEARCH_PATH;"));
if (isSlonyVersionBefore2_2_0)
hasVerFunc = this->database->ExecuteVoid(wxT("CREATE OR REPLACE FUNCTION pgadmin_slony_version() returns text as '$libdir/slony1_funcs', '_Slony_I_getModuleVersion' LANGUAGE C"), false);
else
hasVerFunc = this->database->ExecuteVoid(wxT("CREATE OR REPLACE FUNCTION pgadmin_slony_version() returns text as '$libdir/slony1_funcs.2.2.0', '_Slony_I_2_2_0_getModuleVersion' LANGUAGE C"), false);
if (hasVerFunc)
{
slonyVersion = this->database->ExecuteScalar(wxT("SELECT pgadmin_slony_version();"));
this->database->ExecuteVoid(wxT("DROP FUNCTION pgadmin_slony_version()"), false);
}
else
{
tempScript.Empty();
}
if (slonyVersion.IsEmpty())
{
wxLogError(_("Couldn't test for the Slony version. Assuming 1.2.0"));
slonyVersion = wxT("1.2.0");
}
}
//Here we are finding the exact slony scripts version, which is based on Slony Version and PG Version.
// For Slony 1.2.0 to 1.2.21 and 2.0.0 if PG 7.3 script version is v73
// For Slony 1.2.0 to 1.2.21 and 2.0.0 if PG 7.4 script version is v74
// For Slony 1.2.0 to 1.2.6 if PG 8.0+ script version is v80
// For Slony 1.2.7 to 1.2.21 and 2.0.0 if PG 8.0 script version is v80
// For Slony 1.2.7 to 1.2.21 and 2.0.0 if PG 8.1+ script version is v81
// For Slony 2.0.1 and 2.0.2 if PG 8.3+ script version is v83. (These version onwards do not support PG Version less than 8.3)
// For Slony 2.0.3 if PG 8.3 script version is v83.
// For Slony 2.0.3 if PG 8.4+ script version is v84.
// For Slony 2.1.0 to 2.2.0 if PG 8.3 script version v83
// For Slony 2.1.0 to 2.2.0 if PG 8.4+ script version v84
// Since both 1.2 and 2.0 series is increasing, the following code needs to be updated with each Slony or PG update.
// For Slony 1.2.22 onwards if PG 7.4 script version v74
// For Slony 1.2.22 onwards if PG 8.0 script version v80
// For Slony 1.2.22 onwards if PG 8.3 script version v81
// For Slony 1.2.22 onwards if PG 8.4+ script version v84
if (!tempScript.IsEmpty())
{
wxString slonySeries;
long slonyMinorVersion;
slonySeries = slonyVersion.BeforeLast('.');
slonyVersion.AfterLast('.').ToLong(&slonyMinorVersion);
//Set the slony_base and slony_funcs script version.
if (SlonyMaximumVersion(wxT("1.2"), 6))
{
if (connection->BackendMinimumVersion(8, 0))
scriptVersion = wxT("v80");
else
{
if (connection->BackendMinimumVersion(7, 4))
scriptVersion = wxT("v74");
else
scriptVersion = wxT("v73");
}
}
else
{
// For slony verion 1.2.22 and above set the script version
if (slonySeries == wxT("1.2") && slonyMinorVersion >= 22)
{
if (connection->BackendMinimumVersion(8, 4))
scriptVersion = wxT("v84");
else
{
if (connection->BackendMinimumVersion(8, 1))
scriptVersion = wxT("v81");
else
{
if (connection->BackendMinimumVersion(8, 0))
scriptVersion = wxT("v80");
else
scriptVersion = wxT("v74");
}
}
}
// For slony major version 1.2, minor version <= 21 and slony version 2.0, set the script version
if (SlonyMaximumVersion(wxT("1.2"), 21) || SlonyMaximumVersion(wxT("2.0"), 0))
{
if (connection->BackendMinimumVersion(8, 1))
scriptVersion = wxT("v81");
else
{
if (connection->BackendMinimumVersion(8, 0))
scriptVersion = wxT("v80");
else
{
if (connection->BackendMinimumVersion(7, 4))
scriptVersion = wxT("v74");
else
scriptVersion = wxT("v73");
}
}
}
else
{
if (SlonyMaximumVersion(wxT("2.0"), 2))
scriptVersion = wxT("v83");
else
{
if (SlonyMaximumVersion(wxT("2.0"), 8))
{
if (connection->BackendMinimumVersion(8, 4))
scriptVersion = wxT("v84");
}
if (SlonyMaximumVersion(wxT("2.1"), 4) || SlonyMaximumVersion(wxT("2.2"), 0))
{
if (connection->BackendMinimumVersion(8, 4))
scriptVersion = wxT("v84");
}
else
{
if (scriptVersion.IsEmpty())
scriptVersion = wxT("v83");
}
}
}
}
//Set the correct xxid version if applicable
// For Slony 1.2.0 to 1.2.17 and 2.0.0 if PG 7.3 xxid version is v73
// For Slony 1.2.1 to 1.2.17 and 2.0.0 if PG 7.4+ xxid version is v74
// For Slony 1.2.0 if PG 8.0 xxid version is v80
// For Slony 2.0.1+ and PG8.4+ xxid is obsolete.
if (SlonyMaximumVersion(wxT("1.2"), 0))
{
if (connection->BackendMinimumVersion(8, 0))
xxidVersion = wxT("v80");
else
{
if (connection->BackendMinimumVersion(7, 4))
xxidVersion = wxT("v74");
else
xxidVersion = wxT("v73");
}
}
else
{
// For Slony 1.2.22 and above if PG 7.4 xxid version is v74
// For Slony 1.2.22 and above if PG 8.0 xxid version is v80
// For Slony 1.2.22 and above if PG 8.1 xxid version is v81
// For Slony 1.2.22 and above if PG 8.4+ xxid version is v84
if (slonySeries == wxT("1.2") && slonyMinorVersion >= 22)
{
if (connection->BackendMinimumVersion(8, 4))
xxidVersion = wxT("v84");
else
{
if (connection->BackendMinimumVersion(8, 1))
xxidVersion = wxT("v81");
else
{
if (connection->BackendMinimumVersion(8, 0))
xxidVersion = wxT("v80");
else
xxidVersion = wxT("v74");
}
}
}
if (SlonyMaximumVersion(wxT("1.2"), 21) || SlonyMaximumVersion(wxT("2.0"), 0))
{
if (!connection->BackendMinimumVersion(8, 4))
{
if (connection->BackendMinimumVersion(7, 4))
xxidVersion = wxT("v74");
else
xxidVersion = wxT("v73");
}
}
}
wxString slonyBaseVersionFilename;
wxString slonyFuncsVersionFilename;
if (SlonyMaximumVersion(wxT("2.2"), 0))
{
slonyBaseVersionFilename = wxT("slony1_base.") + scriptVersion + wxT(".2.2.0.sql");
slonyFuncsVersionFilename = wxT("slony1_funcs.") + scriptVersion + wxT(".2.2.0.sql");
}
else
{
slonyBaseVersionFilename = wxT("slony1_base.") + scriptVersion + wxT(".sql");
slonyFuncsVersionFilename = wxT("slony1_funcs.") + scriptVersion + wxT(".sql");
}
wxString xxidVersionFilename;
if (!xxidVersion.IsEmpty())
xxidVersionFilename = wxT("xxid.") + xxidVersion + wxT(".sql");
if (SlonyMaximumVersion(wxT("2.2"), 0))
{
if (((!xxidVersion.IsEmpty() && !AddScript(createScript, xxidVersionFilename)) ||
!AddScript(createScript, wxT("slony1_base.2.2.0.sql")) ||
!AddScript(createScript, slonyBaseVersionFilename) ||
!AddScript(createScript, wxT("slony1_funcs.2.2.0.sql")) ||
!AddScript(createScript, slonyFuncsVersionFilename)))
createScript = wxEmptyString;
}
else
{
if (((!xxidVersion.IsEmpty() && !AddScript(createScript, xxidVersionFilename)) ||
!AddScript(createScript, wxT("slony1_base.sql")) ||
!AddScript(createScript, slonyBaseVersionFilename) ||
!AddScript(createScript, wxT("slony1_funcs.sql")) ||
!AddScript(createScript, slonyFuncsVersionFilename)))
createScript = wxEmptyString;
}
}
// Populate the server combo box
ctlTree *browser = mainForm->GetBrowser();
wxTreeItemIdValue foldercookie, servercookie;
wxTreeItemId folderitem, serveritem;
pgObject *object;
pgServer *server;
int sel = -1;
folderitem = browser->GetFirstChild(browser->GetRootItem(), foldercookie);
while (folderitem)
{
if (browser->ItemHasChildren(folderitem))
{
serveritem = browser->GetFirstChild(folderitem, servercookie);
while (serveritem)
{
object = browser->GetObject(serveritem);
if (object && object->IsCreatedBy(serverFactory))
{
server = (pgServer *)object;
if (server == database->GetServer())
sel = cbServer->GetCount();
cbServer->Append(browser->GetItemText(server->GetId()), (void *)server);
}
serveritem = browser->GetNextChild(folderitem, servercookie);
}
}
folderitem = browser->GetNextChild(browser->GetRootItem(), foldercookie);
}
if (sel >= 0)
cbServer->SetSelection(sel);
}
wxCommandEvent ev;
OnChangeJoin(ev);
return dlgRepClusterBase::Go(modal);
}
void dlgRepCluster::OnChangeJoin(wxCommandEvent &ev)
{
bool joinCluster = chkJoinCluster->GetValue();
txtClusterName->Show(!joinCluster);
cbClusterName->Show(joinCluster);
cbServer->Enable(joinCluster);
cbDatabase->Enable(joinCluster);
txtAdminNodeID->Show(!joinCluster && !cluster);
txtAdminNodeName->Show(!joinCluster && !cluster);
cbAdminNode->Show(joinCluster || cluster);
cbAdminNode->Move(txtAdminNodeID->GetPosition());
// Force the dialogue to resize to prevent a drawing issue on GTK
#ifdef __WXGTK__
SetSize(GetSize().x + 1, GetSize().y + 1);
Layout();
SetSize(GetSize().x - 1, GetSize().y - 1);
#endif
if (joinCluster && !cbDatabase->GetCount())
{
OnChangeServer(ev);
return;
}
OnChange(ev);
}
void dlgRepCluster::OnChangeCluster(wxCommandEvent &ev)
{
clusterBackup = wxEmptyString;
remoteVersion = wxEmptyString;
cbAdminNode->Clear();
cbAdminNode->Append(_("<none>"), (void *) - 1);
int sel = cbClusterName->GetCurrentSelection();
if (remoteConn && sel >= 0)
{
wxString schemaPrefix = qtIdent(wxT("_") + cbClusterName->GetValue()) + wxT(".");
long adminNodeID = settings->Read(wxT("Replication/") + cbClusterName->GetValue() + wxT("/AdminNode"), -1L);
remoteVersion = remoteConn->ExecuteScalar(wxT("SELECT ") + schemaPrefix + wxT("slonyVersion();"));
wxString sql =
wxT("SELECT no_id, no_comment\n")
wxT(" FROM ") + schemaPrefix + wxT("sl_node\n")
wxT(" JOIN ") + schemaPrefix + wxT("sl_path ON no_id = pa_client\n")
wxT(" WHERE pa_server = (SELECT last_value FROM ") + schemaPrefix + wxT("sl_local_node_id)\n")
wxT(" AND pa_conninfo ILIKE ") + qtDbString(wxT("%host=") + remoteServer->GetName() + wxT("%")) + wxT("\n")
wxT(" AND pa_conninfo LIKE ") + qtDbString(wxT("%dbname=") + cbDatabase->GetValue() + wxT("%")) + wxT("\n");
if (remoteServer->GetPort() != 5432)
sql += wxT(" AND pa_conninfo LIKE ") + qtDbString(wxT("%port=") + NumToStr((long)remoteServer->GetPort()) + wxT("%"));
pgSet *set = remoteConn->ExecuteSet(sql);
if (set)
{
if (!set->Eof())
{
long id = set->GetLong(wxT("no_id"));
cbAdminNode->Append(IdAndName(id, set->GetVal(wxT("no_comment"))), (void *)id);
if (adminNodeID == id)
cbAdminNode->SetSelection(cbAdminNode->GetCount() - 1);
}
}
usedNodes.Clear();
set = remoteConn->ExecuteSet(
wxT("SELECT no_id FROM ") + schemaPrefix + wxT("sl_node"));
if (set)
{
while (!set->Eof())
{
usedNodes.Add(set->GetLong(wxT("no_id")));
set->MoveNext();
}
delete set;
}
}
OnChange(ev);
}
bool dlgRepCluster::CopyTable(pgConn *from, pgConn *to, const wxString &table)
{
bool ok = true;
pgSet *set = from->ExecuteSet(wxT("SELECT * FROM ") + table);
if (!set)
return false;
while (ok && !set->Eof())
{
wxString sql = wxT("INSERT INTO ") + table + wxT("(");
wxString vals;
int i;
for (i = 0 ; i < set->NumCols() ; i++)
{
if (i)
{
sql += wxT(", ");;
vals += wxT(", ");
}
sql += set->ColName(i);
if (set->IsNull(i))
vals += wxT("NULL");
else
{
switch (set->ColTypeOid(i))
{
case PGOID_TYPE_BOOL:
case PGOID_TYPE_BYTEA:
case PGOID_TYPE_CHAR:
case PGOID_TYPE_NAME:
case PGOID_TYPE_TEXT:
case PGOID_TYPE_VARCHAR:
case PGOID_TYPE_TIME:
case PGOID_TYPE_TIMESTAMP:
case PGOID_TYPE_TIME_ARRAY:
case PGOID_TYPE_TIMESTAMPTZ:
case PGOID_TYPE_INTERVAL:
case PGOID_TYPE_TIMETZ:
vals += qtDbString(set->GetVal(i));
break;
default:
vals += set->GetVal(i);
}
}
}
ok = to->ExecuteVoid(
sql + wxT(")\n VALUES (") + vals + wxT(");"));
set->MoveNext();
}
delete set;
return ok;
}
void dlgRepCluster::OnOK(wxCommandEvent &ev)
{
#ifdef __WXGTK__
if (!btnOK->IsEnabled())
return;
#endif
EnableOK(false);
bool done = true;
done = connection->ExecuteVoid(wxT("BEGIN TRANSACTION;"));
if (remoteConn)
done = remoteConn->ExecuteVoid(wxT("BEGIN TRANSACTION;"));
// initialize cluster on local node
done = connection->ExecuteVoid(GetSql());
if (done && chkJoinCluster->GetValue())
{
// we're joining an existing cluster
wxString schemaPrefix = qtIdent(wxT("_") + cbClusterName->GetValue()) + wxT(".");
wxString clusterVersion = remoteConn->ExecuteScalar(
wxT("SELECT ") + schemaPrefix + wxT("slonyversion()"));
wxString newVersion = connection->ExecuteScalar(
wxT("SELECT ") + schemaPrefix + wxT("slonyversion()"));
if (clusterVersion != newVersion)
{
wxMessageDialog msg(this,
wxString::Format(_("The newly created cluster version (%s)\n doesn't match the existing cluster's version (%s)"),
newVersion.c_str(), clusterVersion.c_str()),
_("Error while joining replication cluster"), wxICON_ERROR);
msg.ShowModal();
done = false;
}
if (done)
done = CopyTable(remoteConn, connection, schemaPrefix + wxT("sl_node"));
if (done)
done = CopyTable(remoteConn, connection, schemaPrefix + wxT("sl_path"));
if (done)
done = CopyTable(remoteConn, connection, schemaPrefix + wxT("sl_listen"));
if (done)
done = CopyTable(remoteConn, connection, schemaPrefix + wxT("sl_set"));
if (done)
done = CopyTable(remoteConn, connection, schemaPrefix + wxT("sl_subscribe"));
// make sure event seqno starts correctly after node reusage
if (done)
{
pgSet *set = connection->ExecuteSet(
wxT("SELECT ev_origin, MAX(ev_seqno) as seqno\n")
wxT(" FROM ") + schemaPrefix + wxT("sl_event\n")
wxT(" GROUP BY ev_origin"));
if (set)
{
while (done && !set->Eof())
{
if (set->GetVal(wxT("ev_origin")) == txtNodeID->GetValue())
{
done = connection->ExecuteVoid(
wxT("SELECT pg_catalog.setval(") +
qtDbString(wxT("_") + cbClusterName->GetValue() + wxT(".sl_event_seq")) +
wxT(", ") + set->GetVal(wxT("seqno")) + wxT("::int8 +1)"));
}
else
{
done = connection->ExecuteVoid(
wxT("INSERT INTO ") + schemaPrefix + wxT("sl_confirm(con_origin, con_received, con_seqno, con_timestamp\n")
wxT(" VALUES (") + set->GetVal(wxT("ev_origin")) +
wxT(", ") + txtNodeID->GetValue() +
wxT(", ") + set->GetVal(wxT("seqno")) +
wxT(", current_timestamp"));
}
set->MoveNext();
}
delete set;
}
}
// make sure rowid seq starts correctly
if (done)
{
wxString seqno = connection->ExecuteScalar(
wxT("SELECT MAX(seql_last_value)\n")
wxT(" FROM ") + schemaPrefix + wxT("sl_seqlog\n")
wxT(" WHERE seql_seqid = 0 AND seql_origin = ") + txtNodeID->GetValue());
if (!seqno.IsEmpty())
{
done = connection->ExecuteVoid(
wxT("SELECT pg_catalog.setval(") +
qtDbString(wxT("_") + cbClusterName->GetValue() + wxT(".sl_rowid_seq")) +
wxT(", ") + seqno + wxT(")"));
}
}
// create new node on the existing cluster
if (done)
{
wxString sql =
wxT("SELECT ") + schemaPrefix + wxT("storenode(")
+ txtNodeID->GetValue() + wxT(", ")
+ qtDbString(txtNodeName->GetValue());
// When user has not selected cluster drop down in that case "schemaPrefix" will be NULL,
// we have to use slonyVersion instead of remoteVersion
if (remoteVersion.IsEmpty())
{
if (StrToDouble(slonyVersion) >= 1.1 && StrToDouble(slonyVersion) < 2.0)
sql += wxT(", false");
}
else
{
if (StrToDouble(remoteVersion) >= 1.1 && StrToDouble(remoteVersion) < 2.0)
sql += wxT(", false");
}
sql += wxT(");\n")
wxT("SELECT ") + schemaPrefix + wxT("enablenode(")
+ txtNodeID->GetValue() + wxT(");\n");
done = remoteConn->ExecuteVoid(sql);
}
// add admin info to cluster
if (done && cbAdminNode->GetCurrentSelection() > 0)
{
done = remoteConn->ExecuteVoid(
wxT("SELECT ") + schemaPrefix + wxT("storepath(") +
txtNodeID->GetValue() + wxT(", ") +
NumToStr((long)cbAdminNode->wxItemContainer::GetClientData(cbAdminNode->GetCurrentSelection())) + wxT(", ") +
qtDbString(wxT("host=") + database->GetServer()->GetName() +
wxT(" port=") + NumToStr((long)database->GetServer()->GetPort()) +
wxT(" dbname=") + database->GetName()) + wxT(", ")
wxT("0);\n"));
}
}
if (!done)
{
if (remoteConn)
done = remoteConn->ExecuteVoid(wxT("ROLLBACK TRANSACTION;"));
done = connection->ExecuteVoid(wxT("ROLLBACK TRANSACTION;"));
EnableOK(true);
return;
}
if (remoteConn)
done = remoteConn->ExecuteVoid(wxT("COMMIT TRANSACTION;"));
done = connection->ExecuteVoid(wxT("COMMIT TRANSACTION;"));
ShowObject();
Destroy();
}
pgObject *dlgRepCluster::CreateObject(pgCollection *collection)
{
pgObject *obj = slClusterFactory.CreateObjects(collection, 0,
wxT(" WHERE nspname = ") + qtDbString(wxT("_") + GetName()));
return obj;
}
void dlgRepCluster::CheckChange()
{
if (cluster)
{
int sel = cbAdminNode->GetCurrentSelection();
bool changed = (sel >= 0 && (long)cbAdminNode->wxEvtHandler::GetClientData() != cluster->GetAdminNodeID());
EnableOK(changed || txtComment->GetValue() != cluster->GetComment());
}
else
{
size_t i;
bool enable = true;
CheckValid(enable, chkJoinCluster->GetValue() || (!createScript.IsEmpty()),
_("Slony-I creation scripts not available; only joining possible."));
if (chkJoinCluster->GetValue())
CheckValid(enable, !cbClusterName->GetValue().IsEmpty(), _("Please select a cluster name."));
else
CheckValid(enable, !txtClusterName->GetValue().IsEmpty(), _("Please specify name."));
long nodeId = StrToLong(txtNodeID->GetValue());
CheckValid(enable, nodeId > 0, _("Please specify local node ID."));
for (i = 0 ; i < usedNodes.GetCount() && enable; i++)
CheckValid(enable, nodeId != usedNodes[i], _("Node ID is already in use."));
CheckValid(enable, !txtNodeName->GetValue().IsEmpty(), _("Please specify local node name."));
txtAdminNodeName->Enable(nodeId != StrToLong(txtAdminNodeID->GetValue()));
EnableOK(enable);
}
}
void dlgRepCluster::OnEndProcess(wxProcessEvent &ev)
{
if (process)
{
wxString error = process->ReadErrorStream();
clusterBackup += process->ReadInputStream();
delete process;
process = 0;
}
}
// this is necessary because wxString::Replace is ridiculously slow on large strings.
void AppendBuf(wxChar *&buf, int &buflen, int &len, const wxChar *str, int slen = -1)
{
if (slen < 0)
slen = wxStrlen(str);
if (!slen)
return;
if (buflen < len + slen)
{
buflen = (len + slen) * 6 / 5;
wxChar *tmp = new wxChar[buflen + 1];
memcpy(tmp, buf, len * sizeof(wxChar));
delete[] buf;
buf = tmp;
}
memcpy(buf + len, str, slen * sizeof(wxChar));
len += slen;
}
wxString ReplaceString(const wxString &str, const wxString &oldStr, const wxString &newStr)
{
int buflen = str.Length() + 100;
int len = 0;