4 Copyright (C) 1996 Free Software Foundation, Inc.
6 Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
9 This file is part of the GNUstep Database Library.
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Library General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Library General Public License for more details.
21 You should have received a copy of the GNU Library General Public
22 License along with this library; see the file COPYING.LIB.
23 If not, write to the Free Software Foundation,
24 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 // $Id: EOEntity+Factory.m 1 2004-08-20 10:38:46Z znek $
29 #include <GDLAccess/EOEntity+Factory.h>
30 #include <GDLAccess/EOAttribute.h>
31 #include <EOControl/EONull.h>
32 #include <EOControl/EOKeyValueCoding.h>
35 @interface NSObject(PKeyInitializer)
36 - (id)initWithPrimaryKey:(NSDictionary *)_pkey entity:(EOEntity *)_entity;
39 @implementation EOEntity(AttributeNames)
41 - (NSArray *)attributeNames {
42 NSMutableArray *attrNames = [[[NSMutableArray alloc] init] autorelease];
43 NSEnumerator *attrs = [self->attributes objectEnumerator];
44 EOAttribute *attr = nil;
46 while ((attr = [attrs nextObject]))
47 [attrNames addObject:[attr name]];
52 @end /* EOEntity(AttributeNames) */
54 @implementation EOEntity(PrimaryKeys)
56 - (BOOL)isPrimaryKeyAttribute:(EOAttribute *)_attribute {
57 NSEnumerator *pkeys = [self->primaryKeyAttributeNames objectEnumerator];
58 NSString *aname = [_attribute name];
61 while ((n = [pkeys nextObject])) {
62 if ([aname isEqualToString:n])
68 - (unsigned)primaryKeyCount {
69 return [self->primaryKeyAttributeNames count];
72 @end /* EOEntity(PrimaryKeys) */
74 @implementation EOEntity(ObjectFactory)
76 - (id)produceNewObjectWithPrimaryKey:(NSDictionary *)_key {
77 /* Note: used by LSDBObjectNewCommand */
78 Class objectClass = Nil;
81 objectClass = NSClassFromString([self className]);
82 NSAssert(objectClass != nil, @"no enterprise object class set in entity");
84 obj = [objectClass alloc];
85 NSAssert(objectClass != nil, @"could not allocate enterprise object");
87 if ([obj respondsToSelector:@selector(initWithPrimaryKey:entity:)])
88 [obj initWithPrimaryKey:_key entity:self];
92 return AUTORELEASE(obj);
95 - (void)setAttributesOfObjectToEONull:(id)_object {
96 static EONull *null = nil;
102 null = [[NSNull null] retain];
104 attrs = [self->attributes objectEnumerator];
106 pkeyCount = [self->primaryKeyAttributeNames count];
108 NSAssert(NSClassFromString([self className]) == [_object class],
109 @"object does not belong to entity");
111 while ((attr = [attrs nextObject])) {
113 if ([self isPrimaryKeyAttribute:attr]) {
119 [_object takeValue:null forKey:[attr name]];
123 @end /* EOEntity(ObjectFactory) */