Database Management Library
Encyclopedia
DBL - Database Management Library is a RDBMS
Relational database management system
A relational database management system is a database management system that is based on the relational model as introduced by E. F. Codd. Most popular databases currently in use are based on the relational database model....

 contained in a C++ programming library
Library (computer science)
In computer science, a library is a collection of resources used to develop software. These may include pre-written code and subroutines, classes, values or type specifications....

. The DBL source code
Source code
In computer science, source code is text written using the format and syntax of the programming language that it is being written in. Such a language is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source...

 is available under the terms of the GNU General Public License
GNU General Public License
The GNU General Public License is the most widely used free software license, originally written by Richard Stallman for the GNU Project....

.

DBL was developed as a holidays programming project, and it was completely developed within two weeks.

It aims at being easy and simple for C++ programmers to use.

Design

DBL is a library and becomes an integral part of the application program. Unlike client–server database management systems that are standalone process
Process (computing)
In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system , a process may be made up of multiple threads of execution that execute instructions concurrently.A computer program is a...

 with which the application program communicates. The application software uses DBL's functionality through function calls
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

.

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.
  1. 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;
}

Inserting data into a table

This is a basic program that inserts a new record in the table.
  1. 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
read(db); //load the database structure

table *tab = db.get_tab("customer"); //get the table customer from the database
row r( tab->new_row ); //get a row with the structure of the table customer

int cod = 31415;
char name[32] = "Charles";
int birthdate[3] = {27, 6, 2010};
char sex = 'm';
int phone = 88123456;

//set the values stored by the row
r.set(0, &cod);
r.set(1, name);
r.set(2, birthdate);
r.set(3, &sex);
r.set(4, &phone);

int i = num_row(*tab); //get the number of rows in the data file

//write the row into the data file of the table customer,
//where i is the index of the new record
write(*tab, r, i );

return 0;
}

The 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

The 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

The 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

The 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

The 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

The 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, create a table and add columns to this table, besides others operations such as printing.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK