A kind of database schema method and framework
Technical field
The present invention relates to iOS to develop database technical field, more particularly to a kind of database schema method and framework.
Background technology
Database is a kind of local persistent storage mode, has three kinds of databases in iOS exploitations, they are respectively
SQLite, CoreData and Realm, CoreData are that Apple Inc. releases API for data persistence, relative to
For SQLite, CoreData eliminates the trouble for writing SQL statement, and, as iOS native databases, advantage is combined with system for it
Good, the API of support is more, and it is slower that shortcoming is then in response to speed.Realm is increased income out by the Y Combinator start-up groups hatched
A cross-platform move database come, not only supports Android, iOS, also supports macOS, Linux, ReactNative
And Xamarin, the Realm support model in practice are tabulated, but shortcoming is not support cross-thread calls.SQLite is a
Light-weight database, still most commonly used database engine in the world, and increase income and cross-platform, its sentence sometimes
It is similar with MySQL, can be general with the relevant database on Android backstages, but invoked procedure is sufficiently complex.
At present, the application for database also has many extensions, such as Publication No. CN103793431A patent document
Disclose " a kind of new database access framework ", using the teaching of the invention it is possible to provide unified application programming interfaces call to programmer, program
Member need not understand the complicated data access implementation pattern of bottom, by the advanced algorithm of autonomous Design, programmer is only needed simply
Code call can realize delete, be newly-increased, renewal, the database manipulation such as inquiry, support the databases of all main flows, can be with
The development efficiency of programmer is greatly improved, improves the Programming's quality of programmer.Also Publication No. CN103699620A patent
Document discloses a kind of " method and system for realizing database manipulation in object-oriented using ORM frameworks ", and it is to all SQL
Sentence is uniformly processed, and can produce SQL scripts automatically, is avoided the trouble of a large amount of SQL statements of artificial memory, is improved exploitation
Efficiency.
Such as FMDB again, it is a set of third party's framework, and SQLite API is encapsulated, uses more face
To object, the C language code of many troubles, redundancy is eliminated, additionally it is possible to multithreading safety is provided, prevents data corruption, there is provided
Many facilities, but shortcoming be in the project directly in use, still not light enough, can not detaching structure query language
(SQL) sentence, it is more complicated to being used for the people for being unfamiliar with database;In addition, can not also directly be tabulated by model, it is necessary to
Every table is safeguarded one by one, but nearly hundred line code of every table, development cost are high.For above-mentioned problem, it is necessary to
Improvement is made to FMDB, carries out function optimization.
The content of the invention
The technical problem to be solved in the present invention is to be directed to above-mentioned the deficiencies in the prior art, there is provided one kind being capable of support model system
Table, multithreading safety and transaction rollback are supported, easy to operate, without artificial memory and input SQL statement, encapsulation is reliable, coupling
Spend low, database schema method applied widely and high durability and framework.
To achieve these goals, the technical solution adopted by the present invention is:
A kind of database schema method, methods described include:
New model foundation class and extension object base class are established, for meeting single inherit and the databases inherited under scene more
Use so that database manipulation is carried out by object;
Extend the new model foundation class, for by the automation of the sentence of SQL, format or module
Change, so that the sentence of the SQL of the object base class after the new model foundation class and extension calls.
Further, methods described also includes configuration database, for shared data bank object or whether verifies corresponding table
In the presence of.
Further, the configuration database includes:When database starts, calling or configuration data library name;Call or
Configuration database path;Singleton database object;Singleton isochronous queue.
Further, the new model foundation class includes being defined as below:Attribute list parses;Field constrains;Database
Action;
The extension object base class includes being defined as below:Major key increases certainly;Attribute list parses;Field constrains;Data
Storehouse acts.
Further, when the attribute list parsing includes the operation using the Object-Oriented Programming Language bottom for expanding C
Characteristic, mapping class attribute list and attribute type list are corresponded and are converted to form corresponding to database and attribute.
Further, the field constraint includes setting and ignores field and using SQL keyword for one
Individual field enters row constraint;
The database movement includes:Abstract structure query language statement;Encapsulate the abstract structure query language
Statement result.
A kind of database framework, the framework include:
Configuration layer, it is related for configuration database, it whether there is with shared data bank object or checking corresponding table;
Model layer, for establishing relevant rudimentary class, meet that single database inherited and inherited under scene more uses so that logical
Cross object and carry out database manipulation.
Further, the model layer includes:
New model basis generic module, attribute list parsing is performed for establishing new model foundation class;Field constrains;Database
Action;
Object base class expansion module, major key is performed from increasing for extending object base class;Attribute list parses;Field is about
Beam;Database movement.
Further, the configuration layer includes:
Configuration module is called, database name calls or configuration, database path call for when database starts, performing
Or configuration;
Singleton module, for by database object, isochronous queue singleton.
Further, the model layer also includes new model foundation class expansion module, for extending the new model basis
Class, by the automation of the sentence of SQL, format or modularization, after the new model foundation class and extension
The object base class SQL sentence call.
After adopting the above technical scheme, the beneficial effects of the invention are as follows:1st, by establishing new model foundation class, list is realized
Inherit and database manipulation is carried out by object under scene, database support model is tabulated, by extending pair in original FMDB
As foundation class, realize inherit also can carry out database manipulation more under scene by object;It is applicable so as to expand database
Scope;
2nd, by extending the new model foundation class, by sentence automation, formatting or the mould of SQL
Block is in case call so that database manipulation can be achieved without artificial memory and input SQL statement, improve development efficiency
Development cost is saved simultaneously;
3rd, by when database starts, call or configuration data library name;Calling or configuration database path;Singleton number
According to storehouse object;Singleton isochronous queue;So that database supports multithreading safety and transaction rollback;
4th, attribute list parsing is carried out by using characteristic during the operation for the Object-Oriented Programming Language bottom for expanding C, it is real
Showed directly invoking for SQL statement, and preferably ensure database need not each pair one open table and safeguarded, directly built by model
The function of table.
Brief description of the drawings
In order to illustrate the embodiments of the present invention more clearly or prior art technical scheme, accompanying drawing is as follows:
Fig. 1 is a kind of database schema method configuration diagram in the embodiment of the present invention 2;
Fig. 2 is a kind of database framework schematic diagram in the embodiment of the present invention 3.
Embodiment
It is the specific embodiment of the present invention and with reference to accompanying drawing below, technical scheme is further described,
But the present invention is not limited to these embodiments.
Existing FMDB frameworks are the database frameworks commonly used in iOS exploitations, and it is encapsulated in a manner of Objective-C
SQLite C language API, uses more object-oriented, eliminates many troublesome, redundancy C codes, contrast apple is certainly
The CoreData frameworks of band, more light weight and flexibly.But SQL statement operation can not be departed from, this is to being unfamiliar with database
Relative difficulty for operating personnel, operates also partially cumbersome for known technology people, and existing FMDB frameworks can not lead in addition
Model directly tabulation is crossed, it is necessary to be safeguarded to every table, and every table troublesome maintenance, is opened all without the code of nearly hundreds of row
It is high to send out cost.
The object of the invention is exactly to solve the above defect, makes database manipulation more light and flexible, improves development efficiency.
Embodiment 1
The present embodiment provides a kind of database schema method, and methods described includes:
New model foundation class and extension object base class are established, for meeting single inherit and the databases inherited under scene more
Use so that database manipulation is carried out by object;
In view of database can not directly carry out building table by object under existing FMDB frameworks, in the present embodiment, to existing
FMDB frameworks have carried out another layer of encapsulation, establish a new model foundation class --- and XTDBModel, the XTDBModel are inherited
Table under former data framework, while the method for encapsulating various database manipulations is realized with code, so enable to data
Storehouse operation directly can just carry out building table by an object, carry out the operation such as database insertion, deletion, inquiry, renewal.
Such as:
Thus database manipulation can be very convenient, as long as customized entity class is inherited from into XTDBModel, so that it may
To start operating database.
Further, do not propped up in the Object-Oriented Programming Language (Objective-C) due to expanding C in existing FMDB frameworks
Hold inherit scenes, inherit just refers to that subclass needs to inherit the scene of multiple parents more, i.e., existing FMDB frameworks do not support it is more after
The subclass held inherits XTDBModel, can not carry out follow-up database manipulation;In order to support to inherit the subclass energy of multiple parents
Database manipulation is enough carried out, so being extended to existing object base class (NSObject), forms a NSObject+
XTFMDB new object foundation class, it is encapsulated into and new model foundation class identical function;NSObject is institute in Objective-C
There is the base class of class, when each custom entities class carries out database manipulation, can inherit the data in NSObject, so expanding
NSObject is opened up, can be achieved to inherit the subclass of multiple parents in the case where not inheriting XTDBModel scene, also can be right by one
As using database manipulation.This make it that database manipulation is more intelligent, quick and smooth, have also been enlarged database and is applicable model
Enclose.
Extend the new model foundation class, for by the automation of the sentence of SQL (SQL), format or
Modularization, so that the SQL of the object base class after the new model foundation class and extension sentence calls.
Existing FMDB frameworks are when in use, it is necessary to by the input of SQL statement, this for being unfamiliar with the people of database and
Speech uses more complicated, and the mode of artificial memory and input is also comparatively laborious and efficiency is low in addition, and flexibility ratio is inadequate, right
XTDBModel is extended to form an AutoSql, is responsible for automating SQL statement, format, modularization, convenient
XTDBModel and NSObject+FMDB are called to it, realize the flexible operating without artificial memory and input SQL statement,
Improve database development efficiency.
Embodiment 2
As shown in figure 1, the present embodiment and the difference of embodiment before are, the present embodiment provides a kind of more detailed number
According to library architecture, methods described also includes configuration database, whether there is for shared data bank object or checking corresponding table.
The configuration database includes:When database starts, calling or configuration data library name;Calling or configuration database
Path;Singleton database object;Singleton isochronous queue.These are prepared for the initialization operation of data, facilitate number
It is more accurate when being operated according to storehouse, more targeted, database is supported multithreading safety and transaction rollback.
It is detailed in the new model foundation class (XTDBModel) to include being defined as below:Attribute list parses;Field is about
Beam;Database movement;
The extension object base class includes being defined as below:Major key increases certainly;Attribute list parses;Field constrains;Data
Storehouse acts.
Specifically:The foundation of new model foundation class and the extension implementation of object base class are essentially identical, and difference is
Major key (PKID), PKID are the attributes carried in FMDB models foundation class (DBModel), and the subclass for inheriting DBModel can be after
The XTDBModel in the present embodiment is held, therefore XTDBModel need not define PKID, and NSObject+XTFMDB is in the present embodiment
For no succession DBModel subclass service, so without PKID attributes, it is necessary to which self-defining major key adds PKID from increasing
Attribute is as digital major key, it is ensured that uniqueness.Wherein, NSObject+XTFMDB make use of Objective-C category special
Property it is full decoupled, can perfectly solve the scene inherited more, while the also perfect marrow for realizing software development high cohesion lower coupling.
The attribute list parsing includes the fortune using Object-Oriented Programming Language (Objective-C) bottom for expanding C
(Runtime) characteristic during row, Runtime systems can make class with dynamic creation class and object in the present embodiment using Runtime
Parsing, form corresponding to SQLite and attribute are converted to for mapping class attribute list and attribute type list to be corresponded,
The method that database manipulation can be directly invoked so that without as under existing FMDB frameworks, safeguarding that this is significantly to each table
The time of database access is shortened, improves efficiency.
The field constraint includes setting and ignores field and enter row constraint for a field using SQL keywords;Such as:
SQL keyword constraints are realized as follows:
(NSDictionary*)modelPropertiesSqliteKeywords
{
return@{
@"title":@" UNIQUE " ... // desired keyword is added, separated with space
};
}
Field is ignored in setting to be not intended to participation by listing and specifying in code and build the field of table, for example, some and regard
Scheme relevant attribute etc. so that database these fields for needing to ignore operation of automatic identification in operation;Such as:(NSArray*)
ignoreProperties
{
return@[
@" abcabc " ... //abcabc is mutually in requisition for the field ignored
];
}
The database movement definition includes:Abstract SQL statement;Encapsulate the abstract SQL statement result.Database movement
The premise of execution is to need corresponding data function definition, and the mode defined is as follows:First, SQL languages business used
Sentence is kept completely separate, and is taken out additions and deletions and is changed the various situations about being likely encountered looked into, such as batch insertion, renewal;According to condition inquiry etc.,
When abstract, isolate operation object and querying condition and be packaged into available database movement collection as parameter and SQL statement combination
Close, in case calling.
At present in the present embodiment, the operating method taken out is 11 kinds, is to insert single (insert), batch insertion respectively
(insertList) all data in single (update), batch updating (updateList), inquiry table, are updated
(selectAll) (selectWhere), is according to condition inquired about, single (findFirstWhere) is according to condition inquired about, according to condition inquires about
(hasModelWhere) whether is included, current Model (deleteModel) is deleted, according to condition deletes certain Model
(deleteModelWhere) this table (dropTable), is deleted, each own related code is realized, is applied generally to various
Operation to tables of data.
The present embodiment is the further optimization to FMDB frameworks, is integrated with its original good characteristic, supports multithreading peace
Entirely, operation failure transaction rollback is supported, can effectively prevent the confusion of data;And new function optimization is added, lower coupling is supported,
The fast run-up table of model of more scenes, the operation for departing from SQL statement input is supported, does not produce and conflicts with original FMDB frameworks, seal
Filled a variety of operating methods, met the operational requirements of multitype database table, realize it is simple, it is easy to operate, durability is high, can improve
Development efficiency.
Embodiment 3
As shown in Fig. 2 a kind of database framework, the framework includes:
Configuration layer 100, it is related for configuration database, it whether there is with shared data bank object or checking corresponding table;
Model layer 200, for establishing relevant rudimentary class, meet that single database inherited and inherited under scene more uses, make
Database manipulation must be carried out by object.
The model layer 200 includes:
New model basis generic module 210, attribute list parsing is performed for establishing new model foundation class;Field constrains;Number
Acted according to storehouse;
Object base class expansion module 220, major key is performed from increasing for extending object base class;Attribute list parses;Word
Section constraint;Database movement.
The configuration layer 100 includes:
Configuration module 110 is called, for when database starts, performing database name calling or configuration, database path
Call or configure;
Singleton module 120, for by database object, isochronous queue singleton.
The model layer 200 also includes new model foundation class expansion module 230, for extending the new model foundation class,
By the automation of SQL sentence, format or modularization, so as to the object base after the new model foundation class and extension
The SQL of class sentence calls.
It should be noted that the framework that the present embodiment provides is according to the database schema method pointed out in above-described embodiment
Come what is realized, the database schema method to be pointed out in above-described embodiment provides physical support and service.
It is as follows that the database framework of the present embodiment accesses operation implementation process:In view of some parts and existing FMDB frameworks
Under operation it is identical, therefore here is omitted, first, can be called in configuration layer 100 when database application APP starts and configure letter
Number, that is, perform database name and call or configure, database path calls or configuration, by database object, isochronous queue singleton
Action, so as to shared data bank object or checking corresponding table whether there is;Configuration layer 100 configure after the completion of, it is necessary to we from
A class Model is defined, and defines some attributes, such as age-sex wherein, then allows this Similar integral in new model
New model foundation class in basic generic module 210, new model foundation class can internally parse this Model, and generate correspondingly
A database tuple type, and the type of corresponding field is changed one by one;In database access process, we can also be right
Model setting constraints, such as there is which field to be not involved in building table, or the uniqueness of specified name, specify corresponding operation side
Method etc.;Then, the SQL statement encapsulated in new model foundation class expansion module 230 is called, operating database action, is at present 11
Kind, additions and deletions, which change, looks into, single batch etc., referring particularly to embodiment 2, without hand-written SQL statement again, the knot with regard to that can obtain our needs
Fruit, operation are flexible and light.
Specific embodiment described herein is only to spirit explanation for example of the invention.Technology belonging to the present invention is led
The technical staff in domain can be made various modifications or supplement to described specific embodiment or be replaced using similar mode
Generation, but without departing from the spiritual of the present invention or surmount scope defined in appended claims.