DBD-Pg

 view release on metacpan or  search on metacpan

quote.c  view on Meta::CPAN

        ||
        (0 == length)
        ) {
        *new_length = 5;
        New(0, new_string, *new_length + 1, char);
        Copy("FALSE", new_string, *new_length, char);
        new_string[*new_length] = '\0';
        return new_string;
    }

    croak("quote_bool: invalid input");
    return NULL;
}

/*
  Quote an integer.
  This function will croak if an unexpected character appears (including a NUL).
  It allows for leading and trailing spaces, and + or - signs before the digit.
  Note that we don't go overboard in checking, letting the server handle items like '+-123'
*/
char * quote_integer(pTHX_ const char *string, STRLEN length, STRLEN *new_length, const int supports_estring)
{
    char * new_string;
    const char * const string_start = string;
    const STRLEN original_length = length;
    bool seen_digit = DBDPG_FALSE;
    bool seen_trailing_space = DBDPG_FALSE;

    PERL_UNUSED_ARG(supports_estring);

    while (length > 0) {
        if (isdigit((unsigned char)*string) && !seen_trailing_space) {
            seen_digit = DBDPG_TRUE;
        }
        else if (' ' == *string) {
            if (seen_digit)
                seen_trailing_space = DBDPG_TRUE;
        }
        else if (!seen_digit && !seen_trailing_space && ('+' == *string || '-' == *string)) {
            ; /* All ok */
        }
        else {
            croak("quote_integer: invalid input");
        }
        string++;
        length--;
    }
    if (!seen_digit)
        croak("quote_integer: invalid input");

    New(0, new_string, original_length + 1, char);
    Copy(string_start, new_string, original_length, char);
    new_string[original_length] = '\0';
    (*new_length) = original_length;
    return new_string;
}

/*
  Quote a float aka numeric.
  We handle some string literals, e.g. "infinity"
  The server is more liberal than we are here, allowing nonsense things like '+NaN'.
  If not a special string, we allow for <space> .+- e E and 0-9 characters.
*/
char * quote_float(pTHX_ const char *string, STRLEN length, STRLEN *new_length, const int supports_estring)
{
    const char * const string_start = string;
    const STRLEN original_length = length;
    char * new_string;
    char * new_string_start;
    bool seen_digit = DBDPG_FALSE;

    PERL_UNUSED_ARG(supports_estring);

    if ((3 == length && 0 == strncasecmp(string, "NaN", 3)) ||
        (8 == length && 0 == strncasecmp(string, "Infinity", 8)) ||
        (9 == length && 0 == strncasecmp(string, "+Infinity", 9)) ||
        (9 == length && 0 == strncasecmp(string, "-Infinity", 9)) ||
        (3 == length && 0 == strncasecmp(string, "Inf", 3)) ||
        (4 == length && 0 == strncasecmp(string, "+Inf", 4)) ||
        (4 == length && 0 == strncasecmp(string, "-Inf", 4))) {
        New(0, new_string, length + 3, char);
        new_string_start = new_string;
        *new_string++ = '\'';
        Copy(string_start, new_string, length, char);
        new_string += length;
        *new_string++ = '\'';
        *new_string++ = '\0';
        *new_length = length + 2;
        return new_string_start;
    }

    while (length > 0) {
        if (isdigit((unsigned char)*string)) {
            seen_digit = DBDPG_TRUE;
        }
        else if (' ' == *string
                 || '.' == *string
                 || '+' == *string
                 || '-' == *string
                 || 'e' == *string
                 || 'E' == *string) {
            ; /* Technically the order matters, but this is a simple check */
        }
        else
            croak("quote_float: invalid input");

        string++;
        length--;
    }
    if (!seen_digit)
        croak("quote_float: invalid input");

    New(0, new_string, original_length + 1, char);
    new_string_start = new_string;
    Copy(string_start, new_string, original_length, char);
    new_string[original_length] = '\0';
    (*new_length) = original_length;
    return new_string_start;
}

/*
  Quote an identifier.
  This function will croak if a NUL occurs before the supplied length.
  Reference: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
*/

char * quote_identifier(pTHX_ const char *string, STRLEN length, STRLEN *new_length, const int supports_estring)
{
    const char * const string_start = string;
    const STRLEN original_length = length;
    char * new_string;
    char * new_string_start;
    STRLEN embedded_quotes = 0;
    bool needs_quoting = DBDPG_FALSE;



( run in 0.370 second using v1.01-cache-2.11-cpan-ff9377addf4 )