This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
dlgProperty.cpp
2372 lines (1971 loc) · 56.3 KB
/
dlgProperty.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
//
// dlgQuery.cpp - Property Dialog
//
//////////////////////////////////////////////////////////////////////////
// wxWindows headers
#include <wx/wx.h>
#include <wx/button.h>
// App headers
#include "pgAdmin3.h"
#include "ctl/ctlMenuToolbar.h"
#include "ctl/ctlSQLBox.h"
#include "schema/pgCollection.h"
#include "schema/pgDatatype.h"
#include "utils/misc.h"
#include "utils/pgDefs.h"
#include "ctl/ctlSecurityPanel.h"
#include "ctl/ctlDefaultSecurityPanel.h"
// Images
#include "images/properties.pngc"
#include "frm/frmMain.h"
#include "frm/frmHint.h"
// Property dialogs
#include "dlg/dlgProperty.h"
#include "dlg/dlgServer.h"
#include "dlg/dlgAggregate.h"
#include "dlg/dlgColumn.h"
#include "dlg/dlgIndex.h"
#include "dlg/dlgIndexConstraint.h"
#include "dlg/dlgForeignKey.h"
#include "dlg/dlgCheck.h"
#include "dlg/dlgRule.h"
#include "dlg/dlgTrigger.h"
#include "dlg/dlgEventTrigger.h"
#include "agent/dlgJob.h"
#include "agent/dlgStep.h"
#include "agent/dlgSchedule.h"
#include "slony/dlgRepCluster.h"
#include "slony/dlgRepNode.h"
#include "slony/dlgRepPath.h"
#include "slony/dlgRepListen.h"
#include "slony/dlgRepSet.h"
#include "slony/dlgRepSequence.h"
#include "slony/dlgRepTable.h"
#include "slony/dlgRepSubscription.h"
#include "schema/pgTable.h"
#include "schema/pgColumn.h"
#include "schema/pgTrigger.h"
#include "schema/pgGroup.h"
#include "schema/pgUser.h"
#include "schema/pgEventTrigger.h"
void dataType::SetOid(OID id)
{
oid = id;
}
void dataType::SetTypename(wxString name)
{
typeName = name;
}
OID dataType::GetOid()
{
return oid;
}
wxString dataType::GetTypename()
{
return typeName;
}
#define CTRLID_CHKSQLTEXTFIELD 1000
BEGIN_EVENT_TABLE(dlgProperty, DialogWithHelp)
EVT_NOTEBOOK_PAGE_CHANGED(XRCID("nbNotebook"), dlgProperty::OnPageSelect)
EVT_TEXT(XRCID("txtName"), dlgProperty::OnChange)
EVT_TEXT(XRCID("cbOwner"), dlgProperty::OnChangeOwner)
EVT_COMBOBOX(XRCID("cbOwner"), dlgProperty::OnChange)
EVT_TEXT(XRCID("cbSchema"), dlgProperty::OnChange)
EVT_COMBOBOX(XRCID("cbSchema"), dlgProperty::OnChange)
EVT_TEXT(XRCID("txtComment"), dlgProperty::OnChange)
EVT_CHECKBOX(CTRLID_CHKSQLTEXTFIELD, dlgProperty::OnChangeReadOnly)
EVT_BUTTON(wxID_HELP, dlgProperty::OnHelp)
EVT_BUTTON(wxID_OK, dlgProperty::OnOK)
END_EVENT_TABLE();
dlgProperty::dlgProperty(pgaFactory *f, frmMain *frame, const wxString &resName) : DialogWithHelp(frame)
{
readOnly = false;
sqlPane = 0;
sqlTextField1 = 0;
sqlTextField2 = 0;
processing = false;
mainForm = frame;
database = 0;
connection = 0;
factory = f;
item = (void *)NULL;
owneritem = (void *)NULL;
chkReadOnly = (wxCheckBox *)NULL;
SetFont(settings->GetSystemFont());
LoadResource(frame, resName);
#ifdef __WXMSW__
SetWindowStyleFlag(GetWindowStyleFlag() & ~wxMAXIMIZE_BOX);
#endif
nbNotebook = CTRL_NOTEBOOK("nbNotebook");
if (!nbNotebook)
{
wxMessageBox(wxString::Format(_("Problem with resource %s: Notebook not found.\nPrepare to crash!"), resName.c_str()));
return;
}
// Set the icon
wxBitmap bm(factory->GetImage());
wxIcon ico;
ico.CopyFromBitmap(bm);
SetIcon(ico);
txtName = CTRL_TEXT("txtName");
txtOid = CTRL_TEXT("txtOID");
txtComment = CTRL_TEXT("txtComment");
cbOwner = CTRL_COMBOBOX2("cbOwner");
cbSchema = CTRL_COMBOBOX2("cbSchema");
cbClusterSet = CTRL_COMBOBOX1("cbClusterSet");
wxString db = wxT("Database");
wxString ts = wxT("Tablespace");
wxString rg = wxT("Resource Group");
enableSQL2 = db.Cmp(factory->GetTypeName()) == 0
|| ts.Cmp(factory->GetTypeName()) == 0
|| rg.Cmp(factory->GetTypeName()) == 0;
wxNotebookPage *page = nbNotebook->GetPage(0);
wxASSERT(page != NULL);
page->GetClientSize(&width, &height);
numericValidator.SetStyle(wxFILTER_NUMERIC);
btnOK->Disable();
statusBar = XRCCTRL(*this, "unkStatusBar", wxStatusBar);
}
dlgProperty::~dlgProperty()
{
wxString prop = wxT("Properties/") + wxString(factory->GetTypeName());
settings->WritePoint(prop, GetPosition());
if (GetWindowStyle() & wxRESIZE_BORDER)
settings->WriteSize(prop, GetSize());
if (obj)
obj->SetWindowPtr(NULL);
}
wxString dlgProperty::GetHelpPage() const
{
wxString page;
pgObject *obj = ((dlgProperty *)this)->GetObject();
if (obj)
page = obj->GetHelpPage(false);
else
{
// Attempt to get he page from the dialogue, otherwise, take a shot at it!
page = this->GetHelpPage(true);
if (page.Length() == 0)
{
page = wxT("pg/sql-create");
page += wxString(factory->GetTypeName()).Lower();
}
}
return page;
}
void dlgProperty::CheckValid(bool &enable, const bool condition, const wxString &msg)
{
if (enable)
{
if (!condition)
{
if (statusBar)
statusBar->SetStatusText(msg);
enable = false;
}
}
}
void dlgProperty::SetDatabase(pgDatabase *db)
{
database = db;
if (db)
connection = db->GetConnection();
}
void dlgProperty::SetDatatypeCache(dataTypeCache cache)
{
dtCache = cache;
}
void dlgProperty::EnableOK(bool enable)
{
btnOK->Enable(enable);
if (enable)
{
if (statusBar)
statusBar->SetStatusText(wxEmptyString);
}
}
void dlgProperty::SetSqlReadOnly(bool readonly)
{
if (chkReadOnly)
chkReadOnly->Enable(!readonly);
}
int dlgProperty::Go(bool modal)
{
wxASSERT(factory != 0);
if(GetObject())
obj = GetObject();
else
obj = mainForm->GetBrowser()->GetObject(mainForm->GetBrowser()->GetSelection());
// restore previous position and size, if applicable
wxString prop = wxT("Properties/") + wxString(factory->GetTypeName());
wxSize origSize = GetSize();
if (GetWindowStyle() & wxRESIZE_BORDER)
SetSize(settings->Read(prop, GetSize()));
wxPoint pos = settings->Read(prop, GetPosition());
if (pos.x < 0)
pos.x = 0;
if (pos.y < 0)
pos.y = 0;
Move(pos);
wxSize size = GetSize();
CheckOnScreen(this, pos, size, origSize.GetWidth(), origSize.GetHeight());
Move(pos);
ctlComboBoxFix *cbowner = (ctlComboBoxFix *)cbOwner;
ctlComboBoxFix *cbschema = (ctlComboBoxFix *)cbSchema;
if (cbClusterSet)
{
cbClusterSet->Append(wxEmptyString);
cbClusterSet->SetSelection(0);
if (mainForm && database)
{
wxArrayString clusters = database->GetSlonyClusters(mainForm->GetBrowser());
size_t i;
for (i = 0 ; i < clusters.GetCount() ; i++)
{
wxString cluster = wxT("_") + clusters.Item(i);
pgSetIterator sets(connection,
wxT("SELECT set_id, ") + qtIdent(cluster) + wxT(".slonyversionmajor(), ") + qtIdent(cluster) + wxT(".slonyversionminor()\n")
wxT(" FROM ") + qtIdent(cluster) + wxT(".sl_set\n")
wxT(" WHERE set_origin = ") + qtIdent(cluster) +
wxT(".getlocalnodeid(") + qtDbString(cluster) + wxT(");"));
while (sets.RowsLeft())
{
wxString str;
long setId = sets.GetLong(wxT("set_id"));
long majorVer = sets.GetLong(wxT("slonyversionmajor"));
long minorVer = sets.GetLong(wxT("slonyversionminor"));
str.Printf(_("Cluster \"%s\", set %ld"), clusters.Item(i).c_str(), setId);
cbClusterSet->Append(str, static_cast<void *>(new replClientData(cluster, setId, majorVer, minorVer)));
}
}
}
if (cbClusterSet->GetCount() < 2)
cbClusterSet->Disable();
}
if (cbowner && !cbowner->GetCount())
{
if (!GetObject())
cbOwner->Append(wxEmptyString);
AddGroups(cbowner);
AddUsers(cbowner);
}
if (txtOid)
txtOid->Disable();
if (cbschema && !cbschema->GetCount())
AddSchemas(cbschema);
if (GetObject())
{
if (txtName)
txtName->SetValue(GetObject()->GetName());
if (txtOid)
txtOid->SetValue(NumToStr((unsigned long)GetObject()->GetOid()));
if (cbOwner)
cbOwner->SetValue(GetObject()->GetOwner());
if (cbSchema)
cbSchema->SetValue(GetObject()->GetSchema()->GetName());
if (txtComment)
txtComment->SetValue(GetObject()->GetComment());
if (!readOnly && !GetObject()->CanCreate())
{
// users who can't create will usually not be allowed to change either.
readOnly = false;
}
wxString typeName = factory->GetTypeName();
SetTitle(wxString(wxGetTranslation(typeName)) + wxT(" ") + GetObject()->GetFullIdentifier());
}
else
{
if (factory)
SetTitle(wxGetTranslation(factory->GetNewString()));
if (cbSchema)
{
if (obj->GetMetaType() == PGM_SCHEMA)
cbSchema->SetValue(obj->GetName());
else
cbSchema->SetValue(obj->GetSchema()->GetName());
}
}
if (statusBar)
statusBar->SetStatusText(wxEmptyString);
if (nbNotebook)
{
wxNotebookPage *pg = nbNotebook->GetPage(0);
if (pg)
pg->SetFocus();
}
// This fixes a UI glitch on MacOS X and Windows
// Because of the new layout code, the Privileges pane don't size itself properly
SetSize(GetSize().GetWidth() + 1, GetSize().GetHeight());
SetSize(GetSize().GetWidth() - 1, GetSize().GetHeight());
if (modal)
return ShowModal();
else
Show(true);
return 0;
}
void dlgProperty::CreateAdditionalPages()
{
if (wxString(factory->GetTypeName()).Cmp(wxT("Server")))
{
// create a panel
sqlPane = new wxPanel(nbNotebook);
// add panel to the notebook
nbNotebook->AddPage(sqlPane, wxT("SQL"));
// create a flex grid sizer
wxFlexGridSizer *fgsizer = new wxFlexGridSizer(1, 5, 5);
// add checkbox to the panel
chkReadOnly = new wxCheckBox(sqlPane, CTRLID_CHKSQLTEXTFIELD, _("Read only"));
chkReadOnly->SetValue(true);
fgsizer->Add(chkReadOnly, 1, wxALL | wxALIGN_LEFT, 5);
// text entry box
sqlTextField1 = new ctlSQLBox(sqlPane, CTL_PROPSQL,
wxDefaultPosition, wxDefaultSize,
wxTE_MULTILINE | wxSUNKEN_BORDER | wxTE_RICH2);
fgsizer->Add(sqlTextField1, 1, wxALL | wxEXPAND, 5);
// text entry box
if (enableSQL2)
{
sqlTextField2 = new ctlSQLBox(sqlPane, CTL_PROPSQL,
wxDefaultPosition, wxDefaultSize,
wxTE_MULTILINE | wxSUNKEN_BORDER | wxTE_RICH2);
fgsizer->Add(sqlTextField2, 1, wxALL | wxEXPAND, 5);
}
fgsizer->AddGrowableCol(0);
fgsizer->AddGrowableRow(1);
if (fgsizer->GetRows() > 1)
{
fgsizer->AddGrowableRow(2);
}
sqlPane->SetAutoLayout(true);
sqlPane->SetSizer(fgsizer);
}
}
wxString dlgProperty::GetName()
{
if (txtName)
{
if (GetObject())
{
// If there is an existing object name with a leading or trailing
// space, don't try to remove it.
if (GetObject()->GetName() == txtName->GetValue())
return txtName->GetValue();
else
return txtName->GetValue().Strip(wxString::both);
}
else
return txtName->GetValue().Strip(wxString::both);
}
return wxEmptyString;
}
void dlgProperty::AppendNameChange(wxString &sql, const wxString &objName)
{
if (GetObject()->GetName() != GetName())
{
if (objName.Length() > 0)
{
sql += wxT("ALTER ") + objName
+ wxT("\n RENAME TO ") + qtIdent(GetName())
+ wxT(";\n");
}
else
{
sql += wxT("ALTER ") + GetObject()->GetTypeName().MakeUpper()
+ wxT(" ") + GetObject()->GetQuotedFullIdentifier()
+ wxT("\n RENAME TO ") + qtIdent(GetName())
+ wxT(";\n");
}
}
}
void dlgProperty::AppendOwnerChange(wxString &sql, const wxString &objName)
{
if (!GetObject() || GetObject()->GetOwner() != cbOwner->GetValue())
{
sql += wxT("ALTER ") + objName
+ wxT("\n OWNER TO ") + qtIdent(cbOwner->GetValue())
+ wxT(";\n");
}
}
void dlgProperty::AppendOwnerNew(wxString &sql, const wxString &objName)
{
if (cbOwner->GetGuessedSelection() > 0)
sql += wxT("ALTER ") + objName
+ wxT("\n OWNER TO ") + qtIdent(cbOwner->GetValue())
+ wxT(";\n");
}
void dlgProperty::AppendSchemaChange(wxString &sql, const wxString &objName)
{
wxString currentschema;
if (GetObject()->GetMetaType() == PGM_SCHEMA)
{
currentschema = GetObject()->GetName();
}
else
{
currentschema = GetObject()->GetSchema()->GetName();
}
if (currentschema != cbSchema->GetValue())
{
sql += wxT("ALTER ") + objName
+ wxT("\n SET SCHEMA ") + qtIdent(cbSchema->GetValue())
+ wxT(";\n");
}
}
void dlgProperty::AppendComment(wxString &sql, const wxString &objName, pgObject *obj)
{
wxString comment = txtComment->GetValue();
if ((!obj && !comment.IsEmpty()) || (obj && obj->GetComment() != comment))
{
sql += wxT("COMMENT ON ") + objName
+ wxT("\n IS ") + qtDbString(comment) + wxT(";\n");
}
}
void dlgProperty::AppendComment(wxString &sql, const wxString &objType, pgSchema *schema, pgObject *obj)
{
wxString comment = txtComment->GetValue();
if ((!obj && !comment.IsEmpty()) || (obj && obj->GetComment() != comment))
{
sql += wxT("COMMENT ON ") + objType + wxT(" ");
if (schema)
sql += schema->GetQuotedPrefix();
sql += qtIdent(GetName()) + wxT("\n IS ") + qtDbString(comment) + wxT(";\n");
}
}
void dlgProperty::AppendQuoted(wxString &sql, const wxString &name)
{
// quick and quite dirty:
// !!! this is unsafe if the name itself contains a dot which isn't meant as separator between schema and object
if (name.First('.') >= 0)
{
sql += qtIdent(name.BeforeFirst('.')) + wxT(".") + qtIdent(name.AfterFirst('.'));
}
else
sql += qtIdent(name);
}
void dlgProperty::AppendQuotedType(wxString &sql, const wxString &name)
{
// see AppendQuoted()
if (name.First('.') >= 0)
{
sql += qtIdent(name.BeforeFirst('.')) + wxT(".") + qtTypeIdent(name.AfterFirst('.'));
}
else
sql += qtTypeIdent(name);
}
void dlgProperty::FillCombobox(const wxString &query, ctlComboBoxFix *cb1, ctlComboBoxFix *cb2)
{
if (!cb1 && !cb2)
return;
pgSet *set = connection->ExecuteSet(query);
if (set)
{
while (!set->Eof())
{
if (cb1)
cb1->Append(set->GetVal(0));
if (cb2)
cb2->Append(set->GetVal(0));
set->MoveNext();
}
delete set;
}
}
void dlgProperty::AddDatabases(ctlComboBoxFix *cb)
{
FillCombobox(wxT("SELECT datname FROM pg_database ORDER BY 1"), cb);
}
void dlgProperty::AddUsers(ctlComboBoxFix *cb1, ctlComboBoxFix *cb2)
{
if (connection->BackendMinimumVersion(8, 1))
{
FillCombobox(wxT("SELECT rolname FROM pg_roles WHERE rolcanlogin ORDER BY 1"), cb1, cb2);
}
else
{
FillCombobox(wxT("SELECT usename FROM pg_user ORDER BY 1"), cb1, cb2);
}
}
void dlgProperty::AddGroups(ctlComboBoxFix *combo)
{
if (connection->BackendMinimumVersion(8, 1))
{
FillCombobox(wxT("SELECT rolname FROM pg_roles WHERE NOT rolcanlogin ORDER BY 1"), combo);
}
else
{
FillCombobox(wxT("SELECT groname FROM pg_group ORDER BY 1"), combo);
}
}
void dlgProperty::AddSchemas(ctlComboBoxFix *combo)
{
if (connection->BackendMinimumVersion(8, 1))
{
FillCombobox(wxT("SELECT nspname FROM pg_namespace WHERE nspname NOT LIKE E'pg\\\\_%' AND nspname != 'information_schema' ORDER BY nspname"),
combo);
}
}
void dlgProperty::PrepareTablespace(ctlComboBoxFix *cb, const OID current)
{
wxASSERT(cb != 0);
if (connection->BackendMinimumVersion(8, 0))
{
// Populate the combo
cb->FillOidKey(connection, wxT("SELECT oid, spcname FROM pg_tablespace WHERE spcname <> 'pg_global' ORDER BY spcname"));
if (current)
cb->SetKey(current);
else
{
if (database)
cb->SetValue(database->GetDefaultTablespace());
else
{
wxString def = connection->ExecuteScalar(wxT("SELECT current_setting('default_tablespace');"));
if (def == wxEmptyString || def == wxT("unset"))
def = wxT("pg_default");
cb->SetValue(def);
}
}
}
else
cb->Disable();
}
void dlgProperty::OnChangeStc(wxStyledTextEvent &ev)
{
CheckChange();
}
void dlgProperty::OnChange(wxCommandEvent &ev)
{
CheckChange();
}
void dlgProperty::OnChangeOwner(wxCommandEvent &ev)
{
ctlComboBox *cb = cbOwner;
if (cb)
cb->GuessSelection(ev);
CheckChange();
}
void dlgProperty::OnChangeReadOnly(wxCommandEvent &ev)
{
size_t pos;
bool showmessage;
showmessage = chkReadOnly->GetValue()
&& ! (!enableSQL2 && GetSql().Length() == 0 && sqlTextField1->GetText().Cmp(_("-- nothing to change")) == 0)
&& ! (!enableSQL2 && GetSql().Length() == 0 && sqlTextField1->GetText().Cmp(_("-- definition incomplete")) == 0)
&& ! (enableSQL2 && GetSql().Length() == 0 && GetSql2().Length() == 0 && sqlTextField1->GetText().Cmp(_("-- nothing to change")) == 0 && sqlTextField2->GetText().Length() == 0)
&& ! (enableSQL2 && GetSql().Length() == 0 && GetSql2().Length() == 0 && sqlTextField1->GetText().Cmp(_("-- definition incomplete")) == 0 && sqlTextField2->GetText().Length() == 0)
&& (sqlTextField1->GetText().Cmp(GetSql()) != 0 || (enableSQL2 && sqlTextField2->GetText().Cmp(GetSql2()) != 0));
if (showmessage)
{
if (wxMessageBox(_("Are you sure you wish to cancel your edit?"), _("SQL editor"), wxYES_NO | wxNO_DEFAULT) != wxYES)
{
chkReadOnly->SetValue(false);
return;
}
}
sqlTextField1->SetReadOnly(chkReadOnly->GetValue());
if (enableSQL2)
{
sqlTextField2->SetReadOnly(chkReadOnly->GetValue());
}
for (pos = 0; pos < nbNotebook->GetPageCount() - 1; pos++)
{
nbNotebook->GetPage(pos)->Enable(chkReadOnly->GetValue());
}
if (chkReadOnly->GetValue())
{
FillSQLTextfield();
}
}
void dlgProperty::FillSQLTextfield()
{
// create a function because this is a duplicated code
sqlTextField1->SetReadOnly(false);
if (enableSQL2)
{
sqlTextField2->SetReadOnly(false);
}
if (btnOK->IsEnabled())
{
wxString tmp;
if (cbClusterSet && cbClusterSet->GetSelection() > 0)
{
replClientData *data = (replClientData *)cbClusterSet->wxItemContainer::GetClientData(cbClusterSet->GetSelection());
if(data)
tmp.Printf(_("-- Execute replicated using cluster \"%s\", set %ld\n"), data->cluster.c_str(), data->setId);
}
sqlTextField1->SetText(tmp + GetSql());
if (enableSQL2)
{
sqlTextField2->SetText(GetSql2());
}
}
else
{
if (GetObject())
sqlTextField1->SetText(_("-- nothing to change"));
else
sqlTextField1->SetText(_("-- definition incomplete"));
if (enableSQL2)
{
sqlTextField2->SetText(wxT(""));
}
}
sqlTextField1->SetReadOnly(true);
if (enableSQL2)
{
sqlTextField2->SetReadOnly(true);
}
}
bool dlgProperty::tryUpdate(wxTreeItemId collectionItem)
{
ctlTree *browser = mainForm->GetBrowser();
pgCollection *collection = (pgCollection *)browser->GetObject(collectionItem);
if (collection && collection->IsCollection() && factory->GetCollectionFactory() == collection->GetFactory())
{
pgObject *data = CreateObject(collection);
if (data)
{
wxString nodeName = this->GetDisplayName();
if (nodeName.IsEmpty())
nodeName = data->GetDisplayName();
size_t pos = 0;
wxTreeItemId newItem;
if (!data->IsCreatedBy(columnFactory))
{
// columns should be appended, not inserted alphabetically
wxCookieType cookie;
newItem = browser->GetFirstChild(collectionItem, cookie);
while (newItem)
{
if (browser->GetItemText(newItem) > nodeName)
break;
pos++;
newItem = browser->GetNextChild(collectionItem, cookie);
}
}
if (newItem)
browser->InsertItem(collectionItem, pos, nodeName, data->GetIconId(), -1, data);
else
browser->AppendItem(collectionItem, nodeName, data->GetIconId(), -1, data);
if (data->WantDummyChild())
browser->AppendItem(data->GetId(), wxT("Dummy"));
if (browser->GetSelection() == item)
collection->ShowTreeDetail(browser, 0, mainForm->GetProperties());
else
collection->UpdateChildCount(browser);
}
else
{
// CreateObject didn't return a new pgObject; refresh the complete collection
mainForm->Refresh(collection);
}
return true;
}
return false;
}
void dlgProperty::ShowObject()
{
mainForm->ObjectBrowserRefreshing(true);
pgObject *data = GetObject();
// We might have a parent to refresh. If so, the children will
// inherently get refreshed as well. Yay :-)
if (owneritem)
{
// Get the object node in case we need it later
wxTreeItemId objectnode = mainForm->GetBrowser()->GetItemParent(owneritem);
// Stash the selected items path
wxString currentPath = mainForm->GetCurrentNodePath();
pgObject *tblobj = mainForm->GetBrowser()->GetObject(owneritem);
if (tblobj)
{
dlgProperty *ownDialog = NULL;
if (data)
{
ownDialog = data->GetWindowPtr();
data->SetWindowPtr(NULL);
}
mainForm->Refresh(tblobj);
if (data)
{
data->SetWindowPtr(ownDialog);
}
}
// Restore the previous selection...
mainForm->SetCurrentNode(mainForm->GetBrowser()->GetRootItem(), currentPath);
}
else if (data)
{
pgObject *newData = data->Refresh(mainForm->GetBrowser(), item);
if (newData && newData != data)
{
mainForm->SetCurrentObject(newData);
mainForm->GetBrowser()->SetItemData(item, newData);
newData->SetId(item);
delete data;
SetObject(newData);
newData->UpdateIcon(mainForm->GetBrowser());
}
if (newData)
{
mainForm->GetBrowser()->DeleteChildren(newData->GetId());
if (item == mainForm->GetBrowser()->GetSelection())
newData->ShowTree(mainForm, mainForm->GetBrowser(), mainForm->GetProperties(), 0);
mainForm->GetBrowser()->SetItemText(item, newData->GetFullName());
mainForm->GetSqlPane()->SetReadOnly(false);
mainForm->GetSqlPane()->SetText(newData->GetSql(mainForm->GetBrowser()));
mainForm->GetSqlPane()->SetReadOnly(true);
}
}
else if (item && chkReadOnly->GetValue())
{
wxTreeItemId collectionItem = item;
while (collectionItem)
{
// search up the tree for our collection
if (tryUpdate(collectionItem))
break;
collectionItem = mainForm->GetBrowser()->GetItemParent(collectionItem);
}
}
else // Brute force update the current item
{
pgObject *currobj = mainForm->GetBrowser()->GetObject(mainForm->GetBrowser()->GetSelection());
if (currobj)
mainForm->Refresh(currobj);
}
mainForm->ObjectBrowserRefreshing(false);
}
bool dlgProperty::apply(const wxString &sql, const wxString &sql2)
{
wxString tmp;
pgConn *myConn = connection;
if (GetDisconnectFirst())
{
myConn = database->GetServer()->GetConnection();
database->Disconnect();
}
if (!sql.IsEmpty())
{
wxArrayString queries;
if (WannaSplitQueries())
queries = SplitQueries(BuildSql(sql));
else
queries.Add(BuildSql(sql));
for (size_t index = 0; index < queries.GetCount(); index++)
{
tmp = queries.Item(index);
if (!myConn->ExecuteVoid(tmp))
{
// error message is displayed inside ExecuteVoid
return false;
}
if (database)
database->AppendSchemaChange(tmp);
}
}
// Process the second SQL statement. This is primarily only used by
// CREATE DATABASE which cannot be run in a multi-statement query in
// PostgreSQL 8.3+
if (!sql2.IsEmpty())
{
tmp = BuildSql(sql2);
if (!myConn->ExecuteVoid(tmp))
{
// error message is displayed inside ExecuteVoid
// Warn the user about partially applied changes, but don't bail out.
// Carry on as if everything was successful (because the most important
// change was!!
wxMessageBox(_("An error occurred executing the second stage SQL statement.\n\nChanges may have been partially applied."), _("Warning"), wxICON_EXCLAMATION | wxOK, this);
}
else // Only apend schema changes if there was no error.
{
if (database)
database->AppendSchemaChange(tmp);
}
}
ShowObject();
return true;
}
wxString dlgProperty::BuildSql(const wxString &sql)
{
wxString tmp;
if (cbClusterSet && cbClusterSet->GetSelection() > 0)
{
replClientData *data = (replClientData *)cbClusterSet->wxItemContainer::GetClientData(cbClusterSet->GetSelection());
if (data)
{
if (data->majorVer > 1 || (data->majorVer == 1 && data->minorVer >= 2))
{
// From slony version 2.2.0 onwards ddlscript_prepare() method is removed and
// ddlscript_complete() method arguments got changed so we have to use ddlcapture() method
// instead of ddlscript_prepare() and changed the argument of ddlscript_complete() method
if ((data->majorVer == 2 && data->minorVer >= 2) || (data->majorVer > 2))
{
tmp = wxT("SELECT ") + qtIdent(data->cluster)
+ wxT(".ddlcapture(") + qtDbString(sql) + wxT(", ") + wxT("NULL::text") + wxT(" );\n")
+ wxT("SELECT ") + qtIdent(data->cluster)
+ wxT(".ddlscript_complete(") + wxT("NULL::text") + wxT(" );\n");
}
else
{
tmp = wxT("SELECT ") + qtIdent(data->cluster)
+ wxT(".ddlscript_prepare(") + NumToStr(data->setId) + wxT(", -1);\n")
+ sql + wxT(";\n")
+ wxT("SELECT ") + qtIdent(data->cluster)
+ wxT(".ddlscript_complete(") + NumToStr(data->setId) + wxT(", ")
+ qtDbString(sql) + wxT(", -1);\n");
}
}
else
{
tmp = wxT("SELECT ") + qtIdent(data->cluster)
+ wxT(".ddlscript(") + NumToStr(data->setId) + wxT(", ")
+ qtDbString(sql) + wxT(", 0);\n");
}
}
}
else
tmp = sql;
return tmp;
}
wxArrayString dlgProperty::SplitQueries(const wxString &sql)
{