简体   繁体   English

如何将这个 PostgreSQL 函数从 PL/Py 翻译成 PL/SQL?

[英]How to translate this PostgreSQL function from PL/Py to PL/SQL?

I have a function written in PL/Python.我有一个用 PL/Python 编写的函数。 It is a database function that runs in Python, which is permitted because of a procedural language installed via:它是一个在 Python 中运行的数据库函数,由于通过以下方式安装了过程语言,因此允许使用:

CREATE PROCEDURAL LANGUAGE 'plpythonu' HANDLER plpython_call_handler

(I found a nice trick, to allow non-admin users permission to run, by using a unique name, though it has not much to do with my question, I'm sure some of you will wonder how I am doing this, so below is the answer) (我发现了一个很好的技巧,通过使用唯一名称来允许非管理员用户运行,尽管这与我的问题没有太大关系,我相信你们中的一些人会想知道我是如何做到这一点的,所以下面是答案)

CREATE TRUSTED PROCEDURAL LANGUAGE 'plpythonu2' HANDLER plpython_call_handler
GRANT USAGE ON LANGUAGE plpythonu2 TO admin;

Now to the question at hand, my "hack" above works for me, but if I want to use Amazon's RDS service, I cannot install languages, and PL/Python is not available.现在回到手头的问题,我上面的“hack”对我有用,但是如果我想使用亚马逊的 RDS 服务,我无法安装语言,并且 PL/Python 不可用。 SQL however, is.然而,SQL 是。

Therefore, I need help translating the following function, written in Python into pure SQL.因此,我需要帮助将以下用 Python 编写的函数翻译成纯 SQL。

CREATE OR REPLACE FUNCTION "public"."human_readable_bits" (
  "b" bigint = 0
)
RETURNS varchar AS
$body$
import math
if b:
  exponent = math.floor(math.log(b)/math.log(1024))
  val = b/pow(1024, math.floor(exponent))
  val = round(val*2)/2 -- This rounds to the nearest HALF (X.5) B, Kb, Mb, Gb, etc.
  return "%.2f %s" % (val, ('B','Kb','Mb','Gb','Tb','Pb','Eb','Zb','Yb')[int(exponent)])
else:
  return "0 Gb"
$body$
LANGUAGE 'plpythonu2'
VOLATILE
RETURNS NULL ON NULL INPUT
SECURITY INVOKER
COST 100;

This function allows me to perform queries such as:此功能允许我执行查询,例如:

 => SELECT human_readable_bits(3285824466906);
 human_readable_bits
---------------------
 3.00 Tb
(1 row)

OR或者

=> SELECT human_readable_bits(5920466906);
 human_readable_bits
---------------------
 5.50 Gb
(1 row)

Also, as a side-note/secondary question, after I created the function, when I look at the DDL, it has a line in it that says "SECURITY INVOKER," does anyone know what that means/does?另外,作为一个旁注/次要问题,在我创建该函数后,当我查看 DDL 时,其中有一行写着“SECURITY INVOKER”,有谁知道这意味着/做什么?

A conversion for a plain PLPGSQL function would be:普通 PLPGSQL 函数的转换是:

CREATE OR REPLACE FUNCTION public.human_readable_bits(b NUMERIC)
  RETURNS VARCHAR AS
$BODY$
declare
   exponent integer;
   val float;
   arr varchar[];
   sz VARCHAR(10);
   result varchar(20);
BEGIN
    if b is null or b = 0 then
       return '0 B';
    end if;

    if b < 1024 then
        return b::varchar || ' Bits';
    end if;

    arr := ARRAY['B','Kb','Mb','Gb','Tb','Pb','Eb','Zb','Yb'];
    exponent := floor( log(b) / log(1024));
    val := b/power(1024,exponent);
    val := round(val*2)/2;

    sz := arr[trunc(floor(log(b) / log(1024)))];

    if strpos(val::varchar,'.') > 0  then
       result := substr(val::varchar, 1, strpos(val::varchar,'.')-1);
       result := result || '.' || rpad( substr(val::varchar, strpos(val::varchar,'.')+1), 2, '0' ) || ' ' || sz;
    else
       result := val::varchar || '.00 ' || sz;
    end if;
    return result;

END;
$BODY$
  LANGUAGE plpgsql VOLATILE;

As a result of this function:作为此功能的结果:

select human_readable_bits(328582446690656456434534453) hrb0,
       human_readable_bits(3285824466906) hrb1,
       human_readable_bits(5920466906) hrb2,
       human_readable_bits(1024) hrb3,
       human_readable_bits(512) hrb4,
       human_readable_bits(null) hrb5;

Would result in:会导致:

   hrb0       hrb1        hrb2        hrb3        hrb4        hrb5
  272.00 Zb  3.00 Gb     5.50 Mb     1.00 B     512 Bits       0 B

As per your side question the answer can easily be found at the Create Function Documentation根据您的附带问题,可以在创建函数文档中轻松找到答案

SECURITY INVOKER indicates that the function is to be executed with the privileges of the user that calls it. SECURITY INVOKER表示该函数将以调用它的用户的权限执行。 That is the default.这是默认设置。 SECURITY DEFINER specifies that the function is to be executed with the privileges of the user that created it. SECURITY DEFINER 指定函数将以创建它的用户的权限执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM