~ubuntu-branches/debian/squeeze/pgadmin3/squeeze

« back to all changes in this revision

Viewing changes to pgadmin/schema/pgView.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Porcheron
  • Date: 2008-02-07 00:56:22 UTC
  • mto: (2.1.6 hardy) (6.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20080207005622-c2ail8p4d0sk3dnw
Tags: upstream-1.8.2
ImportĀ upstreamĀ versionĀ 1.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////////
 
2
//
 
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
 
7
//
 
8
// pgView.cpp - View class
 
9
//
 
10
//////////////////////////////////////////////////////////////////////////
 
11
 
 
12
// wxWindows headers
 
13
#include <wx/wx.h>
 
14
 
 
15
// App headers
 
16
#include "pgAdmin3.h"
 
17
#include "utils/misc.h"
 
18
#include "schema/pgColumn.h"
 
19
#include "schema/pgView.h"
 
20
#include "frm/frmHint.h"
 
21
 
 
22
 
 
23
pgView::pgView(pgSchema *newSchema, const wxString& newName)
 
24
: pgRuleObject(newSchema, viewFactory, newName)
 
25
{
 
26
        hasInsertRule=false;
 
27
        hasUpdateRule=false;
 
28
        hasDeleteRule=false;
 
29
}
 
30
 
 
31
pgView::~pgView()
 
32
{
 
33
}
 
34
 
 
35
bool pgView::IsUpToDate()
 
36
{
 
37
    wxString sql = wxT("SELECT xmin FROM pg_class WHERE oid = ") + this->GetOidStr();
 
38
        if (!this->GetDatabase()->GetConnection() || this->GetDatabase()->ExecuteScalar(sql) != NumToStr(GetXid()))
 
39
                return false;
 
40
        else
 
41
                return true;
 
42
}
 
43
 
 
44
wxMenu *pgView::GetNewMenu()
 
45
{
 
46
    wxMenu *menu=pgObject::GetNewMenu();
 
47
    if (schema->GetCreatePrivilege())
 
48
        ruleFactory.AppendMenu(menu);
 
49
 
 
50
    return menu;
 
51
}
 
52
 
 
53
 
 
54
bool pgView::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded)
 
55
{
 
56
    wxString sql = wxT("DROP VIEW ") + this->GetSchema()->GetQuotedIdentifier() + wxT(".") + this->GetQuotedIdentifier();
 
57
    if (cascaded)
 
58
        sql += wxT(" CASCADE");
 
59
    return GetDatabase()->ExecuteVoid(sql);
 
60
}
 
61
 
 
62
 
 
63
wxString pgView::GetSql(ctlTree *browser)
 
64
{
 
65
    if (sql.IsNull())
 
66
    {
 
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()
 
71
            + wxT("\n\n") 
 
72
            + GetOwnerSql(7, 3, wxT("TABLE ") + GetQuotedFullIdentifier())
 
73
            + GetGrant(wxT("arwdRxt"), wxT("TABLE ") + GetQuotedFullIdentifier())
 
74
            + GetCommentSql()
 
75
            + wxT("\n");
 
76
 
 
77
        pgCollection *columns=browser->FindCollection(columnFactory, GetId());
 
78
        if (columns)
 
79
        {
 
80
            wxString defaults, comments;
 
81
            columns->ShowTreeDetail(browser);
 
82
            treeObjectIterator colIt(browser, columns);
 
83
 
 
84
            pgColumn *column;
 
85
            while ((column = (pgColumn*)colIt.GetNextObject()) != 0)
 
86
            {
 
87
                column->ShowTreeDetail(browser);
 
88
                if (column->GetColNumber() > 0)
 
89
                {
 
90
                    if (!column->GetDefault().IsEmpty())
 
91
                    {
 
92
                        defaults += wxT("ALTER TABLE ") + GetQuotedFullIdentifier()
 
93
                                 +  wxT(" ALTER COLUMN ") + column->GetQuotedIdentifier()
 
94
                                 +  wxT(" SET DEFAULT ") + column->GetDefault()
 
95
                                 + wxT(";\n");
 
96
                    }
 
97
                    comments += column->GetCommentSql();
 
98
                }
 
99
            }
 
100
            if (!defaults.IsEmpty())
 
101
                sql += defaults + wxT("\n");
 
102
 
 
103
            if (!comments.IsEmpty())
 
104
                sql += comments + wxT("\n");
 
105
        }
 
106
    }
 
107
    return sql;
 
108
}
 
109
 
 
110
 
 
111
wxString pgView::GetCols(ctlTree *browser, size_t indent, wxString &QMs, bool withQM)
 
112
{
 
113
        wxString sql;
 
114
        wxString line;
 
115
        
 
116
        int colcount=0;
 
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"));
 
122
 
 
123
 
 
124
    while (set.RowsLeft())
 
125
    {
 
126
        if (colcount++)
 
127
                {
 
128
                        line += wxT(", ");
 
129
                        QMs += wxT(", ");
 
130
                }
 
131
                if (line.Length() > 60)
 
132
                {
 
133
                        if (!sql.IsEmpty())
 
134
                        {
 
135
                                sql += wxT("\n") + wxString(' ', indent);
 
136
                        }
 
137
                        sql += line;
 
138
                        line = wxEmptyString;
 
139
                        QMs += wxT("\n") + wxString(' ', indent);
 
140
                }
 
141
 
 
142
                line += qtIdent(set.GetVal(0));
 
143
                if (withQM)
 
144
                        line += wxT("=?");
 
145
                QMs += wxT("?");
 
146
    }
 
147
 
 
148
        if (!line.IsEmpty())
 
149
        {
 
150
                if (!sql.IsEmpty())
 
151
                        sql += wxT("\n") + wxString(' ', indent);
 
152
                sql += line;
 
153
        }
 
154
        return sql;
 
155
}
 
156
 
 
157
 
 
158
wxString pgView::GetSelectSql(ctlTree *browser)
 
159
{
 
160
        wxString qms;
 
161
        wxString sql=
 
162
                wxT("SELECT ") + GetCols(browser, 7, qms, false) + wxT("\n")
 
163
                wxT("  FROM ") + GetQuotedFullIdentifier() + wxT(";\n");
 
164
        return sql;
 
165
}
 
166
 
 
167
 
 
168
wxString pgView::GetInsertSql(ctlTree *browser)
 
169
{
 
170
        wxString qms;
 
171
        wxString sql = 
 
172
                wxT("INSERT INTO ") + GetQuotedFullIdentifier() + wxT("(\n")
 
173
                wxT("            ") + GetCols(browser, 12, qms, false) + wxT(")\n")
 
174
                wxT("    VALUES (") + qms + wxT(");\n");
 
175
        return sql;
 
176
}
 
177
 
 
178
 
 
179
wxString pgView::GetUpdateSql(ctlTree *browser)
 
180
{
 
181
        wxString qms;
 
182
        wxString sql = 
 
183
                wxT("UPDATE ") + GetQuotedFullIdentifier() + wxT("\n")
 
184
                wxT("   SET ") + GetCols(browser, 7, qms, true) + wxT("\n")
 
185
                wxT(" WHERE <condition>;\n");
 
186
        return sql;
 
187
}
 
188
 
 
189
void pgView::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
 
190
{
 
191
    if (!expandedKids)
 
192
    {
 
193
        expandedKids = true;
 
194
        browser->RemoveDummyChild(this);
 
195
        
 
196
        browser->AppendCollection(this, columnFactory);
 
197
 
 
198
        pgCollection *collection = browser->AppendCollection(this, ruleFactory);
 
199
        collection->iSetOid(GetOid());
 
200
                collection->ShowTreeDetail(browser);
 
201
        treeObjectIterator colIt(browser, collection);
 
202
 
 
203
                pgRule *rule;
 
204
                while (!hasInsertRule && !hasUpdateRule && !hasDeleteRule && (rule=(pgRule*)colIt.GetNextObject()) != 0)
 
205
                {
 
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;
 
212
                }
 
213
 
 
214
    }
 
215
    if (properties)
 
216
    {
 
217
        CreateListColumns(properties);
 
218
        wxString def=GetDefinition().Left(250);
 
219
        def.Replace(wxT("\n"), wxT(" "));
 
220
 
 
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()));
 
228
    }
 
229
}
 
230
 
 
231
 
 
232
 
 
233
pgObject *pgView::Refresh(ctlTree *browser, const wxTreeItemId item)
 
234
{
 
235
    pgObject *view=0;
 
236
    pgCollection *coll=browser->GetParentCollection(item);
 
237
    if (coll)
 
238
    {
 
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()));
 
242
        else
 
243
            view = viewFactory.CreateObjects(coll, 0, wxT("\n   AND c.oid=") + GetOidStr());
 
244
    }
 
245
 
 
246
    return view;
 
247
}
 
248
 
 
249
void pgView::ShowHint(frmMain *form, bool force)
 
250
{
 
251
    wxArrayString hints;
 
252
    hints.Add(HINT_OBJECT_EDITING);
 
253
    frmHint::ShowHint((wxWindow *)form, hints, GetFullIdentifier(), force);
 
254
}
 
255
 
 
256
///////////////////////////////////////////////////////
 
257
 
 
258
 
 
259
pgObject *pgViewFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restriction)
 
260
{
 
261
    pgView *view=0;
 
262
 
 
263
 
 
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")
 
274
        + restriction
 
275
        + wxT(" ORDER BY relname"));
 
276
 
 
277
    if (views)
 
278
    {
 
279
        while (!views->Eof())
 
280
        {
 
281
            view = new pgView(collection->GetSchema(), views->GetVal(wxT("relname")));
 
282
 
 
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")));
 
289
 
 
290
            if (browser)
 
291
            {
 
292
                collection->AppendBrowserItem(browser, view);
 
293
                        views->MoveNext();
 
294
            }
 
295
            else
 
296
                break;
 
297
        }
 
298
 
 
299
                delete views;
 
300
    }
 
301
    return view;
 
302
}
 
303
 
 
304
 
 
305
#include "images/view.xpm"
 
306
#include "images/view-sm.xpm"
 
307
#include "images/views.xpm"
 
308
 
 
309
pgViewFactory::pgViewFactory() 
 
310
: pgSchemaObjFactory(__("View"), __("New View..."), __("Create a new View."), view_xpm, view_sm_xpm)
 
311
{
 
312
    metaType = PGM_VIEW;
 
313
}
 
314
 
 
315
 
 
316
pgViewFactory viewFactory;
 
317
static pgaCollectionFactory cf(&viewFactory, __("Views"), views_xpm);