123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- //
- // LKDBHelper.h
- // upin
- //
- // Created by Fanhuan on 12-12-6.
- // Copyright (c) 2012年 linggan. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "FMDatabaseQueue.h"
- #import "FMDatabase.h"
- #import "LKDBUtils.h"
- #import "LKDB+Manager.h"
- #import "LKDB+Mapping.h"
- #import "NSObject+LKModel.h"
- #import "NSObject+LKDBHelper.h"
- #ifdef DEBUG
- #ifdef NSLog
- #define LKLog(fmt, ...) NSLog(@"#LKLog:\n" fmt,##__VA_ARGS__);
- #else
- #define LKLog(fmt, ...) NSLog(@"\n#LKLog: %s [Line %d] \n" fmt, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
- #endif
- #else
- # define LKLog(...)
- #endif
- @interface LKDBHelper : NSObject
- // you can use [LKDBHelper getUsingLKDBHelper]
- #pragma mark- deprecated
- +(LKDBHelper*)sharedDBHelper DEPRECATED_ATTRIBUTE;
- #pragma mark-
- -(id)initWithDBName:(NSString*)dbname;
- /**
- * @brief change database , filepath the use of : "documents/db/" + fileName + ".db"
- */
- -(void)setDBName:(NSString*)fileName;
- /**
- * @brief execute database operations synchronously,not afraid of recursive deadlock 同步执行数据库操作 可递归调用
- */
- -(void)executeDB:(void (^)(FMDatabase *db))block;
- -(BOOL)executeSQL:(NSString *)sql arguments:(NSArray *)args;
- -(NSString *)executeScalarWithSQL:(NSString *)sql arguments:(NSArray *)args;
- @end
- @interface LKDBHelper(DatabaseManager)
- //create table with entity class
- -(BOOL)createTableWithModelClass:(Class)model;
- //drop all table
- -(void)dropAllTable;
- //drop table with entity class
- -(BOOL)dropTableWithClass:(Class)modelClass;
- @end
- @interface LKDBHelper(DatabaseExecute)
- /**
- * @brief The number of rows query table
- *
- * @param modelClass entity class
- * @param where can use NSString or NSDictionary or nil
- *
- * @return rows number
- */
- -(int)rowCount:(Class)modelClass where:(id)where;
- -(void)rowCount:(Class)modelClass where:(id)where callback:(void(^)(int rowCount))callback;
- /**
- * @brief query table
- *
- * @param modelClass entity class
- * @param where can use NSString or NSDictionary or nil
-
- * @param orderBy The Sort: Ascending "name asc",Descending "name desc"
- For example: @"rowid desc" or @"rowid asc"
-
- * @param offset Skip how many rows
- * @param count Limit the number
- *
- * @return query finished result is an array(model instance collection)
- */
- -(NSMutableArray*)search:(Class)modelClass where:(id)where orderBy:(NSString*)orderBy offset:(int)offset count:(int)count;
- -(void)search:(Class)modelClass where:(id)where orderBy:(NSString*)orderBy offset:(int)offset count:(int)count callback:(void(^)(NSMutableArray* array))block;
- //return first model or nil
- -(id)searchSingle:(Class)modelClass where:(id)where orderBy:(NSString*)orderBy;
- /**
- * @brief insert table
- *
- * @param model you want to insert the entity
- *
- * @return the inserted was successful
- */
- -(BOOL)insertToDB:(NSObject*)model;
- -(void)insertToDB:(NSObject*)model callback:(void(^)(BOOL result))block;
- /**
- * @brief insert when the entity primary key does not exist
- *
- * @param model you want to insert the entity
- *
- * @return the inserted was successful
- */
- -(BOOL)insertWhenNotExists:(NSObject*)model;
- -(void)insertWhenNotExists:(NSObject*)model callback:(void(^)(BOOL result))block;
- /**
- * @brief update table
- *
- * @param model you want to update the entity
- * @param where can use NSString or NSDictionary or nil
- when "where" is nil : update the value based on rowid colume or primary key colume
- *
- * @return the updated was successful
- */
- -(BOOL)updateToDB:(NSObject *)model where:(id)where;
- -(void)updateToDB:(NSObject *)model where:(id)where callback:(void (^)(BOOL result))block;
- -(BOOL)updateToDB:(Class)modelClass set:(NSString*)sets where:(id)where;
- /**
- * @brief delete table
- *
- * @param model you want to delete entity
- when entity property "rowid" == 0 based on the primary key to delete
- *
- * @return the deleted was successful
- */
- -(BOOL)deleteToDB:(NSObject*)model;
- -(void)deleteToDB:(NSObject*)model callback:(void(^)(BOOL result))block;
- /**
- * @brief delete table with "where" constraint
- *
- * @param modelClass entity class
- * @param where can use NSString or NSDictionary, can not is nil
- *
- * @return the deleted was successful
- */
- -(BOOL)deleteWithClass:(Class)modelClass where:(id)where;
- -(void)deleteWithClass:(Class)modelClass where:(id)where callback:(void (^)(BOOL result))block;
- /**
- * @brief entity exists?
- * for primary key colume
- (if rowid > 0 would certainly exist so we do not rowid judgment)
- * @param model entity
- *
- * @return YES: entity presence , NO: entity not exist
- */
- -(BOOL)isExistsModel:(NSObject*)model;
- -(BOOL)isExistsClass:(Class)modelClass where:(id)where;
- /**
- * @brief Clear data based on the entity class
- *
- * @param modelClass entity class
- */
- +(void)clearTableData:(Class)modelClass;
-
- /**
- * @brief Clear Unused Data File
- if you property has UIImage or NSData, will save their data in the (documents dir)
- *
- * @param modelClass entity class
- * @param columes UIImage or NSData Colume Name
- */
- +(void)clearNoneImage:(Class)modelClass columes:(NSArray*)columes;
- +(void)clearNoneData:(Class)modelClass columes:(NSArray*)columes;
- @end
|