<commit_before><?php
namespace app;

use \PDO;

require_once  __DIR__ . '/../models/token.php';
require_once  __DIR__ . '/../models/user.php';

$db = new PDO('mysql:host=' . $_ENV['DB_HOST'] . ';dbname=' . $_ENV['DB_NAME'] . ';charset=utf8', $_ENV['DB_USER'], $_ENV['DB_PASSWORD']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
global $db;

/**
 *
 */
class Database
{
  static function save($table, $values, $where){
    $sql = "UPDATE $table SET ";
    foreach ($values as $key => $value) {
      $sql .= "$key = :$key, ";
    }
    $sql = rtrim($sql, ", ");
    $sql .= "  WHERE ";
    foreach ($where as $key => $value) {
      $sql .= "$key = :$key AND ";
    }
    $sql = rtrim($sql, "AND ");

    $stmt = $db->prepare($sql);

    return $stmt->execute(array_merge($values, $where));
  }

  static function delete($table, $where){
    $sql = "DELETE FROM $table WHERE ";
    foreach ($where as $key => $value) {
      $sql .= "$key = :$key AND ";
    }
    $sql = rtrim($sql, "AND ");

    $stmt = $db->prepare($sql);

    return $stmt->execute($where);
  }

  static function get($table, $values, $where, $class){
    $sql = "SELECT ";
    foreach ($values as $key) {
      $sql .= "$key, ";
    }
    $sql = rtrim($sql, ", ");
    $sql .= " FROM $table  WHERE ";
    foreach ($where as $key => $value) {
      $sql .= "$key = :$key AND ";
    }
    $sql = rtrim($sql, "AND ");

    $stmt = $db->prepare($sql);

    $stmt->setFetchMode(PDO::FETCH_CLASS, $class);
    $stmt->execute(array_merge($values, $where));

    return $stmt->fetch();
  }

  static function create($table, $values){
    $sql = "INSERT INTO $table (";

    foreach ($values as $key => $value) {
      $sql .= "$key, ";
    }

    $sql = rtrim($sql, ", ");

    $sql .= ") VALUES (";
    foreach ($values as $key => $value) {
      $sql .= ":$key, ";
    }
    $sql = rtrim($sql, ", ");
    $sql .= ")";

    $stmt = $db->prepare($sql);

    $stmt->execute($values);
    return $db->lastInsertId();
  }
}<commit_msg>Only create database instance when we really need it<commit_after><?php
//use \PDO;

require_once  __DIR__ . '/../models/token.php';
require_once  __DIR__ . '/../models/user.php';

/**
 *
 */
class Database
{

  static $db = null;

  static function get(){
      if(self::$db){
          return self::$db;
      }

      self::$db = new PDO('mysql:host=' . $_ENV['DB_HOST'] . ';dbname=' . $_ENV['DB_NAME'] . ';charset=utf8', $_ENV['DB_USER'], $_ENV['DB_PASSWORD']);
      self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
      return self::$db;
  }

  static function save($table, $values, $where){
    $sql = "UPDATE $table SET ";
    foreach ($values as $key => $value) {
      $sql .= "$key = :$key, ";
    }
    $sql = rtrim($sql, ", ");
    $sql .= "  WHERE ";
    foreach ($where as $key => $value) {
      $sql .= "$key = :$key AND ";
    }
    $sql = rtrim($sql, "AND ");

    $stmt = self::get()->prepare($sql);

    return $stmt->execute(array_merge($values, $where));
  }

  static function delete($table, $where){
    $sql = "DELETE FROM $table WHERE ";
    foreach ($where as $key => $value) {
      $sql .= "$key = :$key AND ";
    }
    $sql = rtrim($sql, "AND ");

    $stmt = self::get()->prepare($sql);

    return $stmt->execute($where);
  }

  static function fetch($table, $values, $where, $class){
    $sql = "SELECT ";
    foreach ($values as $key) {
      $sql .= "$key, ";
    }
    $sql = rtrim($sql, ", ");
    $sql .= " FROM $table  WHERE ";
    foreach ($where as $key => $value) {
      $sql .= "$key = :$key AND ";
    }
    $sql = rtrim($sql, "AND ");

    $stmt = self::get()->prepare($sql);

    $stmt->setFetchMode(PDO::FETCH_CLASS, $class);
    $stmt->execute(array_merge($values, $where));

    return $stmt->fetch();
  }

  static function create($table, $values){
    $sql = "INSERT INTO $table (";

    foreach ($values as $key => $value) {
      $sql .= "$key, ";
    }

    $sql = rtrim($sql, ", ");

    $sql .= ") VALUES (";
    foreach ($values as $key => $value) {
      $sql .= ":$key, ";
    }
    $sql = rtrim($sql, ", ");
    $sql .= ")";

    $stmt = self::get()->prepare($sql);

    $stmt->execute($values);
    return self::get()->lastInsertId();
  }
}
{'fraction_non_alphanumeric': 0.22732491389207807, 'fraction_numerical': 0.0004592422502870264, 'mean_word_length': 2.726261762189906, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 41, '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': '6918258', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1532, 'n_words': 345}