1--
2-- Test named and nameless parameters
3--
4
5CREATE FUNCTION test_param_names0(integer, integer) RETURNS int AS $$
6return args[0] + args[1]
7$$ LANGUAGE plpythonu;
8
9CREATE FUNCTION test_param_names1(a0 integer, a1 text) RETURNS boolean AS $$
10assert a0 == args[0]
11assert a1 == args[1]
12return True
13$$ LANGUAGE plpythonu;
14
15CREATE FUNCTION test_param_names2(u users) RETURNS text AS $$
16assert u == args[0]
17if isinstance(u, dict):
18    # stringify dict the hard way because otherwise the order is implementation-dependent
19    u_keys = list(u.keys())
20    u_keys.sort()
21    s = '{' + ', '.join([repr(k) + ': ' + repr(u[k]) for k in u_keys]) + '}'
22else:
23    s = str(u)
24return s
25$$ LANGUAGE plpythonu;
26
27-- use deliberately wrong parameter names
28CREATE FUNCTION test_param_names3(a0 integer) RETURNS boolean AS $$
29try:
30	assert a1 == args[0]
31	return False
32except NameError, e:
33	assert e.args[0].find("a1") > -1
34	return True
35$$ LANGUAGE plpythonu;
36
37
38SELECT test_param_names0(2,7);
39SELECT test_param_names1(1,'text');
40SELECT test_param_names2(users) from users;
41SELECT test_param_names2(NULL);
42SELECT test_param_names3(1);
43