Generate Samples Sales and Order Data in PostgreSQL demonstrates how to programmatically generate and insert synthetic customer and sales order data into a PostgreSQL database using Python. Here’s a breakdown of what it does:
- Imports necessary libraries for date/time (
datetime), unique ID generation (uuid), random numbers (random), Java-based database connectivity (jaydebeapi), and OS operations. - Defines simple helpers:
getToday(): Returns the current date as an ISO string.generateSQL(tablename, columns, values): Assembles anINSERT INTOSQL command for a given table, columns, and value strings.generateJSON(columns, values): Intended to build a dict for columns/values, but has structural issues in the provided code.randomString(length): Generates a random lowercase ASCII string of specified length.commaCheck(field): Converts fields to string format appropriate for SQL insertion depending on type.
- The
Customerclass represents a single customer, with fields for customer number (UUID), name, phone, postal code, locale, date created, and email. - On instantiation, populates each field with random or deterministic values and also generates between 1–5 random sales orders associated with this customer.
- Has methods to return the list of columns, values (for SQL), and associated sales orders.
- Represents an order associated with a customer.
- Populates order fields (order number, comments, dates, type, quantity, etc.) with random or deterministic values upon instantiation.
- Methods return column names and value lists for SQL insertion.
Database table schemas are included as comments to match these classes.
- The
connect()function sets up a JDBC database connection to a PostgreSQL instance usingjaydebeapiand a local JDBC driver JAR file. - The connection parameters (hostname, port, user, password, DB name) are hard-coded.
- Returns a connection object.
generateCustomers(conn): Creates 2000 customers, generates a corresponding SQLINSERTfor each and inserts it into thecustomerstable, then callsgenerateOrders()for every associated sales order.generateOrders(orders, conn): For each order, generates and executes an SQLINSERTto theorderstable.
- The
main()function connects to the database and invokesgenerateCustomers(), triggering the customer and order data generation and insertion process. - The script's
__main__block callsmain()to start the process when run as a standalone program.
- Purpose: Populate a PostgreSQL inventory database with randomly generated mock "customers" and their related "sales orders".
- Features:
- Automatic table insert SQL and data generation.
- Class-based organization for clarity and extensibility.
- Supports generating thousands of randomized records, creating a robust test set for demos, development, or benchmarking.
- Notes:
- Actual execution requires PostgreSQL with the correct schema, a working JDBC connector, and valid file paths/user credentials for your environment.
- Some code sections (JSON function, randomString integration, error handling) may need further refinement to be production-ready.
This serves as an automated test data loader and can be adapted for other relational databases or schemas with minor changes.