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
|
//
// Topic.m
// acidcow
//
// Created by Matthew Handler on 4/16/11.
// Copyright 2011 Earl Industries. All rights reserved.
//
#import "Topic.h"
#import "FMDatabase.h"
#import "FMResultSet.h"
@implementation Topic
@synthesize guid, title, iconUrl, description, releaseDate, category, picCount;
+ (Topic *) initFromDatabaseRow:(FMResultSet *)result {
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"US"] autorelease]];
Topic *topic = [[[Topic alloc] init] autorelease];
topic.guid = [result stringForColumn:@"link"];
topic.title = [result stringForColumn:@"title"];
topic.iconUrl = [result stringForColumn:@"iconUrl"];
topic.description = [result stringForColumn:@"description"];
topic.releaseDate = [Topic stringToDate:[result stringForColumn:@"releaseDate"]];
topic.category = [result stringForColumn:@"category"];
topic.picCount = [result intForColumn:@"picCount"];
return topic;
}
+ (NSString *) dateToString:(NSDate *)date {
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"US"] autorelease]];
return [dateFormatter stringFromDate:date];
}
+ (NSDate *) stringToDate:(NSString *)string {
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"US"] autorelease]];
return [dateFormatter dateFromString:string];
}
- (void)serializeToDatabase:(FMDatabase *)db {
[db retain];
NSLog(@"title: %@", title);
[db executeUpdate:@"INSERT INTO picCasts \
(foreignId, link, title, description, releaseDate, category, iconUrl, picCount) \
VALUES \
(?, ?, ?, ?, ?, ?, ?, ?)",
[NSNumber numberWithInt:12], guid, title, description, [Topic dateToString:releaseDate], category, iconUrl, [NSNumber numberWithInt:12]];
//NSLog(@"insert id:%d error: %@", [db lastInsertRowId], [db lastErrorMessage]);
// [NSString stringWithFormat:@"number %d", i],
// [NSNumber numberWithInt:i],
// [NSDate date],
// [NSNumber numberWithFloat:2.2f]];
[db release];
}
- (void)dealloc {
[picCount release];
[guid release];
[title release];
[description release];
[releaseDate release];
[category release];
[iconUrl release];
[super dealloc];
}
@end
|