DataImporter.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "DataInterface/DataImporter.h"
  2. #include <clipp.h>
  3. #include <INIReader.h>
  4. #include <Logging.h>
  5. #include <MySQLClient.h>
  6. #include <SQLiteClient.h>
  7. #include <fstream>
  8. #include <iostream>
  9. #include <sstream>
  10. int main(int argc, char** argv)
  11. {
  12. try
  13. {
  14. Logging::OpenLog();
  15. Logging::SetLogMask(Logging::Severity::Debug);
  16. std::string databaseFile = "";
  17. clipp::group cli {
  18. clipp::value("SQLite Database file", databaseFile)
  19. };
  20. if (!clipp::parse(argc, argv, cli))
  21. {
  22. std::stringstream ss;
  23. ss << clipp::make_man_page(cli, argv[0]);
  24. Logging::Log(Logging::Severity::Info, ss.str());
  25. Logging::CloseLog();
  26. return 1;
  27. }
  28. {
  29. std::ifstream f(databaseFile);
  30. if (!f.good())
  31. throw std::runtime_error("Can't read SQLite Database file.");
  32. }
  33. INIReader iniReader("DataImporter.ini");
  34. if (iniReader.ParseError() != 0)
  35. throw std::runtime_error("Can't read DataImporter.ini.");
  36. MySQL::MySQLClient mySQLClient;
  37. {
  38. std::string hostname = iniReader.Get("MySQL", "Hostname", "");
  39. if (hostname.empty())
  40. throw std::runtime_error("MySQL Hostname directive missing in ini file.");
  41. std::string username = iniReader.Get("MySQL", "Username", "");
  42. if (username.empty())
  43. throw std::runtime_error("MySQL Username directive missing in ini file.");
  44. std::string password = iniReader.Get("MySQL", "Password", "");
  45. if (password.empty())
  46. throw std::runtime_error("MySQL Password directive missing in ini file.");
  47. std::string database = iniReader.Get("MySQL", "Database", "");
  48. if (database.empty())
  49. throw std::runtime_error("MySQL Database directive missing in ini file.");
  50. mySQLClient.Connect(hostname, username, password, database);
  51. }
  52. SQLite::SQLiteClient sqliteClient("./datalog.db");
  53. DataStorageInterface::DataInterface::DataImporter importer(&mySQLClient, &sqliteClient);
  54. importer.ImportData();
  55. Logging::CloseLog();
  56. }
  57. catch (const std::exception& e)
  58. {
  59. std::cerr << "Exception caught" << std::endl;
  60. std::stringstream ss;
  61. ss << "Type : " << typeid(e).name() << std::endl;
  62. ss << "ERROR: " << e.what() << std::endl;
  63. Logging::Log(Logging::Severity::Error, ss.str());
  64. Logging::CloseLog();
  65. return -1;
  66. }
  67. return 0;
  68. }