@@ -19,31 +19,26 @@ def __init__(self, col_name: str, file_handler: FileIO) -> None:
1919 self ._file_handler = file_handler
2020
2121 # Get the data of existing Database or empty database.
22- self ._database = self ._file_handler . read ()
22+ self ._database = self ._get_database ()
2323
24- self ._cursor : int = 0
24+ # Initiating Collecting
25+ self ._collection = self ._get_collection ()
2526
26- # Initiate a default Collection.
27- # Check the Collection is already exists or no.
28- if self ._col_name in self ._database .keys ():
27+ # Cursor
28+ self ._cursor : int = 0
2929
30- # Get the existing Collection
31- self ._collection : list = self ._database [self ._col_name ]
3230
33- else :
34- # Create new Collection
35- self ._database [self ._col_name ] = []
36- self ._collection : list = self ._database [self ._col_name ]
3731
3832 def insert (self , document : Mapping ) -> str :
3933 """
4034 Inserts a single Document into the Database.
4135
4236 Document should be JSON Object.
4337
44- :param document: Document to insert into database
45- :return: None
38+ :param document: Document to insert into the database.
39+ :return: Document ID.
4640 """
41+
4742 # Make sure the document implements the ``Mapping`` interface
4843 if not isinstance (document , Mapping ):
4944 raise ValueError ('Document is not a Dictionary' )
@@ -52,10 +47,14 @@ def insert(self, document: Mapping) -> str:
5247 if "_id_" in document .keys ():
5348 raise KeyError (f"You are not allowed to modify key `_id_`" )
5449
50+
51+ # getting Database
52+ _database = self ._get_database ()
53+
5554 # Create a Document
5655 _document = Document (document )
5756
58- # id of Document
57+ # ID of Document
5958 _doc_id : str = _document .id
6059
6160 # check Document is already exist or not
@@ -65,11 +64,11 @@ def insert(self, document: Mapping) -> str:
6564 self ._collection .append (_document )
6665
6766 # Add modified Collection to Database
68- self . _database [self ._col_name ] = self ._collection
67+ _database [self ._col_name ] = self ._collection
6968
7069 # print(self._database)
7170 # Write current state of Database into the Database-file
72- self ._file_handler .write (self . _database )
71+ self ._file_handler .write (_database )
7372
7473 return _doc_id
7574 else :
@@ -254,15 +253,96 @@ def update(self, document: Mapping, query=None) -> JsonArray[str]:
254253
255254 return JsonArray (_doc_id )
256255
257- def rename (self , new_name : str ) -> str :
258- pass
256+ def rename (self , new_name : str ) -> int :
257+ """
258+ This method used to change the name of collection.
259+ Takes current name & new name to change name of the collection.
260+
261+ :param new_name: New name for collection.
262+ :return: Amount of affected collection.
263+ """
264+
265+ # Initiating counter
266+ count = 0
259267
260- def drop (self ) -> str :
261- pass
268+ # Checking the collection is already exist or not
269+ if new_name not in self ._database .keys ():
270+
271+ # Creating new collection and
272+ # Putting old data into new collection
273+ self ._database [new_name ] = self ._collection
274+
275+ # Writing Current database status into the file
276+ self ._file_handler .write (self ._database )
262277
278+ # Remove old collection
279+ self .drop ()
280+
281+ # Increasing counter
282+ count += 1
283+
284+ return count
285+
286+ def drop (self ) -> int :
287+ """
288+ Deletes the selected collection from the database
289+
290+ :return: Amount of affected collection
291+ """
292+
293+ # Initiating counter
294+ count = 0
295+
296+ # Getting database
297+ _database = self ._file_handler .read ()
298+
299+ # Check database has the collection or not
300+ if self ._col_name in _database .keys ():
301+ # Removing collection from database
302+ _database .pop (self ._col_name )
303+
304+ # Writing current status of database into the file system.
305+ self ._file_handler .write (_database )
306+
307+ # Increasing counter
308+ count += 1
309+
310+ return count
263311
264312
265313 # ----------------------------------------------------------------#
314+ def _get_database (self ) -> Document :
315+ """
316+ Getting Database
317+
318+ :return: Database
319+ """
320+ # Get the data of existing Database or empty database.
321+ database = Document (self ._file_handler .read (), False )
322+
323+ return database
324+
325+ def _get_collection (self ) -> JsonArray :
326+ """
327+ Getting Collection
328+
329+ :return: Collection
330+ """
331+ # Initiate a default Collection.
332+ # Check the Collection is already exists or no.
333+ if self ._col_name in self ._database .keys ():
334+
335+ # Get the existing Collection
336+ _collection : JsonArray = self ._database [self ._col_name ]
337+
338+ else :
339+ # Create new Collection
340+ self ._database [self ._col_name ] = JsonArray ([])
341+ _collection : JsonArray = self ._database [self ._col_name ]
342+
343+ return _collection
344+
345+
266346 def _reset_cursor (self ) -> None :
267347 """
268348 Reset Cursor Pointer to 0th index
0 commit comments