summaryrefslogtreecommitdiff
path: root/kronos/storage.py
blob: 4add988fba081975e0b7eb95c609cce3dfad1e4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# vim: set filencoding=utf8
"""
Storage Back-ends For Kronos

@author: Mike Crute (mcrute@ag.com)
@organization: American Greetings Interactive
@date: February 04, 2010
"""


class StorageError(Exception):
    """
    Base class for all other storage exceptions.
    """


class NotConnected(StorageError):
    """
    Raised when a connection to the back-end is not avaiable.
    """


def _get_filtered_object_dict(dict_):
    output = {}
    for key, value in dict_.__dict__.items():
        if not key.startswith('_') and not callable(value):
            output[key] = value

    return output


class BaseStorageBackEnd(object):

    def __init__(self):
        self.load_engine()

    def get(self, model_obj, **kwargs):
        """
        Get a model object instance from the storage back-end
        where the criteria in the kwargs matches that in the
        back-end.
        """
        name = model_obj.__name__
        result = self.select(name, **kwargs)

        instance = model_obj()
        for key, value in result.items():
            if key != 'id':
                setattr(instance, key, value)
            else:
                instance.__db_id__ = value

        return instance

    def save(self, model_obj):
        """
        Persist a model object with the storage back-end.
        """
        name = model_obj.__class__.__name__
        id_ = getattr(model_obj, '__db_id__', None)

        values = _get_filtered_object_dict(model_obj)

        if id_:
            self.update(name, id_, **values)
        else:
            self.insert(name, **values)

    def load_engine(self):
        """
        Load a storage engine module that contains a function
        called connect suitable for setting up the actual
        connection.
        """

    def select(self, table, **kwargs):
        """
        Select raw data from the storage engine and return
        as a dictionary.
        """

    def insert(self, table, **values):
        """
        Insert values into the back-end named table or
        equivalent data structure.
        """

    def update(self, table, criteria, **values):
        """
        Update values in the back-end table or equivalent
        data structure based on the criteria.
        """

    def connect(self, database, *args, **kwargs):
        """
        Create a connection to the back-end.
        """

    def create_table(self, obj, **columns):
        """
        Create a table or equivalent data structure in the
        storage back-end.
        """


class SQLiteBackEnd(BaseStorageBackEnd):

    def __init__(self):
        super(SQLiteBackEnd, self).__init__()
        self.connection = None

    def load_engine(self):
        import sqlite3
        self.engine = sqlite3

    def _check_connection(self):
        if not self.connection:
            raise NotConnected("Not connected to storage back-end!")

    def connect(self, database, *args, **kwargs):
        self.connection = self.engine.connect(database)

    def select(self, table, **kwargs):
        sql = "SELECT * FROM {0} WHERE ".format(table)
        for key in kwargs.keys():
            sql += "{0}=? ".format(key)

        return self._get_normalized_results(sql, **kwargs)[0]

    def _get_normalized_results(self, sql, **kwargs):
        self._check_connection()
        cursor = self.connection.cursor()

        results = cursor.execute(sql, kwargs.values())
        return self._normalize_results(results)

    def _normalize_results(self, results):
        col_names = [col[0] for col in results.description]
        output = []
        for row in results.fetchall():
            output.append(dict(zip(col_names, row)))

        return output

    def insert(self, table, **values):
        placeholder = ("?," * len(values)).rstrip(',')
        col_spec = ','.join(values.keys())
        sql = "INSERT INTO {0}({1}) VALUES({2})".format(table, col_spec,
                                                        placeholder)

        self._execute_modification(sql, values.values())

    def update(self, table, criteria, **values):
        sql = "UPDATE {0} SET ".format(table)
        for key in values.keys():
            sql += "{0}=?, ".format(key)
        sql = sql[:-2] + "WHERE id=?"

        binds = values.values() + [criteria]
        self._execute_modification(sql, binds)

    def create_table(self, obj, **columns):
        table = obj.__name__

        sql = "CREATE TABLE {0} (".format(table)
        sql += "id INTEGER PRIMARY KEY"
        for key, value in columns.items():
            sql += ", {0} {1}".format(key, value)
        sql += ")"

        self._execute_modification(sql)

    def _execute_modification(self, sql, binds=()):
        self._check_connection()
        cursor = self.connection.cursor()
        cursor.execute(sql, binds)
        self.connection.commit()