示例#1
0
 def get_sql_values(self) -> t.List[t.Any]:
     """
     Convert any Enums into values, and serialise any JSON.
     """
     return [
         convert_to_sql_value(value=value, column=column)
         for column, value in self._values.items()
     ]
示例#2
0
    def test_convert_json(self):
        """
        Make sure Python objects are serialised correctly to JSON strings.
        """
        self.assertEqual(
            convert_to_sql_value(value={"a": 123}, column=JSON()).replace(
                " ", ""
            ),
            '{"a":123}',
        )

        self.assertEqual(
            convert_to_sql_value(value={"a": 123}, column=JSONB()).replace(
                " ", ""
            ),
            '{"a":123}',
        )
示例#3
0
 def test_other(self):
     """
     Make sure simple Python values are returned correctly.
     """
     self.assertEqual(
         convert_to_sql_value(value=1, column=Integer()),
         1,
     )
示例#4
0
    def test_convert_enum(self):
        """
        Make sure Enum instances are converted to their values.
        """

        class Colour(Enum):
            red = "red"

        self.assertEqual(
            convert_to_sql_value(value=Colour.red, column=Varchar()), "red"
        )
示例#5
0
    def test_convert_table_instance(self):
        """
        Make sure Table instances are converted to integers.
        """

        class MyTable(Table):
            pass

        instance = MyTable(id=1)

        self.assertEqual(
            convert_to_sql_value(
                value=instance, column=MyTable._meta.primary_key
            ),
            1,
        )
示例#6
0
    def querystring(self) -> QueryString:
        """
        Used when inserting rows.
        """
        args_dict = {}
        for col in self._meta.columns:
            column_name = col._meta.name
            value = convert_to_sql_value(value=self[column_name], column=col)
            args_dict[column_name] = value

        def is_unquoted(arg):
            return type(arg) == Unquoted

        # Strip out any args which are unquoted.
        filtered_args = [i for i in args_dict.values() if not is_unquoted(i)]

        # If unquoted, dump it straight into the query.
        query = ",".join([
            args_dict[column._meta.name].value
            if is_unquoted(args_dict[column._meta.name]) else "{}"
            for column in self._meta.columns
        ])
        return QueryString(f"({query})", *filtered_args)
示例#7
0
    def clean_value(self, value: t.Any) -> t.Any:
        """
        If a where clause contains a ``Table`` instance, we should convert that
        to a column reference. For example:

        .. code-block:: python

            manager = await Manager.objects.where(
                Manager.name == 'Guido'
            ).first()

            # The where clause should be:
            await Band.select().where(Band.manager.id == guido.id)
            # Or
            await Band.select().where(Band.manager == guido.id)

            # If the object is passed in, i.e. `guido` instead of `guido.id`,
            # it should still work.
            await Band.select().where(Band.manager == guido)

        Also, convert Enums to their underlying values, and serialise any JSON.

        """
        return convert_to_sql_value(value=value, column=self.column)