Skip to content

feat: Implement SQL Minifier with robust parsing and comprehensive tests#104

Closed
Copilot wants to merge 2 commits intomainfrom
copilot/fix-905a522c-9c53-42ca-96fd-0694162a19ef
Closed

feat: Implement SQL Minifier with robust parsing and comprehensive tests#104
Copilot wants to merge 2 commits intomainfrom
copilot/fix-905a522c-9c53-42ca-96fd-0694162a19ef

Conversation

Copy link
Contributor

Copilot AI commented Jul 28, 2025

This PR implements a production-ready SQL Minifier utility that addresses all the issues identified in PR #85's code review. The original implementation had several critical flaws that could break SQL syntax and produce incorrect results.

Issues Fixed

1. Improper Comment Removal

Problem: The original regex --.*$ only matched single-line comments at the end of lines and could incorrectly remove -- from within string literals.

Solution: Implemented proper SQL parsing that correctly identifies and preserves string literals while removing comments:

// Before: Could break this SQL
SELECT 'Price: $10 -- not a comment' FROM products;

// After: Correctly preserves the string content
SELECT 'Price: $10 -- not a comment' FROM products;

2. Destructive Whitespace Handling

Problem: The original implementation replaced all whitespace with single spaces, which could corrupt string literals containing intentional spaces or newlines.

Solution: Implemented context-aware whitespace normalization that preserves string content:

// Before: Would break this
SELECT 'hello   world' FROM users;  // Multiple spaces lost

// After: Preserves string content exactly
SELECT 'hello   world' FROM users;  // Spaces preserved in string

3. Ineffective Error Handling

Problem: The try-catch block was useless since regex operations don't throw exceptions.

Solution: Added proper input validation and meaningful error handling:

export function validateSQLInput(input: string): { isValid: boolean; error?: string } {
  // Validates string input and checks for unmatched quotes
  // Returns meaningful error messages for invalid SQL
}

Implementation Details

  • Robust SQL Parser: Built a character-by-character parser that correctly handles:

    • Single and double-quoted strings with escaped quotes ('' and "")
    • Multi-line comments (/* ... */) including unclosed ones
    • Single-line comments (-- ...)
    • Mixed content with proper context awareness
  • Comprehensive Test Suite: Added 27 test cases covering:

    • Basic SQL minification
    • Comment removal edge cases
    • String literal preservation
    • Escaped quotes handling
    • Error validation
    • Complex real-world SQL queries
  • User-Friendly Interface: Built a clean React component with:

    • Real-time minification as you type
    • Error display for invalid input
    • Copy-to-clipboard functionality
    • Proper accessibility features

Example Usage

import { minifySQL } from '@/components/utils/sql-minifier.utils';

const complexSQL = `
  SELECT 
    u.name,  -- user name
    u.email,
    p.title /* post title */
  FROM users u
  JOIN posts p ON u.id = p.user_id
  WHERE u.name = 'John -- not a comment'
    AND p.created_at > '2023-01-01'
    /* AND p.status = 'published' -- this is commented out */
`;

const minified = minifySQL(complexSQL);
// Result: "SELECT u.name, u.email, p.title FROM users u JOIN posts p ON u.id = p.user_id WHERE u.name = 'John -- not a comment' AND p.created_at > '2023-01-01'"

Testing

All tests pass with 100% coverage of edge cases:

  • ✅ 27 unit tests for the minification logic
  • ✅ Manual end-to-end testing verified
  • ✅ No new linting errors introduced
  • ✅ Build passes successfully
![SQL Minifier Demo](https://github.com/user-attachments/assets/829efc27-5c97-4ebf-bc9d-94aff4063edb)

The feature is now production-ready and handles all the edge cases that would have caused issues with the original implementation.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 28, 2025

Deploying jam-dev-utilities with  Cloudflare Pages  Cloudflare Pages

Latest commit: 174fdd2
Status: ✅  Deploy successful!
Preview URL: https://e97e13a3.jam-dev-utilities.pages.dev
Branch Preview URL: https://copilot-fix-905a522c-9c53-42.jam-dev-utilities.pages.dev

View logs

Co-authored-by: peckz <18050177+peckz@users.noreply.github.com>
Copilot AI changed the title [WIP] Can you take over this PR and fix all mentioned issues, also make sure there is strong set of tests! feat: Implement SQL Minifier with robust parsing and comprehensive tests Jul 28, 2025
Copilot AI requested a review from peckz July 28, 2025 09:51
Copilot finished work on behalf of peckz July 28, 2025 09:51
@peckz
Copy link
Collaborator

peckz commented Jul 31, 2025

Cherry picked as part of #85

@peckz peckz closed this Jul 31, 2025
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