1
//////////////////////////////////////////////////////////////////////////
3
// pgAdmin III - PostgreSQL Tools
4
// RCS-ID: $Id: pgView.cpp 6999 2008-01-16 15:31:52Z dpage $
5
// Copyright (C) 2002 - 2008, The pgAdmin Development Team
6
// This software is released under the Artistic Licence
8
// pgView.cpp - View class
10
//////////////////////////////////////////////////////////////////////////
17
#include "utils/misc.h"
18
#include "schema/pgColumn.h"
19
#include "schema/pgView.h"
20
#include "frm/frmHint.h"
23
pgView::pgView(pgSchema *newSchema, const wxString& newName)
24
: pgRuleObject(newSchema, viewFactory, newName)
35
bool pgView::IsUpToDate()
37
wxString sql = wxT("SELECT xmin FROM pg_class WHERE oid = ") + this->GetOidStr();
38
if (!this->GetDatabase()->GetConnection() || this->GetDatabase()->ExecuteScalar(sql) != NumToStr(GetXid()))
44
wxMenu *pgView::GetNewMenu()
46
wxMenu *menu=pgObject::GetNewMenu();
47
if (schema->GetCreatePrivilege())
48
ruleFactory.AppendMenu(menu);
54
bool pgView::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded)
56
wxString sql = wxT("DROP VIEW ") + this->GetSchema()->GetQuotedIdentifier() + wxT(".") + this->GetQuotedIdentifier();
58
sql += wxT(" CASCADE");
59
return GetDatabase()->ExecuteVoid(sql);
63
wxString pgView::GetSql(ctlTree *browser)
67
sql = wxT("-- View: ") + GetQuotedFullIdentifier() + wxT("\n\n")
68
+ wxT("-- DROP VIEW ") + GetQuotedFullIdentifier() + wxT(";")
69
+ wxT("\n\nCREATE OR REPLACE VIEW ") + GetQuotedFullIdentifier() + wxT(" AS \n")
70
+ GetFormattedDefinition()
72
+ GetOwnerSql(7, 3, wxT("TABLE ") + GetQuotedFullIdentifier())
73
+ GetGrant(wxT("arwdRxt"), wxT("TABLE ") + GetQuotedFullIdentifier())
77
pgCollection *columns=browser->FindCollection(columnFactory, GetId());
80
wxString defaults, comments;
81
columns->ShowTreeDetail(browser);
82
treeObjectIterator colIt(browser, columns);
85
while ((column = (pgColumn*)colIt.GetNextObject()) != 0)
87
column->ShowTreeDetail(browser);
88
if (column->GetColNumber() > 0)
90
if (!column->GetDefault().IsEmpty())
92
defaults += wxT("ALTER TABLE ") + GetQuotedFullIdentifier()
93
+ wxT(" ALTER COLUMN ") + column->GetQuotedIdentifier()
94
+ wxT(" SET DEFAULT ") + column->GetDefault()
97
comments += column->GetCommentSql();
100
if (!defaults.IsEmpty())
101
sql += defaults + wxT("\n");
103
if (!comments.IsEmpty())
104
sql += comments + wxT("\n");
111
wxString pgView::GetCols(ctlTree *browser, size_t indent, wxString &QMs, bool withQM)
117
pgSetIterator set(GetConnection(),
118
wxT("SELECT attname\n")
119
wxT(" FROM pg_attribute\n")
120
wxT(" WHERE attrelid=") + GetOidStr() + wxT(" AND attnum>0\n")
121
wxT(" ORDER BY attnum"));
124
while (set.RowsLeft())
131
if (line.Length() > 60)
135
sql += wxT("\n") + wxString(' ', indent);
138
line = wxEmptyString;
139
QMs += wxT("\n") + wxString(' ', indent);
142
line += qtIdent(set.GetVal(0));
151
sql += wxT("\n") + wxString(' ', indent);
158
wxString pgView::GetSelectSql(ctlTree *browser)
162
wxT("SELECT ") + GetCols(browser, 7, qms, false) + wxT("\n")
163
wxT(" FROM ") + GetQuotedFullIdentifier() + wxT(";\n");
168
wxString pgView::GetInsertSql(ctlTree *browser)
172
wxT("INSERT INTO ") + GetQuotedFullIdentifier() + wxT("(\n")
173
wxT(" ") + GetCols(browser, 12, qms, false) + wxT(")\n")
174
wxT(" VALUES (") + qms + wxT(");\n");
179
wxString pgView::GetUpdateSql(ctlTree *browser)
183
wxT("UPDATE ") + GetQuotedFullIdentifier() + wxT("\n")
184
wxT(" SET ") + GetCols(browser, 7, qms, true) + wxT("\n")
185
wxT(" WHERE <condition>;\n");
189
void pgView::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
194
browser->RemoveDummyChild(this);
196
browser->AppendCollection(this, columnFactory);
198
pgCollection *collection = browser->AppendCollection(this, ruleFactory);
199
collection->iSetOid(GetOid());
200
collection->ShowTreeDetail(browser);
201
treeObjectIterator colIt(browser, collection);
204
while (!hasInsertRule && !hasUpdateRule && !hasDeleteRule && (rule=(pgRule*)colIt.GetNextObject()) != 0)
206
if (rule->GetEvent().Find(wxT("INSERT")) >= 0)
207
hasInsertRule = true;
208
if (rule->GetEvent().Find(wxT("UPDATE")) >= 0)
209
hasUpdateRule = true;
210
if (rule->GetEvent().Find(wxT("DELETE")) >= 0)
211
hasDeleteRule = true;
217
CreateListColumns(properties);
218
wxString def=GetDefinition().Left(250);
219
def.Replace(wxT("\n"), wxT(" "));
221
properties->AppendItem(_("Name"), GetName());
222
properties->AppendItem(_("OID"), GetOid());
223
properties->AppendItem(_("Owner"), GetOwner());
224
properties->AppendItem(_("ACL"), GetAcl());
225
properties->AppendItem(_("Definition"), def);
226
properties->AppendItem(_("System view?"), GetSystemObject());
227
properties->AppendItem(_("Comment"), firstLineOnly(GetComment()));
233
pgObject *pgView::Refresh(ctlTree *browser, const wxTreeItemId item)
236
pgCollection *coll=browser->GetParentCollection(item);
239
// OIDs may change in EDB which allows the returned column set to be changed.
240
if (GetConnection()->EdbMinimumVersion(8, 0))
241
view = viewFactory.CreateObjects(coll, 0, wxT("\n AND c.relname=") + GetConnection()->qtDbString(GetName()));
243
view = viewFactory.CreateObjects(coll, 0, wxT("\n AND c.oid=") + GetOidStr());
249
void pgView::ShowHint(frmMain *form, bool force)
252
hints.Add(HINT_OBJECT_EDITING);
253
frmHint::ShowHint((wxWindow *)form, hints, GetFullIdentifier(), force);
256
///////////////////////////////////////////////////////
259
pgObject *pgViewFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restriction)
264
pgSet *views= collection->GetDatabase()->ExecuteSet(
265
wxT("SELECT c.oid, c.xmin, c.relname, pg_get_userbyid(c.relowner) AS viewowner, c.relacl, description, ")
266
wxT("pg_get_viewdef(c.oid") + collection->GetDatabase()->GetPrettyOption() + wxT(") AS definition\n")
267
wxT(" FROM pg_class c\n")
268
wxT(" LEFT OUTER JOIN pg_description des ON (des.objoid=c.oid and des.objsubid=0)\n")
269
wxT(" WHERE ((c.relhasrules AND (EXISTS (\n")
270
wxT(" SELECT r.rulename FROM pg_rewrite r\n")
271
wxT(" WHERE ((r.ev_class = c.oid)\n")
272
wxT(" AND (bpchar(r.ev_type) = '1'::bpchar)) ))) OR (c.relkind = 'v'::char))\n")
273
wxT(" AND relnamespace = ") + collection->GetSchema()->GetOidStr() + wxT("\n")
275
+ wxT(" ORDER BY relname"));
279
while (!views->Eof())
281
view = new pgView(collection->GetSchema(), views->GetVal(wxT("relname")));
283
view->iSetOid(views->GetOid(wxT("oid")));
284
view->iSetXid(views->GetOid(wxT("xmin")));
285
view->iSetOwner(views->GetVal(wxT("viewowner")));
286
view->iSetComment(views->GetVal(wxT("description")));
287
view->iSetAcl(views->GetVal(wxT("relacl")));
288
view->iSetDefinition(views->GetVal(wxT("definition")));
292
collection->AppendBrowserItem(browser, view);
305
#include "images/view.xpm"
306
#include "images/view-sm.xpm"
307
#include "images/views.xpm"
309
pgViewFactory::pgViewFactory()
310
: pgSchemaObjFactory(__("View"), __("New View..."), __("Create a new View."), view_xpm, view_sm_xpm)
316
pgViewFactory viewFactory;
317
static pgaCollectionFactory cf(&viewFactory, __("Views"), views_xpm);