]> err.no Git - sope/blob - sope-core/EOCoreData/EOFetchSpecification+CoreData.m
improved kv unarchiving
[sope] / sope-core / EOCoreData / EOFetchSpecification+CoreData.m
1 /*
2   Copyright (C) 2005 SKYRIX Software AG
3   
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "EOFetchSpecification+CoreData.h"
23 #include "EOSortOrdering+CoreData.h"
24 #include "EOQualifier+CoreData.h"
25 #include "common.h"
26
27
28 @implementation EOFetchSpecification(CoreData)
29
30 - (id)initWithFetchRequest:(NSFetchRequest *)_fr {
31   NSMutableArray *so;
32   EOQualifier *q;
33   NSArray     *sd;
34   unsigned    count;
35
36   if (_fr == nil) {
37     [self release];
38     return nil;
39   }
40
41   /* convert sort descriptors */
42   
43   sd = [_fr sortDescriptors];
44   so = nil;
45   if ((count = [sd count]) > 0) {
46     unsigned i;
47     
48     so = [[NSMutableArray alloc] initWithCapacity:count];
49     for (i = 0; i < count; i++) {
50       EOSortOrdering *soo;
51
52       soo = [[EOSortOrdering alloc] initWithSortDescriptor:
53                                       [sd objectAtIndex:i]];
54       if (soo == nil) {
55         soo = [sd objectAtIndex:i]; /* oh well, this is sneaky */
56         NSLog(@"WARNING(%s): could not convert NSSortDescriptor to "
57               @"EOSortOrdering: %@", __PRETTY_FUNCTION__, soo);
58       }
59       [so addObject:soo];
60       [soo release];
61     }
62   }
63   
64   /* convert predicate */
65   
66   q = [EOQualifier qualifierForPredicate:[_fr predicate]];
67   
68   /* create object */
69   
70   // TODO: maybe add 'affectedStores' as a hint?
71   self = [self initWithEntityName:[[_fr entity] name]
72                qualifier:q sortOrderings:so
73                usesDistinct:YES isDeep:NO
74                hints:nil];
75   [so release]; so = nil;
76   
77   [self setFetchLimit:[_fr fetchLimit]];
78   return self;
79 }
80
81 - (NSArray *)sortOrderingsAsSortDescriptors {
82   NSMutableArray *ma;
83   NSArray  *a;
84   unsigned i, count;
85   
86   if ((a = [self sortOrderings]) == nil)
87     return nil;
88   if ((count = [a count]) == 0)
89     return nil;
90   
91   if (count == 1) /* common, optimization */
92     return [NSArray arrayWithObject:[[a objectAtIndex:0] asSortDescriptor]];
93   
94   ma = [NSMutableArray arrayWithCapacity:count];
95   for (i = 0; i < count; i++)
96     [ma addObject:[[a objectAtIndex:i] asSortDescriptor]];
97   return ma;
98 }
99
100 - (NSFetchRequest *)fetchRequestWithEntity:(NSEntityDescription *)_entity {
101   NSFetchRequest *fr;
102   
103   fr = [[[NSFetchRequest alloc] init] autorelease];
104   [fr setEntity:_entity];
105   [fr setFetchLimit:[self fetchLimit]];
106   [fr setPredicate:[[self qualifier] asPredicate]];
107   [fr setSortDescriptors:[self sortOrderingsAsSortDescriptors]];
108   return fr;
109 }
110
111 - (NSFetchRequest *)fetchRequestWithModel:(NSManagedObjectModel *)_model {
112   NSEntityDescription *entity;
113   NSString *s;
114   
115   entity = ((s = [self entityName]) != nil)
116     ? [[_model entitiesByName] objectForKey:s]
117     : nil;
118   
119   return [self fetchRequestWithEntity:entity];
120 }
121
122 @end /* EOFetchSpecification(CoreData) */
123
124
125 @implementation NSFetchRequest(EOCoreData)
126
127 - (NSFetchRequest *)fetchRequestWithEntity:(NSEntityDescription *)_entity {
128   return self;
129 }
130 - (NSFetchRequest *)fetchRequestWithModel:(NSManagedObjectModel *)_model {
131   return self;
132 }
133
134 @end /* NSFetchRequest(EOCoreData) */