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
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// dlgSynonym.cpp - EnterpriseDB Synonym Property
//
//////////////////////////////////////////////////////////////////////////
// wxWindows headers
#include <wx/wx.h>
// App headers
#include "pgAdmin3.h"
#include "utils/misc.h"
#include "utils/pgDefs.h"
#include "dlg/dlgSynonym.h"
#include "schema/edbSynonym.h"
#include "schema/edbPrivateSynonym.h"
// pointer to controls
#define txtName CTRL_TEXT("txtName")
#define cbTargetType CTRL_COMBOBOX2("cbTargetType")
#define cbTargetSchema CTRL_COMBOBOX2("cbTargetSchema")
#define cbTargetObject CTRL_COMBOBOX("cbTargetObject")
#define stComment CTRL_STATIC("stComment")
dlgProperty *edbSynonymFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
return new dlgSynonym(this, frame, (edbSynonym *)node);
}
dlgProperty *edbPrivateSynonymFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
return new dlgSynonym(this, frame, (edbPrivateSynonym *)node, (pgSchema *)parent);
}
BEGIN_EVENT_TABLE(dlgSynonym, dlgProperty)
EVT_TEXT(XRCID("cbTargetType"), dlgSynonym::OnChangeTargetType)
EVT_COMBOBOX(XRCID("cbTargetType"), dlgProperty::OnChange)
EVT_TEXT(XRCID("cbTargetSchema"), dlgSynonym::OnChangeTargetSchema)
EVT_COMBOBOX(XRCID("cbTargetSchema"), dlgProperty::OnChange)
EVT_COMBOBOX(XRCID("cbTargetObject"), dlgProperty::OnChange)
END_EVENT_TABLE();
dlgSynonym::dlgSynonym(pgaFactory *f, frmMain *frame, edbSynonym *node)
: dlgProperty(f, frame, wxT("dlgSynonym"))
{
synonym = node;
privSynonym = NULL;
synonymSchema = NULL;
cbOwner->Disable();
}
dlgSynonym::dlgSynonym(edbPrivateSynonymFactory *factory, frmMain *frame, edbPrivateSynonym *syn, pgSchema *schema)
: dlgProperty((pgaFactory *)factory, frame, wxT("dlgSynonym"))
{
synonym = NULL;
privSynonym = syn;
synonymSchema = schema;
cbOwner->Disable();
}
pgObject *dlgSynonym::GetObject()
{
if (!synonymSchema)
return synonym;
return (pgObject *)privSynonym;
}
int dlgSynonym::Go(bool modal)
{
if (synonym)
{
// edit mode
txtName->Disable();
cbTargetType->SetSelection(cbTargetType->FindString(synonym->GetTargetType()));
ProcessTypeChange();
if (cbTargetType->GetValue() != _("Public synonym"))
{
cbTargetSchema->SetSelection(cbTargetSchema->FindString(synonym->GetTargetSchema()));
ProcessSchemaChange();
}
cbTargetObject->SetSelection(cbTargetObject->FindString(synonym->GetTargetObject()));
}
else if (privSynonym)
{
// edit mode
txtName->Disable();
cbTargetType->SetSelection(cbTargetType->FindString(privSynonym->GetTargetType()));
ProcessTypeChange();
cbTargetSchema->SetSelection(cbTargetSchema->FindString(privSynonym->GetTargetSchema()));
ProcessSchemaChange();
cbTargetObject->SetSelection(cbTargetObject->FindString(privSynonym->GetTargetObject()));
}
else
{
// create mode
cbTargetType->Enable();
cbTargetSchema->Disable();
cbTargetObject->Disable();
}
txtComment->Disable();
// Functions and Procedures are available 8.4 onwards. So check for the
// same. Unfortunately we need a connection object for that and so we
// re-use an existing one..
if (connection && connection->BackendMinimumVersion(8, 4))
{
cbTargetType->Append(_("Function"));
cbTargetType->Append(_("Procedure"));
}
cbTargetType->Append(_("Sequence"));
cbTargetType->Append(_("Public synonym"));
cbTargetType->Append(_("Table"));
cbTargetType->Append(_("View"));
return dlgProperty::Go(modal);
}
pgObject *dlgSynonym::CreateObject(pgCollection *collection)
{
pgObject *obj = NULL;
if (!synonymSchema)
obj = synonymFactory.CreateObjects(collection, 0,
wxT(" WHERE synname = ") + qtDbString(GetName()));
else
obj = edbPrivFactory.CreateObjects(collection, 0,
wxT(" WHERE s.synname=") + qtDbString(GetName()) +
wxT(" AND s.synnamespace=") + collection->GetSchema()->GetOidStr() + wxT(" \n"));
return obj;
}
void dlgSynonym::CheckChange()
{
bool enable = true;
CheckValid(enable, !txtName->GetValue().IsEmpty(), _("Please specify name."));
CheckValid(enable, !cbTargetType->GetValue().IsEmpty(), _("Please select target type."));
// Public Synonyms does supported in public only
if (!synonymSchema && cbTargetType->GetValue() != _("Public synonym"))
CheckValid(enable, !cbTargetSchema->GetValue().IsEmpty(), _("Please select target schema."));
CheckValid(enable, !cbTargetObject->GetValue().IsEmpty(), _("Please select target object."));
if (!enable)
{
EnableOK(enable);
return;
}
if (synonym)
EnableOK(synonym->GetTargetObject() != cbTargetObject->GetValue());
else if (privSynonym)
EnableOK(privSynonym->GetTargetObject() != cbTargetObject->GetValue());
else
EnableOK(txtName->GetValue() != wxEmptyString && cbTargetObject->GetValue() != wxEmptyString);
}
void dlgSynonym::ProcessTypeChange()
{
cbTargetSchema->Clear();
cbTargetObject->Clear();
if (cbTargetType->GetValue() != _("Public synonym"))
{
pgSet *schemas;
if (connection->BackendMinimumVersion(8, 2))
schemas = connection->ExecuteSet(wxT("SELECT nspname FROM pg_namespace WHERE nspparent = 0 AND nspname NOT LIKE E'pg\\_%' AND nspname NOT IN ('pg_catalog', 'sys', 'dbo', 'pgagent', 'information_schema', 'dbms_job_procedure') ORDER BY nspname;"));
else if (connection->BackendMinimumVersion(8, 1))
schemas = connection->ExecuteSet(wxT("SELECT nspname FROM pg_namespace WHERE nspname NOT LIKE E'pg\\_%' ORDER BY nspname;"));
else
schemas = connection->ExecuteSet(wxT("SELECT nspname FROM pg_namespace WHERE nspname NOT LIKE 'pg\\_%' ORDER BY nspname;"));
for (int x = 0; x < schemas->NumRows(); x++)
{
cbTargetSchema->Append(schemas->GetVal(0));
schemas->MoveNext();
}
delete schemas;
cbTargetSchema->Enable();
cbTargetSchema->SetValue(wxT("public"));
ProcessSchemaChange();
}
else
{
pgSet *synonyms = connection->ExecuteSet(wxT("SELECT synname FROM pg_synonym ORDER BY synname;"));
for (int x = 0; x < synonyms->NumRows(); x++)
{
cbTargetObject->Append(synonyms->GetVal(0));
synonyms->MoveNext();
}
delete synonyms;
cbTargetSchema->Disable();
cbTargetObject->Enable();
}
}
void dlgSynonym::ProcessSchemaChange()
{
cbTargetObject->Clear();
wxString restriction;
if (cbTargetType->GetValue() == _("Sequence"))
restriction = wxT("S");
else if (cbTargetType->GetValue() == _("Table"))
restriction = wxT("r");
else if (cbTargetType->GetValue() == _("View"))
restriction = wxT("v");
else if (cbTargetType->GetValue() == _("Function"))
restriction = wxT("0");
else if (cbTargetType->GetValue() == _("Procedure"))
restriction = wxT("1");
wxString sql;
if (cbTargetType->GetValue() == _("Synonym"))
{
sql = wxT("SELECT synname FROM pg_synonym s JOIN pg_namespace n\n")
wxT(" ON s.synnamespace = n.oid AND \n")
wxT(" n.nspname = ") + qtDbString(cbTargetSchema->GetValue()) +
wxT(" ORDER BY synname;");
}
else if (cbTargetType->GetValue() == _("Function") ||
cbTargetType->GetValue() == _("Procedure"))
{
// "protype" is available, no need to check for version again here..
sql = wxT("SELECT DISTINCT proname from pg_proc p, pg_namespace n\n")
wxT(" WHERE p.pronamespace = n.oid AND\n")
wxT(" n.nspname = ") + qtDbString(cbTargetSchema->GetValue()) + wxT(" AND\n")
wxT(" p.protype = '") + restriction + wxT("' ORDER BY proname;");
}
else if (cbTargetType->GetValue() == _("View"))
{
wxString mviewRestriction = wxT("m");
sql = wxT("SELECT relname FROM pg_class c, pg_namespace n\n")
wxT(" WHERE c.relnamespace = n.oid AND\n")
wxT(" n.nspname = ") + qtDbString(cbTargetSchema->GetValue()) + wxT(" AND\n")
wxT(" (c.relkind = '") + restriction + wxT("' OR c.relkind = '") + mviewRestriction + wxT("') ORDER BY relname;");
}
else
{
sql = wxT("SELECT relname FROM pg_class c, pg_namespace n\n")
wxT(" WHERE c.relnamespace = n.oid AND\n")
wxT(" n.nspname = ") + qtDbString(cbTargetSchema->GetValue()) + wxT(" AND\n")
wxT(" c.relkind = '") + restriction + wxT("' ORDER BY relname;");
}
pgSet *objects = connection->ExecuteSet(sql);
for (int x = 0; x < objects->NumRows(); x++)
{
cbTargetObject->Append(objects->GetVal(0));
objects->MoveNext();
}
delete objects;
cbTargetObject->Enable();
}
wxString dlgSynonym::GetSql()
{
wxString sql;
if (!synonymSchema)
{
sql = wxT("CREATE OR REPLACE PUBLIC SYNONYM ") + qtIdent(txtName->GetValue()) + wxT("\n FOR ");
if (cbTargetSchema->GetValue() != wxEmptyString)
sql += qtIdent(cbTargetSchema->GetValue()) + wxT(".");
sql += qtIdent(cbTargetObject->GetValue()) + wxT(";\n");
AppendComment(sql, wxT("PUBLIC SYNONYM ") + qtIdent(txtName->GetValue()), synonym);
}
else
{
wxString createSql, commentSql;
if (synonymSchema->GetName() == wxT("public"))
{
createSql = wxT("CREATE OR REPLACE PUBLIC SYNONYM ");
commentSql = wxT("PUBLIC SYNONYM ");
}
else
{
createSql = wxT("CREATE OR REPLACE SYNONYM ") + qtIdent(synonymSchema->GetName()) + wxT(".");
commentSql = wxT("PRIVATE SYNONYM ");
}
sql = createSql + qtIdent(txtName->GetValue()) + wxT("\n FOR ");
if (cbTargetSchema->GetValue() != wxEmptyString)
sql += qtIdent(cbTargetSchema->GetValue()) + wxT(".");
sql += qtIdent(cbTargetObject->GetValue()) + wxT(";\n");
AppendComment(sql, commentSql + qtIdent(txtName->GetValue()), synonym);
}
return sql;
}
|