Skip to content

Conversation

@sufleR
Copy link
Owner

@sufleR sufleR commented Dec 23, 2025

Summary

This PR introduces a comprehensive SQL comment removal system that addresses #20. The feature allows developers to configure how and when SQL comments are removed from queries, providing flexibility for different use cases (debugging, logging, production).

Key Features

  • Configurable removal strategies (config.remove_comments):

    • :none - Don't remove any comments
    • :oneline - Remove only single-line comments (--)
    • :multiline - Remove only multi-line comments (/* */)
    • :all - Remove both types (default)
  • Configurable scope (config.remove_comments_from):

    • :none - Don't remove comments anywhere
    • :prepared_for_logs - Remove comments only in prepared_for_logs method
    • :all - Remove comments from all queries (default)
  • SQL Standard Compliance:

    • Follows SQL-92 comment syntax
    • Supports all major databases (PostgreSQL, MySQL, Oracle, SQL Server, SQLite)
    • Preserves comments within quoted strings:
      • Single quotes: '-- not a comment'
      • Double quotes: "-- not a comment"
      • Dollar quotes (PostgreSQL): $$-- not a comment$$, $tag$-- not a comment$tag$
  • Edge Case Handling:

    • SQL-style escaped quotes ('', "")
    • Backslash-escaped quotes (\', \")
    • Comments without trailing newlines
    • Inline comments
    • Multiple comments of different types
    • Comments containing quote characters

Implementation Approach

State Machine Pattern

This implementation uses a state machine approach rather than complex regex for several reasons:

  1. Easier to Debug: Character-by-character processing with clear state tracking
  2. Better Edge Case Handling: Tracks multiple quote types simultaneously
  3. Maintainable: Easy to understand and extend
  4. Reliable: Handles nested quote types correctly

Architecture

  • CommentRemover Service Class (lib/sql_query/comment_remover.rb):

    • Implements state machine for comment removal
    • Handles all quote types and escape sequences
    • SQL-92 standard compliant
  • Config Class Extraction (lib/sql_query/config.rb):

    • Extracted to separate file for better organization
    • Added should_comments_be_removed?(for_logs:) method for clean logic
    • Reduces SqlQuery class complexity
  • Integration Points:

    • SqlQuery#sql - Applies removal based on configuration
    • SqlQuery#prepared_for_logs - Applies removal based on configuration
    • SqlQuery#apply_comment_removal - Helper method with clean logic

Testing

  • 78 total test examples, 0 failures
  • 98.38% code coverage
  • CommentRemover unit tests: 32 examples covering all scenarios
  • Integration tests: 10 examples for configuration combinations
  • Config tests: 12 examples for configuration behavior
  • Test fixtures: 5 SQL templates with various comment scenarios
  • RuboCop compliant: All style checks pass

Documentation

  • CHANGELOG.md: Updated with feature description
  • README.md: Added configuration examples and detailed option descriptions

Default Behavior

By default, both comment types are removed from all queries:

SqlQuery.configure do |config|
  config.remove_comments = :all        # Remove both -- and /* */
  config.remove_comments_from = :all   # From both sql() and prepared_for_logs()
end

Migration Path

This is a backwards-compatible addition:

  • Default behavior removes comments (improves log readability)
  • Can be disabled by setting config.remove_comments = :none
  • Can be scoped to only logs with config.remove_comments_from = :prepared_for_logs

Test Plan

  • All existing tests pass
  • New unit tests for CommentRemover service
  • New integration tests for configuration scenarios
  • Config class tests
  • RuboCop compliance verified
  • Manual testing with various SQL patterns

Addresses #20

This commit introduces a comprehensive SQL comment removal system that
is SQL-92 standard compliant and works across all major databases.

Key Features:
- Configurable comment removal strategies (:none, :oneline, :multiline, :all)
- Configurable scope (:none, :prepared_for_logs, :all)
- SQL standard compliant (SQL-92)
- Preserves comments in quoted strings (single, double, dollar quotes)
- Handles all edge cases (escaped quotes, inline comments, etc.)

Implementation:
- New CommentRemover service class using state machine pattern
- Extracted Config class to separate file for better organization
- Added Config#should_comments_be_removed? method for clean logic
- Integrated into sql() and prepared_for_logs() methods

Testing:
- Comprehensive unit tests for CommentRemover (32 examples)
- Integration tests for configuration scenarios (10 examples)
- Config class tests (12 examples)
- Test fixtures for various comment scenarios
- 78 total tests, 98.38% code coverage
- RuboCop compliant

Documentation:
- Updated CHANGELOG.md with feature description
- Updated README.md with configuration options and examples

Addresses #20
- CommentRemover now accepts config object instead of strategy symbol
- CommentRemover handles all removal decisions (both strategy and scope)
- Simplified SqlQuery#apply_comment_removal to always call CommentRemover
- All comment removal logic now centralized in one place
- Updated all tests to pass config objects and for_logs parameter
- Added comprehensive tests for remove_comments_from configuration
- 84 tests, all passing, 98.33% coverage
- RuboCop compliant
@sufleR sufleR merged commit defbdf5 into master Dec 23, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants