Background
The database is a local persistence storage mode, three databases are respectively SQLite, coreData and Realm in the development of the iOS, coreData is an API for data persistence, compared with SQLite, coreData omits the trouble of writing SQL sentences, and the CoreData is used as an iOS original database, has the advantages of being well combined with a system, supporting more APIs and having the disadvantage of slower response speed. Realm is a cross-platform mobile database which is opened by a startup team hatched by Y combiners, supports not only Android, iOS, but also macOS, linux, reactive and Xamarin, and supports model tabulation in actual application, but has the disadvantage of not supporting cross-thread calls. The SQLite is a lightweight database, is a database engine which is most used in the world, is open-source and cross-platform, is similar to MySQL in terms of sentences, can be commonly used with a relational database of an Android background, and has quite complex calling process.
At present, there are many extensions to database applications, for example, patent document with publication number CN103793431a discloses a "new database access framework", which can provide a unified application program interface for a programmer to call, the programmer does not need to know the complex data access implementation mode of the bottom layer, and through an advanced algorithm designed independently, the programmer can implement database operations such as deletion, addition, update, query, etc. only by simple code call, so as to support all mainstream databases, and can greatly improve the development efficiency of the programmer and the programming quality of the programmer. The patent document with publication number CN103699620A discloses a method and a system for realizing database operation by using an ORM framework in an object-oriented mode, which uniformly processes all SQL sentences, can automatically produce SQL scripts, avoids the trouble of manually memorizing a large number of SQL sentences, and improves the development efficiency.
For example, the FMDB is a third party framework, encapsulates the API of SQLite, is more object-oriented to use, omits a plurality of troublesome and redundant C language codes, can provide multithread security, prevents data confusion and provides a plurality of convenience, but has the defects that the use is still not portable enough when the software is directly used in a project, cannot deviate from a Structured Query Language (SQL) statement, and is relatively complex for people unfamiliar with a database; in addition, the table cannot be directly made through the model, each table needs to be maintained one by one, however, the development cost of each table is high due to the fact that the code of each table is close to hundred rows. Aiming at the problems, the FMDB needs to be improved to optimize the functions.
Disclosure of Invention
Aiming at the defects of the prior art, the invention provides the database architecture method and the framework which can support model tabulation, multithreading security and transaction rollback, are simple and convenient to operate, do not need artificial memory and SQL statement input, are reliable in encapsulation, have low coupling degree, and have wide application range and high reusability.
In order to achieve the above purpose, the technical scheme adopted by the invention is as follows:
a database architecture method, the method comprising:
establishing a new model basic class and an extended object basic class, which are used for meeting the use of databases under single inheritance and multiple inheritance scenes, so that the database operation is carried out through the objects;
and expanding the new model basic class, and automatizing, formatting or modularization the sentences of the structured query language so as to call the sentences of the structured query language of the new model basic class and the expanded object basic class.
Further, the method also includes configuring a database for sharing database objects or verifying whether a correspondence table exists.
Further, the configuration database includes: when the database is started, calling or configuring the name of the database; invoking or configuring a database path; singulating database objects; the synchronization queue is instantiated.
Further, the new model base class includes the following definitions: analyzing an attribute list; field constraints; database actions;
the extended object basic class comprises the following definitions: the primary key is self-increased; analyzing an attribute list; field constraints; database actions.
Further, the attribute list analysis includes converting the mapping type attribute list and the attribute type list into the format and the attribute corresponding to the database by adopting the runtime characteristic of the object-oriented programming language bottom layer of the extended C.
Further, the field constraints include setting an ignore field and constraining one field with a structured query language key;
the database actions include: abstracting structured query language statements; and packaging the abstract structured query language statement result.
A database framework, the framework comprising:
a configuration layer for configuring database correlation to share database objects or verify whether a corresponding table exists;
and the model layer is used for establishing related basic classes, and meeting the requirement of database use under single inheritance and multiple inheritance scenes, so that database operation is carried out through objects.
Further, the model layer includes:
the new model basic class module is used for establishing a new model basic class to execute attribute list analysis; field constraints; database actions;
the object basic class expansion module is used for expanding the object basic class to execute the main key self-increment; analyzing an attribute list; field constraints; database actions.
Further, the configuration layer includes:
the calling configuration module is used for executing database name calling or configuration and database path calling or configuration when the database is started;
and the unionizing module is used for unionizing the database objects and the synchronous queues.
Further, the model layer further includes a new model base class extension module, configured to extend the new model base class, and automate, format, or modularize the statement of the structured query language, so as to call the statement of the structured query language of the new model base class and the extended object base class.
After the technical scheme is adopted, the invention has the beneficial effects that: 1. by establishing a new model basic class, database operation through objects in a single inheritance scene is realized, the database supports model tabulation, and by expanding the object basic class in the original FMDB, database operation can be realized through objects in a multi-inheritance scene; thereby expanding the application range of the database;
2. by expanding the new model basic class, the sentences of the structured query language are automated, formatted or modularized for ready calling, so that database operation can be realized without manual memory and SQL sentence input, the development efficiency is improved, and the development cost is saved;
3. by calling or configuring a database name when the database is started; invoking or configuring a database path; singulating database objects; a singleton synchronous queue; causing the database to support multi-threaded security and transaction rollback;
4. by adopting the runtime characteristic of the object-oriented programming language bottom layer of the extension C to analyze the attribute list, the direct call of SQL sentences is realized, the database is better ensured not to maintain one table each time, and the function of building the table directly through a model is realized.
Detailed Description
The following are specific embodiments of the present invention and the technical solutions of the present invention will be further described with reference to the accompanying drawings, but the present invention is not limited to these embodiments.
The existing FMDB framework is a database framework commonly used in iOS development, and encapsulates the C language API of SQLite in an Objective-C mode, so that the framework is more object-oriented in use, a plurality of troublesome and redundant C codes are saved, and compared with the CoreData framework of an apple, the framework is lighter and more flexible. But still can not break away from SQL statement operation, this is relatively difficult for the operator who is unfamiliar with the database, and it is also complicated to operate for familiar skilled person, and in addition, current FMDB frame can not directly tabulate through the model, need to maintain every table, and every table does not have the code of nearly several hundred lines, and it is troublesome to maintain, and development cost is high.
The invention aims to solve the defects, so that the database operation is lighter and more flexible, and the development efficiency is improved.
Example 1
The embodiment provides a database architecture method, which comprises the following steps:
establishing a new model basic class and an extended object basic class, which are used for meeting the use of databases under single inheritance and multiple inheritance scenes, so that the database operation is carried out through the objects;
in view of the fact that the database under the existing FMDB frame cannot be directly built through objects, in the embodiment, the existing FMDB frame is further packaged, a new model basic class, namely the XTDBM model, is built, the XTDBM model inherits the tables under the original data frame, and meanwhile, the method for packaging various database operations is achieved through codes, so that the database operations can be directly built through one object, and operations such as database insertion, deletion, query and update are performed.
For example:
thus, the database operation is very convenient, and the database can be started to be operated only by inheriting the custom entity class from the XTDBM model.
Further, since the multi-inheritance scene is not supported in the object-oriented programming language (objected-C) of the extended C in the existing FMDB framework, the multi-inheritance refers to a scene that the sub-class needs to inherit a plurality of parent classes, that is, the existing FMDB framework does not support the multi-inheritance sub-class inheritance XTDBModel, and the subsequent database operation cannot be performed; in order to support the sub-class inheriting multiple parent classes to enable database operations, the existing object base class (NSObject) is extended to form a new object base class of NSObject+XTFMDB, and the new object base class is encapsulated with the same function as the new model base class; the NSObject is the base class of all classes in the Objective-C, and each custom entity class inherits the data in the NSObject when performing database operation, so that the NSObject is expanded, and the operation that the subclasses inheriting multiple parent classes can also use the database operation through one object under the scene of not inheriting the XTDBM model can be realized. The method ensures that the database operation is more intelligent, quick and smooth, and also expands the application range of the database.
And expanding the new model basic class, wherein the new model basic class is used for automating, formatting or modularization of the statement of the Structured Query Language (SQL) so as to facilitate the statement call of the SQL of the new model basic class and the expanded object basic class.
When the existing FMDB framework is used, the input of SQL sentences is needed, the use is complex for people unfamiliar with the database, in addition, the manual memory and input modes are complex and low in efficiency, the flexibility is not enough, the XTDBM is expanded to form an AutoSql, the SQL sentences are automated, formatted and modularized, the SQL sentences are conveniently called by the XTDBM and NSObject+FMDB, the flexible operation without the manual memory and input of the SQL sentences is realized, and the development efficiency of the database is improved.
Example 2
As shown in fig. 1, the difference between this embodiment and the previous embodiment is that this embodiment provides a more detailed database architecture method, and the method further includes configuring a database for sharing a database object or verifying whether a correspondence table exists.
The configuration database comprises: when the database is started, calling or configuring the name of the database; invoking or configuring a database path; singulating database objects; the synchronization queue is instantiated. These are all prepared for the initialization operation of the data, and are convenient for the database to operate more accurately and more specifically, so that the database supports multi-thread security and transaction rollback.
The new model base class (XTDBModel) includes the following definitions in detail: analyzing an attribute list; field constraints; database actions;
the extended object basic class comprises the following definitions: the primary key is self-increased; analyzing an attribute list; field constraints; database actions.
Specifically: the establishment of the new model base class is basically the same as the extended implementation of the object base class, except that the Primary Key (PKID), which is a self-contained attribute in the FMDB model base class (DBModel), and the subclass inheriting the DBModel can inherit the XTDBModel in this embodiment, so that the XTDBModel does not need to define the PKID, while in this embodiment, nsobject+xtfmdb serves the subclass which does not inherit the DBModel, so that there is no PKID attribute, the primary key self-increment needs to be defined by itself, and the PKID attribute is added as a digital primary key, so as to ensure uniqueness. The NSObject+XTFMDB utilizes category characteristics of Objective-C to completely decouple, can perfectly solve a scene of multiple inheritance, and simultaneously perfectly realizes the essence of high cohesion and low coupling of software development.
The attribute list analysis includes using the Runtime (run) feature of the object-oriented programming language (Objective-C) bottom layer of the extended C, and the run system can dynamically create classes and objects, in this embodiment, the run system uses run as the analysis of the classes, which is used to convert the mapping class attribute list and the attribute type list into the format and attribute corresponding to SQLite in a one-to-one correspondence, and can directly call the database operation method, so that it is not necessary to maintain each table like in the existing FMDB framework, which greatly shortens the time of database access, and improves the efficiency.
The field constraint comprises setting an ignore field and utilizing SQL keywords to constraint on one field; for example: the SQL key constraint is implemented as follows:
(NSDictionary*)modelPropertiesSqliteKeywords
{
return@{
the @ "title" @ "UNIQUE" @, "I/O add the desired keywords, separated by spaces
};
}
Setting the ignore field may cause the database to automatically identify these fields requiring ignore operations at the time of operation by listing and specifying in the code fields that are not intended to participate in the construction of the table, such as some view-related attribute, etc.; such as: (NSArray. RTM.) ignoreProperties
{
return@[
@ "abcab",.. the// abcab is the corresponding field that needs to be ignored
];
}
The database action definition includes: abstract SQL statements; and packaging the abstract SQL statement result. The precondition for the execution of the database actions is that the corresponding data function definition is required, and the defining manner is as follows: firstly, completely separating SQL sentences used by the service, and abstracting various conditions possibly encountered by adding, deleting and checking, such as batch insertion and updating; and according to conditional query and the like, when in abstraction, separating an operation object and query conditions as parameters and combining SQL sentences to package into an available database action set for calling.
In the present embodiment, the abstracted operation methods are 11 kinds, namely, inserting a single (insert), inserting a batch (insert list), updating a single (update), updating a batch (update list), querying all data (selected all) in a table, querying by condition (selected white), querying by condition (findfirstwhite), querying by condition whether the query by condition contains (hasModelwhite), deleting the current Model (deleteModel), deleting a certain Model (deleteModelWhere) by condition, deleting the table (drop table), and each have related code implementation, which is basically applicable to various operations of the data table.
The embodiment further optimizes the FMDB frame, integrates the original excellent characteristics, supports multi-thread security, supports operation failure transaction rollback, and can effectively prevent data confusion; and new function optimization is added, low coupling is supported, a multi-scene model is quickly built, the operation of separating SQL statement input is supported, no conflict is generated with the original FMDB frame, various operation methods are packaged, the operation requirements of various database tables are met, the implementation is simple, the operation is convenient, the reusability is high, and the development efficiency can be improved.
Example 3
As shown in fig. 2, a database framework, the framework comprising:
a configuration layer 100 for configuring database correlation to share database objects or to verify whether a corresponding table exists;
and the model layer 200 is used for establishing relevant basic classes, and meeting the requirement of database use in single-inheritance and multi-inheritance scenes, so that database operation is performed through objects.
The model layer 200 includes:
a new model base class module 210, configured to establish a new model base class to perform attribute list parsing; field constraints; database actions;
an object base class extension module 220, configured to extend an object base class to perform primary key self-augmentation; analyzing an attribute list; field constraints; database actions.
The configuration layer 100 includes:
a calling configuration module 110, configured to execute database name calling or configuration and database path calling or configuration when the database is started;
the unionizing module 120 is configured to unionize the database object and the synchronization queue.
The model layer 200 further includes a new model base class extension module 230 for extending the new model base class, and automating, formatting or modularizing the SQL statements so that the new model base class and the extended SQL statements of the object base class are called.
It should be noted that, the framework provided in this embodiment is implemented according to the database architecture method indicated in the foregoing embodiment, and provides physical support and services for the database architecture method indicated in the foregoing embodiment.
The implementation process of the database frame access operation in this embodiment is as follows: in view of the fact that some parts are the same as those under the existing FMDB framework, the description is omitted herein, firstly, when the configuration layer 100 will call the configuration function when the database application APP is started, that is, perform the database name call or configuration, the database path call or configuration, and the act of singulating the database objects and the synchronization queue to share the database objects or verify whether the corresponding tables exist; after the configuration layer 100 is configured, we need to customize a class Model and define some attributes therein, such as age, gender, etc., and then let such inherit the new Model base class in the new Model base class module 210, the new Model base class internally analyzes the Model, generates a corresponding database tuple type and converts the types of the corresponding fields one by one; in the process of accessing the database, we can also set constraints on the Model, such as which fields do not participate in building a table, or specify the uniqueness of the name, specify the corresponding operation method, etc.; then, the SQL statement encapsulated in the new model basic class extension module 230 is called to operate database actions, which are 11 types, add-delete-modify-check, single batch and the like at present, and the result needed by users can be obtained without handwriting the SQL statement in detail in the embodiment 2, so that the operation is flexible and portable.
The specific embodiments described herein are offered by way of example only to illustrate the spirit of the invention. Those skilled in the art may make various modifications or additions to the described embodiments or substitutions thereof without departing from the spirit of the invention or exceeding the scope of the invention as defined in the accompanying claims.