0

I want to use << and >> operator to load and save to an external database. What would the external database have to implement for that to work?

Thoughts or experiences? I am using SQL to interact with my sqlite3 database.

For instance, I want to say:

MyClass c;
c >> databaseInstance;
c << databaseInstance;
Alexander
  • 23,432
  • 11
  • 63
  • 73
reza
  • 5,972
  • 15
  • 84
  • 126
  • Well the << operator wants to operate on "streams". I can see that databases probably COULD SOMEHOW be exposed as streams... But what would you want to achieve by READING from a database instance? What should the db read? – sinni800 Nov 26 '12 at 17:38
  • 1
    its a very odd abstraction. >> and << work on stream-like things. A database is not a stream-like thing. Its more like a dictionary – pm100 Nov 26 '12 at 17:40
  • No you don't. operator overloading is somethimng to make your code more succinct and clearer. If I saw theose operators used with a DB, it would be seen as terse and opaque. – Tony Hopkinson Nov 26 '12 at 17:53

2 Answers2

3

databaseInstance would need to be of some type that provides operator>> and operator<< functions that write to the database in the way that you want, thus providing a stream-like interface. These functions will be defined somewhat like so:

database_type& operator<<(database_type& databaseInstance, MyClass& c)
{
  // Write to database here
  return databaseInstance;
}

database_type& operator>>(database_type& databaseInstance, MyClass& c)
{
  // Read from database here
  return databaseInstance;
}

This makes use of operator overloading. There's not much more to say than that and it's a pretty odd thing to do with a database as a database is not very much like a stream.

You probably want to reverse the usage of the operators for the sake of consistency with the C++ standard library:

MyClass c;
databaseInstance << c;
databaseInstance >> c;

However, it's uncertain exactly what the final line would read into your MyClass object. The second line only really makes sense if this databaseInstance has been configured to insert into a particular table.

Community
  • 1
  • 1
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • 1
    Exactly what I thought. A database instance has so many things it could return, it doesn't really make sense in this way. Also: In which table, database, etc should it insert in the first case? I guess it could find out by reflecting the actual object, but still. – sinni800 Nov 26 '12 at 17:41
  • @MooingDuck Hope you feel better. :D – Joseph Mansfield Nov 26 '12 at 19:07
1

hmm. interesting..

ok, I assume that you just want to select from the DB in the case of operator>> and insert/update in the case of operator<<. The trick here is knowing what columns to read/write to.

You could use a stream manipulator (like "hex" that turns an integer output into hex format) to determine which column to use, so you'd have something like

int c;
DBStreamInterface db(tableA);
c << columnA << db;

or you could define a struct type that had the same layout as a table, and read the entire row into it:

struct TableRow {
  int columnA;
  string columnB;
};
TableRow t << db;

but you'd have to be careful with matching the schema in the DB to the definition of the struct (unless you wanted to go go all-out in template code generation where you could create the appropriate struct by reading the schema DDL and dynamically allocating storage for each field in a class constructor).

Community
  • 1
  • 1
gbjbaanb
  • 51,617
  • 12
  • 104
  • 148