18
18
#include "schema/edbPackageFunction.h"
21
edbPackageFunction::edbPackageFunction(edbPackage *newPackage, const wxString& newName)
22
: edbPackageObject(newPackage, packageFunctionFactory, newName)
26
edbPackageFunction::edbPackageFunction(edbPackage *newPackage,pgaFactory &factory, const wxString& newName)
27
: edbPackageObject(newPackage, factory, newName)
31
edbPackageProcedure::edbPackageProcedure(edbPackage *newPackage, const wxString& newName)
32
: edbPackageFunction(newPackage, packageProcedureFactory, newName)
21
edbPackageFunction::edbPackageFunction(edbPackage *newPackage, const wxString &newName)
22
: edbPackageObject(newPackage, packageFunctionFactory, newName)
26
edbPackageFunction::edbPackageFunction(edbPackage *newPackage, pgaFactory &factory, const wxString &newName)
27
: edbPackageObject(newPackage, factory, newName)
31
edbPackageProcedure::edbPackageProcedure(edbPackage *newPackage, const wxString &newName)
32
: edbPackageFunction(newPackage, packageProcedureFactory, newName)
36
36
wxString edbPackageFunction::GetFullName()
38
return GetName() + wxT("(") + GetArgSigList() + wxT(")");
38
return GetName() + wxT("(") + GetArgSigList() + wxT(")");
41
41
wxString edbPackageProcedure::GetFullName()
43
if (GetArgSigList().IsEmpty())
46
return GetName() + wxT("(") + GetArgSigList() + wxT(")");
43
if (GetArgSigList().IsEmpty())
46
return GetName() + wxT("(") + GetArgSigList() + wxT(")");
49
49
wxString edbPackageFunction::GetArgListWithNames()
53
for (unsigned int i=0; i < argTypesArray.Count(); i++)
62
if (!argNamesArray.Item(i).IsEmpty())
63
arg += qtIdent(argNamesArray.Item(i));
65
if (!argModesArray.Item(i).IsEmpty())
68
arg += argModesArray.Item(i);
70
arg += wxT(" ") + argModesArray.Item(i);
75
if (!argModesArray.Item(i).IsEmpty())
76
arg += argModesArray.Item(i);
78
if (!argNamesArray.Item(i).IsEmpty())
81
arg += qtIdent(argNamesArray.Item(i));
83
arg += wxT(" ") + qtIdent(argNamesArray.Item(i));
88
arg += wxT(" ") + argTypesArray.Item(i);
90
arg += argTypesArray.Item(i);
92
// Parameter default value
93
if (GetConnection()->HasFeature(FEATURE_FUNCTION_DEFAULTS) || GetConnection()->BackendMinimumVersion(8, 4))
95
if (!argModesArray.Item(i).IsSameAs(wxT("OUT"), false) && !argDefsArray.Item(i).IsEmpty())
96
arg += wxT(" DEFAULT ") + argDefsArray.Item(i);
53
for (unsigned int i = 0; i < argTypesArray.Count(); i++)
62
if (!argNamesArray.Item(i).IsEmpty())
63
arg += qtIdent(argNamesArray.Item(i));
65
if (!argModesArray.Item(i).IsEmpty())
68
arg += argModesArray.Item(i);
70
arg += wxT(" ") + argModesArray.Item(i);
75
if (!argModesArray.Item(i).IsEmpty())
76
arg += argModesArray.Item(i);
78
if (!argNamesArray.Item(i).IsEmpty())
81
arg += qtIdent(argNamesArray.Item(i));
83
arg += wxT(" ") + qtIdent(argNamesArray.Item(i));
88
arg += wxT(" ") + argTypesArray.Item(i);
90
arg += argTypesArray.Item(i);
92
// Parameter default value
93
if (GetConnection()->HasFeature(FEATURE_FUNCTION_DEFAULTS) || GetConnection()->BackendMinimumVersion(8, 4))
95
if (!argModesArray.Item(i).IsSameAs(wxT("OUT"), false) && !argDefsArray.Item(i).IsEmpty())
96
arg += wxT(" DEFAULT ") + argDefsArray.Item(i);
104
104
wxString edbPackageFunction::GetArgSigList()
108
for (unsigned int i=0; i < argTypesArray.Count(); i++)
110
// OUT parameters are not considered part of the signature
111
if (argModesArray.Item(i) != wxT("OUT"))
116
args += argTypesArray.Item(i);
108
for (unsigned int i = 0; i < argTypesArray.Count(); i++)
110
// OUT parameters are not considered part of the signature
111
if (argModesArray.Item(i) != wxT("OUT"))
116
args += argTypesArray.Item(i);
122
122
wxString edbPackageFunction::GetSql(ctlTree *browser)
126
sql = wxT("-- Package Function: ") + GetName() + wxT("\n\n");
127
sql += GetSource() + wxT("\n\n");
126
sql = wxT("-- Package Function: ") + GetName() + wxT("\n\n");
127
sql += GetSource() + wxT("\n\n");
133
133
wxString edbPackageProcedure::GetSql(ctlTree *browser)
137
sql = wxT("-- Package Procedure: ") + GetName() + wxT("\n\n");
138
sql += GetSource() + wxT("\n\n");
137
sql = wxT("-- Package Procedure: ") + GetName() + wxT("\n\n");
138
sql += GetSource() + wxT("\n\n");
144
144
void edbPackageFunction::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
148
CreateListColumns(properties);
148
CreateListColumns(properties);
150
properties->AppendItem(_("Name"), GetName());
151
properties->AppendItem(_("OID"), GetOid());
152
properties->AppendItem(_("Argument count"), GetArgCount());
153
properties->AppendItem(_("Arguments"), GetArgListWithNames());
154
properties->AppendItem(_("Signature arguments"), GetArgSigList());
155
if (!GetIsProcedure())
156
properties->AppendItem(_("Return type"), GetReturnType());
157
properties->AppendItem(_("Visibility"), GetVisibility());
158
properties->AppendItem(_("Source"), firstLineOnly(GetSource()));
150
properties->AppendItem(_("Name"), GetName());
151
properties->AppendItem(_("OID"), GetOid());
152
properties->AppendItem(_("Argument count"), GetArgCount());
153
properties->AppendItem(_("Arguments"), GetArgListWithNames());
154
properties->AppendItem(_("Signature arguments"), GetArgSigList());
155
if (!GetIsProcedure())
156
properties->AppendItem(_("Return type"), GetReturnType());
157
properties->AppendItem(_("Visibility"), GetVisibility());
158
properties->AppendItem(_("Source"), firstLineOnly(GetSource()));
164
164
pgObject *edbPackageFunction::Refresh(ctlTree *browser, const wxTreeItemId item)
166
pgObject *packageFunction=0;
167
pgCollection *coll=browser->GetParentCollection(item);
170
if (coll->GetConnection()->EdbMinimumVersion(8, 2))
171
packageFunction = packageFunctionFactory.CreateObjects(coll, 0, wxT("\n AND pronamespace=") + GetPackage()->GetOidStr() + wxT(" AND proname='") + GetName() + wxT("'"));
173
packageFunction = packageFunctionFactory.CreateObjects(coll, 0, wxT("\n AND packageoid=") + GetPackage()->GetOidStr() + wxT(" AND eltname='") + GetName() + wxT("'"));
166
pgObject *packageFunction = 0;
167
pgCollection *coll = browser->GetParentCollection(item);
170
if (coll->GetConnection()->EdbMinimumVersion(8, 2))
171
packageFunction = packageFunctionFactory.CreateObjects(coll, 0, wxT("\n AND pronamespace=") + GetPackage()->GetOidStr() + wxT(" AND proname='") + GetName() + wxT("'"));
173
packageFunction = packageFunctionFactory.CreateObjects(coll, 0, wxT("\n AND packageoid=") + GetPackage()->GetOidStr() + wxT(" AND eltname='") + GetName() + wxT("'"));
176
return packageFunction;
176
return packageFunction;
182
182
edbPackageFunction *edbPackageFunctionFactory::AppendFunctions(pgObject *obj, edbPackage *package, ctlTree *browser, const wxString &restriction)
184
edbPackageFunction *packageFunction=0;
184
edbPackageFunction *packageFunction = 0;
185
185
pgSet *packageFunctions;
188
cacheMap typeCache, exprCache;
189
wxString sql, argDefsCol;
191
if (obj->GetConnection()->HasFeature(FEATURE_FUNCTION_DEFAULTS))
192
argDefsCol = wxT("proargdefvals, ");
194
if (obj->GetConnection()->EdbMinimumVersion(8, 2))
188
cacheMap typeCache, exprCache;
189
wxString sql, argDefsCol;
191
if (obj->GetConnection()->HasFeature(FEATURE_FUNCTION_DEFAULTS))
192
argDefsCol = wxT("proargdefvals, ");
194
if (obj->GetConnection()->EdbMinimumVersion(8, 2))
196
196
sql = wxT("SELECT pg_proc.oid, proname AS eltname, prorettype AS eltdatatype, pronargs AS nargs, proaccess AS visibility,\n")
197
wxT(" proallargtypes AS allargtypes, proargtypes AS argtypes, proargnames AS argnames, proargmodes AS argmodes,") + argDefsCol + wxT("\n")
198
wxT(" CASE WHEN format_type(prorettype, NULL) = 'void' THEN 'P' ELSE 'F' END AS eltclass\n")
199
wxT(" FROM pg_proc, pg_namespace\n")
200
+ restriction + wxT("\n")
201
wxT(" AND pg_proc.pronamespace = pg_namespace.oid\n")
202
wxT(" ORDER BY eltname");
197
wxT(" proallargtypes AS allargtypes, proargtypes AS argtypes, proargnames AS argnames, proargmodes AS argmodes,") + argDefsCol + wxT("\n")
198
wxT(" CASE WHEN format_type(prorettype, NULL) = 'void' THEN 'P' ELSE 'F' END AS eltclass\n")
199
wxT(" FROM pg_proc, pg_namespace\n")
200
+ restriction + wxT("\n")
201
wxT(" AND pg_proc.pronamespace = pg_namespace.oid\n")
202
wxT(" ORDER BY eltname");
206
206
sql = wxT("SELECT oid, eltname, eltdatatype, eltclass, nargs, visibility,\n")
207
wxT(" allargtypes, argtypes, argnames, argmodes\n")
208
wxT(" FROM edb_pkgelements\n")
207
wxT(" allargtypes, argtypes, argnames, argmodes\n")
208
wxT(" FROM edb_pkgelements\n")
209
209
+ restriction + wxT("\n")
210
210
wxT(" ORDER BY eltname");
213
packageFunctions = obj->GetDatabase()->ExecuteSet(sql);
215
pgSet *types = obj->GetDatabase()->ExecuteSet(wxT(
216
"SELECT oid, format_type(oid, NULL) AS typname FROM pg_type"));
220
typeCache[types->GetVal(wxT("oid"))] = types->GetVal(wxT("typname"));
224
if (packageFunctions)
226
while (!packageFunctions->Eof())
228
if (packageFunctions->GetVal(wxT("eltclass")) == wxT("F"))
229
packageFunction = new edbPackageFunction(package, packageFunctions->GetVal(wxT("eltname")));
231
packageFunction = new edbPackageProcedure(package, packageFunctions->GetVal(wxT("eltname")));
233
// Tokenize the arguments
234
wxStringTokenizer argTypesTkz(wxEmptyString), argModesTkz(wxEmptyString);
235
queryTokenizer argNamesTkz(wxEmptyString, (wxChar)','), argDefsTkz(wxEmptyString, (wxChar)',');
239
tmp = packageFunctions->GetVal(wxT("allargtypes"));
241
argTypesTkz.SetString(tmp.Mid(1, tmp.Length()-2), wxT(","));
244
tmp = packageFunctions->GetVal(wxT("argtypes"));
246
argTypesTkz.SetString(tmp);
250
tmp = packageFunctions->GetVal(wxT("argnames"));
252
argNamesTkz.SetString(tmp.Mid(1, tmp.Length()-2), wxT(","));
255
tmp = packageFunctions->GetVal(wxT("argmodes"));
257
argModesTkz.SetString(tmp.Mid(1, tmp.Length()-2), wxT(","));
260
if (obj->GetConnection()->HasFeature(FEATURE_FUNCTION_DEFAULTS))
262
tmp = packageFunctions->GetVal(wxT("proargdefvals"));
264
argDefsTkz.SetString(tmp.Mid(1, tmp.Length()-2), wxT(","));
267
// Now iterate the arguments and build the arrays
268
wxString type, name, mode, def;
270
while (argTypesTkz.HasMoreTokens())
272
// Add the arg type. This is a type oid, so
273
// look it up in the hashmap
274
type = argTypesTkz.GetNextToken();
275
packageFunction->iAddArgType(typeCache[type]);
277
// Now add the name, stripping the quotes if
279
name = argNamesTkz.GetNextToken();
283
name = name.Mid(1, name.Length()-2);
284
packageFunction->iAddArgName(name);
287
packageFunction->iAddArgName(wxEmptyString);
290
mode = argModesTkz.GetNextToken();
293
if (mode == wxT('o') || mode == wxT("2"))
295
else if (mode == wxT("b"))
296
if (packageFunctions->GetVal(wxT("eltclass")) == wxT("F"))
297
mode = wxT("IN OUT");
300
else if (mode == wxT("3"))
301
mode = wxT("IN OUT");
305
packageFunction->iAddArgMode(mode);
308
packageFunction->iAddArgMode(wxEmptyString);
310
// Finally the defaults, as we got them.
311
def = argDefsTkz.GetNextToken();
312
if (!def.IsEmpty() && !def.IsSameAs(wxT("-")))
315
def = def.Mid(1, def.Length()-2);
317
// Check the cache first - if we don't have a value, get it and cache for next time
318
wxString val = exprCache[def];
319
if (val == wxEmptyString)
321
val = obj->GetDatabase()->ExecuteScalar(wxT("SELECT pg_get_expr('") + def + wxT("', 'pg_catalog.pg_class'::regclass)"));
322
exprCache[def] = val;
324
packageFunction->iAddArgDef(val);
327
packageFunction->iAddArgDef(wxEmptyString);
330
packageFunction->iSetOid(packageFunctions->GetOid(wxT("oid")));
331
packageFunction->iSetArgCount(packageFunctions->GetOid(wxT("nargs")));
332
packageFunction->iSetReturnType(typeCache[packageFunctions->GetVal(wxT("eltdatatype"))]);
334
if (packageFunctions->GetVal(wxT("visibility")) == wxT("+"))
335
packageFunction->iSetVisibility(_("Public"));
336
else if (packageFunctions->GetVal(wxT("visibility")) == wxT("-"))
337
packageFunction->iSetVisibility(_("Private"));
339
packageFunction->iSetVisibility(_("Unknown"));
341
packageFunction->iSetSource(package->GetBodyInner());
345
browser->AppendObject(obj, packageFunction);
213
packageFunctions = obj->GetDatabase()->ExecuteSet(sql);
215
pgSet *types = obj->GetDatabase()->ExecuteSet(wxT(
216
"SELECT oid, format_type(oid, NULL) AS typname FROM pg_type"));
220
typeCache[types->GetVal(wxT("oid"))] = types->GetVal(wxT("typname"));
224
if (packageFunctions)
226
while (!packageFunctions->Eof())
228
if (packageFunctions->GetVal(wxT("eltclass")) == wxT("F"))
229
packageFunction = new edbPackageFunction(package, packageFunctions->GetVal(wxT("eltname")));
231
packageFunction = new edbPackageProcedure(package, packageFunctions->GetVal(wxT("eltname")));
233
// Tokenize the arguments
234
wxStringTokenizer argTypesTkz(wxEmptyString), argModesTkz(wxEmptyString);
235
queryTokenizer argNamesTkz(wxEmptyString, (wxChar)','), argDefsTkz(wxEmptyString, (wxChar)',');
239
tmp = packageFunctions->GetVal(wxT("allargtypes"));
241
argTypesTkz.SetString(tmp.Mid(1, tmp.Length() - 2), wxT(","));
244
tmp = packageFunctions->GetVal(wxT("argtypes"));
246
argTypesTkz.SetString(tmp);
250
tmp = packageFunctions->GetVal(wxT("argnames"));
252
argNamesTkz.SetString(tmp.Mid(1, tmp.Length() - 2), wxT(","));
255
tmp = packageFunctions->GetVal(wxT("argmodes"));
257
argModesTkz.SetString(tmp.Mid(1, tmp.Length() - 2), wxT(","));
260
if (obj->GetConnection()->HasFeature(FEATURE_FUNCTION_DEFAULTS))
262
tmp = packageFunctions->GetVal(wxT("proargdefvals"));
264
argDefsTkz.SetString(tmp.Mid(1, tmp.Length() - 2), wxT(","));
267
// Now iterate the arguments and build the arrays
268
wxString type, name, mode, def;
270
while (argTypesTkz.HasMoreTokens())
272
// Add the arg type. This is a type oid, so
273
// look it up in the hashmap
274
type = argTypesTkz.GetNextToken();
275
packageFunction->iAddArgType(typeCache[type]);
277
// Now add the name, stripping the quotes if
279
name = argNamesTkz.GetNextToken();
283
name = name.Mid(1, name.Length() - 2);
284
packageFunction->iAddArgName(name);
287
packageFunction->iAddArgName(wxEmptyString);
290
mode = argModesTkz.GetNextToken();
293
if (mode == wxT('o') || mode == wxT("2"))
295
else if (mode == wxT("b"))
296
if (packageFunctions->GetVal(wxT("eltclass")) == wxT("F"))
297
mode = wxT("IN OUT");
300
else if (mode == wxT("3"))
301
mode = wxT("IN OUT");
305
packageFunction->iAddArgMode(mode);
308
packageFunction->iAddArgMode(wxEmptyString);
310
// Finally the defaults, as we got them.
311
def = argDefsTkz.GetNextToken();
312
if (!def.IsEmpty() && !def.IsSameAs(wxT("-")))
315
def = def.Mid(1, def.Length() - 2);
317
// Check the cache first - if we don't have a value, get it and cache for next time
318
wxString val = exprCache[def];
319
if (val == wxEmptyString)
321
val = obj->GetDatabase()->ExecuteScalar(wxT("SELECT pg_get_expr('") + def + wxT("', 'pg_catalog.pg_class'::regclass)"));
322
exprCache[def] = val;
324
packageFunction->iAddArgDef(val);
327
packageFunction->iAddArgDef(wxEmptyString);
330
packageFunction->iSetOid(packageFunctions->GetOid(wxT("oid")));
331
packageFunction->iSetArgCount(packageFunctions->GetOid(wxT("nargs")));
332
packageFunction->iSetReturnType(typeCache[packageFunctions->GetVal(wxT("eltdatatype"))]);
334
if (packageFunctions->GetVal(wxT("visibility")) == wxT("+"))
335
packageFunction->iSetVisibility(_("Public"));
336
else if (packageFunctions->GetVal(wxT("visibility")) == wxT("-"))
337
packageFunction->iSetVisibility(_("Private"));
339
packageFunction->iSetVisibility(_("Unknown"));
341
packageFunction->iSetSource(package->GetBodyInner());
345
browser->AppendObject(obj, packageFunction);
346
346
packageFunctions->MoveNext();
352
352
delete packageFunctions;
355
return packageFunction;
355
return packageFunction;
358
358
pgObject *edbPackageFunctionFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restriction)
362
if (collection->GetDatabase()->GetConnection()->EdbMinimumVersion(8, 2))
363
restr = wxT(" WHERE format_type(prorettype, NULL) != 'void' AND pronamespace = ");
365
restr = wxT(" WHERE eltclass = 'F' AND packageoid = ");
367
restr += ((edbPackageObjCollection *)collection)->GetPackage()->GetOidStr();
369
return AppendFunctions(collection, ((edbPackageObjCollection *)collection)->GetPackage(), browser, restr);
362
if (collection->GetDatabase()->GetConnection()->EdbMinimumVersion(8, 2))
363
restr = wxT(" WHERE format_type(prorettype, NULL) != 'void' AND pronamespace = ");
365
restr = wxT(" WHERE eltclass = 'F' AND packageoid = ");
367
restr += ((edbPackageObjCollection *)collection)->GetPackage()->GetOidStr();
369
return AppendFunctions(collection, ((edbPackageObjCollection *)collection)->GetPackage(), browser, restr);
372
372
pgObject *edbPackageProcedureFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restriction)
376
if (collection->GetDatabase()->GetConnection()->EdbMinimumVersion(8, 2))
377
restr = wxT(" WHERE format_type(prorettype, NULL) = 'void' AND pronamespace = ");
379
restr = wxT(" WHERE eltclass = 'P' AND packageoid = ");
381
restr += ((edbPackageObjCollection *)collection)->GetPackage()->GetOidStr();
383
return AppendFunctions(collection, ((edbPackageObjCollection *)collection)->GetPackage(), browser, restr);
376
if (collection->GetDatabase()->GetConnection()->EdbMinimumVersion(8, 2))
377
restr = wxT(" WHERE format_type(prorettype, NULL) = 'void' AND pronamespace = ");
379
restr = wxT(" WHERE eltclass = 'P' AND packageoid = ");
381
restr += ((edbPackageObjCollection *)collection)->GetPackage()->GetOidStr();
383
return AppendFunctions(collection, ((edbPackageObjCollection *)collection)->GetPackage(), browser, restr);
386
#include "images/function.xpm"
387
#include "images/functions.xpm"
386
#include "images/function.pngc"
387
#include "images/functions.pngc"
389
edbPackageFunctionFactory::edbPackageFunctionFactory(const wxChar *tn, const wxChar *ns, const wxChar *nls, const char **img)
390
: edbPackageObjFactory(tn, ns, nls, img)
389
edbPackageFunctionFactory::edbPackageFunctionFactory(const wxChar *tn, const wxChar *ns, const wxChar *nls, wxImage *img)
390
: edbPackageObjFactory(tn, ns, nls, img)
392
metaType = EDB_PACKAGEFUNCTION;
392
metaType = EDB_PACKAGEFUNCTION;
395
edbPackageFunctionFactory packageFunctionFactory(__("Function"), __("New Function..."), __("Create a new Function."), function_xpm);
396
static pgaCollectionFactory cff(&packageFunctionFactory, __("Functions"), functions_xpm);
395
edbPackageFunctionFactory packageFunctionFactory(__("Function"), __("New Function..."), __("Create a new Function."), function_png_img);
396
static pgaCollectionFactory cff(&packageFunctionFactory, __("Functions"), functions_png_img);
398
398
pgCollection *edbPackageObjFactory::CreateCollection(pgObject *obj)
400
return new edbPackageObjCollection(GetCollectionFactory(), (edbPackage*)obj);
400
return new edbPackageObjCollection(GetCollectionFactory(), (edbPackage *)obj);
403
#include "images/procedure.xpm"
404
#include "images/procedures.xpm"
403
#include "images/procedure.pngc"
404
#include "images/procedures.pngc"
406
edbPackageProcedureFactory::edbPackageProcedureFactory()
407
: edbPackageFunctionFactory(__("Procedure"), __("New Procedure..."), __("Create a new Procedure."), procedure_xpm)
406
edbPackageProcedureFactory::edbPackageProcedureFactory()
407
: edbPackageFunctionFactory(__("Procedure"), __("New Procedure..."), __("Create a new Procedure."), procedure_png_img)
411
411
edbPackageProcedureFactory packageProcedureFactory;
412
static pgaCollectionFactory cfp(&packageProcedureFactory, __("Procedures"), procedures_xpm);
412
static pgaCollectionFactory cfp(&packageProcedureFactory, __("Procedures"), procedures_png_img);