-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalendarDao.m
executable file
·122 lines (103 loc) · 3.39 KB
/
CalendarDao.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// CalendarDao.m
// TradeShowInfo
//
// Created by brightcreek on 3/30/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "CalendarDao.h"
#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
#import "Calendar.h"
#import "Hints.h"
@implementation CalendarDao
-(NSString *)setTable:(NSString *)sql{
return [NSString stringWithFormat:sql, @"Calendar"];
}
// SELECT
-(Calendar *)getCalendarById:(int)index{
FMResultSet *rs = [db executeQuery:[self setTable:@"SELECT * FROM %@ WHERE id = ?"], [NSNumber numberWithInt:index]];
if ([rs next]) {
Calendar *c = [[[Calendar alloc]
initWithIndex:[rs intForColumn:@"id"]
Title:[rs stringForColumn:@"title"]
Importance:[rs intForColumn:@"importance"]
Begin:[rs stringForColumn:@"begin"]
End:[rs stringForColumn:@"end"]
Notes:[rs stringForColumn:@"notes"]]
autorelease];
[rs close];
return c;
}
return nil;
}
// SELECT
-(NSMutableArray *)select:(int)sortIndex{
NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:0];
NSString *sql = nil;
if(sortIndex==0){
sql = @"SELECT * FROM Calendar order by begin";
}else {
sql = @"SELECT * FROM Calendar order by importance";
}
FMResultSet *rs = [db executeQuery:sql];
while ([rs next]) {
Calendar *c = [[Calendar alloc]
initWithIndex:[rs intForColumn:@"id"]
Title:[rs stringForColumn:@"title"]
Importance:[rs intForColumn:@"importance"]
Begin:[rs stringForColumn:@"begin"]
End:[rs stringForColumn:@"end"]
Notes:[rs stringForColumn:@"notes"]
];
[result addObject:c];
//[c release];
}
[rs close];
return result;
}
// INSERT
-(void)insertWithTitle:(NSString *)title Importance:(int)importance Begin:(NSString *)begin End:(NSString *)end Notes:(NSString *)notes{
[db executeUpdate:[self setTable:@"INSERT INTO %@ (title, importance, begin, end, notes) VALUES (?,?,?,?,?)"], title, [NSNumber numberWithInt:importance], begin, end, notes];
if ([db hadError]) {
NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
}
}
// UPDATE
-(BOOL)updateAt:(int)index Title:(NSString *)title Importance:(int)importance Begin:(NSString *)begin End:(NSString *)end Notes:(NSString *)notes{
BOOL success = YES;
[db executeUpdate:[self setTable:@"UPDATE %@ SET title=?, importance=?, begin=?, end=?, notes=? WHERE id=?"], title, [NSNumber numberWithInt:importance], begin, end, notes, [NSNumber numberWithInt:index]];
if ([db hadError]) {
NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
success = NO;
}
return success;
}
// DELETE
- (BOOL)deleteAt:(int)index{
BOOL success = YES;
[db executeUpdate:[self setTable:@"DELETE FROM %@ WHERE id = ?"], [NSNumber numberWithInt:index]];
if ([db hadError]) {
NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
success = NO;
}
return success;
}
-(Hints *)getHintsById:(int)index{
FMResultSet *rs = [db executeQuery:@"SELECT * FROM Hints WHERE id = 2"];
// FMResultSet *rs = [db executeQuery:[self setTable:@"SELECT * FROM %@ WHERE id = ?"], [NSNumber numberWithInt:index]];
if ([rs next]) {
Hints *h = [[[Hints alloc] initWithIndex:[rs intForColumn:@"id"]
Name:[rs stringForColumn:@"name"]
Show:[rs stringForColumn:@"show"]
TsId:[rs intForColumn:@"tsId"]]
autorelease];
[rs close];
return h;
}
return nil;
}
- (void)dealloc {
[super dealloc];
}
@end