Bug #56361 | Table Methods | ||
---|---|---|---|
Submitted: | 30 Aug 2010 11:04 | Modified: | 29 May 2012 13:58 |
Reporter: | Winfried Trümper | Email Updates: | |
Status: | Verified | Impact on me: | |
Category: | MySQL Server: Stored Routines | Severity: | S4 (Feature request) |
Version: | OS: | Any | |
Assigned to: | CPU Architecture: | Any |
[30 Aug 2010 11:04]
Winfried Trümper
[9 Jan 2011 12:55]
Valeriy Kravchuk
Are you referring to something similar to object types with methods in Oracle RDBMS? Tables in SQL do NOT have methods in any SQL Standard I know about...
[9 Jan 2011 22:31]
Winfried Trümper
Changed the synopsis to Table Methods after evaluating and editing the MySQL source. Overall it should be more consistent that way. The definition of a table method now reads: DELIMITER // CREATE FUNCTION withdraw(arg_amount double) RETURNS double METHOD_TABLE 'accounts' /* marking the definition as a Table Method */ BEGIN THIS.acc_amount = THIS.acc_amount + arg_amount; INSERT INTO statements VALUES (THIS.acc_number, arg_amount, NOW()); RETURN(THIS.acc_amount); END // DELIMITER ;
[9 Jan 2011 22:32]
Winfried Trümper
I appreciate the attention you give to this feature request. Regarding your questions: #1 The approach appears different from the object types available in Oracle RDBMS. #2 A few reserved methods already exist since MySQL 5.0, although the documentation uses a different terminology. Object Oriented terminology and Relational terminology correspond in the following way: class=table, instance/object=row, method=... Methods are functions, which belong to a class and can be invoked on an object. That applies to triggers: they belong to a table and they are invoked on a row. Thus triggers are methods, to be more precise: they are constructors and destructors. However, constructors and destructors are called implicitly, so no calling syntax for triggers would remind anybody of "hey look, we already got methods in MySQL". Actually a special invocation syntax for MySQL methods is required to make it smooth for multi-table selects. See my next message for a comparison. Regarding the method definition, I suggest a TABLE clause for functions, which enables the use of THIS in the SP code. It is meant as the analogy to NEW and OLD for triggers. The alternative would be a CREATE METHOD syntax - too heavy. In short, my suggestion is the generalization of an excellent feature already contained in MySQL. Again, thank you for your interest. -Winfried
[9 Jan 2011 22:33]
Winfried Trümper
C++ method invocation: account_object = somehow get it; account_object.withdraw(789); MySQL table method invocation: SELECT withdraw(789) FROM accounts WHERE acc_number = 123456; Inside the withdraw function the alias THIS provides full access to the retrieved row. Qualified for a multi-table SELECT: SELECT accounts!withdraw(789) FROM accounts INNER JOIN ... WHERE acc_number = 123456; The need for (fully) qualified method names for multi-table SELECT effectively prevents multiple inheritance (MI). A matter of personal taste whether it's good or bad. However, MI needs a method resolution order and that would require further keywords. Too complex to implement.
[9 Jan 2011 22:41]
Winfried Trümper
Requirements for table methods: * METHOD_TABLE clause for functions, which takes the associated table name. * ALTER TABLE proc ADD COLUMN method_table char(64) AFTER db; * THIS alias in analogy to NEW and OLD for triggers * Method invocation syntax for multi-table select * Check at start of SP execution whether function is a table method Benefits of table methods: * Same as for triggers: potentially faster SP, more concise code * Creating a unique selling point with a relatively small modification of existing code.