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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| #import "Database.h"
// Static Statement
static sqlite3_stmt* statement = nil;
@implementation Database
- (id) init {
self = [super init];
if (self != nil) {
is_opened = NO;
[self open];
}
return self;
}
- (void) dealloc {
[self close];
[super dealloc];
}
#pragma mark - Close & Open Database
- (void) close {
if (sqlite3_close(database) == SQLITE_OK) {
DLOG( (@"Database::close: No problem") );
database = nil;
} else {
DLOG( (@"Database::close: Error: %s", sqlite3_errmsg(database)) );
// Force
sqlite3_finalize(statement);
statement = nil;
if (sqlite3_close(database) == SQLITE_OK) {
DLOG( (@"Database::close (Second try): No problem") );
database = nil;
} else {
DLOG( (@"Database::close (Second try): Error: %s", sqlite3_errmsg(database)) );
}
}
is_opened = NO;
}
- (void) open {
// if (is_opened)
// return;
is_opened = YES;
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentDirectory = [paths objectAtIndex:0];
NSString* path = [documentDirectory stringByAppendingPathComponent:@"database.sqlite"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
DLOG( (@"Database::open: No problem") );
} else {
DLOG( (@"Database::open: Error: %s", sqlite3_errmsg(database)) );
}
}
#pragma mark - Requests
- (void) clean_user_datas {
NSString* request = nil;
SET_STRING(request, @"UPDATE User_Informations SET screenshot=?, message_screenshot=?, message=?, url=?, latitude=?, longitude=?, heading=?, pitch=? WHERE id=1")
if (sqlite3_prepare_v2(database, [request UTF8String], -1, &statement, NULL) == SQLITE_OK) {
char c = '\0';
sqlite3_bind_blob(statement, 1, &c, 1, SQLITE_STATIC);
sqlite3_bind_blob(statement, 2, &c, 1, SQLITE_STATIC);
sqlite3_bind_text(statement, 3, &c, 1, SQLITE_STATIC);
sqlite3_bind_text(statement, 4, &c, 1, SQLITE_STATIC);
sqlite3_bind_double(statement, 5, 0.0f);
sqlite3_bind_double(statement, 6, 0.0f);
sqlite3_bind_double(statement, 7, 0.0f);
sqlite3_bind_double(statement, 8, 0.0f);
if (SQLITE_DONE != sqlite3_step(statement)) {
DLOG( (@"Database::clean_database - error %s", sqlite3_errmsg(database)) );
} else {
DLOG( (@"Database::clean_database - ok") );
}
} else {
DLOG( (@"Database::clean_database - error %s", sqlite3_errmsg(database)) );
}
sqlite3_finalize(statement);
statement = nil;
}
- (void) get_facebook_friends_list:(NSMutableArray*) friends_list {
if (friends_list == nil) { return; }
[friends_list removeAllObjects];
Facebook_Friend* friend = nil;
NSString* request = nil;
SET_STRING(request, @"SELECT id, name FROM Facebook_Friends")
if (sqlite3_prepare_v2(database, [request UTF8String], -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
friend = [[Facebook_Friend alloc] init_friend];
if (friend == nil) { continue; }
[friend set_id:[NSString stringWithCString:(const char*) sqlite3_column_text(statement, 0) encoding:NSASCIIStringEncoding]];
[friend set_name:[NSString stringWithCString:(const char*) sqlite3_column_text(statement, 1) encoding:NSUTF8StringEncoding]];
if ((friend->name != nil) && (friend->friend_id != nil)) {
[friends_list addObject:friend];
DLOG( (@"Database - Get facebook friend:%@ %@]", friend->name, friend->friend_id) )
}
RELEASE(friend)
}
} else {
DLOG( (@"Database::get_facebook_friends - error %s", sqlite3_errmsg(database)) );
}
sqlite3_finalize(statement);
statement = nil;
}
- (void) save_facebook_friends_list:(NSArray*) friends_list {
if ((friends_list == nil) || ([friends_list count] < 1)) { return; }
NSString* friend_id = nil;
NSString* name = nil;
NSString* request = nil;
SET_STRING(request, @"INSERT INTO Facebook_Friends('id', 'name') VALUES (?, ?)")
char * c_name = nil;
for (Facebook_Friend* friend in friends_list) {
friend_id = friend->friend_id;
name = friend->name;
if ((friend_id == nil) || (name == nil)) {
friend_id = nil;
name = nil;
continue;
}
if (sqlite3_prepare_v2(database, [request UTF8String], -1, &statement, NULL) == SQLITE_OK) {
sqlite3_bind_text(statement, 1, [friend_id cStringUsingEncoding:NSASCIIStringEncoding], [friend_id length], SQLITE_STATIC);
c_name = (char*) [name cStringUsingEncoding:NSUTF8StringEncoding];
sqlite3_bind_text(statement, 2, c_name, strlen(c_name), SQLITE_STATIC);
switch ( sqlite3_step(statement) ) {
case SQLITE_DONE:
DLOG( (@"Database - Save facebook friend:%@ %@]", name, friend_id) )
break;
default:
break;
}
} else {
DLOG( (@"Database::save_facebook_friends_list - error %s", sqlite3_errmsg(database)) );
}
sqlite3_finalize(statement);
statement = nil;
friend_id = nil;
name = nil;
}
} |
Partager