Package backend :: Package server :: Package rhnSQL :: Module sql_row
[hide private]
[frames] | no frames]

Source Code for Module backend.server.rhnSQL.sql_row

  1  # 
  2  # Copyright (c) 2008--2016 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15  # a class used to handle a row of data in a particular table 
 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):
39 UserDictCase.__init__(self) 40 if not isinstance(db, sql_base.Database): 41 raise rhnException("Argument db is not a database instance", db) 42 self.db = db 43 self.table = table 44 self.hashname = string.lower(hashname) 45 46 # and the data dictionary 47 self.data = {} 48 # is this a real entry (ie, use insert or update) 49 self.real = 0 50 if hashval is not None: # if we have to load an entry already... 51 self.load(hashval)
52
53 - def __repr__(self):
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
59 - def __setitem__(self, name, value):
60 """ make it work like a dictionary """ 61 x = string.lower(name) 62 # forbid setting the value of the hash column because of the 63 # ambiguity of the operation (is it a "save as new id" or 64 # "load from new id"?). We provide interfaces for load, save 65 # and create instead. 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
71 - def __getitem__(self, name):
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
83 - def reset(self, val=0):
84 """ reset the changed status for these entries """ 85 for k, v in self.data.items(): 86 # tuples do not support item assignement 87 self.data[k] = (v[0], val)
88
89 - def create(self, hashval):
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
99 - def load_sql(self, sql, pdict={}):
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):
114 """ now save an entry """ 115 if self.hashname not in self.data: 116 raise AttributeError("Table does not have a hash `%s' key" % self.hashname) 117 # get a list of fields to be set 118 items = [(a[0], a[1][0]) for a in [b for b in list(self.data.items()) if b[1][1] == 1]] 119 if not items: # if there is nothing for us to do, avoid doing it. 120 return 121 # and now build the SQL statements 122 if self.real: # Update 123 if not with_updates: 124 raise sql_base.ModifiedRowError() 125 sql, pdict = sql_lib.build_sql_update(self.table, self.hashname, items) 126 else: 127 sql, pdict = sql_lib.build_sql_insert(self.table, self.hashname, items) 128 h = self.db.prepare(sql) 129 pdict["p0"] = self.data[self.hashname][0] 130 # and now do it 131 h.execute(**pdict) 132 self.real = 1 133 return
134