Vous êtes sur la page 1sur 5

Database Management Library

ROCHA, Rodrigo C. O.

Introduction to the DBL


This document is concerned with the first version of the Database Management Library, D L. !t is a C"" ob#ect$oriented %ibrary. !n this document ! wi%% describe how this %ibrary wor&s and how to use it. The C L %ibrary aims at being easy and sim'%e, however usefu%. The base c%asses of this %ibrary are( $ database $ tab%e $ row $ storage $ co%umn $ inde) The class database This c%ass stores the database name and its tab%es. 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 void de%-tab*char *tabname+, //delete a table table *get-tab*char *tabname+, //return the pointer to the table .sefu% functions that use the c%ass 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 de%*database &db+, //delete the database and its tables files friend void 'rint*database &db+, //print the database on the screen The class table This c%ass stores the tab%e name and its structure, the co%umns of the tab%e. The main functions are( char *name*+, //get the table name char *name*char *dbname+, //set the table name void add-co%*co%umn &c+, //add a new column to the table void add-co%*char *co%-name, char co%-ty'e, int co%-%en/0, char '&ey/1+, co%umn *get-co%*int id)+, //get the column by its index co%umn *get-co%*char *name+, //get the column by its name int num-co%*+, //get the number of columns in the table void set-structure*+, //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 row new-row*+, //get a new row with the table structure .sefu% functions that use the c%ass tab%e are( void write*tab%e &t+, //write the table structure into a file void read*tab%e &t+, //read the table structure from a file

friend void de%*tab%e &t+, //delete the table files, header and data files void 'rint*tab%e &t+, //print the table on the screen friend std::ostream &operator<<*std::ostream &o, tab%e &t+, //print the table structure int num-row*tab%e &t+, //get the number of rows in the data file of the table The class ro This c%ass stores the co%umns of the tab%e and the data to be stored in the data fi%e. The main functions are( void set*int id), storage &s+, 22set the storage of a co%umn by its inde) void set*int id), void* v+, 22set the va%ue to be stored in a co%umn by its inde) storage *get*int id)+, 22get the storage from the a co%umn by its inde) .sefu% functions that use the c%ass row are( void write*tab%e &t, row &r, int id)+, //write the data in the data file of the table void read*tab%e &t, row &r, int id)+, //read the data from the data file of the table void de%*char *fi%e, tab%e &t, int id)+, 22de%ete the data from the data fi%e of the tab%e The class storage This c%ass stores the co%umn and a va%ue for that co%umn. The main functions are( char *va%ue*+, //get the value being stored by the object void va%ue*void *va%+, //set the value to be stored void va%ue*char *va%+, //set the value to be stored, a C-style string and a%% functions of the c%ass column. .sefu% functions that use the c%ass storage are( int get-int*storage &s+, //get the integer being stored char get-char*storage &s+, //get the char being stored bool get-boo%*storage &s+, //get the bool being stored float get-f%oat*storage &s+, //get the float being stored double get-doub%e*storage &s+, //get the double being stored The class column This c%ass stores the name and the structure of a co%umn. The main functions are( char *name*+, //get the name of the column char *name*char *n+, //set the name of the column char ty'e*+, //get the type of the column char ty'e*char t+, //set the type of the column int %ength*+, //get the length of the array that the column can hold int %ength*int %en+, //set the length of the array that the column can hold, %en31 void '&ey*char b+, //set if the column is the primary key or not ! is false, " is true# char '&ey*+, //get if the column is the primary key or not int tota%-si4e*+, //get the total si$e, in bytes, that the column can hold The class inde! This c%ass stores the inde)es of a tab%e. The main functions are( int see&*void *va%+, //look for a value in the indexes int see&*char *va%+, //look for a C-style string in the indexes

.sefu% functions that use the c%ass inde) are( void write*tab%e &t, inde) &id)+, //write the indexes of a table into a file void read*inde) &id)+, //read the indexes from a file "!ample The fo%%owing e)am'%e aims at demonstrating how to use the %ibrary.

5inc%ude 6iostream3 using std((cout, using std((end%, 5inc%ude 7db%.h7 22inc%ude the database management %ibrary int main*+ 8 'ath* 7D(997 +, 22set the 'ath to the fo%der where the fi%es wi%% be stored database db*7mydatabase7+, 22mydatabase is the name of the database db.new-tab*7customer7+, 22create a new tab%e in the database, ca%%ed customer 'rint*db+, 22'rint the database structure write*db+, 22write the database structure into a fi%e char '&ey / 0, tab%e :tab / db.get-tab*7customer7+, 22get the tab%e, customer, from the database tab$3add-co%*7cod7, !;T<=<R, 0, '&ey+, 22add a co%umn ca%%ed cod to the tab%e customer tab$3add-co%*7name7, CHARACT<R, >?+, 22add a co%umn ca%%ed name to the tab%e customer tab$3add-co%*7brithdate7, !;T<=<R, >+, tab$3add-co%*7se)7, CHARACT<R, 0+, tab$3add-co%*7'hone7, !;T<=<R, 0+, tab$3set-structure*+, cout 66 :tab 66 end%, 22'rint the tab%e structure write*:tab+, 22write the tab%e structure into fi%es create-data-fi%e*:tab+, row r* tab$3new-row*+ +, 22get a row with the structure of the tab%e customer int cod / >0@0A, char nameB>?C / 7Char%es7, int birthdateB>C / 8?D, E, ?101F, char se) / GmG, int 'hone / HH0?>@AE, 22set the va%ues stored by the row r.set*1, Icod+,

r.set*0, name+, r.set*?, birthdate+, r.set*>, Ise)+, r.set*@, I'hone+, int i / num-row*:tab+, 22get the number of rows in the data fi%e 22write the row into the data fi%e of the tab%e customer write*:tab, r, i +, 22where i is the inde) of the record inde) id)*:tab, 1+, 22create the inde)es of the tab%e customer, inde)ed by the first co%umn, cod write*:tab, id)+, 22write the inde)es into a fi%e row r?* tab$3new-row*+ +, 22get a row with the structure of the tab%e customer i / id).see&*Icod+, 22get the inde) of the row read*:tab, r?, i +, 22read a row from the data fi%e 'rint*:tab+, return 1, F

The DBL #ommand$Line Interface


! have a%so created a Command$Line !nterface 'rogram so that one can 'ro#ect your own database without the need of creating a 'rogram to do that. y this 'rogram one can create a database, create a tab%e and add co%umns to this tab%e, besides others o'erations such as 'rinting. The D LCL! 'rogram can receive a command$%ine 'arameter with a 'ath to where you want to store the database that wi%% be managed. The 'ath must be finished with bac&s%ash, e.g. #:%mydb% is a va%id 'ath. The acce'tab%e commands are( $ create database D ;AM<& creates a database called %&'()* and makes it the database that will be managed. $ use D ;AM<& makes %&'()* the database that will be used. $ create table TA ;AM<& creates a table called T(&'()*. $ add column COL;AM<'TJK<( L<;=TH( p)ey * to TA ;AM<& adds a new column, C+,'()*, to the table called T(&'()*. T-.* can be boolean, character, integer, float, double or real. ,*'/T0 is the length of the storage, as it can be an array a vector#, where ,*'/T0 1 !. The third parameter is optional, if one uses the it, it means that the column will be the primary key of the table.

$ print database D ;AM<& prints the database called %&'()* on the screen. $ print structure TA ;AM<& prints the structure of the table called T(&'()* on the screen. $ delete database D ;AM<& deletes the database, %&'()*, as well as its tables. $ delete table TA ;AM<& deletes the table called T(&'()*.

Vous aimerez peut-être aussi