<commit_before>"""Functionality to support interacting with a SQLite Database
  Note the threading limitation at:
  http://cherrypy.readthedocs.org/en/latest/tutorials.html#tutorial-9-data-is-all-my-life
  This is why we open/close the connection around each call
"""

import os
import json
import onrampdb
import sqlite3
from threading import Lock

class Database_sqlite(onrampdb.Database):
    _name = '[DB SQLite]'
    _lock = Lock()

    def __init__(self, logger, auth):
        onrampdb.Database.__init__(self, logger, auth)

        if os.path.exists(self._auth['filename']) == False:
            logger.critical(self._name + " Filename does not exist \""+self._auth['filename']+"\"")
        else:
            logger.debug(self._name + " Will connect with " + self._auth['filename'])

        self._connection = None
        self._cursor = None


    ##########################################################
    def connect(self):
        noop = 1

    def _connect(self):
        #self._logger.debug(self._name + " Connecting...")
        self._connection = sqlite3.connect( self._auth['filename'] )
        self._cursor = self._connection.cursor()
        self._lock.acquire()

    def is_connected(self):
        is_connected = self._connection is not None
        return is_connected

    def disconnect(self):
        noop = 1

    def _disconnect(self):
        #self._logger.debug(self._name + " Disonnecting...")
        self._connection.commit()
        self._connection.close()
        self._connection = None
        self._cursor = None
        self._lock.release()

    #######################################################################
    def _valid_id_check(self, sql, args):
        self._logger.debug(self._name + " " + sql)

        self._connect()
        self._cursor.execute(sql, args )
        row = self._cursor.fetchone()
        self._disconnect()

        if row is None:
            return False
        else:
            return True<commit_msg>reposition the lock so that it protects the whole connection<commit_after>"""Functionality to support interacting with a SQLite Database
  Note the threading limitation at:
  http://cherrypy.readthedocs.org/en/latest/tutorials.html#tutorial-9-data-is-all-my-life
  This is why we open/close the connection around each call
"""

import os
import json
import onrampdb
import sqlite3
from threading import Lock
from time import sleep

class Database_sqlite(onrampdb.Database):
    _name = '[DB SQLite]'
    _lock = Lock()

    def __init__(self, logger, auth):
        onrampdb.Database.__init__(self, logger, auth)

        if os.path.exists(self._auth['filename']) == False:
            logger.critical(self._name + " Filename does not exist \""+self._auth['filename']+"\"")
        else:
            logger.debug(self._name + " Will connect with " + self._auth['filename'])

        self._connection = None
        self._cursor = None


    ##########################################################
    def connect(self):
        noop = 1

    def _connect(self):
        #self._logger.debug(self._name + " Connecting...")
        # Need a lock to protect a re-entrant thread situation
        self._lock.acquire()

        self._connection = sqlite3.connect( self._auth['filename'] )
        self._cursor = self._connection.cursor()

    def is_connected(self):
        is_connected = self._connection is not None
        return is_connected

    def disconnect(self):
        noop = 1

    def _disconnect(self):
        #self._logger.debug(self._name + " Disonnecting...")
        self._connection.commit()
        self._connection.close()
        self._connection = None
        self._cursor = None

        # Need a lock to protect a re-entrant thread situation
        self._lock.release()

    #######################################################################
    def _valid_id_check(self, sql, args):
        self._logger.debug(self._name + " " + sql)

        self._connect()
        self._cursor.execute(sql, args )
        row = self._cursor.fetchone()
        self._disconnect()

        if row is None:
            return False
        else:
            return True
{'fraction_non_alphanumeric': 0.16395014381591563, 'fraction_numerical': 0.0023969319271332696, 'mean_word_length': 2.628695652173913, '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': '1440325', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1119, 'n_words': 286}