Database Module

Database operations for GS1 GPC.

This module provides functions for setting up and interacting with the database, including creating tables and inserting data.

class gs1_gpc.db.DatabaseConnection(connection_string, db_type='sqlite')[source]

Bases: object

Database connection abstraction for SQLite and PostgreSQL.

__init__(connection_string, db_type='sqlite')[source]

Initialize a database connection.

Parameters:
  • connection_string (str) – Connection string or path to database file

  • db_type (str) – Database type (‘sqlite’ or ‘postgresql’)

connect()[source]

Connect to the database.

Returns:

(connection, cursor) or (None, None) on failure

Return type:

tuple

close()[source]

Close the database connection.

commit()[source]

Commit changes to the database.

rollback()[source]

Rollback changes to the database.

gs1_gpc.db.setup_database(db_connection)[source]

Create GPC tables if they don’t exist.

The schema uses junction tables for the many-to-many relationships between bricks and attribute types, and between attribute types and attribute values. In the GS1 GPC XML, the same attribute type can appear on many bricks, and the same attribute value can appear under many attribute types. Previous versions used single foreign keys which silently dropped these relationships via INSERT OR IGNORE.

Parameters:

db_connection (DatabaseConnection) – Database connection object

Returns:

True if successful, False otherwise

Return type:

bool

gs1_gpc.db.insert_segment(cursor, segment_code, description)[source]

Insert a segment record.

Parameters:
  • cursor – Database cursor

  • segment_code (str) – Segment code

  • description (str) – Segment description

Returns:

True if successful, False otherwise

Return type:

bool

gs1_gpc.db.insert_family(cursor, family_code, description, segment_code)[source]

Insert a family record.

Parameters:
  • cursor – Database cursor

  • family_code (str) – Family code

  • description (str) – Family description

  • segment_code (str) – Segment code

Returns:

True if successful, False otherwise

Return type:

bool

gs1_gpc.db.insert_class(cursor, class_code, description, family_code)[source]

Insert a class record.

Parameters:
  • cursor – Database cursor

  • class_code (str) – Class code

  • description (str) – Class description

  • family_code (str) – Family code

Returns:

True if successful, False otherwise

Return type:

bool

gs1_gpc.db.insert_brick(cursor, brick_code, description, class_code)[source]

Insert a brick record.

Parameters:
  • cursor – Database cursor

  • brick_code (str) – Brick code

  • description (str) – Brick description

  • class_code (str) – Class code

Returns:

True if successful, False otherwise

Return type:

bool

gs1_gpc.db.insert_attribute_type(cursor, att_type_code, att_type_text, brick_code)[source]

Insert an attribute type record and link it to a brick.

Inserts the attribute type into the global table (if not already present) and creates the brick-to-attribute-type junction record.

Parameters:
  • cursor – Database cursor

  • att_type_code (str) – Attribute type code

  • att_type_text (str) – Attribute type description

  • brick_code (str) – Brick code

Returns:

True if the attribute type was newly inserted, False if it already existed

Return type:

bool

gs1_gpc.db.insert_attribute_value(cursor, att_value_code, att_value_text, att_type_code)[source]

Insert an attribute value record and link it to an attribute type.

Inserts the attribute value into the global table (if not already present) and creates the attribute-type-to-value junction record.

Parameters:
  • cursor – Database cursor

  • att_value_code (str) – Attribute value code

  • att_value_text (str) – Attribute value description

  • att_type_code (str) – Attribute type code

Returns:

True if the attribute value was newly inserted, False if it already existed

Return type:

bool