Documentation Archive Developer
Search
PATH  WebObjects 4.0 Documentation > EOAccess Reference



SQL Statement Formats

In addition to mapping database values to object values, an EOAttribute can alter the way values are selected, inserted, and updated in the database by defining special format strings. These format strings allow a client application to extend its reach right down to the server for certain operations. For example, you might want to view an employee's salary on a yearly basis, without defining a derived attribute as in a previous example. In this case, you could set the salary attribute's SELECT statement format to "salary * 12" (with setReadFormat:) and the INSERT and UPDATE statement formats to "salary / 12" (setWriteFormat:). Thus, whenever your application retrieves values for the salary attribute they're multiplied by 12, and when it writes values back to the database they're divided by 12.

Your application can use any legal SQL value expression in a format string, and can even access server-specific features such as functions and stored procedures (see EOEntity's setStoredProcedure:forOperation: method description for more information). Accessing server-specific features can offer your application great flexibility in dealing with its server, but does limit its portability. You're responsible for ensuring that your SQL is well-formed and will be understood by the database server.

Format strings for the setReadFormat: and setWriteFormat: methods should use "%P" as the substitution character for the value that is being formatted. "%@" will not work. For example:

[myAttribute setReadFormat:@"TO_UPPER(%P)"];
[myAttribute setWriteFormat:@"TO_LOWER(%P)"];

Instead of setting the read and write formats programmatically, you can set them in EOModeler, which is more common. For more information, see the chapter "Using EOModeler" in WebObjects Tools and Techniques.





Copyright © 1998, Apple Computer, Inc. All rights reserved.