#import "DataBase.h"
#import <PlausibleDatabase/PlausibleDatabase.h>
static PLSqliteDatabase * dbPointer;
@implementation DataBase
//單例
+ (PLSqliteDatabase *) setup{
if (dbPointer) {
return dbPointer;
}
NSLog(@"%@",NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES));
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *realPath = [documentPath stringByAppendingPathComponent:@"book.sqlite"];
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"Book" ofType:@"sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:realPath]) {
NSError *error;
if (![fileManager copyItemAtPath:sourcePath toPath:realPath error:&error]) {
NSLog(@"%@",[error localizedDescription]);
}
}
NSLog(@"複製sqlite到路徑:%@成功。",realPath);
//把dbpointer地址修改爲可修改的realPath。
dbPointer = [[PLSqliteDatabase alloc] initWithPath:realPath];
[dbPointer open];
return dbPointer;
}
+ (void) close{
if (dbPointer) {
[dbPointer close];
dbPointer = NULL;
}
}
@end
//查找table所有書籍的方法
+ (NSArray *) findAll{
PLSqliteDatabase *dataBase = [DataBase setup];
id<PLResultSet> rs;
rs = [dataBase executeQuery:@"SELECT * FROM Book"];
//定義一個數組存放所有書籍的信息
NSMutableArray *books = [[NSMutableArray alloc] init];
//把rs中的數據庫信息遍歷到books數組
while ([rs next]) {
NSString *ID = [rs objectForColumn:@"ID"];
NSString *name = [rs objectForColumn:@"Name"];
NSString *author = [rs objectForColumn:@"Author"];
NSString *publishHouse = [rs objectForColumn:@"PublishHouse"];
NSString *date = [rs objectForColumn:@"Date"];
NSString *briefIntroducation = [rs objectForColumn:@"BriefIntroducation"];
//初始化book 存放到books裏面
Book *book = [[Book alloc] initWithID:ID name:name publishHouse:publishHouse author:author briefIntroducation:briefIntroducation date:date];
[books addObject:book];
[book release];
}
//關閉數據庫
[rs close];
return books;
}
//根據ID搜索相關書籍的方法
+ (id) find:(NSInteger) ID{
PLSqliteDatabase *dataBase = [DataBase setup];
id<PLResultSet> rs;
NSString *findSql = [NSString stringWithFormat:@"select * FROM Book WHERE ID = %d",ID];
rs = [dataBase executeQuery:findSql];
Book *book = nil;
if([rs next]) {
NSString *ID = [rs objectForColumn:@"ID"];
NSString *name = [rs objectForColumn:@"Name"];
NSString *author = [rs objectForColumn:@"Author"];
NSString *publishHouse = [rs objectForColumn:@"PublishHouse"];
NSString *date = [rs objectForColumn:@"Date"];
NSString *briefIntroducation = [rs objectForColumn:@"BriefIntroducation"];
book = [[Book alloc] initWithID:ID name:name publishHouse:publishHouse author:author briefIntroducation:briefIntroducation date:date];
}
else {
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Can not find!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
NSLog(@"Error!!Can not find.");
[errorAlert release];
}
[rs close];
return [book autorelease];
}
//查詢當前有本書的方法
+ (int) count{
PLSqliteDatabase *dataBase = [DataBase setup];
int bookCount = 0;
id<PLResultSet>rs;
rs = [dataBase executeQuery:@"SELECT COUNT(*) AS EventCount FROM Event"];
if ([rs next]) {
bookCount = [[rs objectForColumn:@"BookCount"] intValue];
}
[rs close];
return bookCount;
}
//修改某一本書籍的方法
+ (int) update:(NSString *)ID withName:(NSString *) name publishHouse:(NSString *) publishHouse author:(NSString *) author briefIntroducation:(NSString *)briefIntroducation date:(NSString *) date{
PLSqliteDatabase *dataBase = [DataBase setup];
BOOL bResult = -1;
@try {
[dataBase beginTransaction];
bResult = [dataBase executeUpdate: @"UPDATE Book SET name = ?, publishHouse = ?, author = ?, briefIntroducation = ?, date = ?",
ID,
name,
publishHouse,
author,
briefIntroducation,
date];
[dataBase commitTransaction];
}
@catch (NSException *exception) {
[dataBase rollbackTransaction];
}
@finally {
}
return bResult;
}
//創建新書籍的方法
+ (int) createWithID:(NSString *) ID name:(NSString *) name publishHouse:(NSString *) publishHouse author:(NSString *) author briefIntroducation:(NSString *)briefIntroducation date:(NSString *) date1{
PLSqliteDatabase *dataBase = [DataBase setup];
BOOL bResult = [dataBase executeUpdate: @"INSERT INTO Book (ID, Name, PublishHouse, Author, BriefIntroducation, Date) VALUES (?,?,?,?,?,?)",
ID,
name,
publishHouse,
author,
briefIntroducation,
date1];
return bResult;
}
//刪除書籍的方法
+ (int) delete:(NSString *) ID{
PLSqliteDatabase *dataBase = [DataBase setup];
BOOL bResult = [dataBase executeUpdate:@"DELETE FROM Book WHERE ID = ?",
ID];
return bResult;
}
//書籍這個類的初始化方法
- (id) initWithID:(NSString *) ID name:(NSString *) name publishHouse:(NSString *) publishHouse author:(NSString *) author briefIntroducation:(NSString *)briefIntroducation date:(NSString *) date{
if (self = [super init]) {
_id = [ID retain];
_name = [name retain];
_publishHouse = [publishHouse retain];
_author = [author retain];
_briefIntroducation = [briefIntroducation retain];
_date = [date retain];
}
return self;
}