def bin(leftExpr, binaryOp, rightExpr): c = Context(namespace=Names.Expression.BinaryOperator) res1, res2 = convert(leftExpr, rightExpr) c.varBinaryLeft = str(res1) c.varBinaryRight = str(res2) c.varBinaryOperator = binaryOp return c
def sql(self): context = Context(namespace=Names.Update) context.varTableName = self.model.Name def gene(): nonlocal context assert len(self._fields) if self._where: context.varWhereExpr = str(self._where) for field, expr in self._fields: c = Context(namespace=Names.Update.ColumnsLevel) c.varColumnName = field.Name c.varColumnUpdateExpr = expr context.level(c) return self.builder.build(context) if self._instances: for model_inst in self._instances: if not self.only_fields: self._fields = list(zip(self.model, model_inst.storage_values())) else: self._fields = [(x, model_inst.storage_value(x)) for x in self.only_fields] value = model_inst.value(self.model.PrimaryKey()) assert value != None, "Не указано значение первичного ключа для обновления!" self._where = self.model.PrimaryKey() == value yield gene() else: yield gene()
def test_context(self): a = Context(namespace=Names.Create) a.flagTemp = True a.varDatabaseName = "test" def fail_test(): nonlocal a a.varDatabaseName = 123 self.assertRaises(ValueError, fail_test)
def field(self, model, field): c = Context(namespace=Names.Create.ColumnsLevel) c.varColumnName = field.Name c.varColumnType = field.StorageType for feature in field.features: feature.createHook(self, field, c) return c
def gene(): nonlocal context assert len(self._fields) if self._where: context.varWhereExpr = str(self._where) for field, expr in self._fields: c = Context(namespace=Names.Update.ColumnsLevel) c.varColumnName = field.Name c.varColumnUpdateExpr = expr context.level(c) return self.builder.build(context)
def sql(self): table = self.model.Name context = Context(namespace=Names.Select) context.varSelectTable = table if self._where: context.varWhereExpr = str(self._where) if self._fields: names = [f.name for f in self._fields] for name in names: c = Context(namespace=Names.Select.ColumnsLevel) c.varTableName = table c.varColumnName = name context.level(c) return self.builder.build(context)
def like(leftExpr, rightExpr, reverse=False, match=False): c = Context(namespace=Names.Expression.Like) c.varLikeLeft = str(leftExpr) c.varLikeRight = str(rightExpr) if reverse: c.flagReverse = True if not match: c.flagLike = True else: c.flagMatch = True return c
def sql(self): context = Context(namespace=Names.Insert) context.varTableName = self.model.Name for instance in self.models: for name, value in instance.storage_items(): c = Context(namespace=Names.Insert.ValuesLevel) c.varValue = value field = instance[name] for feature in field.features: feature.insertHook(self, instance, field , c) context.level(c) yield self.builder.build(context)
def sql(self): context = Context(namespace=Names.Create) context.varTableName = self.model.Name for field in self.model: context.level(self.field(self.model, field)) return self.builder.build(context)
def sql(self): context = Context(namespace=Names.Delete) assert self._where context.varTableName = self.model.Name context.varWhereExpr = str(self._where) return self.builder.build(context)