SGBD/table.py

53 lines
1.6 KiB
Python
Raw Normal View History

2023-11-24 01:25:20 +00:00
class Table:
def __init__(self, name):
self.name = name
self.columns = []
self.rows = []
def __str__(self):
return self.name
def __repr__(self):
return self.name
def load_from_csv(self, csv_file):
with open(csv_file, 'r') as f:
reader = f.read().splitlines()
# First line is the column names
self.columns = reader[0].split(',')
reader.pop(0)
for row in reader:
row = row.split(',')
self.rows.append(row)
def print_columns(self, column_list: list, where_filter: list = None, join_stmt: list = None):
if column_list[0] == '*':
column_list = self.columns
print(', '.join(column_list))
print('-' * 20)
for row in self.rows:
if where_filter is not None:
column_index = self.columns.index(where_filter[0])
row_value = row[column_index]
if where_filter[1] == '=':
if row_value != where_filter[2]:
continue
elif where_filter[1] == '>':
if row_value <= where_filter[2]:
continue
elif where_filter[1] == '<':
if row_value >= where_filter[2]:
continue
row_values = []
for column in column_list:
column_index = self.columns.index(column)
row_values.append(row[column_index])
print(', '.join(row_values))