1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
44
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
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
70 id = self.get(item, None)
71 if id == None:
72 id = self.addId(item)
73 return id
74
75
77 cu = self.db.cursor()
78 cu.execute("INSERT INTO %s (%s) VALUES (?)" %(
79 self.tableName, self.strName), (item,))
80 return cu.lastrowid
81
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
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
98 return "SELECT %s FROM %s WHERE %s=?" %(
99 self.keyName, self.tableName, self.strName)
100
105
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
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):
127
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
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
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
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
156
159
162
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):