def __init__(self,data,condId,directAccess): '''Constructor, set local data and parse condition''' self.newcond = None if "value" not in data: raise DutilException("FilterError","Item 'value' is missing in condition.") if "condition" not in data: raise DutilException("FilterError","Item 'condition' is missing in condition.") self.directAccess = directAccess self.val = data["value"] self.op = data["condition"] self.counter = condId self.id = "${0}".format(condId) self.metadata = False self.idSet = False self.checkDistinct = False if "metadata" in data: self.kind = "metadata" self.metadata = True self.col = data["metadata"] # we cannot see now, if it is about pending changeset or list revisions if self.col not in ["revision","message","author","timestamp","changeset"]: raise DutilException("FilterError","Attribute {0} does not exists.".format(self.col)) elif "kind" in data: self.kind = data["kind"] if "attribute" not in data: raise DutilException("FilterError","Item 'attribute' is missing in condition.") self.col = data["attribute"] if self.kind not in generated.kinds(): raise DutilException("FilterError","Kind {0} does not exists.".format(self.col)) # add also name if self.col not in generated.atts(self.kind) and self.col != "name": raise DutilException("FilterError","Attribute {0} does not exists.".format(self.col)) if self.col != "name" and generated.atts(self.kind)[self.col] == "identifier_set": self.idSet = True # performance hack if self.op == "columnContains": global numberOfColumnContainsInFilter numberOfColumnContainsInFilter = numberOfColumnContainsInFilter + 1 else: raise DutilException("FilterError","Item 'kind' or 'metadata' must be in condition.") self.parse()
def oneKindDiff(kindName,diffname,a = None,b = None): '''get diff for one kind''' if diffname == "resolved": if hasTemplate(kindName): diffname = "_resolved_diff" else: diffname = "_diff" else: diffname = "_diff" with xact(): if (a is None) and (b is None): # diff for temporaryChangeset diffname = "_init_ch" + diffname init = proc(kindName + diffname + "(bigint)") #send proper values for diff_set_attr... (#292) a = 0 init(a) paramsDef = "$1" params = [a] elif (b is None): # diff for temporaryChangeset with changeset parameter diffname = "_init_ch" + diffname init = proc(kindName + diffname + "(bigint)") #get changeset ids first changeset2id = proc("changeset2id(text)") a = changeset2id(a) init(a) paramsDef = "$1" params = [a] else: # diff for 2 revisions diffname = "_init" + diffname init = proc(kindName + diffname + "(bigint,bigint)") #get changeset ids first revision2num = proc("revision2num(text)") a = revision2num(a) b = revision2num(b) init(a,b) paramsDef = "$1,$2" params = [a,b] Postgres.NOTICE("Running diff: {0}{1} {2}".format(kindName,diffname,[int(x) for x in params])) terminate = proc(kindName + "_terminate_diff()") created = prepare("SELECT * FROM {0}_diff_created({1})".format(kindName,paramsDef)) renamed = prepare("SELECT * FROM {0}_diff_rename({1})".format(kindName,paramsDef)) setattr = prepare("SELECT * FROM {0}_diff_set_attributes({1})".format(kindName,paramsDef)) if "identifier_set" in generated.atts(kindName).values(): '''for identifier_set''' setattr2 = prepare("SELECT * FROM {0}_diff_refs_set_set_attributes({1})".format(kindName,paramsDef)) else: setattr2 = None deleted = prepare("SELECT * FROM {0}_diff_deleted({1})".format(kindName,paramsDef)) res = list() for line in deleted(*params): line = pytypes(line) obj = dict() obj["command"] = "deleteObject" obj["kindName"] = kindName obj["objectName"] = line[0] res.append(obj) for line in renamed(*params): line = pytypes(line) obj = dict() obj["command"] = "renameObject" obj["kindName"] = kindName obj["oldObjectName"] = line[0] obj["newObjectName"] = line[1] res.append(obj) for line in created(*params): line = pytypes(line) obj = dict() obj["command"] = "createObject" obj["kindName"] = kindName obj["objectName"] = line[0] res.append(obj) setattrRes = setattr(*params) if setattr2 is not None: setattrRes = setattrRes + setattr2(*params) for line in setattrRes: line = pytypes(line,[2,3]) obj = dict() obj["command"] = "setAttribute" obj["kindName"] = kindName obj["objectName"] = line[0] obj["attributeName"] = line[1] obj["oldAttributeData"] = line[2] obj["attributeData"] = line[3] res.append(obj) terminate() return res
def hasTemplate(kindName): '''Test if given kind is templated''' if "template_"+kindName in generated.atts(kindName): return True else: return False
def getJoin(self,mykind): '''Return join part of sql statement''' ret = '' self.kinds = self.kinds - set([mykind]) for kind in self.kinds: if mykind == "metadata": joincond = "{0}.id = {1}.version".format(mykind,kind) ret = ret + self.JOIN + "{tbl}_history AS {tbl} ON {cond} ".format(tbl = kind, cond = joincond) else: if kind not in generated.kinds(): raise DutilException("FilterError","Kind {0} does not exists.".format(kind)) # check for refs and templates refNames = generated.refNames() relNames = generated.refNames() # add templates - same as refs for our stuff here relNames.update(generated.templateNames()) # add contains/containable - same as refs for our stuff here relNames.update(generated.containsNames()) relNames.update(generated.containableNames()) findJoinable = False # find if there is ref relation from mykind to kind or kind to mykind for relName in relNames: fromTbl = generated.relFromTbl(relName) toTbl = generated.relToTbl(relName) fromCol = generated.relFromCol(relName) if generated.atts(fromTbl)[fromCol] == "identifier_set": if fromTbl == kind and toTbl == mykind: # join inner table tbl = "inner_{0}_{1}".format(fromTbl,fromCol) joincond = "{0}.uid = {1}.{0}".format(toTbl,tbl) ret = ret + self.JOIN + "{tbl}{data} AS {tbl} ON {cond} ".format(tbl = tbl, cond = joincond, data = getDataSuffix(toTbl,self.directAccess)) # and join table of wanted kind tbl = "inner_{0}_{1}".format(fromTbl,fromCol) joincond = "{0}.uid = {1}.{0}".format(fromTbl,tbl) ret = ret + self.JOIN + "{tbl}{data} AS {tbl} ON {cond} ".format(tbl = fromTbl, cond = joincond, data = getDataSuffix(fromTbl,self.directAccess)) findJoinable = True elif toTbl == kind and fromTbl == mykind: # join inner table tbl = "inner_{0}_{1}".format(fromTbl,fromCol) joincond = "{0}.uid = {1}.{0}".format(fromTbl,tbl) ret = ret + self.JOIN + "{tbl}{data} AS {tbl} ON {cond} ".format(tbl = tbl, cond = joincond, data = getDataSuffix(fromTbl,self.directAccess)) # and join table of wanted kind tbl = "inner_{0}_{1}".format(fromTbl,fromCol) joincond = "{0}.uid = {1}.{0}".format(toTbl,tbl) ret = ret + self.JOIN + "{tbl}{data} AS {tbl} ON {cond} ".format(tbl = toTbl, cond = joincond, data = getDataSuffix(toTbl,self.directAccess)) findJoinable = True elif fromTbl == kind and toTbl == mykind: joincond = "{0}.uid = {1}.{2}".format(mykind,kind,fromCol) ret = ret + self.JOIN + "{tbl}{data} AS {tbl} ON {cond} ".format(tbl = kind, cond = joincond, data = getDataSuffix(kind,self.directAccess)) findJoinable = True # back relation only for refs elif relName in refNames and toTbl == kind and fromTbl == mykind: joincond = "{0}.{2} = {1}.uid".format(mykind,kind,fromCol) ret = ret + self.JOIN + "{tbl}{data} AS {tbl} ON {cond} ".format(tbl = kind, cond = joincond, data = getDataSuffix(kind,self.directAccess)) findJoinable = True # find if there is embeding embedNames = generated.embedNames() for relName in embedNames: fromTbl = generated.relFromTbl(relName) toTbl = generated.relToTbl(relName) if fromTbl == kind and toTbl == mykind: joincond = "{0}.uid = {1}.{2}".format(mykind,kind,generated.relFromCol(relName)) ret = ret + self.JOIN + "{tbl}{data} AS {tbl} ON {cond} ".format(tbl = kind, cond = joincond, data = getDataSuffix(kind,self.directAccess)) findJoinable = True if toTbl == kind and fromTbl == mykind: joincond = "{0}.{2} = {1}.uid".format(mykind,kind,generated.relFromCol(relName)) ret = ret + self.JOIN + "{tbl}{data} AS {tbl} ON {cond} ".format(tbl = kind, cond = joincond, data = getDataSuffix(kind,self.directAccess)) findJoinable = True if not findJoinable: raise DutilException("FilterError","Kind {0} cannot be joined with kind {1}.".format(kind,mykind)) #TODO: merge, template relations return ret + self.getIdSetJoin()