Skip to content

Latest commit

 

History

History
67 lines (56 loc) · 3.13 KB

File metadata and controls

67 lines (56 loc) · 3.13 KB

skip_sql_comments

Location

src/bin/pgbench/pgbench.c:5550-5584

Overview

Skips over leading whitespace and SQL-style line comments (--) to find the start of actual SQL command content.

Definition

static char *
skip_sql_comments(char *sql_command)

Detailed Description

The skip_sql_comments function processes SQL command text to locate the beginning of executable SQL content by skipping over non-essential elements. It iteratively advances through the input string, ignoring whitespace characters and SQL line comments that begin with '--'. The function handles comments by finding the newline character that terminates them, then continues processing from the next line. This preprocessing is essential for pgbench's SQL command parsing pipeline, ensuring that only meaningful SQL content is processed.

The function uses a simple state machine approach, continuously checking each character and advancing the pointer until it encounters the first non-whitespace, non-comment character. If the entire string contains only whitespace and comments, the function returns NULL to indicate no executable content was found.

Parameters / Member Variables

  • *sql_command: Pointer to a null-terminated string containing the SQL command text to process

Dependencies

  • Functions called/Symbols referenced:
    • isspace: Standard C library function to check for whitespace characters
    • strncmp: Standard C library function to compare string prefixes
    • strchr: Standard C library function to find character occurrences
  • Called from (representative examples):

Notes and Other Information

  • The function modifies the input pointer but does not alter the actual string content
  • Returns NULL if no executable SQL content is found after skipping comments and whitespace
  • Only handles '--' style line comments, not /* */ block comments
  • The function assumes properly formed input and does not perform comprehensive SQL syntax validation
  • Used as a preprocessing step before more detailed SQL command parsing and validation
  • The comment handling stops at newline characters, following standard SQL comment behavior
  • Whitespace detection uses the standard isspace() function, which handles various whitespace characters

Simplified Source

static char *skip_sql_comments(char *sql_command) {
    char *p = sql_command;

    // Skip whitespace and line comments
    for (;;) {
        if (isspace((unsigned char) *p)) {
            // Skip whitespace characters
            p++;
        } else if (strncmp(p, "--", 2) == 0) {
            // Skip '--' style comment until newline
            p = strchr(p, '\n');
            if (p == NULL)
                return NULL;  // Comment goes to end of string
            p++;
        } else {
            // Found start of actual content
            break;
        }
    }

    // Return NULL if only whitespace and comments found
    if (*p == '\0')
        return NULL;

    return p;
}