# Path: psqlextra/expressions.py
# class HStoreValue(expressions.Expression):
#     """Represents a HStore value.
# 
#     The base PostgreSQL implementation Django provides, always
#     represents HStore values as dictionaries, but this doesn't work if
#     you want to use expressions inside hstore values.
#     """
# 
#     def __init__(self, value):
#         """Initializes a new instance."""
# 
#         self.value = value
# 
#     def resolve_expression(self, *args, **kwargs):
#         """Resolves expressions inside the dictionary."""
# 
#         result = dict()
#         for key, value in self.value.items():
#             if hasattr(value, "resolve_expression"):
#                 result[key] = value.resolve_expression(*args, **kwargs)
#             else:
#                 result[key] = value
# 
#         return HStoreValue(result)
# 
#     def as_sql(self, compiler, connection):
#         """Compiles the HStore value into SQL.
# 
#         Compiles expressions contained in the values
#         of HStore entries as well.
# 
#         Given a dictionary like:
# 
#             dict(key1='val1', key2='val2')
# 
#         The resulting SQL will be:
# 
#             hstore(hstore('key1', 'val1'), hstore('key2', 'val2'))
#         """
# 
#         sql = []
#         params = []
# 
#         for key, value in self.value.items():
#             if hasattr(value, "as_sql"):
#                 inner_sql, inner_params = value.as_sql(compiler, connection)
#                 sql.append(f"hstore(%s, {inner_sql})")
#                 params.append(key)
#                 params.extend(inner_params)
#             elif value is not None:
#                 sql.append("hstore(%s, %s)")
#                 params.append(key)
#                 params.append(str(value))
#             else:
#                 sql.append("hstore(%s, NULL)")
#                 params.append(key)
# 
#         return " || ".join(sql), params
# 
# Path: psqlextra/types.py
# class ConflictAction(Enum):
#     """Possible actions to take on a conflict."""
# 
#     NOTHING = "NOTHING"
#     UPDATE = "UPDATE"
# 
#     @classmethod
#     def all(cls) -> List["ConflictAction"]:
#         return [choice for choice in cls]


from collections.abc import Iterable
from typing import Tuple, Union
from django.core.exceptions import SuspiciousOperation
from django.db.models import Expression, Model, Q
from django.db.models.fields.related import RelatedField
from django.db.models.sql.compiler import SQLInsertCompiler, SQLUpdateCompiler
from django.db.utils import ProgrammingError
from .expressions import HStoreValue
from .types import ConflictAction
import django

        if isinstance(expression, Q):
            if django.VERSION < (3, 1):
                raise SuspiciousOperation(
                    "Q objects in psqlextra can only be used with Django 3.1 and newer"
                )

            return self.query.build_where(expression).as_sql(
                self, self.connection
            )

        elif isinstance(expression, Expression):
            return self.compile(expression)

        return expression, tuple()

    def _assert_valid_field(self, field_name: str):
        """Asserts that a field with the specified name exists on the model and
        raises :see:SuspiciousOperation if it does not."""

        field_name = self._normalize_field_name(field_name)
        if self._get_model_field(field_name):
            return

        raise SuspiciousOperation(
            (
                "%s is not a valid conflict target, specify "
                "a list of column names, or tuples with column "
                "names and hstore key."
            )

# Path: psqlextra/expressions.py
# class HStoreValue(expressions.Expression):
#     """Represents a HStore value.
# 
#     The base PostgreSQL implementation Django provides, always
#     represents HStore values as dictionaries, but this doesn't work if
#     you want to use expressions inside hstore values.
#     """
# 
#     def __init__(self, value):
#         """Initializes a new instance."""
# 
#         self.value = value
# 
#     def resolve_expression(self, *args, **kwargs):
#         """Resolves expressions inside the dictionary."""
# 
#         result = dict()
#         for key, value in self.value.items():
#             if hasattr(value, "resolve_expression"):
#                 result[key] = value.resolve_expression(*args, **kwargs)
#             else:
#                 result[key] = value
# 
#         return HStoreValue(result)
# 
#     def as_sql(self, compiler, connection):
#         """Compiles the HStore value into SQL.
# 
#         Compiles expressions contained in the values
#         of HStore entries as well.
# 
#         Given a dictionary like:
# 
#             dict(key1='val1', key2='val2')
# 
#         The resulting SQL will be:
# 
#             hstore(hstore('key1', 'val1'), hstore('key2', 'val2'))
#         """
# 
#         sql = []
#         params = []
# 
#         for key, value in self.value.items():
#             if hasattr(value, "as_sql"):
#                 inner_sql, inner_params = value.as_sql(compiler, connection)
#                 sql.append(f"hstore(%s, {inner_sql})")
#                 params.append(key)
#                 params.extend(inner_params)
#             elif value is not None:
#                 sql.append("hstore(%s, %s)")
#                 params.append(key)
#                 params.append(str(value))
#             else:
#                 sql.append("hstore(%s, NULL)")
#                 params.append(key)
# 
#         return " || ".join(sql), params
# 
# Path: psqlextra/types.py
# class ConflictAction(Enum):
#     """Possible actions to take on a conflict."""
# 
#     NOTHING = "NOTHING"
#     UPDATE = "UPDATE"
# 
#     @classmethod
#     def all(cls) -> List["ConflictAction"]:
#         return [choice for choice in cls]


# Path: psqlextra/compiler.py
from collections.abc import Iterable
from typing import Tuple, Union
from django.core.exceptions import SuspiciousOperation
from django.db.models import Expression, Model, Q
from django.db.models.fields.related import RelatedField
from django.db.models.sql.compiler import SQLInsertCompiler, SQLUpdateCompiler
from django.db.utils import ProgrammingError
from .expressions import HStoreValue
from .types import ConflictAction
import django


        if isinstance(expression, Q):
            if django.VERSION < (3, 1):
                raise SuspiciousOperation(
                    "Q objects in psqlextra can only be used with Django 3.1 and newer"
                )

            return self.query.build_where(expression).as_sql(
                self, self.connection
            )

        elif isinstance(expression, Expression):
            return self.compile(expression)

        return expression, tuple()

    def _assert_valid_field(self, field_name: str):
        """Asserts that a field with the specified name exists on the model and
        raises :see:SuspiciousOperation if it does not."""

        field_name = self._normalize_field_name(field_name)
        if self._get_model_field(field_name):
            return

        raise SuspiciousOperation(
            (
                "%s is not a valid conflict target, specify "
                "a list of column names, or tuples with column "
                "names and hstore key."
            )

            % str(field_name)