rql_schema_holder.py 5.13 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
# copyright 2013-2022 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# contact http://www.logilab.fr -- mailto:contact@logilab.fr
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import hashlib

from cubicweb.utils import json_dumps


class RqlIOSchemaHolder(object):
    """Class used to load the schema and its hash.
Noé Gaumont's avatar
Noé Gaumont committed
25
26
27
    Both can be fetched by the client via dedicated views,
    so it can decide to update the locally stored schema if
    the integrity is not valid.
28
    """
Noé Gaumont's avatar
Noé Gaumont committed
29

30
31
    _schema = None
    _hash = None
32
33

    @staticmethod
Noé Gaumont's avatar
Noé Gaumont committed
34
35
36
    def format_schema(
        typeNames, specializes, attributeNames, relationsTo, relationsFrom
    ):
37
38
39
40
41
        return {
            "eid": typeNames[0][0],
            "name": typeNames[0][1],
            "modificationDate": typeNames[0][2].strftime("%Y/%m/%d %H:%M:%S"),
            "specializes": specializes[0][0] if specializes else None,
Noé Gaumont's avatar
Noé Gaumont committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
            "attributes": [
                {"eid": row[0], "name": row[1], "type": row[2], "cardinality": row[3]}
                for row in attributeNames
            ],
            "relationsTo": [
                {
                    "eid": row[0],
                    "name": row[1],
                    "from": row[2],
                    "fromName": typeNames[0][1],
                    "fromCardinality": row[6],
                    "to": row[3],
                    "toName": row[4],
                    "toCardinality": row[6],
                    "description": row[5],
                }
                for row in relationsTo
            ],
            "relationsFrom": [
                {
                    "eid": row[0],
                    "name": row[1],
                    "from": row[2],
                    "fromName": row[3],
                    "fromCardinality": row[6],
                    "to": row[4],
                    "toName": typeNames[0][1],
                    "toCardinality": row[6],
                    "description": row[5],
                }
                for row in relationsFrom
            ],
74
75
        }

76
77
78
79
80
    @classmethod
    def get_schema(cls, cnx):
        if cls._schema is not None:
            return cls._schema

81
        entities = []
Noé Gaumont's avatar
Noé Gaumont committed
82
        etypes = cnx.execute("Any T WHERE T is CWEType")
83
84
85
86
        for etype in etypes:
            eid = etype[0]
            rql_requests = [
                (
Noé Gaumont's avatar
Noé Gaumont committed
87
88
                    f"Any TYPE,NAME,MD WHERE TYPE eid {eid}, TYPE name NAME, "
                    "TYPE modification_date MD"
89
                ),
Noé Gaumont's avatar
Noé Gaumont committed
90
                (f"Any PARENT WHERE TYPE eid {eid}, TYPE specializes PARENT"),
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
                (
                    f"""Any ATTR,NAME,ATTRTYPENAME,CARD ORDERBY ON WHERE TYPE eid {eid},
                    ATTR from_entity TYPE,
                    ATTR to_entity ATTRTYPE,
                    ATTR relation_type RELTYPE,
                    RELTYPE name NAME,
                    ATTR is CWAttribute,
                    ATTR cardinality CARD?,
                    ATTR ordernum ON,
                    ATTRTYPE name ATTRTYPENAME"""
                ),
                (
                    f"""Any ATTR,NAME,SUBJECT,OBJECT,ONAME,DESC,
                    CARD ORDERBY ON WHERE SUBJECT eid {eid},
                    ATTR from_entity SUBJECT,
                    ATTR to_entity OBJECT,
                    OBJECT name ONAME,
                    ATTR relation_type RELTYPE,
                    RELTYPE name NAME,
                    RELTYPE description DESC,
                    ATTR is CWRelation,
                    ATTR cardinality CARD?,
                    ATTR ordernum ON"""
                ),
                (
                    f"""Any ATTR,NAME,SUBJECT,SNAME,OBJECT,DESC,
                    CARD ORDERBY ON WHERE OBJECT eid {eid},
                    ATTR to_entity OBJECT,
                    ATTR from_entity SUBJECT,
                    SUBJECT name SNAME,
                    ATTR relation_type RELTYPE,
                    RELTYPE name NAME,
                    RELTYPE description DESC,
                    ATTR is CWRelation,
                    ATTR cardinality CARD?,
                    ATTR ordernum ON"""
Noé Gaumont's avatar
Noé Gaumont committed
127
                ),
128
            ]
129
            rsets = [cnx.execute(request) for request in rql_requests]
130
            entities.append(RqlIOSchemaHolder.format_schema(*rsets))
131
132
        cls._schema = {"entities": entities}
        return cls._schema
133

134
135
136
137
138
139
    @classmethod
    def get_schema_hash(cls, cnx):
        if cls._hash is not None:
            return cls._hash
        cls._hash = hashlib.md5(
            json_dumps(cls.get_schema(cnx)["entities"]).encode(cnx.encoding)
140
        ).hexdigest()
141
        return cls._hash