|
| 1 | +from sqlalchemy import ( |
| 2 | + and_, |
| 3 | + delete, |
| 4 | + insert, |
| 5 | + or_, |
| 6 | + select, |
| 7 | + update, |
| 8 | +) |
| 9 | + |
| 10 | +from examples.config import ENGINE |
| 11 | + |
| 12 | +# Import the Table objects (not classes) from your metadata |
| 13 | +from examples.features.non_orm._0_create_tables import ( |
| 14 | + email_address_table, |
| 15 | + user_table, |
| 16 | +) |
| 17 | + |
| 18 | +# 0. Clean tables |
| 19 | +with ENGINE.begin() as conn: |
| 20 | + conn.execute(delete(email_address_table)) |
| 21 | + conn.execute(delete(user_table)) |
| 22 | + |
| 23 | +# 1. Insert multiple entries |
| 24 | +data = [ |
| 25 | + { |
| 26 | + "first_name": "Lux", |
| 27 | + "last_name": "Noceda", |
| 28 | + |
| 29 | + }, |
| 30 | + { |
| 31 | + "first_name": "Eda", |
| 32 | + "last_name": "Clawthorne", |
| 33 | + "email_addresses": [ "[email protected]"], |
| 34 | + }, |
| 35 | + { |
| 36 | + "first_name": "Raine", |
| 37 | + "last_name": "Whispers", |
| 38 | + "email_addresses": [ "[email protected]"], |
| 39 | + }, |
| 40 | + { |
| 41 | + "first_name": "Amity", |
| 42 | + "last_name": "Blight", |
| 43 | + "email_addresses": [ "[email protected]"], |
| 44 | + }, |
| 45 | + { |
| 46 | + "first_name": "Willow", |
| 47 | + "last_name": "Park", |
| 48 | + "email_addresses": [ "[email protected]"], |
| 49 | + }, |
| 50 | +] |
| 51 | + |
| 52 | +with ENGINE.begin() as conn: |
| 53 | + # a. Insert Users |
| 54 | + user_payload = [ |
| 55 | + {"first_name": d["first_name"], "last_name": d["last_name"]} for d in data |
| 56 | + ] |
| 57 | + conn.execute(insert(user_table), user_payload) |
| 58 | + |
| 59 | + # b. Retrieve user ids |
| 60 | + stmt = select(user_table.c.id, user_table.c.first_name, user_table.c.last_name) |
| 61 | + user_map = {(row.first_name, row.last_name): row.id for row in conn.execute(stmt)} |
| 62 | + |
| 63 | + # c. Insert EmailAddresses |
| 64 | + email_payload = [] |
| 65 | + for entry in data: |
| 66 | + u_id = user_map.get((entry["first_name"], entry["last_name"])) |
| 67 | + for email in entry["email_addresses"]: |
| 68 | + email_payload.append({"user_id": u_id, "email_address": email}) |
| 69 | + conn.execute(insert(email_address_table), email_payload) |
| 70 | + |
| 71 | + |
| 72 | +# 2. Display results |
| 73 | +def select_all_entries(): |
| 74 | + with ENGINE.connect() as conn: |
| 75 | + # Join user_table and email_address_table explicitly |
| 76 | + stmt = select( |
| 77 | + user_table.c.id, |
| 78 | + user_table.c.first_name, |
| 79 | + user_table.c.last_name, |
| 80 | + email_address_table.c.email_address, |
| 81 | + ).select_from(user_table.join(email_address_table, isouter=True)) |
| 82 | + results = conn.execute(stmt).fetchall() |
| 83 | + |
| 84 | + # Grouping manually since Core doesn't have ORM's unique().all() object deduplication |
| 85 | + grouped = {} |
| 86 | + for row in results: |
| 87 | + key = (row.id, row.first_name, row.last_name) |
| 88 | + if key not in grouped: |
| 89 | + grouped[key] = [] |
| 90 | + if row.email_address: |
| 91 | + grouped[key].append(row.email_address) |
| 92 | + |
| 93 | + for (u_id, f_name, l_name), emails in grouped.items(): |
| 94 | + print(f"{u_id} {f_name} {l_name} {emails}") |
| 95 | + |
| 96 | + |
| 97 | +select_all_entries() |
| 98 | + |
| 99 | +# 3. Update multiple entries |
| 100 | +with ENGINE.begin() as conn: |
| 101 | + # a. Update the last_name of Users |
| 102 | + new_last_name = "Clawthorne-Whispers" |
| 103 | + conn.execute( |
| 104 | + update(user_table) |
| 105 | + .where(user_table.c.last_name.in_(["Whispers", "Clawthorne"])) |
| 106 | + .values(last_name=new_last_name) |
| 107 | + ) |
| 108 | + |
| 109 | + # b. Update the EmailAddress |
| 110 | + eda_id_subq = ( |
| 111 | + select(user_table.c.id) |
| 112 | + .where(user_table.c.first_name == "Eda") |
| 113 | + .scalar_subquery() |
| 114 | + ) |
| 115 | + conn.execute( |
| 116 | + update(email_address_table) |
| 117 | + .where(email_address_table.c.user_id == eda_id_subq) |
| 118 | + . values( email_address="[email protected]") |
| 119 | + ) |
| 120 | + |
| 121 | +# 4. Delete multiple entries |
| 122 | +with ENGINE.begin() as conn: |
| 123 | + # a. Define the selection criteria for the users you want to target |
| 124 | + target_criteria = or_( |
| 125 | + and_(user_table.c.first_name == "Amity", user_table.c.last_name == "Blight"), |
| 126 | + and_(user_table.c.first_name == "Willow", user_table.c.last_name == "Park"), |
| 127 | + and_(user_table.c.first_name == "Lux", user_table.c.last_name == "Noceda"), |
| 128 | + ) |
| 129 | + stmt = select(user_table.c.id).where(target_criteria) |
| 130 | + users_ids = conn.execute(stmt).scalars().all() |
| 131 | + |
| 132 | + # b. Delete EmailAddresses associated with these Users, as they graduated |
| 133 | + for uid in users_ids: |
| 134 | + email_delete_stmt = delete(email_address_table).where( |
| 135 | + email_address_table.c.user_id == uid |
| 136 | + ) |
| 137 | + conn.execute(email_delete_stmt) |
| 138 | + |
| 139 | +select_all_entries() |
0 commit comments