项目作者: li6185377

项目描述 :
全自动的插入,查询,更新,删除, an automatic database operation thread-safe and not afraid of recursive deadlock
高级语言: Objective-C
项目地址: git://github.com/li6185377/LKDBHelper-SQLite-ORM.git
创建时间: 2013-04-15T12:31:09Z
项目社区:https://github.com/li6185377/LKDBHelper-SQLite-ORM

开源协议:MIT License

下载


LKDBHelper

this is sqlite ORM (an automatic database operation)

thread-safe and not afraid of recursive deadlock

简书:不定时更新 http://www.jianshu.com/users/376b950a20ec

Big Upgrade 2.0

Supported NSArray,NSDictionary, ModelClass, NSNumber, NSString, NSDate, NSData, UIColor, UIImage, CGRect, CGPoint, CGSize, NSRange, int,char,float, double, long.. attribute to insert and select automation.

全面支持 NSArray,NSDictionary, ModelClass, NSNumber, NSString, NSDate, NSData, UIColor, UIImage, CGRect, CGPoint, CGSize, NSRange, int,char,float, double, long.. 等属性的自动化操作(插入和查询)


Requirements

由于 FMDB 限制,需要支持 iOS12 之前系统,自行限定到 FMDB(2.7.5) 和 LKDBHelper (2.6.3)

Adding to your project

If you are using CocoaPods, then, just add this line to your Podfile

  1. pod 'LKDBHelper'

Before iOS12

  1. pod 'LKDBHelper', '2.6.3'
  2. pod 'FMDB', '2.7.5'

If you are using encryption, Order can not be wrong

  1. pod 'FMDB/SQLCipher'
  2. pod 'LKDBHelper'

@property(strong,nonatomic)NSString* encryptionKey;

Basic usage

  1. Create a new Objective-C class for your data model
  1. @interface LKTest : NSObject
  2. @property (nonatomic, copy) NSURL *url;
  3. @property (nonatomic, copy) NSString *name;
  4. @property (nonatomic, assign) NSUInteger age;
  5. @property (nonatomic, assign) BOOL isGirl;
  6. @property (nonatomic, strong) LKTestForeign *address;
  7. @property (nonatomic, strong) NSArray *blah;
  8. @property (nonatomic, strong) NSDictionary *hoho;
  9. @property (nonatomic, assign) char like;
  10. ...
  1. in the *.m file, overwirte getTableName function (option)
  1. + (NSString *)getTableName {
  2. return @"LKTestTable";
  3. }
  1. in the *.m file, overwirte callback function (option)
  1. @interface NSObject (LKDBHelper_Delegate)
  2. + (void)dbDidCreateTable:(LKDBHelper *)helper tableName:(NSString *)tableName;
  3. + (void)dbDidAlterTable:(LKDBHelper *)helper tableName:(NSString *)tableName addColumns:(NSArray *)columns;
  4. + (BOOL)dbWillInsert:(NSObject *)entity;
  5. + (void)dbDidInserted:(NSObject *)entity result:(BOOL)result;
  6. + (BOOL)dbWillUpdate:(NSObject *)entity;
  7. + (void)dbDidUpdated:(NSObject *)entity result:(BOOL)result;
  8. + (BOOL)dbWillDelete:(NSObject *)entity;
  9. + (void)dbDidDeleted:(NSObject *)entity result:(BOOL)result;
  10. ///data read finish
  11. + (void)dbDidSeleted:(NSObject *)entity;
  12. @end
  1. Initialize your model with data and insert to database
  1. LKTestForeign *foreign = [[LKTestForeign alloc] init];
  2. foreign.address = @":asdasdasdsadasdsdas";
  3. foreign.postcode = 123341;
  4. foreign.addid = 213214;
  5. //插入数据 insert table row
  6. LKTest *test = [[LKTest alloc] init];
  7. test.name = @"zhan san";
  8. test.age = 16;
  9. //外键 foreign key
  10. test.address = foreign;
  11. test.blah = @[@"1", @"2", @"3"];
  12. test.blah = @[@"0", @[@1] ,@{ @"2" : @2 }, foreign];
  13. test.hoho = @{@"array" : test.blah, @"foreign" : foreign, @"normal" : @123456, @"date" : [NSDate date]};
  14. //同步 插入第一条 数据 Insert the first
  15. [test saveToDB];
  16. //or
  17. //[globalHelper insertToDB:test];
  1. select 、 delete 、 update 、 isExists 、 rowCount …
  1. select:
  2. NSMutableArray *array = [LKTest searchWithWhere:nil orderBy:nil offset:0 count:100];
  3. for (id obj in array) {
  4. addText(@"%@",[obj printAllPropertys]);
  5. }
  6. delete:
  7. [LKTest deleteToDB:test];
  8. update:
  9. test.name = "rename";
  10. [LKTest updateToDB:test where:nil];
  11. isExists:
  12. [LKTest isExistsWithModel:test];
  13. rowCount:
  14. [LKTest rowCountWithWhere:nil];
  1. Description of parameters “where”
  1. For example:
  2. single: @"rowid = 1" or @{ @"rowid" : @1 }
  3. more: @"rowid = 1 and sex = 0" or @{ @"rowid" : @1, @"sex" : @0 }
  4. when where is "or" type , such as @"rowid = 1 or sex = 0"
  5. you only use NSString
  6. array: @"rowid in (1,2,3)" or @{ @"rowid" : @[@1, @2, @3] }
  7. composite: @"rowid in (1,2,3) and sex=0 " or @{ @"rowid" : @[@1, @2, @3], @"sex" : @0}
  8. If you want to be judged , only use NSString
  9. For example: @"date >= '2013-04-01 00:00:00'"

table mapping

overwirte getTableMapping Function (option)

  1. //手动or自动 绑定sql列
  2. + (NSDictionary *)getTableMapping {
  3. return @{ @"name" : LKSQL_Mapping_Inherit,
  4. @"MyAge" : @"age",
  5. @"img" : LKSQL_Mapping_Inherit,
  6. @"MyDate" : @"date",
  7. // version 2 after add
  8. @"color" : LKSQL_Mapping_Inherit,
  9. //version 3 after add
  10. @"address" : LKSQL_Mapping_UserCalculate,
  11. @"error" : LKSQL_Mapping_Inherit
  12. };
  13. }

table update (option)

  1. // 表结构更新回调
  2. + (void)dbDidAlterTable:(LKDBHelper *)helper tableName:(NSString *)tableName addColumns:(NSArray *)columns {
  3. for (int i = 0; i < columns.count; i++) {
  4. LKDBProperty *p = [columns objectAtIndex:i];
  5. if ([p.propertyName isEqualToString:@"error"]) {
  6. [helper executeDB:^(FMDatabase *db) {
  7. NSString *sql = [NSString stringWithFormat:@"update %@ set error = name", tableName];
  8. [db executeUpdate:sql];
  9. }];
  10. }
  11. }
  12. LKErrorLog(@"your know %@", columns);
  13. }

set column attribute (option)

  1. // 定制化列属性
  2. + (void)columnAttributeWithProperty:(LKDBProperty *)property {
  3. if ([property.sqlColumnName isEqualToString:@"MyAge"]) {
  4. property.defaultValue = @"15";
  5. } else if ([property.propertyName isEqualToString:@"date"]) {
  6. // if you use unique,this property will also become the primary key
  7. // property.isUnique = YES;
  8. property.checkValue = @"MyDate > '2000-01-01 00:00:00'";
  9. property.length = 30;
  10. }
  11. }

demo screenshot

demo screenshot

table test data



foreign key data


Use in swift

Remember to override the class function getTableName for model.

Change-log

Version 1.1 @ 2012-6-20

  • automatic table mapping
  • support optional columns
  • support column attribute settings
  • you can return column content

Version 1.0 @ 2013-5-19

  • overwrite and rename LKDBHelper
  • property type support: UIColor,NSDate,UIImage,NSData,CGRect,CGSize,CGPoint,int,float,double,NSString,short,char,bool,NSInterger..
  • fix a recursive deadlock.
  • rewrite the asynchronous operation -
  • thread-safe
  • various bug modified optimize cache to improve performance
  • test and demos
  • bug fixes, speed improvements

Version 0.0.1 @ 2012-10-1

  • Initial release with LKDAOBase

License

This code is distributed under the terms and conditions of the MIT license.


Contribution guidelines

  • if you are fixing a bug you discovered, please add also a unit test so I know how exactly to reproduce the bug before merging

Contributors

Author: Jianghuai Li

Contributors: waiting for you to join