473,473 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

class objects to database translation

Hi,

The problem is :

You have a class A with one or more data variables. You want to store the
data variables in a database for example MySQL. Also you do not want to have
SQL code in your class A. Is there a way to translate the Object A to a sql
independed structure. I think it is something with persistent objects or am
I wrong. has anyone experience with this ?

John
Jul 22 '05 #1
1 1495
"Johan" <me@knoware.nl> wrote in message news:<10*************@corp.supernews.com>...
Hi,

The problem is :

You have a class A with one or more data variables. You want to store the
data variables in a database for example MySQL. Also you do not want to have
SQL code in your class A. Is there a way to translate the Object A to a sql
independed structure. I think it is something with persistent objects or am
I wrong. has anyone experience with this ?

John


What I've done in this situation is implement a container-like class
hierarchy that encapsulates all of the database functionality. For
each class that you want to store in the database you create a new
container class that knows how to translate the database
representation into the C++ representation.

Here's the (very) basic idea:

class Foo { /* a normal class */ };

class Database { /* execute SQL statements using MySQL API */ };

template <typename Type>
class DatabaseSet
{
public :
// implement a persistent container interface
class Iterator;
class ConstIterator;
DatabaseSet& store(); // store objects in database using SQL
DatabaseSet& retrieve(); // retrieve objects from database using
SQL
Iterator insert(const Type& type);
ConstIterator find(const Type& target) const;
Iterator find(const Type& target);
// etc.
virtual ~DatabaseSet();

protected :
DatabaseSet(const Database& database) {} // abstract base class
// generate SQL statements
virtual std::string select_query() const = 0;
virtual std::string insert_query(const Type& type) const = 0;
virtual std::string update_query(const Type& type) const = 0;

private :
SomeContainer<Type> m_container;
Database m_database; // connection to MySQL database
};

class FooSet : public DatabaseSet<Foo>
{
public :
FooSet(const Database& database) : DatabaseSet<Foo>(database) {}

private :
// Foo-specific SQL
virtual std::string select_query() const;
virtual std::string insert_query() const;
virtual std::string update_query() const;
};
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Michael Klatt | last post by:
I am working on a library which uses the GKS graphics package. I have a Gks object which opens the GKS subsystem in its constructor: // Gks.hpp class Gks { public : Gks(); };
8
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
5
by: BigMan | last post by:
Does the standard define the order in which static members of a class are created and initialized?
7
by: A_StClaire_ | last post by:
hi, I'm working on a project spanning five .cpp files. each file was used to define a class. the first has my Main and an #include for each of the other files. problem is my third file...
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
6
by: Orgun | last post by:
Hi, I sent this message to the moderated c++ group too but it is waiting for moderator approval and I wanted to send here too. I am new to Design Patterns. I want to write a simple...
0
by: PopeDarren | last post by:
_Short version_: Is it possible to directly access the hyperlink objects in the MenuItem and TreeNode objects on the server-side? If so, how do I do it? _Long version_: I'm getting to the end...
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.