Changeset 32

Show
Ignore:
Timestamp:
11/05/07 11:03:50 (10 months ago)
Author:
nlawren2
Message:

Playing withe libpq

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • vowfs/db.cxx

    r29 r32  
    22#include "db.hxx" 
    33 
    4 namespace VoWFS{ 
    5         Database::Database(string host, int port, string db_name, string user, string password){ 
    6                 connection = PQsetdbLogin(host.c_str(), 
    7                                           /*to_string*/(port), 
    8                                           NULL,NULL, 
    9                                           db_name.c_str(), 
    10                                           user.c_str(), 
    11                                           password.c_str()); 
    12         } 
     4using namespace VoWFS; 
    135 
    14         Database::~Database(){ 
    15                 PQfinish(connection); 
    16         } 
    17 }; 
     6 
     7Database::Database(string host, int port, string db_name, string user, string password){ 
     8        connection = PQsetdbLogin(host.c_str(), 
     9                                  /*to_string*/(port), 
     10                                  NULL,NULL, 
     11                                  db_name.c_str(), 
     12                                  user.c_str(), 
     13                                  password.c_str()); 
     14
     15 
     16Database::~Database(){ 
     17        PQfinish(connection); 
     18
     19 
     20int Database::getFileId(string file_path){ 
     21        int split = file_path.find_last_of('/'); 
     22 
     23        string directory_name = file_path.substr(0,split); 
     24        string file_name = file_path.substr(split,file_path.size()); 
     25         
     26// TODO: SQL Command: Get file_id in column 1 
     27        PQresult *pq_data = PQexec(connection,""); 
     28 
     29        // All of the results should have the same file id, take the top one 
     30 
     31        int column_number = PQftablecol(pq_data,1); 
     32 
     33        return file_id=*(int*)PQgetvalue(pq_data,0,column_number); 
     34
     35 
     36File Database::getFile(int file_id){ 
     37// TODO: SQL Command: Get file stats of the most recent version from file_id 
     38        PQresult *pq_data = PQexec(connection,""); 
     39        return File(pq_data); 
     40
     41 
     42File Database::getFile(int file_id, int revision){ 
     43// TODO: SQL Command: Get file stats from file_id and revision 
     44        PQresult *pq_data = PQexec(connection,""); 
     45        return File(pq_data); 
     46
     47 
     48 
     49 
     50int Database::getDirId(string dir_name){ 
     51         
     52// TODO: SQL Command: Get dir_id in column 1 
     53        PQresult *pq_data = PQexec(connection,""); 
     54 
     55        // All of the results should have the same file id, take the top one 
     56 
     57        int column_number = PQftablecol(pq_data,1); 
     58 
     59        return file_id=*(int*)PQgetvalue(pq_data,0,column_number); 
     60
     61 
     62 
     63Dir Database::getDir(int dir_id){ 
     64// TODO: SQL Command: Get dir stats of the most recent version from dir_id 
     65        PQresult *pq_data = PQexec(connection,""); 
     66        return Dir(pq_data); 
     67
     68 
     69Dir Database::getDir(int dir_id, int revision){ 
     70// TODO: SQL Command: Get dir stats from dir_id and revision 
     71        PQresult *pq_data = PQexec(connection,""); 
     72        return Dir(pq_data); 
     73
     74 
     75 
  • vowfs/db.hxx

    r29 r32  
    55#include "vowfs.hxx" 
    66 
    7 /* TODO: Implement all get-er functions 
    8  *       It isn't too difficult, but we  
    9  *       need all of the SQL queries for 
    10  *       them. 
    11  */ 
     7// TODO: Get SQL functions to Implement all get-er functions 
    128 
    139namespace VoWFS{ 
     
    2622                 
    2723                File getFile(int file_id, int revision); 
     24 
     25                string getFileData(int file_id); 
     26                 
     27                string getFileData(int file_id, int revision); 
    2828 
    2929 
  • vowfs/dir.cxx

    r29 r32  
    55                parent_dir_id   = 0; 
    66                directory_path  = ""; 
    7                 start_time    = 0; 
    8                 end_time      = 0; 
     7                write_time    = 0; 
     8                modify_time   = 0; 
    99                delete_flag     = false; 
    1010        } 
     
    1717                        if( PQnfields(pq_data) < NUM_FIELDS ){ 
    1818                                NUM_FIELDS = 0; 
    19                                 // TODO: Throw error 
     19                                throw BAD_FIELDS; 
    2020                        } 
    2121 
     
    2424                        pq_format.parent_dir_id  = PQfnumber(pq_data,"parent_dir_id"); 
    2525                        pq_format.directory_path = PQfnumber(pq_data,"directory_path"); 
    26                         pq_format.end_time      = PQfnumber(pq_data,"end_time"); 
     26                        pq_format.modify_time   = PQfnumber(pq_data,"modify_time"); 
    2727                        pq_format.delete_flag    = PQfnumber(pq_data,"delete_flag"); 
    2828                } 
    2929 
    30                 if( PQntuples(pq_data) <= row || PQnfields(pq_data) < NUM_FIELDS ){ 
    31                         // TODO: Throw error 
    32                 } 
     30                if( PQntuples(pq_data) <= row ) throw BAD_ENTRY; 
     31                if( PQnfields(pq_data) < NUM_FIELDS ) throw BAD_FIELDS; 
    3332                 
    3433                revision_id     = *(int*)PQgetvalue(pq_data, row, pq_format.revision_id ); 
     
    3736                directory_path  = string(PQgetvalue(pq_data, row, pq_format.directory_path )); 
    3837 
    39                // TODO: How does PostgreSQL store times and booleans?  Is this correct? 
    40                 start_time     = *(long*)PQgetvalue(pq_data, row, pq_format.start_time ); 
    41                 end_time       = *(long*)PQgetvalue(pq_data, row, pq_format.end_time ); 
     38// TODO: How does PostgreSQL store times and booleans?  Is this correct? 
     39                write_time     = *(long*)PQgetvalue(pq_data, row, pq_format.write_time ); 
     40                modify_time    = *(long*)PQgetvalue(pq_data, row, pq_format.modify_time ); 
    4241                delete_flag     = *(bool*)PQgetvalue(pq_data, row, pq_format.delete_flag ); 
     42 
     43                PQclear(pq_data); 
    4344        } 
    4445}; 
  • vowfs/dir.hxx

    r29 r32  
    1818                string directory_path; 
    1919                 
    20                 long start_time; 
    21                 long end_time; 
     20                long write_time; 
     21                long modify_time; 
    2222 
    2323                /* As with File's data, when the class is first constructed,  
     
    3434                        int parent_dir_id; 
    3535                        int directory_path; 
    36                         int start_time; 
    37                         int end_time; 
     36                        int write_time; 
     37                        int modify_time; 
    3838                }; 
    3939 
     
    6464 
    6565                int startTime(){ 
    66                         return start_time; 
     66                        return write_time; 
    6767                } 
    6868 
    6969                int endTime(){ 
    70                         return end_time; 
     70                        return modify_time; 
    7171                } 
    7272 
     
    7676 
    7777                list<File> listFileContents(){ 
    78                        // TODO: What is the Query for Files 
     78// TODO: What is the Query for Files 
    7979                        return file_list=db.getFiles(""); 
    8080                } 
     
    8282                 
    8383                list<File> listDirContents(){ 
    84                        // TODO: What is the Query for Directories 
     84// TODO: What is the Query for Directories 
    8585                        return dir_list=db.getDirs(""); 
    8686                } 
  • vowfs/file.cxx

    r29 r32  
    22#include "file.hxx" 
    33 
    4 namespace VoWFS{ 
    5         File::File(){ 
    6                 revision_id     = 0; 
     4namespace VoWFS{ File::File(){ revision_id      = 0; 
    75                file_id         = 0; 
    86                parent_file_id  = 0; 
     
    119                file_name       = ""; 
    1210                directory_path  = ""; 
    13                 start_time      = 0; 
    14                 end_time        = 0; 
     11                write_time      = 0; 
    1512                delete_flag     = 0; 
    1613        } 
    1714         
    18         string File::readData(){ 
    19                 char *tmp_data// TODO: Get the file data based on the file_id 
    20         } 
    21  
    2215        File::File(PQresult *pq_data, int row){ 
    2316                if( NUM_FIELDS ){ 
     
    2720                        if( PQnfields(pq_data) < NUM_FIELDS ){ 
    2821                                NUM_FIELDS = 0; 
    29                                 // TODO: Throw error 
     22                                throw BAD_FIELDS; 
    3023                        } 
    3124 
     
    3730                        pq_format.file_name      = PQfnumber(pq_data,"file_name"); 
    3831                        pq_format.directory_path = PQfnumber(pq_data,"directory_path"); 
    39                         pq_format.start_time     = PQfnumber(pq_data,"start_time"); 
    40                         pq_format.end_time       = PQfnumber(pq_data,"end_time"); 
     32                        pq_format.write_time     = PQfnumber(pq_data,"write_time"); 
    4133                        pq_format.create_flag    = PQfnumber(pq_data,"create_flag"); 
    4234                        pq_format.delete_flag    = PQfnumber(pq_data,"delete_flag"); 
    4335                } 
    4436 
    45                 if( PQntuples(pq_data) <= row || PQnfields(pq_data) < NUM_FIELDS ){ 
    46                         // TODO: Throw error 
    47                 } 
     37                if( PQntuples(pq_data) <= row  ) throw BAD_ENTRY; 
    4838                 
     39                if( PQnfields(pq_data) < NUM_FIELDS ) throw BAD_FIELDS; 
     40         
     41                // Now we initialize the member variables 
    4942                revision_id     = *(int*)PQgetvalue(pq_data, row, pq_format.revision_id ); 
    5043                file_id         = *(int*)PQgetvalue(pq_data, row, pq_format.file_id ); 
     
    5548                directory_path  = string(PQgetvalue(pq_data, row, pq_format.directory_path )); 
    5649 
    57                 // TODO: How does PostgreSQL store times and booleans?  Is this correct? 
    58                 start_time      = *(long*)PQgetvalue(pq_data, row, pq_format.start_time ); 
    59                 end_time        = *(long*)PQgetvalue(pq_data, row, pq_format.end_time ); 
     50// TODO: How does PostgreSQL store times and booleans?  Is this correct? 
     51                write_time      = *(long*)PQgetvalue(pq_data, row, pq_format.write_time ); 
    6052                delete_flag     = *(bool*)PQgetvalue(pq_data, row, pq_format.delete_flag ); 
     53 
     54                PQclear(pq_data); 
    6155        } 
    6256         
    6357        string File::readData(){ 
    64                 char *tmp_data// TODO: Get the file data based on the file_id 
     58                string this_data=db.getFileData(file_id, revision_id); 
    6559                 
    66                 File new_file=db.getFile(file_id, revision_id +1); 
     60                File new_file=db.getFile(file_id, revision_id+1); 
    6761 
    68                 if( new_file ){ 
    69                         // TODO: patch based on new_file.fileData() and tmp_data 
    70                 } 
    71                 else{ 
    72                         data=tmp_data; 
    73                 } 
    74                 return data; 
     62                if( new_data != ""  ) 
     63                        return data=diff.patch(new_file.readData(), this_data); 
     64                else 
     65                        return data=this_data; 
    7566        } 
    7667}; 
  • vowfs/file.hxx

    r29 r32  
    2525                string directory_path; 
    2626                 
    27                 long start_time; 
    28                 long end_time; 
     27                long write_time; 
    2928                bool delete_flag; 
    3029 
     
    5352                        int file_name; 
    5453                        int directory_path; 
    55                         int start_time; 
    56                         int end_time; 
     54                        int write_time; 
    5755                        int delete_flag; 
    5856                }; 
  • vowfs/vowfs.hxx

    r29 r32  
    1616}; 
    1717 
     18#include "error.hxx" 
    1819#include "xdelta.hxx" 
    1920#include "db.hxx"