Skip to content

Commit

Permalink
filter_records function
Browse files Browse the repository at this point in the history
  • Loading branch information
truethari committed Feb 21, 2022
1 parent f726243 commit 35c0869
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ True
>> _dbmanager.delete_record(table="STUDENTS", primary_key="1010")
```

### Filter record/s from a table

```console
>> _dbmanager.filter_records(table="STUDENTS", values={"year":"2022"})
[{'student_id': '1010', 'name': 'ABC', 'mark': 10, 'year': '2022'}, {'student_id': '1011', 'name': 'DEF', 'mark': 100, 'year': '2022'}]
```

## 🌱 Contributing Guide

- Fork the project from the `alpha` branch and submit a Pull Request (PR)
Expand Down
35 changes: 35 additions & 0 deletions ReallySimpleDB/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,41 @@ def delete_record(self, table:str, primary_key, database:str=""):
self.connection.commit()

return True

def filter_records(self, table:str, values:dict, database:str=""):
if self.connection == "" and not len(database):
raise TypeError("delete_record() missing 1 required positional argument: 'database'")

if len(database):
self.create_connection(database)

if self.is_table(table_name=table, database=database):
cursor = self.connection.cursor()

sql = "SELECT * FROM {} WHERE ".format(table)

for value in values:
try:
sql += value + "='" + values[value] + "' AND "
except TypeError:
sql += value + "=" + str(values[value]) + " AND "

sql = sql[:-5] + ";"

cursor.execute(sql)
rows = cursor.fetchall()

columns = self.get_columns(table=table, database=database)
records = []
tmp_dict = {}

for row in rows:
for index, data in enumerate(row):
tmp_dict[columns[index]] = data
records.append(tmp_dict)
tmp_dict = {}

return records

def close_connection(self):
self.connection.close()
Expand Down
9 changes: 7 additions & 2 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from ast import Assert
import os
from re import X
from ReallySimpleDB import dbmanager

_dbmanager = dbmanager()
Expand Down Expand Up @@ -117,6 +115,13 @@ def test_get_all_records():
assert _dbmanager.get_all_records(table="STUDENTS") == [{'student_id': '1010', 'name': 'ABC', 'mark': 10, 'year': '2022'},
{'student_id': '1011', 'name': 'DEF', 'mark': 100, 'year': '2022'}]

def test_filter_record_1():
assert _dbmanager.filter_records(table="STUDENTS", values={"year":"2022"}) == [{'student_id': '1010', 'name': 'ABC', 'mark': 10, 'year': '2022'},
{'student_id': '1011', 'name': 'DEF', 'mark': 100, 'year': '2022'}]

def test_filter_record_2():
assert _dbmanager.filter_records(table="STUDENTS", values={"mark":100, "year":"2022"}) == [{'student_id': '1011', 'name': 'DEF', 'mark': 100, 'year': '2022'}]

def test_delete_record_1():
assert _dbmanager.delete_record(table="STUDENTS", primary_key="1010")

Expand Down

0 comments on commit 35c0869

Please sign in to comment.