def get(cls, key): with gripe.db.Tx.begin(): if isinstance(key, str): key = grumble.key.Key(key) assert isinstance( key, grumble.key.Key), "ModelQuery.get requires a valid key object" q = ModelQuery().set_key(key) mm = grumble.schema.ModelManager.for_name(key.kind()) r = mm.getModelQueryRenderer(q) return r.execute(grumble.dbadapter.QueryType.Columns, key.kind()).single_row_bycolumns()
def delete_one(cls, key): if isinstance(key, basestring): key = grumble.key.Key(key) elif hasattr(key, "key") and callable(key.key): key = key.key() assert isinstance(key, grumble.key.Key), "ModelQuery.delete_one requires a valid key object" return ModelQuery().set_key(key)._delete(key.kind())
def set(cls, insert, key, values): with gripe.db.Tx.begin(): if isinstance(key, str): key = grumble.key.Key(key) elif key is None and insert: pass elif hasattr(key, "key") and callable(key.key): key = key.key() else: assert isinstance(key, grumble.key.Key), \ "ModelQuery.get requires a valid key object, not a %s" % type(key) q = ModelQuery().set_key(key) mm = grumble.schema.ModelManager.for_name(key.kind()) r = mm.getModelQueryRenderer(q) r.execute( grumble.dbadapter.QueryType.Insert if insert else grumble.dbadapter.QueryType.Update, key.kind(), values)
def delete_one(cls, key): if isinstance(key, basestring): key = grumble.key.Key(key) elif hasattr(key, "key") and callable(key.key): key = key.key() assert isinstance( key, grumble.key.Key ), "ModelQuery.delete_one requires a valid key object" return ModelQuery().set_key(key)._delete(key.kind())
def to_sql(self, vals=None, alias="k"): if self.value is None: if self.op == "!=": n = " IS NOT NULL" else: n = " IS NULL" return '(%s.%s%s)' % (self.alias(alias), self.colname, n) elif self.op and self.op.endswith("IN"): try: if vals is not None: vals.extend(self.value) num = len(self.value) except TypeError: if vals is not None: vals.append(self.value) num = 1 if num > 1: return ' (%s.%s %s (' % (self.alias(alias), self.colname, self.op) + \ ', '.join(["%%s"] * num) + ')' elif num == 1: return '(%s.%s %s %%s)' % \ (self.alias(alias), self.colname, "=" if self.op == "IN" else "!=") elif self.op == '->': key = grumble.key.to_key(self.value) if key: if vals is not None: vals.append(str(key)) vals.append(str(key)) mm = key.modelmanager() return ('''%s.%s IN (SELECT CONCAT_WS(\'/\', "_parent", \'%s:\' || "%s") FROM %s WHERE (POSITION(%%s IN "_parent") > 0) OR (CONCAT_WS(\'/\', "_parent", \'%s:\' || "%s") = %%s))''' % (self.alias(alias), self.colname, key.kind(), mm.key_col.name, mm.tablename, key.kind(), mm.key_col.name)) else: return '' else: if vals is not None: vals.append(self.value) return '(%s.%s %s %%s)' % (self.alias(alias), self.colname, self.op)
def get(cls, key): with gripe.db.Tx.begin(): if isinstance(key, basestring): key = grumble.key.Key(key) else: assert isinstance(key, grumble.key.Key), "ModelQuery.get requires a valid key object" q = ModelQuery().set_key(key) mm = grumble.schema.ModelManager.for_name(key.kind()) r = mm.getModelQueryRenderer(q) return r.execute(grumble.dbadapter.QueryType.Columns).single_row_bycolumns()
def set(cls, insert, key, values): with gripe.db.Tx.begin(): if isinstance(key, basestring): key = grumble.key.Key(key) elif key is None and insert: pass elif hasattr(key, "key") and callable(key.key): key = key.key() else: assert isinstance(key, grumble.key.Key), "ModelQuery.get requires a valid key object, not a %s" % type(key) q = ModelQuery().set_key(key) mm = grumble.schema.ModelManager.for_name(key.kind()) r = mm.getModelQueryRenderer(q) r.execute(grumble.dbadapter.QueryType.Insert if insert else grumble.dbadapter.QueryType.Update, values)