Skip to content

Commit d95733b

Browse files
committed
Feature Added: Rename & Drop
1 parent f05b5e7 commit d95733b

File tree

1 file changed

+57
-8
lines changed

1 file changed

+57
-8
lines changed

filexdb/collection.py

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ 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 = Document(self._file_handler.read(), False)
2323

2424
self._cursor: int = 0
2525

@@ -28,12 +28,12 @@ def __init__(self, col_name: str, file_handler: FileIO) -> None:
2828
if self._col_name in self._database.keys():
2929

3030
# Get the existing Collection
31-
self._collection: list = self._database[self._col_name]
31+
self._collection: JsonArray = self._database[self._col_name]
3232

3333
else:
3434
# Create new Collection
35-
self._database[self._col_name] = []
36-
self._collection: list = self._database[self._col_name]
35+
self._database[self._col_name] = JsonArray([])
36+
self._collection: JsonArray = self._database[self._col_name]
3737

3838
def insert(self, document: Mapping) -> str:
3939
"""
@@ -254,12 +254,61 @@ def update(self, document: Mapping, query=None) -> JsonArray[str]:
254254

255255
return JsonArray(_doc_id)
256256

257-
def rename(self, new_name: str) -> str:
258-
pass
257+
def rename(self, new_name: str) -> int:
258+
"""
259+
This method used to change the name of collection.
260+
Takes current name & new name to change name of the collection.
261+
262+
:param new_name: New name for collection.
263+
:return: Amount of affected collection.
264+
"""
265+
266+
# Initiating counter
267+
count = 0
268+
269+
# Checking the collection is already exist or not
270+
if new_name not in self._database.keys():
271+
272+
# Creating new collection and
273+
# Putting old data into new collection
274+
self._database[new_name] = self._collection
275+
276+
# Writing Current database status into the file
277+
self._file_handler.write(self._database)
278+
279+
# Remove old collection
280+
self.drop()
281+
282+
# Increasing counter
283+
count += 1
284+
285+
return count
286+
287+
def drop(self) -> int:
288+
"""
289+
Deletes the selected collection from the database
290+
291+
:return: Amount of affected collection
292+
"""
293+
294+
# Initiating counter
295+
count = 0
296+
297+
# Getting database
298+
_database = self._file_handler.read()
299+
300+
# Check database has the collection or not
301+
if self._col_name in _database.keys():
302+
# Removing collection from database
303+
_database.pop(self._col_name)
304+
305+
# Writing current status of database into the file system.
306+
self._file_handler.write(_database)
259307

260-
def drop(self) -> str:
261-
pass
308+
# Increasing counter
309+
count += 1
262310

311+
return count
263312

264313

265314
# ----------------------------------------------------------------#

0 commit comments

Comments
 (0)