是否可以使用PostgreSQL plv8扩展名创建可重用的函数? [英] Is it possible to create a re-usable function with the PostgreSQL plv8 extension?

查看:195
本文介绍了是否可以使用PostgreSQL plv8扩展名创建可重用的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Google的开放位置代码javascript实现添加到PostgreSQL中(使用 plv8扩展),并使其可用于从PostGIS几何/地理数据类型进行编码/解码。

I wanted to drop in google's open location code javascript implementation into PostgreSQL (using the plv8 extension) and make it available to encode/decode from PostGIS geometry/geography data types.

虽然我成功了,但是却无法解决如何仅为 https创建单个函数://github.com/google/open-location-code/blob/master/js/src/openlocationcode.js 文件,最后我将该函数的副本放入需要编码的每个函数中/解码加码。当我尝试将其拉出自己的函数时,我可能会得到一个包含JavaScript的字符串或一个[Object],[object]的字符串,而不是一个可调用的函数。

While I was successful, I wasn't able to work out how to only create a single function for the https://github.com/google/open-location-code/blob/master/js/src/openlocationcode.js file and I ended up putting a copy of that function into each function where I needed to encode/decode plus codes. When I attempted to pull it out into its own function, I could either get a string containing javascript or a string of [Object],[object] rather than a callable function.

PostgreSQL中的plv8扩展可能吗?

Is this possible with the plv8 extension in PostgreSQL?

不完整的代码段示例(完整版本在这里):

Incomplete code snippet example (full version here):

DROP FUNCTION IF EXISTS olc.encode(float,float,integer);

CREATE OR REPLACE FUNCTION olc.encode(
    p_latitude double precision,
    p_longitude double precision,
    p_code_length integer DEFAULT 10
)
  RETURNS text AS
$BODY$


 var f  =  function () {
    var OpenLocationCode = {};

    /**
     * Provides a normal precision code, approximately 14x14 meters.
     * @const {number}
     */
    OpenLocationCode.CODE_PRECISION_NORMAL = 10;

    /**
     * Provides an extra precision code, approximately 2x3 meters.
     * @const {number}
     */
    OpenLocationCode.CODE_PRECISION_EXTRA = 11;

    // A separator used to break the code into two parts to aid memorability.
    var SEPARATOR_ = '+';

    // The number of characters to place before the separator.
    var SEPARATOR_POSITION_ = 8;

    // The character used to pad codes.
    var PADDING_CHARACTER_ = '0';


推荐答案

您有两个选择。


  1. 将函数的源代码存储在特殊的数据库表中,并使用 select加载它code>和 eval()。了解有关此答案的详细信息: plv8 JavaScript语言扩展可以调用第三方库吗?

  1. Store the source code of the function in a special database table and load it using select and eval(). Read about details in this answer: Can plv8 JavaScript language extension call 3rd party libraries?

将函数放置在初始化模块中,并使用配置参数 plv8.start_proc 设置此模块,以便在启动时自动执行该功能。您可以在 PL / v8文档中找到详细信息。

Place the function in an initialization module and set this module using configuration parameter plv8.start_proc so it is executed automatically on start-up. You can find details in the PL/v8 documentation.

第二个选项确实很方便,不需要额外的表格,但是看起来有点棘手。一个简单的例子:我们希望在所有plv8函数中都预定义一个函数 square_of_sum(a,b)。首先,创建初始化函数:

The second option is really handy and does not need an additional table, but may seem a bit tricky. A simple example: we want to have a function square_of_sum(a, b) predefined in all our plv8 functions. First, create the initialization function:

create or replace function plv8_init()
returns void language plv8 as $$

    square_of_sum = function(a, b) {
        return (a+ b)* (a+ b)
    }

$$;

设置数据库的初始化函数:

set the initialization function for the database:

alter database my_database set plv8.start_proc to plv8_init;

并关闭当前连接。

在所有后续会话中,函数 square_of_sum(a,b)在每个其他plv8函数中都是已知的,例如:

In all subsequent sessions the function square_of_sum(a, b) is known in every other plv8 function, e.g.:

create or replace function js_test()
returns int language plv8 as $$
    return square_of_sum(3, 2)
$$;

select js_test();

 js_test
---------
      25
(1 row) 

这篇关于是否可以使用PostgreSQL plv8扩展名创建可重用的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆