OC/Swift iOS fast one-stop SQLite database setup. Call lighter. Faster. No need to pay attention to details.
Just add '@objc dynamic' before each attribute var. Other uses are the same as objective-C
Use iOS Runtime to add ORM model relational mapping on FMDB, the most authoritative SQLite open source library, and use the way of Category to separate from the base class, and add the default field dynamically. Enable any class to build tables.
Access mode:
pod 'XTFMDB'
Import header file #import < xtfMDB.h >
Complete the configuration in the AppDelegate didFinishLaunchingWithOptions
[XTFMDBBase sharedInstance].isDebugMode = YES; // Whether to print the internal log
NSString *yourDbPath = @"... /your_DB_Name";
[[XTFMDBBase sharedInstance] configureDBWithPath:yourDbPath];
// insert
- (BOOL)xt_insert;
+ (BOOL)xt_insertList:(NSArray *)modelList;
// insert or ignore
- (BOOL)xt_insertOrIgnore;
+ (BOOL)xt_insertOrIgnoreWithList:(NSArray *)modelList;
// insert or replace
- (BOOL)xt_insertOrReplace;
+ (BOOL)xt_insertOrReplaceWithList:(NSArray *)modelList;
// upsert
- (BOOL)xt_upsertWhereByProp:(NSString *)propName;
The following M1 represents instances under anyModel.class.
[m1 xt_insert]; / / a single
[AnyModel xt_insertList:list]; / / batch
[m1 xt_insertOrIgnore]; // If there is one, ignore it
[AnyModel xt_insertOrIgnoreWithList:list]; // If there is one, ignore it
[m1 xt_insertOrReplace]; // If there is one, replace it
[AnyModel xt_insertOrReplaceWithList:list]; // If there is one, replace it
[m1 xt_upsertWhereByProp:@"name"]; // Update if it exists, insert if it does not.
// update by pkid .
- (BOOL)xt_update; // Update default update by pkid. if pkid nil, update by a unique prop if has .
+ (BOOL)xt_updateListByPkid:(NSArray *)modelList; // Batch update
// update by custom key
- (BOOL)xt_updateWhereByProp:(NSString *)propName; / / a single
+ (BOOL)xt_updateList:(NSArray *)modelList whereByProp:(NSString *)propName; / / batch
e.g.
[m1 xt_update]; // Update this object (pkid first, if the primary key is empty, find if there is a unique field to update.)
[AnyModel xt_updateListByPkid:list]; / / batch
[m1 xt_updateWhereByProp:@"name"]; // Update this object (manually specify a field)
[AnyModel xt_updateList:list whereByProp:@"name"]; / / batch
+ (NSArray *)xt_findAll;
+ (NSArray *)xt_findWhere:(NSString *)strWhere; // param e.g. @" pkid = '1' "
+ (instancetype)xt_findFirstWhere:(NSString *)strWhere;
+ (instancetype)xt_findFirst;
+ (BOOL)xt_hasModelWhere:(NSString *)strWhere;
// any sql execute Query
+ (NSArray *)xt_findWithSql:(NSString *)sql;
+ (instancetype)xt_findFirstWithSql:(NSString *)sql;
e.g.
list = [AnyModel xt_findAll]; // Query all records in this table
list = [AnyModel xt_findWhere:@"name == 'mamba'"]; // Conditional query
item = [AnyModel xt_findFirstWhere:@"name == 'mamba'"]; // Query a single item
item = [AnyModel xt_findFirst];
bool has = [AnyModel xt_hasModelWhere:@"age < 4"] ; // Check whether there is data that meets the condition
list = [AnyModel xt_findWithSql:@"select * from AnyModel"] ; // Custom SQL statement, query list
item = [AnyModel xt_findFirstWithSql:@"select * from AnyModel where age == 111"] ; // Custom SQL statement, query a single
- (BOOL)xt_deleteModel;
+ (BOOL)xt_deleteModelWhere:(NSString *)strWhere; // param e.g. @" pkid = '1' "
+ (BOOL)xt_dropTable;
e.g.
[m1 xt_deleteModel]; // Delete the record
[AnyModel xt_deleteModelWhere:@"name == 'peter'"];
[AnyModel xt_dropTable]; / / delete table
// func execute Statements
+ (id)xt_anyFuncWithSql:(NSString *)sql;
+ (BOOL)xt_isEmptyTable;
+ (int)xt_count;
+ (int)xt_countWhere:(NSString *)whereStr;
+ (double)xt_maxOf:(NSString *)property;
+ (double)xt_maxOf:(NSString *)property where:(NSString *)whereStr;
+ (double)xt_minOf:(NSString *)property;
+ (double)xt_minOf:(NSString *)property where:(NSString *)whereStr;
+ (double)xt_sumOf:(NSString *)property;
+ (double)xt_sumOf:(NSString *)property where:(NSString *)whereStr;
+ (double)xt_avgOf:(NSString *)property;
+ (double)xt_avgOf:(NSString *)property where:(NSString *)whereStr;
e.g.
int count = [AnyModel xt_count] ;
int count = [AnyModel xt_countWhere:@"age < 10"] ;
double max = [AnyModel xt_maxOf:@"age"] ;
double max = [AnyModel xt_maxOf:@"age" where:@"location == 'shanghai'"] ;
id val = [AnyModel shmdb_anyFuncWithSql:@"..."] ;
/ * *
Order by . (in memory)
@param columnName --- must be a int column
@param descOrAsc BOOL desc - 1 , asc - 0
@return a sorted list
* /
- (NSArray *)shmdb_orderby:(NSString *)columnName
descOrAsc:(BOOL)descOrAsc;
e.g.
[list shmdb_orderby:@"age" descOrAsc:1]; // In descending order of age
To further configure the table, override three methods in the AnyModel class
// props Sqlite Keywords
+ (NSDictionary *)modelPropertiesSqliteKeywords; // set sqlite Constraints of property
// ignore Properties . these properties will not join db CURD .
+ (NSArray *)ignoreProperties;
// Container property , value should be Class or Class name. Same as YYmodel .
+ (NSDictionary *)modelContainerPropertyGenericClass;
ModelPropertiesSqliteKeywords, configuration properties, a default value is not empty and has joined without configuration, for example, in here you can specify the uniqueness of a particular field
+ (NSDictionary *)modelPropertiesSqliteKeywords {
return @{@"name":@"UNIQUE"} ;
}
IgnoreProperties, which configures fields that do not want to participate in table building. For example, viewModel-related properties.
+ (NSArray *)ignoreProperties {
return @[@"a1",@"a2"] ;
}
ModelContainerPropertyGenericClass, dealing with nested in the container types have other classes.
@class Shadow, Border, Attachment;
@interface Attributes
@property NSString *name;
@property NSArray *shadows; //Array<Shadow>
@property NSSet *borders; //Set<Border>
@property NSMutableDictionary *attachments; //Dict<NSString,Attachment>
@end
@implementation Attributes
// Return the data type (Class or Class Name) that needs to be stored in the container Class.
+ (NSDictionary *)modelContainerPropertyGenericClass {
return @{@"shadows" : [Shadow class],
@"borders" : Border.class,
@"attachments" : @"Attachment" };
}
@end
/ * *
The DB Version Upgrade
@ param tableCls Class
@param paramsAdd @[propName1,propName2...]
@param version (int) start from 1
* /
- (void)dbUpgradeTable:(Class)tableCls
ParamsAdd paramsAdd: (NSArray *)
Version: (int) version;
e.g. One line of code completes the database upgrade.
[[XTFMDBBase sharedInstance] dbUpgradeTable:AnyModel1.class
ParamsAdd: @ @ "b1" @ "b2" @ "b3"]
Version: 2];
// Just pass in the corresponding table, the new field array, and the corresponding database version number. The version number starts from 1 by default. Each upgrade increases incrementally.
Identified by this version number.
[[XTFMDBBase sharedInstance] dbUpgradeTable:AnyModel2.class
paramsAdd:@[ @"c1",@"c2",@"c3" ]
version:3];