<commit_before>#include "database.h"

#include <utility>
#include <boost/log/trivial.hpp>
#include <patlms/type/bash_log_entry.h>
#include <patlms/type/apache_log_entry.h>

#include "detail/sqlite.h"
#include "src/database/exception/detail/cant_open_database_exception.h"
#include "src/database/exception/detail/cant_close_database_exception.h"
#include "src/database/exception/detail/cant_execute_sql_statement_exception.h"

using namespace std;

namespace database
{

DatabasePtr Database::Create() {
  std::unique_ptr<detail::SQLite> sqlite(new detail::SQLite());
  return Database::Create(std::move(sqlite));
}

DatabasePtr Database::Create(std::unique_ptr<detail::SQLiteInterface> sqlite) {
  DatabasePtr db(new Database(move(sqlite)));
  return db;
}

void Database::Open(const std::string &file_path) {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::Open: Function call";

  int ret = sqlite_interface_->Open(file_path.c_str(), &db_handle_, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr);
  if (ret != SQLITE_OK) {
    BOOST_LOG_TRIVIAL(error) << "database::Database::Open: Open error: " << ret;
    throw exception::detail::CantOpenDatabaseException();
  }

  is_open_ = true;
}

bool Database::IsOpen() const {
  return is_open_;
}

void Database::CreateDateTable() {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::CreateDateTable: Function call";

  if (is_open_ == false) {
    BOOST_LOG_TRIVIAL(error) << "database::Database::CreateDateTable: Database is not open.";
    throw exception::detail::CantExecuteSqlStatementException();
  }

  const char *sql =
      " create table if not exists DATE_TABLE("
      "   ID integer primary key, "
      "   DAY integer, "
      "   MONTH integer, "
      "   YEAR integer, "
      "   unique(DAY, MONTH, YEAR) "
      " );";

  int ret = sqlite_interface_->Exec(db_handle_, sql, nullptr, nullptr, nullptr);
  StatementCheckForError(ret, "Create BASH_LOGS_TABLE error");
}

void Database::CreateBashLogsTable() {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::CreateBashLogsTable: Function call";

  if (is_open_ == false) {
    BOOST_LOG_TRIVIAL(error) << "database::Database::CreateBashLogsTable: Database is not open.";
    throw exception::detail::CantExecuteSqlStatementException();
  }

  const char *sql =
      "create table if not exists BASH_LOGS_TABLE("
      "  ID integer primary key, "
      "  AGENT_NAME text, "
      "  UTC_HOUR integer, "
      "  UTC_MINUTE integer, "
      "  UTC_SECOND integer, "
      "  UTC_DAY integer, "
      "  UTC_MONTH integer, "
      "  UTC_YEAR integer, "
      "  USER_ID integer, "
      "  COMMAND text "
      ");";
  int ret = sqlite_interface_->Exec(db_handle_, sql, nullptr, nullptr, nullptr);
  StatementCheckForError(ret, "Create BASH_LOGS_TABLE error");
}

void Database::CreateApacheLogsTable() {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::CreateApacheLogsTable: Function call";

  if (is_open_ == false) {
    BOOST_LOG_TRIVIAL(error) << "database::Database::CreateBashLogsTable: Database is not open.";
    throw exception::detail::CantExecuteSqlStatementException();
  }

  const char *sql =
      "create table if not exists APACHE_LOGS_TABLE("
      "  ID integer primary key, "
      "  AGENT_NAME text,"
      "  VIRTUALHOST text, "
      "  CLIENT_IP text, "
      "  UTC_HOUR integer, "
      "  UTC_MINUTE integer, "
      "  UTC_SECOND integer, "
      "  UTC_DAY integer, "
      "  UTC_MONTH integer, "
      "  UTC_YEAR integer, "
      "  REQUEST text, "
      "  STATUS_CODE integer, "
      "  BYTES integer, "
      "  USER_AGENT text "
      ");";
  int ret = sqlite_interface_->Exec(db_handle_, sql, nullptr, nullptr, nullptr);
  StatementCheckForError(ret, "Create APACHE_LOGS_TABLE error");
}

void Database::CreateApacheSessionTable() {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::CreateApacheSessionTable: Function call";

  if (is_open_ == false) {
    BOOST_LOG_TRIVIAL(error) << "database::Database::CreateApacheSessionTable: Database is not open.";
    throw exception::detail::CantExecuteSqlStatementException();
  }

  const char *sql =
      "create table if not exists APACHE_SESSION_TABLE("
      "  ID integer primary key, "
      "  AGENT_NAME text,"
      "  VIRTUALHOST text, "
      "  CLIENT_IP text, "
      "  UTC_HOUR integer, "
      "  UTC_MINUTE integer, "
      "  UTC_SECOND integer, "
      "  UTC_DAY integer, "
      "  UTC_MONTH integer, "
      "  UTC_YEAR integer, "
      "  SESSION_LENGTH integer, "
      "  BANDWIDTH_USAGE integer, "
      "  REQUESTS_COUNT integer, "
      "  ERROR_PERCENTAGE integer, "
      "  USER_AGENT text "
      ");";
  int ret = sqlite_interface_->Exec(db_handle_, sql, nullptr, nullptr, nullptr);
  StatementCheckForError(ret, "Create APACHE_SESSION_TABLE error");
}

void Database::CreateApacheSessionExistsTable() {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::CreateApacheSessionTable: Function call";

  if (is_open_ == false) {
    BOOST_LOG_TRIVIAL(error) << "database::Database::CreateApacheSessionTable: Database is not open.";
    throw exception::detail::CantExecuteSqlStatementException();
  }

  const char *sql =
      "create table if not exists APACHE_SESSION_EXISTS_TABLE("
      "  ID integer primary key, "
      "  DATE_ID integer not null unique, "
      "  EXIST integer not null unique, "
      "  foreign key(DATE_ID) references DATE_TABLE(ID) "
      ");";
  int ret = sqlite_interface_->Exec(db_handle_, sql, nullptr, nullptr, nullptr);
  StatementCheckForError(ret, "Create APACHE_SESSION_EXISTS_TABLE error");
}

void Database::AddDate(int day, int month, int year) {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::AddDate: Function call";

  if (is_open_ == false) {
    BOOST_LOG_TRIVIAL(error) << "database::Database::AddDate: Database is not open.";
    throw exception::detail::CantExecuteSqlStatementException();
  }

  string sql =
      " insert or ignore into DATA_TABLE (DAY, MONTH, YEAR) "
      " values ( "
      + to_string(day) + ", " + to_string(month) + ", " + to_string(year)
      + " );";
  int ret = sqlite_interface_->Exec(db_handle_, sql.c_str(), nullptr, nullptr, nullptr);
  StatementCheckForError(ret, "Create APACHE_SESSION_TABLE error");
}

long long Database::GetDateId(int day, int month, int year) {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::GetDateId: Function call";
  int ret;
  long long id = -1;

  if (is_open_ == false) {<commit_msg>Fix table name in insert query<commit_after>#include "database.h"

#include <utility>
#include <boost/log/trivial.hpp>
#include <patlms/type/bash_log_entry.h>
#include <patlms/type/apache_log_entry.h>

#include "detail/sqlite.h"
#include "src/database/exception/detail/cant_open_database_exception.h"
#include "src/database/exception/detail/cant_close_database_exception.h"
#include "src/database/exception/detail/cant_execute_sql_statement_exception.h"

using namespace std;

namespace database
{

DatabasePtr Database::Create() {
  std::unique_ptr<detail::SQLite> sqlite(new detail::SQLite());
  return Database::Create(std::move(sqlite));
}

DatabasePtr Database::Create(std::unique_ptr<detail::SQLiteInterface> sqlite) {
  DatabasePtr db(new Database(move(sqlite)));
  return db;
}

void Database::Open(const std::string &file_path) {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::Open: Function call";

  int ret = sqlite_interface_->Open(file_path.c_str(), &db_handle_, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr);
  if (ret != SQLITE_OK) {
    BOOST_LOG_TRIVIAL(error) << "database::Database::Open: Open error: " << ret;
    throw exception::detail::CantOpenDatabaseException();
  }

  is_open_ = true;
}

bool Database::IsOpen() const {
  return is_open_;
}

void Database::CreateDateTable() {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::CreateDateTable: Function call";

  if (is_open_ == false) {
    BOOST_LOG_TRIVIAL(error) << "database::Database::CreateDateTable: Database is not open.";
    throw exception::detail::CantExecuteSqlStatementException();
  }

  const char *sql =
      " create table if not exists DATE_TABLE("
      "   ID integer primary key, "
      "   DAY integer, "
      "   MONTH integer, "
      "   YEAR integer, "
      "   unique(DAY, MONTH, YEAR) "
      " );";

  int ret = sqlite_interface_->Exec(db_handle_, sql, nullptr, nullptr, nullptr);
  StatementCheckForError(ret, "Create BASH_LOGS_TABLE error");
}

void Database::CreateBashLogsTable() {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::CreateBashLogsTable: Function call";

  if (is_open_ == false) {
    BOOST_LOG_TRIVIAL(error) << "database::Database::CreateBashLogsTable: Database is not open.";
    throw exception::detail::CantExecuteSqlStatementException();
  }

  const char *sql =
      "create table if not exists BASH_LOGS_TABLE("
      "  ID integer primary key, "
      "  AGENT_NAME text, "
      "  UTC_HOUR integer, "
      "  UTC_MINUTE integer, "
      "  UTC_SECOND integer, "
      "  UTC_DAY integer, "
      "  UTC_MONTH integer, "
      "  UTC_YEAR integer, "
      "  USER_ID integer, "
      "  COMMAND text "
      ");";
  int ret = sqlite_interface_->Exec(db_handle_, sql, nullptr, nullptr, nullptr);
  StatementCheckForError(ret, "Create BASH_LOGS_TABLE error");
}

void Database::CreateApacheLogsTable() {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::CreateApacheLogsTable: Function call";

  if (is_open_ == false) {
    BOOST_LOG_TRIVIAL(error) << "database::Database::CreateBashLogsTable: Database is not open.";
    throw exception::detail::CantExecuteSqlStatementException();
  }

  const char *sql =
      "create table if not exists APACHE_LOGS_TABLE("
      "  ID integer primary key, "
      "  AGENT_NAME text,"
      "  VIRTUALHOST text, "
      "  CLIENT_IP text, "
      "  UTC_HOUR integer, "
      "  UTC_MINUTE integer, "
      "  UTC_SECOND integer, "
      "  UTC_DAY integer, "
      "  UTC_MONTH integer, "
      "  UTC_YEAR integer, "
      "  REQUEST text, "
      "  STATUS_CODE integer, "
      "  BYTES integer, "
      "  USER_AGENT text "
      ");";
  int ret = sqlite_interface_->Exec(db_handle_, sql, nullptr, nullptr, nullptr);
  StatementCheckForError(ret, "Create APACHE_LOGS_TABLE error");
}

void Database::CreateApacheSessionTable() {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::CreateApacheSessionTable: Function call";

  if (is_open_ == false) {
    BOOST_LOG_TRIVIAL(error) << "database::Database::CreateApacheSessionTable: Database is not open.";
    throw exception::detail::CantExecuteSqlStatementException();
  }

  const char *sql =
      "create table if not exists APACHE_SESSION_TABLE("
      "  ID integer primary key, "
      "  AGENT_NAME text,"
      "  VIRTUALHOST text, "
      "  CLIENT_IP text, "
      "  UTC_HOUR integer, "
      "  UTC_MINUTE integer, "
      "  UTC_SECOND integer, "
      "  UTC_DAY integer, "
      "  UTC_MONTH integer, "
      "  UTC_YEAR integer, "
      "  SESSION_LENGTH integer, "
      "  BANDWIDTH_USAGE integer, "
      "  REQUESTS_COUNT integer, "
      "  ERROR_PERCENTAGE integer, "
      "  USER_AGENT text "
      ");";
  int ret = sqlite_interface_->Exec(db_handle_, sql, nullptr, nullptr, nullptr);
  StatementCheckForError(ret, "Create APACHE_SESSION_TABLE error");
}

void Database::CreateApacheSessionExistsTable() {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::CreateApacheSessionTable: Function call";

  if (is_open_ == false) {
    BOOST_LOG_TRIVIAL(error) << "database::Database::CreateApacheSessionTable: Database is not open.";
    throw exception::detail::CantExecuteSqlStatementException();
  }

  const char *sql =
      "create table if not exists APACHE_SESSION_EXISTS_TABLE("
      "  ID integer primary key, "
      "  DATE_ID integer not null unique, "
      "  EXIST integer not null unique, "
      "  foreign key(DATE_ID) references DATE_TABLE(ID) "
      ");";
  int ret = sqlite_interface_->Exec(db_handle_, sql, nullptr, nullptr, nullptr);
  StatementCheckForError(ret, "Create APACHE_SESSION_EXISTS_TABLE error");
}

void Database::AddDate(int day, int month, int year) {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::AddDate: Function call";

  if (is_open_ == false) {
    BOOST_LOG_TRIVIAL(error) << "database::Database::AddDate: Database is not open.";
    throw exception::detail::CantExecuteSqlStatementException();
  }

  string sql =
      " insert or ignore into DATE_TABLE (DAY, MONTH, YEAR) "
      " values ( "
      + to_string(day) + ", " + to_string(month) + ", " + to_string(year)
      + " );";
  int ret = sqlite_interface_->Exec(db_handle_, sql.c_str(), nullptr, nullptr, nullptr);
  StatementCheckForError(ret, "Insert or ignore date error");
}

long long Database::GetDateId(int day, int month, int year) {
  BOOST_LOG_TRIVIAL(debug) << "database::Database::GetDateId: Function call";
  int ret;
  long long id = -1;

  if (is_open_ == false) {
{'fraction_non_alphanumeric': 0.12419667053813395, 'fraction_numerical': 0.0001548586914440573, 'mean_word_length': 3.998452012383901, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4130130', 'n_tokens_mistral': 4260, 'n_tokens_neox': 4153, 'n_words': 883}