forked from SeanCCarter/ohell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
166 lines (132 loc) · 4.36 KB
/
database.py
File metadata and controls
166 lines (132 loc) · 4.36 KB
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
import _mysql
from MySQLdb.constants import FIELD_TYPE
class DictObject(object):
"""
Class that creates dynamic attributes from a specified dictionary
"""
def __init__(self, dict):
"""
Constructor
Parameter:
dict - Dictionary with attributes for object
"""
self._dict = dict
def __getattribute__( self, name):
"""
Get attribute hook
Parameter:
name - Name of attribute
"""
# Get internal _dict attribute
dict = object.__getattribute__(self, "_dict")
# if attribute define in dict, use that it
# otherwise use real attribute of class
if name in dict:
return dict[name]
else:
return object.__getattribute__(self, name)
class DBDictGenerator:
"""
Class that creates a dictionary from the results of a DB query
"""
def __init__( self, db, query):
db.query(query)
self.result = db.store_result()
def generate_dict(self):
(dict,) = self.result.fetch_row(1,1)
return dict
class Row(DictObject):
"""
Class that represents a row result of a database query
with dynamic attributes of a python object
"""
def __init__( self, generator):
"""
Constructor
Requires a generator object that has a generate_dict()
method that returns a dictionary with the desired attribute
Parameter:
generator - Generator of internal dictonary for attributes
"""
DictObject.__init__( self, generator.generate_dict())
def __str__( self ):
dict = object.__getattribute__(self, "_dict")
return str(dict)
class SimpleDictGenerator:
"""
Simple DictGenerator object that simple returns a specified
dictionary
"""
def __init__(self, dict):
self.dict = dict
def generate_dict(self):
return self.dict
def open_database( host, db_name, config_file):
my_conv = { FIELD_TYPE.LONG: int }
config = open(config_file)
line = config.readline()
(user, password) = line.split()
db = _mysql.connect(host=host, user=user, passwd=password,
db=db_name, conv=my_conv)
return db
def table_generator( db, query):
"""
A generator function that returns the rows of a database query
Parameters:
db - Database connection
query - Query to use
"""
db.query(query)
result = db.store_result()
done = False
while not done:
row = result.fetch_row(1,1)
done = len(row) == 0
if not done:
(dict,) = row
yield Row(SimpleDictGenerator(dict))
def get_table( db, table_name, key_name, suffix=None, columns='*'):
"""
Get table as a dictonary of items keyed by database key
This method only works with a single field database key
Parameters:
db - Database connection
table_name - Name(s) of table to retrieve data from (comma separated)
key_name - Name of table primary key
suffix - Suffix for SELECT statement (put WHERE and ORDERED BY here)
columns - Which columns to return (defaults to all)
Returns:
A dictionary of Row objects from query
"""
if suffix == None:
query = 'SELECT ' + columns + ' FROM ' + table_name + ';'
else:
query = 'SELECT ' + columns + ' FROM ' + table_name + ' ' + suffix + ';'
#print query
rowList = [ x for x in table_generator(db, query)]
table = {}
for r in rowList:
table[r.__getattribute__(key_name)] = r
return table
def get_table_with_composite_key( db, table_name, key_names, suffix=None, columns='*'):
"""
Get table as a dictonary of items keyed by a composite database key
This method only works with a composite key of two fields
Parameters:
db - Database connection
table_name - Name(s) of table to retrieve data from (comma separated)
key_names - List of composite key fields
suffix - Suffix for SELECT statement (put WHERE and ORDERED BY here)
columns - Which columns to return (defaults to all)
Returns:
A dictionary of Row objects from query
"""
if suffix == None:
query = 'SELECT ' + columns + ' FROM ' + table_name + ';'
else:
query = 'SELECT ' + columns + ' FROM ' + table_name + ' ' + suffix + ';'
rowList = [ x for x in table_generator(db, query)]
table = {}
for r in rowList:
table[(r.__getattribute__(key_names[0]), r.__getattribute__(key_names[1]))] = r
return table