Chapter 3 - User-defined Routines and Object Behavior Flashcards
What is the general idea and the different kinds of User-defined Routines?
- Named persisted code to be invoked in SQL (PSM/external)
- Integrated in schema, CREATE/ALTER/DROP, privileges
- Procedure (CALL statement), function (expression), method (type function)
What is the general structure of a routine definition in SQL?
- > Head: Name + list of parameters (IN/OUT/INOUT) + returns* type
- procedures
- **functions/methods
- > Body: SQL (PSM); or host programming language
What are the alternatives for providing implementation for routines?
SQL(PSM)**, external
** Procedural language extensions (statements)
What are the differences between procedures and functions in SQL?
- > Procedure -> Statement context
- OUT/INOUT parameters: modified by procedure and mode available to caller afterwords
- Dynamic Result Sets: Declare and return cursors
- > Function -> Expression context
- returns (mandatory) (no result sets)
Which additional functionality is provided by table functions?
Transform non-relational data on relational table dynamically
Returns a table (multiset of rows)
-> RETURNS TABLE (name type, name type, …)
-> RETURNS TABLE (SELECT… FROM…)
Used in FROM clause
-> SELECT … FROM TABLE (f(a,b) AS
Compare table functions and views as mechanisms for virtual tables?
TFs support dynamic parameters
How are privileges applied to UDRs?
Execute privilege
Roles: (DEFINER**, INVOKER) when executing queries inside routine def
** With GRANT option
** Routine droped if lost
What are implications of cross-schema overloading?
- Overloading routines with the same signature
* Need to define schema path for overload resolution
What is the general idea and the algorithm for subject routine determination in SQL?
- > Decide which routine to invoke based on: Static types of args, type precedence list (subtype relation for STs) schema path, EXECUTE privilege
- > Algorithm: 1. set of candidates (name+ num of args) from all PATH if not fully qualified. 2. Eliminate unprivileged and incompatible arg types (not in prec. list). 3. Select best match from left to right. 4. Select first on PATH.
How is the overloading of procedures supported in SQL?
Only based on param numbers.
What are methods in SQL and how do they differ from functions?
-> SQL functions belonging to a structured type.
diffs:
-> Implicit SELF parameter (subject parameter)
-> Separate body and signature def (same schema as ST)
-> Invoked with dot notation: UDT-value.method()
CREATE TYPE T AS (…)
METHOD in RETURNS type;
CREATE METHOD m FOR T
BEGIN … END;
What are the different types of methods supported in SQL?
-> Instance (original vs overriding), constructor, static
How does SQL support overriding methods?
Signature must match, except for implict SELF, which is of subtype
How does subject routine determination work for instance methods?
- > Just like for functions, but setting PATH to list of supertype schemas
- > Dynamic dispatch: decide between overriding methods based on dynamic type of SELF.
- Schema evolution affects actual invoked method
How does method invocation work for typed tables and object references?
Typed tables: DEREF(oid).method( ) -> Transform ROW into value of ST
references: “->” notation -> Requires SELECT privilege on scoped table: table-specific INVOKE privileges