Skip to content

Methods and the CALL statement

View as Markdown

A method is a function attached to a table. Methods are written in C, loaded dynamically when they are first called, and run with the CALL statement; the value they return is passed back to the caller. The system classes db_user, db_authorization and db_authorizations provide the methods used for user and privilege management; DB_METHOD lists all methods.

For server-side logic written by users, Java stored functions and procedures are usually the better choice.

  • A class method is called on the table itself (the class object). It typically creates or looks up rows, or reads and updates class attributes. find_user is a class method of db_user.
  • An instance method is called on one row (an instance). It typically computes or updates values of that row. set_password is an instance method of db_user, called on a user object.

Subclasses inherit the class and instance methods of their superclasses. Name conflicts between methods are resolved like those between columns; see Table inheritance.

CALL method_name ( [ argument [, argument ...] ] ) ON call_target [ { TO | INTO } variable ];
CALL method_name ( call_target [, argument ...] ) [ { TO | INTO } variable ];
call_target ::= CLASS table_name | object_expression
  • method_name is a method of the target's table or a system method. Methods without arguments are called with empty parentheses.
  • To call a class method, put CLASS before the table name: ON CLASS db_user.
  • To call an instance method, give an expression that evaluates to a row object, typically a session variable that received the object from an earlier call. In the second form, the object is the first argument.
  • TO or INTO stores the returned value in a session variable, which can be used as a target or argument of later calls and read in queries such as SELECT variable FROM db_root.
  • A method call can be the target or an argument of another call.
-- class method: look up a user and keep the object in u
CALL find_user ('dba') ON CLASS db_user INTO u;
SELECT u.name FROM db_root; -- DBA
-- instance method, both forms
CALL add_user ('user_5', '') ON CLASS db_user TO v;
CALL set_password ('abc') ON v;
CALL set_password (v, 'abc');