A Procedural Language in mruby

November 15, 2015 ยท View on GitHub

TBD

Features

  • Scalar function calls
  • Type conversion between mruby and PostgreSQL built-in types
  • Trigger functions
  • Set returning function calls

Quick Start

TBD

Trigger Functions

You can define a trigger in plmruby. When a trigger function is called, values listed below are passed.

ArgumentTypeDescription
newHashThe new database row for INSERT/UPDATE operations in row-level triggers. Otherwise, nil.
oldHashThe old database row for UPDATE/DELETE operations in row-level triggers. Otherwise, nil.
tg_nameStringA trigger name
tg_whenSymbolOne of :before, :after, or :instead_of
tg_levelSymbolOne of :row, or :statement
tg_opSymbolOne of :insert, :delete, :update, or :truncate
tg_relidFixnumOID of the table on which the trigger occurred.
tg_table_nameStringThe name of the table on which the trigger occurred.
tg_table_schemaStringThe schema of the table on which the trigger occurred.
tg_argvArrayThe arguments from the CREATE TRIGGER statement.

Return Value From Trigger

Trigger function may return nil, :OK, :SKIP and any tuple like value. A value returned is treated as follows. Tuple like value indicates a value which can be converted into PostgreSQL record type. See [Type Conversion](#Type Conversion) section. Otherwise, an error occurred.

Returned Valuenil:ok:skipTuple like value
:STATEMENTignoredignoredignoredignored
:ROW + :AFTERignoredignoredignoredignored
:ROW + :BEFOREunmodifiedunmodifiedskippedmodified
:ROW + :INSTEAD_OFunmodifiedunmodifiedskippedmodified

ignored: The returned value is ignored.

unmodified: The row value is new if trigger is fired by :UPDATE, otherwise row value is old.

skip: Skip the rest of the operation for this row, and the row value is unmodified (i.e., subsequent triggers are not fired, and the INSERT/UPDATE/DELETE does not occur for this row).

modified: the row value will be replaced with the returned value.

Type Conversion

From PostgreSQL to mruby

When mruby function is called, the passed values will be automatically converted from PostgreSQL's type into mruby type as follows. Otherwise, the value will be converted into String via normal stringify procedure of PostgreSQL (pg_type.typoutput).

Argument Type in PostgreSQLType in mruby
oidFixnum
booltrue / false
int2Fixnum
int4Fixnum
int8Fixnum
float4Float
float8Float
numericFloat
dateTime
timestampTime
timestamptzTime
textString
varcharString
charString
jsonHash or Array (via JSON.parse. see https://github.com/mattn/mruby-json)
array / anyarrayArray
recordHash
OtherwiseString (via pg_type.typoutput)

From mruby to PostgreSQL

When mruby returns a value to PostgreSQL, plmruby will attempt to convert the value so that it can adapt the type declared as RETURNS. mruby types which can be converted into a particular PostgreSQL type is listed below. When a value with a type which is not listed, the value will be stringified via .to_s at first, then passed to pg_type.typinput, which is common string parse procedure for all type.

RETURNS type in PostgreSQLType in mruby
oidFixnum
booltrue / false
int2Fixnum
int4Fixnum
int8Fixnum
float4Float
float8Float
numericFloat
dateTime
timestampTime
timestamptzTime
textString
varcharString
charString
jsonHash or Array (via JSON.stringify. see https://github.com/mattn/mruby-json)
arrayArray
recordHash
Otherwisecall .to_s, then passed to pg_type.typinput

Set Returning Functions

PostgreSQL can return TBD