472,353 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 1453
"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...
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...
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...
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...
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:...
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...
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...
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.