this repo has no description

Add query function

+109 -1
+37
db.py
··· 153 153 return f"Database({list(self.tables.keys())!r})" 154 154 155 155 156 + def query( 157 + db, 158 + select=(), 159 + select_as=None, 160 + distinct=None, 161 + from_=None, 162 + join=(), 163 + where=(), 164 + group_by=(), 165 + having=None, 166 + order_by=None, 167 + offset=None, 168 + limit=None, 169 + ) -> Table: 170 + if from_ is None: 171 + raise ValueError("Need a FROM clause") 172 + result = db.FROM(*from_) 173 + for j in join: 174 + table_name, pred = j 175 + result = db.JOIN(result, db.tables[table_name], pred) 176 + for w in where: 177 + result = db.WHERE(result, w) 178 + if group_by: 179 + result = db.GROUP_BY(result, group_by) 180 + if having: 181 + result = db.HAVING(result, having) 182 + if select: 183 + result = db.SELECT(result, select, select_as or {}) 184 + if order_by: 185 + result = db.ORDER_BY(result, order_by) 186 + if offset: 187 + result = db.OFFSET(result, offset) 188 + if limit: 189 + result = db.LIMIT(result, limit) 190 + return result 191 + 192 + 156 193 def csv(table): 157 194 print(",".join(table.colnames())) 158 195 for row in table.rows:
+72 -1
db_tests.py
··· 1 1 import unittest 2 - from db import Database, Table 2 + from db import Database, Table, query 3 3 4 4 __import__("sys").modules["unittest.util"]._MAX_LENGTH = 999999999 5 5 ··· 577 577 "city": "Houston", 578 578 "state": "Texas", 579 579 }, 580 + ), 581 + ) 582 + 583 + 584 + class EndToEndTests(unittest.TestCase): 585 + def test_query(self): 586 + db = Database() 587 + friend = db.CREATE_TABLE("friend") 588 + db.INSERT_INTO( 589 + "friend", 590 + [ 591 + {"id": 1, "name": "Alice", "city": "Denver", "state": "Colorado"}, 592 + { 593 + "id": 2, 594 + "name": "Bob", 595 + "city": "Colorado Springs", 596 + "state": "Colorado", 597 + }, 598 + {"id": 3, "name": "Charles", "city": "South Park", "state": "Colorado"}, 599 + {"id": 4, "name": "Dave", "city": "Fort Collins", "state": "Colorado"}, 600 + {"id": 5, "name": "Edna", "city": "Houston", "state": "Texas"}, 601 + {"id": 6, "name": "Francis", "city": "Denver", "state": "Colorado"}, 602 + {"id": 7, "name": "Gloria", "city": "Corpus Christi", "state": "Texas"}, 603 + {"id": 9, "name": "Homer", "city": "Denver", "state": "Colorado"}, 604 + ], 605 + ) 606 + employee = db.CREATE_TABLE("employee") 607 + db.INSERT_INTO( 608 + "employee", 609 + [ 610 + {"id": 1, "name": "Alice", "department_id": 100, "salary": 100}, 611 + {"id": 2, "name": "Bob", "department_id": 2, "salary": 150}, 612 + {"id": 3, "name": "Charles", "department_id": 2, "salary": 200}, 613 + {"id": 4, "name": "Dave", "department_id": 1, "salary": 180}, 614 + ], 615 + ) 616 + department = db.CREATE_TABLE("department") 617 + db.INSERT_INTO( 618 + "department", 619 + [ 620 + {"id": 1, "title": "Accounting"}, 621 + {"id": 2, "title": "Engineering"}, 622 + ], 623 + ) 624 + result = query( 625 + db, 626 + select=["employee.name", "department.title", "friend.state"], 627 + select_as={ 628 + "employee.name": "Name", 629 + "department.title": "Dept", 630 + "friend.state": "From", 631 + }, 632 + from_=["employee"], 633 + join=[ 634 + [ 635 + "department", 636 + lambda row: row["employee.department_id"] == row["department.id"], 637 + ], 638 + ["friend", lambda row: row["friend.name"] == row["employee.name"]], 639 + ], 640 + where=[ 641 + lambda row: row["employee.salary"] > 150, 642 + lambda row: row["friend.state"] == "Colorado", 643 + ], 644 + order_by=lambda row: row["Dept"], 645 + ) 646 + self.assertEqual( 647 + result.rows, 648 + ( 649 + {"Name": "Dave", "Dept": "Accounting", "From": "Colorado"}, 650 + {"Name": "Charles", "Dept": "Engineering", "From": "Colorado"}, 580 651 ), 581 652 ) 582 653