void ScientistRepository::add(Scientist scientist) {
    // Replace our chosen delimiter with space to avoid breaking the delimited format of the file
    std::replace(scientist.name.begin(),scientist.name.end(),delimiter,' ');
    scientistList.push_back(scientist);
    save();

    QSqlDatabase db = databaseConnect();
    QSqlQuery query(db);

    QVariant qstrName = QVariant(QString::fromStdString(scientist.name));
    QVariant qstrBirth = QVariant(QString::fromStdString(scientist.dateOfBirth));
    QVariant qstrDeath = QVariant(QString::fromStdString(scientist.dateOfDeath));
    QVariant qstrGender = QVariant(QString::fromStdString(scientist.gender));


    query.prepare("INSERT INTO Scientists ('Name', 'DateOfBirth', 'DateOfDeath', 'Gender')"
                              "VALUES(:Name, :DateOfBirth, :DateOfDeath, :Gender)");

        query.bindValue(":Name",qstrName);
        query.bindValue(":DateOfBirth",qstrBirth);
        query.bindValue(":DateOfDeath",qstrDeath);
        query.bindValue(":Gender",qstrGender);

        query.exec();
}
std::list<Scientist> ScientistRepository::search(std::string searchTerm)
{
    QSqlDatabase db = databaseConnect();
    QSqlQuery query(db);

    query.exec(QString::fromStdString("Select * from Scientists where Name like '%" + searchTerm + "%'"));


    std::list<Scientist> scientists = std::list<Scientist>();

    while(query.next())
    {
        Scientist a = Scientist();
        a.name = query.value("Name").toString().toStdString();
        a.dateOfBirth = query.value("DateOfBirth").toString().toStdString();
        a.dateOfDeath = query.value("DateofDeath").toString().toStdString();
        a.gender = query.value("Gender").toString().toStdString();
        scientists.push_back(a);
    }

    return scientists;





//    // Naive search implementation, finds the case sensitive substring in the name and returns first match
//    for(std::list<Scientist>::iterator iter = scientistList.begin(); iter != scientistList.end(); iter++) {
//        if(iter->name.find(searchTerm) != std::string::npos) {
//            return new Scientist(*iter);
//        }
//    }
//    return NULL;
}
std::list<Scientist> ScientistRepository::list() {

    std::list<Scientist> scientists = std::list<Scientist>();

    QSqlDatabase db = databaseConnect();
    QSqlQuery query(db);

    query.exec("SELECT * FROM Scientists");

    while(query.next()){
        Scientist a = Scientist();
        a.name = query.value("Name").toString().toStdString();
        a.dateOfBirth = query.value("DateOfBirth").toString().toStdString();
        a.dateOfDeath = query.value("DateofDeath").toString().toStdString();
        a.gender = query.value("Gender").toString().toStdString();
        scientists.push_back(a);
    }

    return scientists;
}
Exemple #4
0
/*******************************
*
* Henrique Salvadori Coelho
* [email protected]
* hcoelho.com
*
********************************
*
* databaseGetPrimary()
*
* Gets an available primary key
*
* char [] databaseFile - Name of the database file
*
* Returns a valid primary key (long long)
*/
long long databaseGetPrimary ( char * databaseFile )
{
  long long keyToTest = 1;
  long long keyInDatabase = 0;
  int foundCurrentKey = 0;

  // Connecting
  FILE * databaseConnection = NULL;
  databaseConnection = databaseConnect ( databaseFile, DBMODE_R );

  // Looping until the function finds a valid key and calls the return
  do
  {

    // Looping all the registers looking for the key
    while ( databaseReadLine ( databaseConnection, "k", &keyInDatabase ) == 1 && foundCurrentKey != 1 )
    {
      // The key was found, therefore, it's not valid
      if ( keyInDatabase == keyToTest ) foundCurrentKey = 1;
    }

    // If the current key was found, jumps to the next key and resets the current key controller
    if ( foundCurrentKey == 1 )
    {
      keyToTest++;
      foundCurrentKey = 0;
    }

    // Did not find the key, it is valid
    else
    {
      databaseClose ( databaseConnection );

      return keyToTest;
    }

    databaseRewind ( databaseConnection );
  }
  while ( 1 );
}
std::list<Scientist> ScientistRepository::list(int col, int mod)
{

    QSqlDatabase db = databaseConnect();
    QSqlQuery query(db);

    string sorter;

    switch(col)
    {
        case 1:
           if (mod == 1)
           {
               sorter = "Select * from Scientists order by Name ASC";
           }
           else if (mod == 2)
           {
               sorter = "Select * from Scientists order by Name DESC";
           }
           break;
    case 2:
        if (mod == 1)
        {
            sorter = "Select * from Scientists order by DateOfBirth ASC";
        }
        else if (mod == 2)
        {
            sorter = "Select * from Scientists order by DateOfBirth DESC";
        }
        break;
    case 3:
        if (mod == 1)
        {
            sorter = "Select * from Scientists order by DateOfDeath ASC";
        }
        else if (mod == 2)
        {
            sorter = "Select * from Scientists order by DateOfDeath DESC";
        }
        break;
    case 4:
        if (mod == 1)
        {
            sorter = "Select * from Scientists order by Gender ASC";
        }
        else if (mod == 2)
        {
            sorter = "Select * from Scientists order by Gender DESC";
        }
        break;
    }

    query.exec(QString::fromStdString(sorter));

    std::list<Scientist> scientists = std::list<Scientist>();

    while(query.next())
    {
        Scientist a = Scientist();
        a.name = query.value("Name").toString().toStdString();
        a.dateOfBirth = query.value("DateOfBirth").toString().toStdString();
        a.dateOfDeath = query.value("DateofDeath").toString().toStdString();
        a.gender = query.value("Gender").toString().toStdString();
        scientists.push_back(a);
    }

    return scientists;
}
Exemple #6
0
int main ( int argc, char * argv [] )
{
  int       id;
  char      field1 [30];
  char      field2 [30];
  char      field3 [30];
  long long field4;
  double    field5;
  int       control;
  char      databaseLineStructure [1024];
  char      database [] = "sampleDatabase.db";
  int       primaryKey;
  char      arrayField1 [30] [200];
  char      arrayField2 [30] [200];
  char      arrayField3 [30] [200];

  // Connecting
  FILE * sampleDatabase = NULL;
  sampleDatabase = databaseConnect ( database, DBMODE_R );

  // Setting the path for the program
  //databaseGetFullPath ( SCRIPT_PATH, argv[0] );

  printf ( "%s", SCRIPT_PATH );

  printf ( "========================================\n\n" );

  // Showing the number of lines
  printf ( "The number of lines is: \n" );
  printf ( "%d\n\n", databaseNumberEntries ( database ) );


  // Showing the first line
  printf ( "The first line is: \n" );
  databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5 );
  printf ( "%d, %s, %s, %s, %lld, %lf\n\n", id, field1, field2, field3, field4, field5 );


  // Skipping lines
  printf ( "Skipping 23 lines\n\n" );
  databaseSkipLines ( sampleDatabase, 23 );


  // Showing the next line
  printf ( "The 25th line is: \n" );
  databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5 );
  printf ( "%d, %s, %s, %s, %lld, %lf\n\n", id, field1, field2, field3, field4, field5 );


  // Dumping contents, rewinding, dumping again and rewinding again
  printf ( "Dumping the rest of the content on the screen:\n" );
  while ( databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5 ) == 1 )
  {
    printf("%d, %s, %s, %s, %lld, %lf\n", id, field1, field2, field3, field4, field5);
  }

  printf ( "\nRewinding the database\n\n" );
  databaseRewind ( sampleDatabase );

  printf ( "Dumping all the content on the screen:\n" );
  while ( databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5 ) == 1 )
  {
    printf( "%d, %s, %s, %s, %lld, %lf\n", id, field1, field2, field3, field4, field5 );
  }

  printf ( "\nRewinding the database again\n\n" );
  databaseRewind ( sampleDatabase );



  printf ( "The fields of the registry with id = 7 is:\n" );
  control = 0;
  while ( databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5) == 1 && control == 0)
    if ( id == 7 )
    {
      printf ( "%d, %s, %s, %s, %lld, %lf\n", id, field1, field2, field3, field4, field5 );
      control = 1;
    }
  if ( control == 0 ) printf ( "No results found!\n" );


  printf ( "\n\nThe fields of the registry with id = 99 is:\n" );
  databaseRewind ( sampleDatabase );
  control = 0;
  while ( databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5 ) == 1 && control == 0 )
    if ( id == 99 )
    {
      printf ( "%d, %s, %s, %s, %lld, %lf\n", id, field1, field2, field3, field4, field5 );
      control = 1;
    }
  if ( control == 0 ) printf ( "No results found!\n" );


  printf ( "\n\nThe fields of the registry with field4 = 8 are:\n" );
  databaseRewind ( sampleDatabase );
  control = 0;
  while ( databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5 ) == 1 )
  {
    if ( field4 == 8 )
    {
      printf ( "%d, %s, %s, %s, %lld, %lf\n", id, field1, field2, field3, field4, field5 );
      control = 1;
    }
  }
  if ( control == 0 ) printf ( "No results found!\n" );


  // Closing the 'read' database
  // This is necessary because we are going to use the file to write instead of reading
  printf ( "\nClosing the database\n\n" );
  databaseClose ( sampleDatabase );

  // Looking for a primary key that is not occupied in field 1
  primaryKey = databaseGetPrimary ( database );

  // Writing a new line in the database
  printf ( "\nWriting a line in the database\n\n" );
  databaseWriteLine ( database, "dsssdf", primaryKey, "New field 1", "New field 2", "New field 3", 8ll, 3.5 );


  // Opening the database to read again
  printf ( "\nOpening the database to read\n\n" );
  sampleDatabase = databaseConnect ( "sampleDatabase.db", DBMODE_R );


  printf ( "Recording the content in arrays\n" );
  for ( control = 0; databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5 ) == 1; control++ )
  {
    strcpy ( arrayField1 [ control ], field1 );
    strcpy ( arrayField2 [ control ], field2 );
    strcpy ( arrayField3 [ control ], field3 );
  }

  printf ( "The 2nd field of the 16th position is: %s\n\n", arrayField2 [15] );

  databaseClose ( sampleDatabase );


  // Updating a field
  printf ( "Updating a line by ID\n\n" );
  databaseUpdateLine ( database, "sssdf", 26, "Field1_Updated!", "Field2_Updated!", "Field3_Updated!", 90ll, 87.678 );


  // Deleting a field
  printf("Deleting a line by ID\n\n");
  databaseDeleteLine ( database, 27 );
}