CppRestApi database

Admin私のストア

The Database class of CppRestApi specifies the Engine (Mysql) and Format (UserFormat, etc.) as template parameters. The query string is set with the setQuery method, and the query is executed with the execute method. Finally, the record is retrieved with the get method, and converted to a Format type with the engine's setRecord method. A series of methods can be executed as method chains. The execute method sets the argument to the query placeholder as a template parameter pack. The get method converts the execution result to a tuple type defined by the Format type of the record in the Engine class. The result is then saved in a Format::Record list.

Database db{Engine, Format};

std::list<Format::Record> result = db.setQuery(query_string).execute(paramaters...).get();

The Query class of CppRestApi wraps the method chains of the Database class with select, update, del, and insert methods.

The Engine class implements the details of the database. You can replace the Mysql implementation class with another database implementation class as the template parameter of the Database class, as long as it provides the same methods.

In the Mysql class, just like the Database class, you set the query string with the setQuery method and execute the query with the execute method.

session . sql (query). bind (paramaters...). execute ()

The setRecord method converts the Row type to a Tuple type defined by the Format type.

The Tuple type is the standard tuple type, and Row is the mysql-connector's Row type.

indices is a template parameter for the std::get method.

setRecord ( Tuple & record , row & row , std :: index_sequence <indices...>)

In database design, the Database class receives query results from the implementation class and creates a Record list of the Format class.

The Query class wraps the Database class with common method names.

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.