Database bonding layer-Based on protobuffer

Source: Internet
Author: User

Background:

Recently, I was suffering from a large number of database operations. Because database operations are performed by concatenating strings, an interface is required for each database operation service. As a result, operations on the same table need to be performed by a pile of interfaces. For example, the table person_t has three fields (age, name, and sex). If you update age and sex, you need an interface. If you want to update age, you need another interface, you also need a field for name update. This kind of interface will be derived from the business, which will lead to an extra high cost of interface explosion and concatenation string errors. When I was so miserable, I thought of using protobuf to solve this problem. proto_db was born.

 

Purpose:

Similar to saop technology, it is based on protobuf. It is used for serialization between database data and C ++ object data. Of course, this object can also be serialized on the network. Achieve two goals:

1. Automatic ing between database tables and objects. You do not need to think that the write struct corresponds to the database table fields one by one.

2. All database operations work with the inherent interface through the object. You do not need to write or splice the SQL statement interface separately (the explanation is a bit fuzzy. Continue to explain it below)

 

Horizontal comparison:

The list here is compared with the other two database operation libraries.

  Proto_db DTL OTL
Model Simple interface operation based on protobuf ODBC-based operations using STL algorithms and data structures Uses iostream operations based on APIs provided by various databases
Ease of use Very simple Simple Simple
Stability Personal work, not commercial, not quite mature Reliable Reliable
Scalability Convenient Relatively easy Not easy

 

Design Concept:

 

Example:

Let's take a look at the interface.

// Insert bool SQL _insert (const Google: protobuf: Message & MSG); // modify bool SQL _update (const Google: protobuf: Message & MSG, const Google :: protobuf: Message & where); // Delete bool SQL _delete (const Google: protobuf: Message & where); // query template <typename T, typename columnt> STD :: vector <STD: shared_ptr <t> SQL _select (const columnt & cols, const where_t & where );

The interface must be completed by passing in a protobuffer object. For select, You need to input the required column name and the queried database, so it is strange

Here, we assume that there is a person message object, which contains three attributes: name, ID, and email.

To insert an example:

Person person;person.set_name("test name");//person.set_email("test@qq.com");person.set_id(1);query.sql_insert(person);

 

Here, only the name attribute is set for person. A new record is inserted, but the email and ID are not inserted at the same time. The inserted data only contains the name field. This achieves the effect.

 

Let's look at an updated example:

Person person;person.set_name("test name");person.set_email("test@qq.com");        Person where_t;where_t.set_id(1);query.sql_update(person, where_t);

 

Here, two person objects are used. The first person object sets the name and email, and the second person object sets only the ID, that is to say, our update will update the name and email of the corresponding record according to the ID value of where_t. Is it especially convenient?

 

Is query a little troublesome?

// selectPerson person;typedef std::shared_ptr<Person> person_ptr;std::vector<person_ptr> persons = query.sql_select<Person>(     all_columns_t(),     (where_t("id") == 1 && where_t("name") == "test name"));std::for_each(persons.begin(), persons.end(), [](const person_ptr &val){    std::cout << val->name() << " "        << val->id() << " "        << val->email() << std::endl;});

The data queried by the current interface can only be of the vector type.

 

Performance:

The overall performance will be more than the direct use of the connected database, specifically in this regard:

1. Dynamic overhead for building SQL statements

2. protobuf parsing has dynamic overhead

 

Constraints:

Because reflection is used in protobuffer implementation, there are two constraints:

1. The database name is the object name

2. The field name in the database table is the interface Name of the object property.

To reduce repetitive work, a tool is provided to traverse database tables and generate corresponding CPP files. If you are interested, take a look.

 

Outlook:

Protobuffer is a very powerful weapon. It can be used not only for serialization, but also a lot. You want to use protobuffer to perform operations on objects from the database to the network. In fact, this technology has been available in other languages for a long time. Here, it is just imitating it to reduce the workload of C ++ development at ordinary times and increase productivity.

The following improvements:

1. Use the connection pool

2. Database Connection layer abstraction, supporting more database operation Libraries

3. Enhance the LINQ syntax and provide advanced functions, such as sorting, group, and join.

 

Download

GitHub: https://github.com/chenyu2202863/proto_db.git

This library uses a lot of C ++ 11 syntaxes. If you want to explore more C ++ school novelty, you can join this group of 165666547 (with a small advertisement)

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.