Package conary :: Package dbstore :: Module idtable
[hide private]
[frames] | no frames]

Source Code for Module conary.dbstore.idtable

  1  # 
  2  # Copyright (c) 2004-2008 rPath, Inc. 
  3  # 
  4  # This program is distributed under the terms of the Common Public License, 
  5  # version 1.0. A copy of this license should have been distributed with this 
  6  # source file in a file called LICENSE. If it is not present, the license 
  7  # is always available at http://www.rpath.com/permanent/licenses/CPL-1.0. 
  8  # 
  9  # This program is distributed in the hope that it will be useful, but 
 10  # without any warranty; without even the implied warranty of merchantability 
 11  # or fitness for a particular purpose. See the Common Public License for 
 12  # full details. 
 13  # 
 14   
 15  # FIXME: convert to use the dbstore modules 
 16   
17 -def createIdTable(db, tableName, keyName, strName, colType = 'STRING'):
18 commit = False 19 idxName = "%s_uq" % tableName 20 cu = db.cursor() 21 if tableName not in db.tables: 22 cu.execute(""" 23 CREATE TABLE %s ( 24 %s %%(PRIMARYKEY)s, 25 %s %%(%s)s 26 ) %%(TABLEOPTS)s""" %(tableName, keyName, strName, colType) % db.keywords) 27 db.tables[tableName] = [] 28 commit = True 29 db.createIndex(tableName, idxName, strName, unique = True) 30 return commit
31
32 -def createMappingTable(db, tableName, key, item):
33 commit = False 34 cu = db.cursor() 35 if tableName not in db.tables: 36 cu.execute(""" 37 CREATE TABLE %s( 38 %s INTEGER, 39 %s INTEGER 40 )""" % (tableName, key, item)) 41 db.tables[tableName] = [] 42 commit = True 43 return commit
44
45 -def createIdPairTable(db, tableName, tup1, tup2, item):
46 commit = False 47 cu = db.cursor() 48 if tableName not in db.tables: 49 cu.execute(""" 50 CREATE TABLE %s( 51 %s INTEGER, 52 %s INTEGER, 53 %s INTEGER 54 )""" % (tableName, tup1, tup2, item)) 55 db.tables[tableName] = [] 56 commit = True 57 return commit
58
59 -class IdTable:
60 """ 61 Generic table for assigning id's to simple items. 62 """
63 - def __init__(self, db, tableName, keyName, strName):
64 self.db = db 65 self.tableName = tableName 66 self.keyName = keyName 67 self.strName = strName
68
69 - def getOrAddId(self, item):
70 id = self.get(item, None) 71 if id == None: 72 id = self.addId(item) 73 return id
74 75 # DBSTORE: use dbstore sequences
76 - def addId(self, item):
77 cu = self.db.cursor() 78 cu.execute("INSERT INTO %s (%s) VALUES (?)" %( 79 self.tableName, self.strName), (item,)) 80 return cu.lastrowid
81
82 - def delId(self, theId):
83 assert(isinstance(theId, (int, long))) 84 cu = self.db.cursor() 85 cu.execute("DELETE FROM %s WHERE %s=?" 86 %(self.tableName, self.keyName), (theId,))
87
88 - def getId(self, theId):
89 cu = self.db.cursor() 90 cu.execute("SELECT %s FROM %s WHERE %s=?" 91 %(self.strName, self.tableName, self.keyName), (theId,)) 92 try: 93 return cu.next()[0] 94 except StopIteration: 95 raise KeyError, theId
96
97 - def __select(self):
98 return "SELECT %s FROM %s WHERE %s=?" %( 99 self.keyName, self.tableName, self.strName)
100
101 - def has_key(self, item):
102 cu = self.db.cursor() 103 cu.execute(self.__select(), (item,)) 104 return not(cu.fetchone() == None)
105
106 - def __delitem__(self, item):
107 assert(type(item) is str) 108 cu = self.db.cursor() 109 cu.execute("DELETE FROM %s WHERE %s=?" 110 %(self.tableName, self.strName), item)
111
112 - def __getitem__(self, item):
113 cu = self.db.cursor() 114 cu.execute(self.__select(), (item,)) 115 try: 116 return cu.next()[0] 117 except StopIteration: 118 raise KeyError, item
119
120 - def get(self, item, defValue):
121 cu = self.db.cursor() 122 cu.execute(self.__select(), (item,)) 123 item = cu.fetchone() 124 if not item: 125 return defValue 126 return item[0]
127
128 - def getItemDict(self, itemSeq):
129 cu = self.db.cursor() 130 cu.execute("SELECT %s, %s FROM %s WHERE %s in (%s)" 131 % (self.strName, self.keyName, self.tableName, self.strName, 132 ",".join(["'%s'" % x for x in itemSeq]))) 133 return dict(cu)
134
135 - def iterkeys(self):
136 cu = self.db.cursor() 137 cu.execute("SELECT %s FROM %s" %(self.strName, self.tableName)) 138 for row in cu: 139 yield row[0]
140
141 - def itervalues(self):
142 cu = self.db.cursor() 143 cu.execute("SELECT %s FROM %s" %(self.keyName, self.tableName)) 144 for row in cu: 145 yield row[0]
146
147 - def iteritems(self):
148 cu = self.db.cursor() 149 cu.execute("SELECT %s, %s FROM %s" 150 %(self.strName, self.keyName, self.tableName)) 151 for row in cu: 152 yield row
153
154 - def keys(self):
155 return [ x for x in self.iterkeys() ]
156
157 - def values(self):
158 return [ x for x in self.itervalues() ]
159
160 - def items(self):
161 return [ x for x in self.iteritems() ]
162
163 -class CachedIdTable(IdTable):
164 """ 165 Provides an IdTable mapping with three differences -- ids are cached, 166 they can't be removed, and getting a tag creates it if it doesn't 167 already exist. This is designed for small tables! 168 """ 169
170 - def __init__(self, db, tableName, keyName, strName):
171 IdTable.