Copyright notice: This article for Bo Master original article, without Bo Master permission not reproduced.
The basic requirements of this ORM are illustrated by a common example of database relationships.
class, Student This pair of tables, the relationship is 1 to many , class table primary Key ID, student table primary key num, foreign key class ID.
The table structure is as follows:
Class Table class Structure
Student Student Table:
The corresponding entity classes in the program are: Class entity (classentity), student entity (Student)
The basic OBJECTIVE-C class structure is as follows:
@interface Classentity:nsobject@property (nonatomic,assign) Nsinteger classId; @property (nonatomic,copy) nsstring* ClassName; @end
@interface studententity:nsobject @property (nonatomic,assign) Nsinteger classId; @property (nonatomic,assign) int age; @property (nonatomic,assign) float score; @property (nonatomic,assign) Nsinteger studentnum; @property (nonatomic,copy) nsstring* Studentname; @end
0. Entity class--build a table statement
In this ORM, I expect the effect to be:
0, if there is a built-in database, I only need to pass in the entity class, you can establish the mapping, direct operation.
1, if there is no built-in database, I only need to pass in the entity class, you can automatically build the database and the mapping between the entity database.
2, support the primary foreign key, support index statement.
Input
@[[Classentity Class],[studententity class]]
Output
CREATE TABLE class (ID integer PRIMARY KEY autoincrement not NULL, name text NOT null DEFAULT (' Software01 '));
"The table name of the database can be arbitrarily configured, student table name can be studententity"
CREATE TABLE ' studententity ' (' num ' integer PRIMARY KEY not null, ' age ' integer NOT null DEFAULT ' + ', ' nam E ' TEXT not null default ' Blockcheng ', ' score ' REAL not null default ' 80.0 ', ' ClassID ' INTEGER, constrain T ' Student_class ' FOREIGN KEY (' classid ') REFERENCES ' class ' (' ID ')); CREATE INDEX idx_studententity_num on studententity (num);
1. Entity class object---->insert statement
This article expects this: to pass in an Entity object, generate SQL automatically, and then save it to SQLite via Fmdb.
Input
classentity* classeentity = [classentity new]; Classeentity.classname = @ "SOFTWARE02"; Classeentity.classid = 2;
studententity* student = [studententity new]; Student.age = 12; Student.score = 80; Student.classid = 2; Student.studentnum = 421125; Student.studentname = @ "Blockcheng";
Output
INSERT into class (' id ', ' name ') VALUES (: ID,: name) args:{id = 2; Name = "Software02";}
INSERT into Studententity ("age", ' name ', ' num ', ' score ', ' ClassID ') VALUES (: Age,: Name,: num,: Score,: ClassID) args:{ Age = 12; ClassID = 2; Name = "Blockcheng"; num = 421125; Score = 80;}
2. Entity class object----->update statement, based on primary key
Pass in an entity that already exists in SQLite, automatically generate an UPDATE statement, and update the condition based on the primary key.
Input
studententity* student = [studententity new]; Student.age = 12; Student.score = 80; Student.classid = 2; Student.studentnum = 421125; Student.studentname = @ "Blockcheng_update";
Output
UPDATE studententity SET age =: Age,name =: Name,num =: Num,score =: Score,classid =: classid WHERE num = ' 421225 ' args:{ Age = 12; ClassID = 2; Name = "Blockcheng_update"; num = 421225; Score = 80;}
3. Query condition----->SELECT Statement---> Entity class object
Generate SQL based on the query criteria passed in:
Input
. Entityclass = [Studententity class]; . Propertyarray = @[@ "Age", @ "ClassId", @ "score", @ "Studentname", @ "Studentnum"]; . Selection = @ "ClassId =?" and studentnum=? ";. Selectionargs = @[@1,@421128]; . by = @ "Studentnum ASC";
Output
SELECT age, ClassID, score, name, num from studententity WHERE classid =? and num=? ORDER by num ASC ARG: ( 1, 421128)
4. Entity class + condition---->delete statement
Generates a DELETE statement based on the class and condition passed in.
Input
. Entityclass = [Studententity class]; . Selection = @ "studentnum=?";. Selectionargs = @[@421138];
Output
DELETE from studententity WHERE num = ' 421138 '
5. Specify condition + entity class---->update, delete
Update Input:
. Entityclass = [Studententity class]; . Selection = @ "studentnum=?";. Selectionargs = @[@421128]; . update= @ "Studentname=?" [Email protected] [@ "Update_condition"];
Update output:
UPDATE studententity SET name=? WHERE num=? Args: ( "Update_condition", 421125)
Delete input:
. Entityclass = [Studententity class]; . Selection = @ "Studentnum <?";. Selectionargs = @[@421135];
Delete output:
DELETE from studententity WHERE num <? Args: ( 421135)
6. Thinking
How to use Runtime+fmdb to achieve the above mentioned effect?
0, how to abstraction and encapsulation, to meet the needs of the basic structure class, easy to use later?
1. How to deal with the mapping between entity and database table clearly, how to design the simple and easy-to-use grammar?
3. What are the basic general functions to provide?
(2) Anticipation + thinking "using Objective-c's runtime characteristics to implement lightweight ORM in conjunction with Fmdb"