You are building an internal analytics dashboard for a SaaS company. Analysts need to generate reports from the users and orders tables without writing raw SQL (to reduce errors and SQL injection risks).
Table Schemas:
users (
id SERIAL PRIMARY KEY,
created_at TIMESTAMPTZ NOT NULL,
email VARCHAR(255),
status VARCHAR(50), -- 'active', 'inactive', 'suspended'
country VARCHAR(2), -- ISO country codes e.g. 'US', 'IN', 'GB'
plan VARCHAR(50), -- 'free', 'pro', 'enterprise'
revenue NUMERIC(12,2), -- lifetime revenue
last_login TIMESTAMPTZ
);
orders (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
order_date TIMESTAMPTZ NOT NULL,
amount NUMERIC(10,2),
status VARCHAR(50), -- 'paid', 'refunded', 'failed'
product VARCHAR(100)
);Task:
Create a fluent, type-safe, secure Python query builder class called AnalyticsQueryBuilder that generates parameterized PostgreSQL queries.