File: gqbDatabase.cpp

package info (click to toggle)
pgadmin3 1.20.0~beta2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 73,704 kB
  • ctags: 18,591
  • sloc: cpp: 193,786; ansic: 18,736; sh: 5,154; pascal: 1,120; yacc: 927; makefile: 516; lex: 421; xml: 126; perl: 40
file content (157 lines) | stat: -rw-r--r-- 5,012 bytes parent folder | download
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
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// gqbDatabase.cpp - Database object for GQB.
//
//////////////////////////////////////////////////////////////////////////

// App headers
#include "pgAdmin3.h"

// wxWindows headers
#include <wx/wx.h>
#include <wx/regex.h>

// App headers
#include "gqb/gqbDatabase.h"
#include "gqb/gqbObject.h"
#include "gqb/gqbSchema.h"
#include "schema/pgSchema.h"

gqbDatabase::gqbDatabase(wxString name, pgConn *connection)
	: gqbObject(name, NULL, connection)
{
	setType(GQB_DATABASE);
}


void gqbDatabase::createObjects(gqbBrowser *_tablesBrowser)
{

	wxString rootNodeString = wxString(conn->GetDbname());
	// Create Root Node
	_tablesBrowser->createRoot(rootNodeString);

	// FillBrowser
	createSchemas(_tablesBrowser, _tablesBrowser->getCatalogRootNode(), GQB_CATALOG, GQB_IMG_CATALOG);
	createSchemas(_tablesBrowser, _tablesBrowser->getTablesRootNode(), GQB_OTHER, GQB_IMG_NAMESPACE);
}


// Use database connection to create all objects inside tree
void gqbDatabase::createSchemas(gqbBrowser *tablesBrowser, wxTreeItemId parentNode, typeSchema MetaType, int indexImage)
{

	// Search Schemas and insert it
	wxString restr =  wxT(" WHERE ");

	if (MetaType != GQB_CATALOG)
	{
		restr += wxT("NOT ");
	}
	restr += wxT("((nspname = 'pg_catalog' AND EXISTS (SELECT 1 FROM pg_class WHERE relname = 'pg_class' AND relnamespace = nsp.oid LIMIT 1)) OR\n");
	restr += wxT("(nspname = 'pgagent' AND EXISTS (SELECT 1 FROM pg_class WHERE relname = 'pga_job' AND relnamespace = nsp.oid LIMIT 1)) OR\n");
	restr += wxT("(nspname = 'information_schema' AND EXISTS (SELECT 1 FROM pg_class WHERE relname = 'tables' AND relnamespace = nsp.oid LIMIT 1)) OR\n");
	restr += wxT("(nspname LIKE '_%' AND EXISTS (SELECT 1 FROM pg_proc WHERE proname='slonyversion' AND pronamespace = nsp.oid LIMIT 1)) OR\n");
	restr += wxT("(nspname = 'dbo' AND EXISTS (SELECT 1 FROM pg_class WHERE relname = 'systables' AND relnamespace = nsp.oid LIMIT 1)) OR\n");
	restr += wxT("(nspname = 'sys' AND EXISTS (SELECT 1 FROM pg_class WHERE relname = 'all_tables' AND relnamespace = nsp.oid LIMIT 1)))\n");

	if (conn->EdbMinimumVersion(8, 2))
	{
		restr += wxT("  AND nsp.nspparent = 0\n");
		// Do not show dbms_job_procedure in schemas
		if (!settings->GetShowSystemObjects())
			restr += wxT("AND NOT (nspname = 'dbms_job_procedure' AND EXISTS(SELECT 1 FROM pg_proc WHERE pronamespace = nsp.oid and proname = 'run_job' LIMIT 1))\n");
	}

	wxString sql;

	if (MetaType == GQB_CATALOG)
	{
		sql = wxT("SELECT 2 AS nsptyp, nspname, nsp.oid")
		      wxT("  FROM pg_namespace nsp\n")
		      + restr +
		      wxT(" ORDER BY 1, nspname");
	}
	else
	{
		if (conn->BackendMinimumVersion(8, 1))
		{
			sql = wxT("SELECT CASE WHEN nspname LIKE E'pg\\\\_temp\\\\_%' THEN 1\n")
			      wxT("            WHEN (nspname LIKE E'pg\\\\_%') THEN 0\n");
		}
		else
		{
			sql = wxT("SELECT CASE WHEN nspname LIKE 'pg\\\\_temp\\\\_%' THEN 1\n")
			      wxT("            WHEN (nspname LIKE 'pg\\\\_%') THEN 0\n");
		}
		sql += wxT("            ELSE 3 END AS nsptyp, nspname, nsp.oid\n")
		       wxT("  FROM pg_namespace nsp\n")
		       + restr +
		       wxT(" ORDER BY 1, nspname");
	}

	pgSet *schemas = conn->ExecuteSet(sql);
	wxTreeItemId parent;

	if (schemas)
	{
		while (!schemas->Eof())
		{
			wxString name = schemas->GetVal(wxT("nspname"));
			long nsptyp = schemas->GetLong(wxT("nsptyp"));

			wxStringTokenizer tokens(settings->GetSystemSchemas(), wxT(","));
			while (tokens.HasMoreTokens())
			{
				wxRegEx regex(tokens.GetNextToken());
				if (regex.Matches(name))
				{
					nsptyp = SCHEMATYP_USERSYS;
					break;
				}
			}

			if (nsptyp <= SCHEMATYP_USERSYS && MetaType != GQB_CATALOG && !settings->GetShowSystemObjects())
			{
				schemas->MoveNext();
				continue;
			}

			int tableImage = GQB_IMG_TABLE, viewImage = GQB_IMG_VIEW;
			gqbSchema *schema;

			if (MetaType == GQB_CATALOG)
			{

				// Create Schema Object
				schema = new gqbSchema(this, name, conn, schemas->GetOid(wxT("oid")));
				parent = tablesBrowser->AppendItem(parentNode, name, indexImage, indexImage, schema);
				schema->createObjects(tablesBrowser, schema->getOid(), parent, GQB_IMG_TABLE, GQB_IMG_VIEW, GQB_IMG_EXTTABLE);

				if(name != wxT("pg_catalog") && name != wxT("pgagent"))
				{
					tableImage = GQB_IMG_CATALOG_OBJ;
					viewImage = GQB_IMG_CATALOG_OBJ;
				}
			}
			else
			{

				// Create Schema Object
				// Note that the schema will be populated when the node is expanded.
				schema = new gqbSchema(this, name, conn, schemas->GetOid(wxT("oid")));
				parent = tablesBrowser->AppendItem(parentNode, name , indexImage, indexImage, schema);
				schema->createObjects(tablesBrowser, schema->getOid(), parent, GQB_IMG_TABLE, GQB_IMG_VIEW, GQB_IMG_EXTTABLE);
			}

			schemas->MoveNext();
		}

		delete schemas;
	}
}