Académique Documents
Professionnel Documents
Culture Documents
ROCHA, Rodrigo C. O.
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
$ 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(&'()*.