1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import string
19
20 from rhn.UserDictCase import UserDictCase
21 from spacewalk.common.rhnException import rhnException
22
23 import sql_base
24 import sql_lib
25
26
27 -class Row(UserDictCase):
28
29 """ This class allows one to work with the columns of a particular row in a more
30 convenient manner (ie, using a disctionary interface). It allows for the row
31 data to be loaded and saved and is generally easier to use than the Table
32 class which is really designed for bulk updates and stuff like that.
33
34 The easiest way to separate what these things are for is to remember that
35 the Table class indexes by KEY, while the Row class indexes by column
36 """
37
38 - def __init__(self, db, table, hashname, hashval=None):
52
54 return "<%s instance at 0x%0x on (%s, %s, %s)>" % (
55 self.__class__.__name__, abs(id(self)),
56 self.table, self.hashname, self.get(self.hashname))
57 __str__ = __repr__
58
60 """ make it work like a dictionary """
61 x = string.lower(name)
62
63
64
65
66 if x == self.hashname:
67 raise AttributeError("Can not reset the value of the hash key")
68 if x not in self.data or self.data[x][0] != value:
69 self.data[x] = (value, 1)
70
72 x = string.lower(name)
73 if x in self.data:
74 return self.data[x][0]
75 raise KeyError("Key %s not found in the Row dictionary" % name)
76
77 - def get(self, name):
78 x = string.lower(name)
79 if x in self.data:
80 return self.data[x][0]
81 return None
82
84 """ reset the changed status for these entries """
85 for k, v in self.data.items():
86
87 self.data[k] = (v[0], val)
88
90 """ create it as a new entry """
91 self.data[self.hashname] = (hashval, 0)
92 self.real = 0
93 self.save()
94
95 - def load(self, hashval):
96 """ load an entry """
97 return self.load_sql("%s = :hashval" % self.hashname, {'hashval': hashval})
98
100 """ load from a sql clause """
101 h = self.db.prepare("select * from %s where %s" % (self.table, sql))
102 h.execute(**pdict)
103 ret = h.fetchone_dict()
104 self.data = {}
105 if not ret:
106 self.real = 0
107 return 0
108 for k, v in ret.items():
109 self.data[k] = (v, 0)
110 self.real = 1
111 return 1
112
113 - def save(self, with_updates=1):
134