Commits

Vladimir Janković committed 23c536e

[sqlite backend] Added Types table and the Fields table is populated when creating entities.

Comments (0)

Files changed (2)

roktar/backends/sqlite_backend.py

 from uuid import uuid4
 
 from _interface import RoktarBackend
+from numbers import Number, Real
+from roktar import errors
+from test.test_typechecks import Integer
 
 
 def _entity_factory(cursor, row):
     text_value_for_indexing TEXT
 );
 
+CREATE TABLE Types (
+    name TEXT PRIMARY KEY,
+    type TEXT
+);
+
 CREATE TABLE Changes (
     id INTEGER PRIMARY KEY,
     change_uuid TEXT,
 """
 
 
+FIELD_INTEGER = "INTEGER"
+FIELD_REAL = "REAL"
+FIELD_TEXT = "TEXT"
+FIELD_PICKLE = "PICKLE"
+
+_field_name_type_map = {
+    FIELD_INTEGER: "integer_value",
+    FIELD_REAL: "real_value",
+    FIELD_TEXT: "text_value",
+    FIELD_PICKLE: "text_value"
+}
+
+
 class BrokerMixin:
     def commit(self):
         self.db_connection.commit()
         return self.db_connection.execute(
             "SELECT MAX(entity_id) AS _max_id FROM Entities"
         ).fetchone()["_max_id"]
-        
+
     def get_last_change_uuid(self):
         return self.db_connection.execute(
             "SELECT MAX(id) AS _max_id FROM Changes"
             for row in cursor.execute("SELECT sql FROM sqlite_master")
         ]
         table_definitions = sorted(filter(None, table_definitions))
-        
+
         if not table_definitions:
             cursor.executescript(schema_definition)
 
 
         last_change = self.get_last_change_uuid()
         for key, value in data.iteritems():
+            t = self.select("Types", name=key).fetchone()
+            old_field_type = t["type"] if t else None
+
+            if isinstance(value, int):
+                field_type = FIELD_INTEGER
+            elif isinstance(value, float):
+                field_type = FIELD_REAL
+            elif isinstance(value, basestring):
+                field_type = FIELD_TEXT
+            else:
+                field_type = FIELD_PICKLE
+
+            if old_field_type and old_field_type != field_type:
+                raise errors.WrongFieldTypeError(key)
+
+            if old_field_type is None:
+                self.insert(
+                    "Types",
+                    name=key,
+                    type=field_type
+                )
+
+            row = dict(entity_id=entity_id, name=key)
+            row[_field_name_type_map[field_type]] = (
+                dumps(value) if field_type == FIELD_PICKLE else value
+            )
+            self.insert("Fields", **row)
+
             new_change = uuid4().hex
             self.insert(
                 "Changes",
 
 class ConfigurationNotFileSystemBasedError(Exception):
     pass
+
+class WrongFieldTypeError(Exception):
+    pass