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
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// dlgCast.cpp - PostgreSQL Operator Property
//
//////////////////////////////////////////////////////////////////////////
// wxWindows headers
#include <wx/wx.h>
// App headers
#include "pgAdmin3.h"
#include "utils/misc.h"
#include "utils/pgDefs.h"
#include "dlg/dlgCast.h"
#include "schema/pgCast.h"
#include "schema/pgDatatype.h"
// pointer to controls
#define txtCastname CTRL_TEXT("txtCastname")
#define cbSourceType CTRL_COMBOBOX2("cbSourceType")
#define cbTargetType CTRL_COMBOBOX2("cbTargetType")
#define cbFunction CTRL_COMBOBOX("cbFunction")
#define chkImplicit CTRL_CHECKBOX("chkImplicit")
#define stComment CTRL_STATIC("stComment")
dlgProperty *pgCastFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
return new dlgCast(this, frame, (pgCast *)node);
}
BEGIN_EVENT_TABLE(dlgCast, dlgTypeProperty)
EVT_TEXT(XRCID("cbSourceType"), dlgCast::OnChangeTypeSrc)
EVT_COMBOBOX(XRCID("cbSourceType"), dlgProperty::OnChange)
EVT_TEXT(XRCID("cbTargetType"), dlgCast::OnChangeTypeTrg)
EVT_COMBOBOX(XRCID("cbTargetType"), dlgProperty::OnChange)
END_EVENT_TABLE();
dlgCast::dlgCast(pgaFactory *f, frmMain *frame, pgCast *node)
: dlgTypeProperty(f, frame, wxT("dlgCast"))
{
cast = node;
txtCastname->Disable();
}
pgObject *dlgCast::GetObject()
{
return cast;
}
int dlgCast::Go(bool modal)
{
if (!connection->BackendMinimumVersion(7, 5))
txtComment->Disable();
if (cast)
{
// edit mode
cbSourceType->Append(cast->GetSourceType());
cbSourceType->SetSelection(0);
cbSourceType->Disable();
cbTargetType->Append(wxEmptyString);
cbTargetType->Append(cast->GetTargetType());
cbTargetType->SetSelection(1);
cbTargetType->Disable();
AddType(wxT(" "), cast->GetSourceTypeOid());
AddType(wxT(" "), cast->GetTargetTypeOid());
cbFunction->Append(cast->GetCastFunction());
cbFunction->SetSelection(0);
cbFunction->Disable();
chkImplicit->Disable();
}
else
{
// create mode
FillDatatype(cbSourceType, cbTargetType, false);
}
return dlgProperty::Go(modal);
}
pgObject *dlgCast::CreateObject(pgCollection *collection)
{
pgObject *obj = castFactory.CreateObjects(collection, 0,
wxT(" WHERE castsource = ") + GetTypeOid(cbSourceType->GetGuessedSelection()) +
wxT("\n AND casttarget = ") + GetTypeOid(cbTargetType->GetGuessedSelection()));
return obj;
}
void dlgCast::CheckChange()
{
if (cast)
{
EnableOK(txtComment->GetValue() != cast->GetComment());
}
else
{
bool enable = true;
CheckValid(enable, cbSourceType->GetGuessedSelection() > 0, _("Please specify source datatype."));
CheckValid(enable, cbTargetType->GetGuessedSelection() > 0, _("Please select target datatype."));
if (enable)
txtCastname->SetValue(cbSourceType->GetValue() + wxT(" -> ") + cbTargetType->GetValue());
else
txtCastname->SetValue(wxEmptyString);
EnableOK(enable);
}
}
void dlgCast::OnChangeTypeSrc(wxCommandEvent &ev)
{
cbSourceType->GuessSelection(ev);
OnChangeType(ev);
}
void dlgCast::OnChangeTypeTrg(wxCommandEvent &ev)
{
cbTargetType->GuessSelection(ev);
OnChangeType(ev);
}
void dlgCast::OnChangeType(wxCommandEvent &ev)
{
functions.Clear();
cbFunction->Clear();
if (cbSourceType->GetGuessedSelection() > 0 && cbTargetType->GetGuessedSelection() > 0)
{
functions.Add(wxEmptyString);
cbFunction->Append(wxT(" "));
wxString qry =
wxT("SELECT proname, nspname\n")
wxT(" FROM pg_proc p\n")
wxT(" JOIN pg_namespace n ON n.oid=pronamespace\n")
wxT(" WHERE proargtypes[0] = ")
+ GetTypeOid(cbSourceType->GetGuessedSelection())
+ wxT("\n AND proargtypes[1] = 0")
wxT("\n AND prorettype = ")
+ GetTypeOid(cbTargetType->GetGuessedSelection());
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;
}
}
CheckChange();
}
wxString dlgCast::GetSql()
{
wxString sql;
if (cast)
{
// edit mode
}
else
{
// create mode
sql = wxT("CREATE CAST (") + cbSourceType->GetValue()
+ wxT(" AS ") + cbTargetType->GetValue()
+ wxT(")\n ");
if (cbFunction->GetCurrentSelection() > 0)
sql += wxT("WITH FUNCTION ") + functions.Item(cbFunction->GetCurrentSelection())
+ wxT("(") + cbSourceType->GetValue() + wxT(")");
else
sql += wxT("WITHOUT FUNCTION");
if (chkImplicit->GetValue())
sql += wxT("\n AS IMPLICIT");
sql += wxT(";\n");
}
AppendComment(sql, wxT("CAST (") + cbSourceType->GetValue()
+ wxT(" AS ") + cbTargetType->GetValue() + wxT(")"), cast);
return sql;
}
|