Totally OO C ++ SQL Wrapper?

So, I'm looking for a SQL wrapper for C ++ that completely hides any SQL text statements. I just can't find anything, I wonder why all the wrappers there seem to at some point want you to write a SQL text statement, for example:

SELECT * FROM stock WHERE item = 'Hotdog Buns' 

here is MySQL ++, for example:

 mysqlpp::Query query = conn.query("select * from stock where item = 'Hotdog Buns'"); 

The most obvious way to do this for me is to create a class that contains properties (columns), with each instance of this class being a row. Therefore, to execute the above request, I would do something like:

 // Class defined something like this... class stock_item : public sql::row { public: stock_item() : m_name( NULL ), m_amount( 0 ) {}; ~stock_item() {}; // Statically define the table static void CreateTable( void ) { // Some C++ reflective mechanism sql::column( "name", char[50] ); sql::column( "amount", u32 ); } private: const char* m_name; u32 m_amount; } // Then a table defined like this sql::table<stock_item> stock; // Query function defined something like this... stock GetHotDogBuns( const stock& shopStock ) { stock hotDogBuns = shopStock.Select( stock_item::Name(), "Hotdog Buns" ); return hotDogBuns; } 

Now I am not a SQL expert, and I didn’t think about the above code for a very long time, but this is just a logical way to deal with a database if your source code is in C ++ instead of being a database expert. What are the problems with such approach?

Is there an open source library that allows you to access the database in a similar way?

EDIT. The reason I would like something like this is because C ++ programmers using our code should not learn SQL syntax and provide a much more natural environment for coding them. I saw something like this in SilverStripe CMS written in php.

+4
source share
4 answers

Check out hiberlite and litesql .

+2
source

RogueWave, used (perhaps still has), has a C ++ database access library like this - using it was pure hell. SQL is a very powerful language, and encapsulating it all in C ++ classes is a very complicated proposition. In addition, at least you did not let me know what your motivation for this is.

+4
source

Quince is an open source C ++ library that eliminates the need to use SQL syntax or SQL types, but at the same time gives about the same expressiveness as SQL. It currently only supports PostgreSQL and sqlite, but you can always add new servers. See Quince-lib.com. (Full disclosure: I wrote it.)

+1
source

I wrote my own library, Fields and Records.

The Field class has methods such as:

 virtual std::string get_sql_creation_text(void) const = 0; virtual std::string get_sql_insert_data(void) const = 0; virtual std::string get_sql_where_clause_equals(void) const = 0; virtual std::string get_value_as_string(void) const = 0; 

My write class is a container of line pointers. I create SQL queries, field iterations using the above methods.

So one of my queries looks like this:

 12:37:41: Selecting rows for iterating using: SELECT * FROM Ing_Quantified LEFT JOIN Ing_Processing USING (ID_Processing) LEFT JOIN Ing_Process_Degrees USING (ID_Process_Degree) LEFT JOIN Ing_Process_Methods USING (ID_Process_Method) LEFT JOIN Ingredients USING (ID_Ingredient) LEFT JOIN Ing_Titles USING (ID_Title) LEFT JOIN Ing_Varieties USING (ID_Variety) LEFT JOIN Ing_Categories USING (ID_Category) LEFT JOIN Ing_Container_Sizes USING (ID_Container_Size) LEFT JOIN Ing_Container_Types USING (ID_Container_Type) LEFT JOIN Meas_Fundamentals USING (ID_Measurements) LEFT JOIN Meas_Systems USING (ID_System) LEFT JOIN Meas_Types USING (ID_Types) WHERE (ID_Recipe = 1); 

All this is done when processing records and fields as common. Fields return their names, which helps to create WHERE clauses in USING clauses above.

I used wxWidgets wxDbTable, but it does not just support shared fields and records.

0
source

Source: https://habr.com/ru/post/1299881/


All Articles