Develop a line of code series for iOS: Get the database in one line

Source: Internet
Author: User

Develop a line of code series for iOS: Get the database in one line
Principle

Relationship between iOS and SQL

Model class structure => SQL table structure

Model instance => a row in the SQL table

Attribute of the Model instance => a column in the SQL table

 

Model and Table

 

@interface TestModel :NSObject@property (assign, nonatomic) NSInteger age;@property (assign, nonatomic) CGFloat height;@property (assign, nonatomic) long distance;@property (assign, nonatomic) double maxDistance;@property (assign, nonatomic) BOOL isOK;@property (assign, nonatomic) char mChar;@property (assign, nonatomic) CGRect rect;@property (assign, nonatomic) CGPoint point;@property (assign, nonatomic) CGSize size;@property (assign, nonatomic) NSRange range;@property (strong, nonatomic) NSNumber *number;@property (strong, nonatomic) NSString *string;@property (strong, nonatomic) UIColor *color;@property (strong, nonatomic) NSDate *date;@property (strong, nonatomic) NSData *data;// convert to data@property (strong, nonatomic) NSValue *value;@property (strong, nonatomic) UIImage *image;@end

 

 

The corresponding TABLE structure is TABLE TestModel (age type, height type, distance type, isOK type, mChar type ,...)

 

Method

The following are some available methods that are still being improved...

 

 

+(ModelToTableMap*)getModelToTableMap;+(NSString*)getDBPath;+(NSString*)getTableName;+(NSArray*)getPrimaryKeys;+(NSDictionary *)getPropertyToColumnMap;+(NSDictionary *)getDefaultValueDictionary;+(NSDictionary *)getLengthDictionary;+(NSDictionary *)getCheckValueDictionary;+(NSDictionary *)getIsUniqueDictionary;+(NSDictionary *)getIsNotNullDictionary;+(BOOL)shouldModelToTableMapContainParentProperties;+(BOOL)shouldModelToTableMapContainSelfProperties;+(NSString *)getDateFormatterString;+(NSString *)getDBImagePathWithImageName:(NSString *)imgName ;+(NSString *)getDBDataPathWithDataName:(NSString *)dataName;+(BOOL)createTable;+(BOOL)dropTable;+(id)firstModelWhere:(NSObject *)where;+(id)firstModelWhere:(NSObject *)where orderBy:(NSString*)orderBy ;+(NSArray *)allModels;+(NSArray *)findModelsWhere:(NSObject *)where;+(NSArray *)findModelsWhere:(NSObject *)where orderBy:(NSString*)orderBy offset:(int)offset count:(int)count;+(BOOL)insertModel:(NSObject *)model;+(BOOL)insertModelWhenNotExists:(NSObject *)model;+(BOOL)deleteModel:(NSObject *)model;+(BOOL)deleteModelsWhere:(NSObject *)where;+(BOOL)updateModelsWithModel:(NSObject *)model where:(NSObject *)where;+(BOOL)updateModelsWithDictionary:(NSDictionary *)dic where:(NSObject *)where;+(BOOL)isModelExists:(NSObject *)model;- (BOOL)saveModel;- (BOOL)deleteModel;- (BOOL)updateModel:(NSObject *)theNewModel;- (BOOL)updateModelWithDictionary:(NSDictionary *)dic;


 

Use

 

TestModel *model = [[TestModel alloc] init];model.age = 20;model.image = [UIImage imageNamed:@img.jpg];
// Add the [model saveModel]
// Delete [model deleteModel]
// Modify [model updateModelWithDictionary: @ {@ age: @ (21)}]
// Find [TestModel allModels]

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.