diff sat_pubsub/pgsql_storage.py @ 352:efbdca10f0fb

schema: node schema implementation node schema is an experimental (not standard yet, protoXEP should follow) feature allowing to attach a data schema to a node. This commit implement it and method needed to retrieve/set a schema.
author Goffi <goffi@goffi.org>
date Fri, 08 Sep 2017 08:02:05 +0200
parents 2098295747fd
children 95c83899b5e9
line wrap: on
line diff
--- a/sat_pubsub/pgsql_storage.py	Fri Sep 08 08:02:05 2017 +0200
+++ b/sat_pubsub/pgsql_storage.py	Fri Sep 08 08:02:05 2017 +0200
@@ -147,7 +147,10 @@
                     const.OPT_ACCESS_MODEL:row[6],
                     const.OPT_PUBLISH_MODEL:row[7],
                     }
-            node = LeafNode(row[0], row[1], configuration)
+            schema = row[8]
+            if schema is not None:
+                schema = parseXml(schema)
+            node = LeafNode(row[0], row[1], configuration, schema)
             node.dbpool = self.dbpool
             return node
         elif row[2] == 'collection':
@@ -157,7 +160,7 @@
                     const.OPT_ACCESS_MODEL: row[6],
                     const.OPT_PUBLISH_MODEL:row[7],
                     }
-            node = CollectionNode(row[0], row[1], configuration)
+            node = CollectionNode(row[0], row[1], configuration, None)
             node.dbpool = self.dbpool
             return node
         else:
@@ -179,6 +182,7 @@
                                  send_last_published_item,
                                  access_model,
                                  publish_model,
+                                 schema::text,
                                  pep
                             FROM nodes
                             WHERE node_id=%s""",
@@ -198,6 +202,7 @@
                                           send_last_published_item,
                                           access_model,
                                           publish_model,
+                                          schema::text,
                                           pep
                                    FROM nodes
                                    WHERE node=%s""",
@@ -228,11 +233,11 @@
         d.addCallback(lambda results: [r[0] for r in results])
         return d
 
-    def createNode(self, nodeIdentifier, owner, config, pep, recipient=None):
+    def createNode(self, nodeIdentifier, owner, config, schema, pep, recipient=None):
         return self.dbpool.runInteraction(self._createNode, nodeIdentifier,
-                                           owner, config, pep, recipient)
+                                           owner, config, schema, pep, recipient)
 
-    def _createNode(self, cursor, nodeIdentifier, owner, config, pep, recipient):
+    def _createNode(self, cursor, nodeIdentifier, owner, config, schema, pep, recipient):
         if config['pubsub#node_type'] != 'leaf':
             raise error.NoCollections()
 
@@ -241,15 +246,16 @@
         try:
             cursor.execute("""INSERT INTO nodes
                               (node, node_type, persist_items,
-                               deliver_payloads, send_last_published_item, access_model, publish_model, pep)
+                               deliver_payloads, send_last_published_item, access_model, publish_model, schema, pep)
                               VALUES
-                              (%s, 'leaf', %s, %s, %s, %s, %s, %s)""",
+                              (%s, 'leaf', %s, %s, %s, %s, %s, %s, %s)""",
                            (nodeIdentifier,
                             config['pubsub#persist_items'],
                             config['pubsub#deliver_payloads'],
                             config['pubsub#send_last_published_item'],
                             config[const.OPT_ACCESS_MODEL],
                             config[const.OPT_PUBLISH_MODEL],
+                            schema,
                             recipient.userhost() if pep else None
                             )
                            )
@@ -398,10 +404,11 @@
 
     implements(iidavoll.INode)
 
-    def __init__(self, nodeDbId, nodeIdentifier, config):
+    def __init__(self, nodeDbId, nodeIdentifier, config, schema):
         self.nodeDbId = nodeDbId
         self.nodeIdentifier = nodeIdentifier
         self._config = config
+        self._schema = schema
 
     def _checkNodeExists(self, cursor):
         cursor.execute("""SELECT 1 as exist FROM nodes WHERE node_id=%s""",
@@ -449,6 +456,24 @@
     def _setCachedConfiguration(self, void, config):
         self._config = config
 
+    def getSchema(self):
+        return self._schema
+
+    def setSchema(self, schema):
+        d = self.dbpool.runInteraction(self._setSchema, schema)
+        d.addCallback(self._setCachedSchema, schema)
+        return d
+
+    def _setSchema(self, cursor, schema):
+        self._checkNodeExists(cursor)
+        cursor.execute("""UPDATE nodes SET schema=%s
+                          WHERE node_id=%s""",
+                       (schema.toXml() if schema else None,
+                        self.nodeDbId))
+
+    def _setCachedSchema(self, void, schema):
+        self._schema = schema
+
     def getMetaData(self):
         config = copy.copy(self._config)
         config["pubsub#node_type"] = self.nodeType