~ubuntu-branches/ubuntu/utopic/pgadmin3/utopic-proposed

« back to all changes in this revision

Viewing changes to src/schema/pgSequence.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Enrici
  • Date: 2004-12-14 23:46:39 UTC
  • Revision ID: james.westby@ubuntu.com-20041214234639-tve0i5l49fq13jli
Tags: upstream-1.2.0
ImportĀ upstreamĀ versionĀ 1.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////////
 
2
//
 
3
// pgAdmin III - PostgreSQL Tools
 
4
// RCS-ID:      $Id: pgSequence.cpp,v 1.28 2004/10/08 13:52:19 andreas Exp $
 
5
// Copyright (C) 2002 - 2004, The pgAdmin Development Team
 
6
// This software is released under the Artistic Licence
 
7
//
 
8
// pgSequence.cpp - Sequence class
 
9
//
 
10
//////////////////////////////////////////////////////////////////////////
 
11
 
 
12
// wxWindows headers
 
13
#include <wx/wx.h>
 
14
 
 
15
// App headers
 
16
#include "pgAdmin3.h"
 
17
#include "misc.h"
 
18
#include "pgObject.h"
 
19
#include "pgSequence.h"
 
20
#include "pgCollection.h"
 
21
 
 
22
 
 
23
pgSequence::pgSequence(pgSchema *newSchema, const wxString& newName)
 
24
: pgSchemaObject(newSchema, PG_SEQUENCE, newName)
 
25
{
 
26
}
 
27
 
 
28
pgSequence::~pgSequence()
 
29
{
 
30
}
 
31
 
 
32
bool pgSequence::DropObject(wxFrame *frame, wxTreeCtrl *browser)
 
33
{
 
34
    return GetDatabase()->ExecuteVoid(wxT("DROP SEQUENCE ") + GetQuotedFullIdentifier() + wxT(";"));
 
35
}
 
36
 
 
37
 
 
38
void pgSequence::UpdateValues()
 
39
{
 
40
    pgSet *sequence=ExecuteSet(
 
41
        wxT("SELECT last_value, min_value, max_value, cache_value, is_cycled, increment_by\n")
 
42
        wxT("  FROM ") + GetQuotedFullIdentifier());
 
43
    if (sequence)
 
44
    {
 
45
        lastValue = sequence->GetLongLong(wxT("last_value"));
 
46
        minValue = sequence->GetLongLong(wxT("min_value"));
 
47
        maxValue = sequence->GetLongLong(wxT("max_value"));
 
48
        cacheValue = sequence->GetLongLong(wxT("cache_value"));
 
49
        increment = sequence->GetLongLong(wxT("increment_by"));
 
50
        cycled = sequence->GetBool(wxT("is_cycled"));
 
51
 
 
52
        delete sequence;
 
53
    }
 
54
}
 
55
 
 
56
 
 
57
wxString pgSequence::GetSql(wxTreeCtrl *browser)
 
58
{
 
59
    if (sql.IsNull())
 
60
    {
 
61
        UpdateValues();
 
62
        sql = wxT("-- Sequence: ") + GetQuotedFullIdentifier() + wxT("\n\n")
 
63
            + wxT("-- DROP SEQUENCE ") + GetQuotedFullIdentifier() + wxT(";")
 
64
            + wxT("\n\nCREATE SEQUENCE ") + GetQuotedFullIdentifier()
 
65
            + wxT("\n  INCREMENT ") + GetIncrement().ToString()
 
66
            + wxT("\n  MINVALUE ") + GetMinValue().ToString()
 
67
            + wxT("\n  MAXVALUE ") + GetMaxValue().ToString()
 
68
            + wxT("\n  START ") + GetLastValue().ToString()
 
69
            + wxT("\n  CACHE ") + GetCacheValue().ToString();
 
70
        if (GetCycled())
 
71
            sql += wxT("\n  CYCLE");
 
72
        AppendIfFilled(sql, wxT("\n  TABLESPACE "), qtIdent(tablespace));
 
73
        sql += wxT(";\n")
 
74
            + GetOwnerSql(7, 3, wxT("TABLE ") + GetQuotedFullIdentifier())
 
75
            + GetGrant(wxT("arwdRxt"), wxT("TABLE ") + GetQuotedFullIdentifier())
 
76
            + GetCommentSql();
 
77
    }
 
78
 
 
79
    return sql;
 
80
}
 
81
 
 
82
void pgSequence::ShowTreeDetail(wxTreeCtrl *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
 
83
{
 
84
    UpdateValues();
 
85
    if (properties)
 
86
    {
 
87
        CreateListColumns(properties);
 
88
 
 
89
        properties->AppendItem(_("Name"), GetName());
 
90
        properties->AppendItem(_("OID"), GetOid());
 
91
        properties->AppendItem(_("Owner"), GetOwner());
 
92
        if (!tablespace.IsEmpty())
 
93
            properties->AppendItem(_("Tablespace"), tablespace);
 
94
        properties->AppendItem(_("ACL"), GetAcl());
 
95
        properties->AppendItem(_("Current value"), GetLastValue());
 
96
        properties->AppendItem(_("Minimum"), GetMinValue());
 
97
        properties->AppendItem(_("Maximum"), GetMaxValue());
 
98
        properties->AppendItem(_("Increment"), GetIncrement());
 
99
        properties->AppendItem(_("Cache"), GetCacheValue());
 
100
        properties->AppendItem(_("Cycled"), GetCycled());
 
101
        properties->AppendItem(_("System sequence?"), GetSystemObject());
 
102
        properties->AppendItem(_("Comment"), GetComment());
 
103
    }
 
104
}
 
105
 
 
106
 
 
107
 
 
108
void pgSequence::ShowStatistics(frmMain *form, ctlListView *statistics)
 
109
{
 
110
    wxLogInfo(wxT("Displaying statistics for sequence on ") +GetSchema()->GetIdentifier());
 
111
 
 
112
    // Add the statistics view columns
 
113
    CreateListColumns(statistics, _("Statistic"), _("Value"));
 
114
 
 
115
    pgSet *stats = GetSchema()->GetDatabase()->ExecuteSet(wxT(
 
116
        "SELECT blks_read, blks_hit FROM pg_statio_all_sequences WHERE relid = ") + GetOidStr());
 
117
 
 
118
    if (stats)
 
119
    {
 
120
        statistics->InsertItem(0, wxT("Blocks Read"), PGICON_STATISTICS);
 
121
        statistics->SetItem(0l, 1, stats->GetVal(wxT("blks_read")));
 
122
        statistics->InsertItem(1, wxT("Blocks Hit"), PGICON_STATISTICS);
 
123
        statistics->SetItem(1, 1, stats->GetVal(wxT("blks_hit")));
 
124
 
 
125
        delete stats;
 
126
    }
 
127
}
 
128
 
 
129
 
 
130
pgObject *pgSequence::Refresh(wxTreeCtrl *browser, const wxTreeItemId item)
 
131
{
 
132
    pgObject *sequence=0;
 
133
    wxTreeItemId parentItem=browser->GetItemParent(item);
 
134
    if (parentItem)
 
135
    {
 
136
        pgObject *obj=(pgObject*)browser->GetItemData(parentItem);
 
137
        if (obj->GetType() == PG_SEQUENCES)
 
138
            sequence = ReadObjects((pgCollection*)obj, 0, wxT("\n   AND cl.oid=") + GetOidStr());
 
139
    }
 
140
    return sequence;
 
141
}
 
142
 
 
143
 
 
144
 
 
145
pgObject *pgSequence::ReadObjects(pgCollection *collection, wxTreeCtrl *browser, const wxString &restriction)
 
146
{
 
147
    pgSequence *sequence=0;
 
148
 
 
149
    pgSet *sequences;
 
150
    if (collection->GetConnection()->BackendMinimumVersion(7, 5))
 
151
    {
 
152
        sequences = collection->GetDatabase()->ExecuteSet(
 
153
            wxT("SELECT cl.oid, relname, spcname, pg_get_userbyid(relowner) AS seqowner, relacl, description\n")
 
154
            wxT("  FROM pg_class cl\n")
 
155
            wxT("  LEFT OUTER JOIN pg_tablespace ta on ta.oid=cl.reltablespace\n")
 
156
            wxT("  LEFT OUTER JOIN pg_description des ON des.objoid=cl.oid\n")
 
157
            wxT(" WHERE relkind = 'S' AND relnamespace  = ") + collection->GetSchema()->GetOidStr()
 
158
            + restriction + wxT("\n")
 
159
            wxT(" ORDER BY relname"));
 
160
    }
 
161
    else
 
162
    {
 
163
        sequences = collection->GetDatabase()->ExecuteSet(
 
164
            wxT("SELECT cl.oid, relname, pg_get_userbyid(relowner) AS seqowner, relacl, description\n")
 
165
            wxT("  FROM pg_class cl\n")
 
166
            wxT("  LEFT OUTER JOIN pg_description des ON des.objoid=cl.oid\n")
 
167
            wxT(" WHERE relkind = 'S' AND relnamespace  = ") + collection->GetSchema()->GetOidStr()
 
168
            + restriction + wxT("\n")
 
169
            wxT(" ORDER BY relname"));
 
170
    }
 
171
    if (sequences)
 
172
    {
 
173
        while (!sequences->Eof())
 
174
        {
 
175
            sequence = new pgSequence(collection->GetSchema(), 
 
176
                                            sequences->GetVal(wxT("relname")));
 
177
 
 
178
            sequence->iSetOid(sequences->GetOid(wxT("oid")));
 
179
            sequence->iSetComment(sequences->GetVal(wxT("description")));
 
180
            sequence->iSetOwner(sequences->GetVal(wxT("seqowner")));
 
181
            sequence->iSetAcl(sequences->GetVal(wxT("relacl")));
 
182
            if (collection->GetConnection()->BackendMinimumVersion(7, 5))
 
183
                sequence->iSetTablespace(sequences->GetVal(wxT("spcname")));
 
184
 
 
185
            if (browser)
 
186
            {
 
187
                collection->AppendBrowserItem(browser, sequence);
 
188
                                sequences->MoveNext();
 
189
            }
 
190
            else
 
191
                break;
 
192
        }
 
193
                delete sequences;
 
194
    }
 
195
    return sequence;
 
196
}