DBAction.m 9.09 KB
//
//  DBAction.m
//  SCIOLive
//
//  Created by wei on 14-9-29.
//  Copyright (c) 2014年 Miao Long. All rights reserved.
//

#import "DBAction.h"
#import "FMDatabase.h"
FMDatabase * db = nil;
static DBAction *instance = nil;
@implementation DBAction

+ (DBAction *)sharedDBAction{
    if (instance == nil){
        instance = [[super allocWithZone:nil]init];
    }
    return instance;
}

+(id)allocWithZone:(NSZone *)zone{
    return [DBAction sharedDBAction];
}

//可写可不写
- (id)init{
    self = [super init];
    if (self) {
        db = [[FMDatabase alloc] initWithPath:SQLITE_PATH];
    }
    return self;
}

-(id)copy{
    return self;
}

- (id)copyWithZone:(NSZone *)zone{
    return self;
}

-(void)createNoticeTable{
    if ([db open]) {
        NSString * sql = @"CREATE TABLE NOTICE (time varchar, title varchar, content varchar)";
        BOOL res = [db executeUpdate:sql];
        if (!res) {
            NSLog(@"建表失败");
        } else {
            NSLog(@"建表成功");
        }
        [db close];
    } else {
        NSLog(@"打开失败");
    }
}

- (BOOL)isInNoticeTable:(NoticeData *)notice{
    if ([db open]) {
        NSString * check = [NSString stringWithFormat:@"SELECT * FROM NOTICE where time = %@",notice.time];
        FMResultSet * re = [db executeQuery:check];
        if ([re next]) {
            return YES;
        }
        [db close];
    }
    return NO;
}

- (void)insertNoticeTable:(NoticeData*)notice{
    if ([db open]) {
        NSString * check = [NSString stringWithFormat:@"SELECT * FROM NOTICE where time = %@",notice.time];
        FMResultSet * re = [db executeQuery:check];
        if ([re next]) {
            NSLog(@"已存在,进行更新操作");
            NSString * sql = [NSString stringWithFormat:@"UPDATE NOTICE SET title = '%@', content = '%@' WHERE time = %@'",notice.time,notice.title,notice.content];
            BOOL res = [db executeUpdate:sql];
            if (!res) {
                NSLog(@"error to insert data");
            } else {
                NSLog(@"succ to insert data");
            }
        }else{
            NSString * sql = [NSString stringWithFormat:@"INSERT INTO NOTICE (time, title, content) values('%@', '%@', '%@')",notice.time,notice.title,notice.content];
            BOOL res = [db executeUpdate:sql];
            if (!res) {
                NSLog(@"error to insert data");
            } else {
                NSLog(@"succ to insert data");
            }
        }
        [db close];
    }
}

- (void)deleteNotice:(NoticeData*)notice{
    if ([db open]) {
        NSString * sql = [NSString stringWithFormat:@"DELETE FROM NOTICE where time = %@",notice.time];
        BOOL res = [db executeUpdate:sql];
        if (!res) {
            NSLog(@"error to delete db data");
        } else {
            NSLog(@"succ to deleta db data");
        }
        [db close];
    }
}

- (NSMutableArray *)queryNoticeTable{
    NSMutableArray *list = [[NSMutableArray alloc] init];
    if ([db open]) {
        NSString * sql = @"SELECT * FROM NOTICE";
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            NoticeData * notice = [[NoticeData alloc] init];
            notice.time  = [rs stringForColumn:@"time"];
            notice.title  = [rs stringForColumn:@"title"];
            notice.content  = [rs stringForColumn:@"content"];

            [list addObject:notice];
        }
        [db close];
    }
    return list;
}

- (void)createTable:(NSString *)tableName{
    if ([db open]) {
        tableName = [tableName substringToIndex:2];
        NSString * sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@SDATATABLE (cmsColumnName varchar, cmsColumnId varchar, cmsContentId int, createDate varchar, desc varchar, gxbArticleId int, gxbVideoId int, img varchar,imgSmall varchar, pageUrl varchar, picType varchar, playType varchar, publishDate varchar, subTitle varchar, title varchar, type varchar, uploadVideoUrl varchar, videoUrl varchar, vipFlag int, uuid varchar,author varchar)",tableName.uppercaseString];
        BOOL res = [db executeUpdate:sql];
        if (!res) {
            NSLog(@"建表失败");
            
            NSString *sql1 = [NSString stringWithFormat:@"ALTER TABLE %@SDATATABLE ADD %@ VARCHAR",tableName,@"playType"];
            BOOL result1 = [db executeUpdate:sql1];
            
            NSString *sql2 = [NSString stringWithFormat:@"ALTER TABLE %@SDATATABLE ADD %@ VARCHAR",tableName,@"uuid"];
            BOOL result2 = [db executeUpdate:sql2];
            
        } else {
            NSLog(@"建表成功");
        }
        [db close];
    } else {
        NSLog(@"打开失败");
    }
}

- (BOOL)isInTable:(NSString *)tableName notice:(TableData*)notice{
    if ([db open]) {
        tableName = [tableName substringToIndex:2];
        NSString * check = [NSString stringWithFormat:@"SELECT * FROM %@SDATATABLE where cmsContentId = %d",tableName.uppercaseString,notice.cmsContentId];
        FMResultSet * re = [db executeQuery:check];
        while ([re next])  {
            return YES;
        }
        [db close];
    }
    return NO;
}

- (BOOL)insertTable:(NSString *)tableName notice:(TableData*)notice{
    if ([db open]) {
        tableName = [tableName substringToIndex:2];
        NSString * check = [NSString stringWithFormat:@"SELECT * FROM %@SDATATABLE where cmsContentId = %d",tableName.uppercaseString,notice.cmsContentId];
        FMResultSet * re = [db executeQuery:check];
        if (![re next]) {
            NSString *desc = [notice.desc stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
            NSString *title = [notice.title stringByReplacingOccurrencesOfString:@"'" withString:@"‘"];
            NSString * sql = [NSString stringWithFormat:@"INSERT INTO %@SDATATABLE (cmsColumnName, cmsColumnId, cmsContentId, createDate, desc, gxbArticleId, gxbVideoId, img, imgSmall, pageUrl, picType, playType, publishDate, subTitle, title, type, uploadVideoUrl, videoUrl, vipFlag, uuid,author) values('%@','%@','%d', '%@','%@','%d','%d', '%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%d', '%@','%@')",tableName.uppercaseString, notice.cmsColumnName, notice.cmsColumnId, notice.cmsContentId, notice.createDate, desc, notice.gxbArticleId,  notice.gxbVideoId, notice.img, notice.imgSmall, notice.pageUrl, notice.picType, notice.playType, notice.publishDate, notice.subTitle, title, notice.type, notice.uploadVideoUrl, notice.videoUrl, notice.vipFlag, notice.uuid?notice.uuid:@"",notice.author];
            BOOL res = [db executeUpdate:sql];
            if (!res) {
                return NO;
            } else {
                return YES;
            }
        }
        [db close];
    }
    return NO;
}

- (BOOL)deleteTable:(NSString *)tableName notice:(TableData*)notice{
    if ([db open]) {
        tableName = [tableName substringToIndex:2];
        NSString * sql = [NSString stringWithFormat:@"DELETE FROM %@SDATATABLE where cmsContentId = %d",tableName.uppercaseString,notice.cmsContentId];
        BOOL res = [db executeUpdate:sql];
        if (!res) {
            return NO;
        } else {
            return YES;
        }
        [db close];
    }
    return NO;
}

- (NSMutableArray *)queryTable:(NSString *)tableName{
    NSMutableArray *list = [[NSMutableArray alloc] init];
    if ([db open]) {
        tableName = [tableName substringToIndex:2];
        NSString *sql = [NSString  stringWithFormat:@"SELECT * FROM %@SDATATABLE",tableName.uppercaseString];
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            TableData * notice = [[TableData alloc] init];
            notice.cmsColumnName = [rs stringForColumn:@"cmsColumnName"];
            notice.cmsColumnId   = [rs stringForColumn:@"cmsColumnId"];
            notice.cmsContentId  = [rs    intForColumn:@"cmsContentId"];
            notice.createDate    = [rs stringForColumn:@"createDate"];
            notice.desc          = [rs stringForColumn:@"desc"];
            notice.gxbArticleId  = [rs    intForColumn:@"gxbArticleId"];
            notice.gxbVideoId    = [rs    intForColumn:@"gxbVideoId"];
            notice.img           = [rs stringForColumn:@"img"];
            notice.imgSmall      = [rs stringForColumn:@"imgSmall"];
            notice.pageUrl       = [rs stringForColumn:@"pageUrl"];
            notice.picType       = [rs stringForColumn:@"picType"];
            notice.playType      = [rs stringForColumn:@"playType"];
            notice.uuid      = [rs stringForColumn:@"uuid"];
            notice.publishDate   = [rs stringForColumn:@"publishDate"];
            notice.subTitle      = [rs stringForColumn:@"subTitle"];
            NSString *title = [rs stringForColumn:@"title"];
            notice.title         = [title stringByReplacingOccurrencesOfString:@"‘" withString:@"'"];
            notice.type          = [rs stringForColumn:@"type"];
            notice.uploadVideoUrl= [rs stringForColumn:@"uploadVideoUrl"];
            notice.videoUrl      = [rs stringForColumn:@"videoUrl"];
            notice.vipFlag       = [rs    intForColumn:@"vipFlag"];
            notice.author        = [rs stringForColumn:@"author"];
            [list addObject:notice];
        }
        [db close];
    }
    return list;
}
@end