Skip to content

Standardize database access layer on free functions #21

@kaladron

Description

@kaladron

Problem

The codebase currently has three different layers for database access, creating inconsistency and confusion. This incomplete transition needs to be resolved by standardizing on a single approach.

Current State - Three Access Patterns

Method 1: Direct SQLite (files_shl.cc)

// Direct database calls - low level

Method 2: Sql Class Wrapper

Sql db;
auto star1 = db.getstar(snum);     // Member function approach
auto planet1 = db.getplanet(s, p); // Inconsistent with free functions

Method 3: Free Functions (Most Common)

auto star2 = ::getstar(snum);      // Global scope
auto star3 = getstar(snum);        // Most widely used

Current Usage Analysis

  • Free functions (getstar(), getplanet(), etc.) are used throughout most of the codebase
  • Sql class methods (Sql::getstar()) are used inconsistently in some files
  • This creates confusion about which pattern to use

Recommended Standardization

Standardize on free functions as they are:

  1. Most widely adopted in the codebase
  2. Simplest to use (no object instantiation required)
  3. Consistent with the command pattern architecture
  4. Already integrated with the module system

Files Requiring Updates (~10 files)

Need to audit and identify files using:

// Replace this pattern:
Sql db;
auto star = db.getstar(snum);

// With this pattern:
auto star = getstar(snum);

Implementation Steps

  1. Audit Phase: Identify all locations using Sql::getstar(), Sql::getplanet(), etc.
  2. Replace Phase: Convert to free function calls
  3. Cleanup Phase: Remove unused Sql class methods if no longer needed
  4. Documentation Phase: Update any documentation to reflect standard pattern

Benefits

  • Single, consistent database access pattern
  • Reduced cognitive overhead for developers
  • Simpler API surface
  • Better integration with existing command architecture
  • Easier testing and mocking

Success Criteria

  • All database access uses free functions (getstar(), getplanet(), etc.)
  • No remaining usage of Sql::get*() member functions
  • Consistent pattern across all command files
  • Build succeeds without warnings
  • Game functionality remains unchanged
  • Documentation updated to reflect standard pattern

Priority

Medium - Improves code consistency and developer experience, part of broader modernization.

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions