From 54bca5894f9f0866538292f40593f99e61eeae97 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 27 Apr 2014 00:47:13 -0400 Subject: [PATCH] Move database stuff to a subpackage; updates. --- bitshift/{database.py => database/__init__.py} | 39 ++++++++++++++++++++++---- schema.sql => bitshift/database/schema.sql | 0 2 files changed, 34 insertions(+), 5 deletions(-) rename bitshift/{database.py => database/__init__.py} (55%) rename schema.sql => bitshift/database/schema.sql (100%) diff --git a/bitshift/database.py b/bitshift/database/__init__.py similarity index 55% rename from bitshift/database.py rename to bitshift/database/__init__.py index 02aa38e..4ed7a02 100644 --- a/bitshift/database.py +++ b/bitshift/database/__init__.py @@ -1,12 +1,16 @@ """ -Module with classes and functions to handle communication with the MySQL +Subpackage with classes and functions to handle communication with the MySQL database backend, which manages the search index. """ +import os + import mmh3 import oursql -# from .languages import ... +# from ..languages import ... + +__all__ = ["Database"] class Database(object): """Represents the MySQL database.""" @@ -16,7 +20,9 @@ class Database(object): def _connect(self): """Establish a connection to the database.""" - self._conn = oursql.connect() + default_file = os.path.join(os.path.dirname(__file__), ".my.cnf") + self._conn = oursql.connect(read_default_file=default_file, + autoping=True, autoreconnect=True) def search(self, query, page=1): """ @@ -48,5 +54,28 @@ class Database(object): :param codelet: The codelet to insert. :type codelet: :py:class:`.Codelet` """ - # code_hash = mmh3.hash64(codelet.code.encode("utf8"))[0] - pass + query = "INSERT INTO codelets VALUES (?, ?, ?, ?, ?, ?, ?, ?)" + + cursor.execute(query, ()) + + # codelet_id -- auto_increment used here + codelet_name + codelet_code_id + codelet_lang + codelet_origin + codelet_url + codelet_rank + codelet_date_created + codelet_date_modified + + # codelet fields + codelet.name + codelet.code + codelet.filename + codelet.language + codelet.authors + codelet.code_url + codelet.date_created + codelet.date_modified + + code_hash = mmh3.hash64(codelet.code.encode("utf8"))[0] diff --git a/schema.sql b/bitshift/database/schema.sql similarity index 100% rename from schema.sql rename to bitshift/database/schema.sql