Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Amplitude/AMPDatabaseHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

@property (nonatomic, strong, readonly) NSString *databasePath;

+ (AMPDatabaseHelper*)getDatabaseHelper;
+ (AMPDatabaseHelper*)getDatabaseHelper:(NSString*) instanceName;
+ (AMPDatabaseHelper*)getDatabaseHelper:(NSString*) instanceName apiKey:(NSString*) apiKey;
+ (AMPDatabaseHelper*)getDatabaseHelperWithInstanceName:(NSString*) instanceName; // for testing only
- (BOOL)createTables;
- (BOOL)dropTables;
- (BOOL)upgrade:(int) oldVersion newVersion:(int) newVersion;
Expand Down
33 changes: 17 additions & 16 deletions Amplitude/AMPDatabaseHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ @interface AMPDatabaseHelper()

@implementation AMPDatabaseHelper
{
BOOL _databaseCreated;
sqlite3 *_database;
dispatch_queue_t _queue;
}
Expand Down Expand Up @@ -70,12 +69,7 @@ @implementation AMPDatabaseHelper
static NSString *const GET_VALUE = @"SELECT %@, %@ FROM %@ WHERE %@ = ?;";


+ (AMPDatabaseHelper*)getDatabaseHelper
{
return [AMPDatabaseHelper getDatabaseHelper:nil];
}

+ (AMPDatabaseHelper*)getDatabaseHelper:(NSString*) instanceName
+ (AMPDatabaseHelper*)getDatabaseHelper:(NSString*) instanceName apiKey:(NSString*) apiKey
{
static NSMutableDictionary *_instances = nil;
static dispatch_once_t onceToken;
Expand All @@ -92,32 +86,39 @@ + (AMPDatabaseHelper*)getDatabaseHelper:(NSString*) instanceName
@synchronized(_instances) {
dbHelper = [_instances objectForKey:instanceName];
if (dbHelper == nil) {
dbHelper = [[AMPDatabaseHelper alloc] initWithInstanceName:instanceName];
dbHelper = [[AMPDatabaseHelper alloc] initWithInstanceName:instanceName andApiKey:apiKey];
[_instances setObject:dbHelper forKey:instanceName];
SAFE_ARC_RELEASE(dbHelper);
}
}
return dbHelper;
}

- (id)init
// for testing only
+ (AMPDatabaseHelper*)getDatabaseHelperWithInstanceName:(NSString*) instanceName
{
return [self initWithInstanceName:nil];
AMPDatabaseHelper *dbHelper = [[AMPDatabaseHelper alloc] initWithInstanceName:instanceName andApiKey:nil];
return SAFE_ARC_AUTORELEASE(dbHelper);
}

- (id)initWithInstanceName:(NSString*) instanceName
// instanceName should not be null, getDatabaseHelper will guard
// apiKey should only be null for testing - Amplitude client will guard
- (id)initWithInstanceName:(NSString*) instanceName andApiKey:(NSString*) apiKey
{
if ([AMPUtils isEmptyString:instanceName]) {
instanceName = kAMPDefaultInstance;
}
instanceName = [instanceName lowercaseString];

if ((self = [super init])) {
NSString *databaseDirectory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
NSString *databasePath = [databaseDirectory stringByAppendingPathComponent:@"com.amplitude.database"];
if (![instanceName isEqualToString:kAMPDefaultInstance]) {
databasePath = [NSString stringWithFormat:@"%@_%@", databasePath, instanceName];
}

// migrate to new db filename
if (![AMPUtils isEmptyString:apiKey]) {
NSString *newDatabasePath = [NSString stringWithFormat:@"%@_%@", databasePath, apiKey];
[AMPUtils moveFileIfNotExists:databasePath to:newDatabasePath];
databasePath = newDatabasePath;
}

_databasePath = SAFE_ARC_RETAIN(databasePath);
_queue = dispatch_queue_create([QUEUE_NAME UTF8String], NULL);
dispatch_queue_set_specific(_queue, kDispatchQueueKey, (__bridge void *)self, NULL);
Expand Down
1 change: 1 addition & 0 deletions Amplitude/AMPUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
+ (id) makeJSONSerializable:(id) obj;
+ (BOOL) isEmptyString:(NSString*) str;
+ (NSDictionary*) validateGroups:(NSDictionary*) obj;
+ (BOOL)moveFileIfNotExists:(NSString*)from to:(NSString*)to;

@end
17 changes: 17 additions & 0 deletions Amplitude/AMPUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,21 @@ + (NSDictionary *) validateGroups:(NSDictionary *) obj
return [NSDictionary dictionaryWithDictionary:dict];
}

+ (BOOL)moveFileIfNotExists:(NSString*)from to:(NSString*)to
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
if (![fileManager fileExistsAtPath:to] &&
[fileManager fileExistsAtPath:from]) {
if ([fileManager copyItemAtPath:from toPath:to error:&error]) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use moveItemAtPath?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

AMPLITUDE_LOG(@"INFO: copied %@ to %@", from, to);
[fileManager removeItemAtPath:from error:NULL];
} else {
AMPLITUDE_LOG(@"WARN: Copy from %@ to %@ failed: %@", from, to, error);
return NO;
}
}
return YES;
}

@end
Loading