From b62933629bbf56de1a4338274665c7947f15475a Mon Sep 17 00:00:00 2001 From: Yingtong Li Date: Thu, 4 Jan 2018 16:40:32 +0800 Subject: [PATCH] Implement deletion --- eos/core/db/__init__.py | 6 ++++++ eos/core/db/mongodb.py | 3 +++ eos/core/db/postgresql.py | 5 +++++ eos/core/objects/__init__.py | 3 +++ 4 files changed, 17 insertions(+) diff --git a/eos/core/db/__init__.py b/eos/core/db/__init__.py index 05504db..6b3a98f 100644 --- a/eos/core/db/__init__.py +++ b/eos/core/db/__init__.py @@ -36,6 +36,9 @@ class DBProvider: def update_by_id(self, collection, _id, value): raise Exception('Not implemented') + def delete_by_id(self, collection, _id): + raise Exception('Not implemented') + def reset_db(self): raise Exception('Not implemented') @@ -55,6 +58,9 @@ class DummyProvider(DBProvider): def update_by_id(self, collection, _id, value): pass + def delete_by_id(self, collection, _id): + pass + def reset_db(self): pass diff --git a/eos/core/db/mongodb.py b/eos/core/db/mongodb.py index 73702ee..bb955d0 100644 --- a/eos/core/db/mongodb.py +++ b/eos/core/db/mongodb.py @@ -42,6 +42,9 @@ class MongoDBProvider(eos.core.db.DBProvider): def update_by_id(self, collection, _id, value): self.db[collection].replace_one({'_id': _id}, value, upsert=True) + def delete_by_id(self, collection, _id): + self.db[collection].delete_one({'_id': _id}) + def reset_db(self): self.client.drop_database(self.db_name) diff --git a/eos/core/db/postgresql.py b/eos/core/db/postgresql.py index d5668e0..e38a6ae 100644 --- a/eos/core/db/postgresql.py +++ b/eos/core/db/postgresql.py @@ -58,6 +58,11 @@ class PostgreSQLDBProvider(eos.core.db.DBProvider): self.cur.execute(SQL('INSERT INTO {} (_id, data) VALUES (%s, %s) ON CONFLICT (_id) DO UPDATE SET data = excluded.data').format(Identifier(table)), (_id, psycopg2.extras.Json(value))) self.conn.commit() + def delete_by_id(self, table, _id): + self.create_table(table) + self.cur.execute(SQL('DELETE FROM {} WHERE _id = %s').format(Identifier(table)), (_id)) + self.conn.commit() + def reset_db(self): self.cur.execute('DROP SCHEMA public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO public') self.conn.commit() diff --git a/eos/core/objects/__init__.py b/eos/core/objects/__init__.py index 885eaed..87870ec 100644 --- a/eos/core/objects/__init__.py +++ b/eos/core/objects/__init__.py @@ -481,6 +481,9 @@ class TopLevelObject(DocumentObject, metaclass=TopLevelObjectType): #res = dbinfo.db[self._db_name].replace_one({'_id': self._fields['_id'].serialise(self._id)}, EosObject.serialise_and_wrap(self), upsert=True) dbinfo.provider.update_by_id(self._db_name, self._fields['_id'].serialise(self._id), EosObject.serialise_and_wrap(self)) + def delete(self): + dbinfo.provider.delete_by_id(self._db_name, self._fields['_id'].serialise(self._id)) + @classmethod def get_all(cls): return [EosObject.deserialise_and_unwrap(x) for x in dbinfo.provider.get_all(cls._db_name)]