<commit_before>const path = require('path');
const fs = require('fs');
const streamsql = require('streamsql');
const config = require('./config');
const mysql = require('mysql');

function getDbConfig (prefix) {
  prefix += '_';
  return {
    driver:     config(prefix+'DRIVER', 'mysql'),
    host:       config(prefix+'HOST', 'localhost'),
    user:       config(prefix+'USER'),
    password:   config(prefix+'PASSWORD'),
    database:   config(prefix+'DATABASE')
  }
}

function getDb (prefix) {
  var db = streamsql.connect(getDbConfig(prefix));

  function handleDisconnect() {
    var newDb = streamsql.connect(getDbConfig(prefix));
    
    db.connection = newDb.connection;
    db.query = newDb.query;
    db.queryStream = newDb.queryStream;
    
    setErrorHandler();
  }

  function setErrorHandler() {
    db.connection.on('error', function(err) {
      if (err.code === 'PROTOCOL_CONNECTION_LOST') {
        console.log('Reconnecting to db');
        handleDisconnect();
      }
      else {
        console.error(err);
      }
    });
  }

  setErrorHandler();

  return db;
};

module.exports.getDb = getDb;
module.exports.getDbConfig = getDbConfig;<commit_msg>Reconnect to database on disconnect<commit_after>const path = require('path');
const fs = require('fs');
const streamsql = require('streamsql');
const config = require('./config');

function getDbConfig (prefix) {
  prefix += '_';
  return {
    driver:     config(prefix+'DRIVER', 'mysql'),
    host:       config(prefix+'HOST', 'localhost'),
    user:       config(prefix+'USER'),
    password:   config(prefix+'PASSWORD'),
    database:   config(prefix+'DATABASE')
  }
}

function getDb (prefix) {
  return streamsql.connect(getDbConfig(prefix));
};

module.exports.getDb = getDb;
module.exports.getDbConfig = getDbConfig;
{'fraction_non_alphanumeric': 0.1532481954469739, 'fraction_numerical': 0.0, 'mean_word_length': 3.66839378238342, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5470756', 'n_tokens_mistral': 551, 'n_tokens_neox': 557, 'n_words': 111}