· 8 years ago · Dec 10, 2017, 06:10 PM
1//
2// ITSConfigModel.m
3// ITSupport
4//
5// Created by Adrian Harding on 03/03/2010.
6// Copyright 2010 First Stop IT. All rights reserved.
7//
8
9#import "ITSConfigModel.h"
10
11@implementation ITSConfigModel
12
13-(id)initWithFilename:(NSString *)filename {
14 [super initWithFilename:filename];
15 [self createTable];
16 return self;
17}
18
19-(void)createTable {
20 if(sqlite3_open([databaseFilename UTF8String], &database) == SQLITE_OK) {
21 sql = "CREATE TABLE IF NOT EXISTS config (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, player_name VARCHAR(255) NOT NULL DEFAULT Player)";
22 result = sqlite3_exec(database, sql, NULL, NULL, &errorMsg);
23 } else {
24 NSAssert1(0, @"createTable open connection failed with message '%s'.", sqlite3_errmsg(database));
25 }
26}
27
28-(void)saveConfigWithConfig: (ITSConfig *) config {
29 if(sqlite3_open([databaseFilename UTF8String], &database) == SQLITE_OK) {
30 sql = "UPDATE config SET player_name = ?";
31
32 if(sqlite3_prepare_v2(database, sql, -1, &statement, nil) == SQLITE_OK) {
33 sqlite3_bind_text(statement, 1, [config.playerName UTF8String], -1, NULL);
34
35 if(sqlite3_step(statement) != SQLITE_DONE) {
36 // Nothing...
37 }
38 sqlite3_finalize(statement);
39 } else {
40 NSAssert1(0, @"saveConfigWithConfig prepare statement failed with message '%s'.", sqlite3_errmsg(database));
41 }
42 } else {
43 NSAssert1(0, @"saveConfigWithConfig open database failed with message '%s'.", sqlite3_errmsg(database));
44 }
45}
46
47-(void)getConfig {
48 data = [[NSMutableArray alloc] init];
49
50 // Open the database connection
51 if(sqlite3_open([databaseFilename UTF8String], &database) == SQLITE_OK) {
52 sql = "SELECT * FROM config LIMIT 1";
53
54 if(sqlite3_prepare_v2(database, sql, -1, &statement, nil) == SQLITE_OK) {
55 while(sqlite3_step(statement) == SQLITE_ROW) {
56 // Read data from the result row
57
58 // Strings need to be checked for null values first
59 NSString *configPlayerName = @"";
60 char *configPlayerNameCString = (char *) sqlite3_column_text(statement, 1);
61
62 // Strings
63 if(configPlayerNameCString) configPlayerName = [[NSString alloc] initWithUTF8String: (char *) configPlayerNameCString];
64
65 // Create a score object and add it to the data array
66 ITSConfig *config = [[ITSConfig alloc] initWithPlayerName: configPlayerName];
67 [data addObject: config];
68 [config release];
69 }
70
71 sqlite3_finalize(statement);
72 } else {
73 NSAssert1(0, @"getConfig prepare statement failed with message '%s'.", sqlite3_errmsg(database));
74 }
75
76 sqlite3_close(database);
77 } else {
78 NSAssert1(0, @"getConfig open database failed with message '%s'.", sqlite3_errmsg(database));
79 }
80}
81
82@end