home *** CD-ROM | disk | FTP | other *** search
- // EOSQLExpression.h
- // Copyright (c) 1994, NeXT Software, Inc. All rights reserved.
-
- #import <Foundation/Foundation.h>
- #import <EOAccess/EORelationship.h>
- #import <EOAccess/EODefines.h>
-
- @class EOQualifier;
- @class EOEntity;
- @class EOAttribute;
-
- // Adaptors must be capable of performing four basic operations:
- // select, insert, update, and delete. The default implementation in
- // the abstract super class obtains the adaptor's query language
- // expression class, instantiates an expression describing the operation,
- // and passes the expression as an argument to evaluateExpression:.
- // This is the default expression class for SQL adaptors.
- //
- // The class will need to be subclassed for specific databases. Normally
- // each adaptor will provide its own subclass.
- //
- // This is class (and its subclasses) are designed for use by anyone who
- // needs to assemble SQL expressions.
-
- @interface EOSQLExpression : NSObject
- {
- NSMutableDictionary *_aliasesByRelationshipPath;
- EOEntity *_entity;
- NSMutableString *_listString;
- NSMutableString *_valueListString;
- NSString *_whereClauseString;
- NSMutableString *_joinClauseString;
- NSMutableString *_orderByString;
- NSMutableArray *_bindings;
- NSMutableArray *_contextStack;
- NSString *_statement;
- BOOL _useAliases;
- }
-
- // Designated initializer
- - initWithEntity:(EOEntity *)entity;
-
- + (EOSQLExpression *)expressionForString:(NSString *)string;
- // Create an expression for the given string
-
- // Factory Methods for creating an insert, update, delete, or select statement
- + (EOSQLExpression *)insertStatementForRow:(NSDictionary *)row entity:(EOEntity *)entity;
- // Builds a SQL INSERT expression to perform the requested operation
-
- + (EOSQLExpression *)updateStatementForRow:(NSDictionary *)row qualifier:(EOQualifier *)qualifier entity:(EOEntity *)entity;
- // Builds a SQL UPDATE expression to perform the requested operation
-
- + (EOSQLExpression *)deleteStatementWithQualifier:(EOQualifier *)qualifier entity:entity;
- // Builds a SQL DELETE expression to perform the requested operation
-
- + (EOSQLExpression *)selectStatementForAttributes:(NSArray *)attributes lock:(BOOL)yn
- fetchSpecification:(EOFetchSpecification *)fetchSpecification
- entity:(EOEntity *)entity;
- // Builds a SQL SELECT expression to perform the requested operation
-
- // Accessor methods
- - (NSMutableDictionary *)aliasesByRelationshipPath;
- - (EOEntity *)entity;
-
- // These strings are built up incrementally by appending to the mutableStrings
- // below. E.g. addUpdateListAttribute:value: appends a formated value to
- // listString.
- - (NSMutableString *)listString;
- - (NSMutableString *)valueList;
- - (NSMutableString *)joinClauseString;
- - (NSMutableString *)orderByString;
- - (NSString *)whereClauseString;
- - (NSString *)statement;
- - (void)setStatement:(NSString *)statement;
-
- // These methods use the internal state of the SQLExpression to generate a syntactic
- // component of a SQLExpression
- - (NSString *)lockClause;
- // Returns a the SQL string which will cause the RDBMS to place a pessimistic lock on
- // any rows fetched
-
- - (NSString *)tableListWithRootEntity:(EOEntity *)entity;
- // Returns a list of aliased table names that can be used as the "FROM" part of a
- // SQL expression
-
-
- // Instance methods for creating an insert, update, delete, or select statement
- - (void)prepareInsertExpressionWithRow:(NSDictionary *)row;
- // Invokes the following methods:
- // [self addInsertListAttribute:] for each att in row
- // [self tableListWithRootEntity:]
- // [self assembleInsertStatementWithTableList: ...]
-
- - (void)prepareUpdateExpressionWithRow:(NSDictionary *)row qualifier:(EOQualifier *)qualifier;
- // Invokes the following methods:
- // [self addUpdateListAttribute:att value:value] for each att in row
- // [qualifier sqlStringForSQLExpression:self]
- // [self tableListWithRootEntity:]
- // [self assembleUpdateStatementWithTableList: ...]
-
- - (void)prepareDeleteExpressionForQualifier:(EOQualifier *)qualifier;
- // Invokes the following methods:
- // [qualifier sqlStringForSQLExpression:self]
- // [self tableListWithRootEntity:]
- // [self assembleDeleteStatementWithTableList: ...]
-
- - (void)prepareSelectExpressionWithAttributes:(NSArray *)attributes lock:(BOOL)yn fetchSpecification:(EOFetchSpecification *)fetchSpecification;
- // Invokes the following methods:
- // [self addSelectListAttribute:] for each att in attributes
- // [qualifier sqlStringForSQLExpression:self]
- // [self addOrderByAttributeOrdering:] for each attributeOrdering in fetchOrders
- // [self joinExpression]
- // [self tableListWithRootEntity:_entity]
- // [self lockClause]
- // [self assembleSelectStatementWithSelectString: ...]
-
-
- // These three methods are used to generate the part of the where clause that specifies the
- // join conditions based on the relationships used in this SQLExpression
- - (NSString *)assembleJoinClauseWithLeftName:(NSString *)leftName rightName:(NSString *)rightName joinSemantic:(EOJoinSemantic)semantic;
- // Invoked repeatedly by joinExpression to assemble each simple join expression clause.
-
- - (void)addJoinClauseWithLeftName:(NSString *)leftName rightName:(NSString *)rightName joinSemantic:(EOJoinSemantic)semantic;
- // Calls assembleJoinClauseWithLeftName: ... and then appends the resulting string to the
- // joinClause string
-
- - (void)joinExpression;
- // Invokes assembleJoinClauseWithLeftName:rightName:joinOperator:joinSemantic on each join pair
- // in each relationship listed in the aliasesByRelationshipPaths dictionary
-
-
- // The following four methods allow subclassers to override the assembling of the SQL pieces into
- // a SQL statement
- - (NSString *)assembleInsertStatementWithRow:(NSDictionary *)row tableList:(NSString *)tableList columnList:(NSString *)columnList valueList:(NSString *)valueList;
-
- - (NSString *)assembleUpdateStatementWithRow:(NSDictionary *)row qualifier:(EOQualifier *)qualifier tableList:(NSString *)tableList updateList:(NSString *)updateList whereClause:(NSString *)whereClause;
-
- - (NSString *)assembleDeleteStatementWithQualifier:(EOQualifier *)qualifier tableList:(NSString *)tableList whereClause:(NSString *)whereClause;
-
- - (NSString *)assembleSelectStatementWithAttributes:(NSArray *)attributes lock:(BOOL)lock qualifier:(EOQualifier *)qualifier fetchOrder:(NSArray *)fetchOrder selectString:(NSString *)selectString columnList:(NSString *)columnList tableList:(NSString *)tableList whereClause:(NSString *)whereClause joinClause:(NSString *)joinClause orderByClause:(NSString *)orderByClause lockClause:(NSString *)lockClause;
-
- // Used for assembling the different syntactic bits and pieces
- - (void)addSelectListAttribute:(EOAttribute *)attribute;
- // Adds the aliased column name for attribute to the listString instance variable
-
- - (void)addInsertListAttribute:(EOAttribute *)attribute value:(NSString *)value;
- // Adds the column name for attribute to the listString and value to the valueList string.
- // calls [[self class] formatValue:forAttribute]
-
- - (void)addUpdateListAttribute:(EOAttribute *)attribute value:(NSString *)value;
- // Adds the column name for attribute and value to the listString attribute.
- // calls [[self class] formatValue:forAttribute:]
-
-
- + (NSString *)formatStringValue:(NSString *)string;
- // Format an NSString for use as a string constant in a SQL statement.
- // Enclose in single quotes and escape any single quotes in the string.
-
- + (NSString *)formatValue:(id)value forAttribute:(EOAttribute *)attribute;
- // Returns a formatted string representation of value that is suitable for use in a sql statement
-
- + (NSString *)formatSQLString:(NSString *)sqlString format:(NSString *)format;
- // Format is one of the read or write formats specified on the attribute.
- // Returns a new string based on the format passed in, if format is nil then it
- // just returns the sqlString.
-
- // The following methods are used in conjunction with EOQualifier
- - (NSString *)sqlStringForConjoinedQualifiers:(NSArray *)qualifiers;
- - (NSString *)sqlStringForDisjoinedQualifiers:(NSArray *)qualifiers;
- - (NSString *)sqlStringForNegatedQualifier:(EOQualifier *)qualifier;
- - (NSString *)sqlStringForKeyValueQualifier:(EOKeyValueQualifier *)qualifier;
- - (NSString *)sqlStringForKeyComparisonQualifier:(EOKeyComparisonQualifier *)qualifier;
- // Methods to add SQL for each type of qualifier
-
- - (void)addOrderByAttributeOrdering:(EOSortOrdering *)sortOrdering;
- // Adds the appropriate information to the end of the orderBy string
-
- - (void)setUseAliases:(BOOL)useAliases;
- - (BOOL)useAliases;
- // By default the SQLExpression uses aliasing for select statements but not for
- // insert, update, or delete. Subclassers can change this behavior by overriding the
- // prepare methods and calling setUseAliasing before calling the super class's
- // implementation.
-
- - (NSString *)sqlStringForAttributeNamed:(NSString *)name;
- // returns the aliased column name for this attribute
-
- - (NSString *)sqlStringForSelector:(SEL)selector value:(id)value;
- // returns a sqlString for one of the predefined selectors in EOQualifier
-
- - (NSString *)sqlStringForValue:(id)value attributeNamed:(NSString *)string;
-
- - (NSString *)sqlStringForAttribute:(EOAttribute *)anAttribute;
- // Does the appropriate aliasing and name generation for an attribute in
- // the context of this particular query.
-
- - (NSString *)sqlStringForAttributePath:(NSArray *)path;
- // Returns an attribute name complete with alias for an attribute path.
- // An attribute path is an array that consists of any number of
- // relationships followed by an attribute.
-
- - (void)appendItem:(NSString *)itemString toListString:(NSMutableString *)listString;
- // Appends item to mutable list string. Default implementation will
- // append the item directly if the list string is empty, and append ", "
- // before the new item if not.
-
- + (NSString *)sqlPatternFromShellPattern:(NSString *)pattern;
- // Returns an equivalent sql pattern from the shell pattern
-
- // These methods are useful for adaptor writers that want or need to use bind variables
- // with their client library. If there is no need for an adaptor to use bind variable
- // then no action need be taken on the part of the adaptor writer. If bind variables
- // are needed the following 3 methods must be implemented in the subclass.
-
- - (NSMutableDictionary *)bindVariableDictionaryForAttribute:(EOAttribute *)attribute value:value;
- // Returns a dictionary to be stored in the EOSQLExpression that contains
- // whatever adaptor specific information is necessary to bind the values.
- // This following keys must have values in the binding dictionary because their
- // values are used by the superclasss: EOBindVariableNameKey, EOBindVariableValueKey,
- // EOBindVariablePlaceHolderKey, and EOBindVariableAttributeKey.
-
- - (BOOL)shouldUseBindVariableForAttribute:(EOAttribute *)att;
- // Returns YES if the the adaptor provides bind variable capability for attributes
- // of this type, no otherwise. Bind variables won't be used for values associated
- // with this attribute when -useBindVariable returns NO. This method should return
- // YES for any attribute that must use bind variables also.
-
- - (BOOL)mustUseBindVariableForAttribute:(EOAttribute *)att;
- // Returns YES if the the adaptor must use bind variable capability for attributes
- // of this type, no otherwise.
-
- // Keys for use in the bindVariableDictionary
- EOACCESS_EXTERN NSString *EOBindVariableNameKey;
- EOACCESS_EXTERN NSString *EOBindVariableAttributeKey;
- EOACCESS_EXTERN NSString *EOBindVariableValueKey;
- EOACCESS_EXTERN NSString *EOBindVariablePlaceHolderKey;
- EOACCESS_EXTERN NSString *EOBindVariableColumnKey;
-
- + (BOOL)useBindVariables;
- // Returns YES if the application is currently using bind variables, NO otherwise.
- // This can be set to YES or NO with the user default named 'EOAdaptorUseBindVariables'
- // in the NSGlobalDomain.
- + (void)setUseBindVariables:(BOOL)yn;
- // Applications can override the user default by invoking this method.
-
- - (NSArray *)bindVariableDictionaries;
- // Returns the current array of binding variable dictionaries.
-
- - (void)addBindVariableDictionary:(NSMutableDictionary *)binding;
- // Add a new BindVariableDictionary to the list maintained by the SQL expression.
- @end
-
- @interface NSString (EOSQLFormatting)
- - (NSString *)sqlString;
- // Returns self
- @end
-
- @interface NSNumber (EOSQLFormatting)
- - (NSString *)sqlString;
- // returns [sefl stringValue];
- @end
-
-