Software:Database Management Library

From HandWiki
Database Management Library (DBL)
Developer(s)Rodrigo C. O. Rocha
Initial releaseJuly 2, 2010 (2010-07-02)
Stable release
1.0 / July 2, 2010; 14 years ago (2010-07-02)
Written inC++
TypeEmbedded RDBMS
LicenseGNU General Public License
Websitesites.google.com/site/rcorcs/download/library sourceforge.net/projects/dblibrary

Database Management Library (DBL) is a relational database management system (RDBMS) contained in a C++ programming library. The DBL source code is available under the terms of the GNU General Public License.

DBL was fully developed within two weeks, as a holiday programming project.

It aims to be easy and simple to use for C++ programming.

Design

DBL is a library and becomes an integral part of the application program. Unlike client–server model database management systems that are standalone process with which the application program communicates. The application software uses DBL's functionality through function calls.

Sample programs

Creating a simple database

This is a basic program that creates a simple database. However, as this task must be done usually once, it can be done by the DBL command-line interface.

#include "dbl.h"

int main()
{
    path( "D:\\" ); //set the path to the folder where the files will be stored

    database db("mydatabase");  //mydatabase is the name of the database
    db.new_tab("customer");  //create a new table called customer in the database
	
    write(db);  //write the database structure into a file

    char pkey = 1;
    table *tab = db.get_tab("customer"); //get the table customer from the database
    tab->add_col("cod", INTEGER, 1, pkey);  //add a column called cod to the table customer
    tab->add_col("name", CHARACTER, 32);  //add a column called name to the table customer
    tab->add_col("brithdate", INTEGER, 3);
    tab->add_col("sex", CHARACTER, 1);
    tab->add_col("phone", INTEGER, 1);
    tab->set_structure();
    write(*tab);  //write the table structure into files
    create_data_file(*tab); //create the data file of the table customer
    
    return 0;
}
  1. include<iostream.h>

int main() {

database db("mydatabase"

Library structure

Class database

This class stores the database name and its tables. The main functions are:

char *name(); //get the database name
   char *name(char *dbname); //set the database name
   void new_tab(char *tabname); //create a new table
   table *get_tab(char *tabname); //return the pointer to the table

Useful functions that use the class database are:

void write(database &db); //write the database structure into a file
   friend void read(database &db); //read the database structure from a file
   friend void del(database &db); //delete the database and its tables files
   friend void print(database &db); //print the database on the screen

Class table

This class stores the table name and its structure, the columns of the table. The main functions are:

char *name(); //get the table name
   char *name(char *dbname); //set the table name
   void add_col(column &c); //add a new column to the table
   void add_col(char *col_name, char col_type, int col_len=1, char pkey=0);
   column *get_col(int idx); //get the column by its index
   column *get_col(char *name); //get the column by its name
   int num_col(); //get the number of columns in the table

   //finish the structure of the table.
   //This function must be called after adding all columns or after reading the structure of the table from a file
   void set_structure();

   row new_row(); //get a new row with the table structure

Useful functions that use the class table are:

void write(table &t); //write the table structure into a file
   void read(table &t); //read the table structure from a file
   friend void del(table &t); //delete the table files, header and data files
   void print(table &t); //print the table on the screen
   friend std::ostream &operator<<(std::ostream &o, table &t); //print the table structure
   int num_row(table &t); //get the number of rows in the data file of the table

Class row

This class stores the columns of the table and the data to be stored in the data file. The main functions are:

void set(int idx, storage &s); //set the storage of a column by its index
   void set(int idx, void* v); //set the value to be stored in a column by its index
   storage *get(int idx); //get the storage from the a column by its index

Useful functions that use the class row are:

void write(table &t, row &r, int idx); //write the data in the data file of the table
   void read(table &t, row &r, int idx); //read the data from the data file of the table
   void del(char *file, table &t, int idx); //delete the data from the data file of the table

Class storage

This class stores the column and a value for that column. The main functions are:

char *value(); //get the value being stored by the object
   void value(void *val); //set the value to be stored
   void value(char *val); //set the value to be stored, a C-style string and all functions of the class column.

Useful functions that use the class storage are:

int get_int(storage &s); //get the integer being stored
   char get_char(storage &s); //get the char being stored
   bool get_bool(storage &s); //get the bool being stored
   float get_float(storage &s); //get the float being stored
   double get_double(storage &s); //get the double being stored

Class column

This class stores the name and the structure of a column. The main functions are:

char *name(); //get the name of the column
   char *name(char *n); //set the name of the column
   char type(); //get the type of the column
   char type(char t); //set the type of the column
   int length(); //get the length of the array that the column can hold
   int length(int len); //set the length of the array that the column can hold, len>0
   void pkey(char b); //set if the column is the primary key or not (0 is false, 1 is true)
   char pkey(); //get if the column is the primary key or not
   int total_size(); //get the total size, in bytes, that the column can hold

Class index

This class stores the indexes of a table. The main functions are:

int seek(void *val); //look for a value in the indexes
   int seek(char *val); //look for a C-style string in the indexes

Useful functions that use the class index are:

void write(table &t, index &idx); //write the indexes of a table into a file
   void read(index &idx); //read the indexes from a file

DBL command-line interface

By the DBL command-line interface program one can create a database, a table, and add columns to this table, besides other operations such as printing.

External links