Code Editorは、OBJECTTABLE
関数の内部で、スクリプト言語としてPython3の使用をサポートしています。追加のパッケージをインストールする必要はありませんが、言語PermissionをUser Roleに追加する必要があります。
The Python Script Engine implemented in the Code Editor is based on GraalVM's Polyglot API which currently supports only *nix systems - which means that Python scripts are supported for CData Virtuality SaaS and *nix-based on-premise CData Virtuality Server.
The admin-role
does not include the necessary permissions to use Python by default. To grant Python access for users with the admin-role
, you need to add the L permission for the "python" resource via a custom role and assign this custom role to the intended users.
L permission for Python removed from the admin-role
since v4.9
スクリプトは、改行 ( \n
) やタブ ( \t
) のような特殊文字を使用せずにすぐに実行でき、if/else ステートメントを使用できます。ObjectTable内でPythonスクリプトを使用する方法については、以下の例を参照してください。
Pythonスクリプトを実行するには、以下の例のようにSQL文で言語として'python'を指定するだけです。
Examples
このスクリプトは式を評価し、
result
変数に格納された結果を返します。二重引用符で囲まれた「result」は CData Virtuality Server に表示された列名、「result」はスクリプト内の変数名に対応します:
SELECT
*
FROM
(
ObjectTable (
language
'python'
'result = 1 + 1'
COLUMNS
"result"
integer
'result'
)
AS
x);;
このスクリプトは Python の関数を使用して式を評価し、結果を取得します:
SELECT
*
FROM
(
ObjectTable (
language
'python'
'import math
a = math.exp(3)'
COLUMNS
"a"
integer
'a'
)
AS
x);;
このスクリプトは複数のカラムを複数のリターンで評価します:
SELECT
*
FROM
(
ObjectTable (
language
'python'
'a = 1 + 1
b = a + 1
c = b * 2'
COLUMNS
"a"
integer
'a'
,
"b"
integer
'b'
,
"c"
integer
'c'
)
AS
x);;
このスクリプトでは、
PASSING
キーワードを使用してユーザー定義変数をスクリプトに渡します。'2' は渡される値で、AS two
はスクリプト内の変数名を参照します:
SELECT
*
FROM
(
ObjectTable (
language
'python'
'a = 1 + two'
PASSING
'2'
AS
two
COLUMNS
"a"
integer
'a'
)
AS
x);;
PASSING
キーワードを使用したこのより複雑なスクリプトでは、複数のユーザー定義変数を使用しています。COLUMNS
フィールドに記載された各行に対して結果が返されます:
SELECT
*
FROM
(
ObjectTable (
language
'python'
'a = 1 + two
b = 2 + three
c = 3 + four
d = teststring
e = f
g = charhere'
PASSING
'2'
AS
two,
'3'
AS
three,
'4'
AS
four,
''
'hello'
''
AS
teststring,
'25.0'
AS
f,
''
's'
''
AS
charhere
COLUMNS
"a"
integer
'a'
,
"b"
integer
'b'
,
"c"
integer
'c'
,
"d"
string
'd'
,
"e"
double
'e'
,
"g"
char
'g'
)
AS
x);;
このスクリプトはif/else文の使い方を示しています:
SELECT
*
FROM
(
ObjectTable (
language
'python'
'result = 2
if (result % 2 == 0):
result = 1
else:
result = 0'
COLUMNS
"result"
integer
'result'
)
AS
x);;