Skip to content

Instantly share code, notes, and snippets.

@xinmyname
Created March 3, 2013 20:46
Show Gist options
  • Select an option

  • Save xinmyname/5078209 to your computer and use it in GitHub Desktop.

Select an option

Save xinmyname/5078209 to your computer and use it in GitHub Desktop.
FMDatabase factory class. Handles the creation of an FMDatabase object. Creates a new database from a template resource if necessary.
//
// FMDatabaseFactory.m
//
// Copyright (c) 2012 Andy Sherwood. All rights reserved.
//
#import "FMDatabaseFactory.h"
#import <FMDatabase.h>
@implementation FMDatabaseFactory
{
NSString* _dbFileName;
NSFileManager* _fileManager;
NSSearchPathDirectory _searchPath;
}
- (id)initWithResourceFileName:(NSString*)dbFileName
searchPath:(NSSearchPathDirectory)searchPath
andFileManager:(NSFileManager*)fileManager
{
self = [super init];
if (self)
{
_dbFileName = [dbFileName copy];
_searchPath = searchPath;
_fileManager = fileManager;
}
return self;
}
- (FMDatabase*)openDatabase
{
NSError* error;
NSString* path = [self databasePath];
if (![_fileManager fileExistsAtPath:path])
{
NSLog(@"Deploying fresh database to: %@", path);
BOOL success = [_fileManager copyItemAtPath:[self freshDatabasePath]
toPath:path
error:&error];
if (!success)
@throw [NSException exceptionWithName:@"Failed to create writeable database"
reason:[error localizedDescription]
userInfo:nil];
}
FMDatabase* db = [FMDatabase databaseWithPath:path];
[db open];
return db;
}
- (void)dropDatabase
{
NSLog(@"Dropping database");
NSError* error;
NSString* path = [self databasePath];
if ([_fileManager fileExistsAtPath:path])
{
BOOL success = [_fileManager removeItemAtPath:path
error:&error];
if (!success)
@throw [NSException exceptionWithName:@"Failed to remove database"
reason:[error localizedDescription]
userInfo:nil];
}
}
- (void)backupDatabase
{
NSLog(@"Backing up database");
NSError* error;
NSString* path = [self databasePath];
NSString* backupPath = [self backupDatabasePath];
if ([_fileManager fileExistsAtPath:backupPath])
{
if (![_fileManager removeItemAtPath:backupPath error:&error])
@throw [NSException exceptionWithName:@"Failed to delete backup database"
reason:[error localizedDescription]
userInfo:nil];
}
if ([_fileManager fileExistsAtPath:path])
{
if (![_fileManager copyItemAtPath:path toPath:backupPath error:&error])
@throw [NSException exceptionWithName:@"Failed to backup database"
reason:[error localizedDescription]
userInfo:nil];
}
else
@throw [NSException exceptionWithName:@"Database does not exist" reason:[error localizedDescription] userInfo:nil];
}
- (void)restoreDatabase
{
NSLog(@"Restoring database");
NSError* error;
NSString* path = [self databasePath];
NSString* backupPath = [self backupDatabasePath];
if ([_fileManager fileExistsAtPath:path])
{
if (![_fileManager removeItemAtPath:path error:&error])
@throw [NSException exceptionWithName:@"Failed to delete database" reason:[error localizedDescription] userInfo:nil];
}
if ([_fileManager fileExistsAtPath:backupPath])
{
if (![_fileManager copyItemAtPath:backupPath toPath:path error:&error])
@throw [NSException exceptionWithName:@"Failed to restore database" reason:[error localizedDescription] userInfo:nil];
}
else
@throw [NSException exceptionWithName:@"Backup database does not exist" reason:[error localizedDescription] userInfo:nil];
}
- (NSString*)databasePath
{
NSArray* paths = NSSearchPathForDirectoriesInDomains(_searchPath, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:_dbFileName];
}
- (NSString*)freshDatabasePath
{
return [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:_dbFileName];
}
- (NSString*)backupDatabasePath
{
NSString* path = [self databasePath];
NSString* suffix = @"~backup";
NSString* containingFolder = [path stringByDeletingLastPathComponent];
NSString* fullFileName = [path lastPathComponent];
NSString* fileExtension = [fullFileName pathExtension];
NSString* fileName = [fullFileName stringByDeletingPathExtension];
NSString* newFileName = [fileName stringByAppendingString:suffix];
NSString* newFullFileName = [newFileName stringByAppendingPathExtension:fileExtension];
return [containingFolder stringByAppendingPathComponent:newFullFileName];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment