Database queries pattern

Hello, new poster here

I'm often creating programs with database access. The way I do it now is to have a class for each type. For example, let's say I'm creating a job database. Then I could have a class called users, jobs, companies, and so on. Each of these will have some fields in them. Then I would implement create, read, update and delete (CRUD) functions for each class.

My question is the following. Since the code for each CRUD-implementation is roughly the same, just with the table and the field names changed, would it be a good idea to create a single implementation for the CRUD functions, and then extend this to each class.

One way I guess I could do this, is by including the database table name in each class, and a list of all field names in the class, then write generic CRUD fucntions, that loop through a list of field names.

The benefit would be less maintenance, faster to create new classes, generic handling of each class, faster to implement new functions (like fx. a find function), however I guess i loose some flexebility. But of course I could just implement CRUD functions on single classes if customization is needed by overriding the fudntions.

Is this a good or bad idea, or am I missing a crucial problem? Should it be implemented in another way than my idea with the list? How do you guys handle multiple classes with similar CRUD-functions?
closed account (48T7M4Gy)
If it's a relational database then the normal SQL functionality and database connectivity or whatever the latest in ADO/ODBC is, is probably not worth tinkering with.

If you have an object oriented database the idea isn't a bad one provided you organise inheritance and the like in a rigorous and organised way.

However, to make that generic would be 'interesting' to say the least.

The data model is obviously key - <list>'s, <vector>s, <tuple>'s, <maps>'s ...
Yeah I see. I thought i could make a list of references to all variables that should be update in the database, but of course I can't do that because they are all of different types. I guess I can't make a list of pointers, where the pointers point to variables of different types? Maybe I could create a class that could hold, a int, double, string, datetime and so on, and make a list of that class. Then when i call that class to get the pointer it would return the one that is non-empty.

So there is no obvious way to do what I want? All people working with this kind of object oriented database write the CRUD-functions for every single class even though it is more or less the same each time?
closed account (48T7M4Gy)
Maybe I could create a class that could hold, a int, double, string, datetime and so on, and make a list of that class
Not maybe ... for sure because taht's what a class is designed to do, encapsulate data especially of different types.

All people working with this kind of object oriented database write the CRUD-functions for every single class even though it is more or less the same each time?
That's what I was talking about :) I reckon that's what happens but that doesn't mean there are no other possibilities. A problem that arises is where two or more classes have a relationship - one to many etc. but that is solved fairly easily with <map>'s and the like, or even within the relevant classes as methods.

There is plenty of reference material on OO databases.

As an observation though, you seem to be conflating the data types (classes) with the data model (<vector>'s etc). Both are easy to implement with an OO management program you design to do CRUD.

Keep in mind the create-C part is simple a constructor and add to a <vector> say. It's no 'bigly'. The others in CRUD aren't insurmountable ether.

So there is no obvious way to do what I want?
Yes, take a look how it is done in Wt:

https://www.webtoolkit.eu/wt/doc/tutorial/dbo.html

Since 'Action' can be anything, you may even extend the behavior to compare two datasets. You may use your own 'field' function.
Actually I think the approach where a certain templated function processes all fields is good.
Topic archived. No new replies allowed.