<commit_before>//===----------------------------------------------------------------------===//
//
//                         Peloton
//
// sqlite.cpp
//
// Identification: src/wire/sqlite.cpp
//
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include <sys/un.h>

#include <string>

#include "common/logger.h"
#include "wire/sqlite.h"

#include <sqlite3.h>

namespace peloton {
namespace wire {

// used to synch sqlite accesses
std::mutex sqlite_mutex;

Sqlite::Sqlite() {
  // filename is null for in memory db
  auto rc = sqlite3_open_v2(
      "sqlite.db", &sqlite_db_,
      SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
  if (rc) {
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(sqlite_db_));
    LOG_ERROR("Can't open database %s", sqlite3_errmsg(sqlite_db_));
    exit(0);
  } else {
    fprintf(stderr, "\n");
  }
}

Sqlite::~Sqlite() { sqlite3_close(sqlite_db_); }

/*
 * PortalExec - Execute query string
 */
int Sqlite::PortalExec(const char *query, std::vector<ResType> &res,
                       std::vector<FieldInfoType> &info, int &rows_change,
                       std::string &err_msg) {
  LOG_INFO("receive %s", query);
  sqlite3_stmt *sql_stmt;
  sqlite3_prepare_v2(sqlite_db_, query, -1, &sql_stmt, NULL);
  GetRowDesc(sql_stmt, info);
  return ExecPrepStmt(sql_stmt, true, res, rows_change, err_msg);
}

/*
 * InitBindPrepStmt - Prepare and bind a query from a query string
 */
int Sqlite::PrepareStmt(const char *query, sqlite3_stmt **stmt,
                        std::string &err_msg) {
  int rc = sqlite3_prepare_v2(sqlite_db_, query, -1, stmt, NULL);
  if (rc != SQLITE_OK) {
    err_msg = std::string(sqlite3_errmsg(sqlite_db_));
    return 1;
  }

  return 0;
}

int Sqlite::BindStmt(std::vector<std::pair<int, std::string>> &parameters,
                     sqlite3_stmt **stmt, std::string &err_msg) {
  int paramno = 1;
  for (auto &param : parameters) {
    auto wire_type = param.first;
    auto &wire_val = param.second;
    int rc;
    switch (wire_type) {
      case WIRE_INTEGER: {
        int int_val = std::stoi(wire_val);
        rc = sqlite3_bind_int(*stmt, paramno, int_val);
      } break;
      case WIRE_FLOAT: {
        double double_val = std::stod(wire_val);
        rc = sqlite3_bind_double(*stmt, paramno, double_val);
      } break;
      case WIRE_TEXT: {
        const char *str_val = wire_val.c_str();
        size_t str_len = wire_val.size();
        rc = sqlite3_bind_text(*stmt, paramno, str_val, (int)str_len,
                               SQLITE_TRANSIENT);
      } break;

      case WIRE_NULL: {
        rc = sqlite3_bind_null(*stmt, paramno);
        break;
      }
      default: { return 1; }
    }
    if (rc != SQLITE_OK) {
      LOG_INFO("Error in binding: %s", sqlite3_errmsg(sqlite_db_));
      err_msg = std::string(sqlite3_errmsg(sqlite_db_));
      return 1;
    }
    paramno++;
  }

  return 0;
}

/*
 * GetRowDesc - Get RowDescription of a query
 */
void Sqlite::GetRowDesc(void *stmt, std::vector<FieldInfoType> &info) {
  auto sql_stmt = (sqlite3_stmt *)stmt;
  auto col_num = sqlite3_column_count(sql_stmt);
  for (int i = 0; i < col_num; i++) {
    int t = sqlite3_column_type(sql_stmt, i);
    const char *name = sqlite3_column_name(sql_stmt, i);
    LOG_INFO("name: %s", name);
    switch (t) {
      case SQLITE_INTEGER: {
        LOG_INFO("col int");
        info.push_back(std::make_tuple(name, 23, 4));
        break;
      }
      case SQLITE_FLOAT: {
        LOG_INFO("col float");
        info.push_back(std::make_tuple(name, 701, 8));
        break;
      }
      case SQLITE_TEXT: {
        LOG_INFO("col text");
        info.push_back(std::make_tuple(name, 25, 255));
        break;
      }
      default: {
        // Workaround for empty results, should still return something
        LOG_ERROR("Unrecognized column type: %d", t);
        info.push_back(std::make_tuple(name, 25, 255));
        break;
      }
    }
  }
}

/*
 * ExecPrepStmt - Execute a statement from a prepared and bound statement
 */
int Sqlite::ExecPrepStmt(void *stmt, bool unnamed, std::vector<ResType> &res,
                         int &rows_change, std::string &err_msg) {
  LOG_INFO("Executing statement......................");
  auto sql_stmt = (sqlite3_stmt *)stmt;
  auto ret = sqlite3_step(sql_stmt);
  LOG_INFO("ret: %d", ret);
  auto col_num = sqlite3_column_count(sql_stmt);
  LOG_INFO("column count: %d", col_num);

  while (ret == SQLITE_ROW) {
    for (int i = 0; i < col_num; i++) {
      int t = sqlite3_column_type(sql_stmt, i);
      const char *name = sqlite3_column_name(sql_stmt, i);
      std::string value;
      switch (t) {
        case SQLITE_INTEGER: {
          int v = sqlite3_column_int(sql_stmt, i);
          value = std::to_string(v);
          break;
        }
        case SQLITE_FLOAT: {
          double v = (double)sqlite3_column_double(sql_stmt, i);
          value = std::to_string(v);
          break;
        }
        case SQLITE_TEXT: {
          const char *v = (char *)sqlite3_column_text(sql_stmt, i);
          value = std::string(v);
          break;
        }
        default:
          break;
      }
      // TODO: refactor this
      res.push_back(ResType());
      CopyFromTo(name, res.back().first);
      CopyFromTo(value.c_str(), res.back().second);
    }
    ret = sqlite3_step(sql_stmt);
  }

  if (unnamed)
    sqlite3_finalize(sql_stmt);
  else
    sqlite3_reset(sql_stmt);
  sqlite3_db_release_memory(sqlite_db_);
  // sql_stmt = nullptr;
  if (ret != SQLITE_DONE) {
    LOG_INFO("ret num %d, err is %s", ret, sqlite3_errmsg(sqlite_db_));
    err_msg = std::string(sqlite3_errmsg(sqlite_db_));
    return 1;
  }
  rows_change = sqlite3_changes(sqlite_db_);
  return 0;
}<commit_msg>Disabled a sqlite function<commit_after>//===----------------------------------------------------------------------===//
//
//                         Peloton
//
// sqlite.cpp
//
// Identification: src/wire/sqlite.cpp
//
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include <sys/un.h>

#include <string>

#include "common/logger.h"
#include "wire/sqlite.h"

#include <sqlite3.h>

namespace peloton {
namespace wire {

// used to synch sqlite accesses
std::mutex sqlite_mutex;

Sqlite::Sqlite() {
  // filename is null for in memory db
  auto rc = sqlite3_open_v2(
      "sqlite.db", &sqlite_db_,
      SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
  if (rc) {
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(sqlite_db_));
    LOG_ERROR("Can't open database %s", sqlite3_errmsg(sqlite_db_));
    exit(0);
  } else {
    fprintf(stderr, "\n");
  }
}

Sqlite::~Sqlite() { sqlite3_close(sqlite_db_); }

/*
 * PortalExec - Execute query string
 */
int Sqlite::PortalExec(const char *query, std::vector<ResType> &res,
                       std::vector<FieldInfoType> &info, int &rows_change,
                       std::string &err_msg) {
  LOG_INFO("receive %s", query);
  sqlite3_stmt *sql_stmt;
  sqlite3_prepare_v2(sqlite_db_, query, -1, &sql_stmt, NULL);
  GetRowDesc(sql_stmt, info);
  return ExecPrepStmt(sql_stmt, true, res, rows_change, err_msg);
}

/*
 * InitBindPrepStmt - Prepare and bind a query from a query string
 */
int Sqlite::PrepareStmt(const char *query, sqlite3_stmt **stmt,
                        std::string &err_msg) {
  int rc = sqlite3_prepare_v2(sqlite_db_, query, -1, stmt, NULL);
  if (rc != SQLITE_OK) {
    err_msg = std::string(sqlite3_errmsg(sqlite_db_));
    return 1;
  }

  return 0;
}

int Sqlite::BindStmt(std::vector<std::pair<int, std::string>> &parameters,
                     sqlite3_stmt **stmt, std::string &err_msg) {
  int paramno = 1;
  for (auto &param : parameters) {
    auto wire_type = param.first;
    auto &wire_val = param.second;
    int rc;
    switch (wire_type) {
      case WIRE_INTEGER: {
        int int_val = std::stoi(wire_val);
        rc = sqlite3_bind_int(*stmt, paramno, int_val);
      } break;
      case WIRE_FLOAT: {
        double double_val = std::stod(wire_val);
        rc = sqlite3_bind_double(*stmt, paramno, double_val);
      } break;
      case WIRE_TEXT: {
        const char *str_val = wire_val.c_str();
        size_t str_len = wire_val.size();
        rc = sqlite3_bind_text(*stmt, paramno, str_val, (int)str_len,
                               SQLITE_TRANSIENT);
      } break;

      case WIRE_NULL: {
        rc = sqlite3_bind_null(*stmt, paramno);
        break;
      }
      default: { return 1; }
    }
    if (rc != SQLITE_OK) {
      LOG_INFO("Error in binding: %s", sqlite3_errmsg(sqlite_db_));
      err_msg = std::string(sqlite3_errmsg(sqlite_db_));
      return 1;
    }
    paramno++;
  }

  return 0;
}

/*
 * GetRowDesc - Get RowDescription of a query
 */
void Sqlite::GetRowDesc(void *stmt, std::vector<FieldInfoType> &info) {
  auto sql_stmt = (sqlite3_stmt *)stmt;
  auto col_num = sqlite3_column_count(sql_stmt);
  for (int i = 0; i < col_num; i++) {
    int t = sqlite3_column_type(sql_stmt, i);
    const char *name = sqlite3_column_name(sql_stmt, i);
    LOG_INFO("name: %s", name);
    switch (t) {
      case SQLITE_INTEGER: {
        LOG_INFO("col int");
        info.push_back(std::make_tuple(name, 23, 4));
        break;
      }
      case SQLITE_FLOAT: {
        LOG_INFO("col float");
        info.push_back(std::make_tuple(name, 701, 8));
        break;
      }
      case SQLITE_TEXT: {
        LOG_INFO("col text");
        info.push_back(std::make_tuple(name, 25, 255));
        break;
      }
      default: {
        // Workaround for empty results, should still return something
        LOG_ERROR("Unrecognized column type: %d", t);
        info.push_back(std::make_tuple(name, 25, 255));
        break;
      }
    }
  }
}

/*
 * ExecPrepStmt - Execute a statement from a prepared and bound statement
 */
int Sqlite::ExecPrepStmt(void *stmt, bool unnamed, std::vector<ResType> &res,
                         int &rows_change, std::string &err_msg) {
  LOG_INFO("Executing statement......................");
  auto sql_stmt = (sqlite3_stmt *)stmt;
  auto ret = sqlite3_step(sql_stmt);
  LOG_INFO("ret: %d", ret);
  auto col_num = sqlite3_column_count(sql_stmt);
  LOG_INFO("column count: %d", col_num);

  while (ret == SQLITE_ROW) {
    for (int i = 0; i < col_num; i++) {
      int t = sqlite3_column_type(sql_stmt, i);
      const char *name = sqlite3_column_name(sql_stmt, i);
      std::string value;
      switch (t) {
        case SQLITE_INTEGER: {
          int v = sqlite3_column_int(sql_stmt, i);
          value = std::to_string(v);
          break;
        }
        case SQLITE_FLOAT: {
          double v = (double)sqlite3_column_double(sql_stmt, i);
          value = std::to_string(v);
          break;
        }
        case SQLITE_TEXT: {
          const char *v = (char *)sqlite3_column_text(sql_stmt, i);
          value = std::string(v);
          break;
        }
        default:
          break;
      }
      // TODO: refactor this
      res.push_back(ResType());
      CopyFromTo(name, res.back().first);
      CopyFromTo(value.c_str(), res.back().second);
    }
    ret = sqlite3_step(sql_stmt);
  }

  if (unnamed)
    sqlite3_finalize(sql_stmt);
  else
    sqlite3_reset(sql_stmt);

  // TODO: Fix this
  //sqlite3_db_release_memory(sqlite_db_);
  // sql_stmt = nullptr;
  if (ret != SQLITE_DONE) {
    LOG_INFO("ret num %d, err is %s", ret, sqlite3_errmsg(sqlite_db_));
    err_msg = std::string(sqlite3_errmsg(sqlite_db_));
    return 1;
  }
  rows_change = sqlite3_changes(sqlite_db_);
  return 0;
}
{'fraction_non_alphanumeric': 0.15893645939517498, 'fraction_numerical': 0.0127420998980632, 'mean_word_length': 2.4194016845774033, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 21, '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': '310008', 'n_tokens_mistral': 4196, 'n_tokens_neox': 4036, 'n_words': 876}