# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import json
import logging
import os
import time
import warnings
from collections import defaultdict
from datetime import datetime
from itertools import chain, repeat
from threading import RLock

import psycopg
from psycopg import Binary, ClientCursor, Cursor
from psycopg import DataError as DatabaseDataError
from psycopg import IntegrityError as DatabaseIntegrityError
from psycopg import IsolationLevel
from psycopg import OperationalError as DatabaseOperationalError
from psycopg import connect
from psycopg.errors import QueryCanceled as DatabaseTimeoutError
from psycopg.sql import SQL, Identifier
from psycopg_pool import ConnectionPool
from sql import Cast, Flavor, For, Literal, Table
from sql.aggregate import Count
from sql.conditionals import Coalesce
from sql.functions import Function
from sql.operators import Any, BinaryOperator, Concat, NotEqual

from trytond import __series__, config
from trytond.backend.database import DatabaseInterface, SQLType
from trytond.sql.operators import RangeOperator

from .table import index_method

__all__ = [
    'Database',
    'DatabaseIntegrityError', 'DatabaseDataError', 'DatabaseOperationalError',
    'DatabaseTimeoutError']

logger = logging.getLogger(__name__)

os.environ['PGTZ'] = os.environ.get('TZ', '')
_default_name = config.get('database', 'default_name', default='template1')


def unescape_quote(s):
    if s.startswith('"') and s.endswith('"'):
        return s.strip('"').replace('""', '"')
    return s


def replace_special_values(s, **mapping):
    for name, value in mapping.items():
        s = s.replace('$' + name, value)
    return s


class LoggingCursor(Cursor):

    def execute(self, query, params=None, *, prepare=None, binary=None):
        if logger.isEnabledFor(logging.DEBUG):
            logger.debug(ClientCursor(self.connection).mogrify(query, params))
        return super().execute(query, params, prepare=prepare, binary=binary)


class ForSkipLocked(For):
    def __str__(self):
        assert not self.nowait, "Can not use both NO WAIT and SKIP LOCKED"
        return super().__str__() + (' SKIP LOCKED' if not self.nowait else '')


class Unaccent(Function):
    __slots__ = ()
    _function = config.get('database', 'unaccent_function', default='unaccent')


class Similarity(Function):
    __slots__ = ()
    _function = config.get(
        'database', 'similarity_function', default='similarity')


class Match(BinaryOperator):
    __slots__ = ()
    _operator = '@@'


class ToTsvector(Function):
    __slots__ = ()
    _function = 'to_tsvector'


class Setweight(Function):
    __slots__ = ()
    _function = 'setweight'


class TsQuery(Function):
    __slots__ = ()


class ToTsQuery(TsQuery):
    __slots__ = ()
    _function = 'to_tsquery'


class PlainToTsQuery(TsQuery):
    __slots__ = ()
    _function = 'plainto_tsquery'


class PhraseToTsQuery(TsQuery):
    __slots__ = ()
    _function = 'phraseto_tsquery'


class WebsearchToTsQuery(TsQuery):
    __slots__ = ()
    _function = 'websearch_to_tsquery'


class TsRank(Function):
    __slots__ = ()
    _function = 'ts_rank'


class AdvisoryLock(Function):
    _function = 'pg_advisory_xact_lock'


class TryAdvisoryLock(Function):
    _function = 'pg_try_advisory_xact_lock'


class JSONBExtractPath(Function):
    __slots__ = ()
    _function = 'jsonb_extract_path'


class JSONKeyExists(BinaryOperator):
    __slots__ = ()
    _operator = '?'


class _BinaryOperatorArray(BinaryOperator):
    "Binary Operator that convert list into Array"

    @property
    def _operands(self):
        if isinstance(self.right, list):
            return (self.left, None)
        return super()._operands

    @property
    def params(self):
        params = super().params
        if isinstance(self.right, list):
            params = params[:-1] + (self.right,)
        return params


class JSONAnyKeyExist(_BinaryOperatorArray):
    __slots__ = ()
    _operator = '?|'


class JSONAllKeyExist(_BinaryOperatorArray):
    __slots__ = ()
    _operator = '?&'


class JSONContains(BinaryOperator):
    __slots__ = ()
    _operator = '@>'


class Database(DatabaseInterface):

    index_translators = []
    _lock = RLock()
    _clean_last = None
    _databases = defaultdict(dict)
    _connpool = None
    _list_cache = {}
    _list_cache_timestamp = {}
    _search_path = None
    _current_user = None
    _has_returning = None
    _has_insert_on_conflict = None
    _has_select_for_skip_locked = None
    _has_proc = defaultdict(lambda: defaultdict(dict))
    _extensions = defaultdict(dict)
    _search_full_text_languages = defaultdict(dict)
    flavor = Flavor(ilike=True, filter_=True)

    TYPES_MAPPING = {
        'SMALLINT': SQLType('INT2', 'INT2'),
        'BIGINT': SQLType('INT8', 'INT8'),
        'BLOB': SQLType('BYTEA', 'BYTEA'),
        'DATETIME': SQLType('TIMESTAMP', 'TIMESTAMP(0)'),
        'REAL': SQLType('FLOAT4', 'FLOAT4'),
        'FLOAT': SQLType('FLOAT8', 'FLOAT8'),
        'FULLTEXT': SQLType('TSVECTOR', 'TSVECTOR'),
        'INTEGER': SQLType('INT4', 'INT4'),
        'JSON': SQLType('JSONB', 'JSONB'),
        'TIMESTAMP': SQLType('TIMESTAMP', 'TIMESTAMP(6)'),
        }

    def __new__(cls, name=_default_name):
        now = datetime.now()
        if cls._clean_last is None:
            cls._clean_last = now

        databases = cls._databases[os.getpid()]

        minconn = config.getint('database', 'minconn', default=1)
        maxconn = config.getint('database', 'maxconn', default=64)
        timeout = config.getint('database', 'timeout')
        last_clean = (now - cls._clean_last).total_seconds()
        if last_clean > timeout:
            for database in list(databases.values()):
                if ((now - database._last_use).total_seconds() > timeout
                        and database.name != name):
                    database._connpool.resize(0, 1)

        if not (inst := databases.get(name)):
            with cls._lock:
                if not (inst := databases.get(name)):
                    inst = DatabaseInterface.__new__(cls, name=name)
                    kwargs = cls._connection_params(name)
                    kwargs['cursor_factory'] = LoggingCursor
                    conninfo = kwargs.pop('conninfo')
                    try:
                        inst._connpool = ConnectionPool(
                            conninfo,
                            kwargs=kwargs,
                            open=True,
                            check=ConnectionPool.check_connection,
                            min_size=minconn, max_size=maxconn)
                    except Exception:
                        logger.error(
                            'connection to "%s" failed', name, exc_info=True)
                        raise
                    else:
                        logger.info('connection to "%s" succeeded', name)
                    databases[name] = inst
        elif (inst._connpool.min_size != minconn
                or inst._connpool.max_size != maxconn):
            inst._connpool.resize(minconn, maxconn)
        inst._last_use = now
        return inst

    def __init__(self, name=_default_name):
        super().__init__(name)

    @classmethod
    def _connection_params(cls, name):
        uri = config.parse_uri(config.get('database', 'uri'))
        if uri.path and uri.path != '/':
            warnings.warn("The path specified in the URI will be overridden")
        return {
            'conninfo': uri._replace(
                scheme='postgresql', path='/' + name).geturl(),
            'fallback_application_name': os.environ.get(
                'TRYTOND_APPNAME', 'trytond'),
            }

    def connect(self):
        return self

    def get_connection(
            self, autocommit=False, readonly=False, statement_timeout=None):
        conn = self._connpool.getconn()
        conn.isolation_level = IsolationLevel.REPEATABLE_READ
        conn.read_only = readonly
        conn.autocommit = autocommit
        if statement_timeout:
            with conn.cursor() as cur:
                cur.execute('SET statement_timeout=%s' %
                    (statement_timeout * 1000))
        return conn

    def put_connection(self, connection):
        self._connpool.putconn(connection)

    def close(self):
        with self._lock:
            logger.info('disconnection from "%s"', self.name)
            self._connpool.close(None)
            self._databases[os.getpid()].pop(self.name)

    @classmethod
    def create(cls, connection, database_name, template='template0'):
        cursor = connection.cursor()
        cursor.execute(
            SQL(
                "CREATE DATABASE {} TEMPLATE {} ENCODING 'unicode'")
            .format(
                Identifier(database_name),
                Identifier(template)))
        connection.commit()
        cls._list_cache.clear()

    @classmethod
    def drop(cls, connection, database_name):
        cursor = connection.cursor()
        cursor.execute(SQL("DROP DATABASE {}")
            .format(Identifier(database_name)))
        cls.clear_cache()

    @classmethod
    def clear_cache(cls):
        cls._list_cache.clear()
        cls._list_cache_timestamp.clear()
        cls._search_path = None
        cls._current_user = None
        cls._has_returning = None
        cls._has_insert_on_conflict = None
        cls._has_select_for_skip_locked = None
        cls._has_proc.clear()
        cls._extensions.clear()
        cls._search_full_text_languages.clear()

    def get_version(self, connection):
        version = connection.info.server_version
        major, rest = divmod(int(version), 10000)
        minor, patch = divmod(rest, 100)
        return (major, minor, patch)

    def list(self, hostname=None):
        now = time.time()
        timeout = config.getint('session', 'timeout')
        res = self.__class__._list_cache.get(hostname)
        timestamp = self.__class__._list_cache_timestamp.get(hostname, now)
        if res and abs(timestamp - now) < timeout:
            return res

        connection = self.get_connection()
        try:
            with connection.cursor() as cursor:
                cursor.execute('SELECT datname FROM pg_database '
                    'WHERE datistemplate = false ORDER BY datname')
                res = []
                for db_name, in cursor:
                    params = self._connection_params(db_name)
                    try:
                        conn = connect(params['conninfo'])
                        try:
                            with conn:
                                if self._test(conn, hostname=hostname):
                                    res.append(db_name)
                        finally:
                            conn.close()
                    except Exception:
                        logger.debug(
                            'Test failed for "%s"', db_name, exc_info=True)
                        continue
        finally:
            connection.rollback()
            self.put_connection(connection)

        self.__class__._list_cache[hostname] = res
        self.__class__._list_cache_timestamp[hostname] = now
        return res

    def init(self):
        from trytond.modules import get_module_info

        connection = self.get_connection()
        try:
            with connection.cursor() as cursor:
                sql_file = os.path.join(os.path.dirname(__file__), 'init.sql')
                with open(sql_file) as fp:
                    for line in fp.read().split(';'):
                        if line and not line.isspace():
                            cursor.execute(line)

                for module in ['ir', 'res']:
                    info = get_module_info(module)
                    cursor.execute('INSERT INTO ir_module '
                        '(create_uid, create_date, name, state) '
                        'VALUES (%s, now(), %s, %s) '
                        'RETURNING id',
                        (0, module, 'to activate'))
                    module_id = cursor.fetchone()[0]
                    for dependency in info.get('depends', []):
                        cursor.execute('INSERT INTO ir_module_dependency '
                            '(create_uid, create_date, module, name) '
                            'VALUES (%s, now(), %s, %s)',
                            (0, module_id, dependency))

            connection.commit()
        finally:
            self.put_connection(connection)

    def test(self, hostname=None, series=False):
        try:
            connection = self.get_connection()
        except Exception:
            logger.debug('Test failed for "%s"', self.name, exc_info=True)
            return False
        try:
            return self._test(connection, hostname=hostname, series=series)
        finally:
            connection.rollback()
            self.put_connection(connection)

    @classmethod
    def _test(cls, connection, hostname=None, series=False):
        with connection.cursor() as cursor:
            cursor.execute(
                'SELECT table_name FROM information_schema.tables '
                'WHERE table_name = %s', ('ir_configuration',))
            if not cursor.rowcount:
                return False
            if series:
                cursor.execute('SELECT series FROM ir_configuration')
                config_series = {s for s, in cursor if s}
                if config_series and __series__ not in config_series:
                    return False
            if hostname:
                cursor.execute(
                    'SELECT hostname FROM ir_configuration')
                hostnames = {h for h, in cursor if h}
                if hostnames and hostname not in hostnames:
                    return False
            return True

    def nextid(self, connection, table, count=1):
        column = 'id' if not table.endswith('__history') else '__id'
        cursor = connection.cursor()
        cursor.execute(
            "SELECT nextval(pg_get_serial_sequence(format(%s, %s::text), %s)) "
            "FROM generate_series(1, %s)",
            ('%I', table, column, count))
        if count == 1:
            return cursor.fetchone()[0]
        else:
            return [id for id, in cursor]

    def setnextid(self, connection, table, value):
        if self.currid(connection, table) >= value:
            return
        column = 'id' if not table.endswith('__history') else '__id'
        cursor = connection.cursor()
        cursor.execute(
            "SELECT setval("
            "pg_get_serial_sequence(format(%s, %s::text), %s), %s)",
            ('%I', table, column, value))

    def currid(self, connection, table):
        column = 'id' if not table.endswith('__history') else '__id'
        cursor = connection.cursor()
        cursor.execute(
            "SELECT pg_get_serial_sequence(format(%s, %s::text), %s)",
            ('%I', table, column))
        sequence_name, = cursor.fetchone()
        cursor.execute(f"SELECT last_value FROM {sequence_name}")
        return cursor.fetchone()[0]

    def estimated_count(self, connection, from_item):
        cursor = connection.cursor()
        if isinstance(from_item, Table):
            cursor.execute(
                'SELECT n_live_tup FROM pg_stat_all_tables '
                'WHERE relname = %s',
                (from_item._name,))
        else:
            cursor.execute(*from_item.select(Count()))
        return cursor.fetchone()[0]

    def notify(self, connection, channel, payload):
        cursor = connection.cursor()
        cursor.execute('SELECT pg_notify(%s, %s)', (channel, payload))

    def get_notifications(self, connection):
        return list(connection.notifies(timeout=2))

    def lock(self, connection, table):
        cursor = connection.cursor()
        cursor.execute(SQL('LOCK {} IN EXCLUSIVE MODE NOWAIT').format(
                Identifier(table)))

    @classmethod
    def lock_records(cls, connection, table, ids):
        table = Table(table)
        cursor = connection.cursor()
        query = table.select(
            Literal(1),
            where=table.id == Any(list(ids)),
            for_=For('UPDATE', nowait=True))
        cursor.execute(*query)

    def lock_id(self, id, timeout=None):
        if not timeout:
            return TryAdvisoryLock(id)
        else:
            return AdvisoryLock(id)

    def has_constraint(self, constraint):
        from trytond.model.modelsql import Exclude
        if isinstance(constraint, Exclude):
            if (index_method(constraint) == 'GIST'
                    and any(not issubclass(o, (RangeOperator, NotEqual))
                        for o in constraint.operators)
                    and not self.has_extension('btree_gist')):
                return False
        return True

    def has_range(self):
        return True

    def has_multirow_insert(self):
        return True

    def get_table_schema(self, connection, table_name):
        cursor = connection.cursor()
        for schema in self.search_path:
            cursor.execute('SELECT 1 '
                'FROM information_schema.tables '
                'WHERE table_name = %s AND table_schema = %s',
                (table_name, schema))
            if cursor.rowcount:
                return schema

    def get_view_schema(self, connection, view_name):
        cursor = connection.cursor()
        for schema in self.search_path:
            cursor.execute('SELECT 1 '
                'FROM pg_matviews '
                'WHERE matviewname = %s AND schemaname = %s',
                (view_name, schema))
            if cursor.rowcount:
                return schema

    @property
    def current_user(self):
        if self._current_user is None:
            connection = self.get_connection()
            try:
                with connection.cursor() as cursor:
                    cursor.execute('SELECT current_user')
                    self._current_user = cursor.fetchone()[0]
                    return self._current_user
            finally:
                connection.rollback()
                self.put_connection(connection)
        return self._current_user

    @property
    def search_path(self):
        if self._search_path is None:
            connection = self.get_connection()
            try:
                with connection.cursor() as cursor:
                    cursor.execute('SHOW search_path')
                    path, = cursor.fetchone()
                    special_values = {
                        'user': self.current_user,
                    }
                    self._search_path = [
                        unescape_quote(replace_special_values(
                                p.strip(), **special_values))
                        for p in path.split(',')]
            finally:
                connection.rollback()
                self.put_connection(connection)
        return self._search_path

    def has_returning(self):
        if self._has_returning is None:
            connection = self.get_connection()
            try:
                # RETURNING clause is available since PostgreSQL 8.2
                self._has_returning = self.get_version(connection) >= (8, 2)
            finally:
                connection.rollback()
                self.put_connection(connection)
        return self._has_returning

    def has_select_for(self):
        return True

    def has_insert_on_conflict(self):
        if self._has_insert_on_conflict is None:
            connection = self.get_connection()
            try:
                self._has_insert_on_conflict = (
                    self.get_version(connection) >= (9, 5))
            finally:
                connection.rollback()
                self.put_connection(connection)
        return self._has_insert_on_conflict

    def get_select_for_skip_locked(self):
        if self._has_select_for_skip_locked is None:
            connection = self.get_connection()
            try:
                # SKIP LOCKED clause is available since PostgreSQL 9.5
                self._has_select_for_skip_locked = (
                    self.get_version(connection) >= (9, 5))
            finally:
                connection.rollback()
                self.put_connection(connection)
        if self._has_select_for_skip_locked:
            return ForSkipLocked
        else:
            return For

    @classmethod
    def has_sequence(cls):
        return True

    def has_proc(self, name, property='oid'):
        if (name in self._has_proc[self.name]
                and property in self._has_proc[self.name][name]):
            return self._has_proc[self.name][name][property]
        connection = self.get_connection()
        result = False
        try:
            with connection.cursor() as cursor:
                cursor.execute(
                    SQL('SELECT {} FROM pg_proc WHERE proname=%s').format(
                        Identifier(property)), (name,))
                result = cursor.fetchone()
                if result:
                    result, = result
        finally:
            connection.rollback()
            self.put_connection(connection)
        self._has_proc[self.name][name][property] = result
        return result

    def has_unaccent(self):
        return self.has_proc(Unaccent._function)

    def has_unaccent_indexable(self):
        return self.has_proc(Unaccent._function, 'provolatile') == 'i'

    def has_similarity(self):
        return self.has_proc(Similarity._function)

    def similarity(self, column, value):
        return Similarity(column, value)

    def has_search_full_text(self):
        return True

    def _search_full_text_language(self, language):
        languages = self._search_full_text_languages[self.name]
        if language not in languages:
            lang = Table('ir_lang')
            connection = self.get_connection()
            try:
                with connection.cursor() as cursor:
                    cursor.execute(*lang.select(
                            Coalesce(lang.pg_text_search, 'simple'),
                            where=lang.code == language,
                            limit=1))
                    config_name, = cursor.fetchone()
            finally:
                connection.rollback()
                self.put_connection(connection)
            languages[language] = config_name
        else:
            config_name = languages[language]
        return config_name

    def format_full_text(self, *documents, language=None):
        size = max(len(documents) // 4, 1)
        if len(documents) > 1:
            weights = chain(
                ['A'] * size, ['B'] * size, ['C'] * size, repeat('D'))
        else:
            weights = [None]
        expression = None
        if language:
            config_name = self._search_full_text_language(language)
        else:
            config_name = None
        for document, weight in zip(documents, weights):
            if not document:
                continue
            if config_name:
                ts_vector = ToTsvector(config_name, document)
            else:
                ts_vector = ToTsvector('simple', document)
            if weight:
                ts_vector = Setweight(ts_vector, weight)
            if expression is None:
                expression = ts_vector
            else:
                expression = Concat(expression, ts_vector)
        return expression

    def format_full_text_query(self, query, language=None):
        connection = self.get_connection()
        try:
            version = self.get_version(connection)
        finally:
            connection.rollback()
            self.put_connection(connection)
        if not isinstance(query, TsQuery):
            if version >= (11, 0):
                ToTsQuery = WebsearchToTsQuery
            else:
                ToTsQuery = PlainToTsQuery
            if language:
                config_name = self._search_full_text_language(language)
            else:
                config_name = 'simple'
            query = ToTsQuery(config_name, query)
        return query

    def search_full_text(self, document, query):
        return Match(document, query)

    def rank_full_text(self, document, query, normalize=None):
        # TODO: weights and cover density
        norm_int = 0
        if normalize:
            values = {
                'document log': 1,
                'document': 2,
                'mean': 4,
                'word': 8,
                'word log': 16,
                'rank': 32,
                }
            for norm in normalize:
                norm_int |= values.get(norm, 0)
        return TsRank(document, query, norm_int)

    def sql_type(self, type_):
        if type_ in self.TYPES_MAPPING:
            return self.TYPES_MAPPING[type_]
        if type_.startswith('VARCHAR'):
            return SQLType('VARCHAR', type_)
        return SQLType(type_, type_)

    def sql_format(self, type_, value):
        if type_ == 'BLOB':
            if value is not None:
                return Binary(value)
        return value

    def unaccent(self, value):
        if self.has_unaccent():
            return Unaccent(value)
        return value

    def sequence_exist(self, connection, name):
        cursor = connection.cursor()
        for schema in self.search_path:
            cursor.execute('SELECT 1 '
                'FROM information_schema.sequences '
                'WHERE sequence_name = %s AND sequence_schema = %s',
                (name, schema))
            if cursor.rowcount:
                return True
        return False

    def sequence_create(
            self, connection, name, number_increment=1, start_value=1):
        cursor = ClientCursor(connection)

        cursor.execute(
            SQL("CREATE SEQUENCE {} INCREMENT BY %s START WITH %s").format(
                Identifier(name)),
            (number_increment, start_value))

    def sequence_update(
            self, connection, name, number_increment=1, start_value=1):
        cursor = ClientCursor(connection)
        cursor.execute(
            SQL("ALTER SEQUENCE {} INCREMENT BY %s RESTART WITH %s").format(
                Identifier(name)),
            (number_increment, start_value))

    def sequence_rename(self, connection, old_name, new_name):
        cursor = ClientCursor(connection)
        if (self.sequence_exist(connection, old_name)
                and not self.sequence_exist(connection, new_name)):
            cursor.execute(
                SQL("ALTER TABLE {} RENAME TO {}").format(
                    Identifier(old_name),
                    Identifier(new_name)))

    def sequence_delete(self, connection, name):
        cursor = connection.cursor()
        cursor.execute(SQL("DROP SEQUENCE {}").format(Identifier(name)))

    def sequence_nextval(self, connection, name):
        cursor = ClientCursor(connection)
        cursor.execute('SELECT NEXTVAL(%s)', (name,))
        return cursor.fetchone()[0]

    def sequence_nextvals(self, connection, name, n):
        cursor = ClientCursor(connection)
        cursor.execute(
            'SELECT NEXTVAL(%s) FROM generate_series(1, %s)', (name, n))
        for val, in cursor:
            yield val

    def sequence_next_number(self, connection, name):
        cursor = connection.cursor()
        version = self.get_version(connection)
        if version >= (10, 0):
            cursor.execute(
                'SELECT increment_by '
                'FROM pg_sequences '
                'WHERE sequencename=%s',
                (name,))
            increment, = cursor.fetchone()
            cursor.execute(
                SQL(
                    'SELECT CASE WHEN NOT is_called THEN last_value '
                    'ELSE last_value + %s '
                    'END '
                    'FROM {}').format(Identifier(name)),
                (increment,))
        else:
            cursor.execute(
                SQL(
                    'SELECT CASE WHEN NOT is_called THEN last_value '
                    'ELSE last_value + increment_by '
                    'END '
                    'FROM {}').format(Identifier(name)))
        return cursor.fetchone()[0]

    def has_channel(self):
        return True

    def has_extension(self, extension_name):
        if extension_name in self._extensions[self.name]:
            return self._extensions[self.name][extension_name]

        connection = self.get_connection()
        try:
            result = False
            with connection.cursor() as cursor:
                cursor = connection.cursor()
                cursor.execute(
                    "SELECT 1 FROM pg_extension WHERE extname=%s",
                    (extension_name,))
                result = bool(cursor.rowcount)
        finally:
            connection.rollback()
            self.put_connection(connection)
        self._extensions[self.name][extension_name] = result
        return result

    def json_get(self, column, key=None):
        column = Cast(column, 'jsonb')
        if key:
            column = JSONBExtractPath(column, key)
        return column

    def json_key_exists(self, column, key):
        return JSONKeyExists(Cast(column, 'jsonb'), key)

    def json_any_keys_exist(self, column, keys):
        return JSONAnyKeyExist(Cast(column, 'jsonb'), keys)

    def json_all_keys_exist(self, column, keys):
        return JSONAllKeyExist(Cast(column, 'jsonb'), keys)

    def json_contains(self, column, json):
        return JSONContains(Cast(column, 'jsonb'), Cast(json, 'jsonb'))

    @classmethod
    def has_materialized_views(cls):
        return True

    def create_materialized_view(self, connection, view_name, query):
        cursor = connection.cursor()
        cursor.execute(
            SQL(
                "CREATE MATERIALIZED VIEW IF NOT EXISTS {} AS {} "
                "WITH NO DATA"
                ).format(Identifier(view_name), SQL(str(query))),
            query.params)
        cursor.execute(
            SQL("CREATE UNIQUE INDEX ON {}(id)").format(Identifier(view_name)))

    def drop_materialized_view(self, connection, view_name):
        cursor = connection.cursor()
        cursor.execute(
            SQL("DROP MATERIALIZED VIEW {}").format(Identifier(view_name)))

    def refresh_materialized_view(
            self, connection, view_name, concurrently=True):
        cursor = connection.cursor()
        cursor.execute(
            SQL("REFRESH MATERIALIZED VIEW {} {}")
            .format(
                SQL('CONCURRENTLY' if concurrently else ''),
                Identifier(view_name)
                ))

    @classmethod
    def has_array(cls):
        return True


def convert_json(value):
    from trytond.protocols.jsonrpc import JSONDecoder
    return json.loads(value, object_hook=JSONDecoder())


psycopg.types.json.set_json_loads(convert_json)
