Archived

This topic is now archived and is closed to further replies.

wild_pointer

databases

Recommended Posts

Anyone know of any tutorials on database programming in c? I don't want to use any existing technologies, and preferably not overly complex - im just interested in basic design. If not some ideas on how to best set up indexing without any specific types of data in mind would be helpful. something like:
  
database<int, std::string, bool, char, float> my_db; 
  
would be really cool, but I have no idea how to do that without knowing how many entries im going to need. [EDIT] stupid tags
[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people [edited by - wild_pointer on November 2, 2002 5:05:48 PM]

Share this post


Link to post
Share on other sites
You want to write a database engine from scratch? That is pretty ambitious, even for a simple engine. The trick is to weak-type tables is to store the data using void pointers:


int iValue = 234;
char *string = "Hello";

database.storeData ((void *)&iValue);
database.storeData ((void *)string);


This isn''t a perfect solution, but it does work very well for most applications. Another idea is to store all data as strings and convert as needed.



int iValue = 234;
char string[32];
sprintf (string, "%d", iValue);
database.storeData (string);

char *data = database.getData ();
iValue = atoi (data);


That way may not be quite as fast, though.

Share this post


Link to post
Share on other sites
spiffgq: You want to write a database engine from scratch? That is pretty ambitious, even for a simple engine.

Well, that depends on what you''re considering simple... writing your own SQL database from scratch is very impractical, but reimplementing something along the lines of dbm/ndbm/gdbm is very feasible (gdbm has only 12 functions total). One of my friends and I are working on a database for our semester project which will hopefully be somewhere between gdbm and berkeley db.

The trick is to weak-type tables is to store the data using void pointers:

That''s one way to do it... but the way we''re planning on doing ours is using a templated database class parameterized by a typelist of key/data pairs so it''s type-safe. The main issue I''m struggling with is whether we should restrict it to POD''s or allow objects to be serialized too.

Share this post


Link to post
Share on other sites
quote:
Original post by damienj
That''s one way to do it... but the way we''re planning on doing ours is using a templated database class parameterized by a typelist of key/data pairs so it''s type-safe. The main issue I''m struggling with is whether we should restrict it to POD''s or allow objects to be serialized too.


Using templates will allow each "table" to have different data types, but all the data within that table will have to be of the same type. That would only really be usefull (IMHO) if you''re storing large data structures like classes that hold all kinds of different information. Therefore, you should allow both PODs and object serialization.

Share this post


Link to post
Share on other sites
This is a semester project as well, I want to index the Yale star catalog.

So I have a list of objects with the constellation, magnitude, etc stored in them. I was hoping there was a nice way to search through this list without explicitly supplying seperate methods for each field.

Share this post


Link to post
Share on other sites
spiffqg: Using templates will allow each "table" to have different data types, but all the data within that table will have to be of the same type.

Well, that''s the idea really. It''s more of an embedded-style database that we''re going for and you typically know at compile time what all the tables and things you''ll need are.

That would only really be usefull (IMHO) if you''re storing large data structures like classes that hold all kinds of different information. Therefore, you should allow both PODs and object serialization.

Structs are PODs too... by objects I wasn''t referring to data structures as much as implementing copy constructors and other object oriented idioms.

Share this post


Link to post
Share on other sites
quote:
Original post by damienj
Well, that''s the idea really. It''s more of an embedded-style database that we''re going for and you typically know at compile time what all the tables and things you''ll need are.

That''s true if you have a sophisticated enough table system.

quote:
Structs are PODs too... by objects I wasn''t referring to data structures as much as implementing copy constructors and other object oriented idioms.

Structs and classes can be PODs, but not necessarily. But I see what you mean.

It looks like the purpose of this database (a catalogue of stars) would be well suited for that type of implementation (having a struct with star data like name, magnitude, etc).

However, you''ll need some kind of indexing scheme. Otherwise, if you want to search by something other than a primary key, you''ll have to search the entire database (or, if it''s sorted, half the database) to find what you want.

For example, you could have all the structs stored on a file in some arbitrary order on disk and have a binary tree or hash table storing the key and the data offset in the file.

Share this post


Link to post
Share on other sites