您的位置:首页 > 数据库

fmdb数据库封装

2017-08-04 17:47 99 查看
//

//  FMDBTool.h

//  SeeTime

//

//  Created by aojinrui on 2017/8/3.

//  Copyright © 2017年 aojinrui. All rights reserved.

//

#import <Foundation/Foundation.h>

#import <FMDB.h>

#import <FMDatabaseQueue.h>

@interface FMDBTool : NSObject

+(FMDatabaseQueue *)shareFMdataBase;

@end

//

//  FMDBTool.m

//  SeeTime

//

//  Created by aojinrui on 2017/8/3.

//  Copyright © 2017年 aojinrui. All rights reserved.

//

#import "FMDBTool.h"

static FMDatabaseQueue *db =
nil;

@implementation FMDBTool

/**

 *创建数据库操作对象

 */

+(FMDatabaseQueue *)shareFMdataBase

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        db = [FMDatabaseQueue databaseQueueWithPath:kDBFilePath];

        

    });

    return db;

}

@end

//

//  FMDBManager.h

//  SeeTime

//

//  Created by aojinrui on 2017/8/3.

//  Copyright © 2017年 aojinrui. All rights reserved.

//

#import "FMDBTool.h"

@interface FMDBManager : FMDBTool

+(void)setFMDBVersion:(uint32_t)aVersion;

+(uint32_t)getFMDBVersion;

+(BOOL)creatTable:(id)tbObj;

+(BOOL)insertTable:(id)tbObj;

+(id)selectWithTable:(id)tbObj withWhere:(NSString*)condition;

+(BOOL)updaTable:(id)tbObj withWhere:(NSString*)condition;

+(BOOL)deleteTable:(id)tbObj withWhere:(NSString*)condition;

@end

//

//  FMDBManager.m

//  SeeTime

//

//  Created by aojinrui on 2017/8/3.

//  Copyright © 2017年 aojinrui. All rights reserved.

//

#import "FMDBManager.h"

#import <objc/runtime.h>

@implementation FMDBManager

+(void)setFMDBVersion:(uint32_t)aVersion

{

    [[self shareFMdataBase] inDatabase:^(FMDatabase *db) {

       

        [db setUserVersion:aVersion];

        

    }];

}

+(uint32_t)getFMDBVersion

{

    __block uint32_t fmversion =
0;

    [[self shareFMdataBase] inDatabase:^(FMDatabase *db) {

        

       fmversion = [db userVersion];

        

    }];

    return fmversion;

}

+(BOOL)creatTable:(id)tbObj

{

    if (!tbObj) {

        return
NO;

    }

    __block
BOOL isSuccess =
NO;

    NSString *tbName = NSStringFromClass([tbObj class]);

    //数据库名字

    NSString *tableName =[NSString stringWithFormat:@"%@",tbName];

    //创建语句

    NSString *msgSql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(Id integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE)",tableName];

    

    [[self shareFMdataBase] inDatabase:^(FMDatabase *db) {

        isSuccess = [db executeUpdate:msgSql];

        ///存储属性的个数

        unsigned
int propertyCount =
0;

       
///通过运行时获取当前类的属性

        objc_property_t *propertys = class_copyPropertyList([tbObj class], &propertyCount);

        

        //把属性放到数组中

        for (int i =
0; i < propertyCount; i ++) {

            ///取出第一个属性

            objc_property_t property = propertys[i];

            NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];

            if (![db columnExists:propertyName inTableWithName:tbName]) {

                

                NSString *propertyAttr =  [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];

                NSArray * attributes = [propertyAttr componentsSeparatedByString:@","];

                NSString * typeAttribute = attributes[0];

                if ([self comparAttri:typeAttribute]) {

                    NSString * attrstr =[self getAttributesWith:typeAttribute];

                    [db executeUpdate:[NSString stringWithFormat:@"ALTER TABLE %@ ADD COLUMN %@ %@",tbName,propertyName,attrstr]];

                }

            }

        }

        free(propertys);

        

    }];

    return isSuccess;

}

+(BOOL)insertTable:(id)tbObj

{

    if (!tbObj) {

        return
NO;

    }

    NSString *tbName = NSStringFromClass([tbObj class]);

    //数据库名字

    NSString *tableName =[NSString stringWithFormat:@"%@",tbName];

     __block
BOOL isok;

    if ([self creatTable:tbObj]) {

        [[self shareFMdataBase] inDatabase:^(FMDatabase *db) {

            

          NSString * sql = [NSString stringWithFormat:@"INSERT INTO %@",tableName];

            sql = [sql stringByAppendingString:@"("];

            ///存储属性的个数

            unsigned
int propertyCount =
0;

            ///通过运行时获取当前类的属性

            objc_property_t *propertys = class_copyPropertyList([tbObj class], &propertyCount);

            //属性名数组

            NSMutableArray *nameArr = [NSMutableArray array];

            //属性值数组

            NSMutableArray *attriArr = [NSMutableArray array];

            //把属性放到数组中

            for (int i =
0; i < propertyCount; i ++) {

                ///取出第一个属性

                objc_property_t property = propertys[i];

                NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];

                NSString *propertyAttr =  [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];

                NSLog(@"%@",propertyAttr);

                NSArray * attributes = [propertyAttr componentsSeparatedByString:@","];

                NSString * typeattri = attributes[0];

                if ([self comparAttri:typeattri]) {

                    [nameArr addObject:propertyName];

                    if ([typeattri isEqualToString:@"T@\"NSArray\""]||[typeattri isEqualToString:@"T@\"NSMutableArray\""]){

                        id attriValue = [tbObj valueForKey:propertyName];

                        [attriArr addObject:[NSKeyedArchiver archivedDataWithRootObject:attriValue]];

                    }else
if ([typeattri isEqualToString:@"T@\"NSDictionary\""]||[typeattri isEqualToString:@"T@\"NSMutableDictionary\""]){

                        id attriValue = [tbObj valueForKey:propertyName];

                        [attriArr addObject:[NSKeyedArchiver archivedDataWithRootObject:attriValue]];

                    }else{

                        id attriValue = [tbObj valueForKey:propertyName];

                        [attriArr addObject:attriValue];

                    }

                }

            }

           NSString *vaStr =@"";

            for (int i=0; i<nameArr.count; i++) {

                NSString *propertyName = nameArr[i];

                

                if (i==0) {

                    sql = [sql stringByAppendingString:propertyName];

                    vaStr = [vaStr stringByAppendingString:@"(?"];

                }else
if (i==nameArr.count-1){

                    sql = [sql stringByAppendingFormat:@",%@) VALUES ",propertyName];

                    vaStr = [vaStr stringByAppendingString:@",?)"];

                    sql = [sql stringByAppendingString:vaStr];

                }else{

                    sql = [sql stringByAppendingFormat:@",%@",propertyName];

                    vaStr = [vaStr stringByAppendingString:@",?"];

                }

            }

            

          isok =  [db executeUpdate:sql withArgumentsInArray:attriArr];

        free(propertys);

        }];

        return isok;

    }

    return
NO;

}

+(id)selectWithTable:(id)tbObj withWhere:(NSString*)condition

{

    if (!tbObj) {

        return
nil;

    }

    NSString *tbName = NSStringFromClass([tbObj class]);

    //数据库名字

    NSString *tableName =[NSString stringWithFormat:@"%@",tbName];

    

    if ([self creatTable:tbObj]) {

        NSString *sql = @"";

        if (condition) {

            sql = [NSString stringWithFormat:@"SELECT * FROM %@  WHERE %@",tableName,condition];

        }else{

            sql = [NSString stringWithFormat:@"SELECT * FROM %@",tableName];

        }

        

        NSMutableArray *arr = [[NSMutableArray alloc]init];

        [[self shareFMdataBase] inDatabase:^(FMDatabase *db) {

            FMResultSet *set = [db executeQuery:sql];

            ///存储属性的个数

            unsigned
int propertyCount =
0;

            ///通过运行时获取当前类的属性

            objc_property_t *propertys = class_copyPropertyList([tbObj class], &propertyCount);

            while ([set next]) {

              id model =  [[NSClassFromString(tbName) alloc] init];

                //把属性放到数组中

                for (int i =
0; i < propertyCount; i ++) {

                    ///取出属性

                    objc_property_t property = propertys[i];

                    NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];

                    NSString *propertyAttr =  [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];

                    NSArray * attributes = [propertyAttr componentsSeparatedByString:@","];

                    NSString * typeattri = attributes[0];

                    if ([self comparAttri:typeattri]) {

                        if ([typeattri isEqualToString:@"T@\"NSData\""]){

                            

                            [model setValue:[set dataForColumn:propertyName] forKey:propertyName];

                        }else
if ([typeattri isEqualToString:@"T@\"NSArray\""]||[typeattri isEqualToString:@"T@\"NSMutableArray\""]){

                            NSArray *tArr = [NSKeyedUnarchiver unarchiveObjectWithData:[set dataForColumn:propertyName]];

                            

                            [model setValue:tArr forKey:propertyName];

                        }else
if ([typeattri isEqualToString:@"T@\"NSDictionary\""]||[typeattri isEqualToString:@"T@\"NSMutableDictionary\""]){

                            NSDictionary *tDic = [NSKeyedUnarchiver unarchiveObjectWithData:[set dataForColumn:propertyName]];

                            [model setValue:tDic forKey:propertyName];

                        }else{

                            [model setValue:[set objectForColumnName:propertyName] forKey:propertyName];

                        }

                    }

                }

                [arr addObject:model];

            }

             free(propertys);

            [set close];

        }];

        return arr;

    }

    return
nil;

}

+(BOOL)updaTable:(id)tbObj withWhere:(NSString*)condition

{

    

    if (!tbObj) {

        return
nil;

    }

    NSString *tbName = NSStringFromClass([tbObj class]);

    //数据库名字

    NSString *tableName =[NSString stringWithFormat:@"%@",tbName];

    

    if ([self creatTable:tbObj]) {

        __block 
BOOL isSuccess;

        [[self shareFMdataBase] inDatabase:^(FMDatabase *db) {

            

            NSString * sql = [NSString stringWithFormat:@"UPDATE %@ set ",tableName];

            ///存储属性的个数

            unsigned
int propertyCount =
0;

            ///通过运行时获取当前类的属性

            objc_property_t *propertys = class_copyPropertyList([tbObj class], &propertyCount);

            //属性名数组

            NSMutableArray *nameArr = [NSMutableArray array];

            //属性值数组

            NSMutableArray *attriArr = [NSMutableArray array];

            //把属性放到数组中

            for (int i =
0; i < propertyCount; i ++) {

                ///取出第一个属性

                objc_property_t property = propertys[i];

                NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];

                NSString *propertyAttr =  [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];

                NSArray * attributes = [propertyAttr componentsSeparatedByString:@","];

                NSString * typeattri = attributes[0];

                if ([self comparAttri:typeattri]) {

                    [nameArr addObject:propertyName];

                    if ([typeattri isEqualToString:@"T@\"NSArray\""]||[typeattri isEqualToString:@"T@\"NSMutableArray\""]){

                        id attriValue = [tbObj valueForKey:propertyName];

                        

                        [attriArr addObject:[NSKeyedArchiver archivedDataWithRootObject:attriValue]];

                    }else
if ([typeattri isEqualToString:@"T@\"NSDictionary\""]||[typeattri isEqualToString:@"T@\"NSMutableDictionary\""]){

                        id attriValue = [tbObj valueForKey:propertyName];

                        

                        [attriArr addObject:[NSKeyedArchiver archivedDataWithRootObject:attriValue]];

                    }else{

                        id attriValue = [tbObj valueForKey:propertyName];

                        [attriArr addObject:attriValue];

                    }

                }

            }

            

            for (int i=0; i<nameArr.count; i++) {

                NSString *propertyName = nameArr[i];

                if (i==nameArr.count-1) {

                    if (condition) {

                        sql = [sql stringByAppendingFormat:@"%@=? WHERE %@",propertyName,condition];

                    }else{

                        sql = [sql stringByAppendingFormat:@"%@=?",propertyName];

                    }

                    break;

                }else{

                    sql = [sql stringByAppendingFormat:@"%@=?,",propertyName];

                }

            }

            isSuccess =  [db executeUpdate:sql withArgumentsInArray:attriArr];

            free(propertys);

        }];

        return isSuccess;

    }

    return
NO;

}

+(BOOL)deleteTable:(id)tbObj withWhere:(NSString*)condition

{

    if (!tbObj) {

        return
NO;

    }

    NSString *tbName = NSStringFromClass([tbObj class]);

    //数据库名字

    NSString *tableName =[NSString stringWithFormat:@"%@",tbName];

   NSString *sql = @"";

    if (condition) {

         sql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@",tableName,condition];

    }else{

         sql = [NSString stringWithFormat:@"DELETE FROM %@",tableName];

    }

    __block
BOOL isSuccess;

    [[self shareFMdataBase] inDatabase:^(FMDatabase *db) {

        isSuccess  = [db executeUpdate:sql];

    }];

    

    return isSuccess;

}

+(NSString*)getAttributesWith:(NSString*)typeattri{

    

    /*

    NULL 值是一个 NULL
值。

    INTEGER
值是一个带符号的整数,根据值的大小存储在 1、2、3、4、6
或 8
字节中。

    REAL
值是一个浮点值,存储为 8
字节的 IEEE
浮点数字。

    TEXT 值是一个文本字符串,使用数据库编码(UTF-8、UTF-16BE
或 UTF-16LE)存储。

    BLOB
值是一个 blob
数据,完全根据它的输入存储。

    */

    //==a1==Ti,N,V_a1

    //==b1==Tq,N,V_b1

    //==c1==Tf,N,V_c1

    //==d1==Td,N,V_d1

    //==e1==TB,N,V_e1

    //==f1==T@"NSString",C,N,V_f1

    //==arr==T@"NSArray",&,N,V_arr

    //==marr==T@"NSMutableArray",&,N,V_marr

    //==dic==T@"NSDictionary",&,N,V_dic

    //==mdic==T@"NSMutableDictionary",&,N,V_mdic

    //==data==T@"NSData",&,N,V_data

    

    NSString *str = @"TEXT";

   

    if ([typeattri isEqualToString:@"Ti"]||[typeattri isEqualToString:@"Tq"]||[typeattri
isEqualToString:@"TB"]||[typeattri isEqualToString:@"TI"]||[typeattri isEqualToString:@"TQ"])
{

        

        return str =
@"INTEGER";

    }else
if ([typeattri isEqualToString:@"Tf"]||[typeattri isEqualToString:@"Td"]){

        

        return str =
@"REAL";

    }else
if ([typeattri isEqualToString:@"T@\"NSString\""]){

        return str =
@"TEXT";

    }else
if ([typeattri isEqualToString:@"T@\"NSData\""]){

        return str =
@"BLOB";

    }else
if ([typeattri isEqualToString:@"T@\"NSArray\""]||[typeattri isEqualToString:@"T@\"NSMutableArray\""]){

        return str =
@"BLOB";

    }else
if ([typeattri isEqualToString:@"T@\"NSDictionary\""]||[typeattri isEqualToString:@"T@\"NSMutableDictionary\""]){

        return str =
@"BLOB";

    }

    return str;

}

+(BOOL)comparAttri:(NSString*)typeattri

{

    

    if ([typeattri isEqualToString:@"Ti"]||[typeattri isEqualToString:@"Tq"]||[typeattri
isEqualToString:@"TB"]||[typeattri isEqualToString:@"TI"]||[typeattri isEqualToString:@"TQ"])
{

        

        return
YES;

    }else
if ([typeattri isEqualToString:@"Tf"]||[typeattri isEqualToString:@"Td"]){

        

        return
YES;

    }else
if ([typeattri isEqualToString:@"T@\"NSString\""]){

        return
YES;

    }else
if ([typeattri isEqualToString:@"T@\"NSData\""]){

        return
YES;

    }else
if ([typeattri isEqualToString:@"T@\"NSArray\""]||[typeattri isEqualToString:@"T@\"NSMutableArray\""]){

        return
YES;

    }else
if ([typeattri isEqualToString:@"T@\"NSDictionary\""]||[typeattri isEqualToString:@"T@\"NSMutableDictionary\""]){

        return
YES;

    }

    

    return
NO;

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: