-
Notifications
You must be signed in to change notification settings - Fork 315
/
Copy pathDatabasesArrayController.m
85 lines (76 loc) · 2.52 KB
/
DatabasesArrayController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// DatabasesArrayCollection.m
// MongoHub
//
// Created by Syd on 10-4-25.
// Copyright 2010 MusicPeace.ORG. All rights reserved.
//
#import "DatabasesArrayController.h"
#import "Connection.h"
#import "Database.h"
@implementation DatabasesArrayController
- (void)awakeFromNib
{
if ([NSArrayController instancesRespondToSelector:@selector(awakeFromNib)])
{
[super awakeFromNib];
}
[self setClearsFilterPredicateOnInsertion:NO];
}
- (id)newObjectWithConn:(Connection *) conn name:(NSString *)name user:(NSString *)user password:(NSString *)password
{
id newObj = [super newObject];
[newObj setValue:conn forKey:@"connection"];
[newObj setValue:name forKey:@"name"];
[newObj setValue:user forKey:@"user"];
[newObj setValue:password forKey:@"password"];
return newObj;
}
- (void)clean:(Connection *)conn databases:(NSArray *)databases
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"connection=%@", conn];
NSArray *dblist = [self itemsUsingFetchPredicate:predicate];
for (Database *db in dblist) {
bool exist = false;
for (NSString *d in databases) {
if (db.name == d) {
exist = true;
break;
}
}
if (!exist) {
[super remove:db];
}
}
}
- (Database *)dbInfo:(Connection *) conn name:(NSString *)name
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"connection=%@ AND name=%@", conn, name];
if ([[self itemsUsingFetchPredicate:predicate] count]>0) {
return [[self itemsUsingFetchPredicate:predicate] objectAtIndex:0];
}
return nil;
}
- (BOOL)checkDuplicate:(Connection *) conn name:(NSString *)name
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"connection=%@ AND name=%@", conn, name];
if ([[self itemsUsingFetchPredicate:predicate] count]>0) {
return YES;
}else {
return NO;
}
}
- (NSArray *)itemsUsingFetchPredicate:(NSPredicate *)fetchPredicate
{
NSError *error = nil;
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:[NSEntityDescription entityForName:[self entityName]
inManagedObjectContext:[self managedObjectContext]]];
NSArray *objects = [[self managedObjectContext]
executeFetchRequest:request error:&error];
if (error) {
NSLog(@"Fetch error! In AWViewPositionArrayController:itemsUseingFetchPredicate");
}
return [objects filteredArrayUsingPredicate:fetchPredicate];
}
@end