1-- import python modules 2 3CREATE FUNCTION import_fail() returns text 4 AS 5'try: 6 import foosocket 7except ImportError: 8 return "failed as expected" 9return "succeeded, that wasn''t supposed to happen"' 10 LANGUAGE plpythonu; 11 12 13CREATE FUNCTION import_succeed() returns text 14 AS 15'try: 16 import array 17 import bisect 18 import calendar 19 import cmath 20 import errno 21 import math 22 import operator 23 import random 24 import re 25 import string 26 import time 27except Exception, ex: 28 plpy.notice("import failed -- %s" % str(ex)) 29 return "failed, that wasn''t supposed to happen" 30return "succeeded, as expected"' 31 LANGUAGE plpythonu; 32 33CREATE FUNCTION import_test_one(p text) RETURNS text 34 AS 35'try: 36 import hashlib 37 digest = hashlib.sha1(p.encode("ascii")) 38except ImportError: 39 import sha 40 digest = sha.new(p) 41return digest.hexdigest()' 42 LANGUAGE plpythonu; 43 44CREATE FUNCTION import_test_two(u users) RETURNS text 45 AS 46'plain = u["fname"] + u["lname"] 47try: 48 import hashlib 49 digest = hashlib.sha1(plain.encode("ascii")) 50except ImportError: 51 import sha 52 digest = sha.new(plain); 53return "sha hash of " + plain + " is " + digest.hexdigest()' 54 LANGUAGE plpythonu; 55 56 57-- import python modules 58-- 59SELECT import_fail(); 60SELECT import_succeed(); 61 62-- test import and simple argument handling 63-- 64SELECT import_test_one('sha hash of this string'); 65 66-- test import and tuple argument handling 67-- 68select import_test_two(users) from users where fname = 'willem'; 69