用Python写了一个postgresql函数,感觉很爽

2015-07-24 10:48:37 · 作者: · 浏览: 3

?

Python写了一个postgresql函数,感觉很爽

CREATE LANGUAGE plpythonu;


postgresql函数

CREATE OR REPLACE FUNCTION myfun1(text)
RETURNS text AS
$BODY$
s = args[0]
h = 0;
n = len(s);
for i, c in enumerate(s):
h = h + ord(c)*31**(n-1-i);
bits = 4*8;
return (h + 2**(bits-1)) % 2**bits - 2**(bits-1)


$BODY$
LANGUAGE 'plpythonu';



调用:返回字符串的hashcode值
select myfun1('测试'),myfun1('a'),myfun1('A')

上述代码在MAC 笔记本上运行成功