Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:


1. Imports and Utility Functions

  • 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 an INSERT INTO SQL 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.

2. Entity Classes

Customer

  • The Customer class 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.

SalesOrder

  • 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.


3. Database Connection

  • The connect() function sets up a JDBC database connection to a PostgreSQL instance using jaydebeapi and a local JDBC driver JAR file.
  • The connection parameters (hostname, port, user, password, DB name) are hard-coded.
  • Returns a connection object.

4. Data Generation and Insertion

  • generateCustomers(conn): Creates 2000 customers, generates a corresponding SQL INSERT for each and inserts it into the customers table, then calls generateOrders() for every associated sales order.
  • generateOrders(orders, conn): For each order, generates and executes an SQL INSERT to the orders table.

5. Execution

  • The main() function connects to the database and invokes generateCustomers(), triggering the customer and order data generation and insertion process.
  • The script's __main__ block calls main() to start the process when run as a standalone program.

Summary

  • 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages