PostgreSQL Manager

Rows

Interacting with table rows.


exists()

Checks if a row exists in the specified table that matches the given conditions.

Signature
def exists(database: str, table: str, conditions: dict, logical_operator: str = "AND") -> bool:

Parameters

Parameter Type Description
database str Name of the database
table str Name of the table
conditions dict Dictionary of field-value pairs to match
logical_operator str Logical operator for combining multiple conditions ("AND" or "OR")

Returns

bool - True if matching row exists, False otherwise

Example

# Check if a user with username 'john_doe' exists
exists = Rows.exists("my_database", "users", {"username": "john_doe"})
print(exists)  # Output: True or False

list()

Retrieves rows from a table, optionally filtered by conditions.

Signature
def list(database: str, table: str, conditions: dict = None, 
         logical_operator: str = "AND", limit: int = 100) -> list[dict]:

Parameters

Parameter Type Description
database str Name of the database
table str Name of the table
conditions dict Optional dictionary of field-value pairs to filter rows
logical_operator str Logical operator for combining multiple conditions ("AND" or "OR")
limit int Maximum number of rows to return (default: 100)

Returns

list[dict] - A list of dictionaries, each representing a row

Examples

# 1. Retrieve all active users
database = "my_database"
table = "users"
conditions = {"status": "active"}
active_users = Rows.list(database, table, conditions, logical_operator="AND", limit=20)
print(active_users)  
# Returns: [{'id': 1, 'username': 'alice', 'status': 'active'}, {'id': 2, 'username': 'bob', 'status': 'active'}]
                                
# 2. Get all orders placed after March 1, 2024
database = "my_database"
table = "orders"
conditions = {"created_at >": "2024-03-01 00:00:00"}
recent_orders = Rows.list(database, table, conditions, logical_operator="AND", limit=50)
print(recent_orders)  
# Returns: [{'id': 5, 'created_at': '2024-03-05', 'amount': 100.0}, ...]
                                
# 3. Fetch all high-value transactions (amount > 1000)
database = "my_database"
table = "transactions"
conditions = {"amount >": 1000}
high_value_transactions = Rows.list(database, table, conditions, logical_operator="AND", limit=30)
print(high_value_transactions)  
# Returns: [{'id': 10, 'amount': 1500, 'status': 'completed'}, ...]
                                
# 4. Get orders with either status 'shipped' or 'delivered'
database = "my_database"
table = "orders"
conditions = {"status": "shipped", "status": "delivered"}
shipped_or_delivered_orders = Rows.list(database, table, conditions, logical_operator="OR", limit=100)
print(shipped_or_delivered_orders)  
# Returns: [{'id': 20, 'status': 'shipped'}, {'id': 21, 'status': 'delivered'}, ...]
                                
# 5. Retrieve users who haven't logged in for more than a year
database = "my_database"
table = "users"
conditions = {"last_login <": "2023-03-25 00:00:00"}
inactive_users = Rows.list(database, table, conditions, logical_operator="AND", limit=100)
print(inactive_users)  
# Returns: [{'id': 7, 'username': 'charlie', 'last_login': '2022-12-10'}, ...]

create()

Inserts one or more new rows into a table.

Signature
def create(database: str, table: str, data: list[dict]) -> bool:

Parameters

Parameter Type Description
database str Name of the database
table str Name of the table
data list[dict] List of dictionaries where each dictionary represents a row to insert

Returns

bool - True if all rows were inserted successfully, False otherwise

Example

# Create two new users
success = Rows.create("my_database", "users", [
    {"username": "alice", "email": "[email protected]"},
    {"username": "bob", "email": "[email protected]"}
])
print("Creation successful:", success)

delete()

Deletes a specific row from a table by its ID.

Signature
def delete(database: str, table: str, row_id: int) -> bool:

Parameters

Parameter Type Description
database str Name of the database
table str Name of the table
row_id int The primary key ID of the row to delete

Returns

bool - True if the row was deleted successfully, False otherwise

Example

# Delete user with ID 5
deleted = Rows.delete("my_database", "users", 5)
print("User deleted:", deleted)

update()

Updates specific fields of a row in a table.

Signature
def update(database: str, table: str, row_id: int, update_data: dict) -> bool:

Parameters

Parameter Type Description
database str Name of the database
table str Name of the table
row_id int The primary key ID of the row to update
update_data dict Dictionary of field-value pairs to update

Returns

bool - True if the row was updated successfully, False otherwise

Example

# Update email for user with ID 3
updated = Rows.update("my_database", "users", 3, 
                      {"email": "[email protected]"})
print("Update successful:", updated)