From 98060f504aedb60713d90b093e8e628b0df9de86 Mon Sep 17 00:00:00 2001 From: hylom Date: Tue, 20 Feb 2018 20:32:18 +0900 Subject: [PATCH] add ns_search subdirectory --- src/ns_search/newslash_db/__init__.py | 4 +++ src/ns_search/newslash_db/base.py | 45 +++++++++++++++++++++++++++ src/ns_search/newslash_db/stories/__init__.py | 5 +++ src/ns_search/newslash_db/stories/stories.py | 33 ++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 src/ns_search/newslash_db/__init__.py create mode 100644 src/ns_search/newslash_db/base.py create mode 100644 src/ns_search/newslash_db/stories/__init__.py create mode 100644 src/ns_search/newslash_db/stories/stories.py diff --git a/src/ns_search/newslash_db/__init__.py b/src/ns_search/newslash_db/__init__.py new file mode 100644 index 00000000..9bdbf36c --- /dev/null +++ b/src/ns_search/newslash_db/__init__.py @@ -0,0 +1,4 @@ +# coding: utf-8 +'''newslash_db: newslash database module''' + +from stories import Stories diff --git a/src/ns_search/newslash_db/base.py b/src/ns_search/newslash_db/base.py new file mode 100644 index 00000000..433a5889 --- /dev/null +++ b/src/ns_search/newslash_db/base.py @@ -0,0 +1,45 @@ +# coding: utf-8 + +import mysql.connector + +CONFIG_FILE="./.config.yml" + +class NewslashDB(object): + def __init__(self, config): + self._config = config or {} + + # set default config values + self._set_config_default("default_limit", 100) + + def config(self, key, value=None): + if value: + self._config[key] = value + return value + return self._config.get(key) + + def _set_config_default(self, key, value): + if key not in self._config: + self.config(key, value) + + def cursor(self): + if "_conn" not in self.__dict__: + self.connect() + cur = self._conn.cursor(buffered=True) + return cur + + def execute(self, query, **kwargs): + cur = self.cursor() + cur.execute(query, params=kwargs) + return cur + + def connect(self): + self._conn = mysql.connector.connect(user=self.config("user"), + password=self.config("password"), + database=self.config("name"), + host=self.config("host")) + + def close(self): + if self._conn: + self._conn.close() + self._conn = None + diff --git a/src/ns_search/newslash_db/stories/__init__.py b/src/ns_search/newslash_db/stories/__init__.py new file mode 100644 index 00000000..4a786738 --- /dev/null +++ b/src/ns_search/newslash_db/stories/__init__.py @@ -0,0 +1,5 @@ +# coding: utf-8 +'''stories: newslash story related database module''' + +from stories import Stories + diff --git a/src/ns_search/newslash_db/stories/stories.py b/src/ns_search/newslash_db/stories/stories.py new file mode 100644 index 00000000..4fbe8c2c --- /dev/null +++ b/src/ns_search/newslash_db/stories/stories.py @@ -0,0 +1,33 @@ +# coding: utf-8 +'''stories.py: newslash story related database module''' + +from ..base import NewslashDB + +class Stories(NewslashDB): + def __init__(self, config): + super(Stories, self).__init__(config) + + def select(self, **kwargs): + if not "limit" in kwargs: + kwargs["limit"] = self.config("default_limit") + + if "stoid" in kwargs: + query = ("SELECT stories.stoid, stories.sid, story_text.* FROM stories" + " LEFT JOIN story_text USING(stoid)" + " WHERE stories.stoid = %(stoid)s" + " ORDER BY stories.stoid DESC LIMIT %(limit)s") + else: + query = ("SELECT stories.stoid, stories.sid, story_text.* FROM stories" + " LEFT JOIN story_text USING(stoid)" + " ORDER BY stories.stoid DESC LIMIT %(limit)s") + + cur = self.execute(query, **kwargs) + result = [] + for item in cur: + result.append(dict(zip(cur.column_names, item))) + + self.close() + return result + + + -- 2.11.0