A method for implementing DB ORM in Flutter

By creating a specific class IHTTupleTwo in Flutter to obtain the key-value pairs of a Model instance and assemble SQL statements, the problem of low efficiency in DB ORM operations caused by reflection shielding is solved, achieving efficient database operations and improved code quality.

CN114756566BActive Publication Date: 2025-10-31INSPUR SOFTWARE CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202210169301.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-02-23
Publication Date
2025-10-31
Estimated Expiration
2042-02-23

AI Technical Summary

Technical Problem

Because Flutter blocks reflection from the Dart basic language, it cannot perform commonly used DB ORM operations, resulting in low efficiency, frequent errors, low readability, and a large amount of glue code.

Method used

By creating a specific class IHTTupleTwo in Flutter, the key-value pairs of Model instances are obtained and SQL statements are assembled to form a program capability library to implement DB ORM, including table creation, query, deletion and update operations.

Benefits of technology

It improves the efficiency of database operations, reduces the amount of glue code, and enhances the accuracy and readability of the code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114756566B_ABST
    Figure CN114756566B_ABST
Patent Text Reader

Abstract

This invention discloses a method for implementing a DB ORM in Flutter, belonging to the field of computer application technology. It obtains key-value pairs from the JSONObject of a Model instance to assemble the corresponding SQL. The implementation process is as follows: 1) First, declare a Tuple class; 2) Obtain the key-value pairs of the Model instance through the JSONSerializable API; 3) Iterate through all the key-value pairs obtained in step 2) and instantiate these data into a List. <tuple>4) The List obtained through step 3) <tuple>Assemble the create SQL statement; complete the design of the database ORM under Flutter. This invention can effectively improve the efficiency of database operations in the Flutter development environment.< / tuple> < / tuple>
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer application technology, specifically a method for implementing DB ORM in Flutter. Background Technology

[0002] When using Flutter for cross-platform development, database operations are hampered by Flutter's blocking of reflection in the Dart language, making it impossible to perform commonly used DB ORM operations. This results in low efficiency, a high risk of errors, poor readability, and the generation of a lot of glue code. Summary of the Invention

[0003] The technical objective of this invention is to address the above-mentioned shortcomings by providing a method for implementing a database ORM in Flutter, which can effectively improve the efficiency of database operations in the Flutter development environment.

[0004] The technical solution adopted by this invention to solve its technical problem is:

[0005] One method for implementing a DB ORM in Flutter involves retrieving key-value pairs from the JOSNObject of a Model instance and then assembling the corresponding SQL. The implementation process is as follows:

[0006] 1) First, declare the Tuple class;

[0007] 2) Obtain the key-value pair of the Model instance using the JSONSerializable generic API;

[0008] 3) Iterate through all the key-value pairs obtained in step 2) and instantiate these data into a List. <tuple>;

[0009] 4) The List obtained through step 3) <tuple>Assemble the CREATE SQL statement;

[0010] 5) Using the same method, assemble insert, update, and delete statements through steps 1) to 4) to complete the design of the DB ORM under Flutter.

[0011] This method creates a specific class to form a program capability library. This capability library can be used to implement a database ORM on the Flutter platform, thereby improving the ability of mobile devices to create, query, delete, and update database tables.

[0012] Preferably, create a Tuple class in Flutter. The Tuple class contains 2-6 properties, and the properties are of type dynamic.

[0013] The first attribute is defined based on the attribute name of the specified Model; the second attribute is either varchar or Text, and the data type is determined according to the attribute type of the Model. For example, the name attribute is of type String.

[0014] Then these attributes are put into a List for processing, and then SQL is assembled to realize the model mapping table creation operation of Flutter DB ORM.

[0015] When creating tables in the Flutter DB ORM, the aforementioned specific class is used. The first property is defined based on the property name of the specified Model, and the second property is either varchar or text. These specific class properties are grouped into a List for processing, and then the SQL is assembled to realize the model mapping table creation operation of the Flutter DB ORM. The same method can be used to complete operations such as querying, deleting, and updating.

[0016] Preferably, the Tuple class is the IHTTupleTwo class, which contains two attributes, item1 and item2, which are used to store the KEY name and VALUE type converted from the Model, respectively.

[0017] Furthermore, regarding the creation of model mapping tables for implementing Flutter DB ORM,

[0018] The first attribute item1 is defined based on the attribute name of the specified Model. When a table needs to be created based on Model(name, age, id), the first attribute is represented by IHTTupleTwo(name, data type).

[0019] The second attribute, item2, can be either varchar(255) or Text.

[0020] Furthermore, based on the stored List <ihttupletwo>The array is automatically assembled into a create SQL statement. Each IHTTupleTwo is iterated through, and a List is mapped based on the Model's attributes and values. <ihttupletwo>data;

[0021] Convert Model to JSONObject: Map<String,dynamic> Then, iterate through all the keys as one item1 of IHTTupleTwo, and the value type as one item2 of IHTTupleTwo.

[0022] Preferably, the key-value pair of the Model instance is obtained by using the JSONSerializable API to get a JSONObject(key-value(Map)).<String,dynamic> ) or List <Map<String,dynamic> >);

[0023] Then, by iterating through the List or directly using map.keys, you can obtain the type data of the attributes and values, thereby getting the actual Model data name and value type.

[0024] Preferably, step 3) involves obtaining all key-value pairs and instantiating these data into a List. <ihttupletwo>:

[0025] The runtime type of the value is obtained through the `runTimeType` property of the value, and the property name is obtained through `map.keys`. This object is instantiated using the `IHTTupleTwo` class declared in the first step and the key-value pair above.

[0026] Iterate through all key-value pairs to create all IHTTupleTwo instances, forming a List. <ihttupletwo>.

[0027] For example, if the key-value pair of the Model instance is 'name': 'smallDog', then the generated IHTTupleTwo instance will be item1: name, item2: varchar(255).

[0028] This invention also claims a system for implementing a DB ORM in Flutter, which automatically constructs a list of specific classes in Flutter and automatically assembles SQL statements from the list.

[0029] This system implements the aforementioned method for implementing DB ORM in Flutter.

[0030] The present invention also claims an apparatus for implementing DB ORM in Flutter, comprising: at least one memory and at least one processor;

[0031] The at least one memory is used to store a machine-readable program;

[0032] The at least one processor is configured to invoke the machine-readable program to execute the above-described method for implementing DB ORM in Flutter.

[0033] The present invention also claims protection for a computer-readable medium storing computer instructions that, when executed by a processor, cause the processor to perform the above-described method for implementing DB ORM in Flutter.

[0034] The method for implementing DB ORM in Flutter according to the present invention has the following advantages compared with the prior art:

[0035] This method designs a DB ORM to compensate for the lack of reflection features in Dart, the base language of Flutter, which makes it impossible to implement DB operations in Flutter development using industry-standard ORMs.

[0036] Using DB ORM improves the development efficiency of database operations, reduces the amount of glue code, and increases the accuracy of database operation code. Attached Figure Description

[0037] Figure 1 This is a schematic diagram of the method flow for implementing DB ORM in Flutter provided by an embodiment of the present invention. Detailed Implementation

[0038] Because the reflection feature of the Dart language is hidden in Flutter, the DB ORM implementation method used in other languages ​​is not feasible in Flutter.

[0039] This invention provides a method for implementing DB ORM in Flutter, which obtains key-value pairs through the JOSNObject of the Model instance to assemble the corresponding SQL.

[0040] In Flutter, first, some specific classes are created. These classes can contain 2-6 properties, and the property type is dynamic. In this example, a class IHTTupleTwo with two properties is created. The construction of Tuple classes with other numbers of properties is similar. When processing DB ORM table creation, the IHTTupleTwo class is used. The first property is defined according to the property name of the specified Model. For example, if a table needs to be created based on Model(name, age, id), the first property is represented by IHTTupleTwo(name, data type), and the second and third are similar. The data type is defined according to the property type of the Model. If the name type is String, then the second property of IHTTupleTwo when creating the table is varchar(255) or Text. Then, these IHTTupleTwos are collected and processed in a List. After that, the SQL is assembled to realize the model mapping table creation operation of Flutter DBORM. Other operations such as insert, update, delete are the same as create, which obtain key-value pairs from the JOSNObject of the Model instance and then assemble the corresponding SQL.

[0041] The IHTTupleTwo class contains two properties, item1 and item2, which are used to store the KEY name and VALUE type converted from the Model, respectively.

[0042] This method creates a specific class to form a program capability library. This capability library can be used to implement a database ORM on the Flutter platform, thereby improving the ability of mobile devices to create, query, delete, and update database tables.

[0043] Given that reflection is disabled in Flutter Dart, this method automatically constructs a list of IHTTupleTwo objects using a custom IHTTupleTwo class, and then automatically assembles the SQL statement from the list.

[0044] Based on the stored List <ihttupletwo>The array automatically constructs the create SQL. Iterate through each IHTTupleTwo. The first attribute item1 is the field name in the SQL, such as name; the second attribute item2 is the field value type in the SQL, such as varchar(255).

[0045] Map a List based on Model attributes and values. <ihttupletwo>Since Flutter's basic language reflection functionality is disabled, a method needs to be added to the Model (calling the JSONSerializable generic API) to convert the Model into a JSONObject: Map.<String,dynamic> Then, all keys are iterated through and each key is treated as an item1 of an IHTTupleTwo, and the value type is treated as item2 of an IHTTupleTwo. For example, if after conversion to a Map the first attribute is 'name' and the value type (runTimeType) is String, then item1 of IHTTupleTwo would be 'name' and item2 would be 'varchar(255)'.

[0046] like Figure 1 As shown, taking table creation as an example, the implementation of this method is as follows:

[0047] 1) First, declare the IHTTupleTwo class, which contains two properties, item1 and item2, to store the KEY name and VALUE type converted from the Model;

[0048] 2) After the Model data model is defined, since there is no reflection mechanism, it is impossible to obtain key-value data through system APIs. Flutter provides JSONSerializable, which can be used to obtain JSONObject(key-value(Map) data.<String,dynamic> ) or List <Map<String,dynamic> >), and then obtain the type data of the attributes and values ​​by iterating through the List or directly through map.keys. This step obtains the actual Model data name and value type;

[0049] 3) Iterate through the data obtained in step 2), obtain the runtime type of the value through `runTimeType`, and obtain the attribute name through `map.keys`; instantiate this object using the `IHTTupleTwo` class declared in step 1) and the above key-value pairs; iterate through all key-value pairs to create all `IHTTupleTwo` instances. For example, if a pair of `Model` instance pairs has `'name':'smallDog'`, then the generated `IHTTupleTwo` instances will be `item1:name, item2:varchar(255)`; thus forming a List. <ihttupletwo>.

[0050] 4) The List obtained through step 3) <ihttupletwo>Assemble the CREATE SQL statement;

[0051] 5) Using the same method, assemble insert, update, and delete statements through steps 1) to 4) to complete the design of the DB ORM under Flutter.

[0052] By declaring the IHTTupleTwo class to store the mapping between key names and value types of Model data (key-value values ​​are obtained through JSONObject), it is easy to assemble SQL to provide suggestions for database operations.

[0053] In practical applications, the specific operation process is as follows:

[0054] 1. Declare the IHTTupleTwo class,

[0055] Class IHTTupleTwo{

[0056] Dynamic item1;

[0057] Dynamic item2;

[0058] }

[0059] 2. Obtain the key-value pair of the Model instance using the JSONSerializable generic API;

[0060] 3. Iterate through all key-value pairs from step two and instantiate these data into a List. <ihttupletwo>;

[0061] 4. Assemble SQL.

[0062] This method is intended for use by technical professionals and can improve the efficiency of database operations in the Flutter development environment.

[0063] Flutter DB ORM improves code readability, increases developer efficiency, and reduces glue code.

[0064] This method has been deployed and applied in online projects such as the Cloud Collaboration Lite version.

[0065] This invention also provides a system for implementing DB ORM in Flutter, which automatically constructs a list of specific classes by creating specific classes in Flutter and automatically assembles SQL statements from the list;

[0066] This system implements the method for implementing DB ORM in Flutter as described in the above embodiments of the present invention.

[0067] This invention also provides an apparatus for implementing a DB ORM in Flutter, comprising: at least one memory and at least one processor;

[0068] The at least one memory is used to store a machine-readable program;

[0069] The at least one processor is configured to call the machine-readable program to execute the method for implementing DB ORM in Flutter as described in the above embodiments of the present invention.

[0070] This invention also provides a computer-readable medium storing computer instructions, which, when executed by a processor, cause the processor to perform the method for implementing DB ORM in Flutter as described in the above embodiments of this invention. Specifically, a system or apparatus equipped with a storage medium storing software program code that implements the functions of any of the above embodiments, and enabling the computer (or CPU or MPU) of the system or apparatus to read and execute the program code stored in the storage medium.

[0071] In this case, the program code read from the storage medium can itself implement the function of any of the above embodiments, and therefore the program code and the storage medium storing the program code constitute part of the present invention.

[0072] Examples of storage media used to provide program code include floppy disks, hard disks, magneto-optical disks, optical disks (such as CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-RAM, DVD-RW, DVD+RW), magnetic tapes, non-volatile memory cards, and ROMs. Alternatively, program code can be downloaded from a server computer via a communication network.

[0073] Furthermore, it should be clear that not only can the program code read by the computer be executed, but also the operating system or other components operating on the computer can be instructed based on the program code to perform some or all of the actual operations, thereby realizing the function of any of the embodiments described above.

[0074] Furthermore, it is understood that the program code read from the storage medium is written to the memory set in the expansion board inserted into the computer or to the memory set in the expansion unit connected to the computer. Then, based on the instructions of the program code, the CPU or other components installed on the expansion board or expansion unit execute some and all of the actual operations, thereby realizing the function of any of the embodiments described above.

[0075] The present invention has been shown and described in detail above with reference to the accompanying drawings and preferred embodiments. However, the present invention is not limited to these disclosed embodiments. Based on the above embodiments, those skilled in the art will know that more embodiments of the present invention can be obtained by combining the code review methods in the different embodiments. These embodiments are also within the protection scope of the present invention.< / ihttupletwo> < / ihttupletwo> < / ihttupletwo> < / ihttupletwo> < / ihttupletwo> < / ihttupletwo> < / ihttupletwo> < / ihttupletwo> < / ihttupletwo> < / tuple> < / tuple>

Claims

1. A method for implementing a DB ORM in Flutter, characterized in that, The method retrieves key-value pairs from the JOSNObject of the Model instance and then assembles the corresponding SQL. The implementation process is as follows: 1) First, declare the Tuple class; 2) Obtain the key-value pair of the Model instance using the JSONSerializable generic API; 3) Iterate through all the key-value pairs obtained in step 2) and instantiate these data into a List. <tuple> ;< / tuple> 4) The List obtained through step 3) <tuple> Assemble the CREATE SQL statement;< / tuple> 5) Using the same method to assemble insert, update, and delete statements through steps 1) to 4), complete the design of the DB ORM under Flutter; In Flutter, create a Tuple class. The Tuple class contains 2-6 properties, and the properties are of type dynamic. The first attribute is defined based on the attribute name of the specified Model; the second attribute is either varchar or Text, and the data type is determined according to the attribute type of the Model. For example, the name attribute is of type String. Then these attributes are put into a List for processing, and then SQL is assembled to realize the model mapping table creation operation of Flutter DB ORM; The Tuple class is the IHTTupleTwo class, which contains two properties, item1 and item2, which are used to store the KEY name and VALUE type converted from the Model, respectively. For implementing model mapping table creation in Flutter DB ORM The first attribute item1 is defined based on the attribute name of the specified Model. When a table needs to be created based on Model(name, age, id), the first attribute is represented by IHTTupleTwo(name, data type). The second attribute, item2, can be either varchar(255) or Text; Based on the stored List <ihttupletwo>The array is automatically assembled into a create SQL statement. Each IHTTupleTwo is iterated through, and a List is mapped based on the Model's attributes and values. <ihttupletwo> data;< / ihttupletwo> < / ihttupletwo> Convert Model to JSONObject: Map<String,dynamic> Then, iterate through all the keys as one item1 of IHTTupleTwo, and the value type as one item2 of IHTTupleTwo.

2. The method for implementing DB ORM in Flutter according to claim 1, characterized in that, The key-value pair of the Model instance is obtained by using the JSONSerializable API to get a JSONObject(key-value(Map)).<String, dynamic> ) or List <Map<String, dynamic> >); Then, by iterating through the List or directly using map.keys, you can obtain the type data of the attributes and values, thereby getting the actual Model data name and value type.

3. The method for implementing DB ORM in Flutter according to claim 1, characterized in that, Step 3) involves obtaining all key-value pairs and instantiating these data into a List. <ihttupletwo> :< / ihttupletwo> The runtime type of the value is obtained through the `runTimeType` property of the value, and the property name is obtained through `map.keys`. This object is instantiated using the `IHTTupleTwo` class declared in the first step and the key-value pair above. Iterate through all key-value pairs to create all IHTTupleTwo instances, forming a List. <ihttupletwo> 。< / ihttupletwo> 4. A system for implementing DB ORM in Flutter, characterized in that, Automatically construct a list of specific classes by creating specific classes in Flutter, and automatically assemble SQL statements from the list; The system implements the method for implementing DB ORM in Flutter as described in any one of claims 1 to 3.

5. An apparatus for implementing a DB ORM in Flutter, characterized in that, include: At least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is configured to invoke the machine-readable program to execute the method for implementing DB ORM in Flutter as described in any one of claims 1 to 3.

6. A computer-readable medium, characterized in that, The computer-readable medium stores computer instructions that, when executed by a processor, cause the processor to perform the method for implementing DB ORM in Flutter as described in any one of claims 1 to 3.