File: dlgConversion.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 (213 lines) | stat: -rw-r--r-- 6,116 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
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
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// dlgConversion.cpp - PostgreSQL Conversion Property
//
//////////////////////////////////////////////////////////////////////////

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

// App headers
#include "pgAdmin3.h"
#include "utils/misc.h"
#include "utils/pgDefs.h"

#include "dlg/dlgConversion.h"
#include "schema/pgConversion.h"
#include "schema/pgSchema.h"


// pointer to controls
#define cbSourceEncoding    CTRL_COMBOBOX("cbSourceEncoding")
#define cbTargetEncoding    CTRL_COMBOBOX("cbTargetEncoding")
#define cbFunction          CTRL_COMBOBOX("cbFunction")
#define chkDefault          CTRL_CHECKBOX("chkDefault")



BEGIN_EVENT_TABLE(dlgConversion, dlgProperty)
	EVT_TEXT(XRCID("cbSourceEncoding"),     dlgProperty::OnChange)
	EVT_COMBOBOX(XRCID("cbSourceEncoding"), dlgProperty::OnChange)
	EVT_TEXT(XRCID("cbTargetEncoding"),     dlgProperty::OnChange)
	EVT_COMBOBOX(XRCID("cbTargetEncoding"), dlgProperty::OnChange)
	EVT_TEXT(XRCID("cbFunction"),           dlgProperty::OnChange)
	EVT_COMBOBOX(XRCID("cbFunction"),       dlgProperty::OnChange)
END_EVENT_TABLE();


dlgProperty *pgConversionFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
	return new dlgConversion(this, frame, (pgConversion *)node, (pgSchema *)parent);
}



dlgConversion::dlgConversion(pgaFactory *f, frmMain *frame, pgConversion *node, pgSchema *sch)
	: dlgProperty(f, frame, wxT("dlgConversion"))
{
	conversion = node;
	schema = sch;
}


pgObject *dlgConversion::GetObject()
{
	return conversion;
}


int dlgConversion::Go(bool modal)
{
	if (!connection->BackendMinimumVersion(7, 4))
		txtComment->Disable();

	if (!connection->BackendMinimumVersion(7, 5))
		cbOwner->Disable();

	if (conversion)
	{
		// edit mode
		cbSchema->Enable(connection->BackendMinimumVersion(9, 1));
		cbSourceEncoding->Append(conversion->GetForEncoding());
		cbSourceEncoding->SetSelection(0);
		cbTargetEncoding->Append(conversion->GetToEncoding());
		cbTargetEncoding->SetSelection(0);

		cbFunction->Append(database->GetSchemaPrefix(conversion->GetProcNamespace()) + conversion->GetProc());
		cbFunction->SetSelection(0);

		if (!connection->BackendMinimumVersion(7, 4))
			txtName->Disable();
		chkDefault->SetValue(conversion->GetDefaultConversion());
		cbSourceEncoding->Disable();
		cbTargetEncoding->Disable();
		cbFunction->Disable();
		chkDefault->Disable();
	}
	else
	{
		// create mode

		wxString qry =
		    wxT("SELECT proname, nspname\n")
		    wxT("  FROM pg_proc p\n")
		    wxT("  JOIN pg_namespace n ON n.oid=pronamespace")
		    wxT("\n WHERE prorettype = ") + NumToStr(PGOID_TYPE_VOID) +
		    wxT("\n   AND proargtypes[0] = ") + NumToStr(PGOID_TYPE_INT4) +
		    wxT("\n   AND proargtypes[1] = ") + NumToStr(PGOID_TYPE_INT4) +
		    wxT("\n   AND proargtypes[2] = ") + NumToStr(PGOID_TYPE_CSTRING) +
		    wxT("\n   AND proargtypes[3] = ") + NumToStr(PGOID_TYPE_CSTRING) +
		    wxT("\n   AND proargtypes[4] = ") + NumToStr(PGOID_TYPE_INT4) +
		    wxT("\n   AND proargtypes[5] = 0");

		pgSet *set = connection->ExecuteSet(qry);
		if (set)
		{
			while (!set->Eof())
			{
				functions.Add(database->GetQuotedSchemaPrefix(set->GetVal(wxT("nspname"))) + qtIdent(set->GetVal(wxT("proname"))));
				cbFunction->Append(database->GetSchemaPrefix(set->GetVal(wxT("nspname"))) + set->GetVal(wxT("proname")));

				set->MoveNext();
			}
			delete set;
		}

		long encNo = 0;
		wxString encStr;
		do
		{
			encStr = connection->ExecuteScalar(
			             wxT("SELECT pg_encoding_to_char(") + NumToStr(encNo) + wxT(")"));
			if (!encStr.IsEmpty())
			{
				cbSourceEncoding->Append(encStr);
				cbTargetEncoding->Append(encStr);
			}
			encNo++;
		}
		while (!encStr.IsEmpty());
	}

	return dlgProperty::Go(modal);
}


pgObject *dlgConversion::CreateObject(pgCollection *collection)
{
	pgObject *obj = conversionFactory.CreateObjects(collection, 0,
	                wxT("\n AND conname = ") + qtDbString(GetName()));

	return obj;
}


void dlgConversion::CheckChange()
{
	if (conversion)
	{
		EnableOK(txtName->GetValue() != conversion->GetName()
		         || cbSchema->GetValue() != conversion->GetSchema()->GetName()
		         || txtComment->GetValue() != conversion->GetComment()
		         || cbOwner->GetValue() != conversion->GetOwner());
	}
	else
	{
		bool enable = true;
		CheckValid(enable, !GetName().IsEmpty(), _("Please specify name."));
		CheckValid(enable, !cbSourceEncoding->GetValue().IsEmpty(), _("Please specify source encoding."));
		CheckValid(enable, !cbTargetEncoding->GetValue().IsEmpty(), _("Please specify target encoding."));
		CheckValid(enable, cbFunction->GetCurrentSelection() >= 0, _("Please specify conversion function."));

		EnableOK(enable);
	}
}




wxString dlgConversion::GetSql()
{
	wxString sql;
	wxString name;

	if (conversion)
	{
		// edit mode
		name = GetName();

		AppendNameChange(sql);
		AppendOwnerChange(sql, wxT("CONVERSION ") + schema->GetQuotedPrefix() + qtIdent(name));

		if (cbSchema->GetValue() != conversion->GetSchema()->GetName())
		{
			sql += wxT("ALTER CONVERSION ") + qtIdent(conversion->GetSchema()->GetName()) + wxT(".") + qtIdent(name)
			       +  wxT("\n  SET SCHEMA ") + qtIdent(cbSchema->GetValue())
			       +  wxT(";\n");
		}
	}
	else
	{
		name = qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName());

		// create mode
		sql = wxT("CREATE ");
		if (chkDefault->GetValue())
			sql += wxT("DEFAULT ");
		sql += wxT("CONVERSION ") + name
		       + wxT("\n   FOR ") + qtDbString(cbSourceEncoding->GetValue())
		       + wxT(" TO ") + qtDbString(cbTargetEncoding->GetValue())
		       + wxT("\n   FROM ") + functions.Item(cbFunction->GetCurrentSelection())
		       + wxT(";\n");

		AppendOwnerNew(sql, wxT("CONVERSION ") + schema->GetQuotedPrefix() + qtIdent(name));
	}
	AppendComment(sql, wxT("CONVERSION ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()), conversion);

	return sql;
}