def _updateOrInsertTableSmall(self, table): """Table is a list of tuples or lists each of which represents a database record (row of a table). For each row in table, either insert it into the database (into the table self.resultClass.table) if a record with the same key does not already exist, or update the existing record if necessary. Assumes that table is sorted by key. """ keyname = self.resultClass.primarykey key_index = self.resultClass.fields.index(keyname) updatesql = self.resultClass.updateSQL if keyname is None: raise DBAppError('updateOrInsertTable not implemented for composite primary keys') wherelist = [] for row in table: wherelist.append("%s = %s" % (keyname, sql.msql_quote(row[key_index]))) wherestr = string.join(wherelist, ' or ') res = self.getTable(where = wherestr, order = keyname) res_dict = {} for i in range(len(res)): res_dict[res[i][key_index]] = res[i] for row in table: key = row[key_index] dbrow = res_dict.get(key) if dbrow: if dbrow != row: updatesql.executeUpdate(row, where = "%s = %s" % (keyname, sql.msql_quote(key))) else: updatesql.executeInsert(row)
def getinv(self, inverse, where = None, v1 = None, k2 = None, v2 = None, order = None): if self.inversefieldtypes.has_key(inverse): if where == v1 == k2 == v2 == order == None: return self._getinverses(inverse) klass, field = self.inversefieldtypes[inverse] wherestr = "%s = %s" % (field, self.data[self.primarykey]) if not where is None and v1 is None: if where: wherestr = "%s and %s" % (where, wherestr) if where and not v1 is None: wherestr = "%s and %s = %s" % (wherestr, where, sql.msql_quote(v1)) if k2 and not v2 is None: wherestr = "%s and %s = %s" % (wherestr, k2, sql.msql_quote(v2)) return klass(where = wherestr, order = order) else: raise DBAppError("Not defined as inverse relation: '%s'" % inverse)
def deleteall(self): keyname = self.resultClass.primarykey if keyname is None: for o in self: o.delete() return wherelist = [] batchcount = 0 if self.data: _cache.clear() for o in self: if not isinstance(o, self.resultClass): raise DBAppError('Wrong object in Container for deletion') wherelist.append("%s = %s" % (keyname, sql.msql_quote(o[keyname]))) batchcount = batchcount + 1 if batchcount > 40: # Wir müssen die deletes ab und zu abschicken, da msql nicht # so lange where strings mag wherestr = string.join(wherelist, ' or ') self.resultClass.updateSQL.executeDelete(wherestr) wherelist = [] batchcount = 0 if wherelist: wherestr = string.join(wherelist, ' or ') self.resultClass.updateSQL.executeDelete(wherestr)
def __init__(self, s_NXXlnokey_word = None, **params): # s_NXXlnokey_word wird wohl niemand als Parameter für den Aufruf nehmen, # d.h. es ist nur belegt, wenn __init__ ohne keywords aufgerufen wurde. # Wir nehmen dann an, daß es sich um den Wert für den primarykey handelt. # self.changed = 0 if not s_NXXlnokey_word is None: if _cache.get(self, s_NXXlnokey_word): ## if self._test_data(): ## print self._test_data() ## print self.data ## raise AttributeError, 'in __getitem__' return primarykeyval = s_NXXlnokey_word if params: raise DBAppError("No additional parameters allowed if primarykey value is given.") params[self.primarykey] = primarykeyval elif not params: # keine Parameter, leeres, unregistriertes Objekt self.data = {} return self.data = params if _cache.get(self): return wherelist = [] for k,v in params.items(): wherelist.append("%s = %s" % (k, sql.msql_quote(v))) where = string.join(wherelist, ' and ') res = self.querySQL.executeQuery(where) if len(res) != 1: raise DBAppError( "No unique object for given parameters: %s (class %s)" % (params, self.__class__.__name__)) self.data = sql.tuple2dict(res[0], self.querySQL.fieldnames) self.data['__p__'] = 1 _cache.cache(self)
def _testKey(self): if self.primarykey: val = self.get(self.primarykey) if val is None: raise DBAppError("Required key '%s' missing for insert, update or delete" % (self.primarykey,)) where ="%s = %s" % (self.primarykey, sql.msql_quote(val)) else: kv = self._getKeyValues(self.data) if kv: otherkeys, values = kv else: raise DBAppError("Required keys '%s' missing for insert, update or delete" % (self.otherkeys,)) wherelist = [] for k,v in map(None, otherkeys, values): wherelist.append("%s = %s" % (k, sql.msql_quote(v))) where = string.join(wherelist, ' and ') return where, self.querySQL.executeQuery(where)
def __init__(self, data = [], where = None, k2 = None, v2 = None, order = '', join = []): self.data = [] if data and where is None: if type(data) == type([]): # Initialisierung mit Liste von ids if type(data[0]) in (type(1L), type(1)): for id in data: self.data.append(self.resultClass(id)) # Initialisierung mit Liste von Objekten elif isinstance(data[0], self.resultClass): self.data = data else: raise DBAppError('Wrong data for initialization of Container: %s', data) elif isinstance(data, self.__class__): # Initialisierung mit Container self.data = data.data else: raise DBAppError('Wrong data for initialization of Container: %s', data) return if data and not where is None: # wird als feld = wert interpretiert where = "%s = %s" % (data, sql.msql_quote(where)) if k2 and not v2 is None: where = "%s and %s = %s" % (where, k2, sql.msql_quote(v2)) if not where is None: where, order, join = self._expand(where, order, join) res = self.querySQL.executeQuery(where, order, join) for tuple in res: dict = sql.tuple2dict(tuple, self.querySQL.fieldnames) m = self.resultClass() m.data = dict # m.data may be replaced by an already cached state of the object # the values from the database should be identical to the cached # values, but the cached object may have additional values if _cache.get(m): # vielleicht besser: # assert m.data == dict (nicht ganz, da es auch noch gecachte paths geben kann) m.data.update(dict) else: m.data['__p__'] = 1 _cache.cache(m) self.data.append(m)
def __init__(self, s_NXXlnokey_word = None, **params): # s_NXXlnokey_word wird wohl niemand als Parameter für den Aufruf nehmen, # d.h. es ist nur belegt, wenn __init__ ohne keywords aufgerufen wurde. # Wir nehmen dann an, daß es sich um den Wert für den primarykey handelt. # self.changed = 0 if not s_NXXlnokey_word is None: # Hier gibt es nur Treffer, wenn s_NXXlnokey_word im Normalfall # ein Integer ist. Falls die ID als String übergeben wird, # kommt weiter unten die SQL-Query zum Zuge. Ebenfalls erfolgreich, # aber unter Umgehung des Caches # Braucht man den Cache überhaupt? if _cache.get(self, s_NXXlnokey_word): return primarykeyval = s_NXXlnokey_word if params: raise DBAppError("No additional parameters allowed if primarykey value is given.") params[self.primarykey] = primarykeyval elif not params: # keine Parameter, leeres, unregistriertes Objekt self.data = {} return self.data = params if _cache.get(self): return wherelist = [] for k,v in params.items(): wherelist.append("%s = %s" % (k, sql.msql_quote(v))) where = string.join(wherelist, ' and ') res = self.querySQL.executeQuery(where) if len(res) != 1: raise DBAppError( "No unique object for given parameters: %s (class %s)" % (params, self.__class__.__name__)) self.data = sql.tuple2dict(res[0], self.querySQL.fieldnames) self.data['__p__'] = 1 _cache.cache(self)