Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Defines | Functions

uuid.c File Reference

#include "postgres.h"
#include "access/hash.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/uuid.h"
Include dependency graph for uuid.c:

Go to the source code of this file.

Data Structures

struct  pg_uuid_t

Defines

#define UUID_LEN   16

Functions

static void string_to_uuid (const char *source, pg_uuid_t *uuid)
static int uuid_internal_cmp (const pg_uuid_t *arg1, const pg_uuid_t *arg2)
Datum uuid_in (PG_FUNCTION_ARGS)
Datum uuid_out (PG_FUNCTION_ARGS)
Datum uuid_recv (PG_FUNCTION_ARGS)
Datum uuid_send (PG_FUNCTION_ARGS)
Datum uuid_lt (PG_FUNCTION_ARGS)
Datum uuid_le (PG_FUNCTION_ARGS)
Datum uuid_eq (PG_FUNCTION_ARGS)
Datum uuid_ge (PG_FUNCTION_ARGS)
Datum uuid_gt (PG_FUNCTION_ARGS)
Datum uuid_ne (PG_FUNCTION_ARGS)
Datum uuid_cmp (PG_FUNCTION_ARGS)
Datum uuid_hash (PG_FUNCTION_ARGS)

Define Documentation

#define UUID_LEN   16

Definition at line 22 of file uuid.c.

Referenced by uuid_hash(), uuid_internal_cmp(), uuid_recv(), and uuid_send().


Function Documentation

static void string_to_uuid ( const char *  source,
pg_uuid_t uuid 
) [static]

Definition at line 83 of file uuid.c.

References pg_uuid_t::data, ereport, errcode(), errmsg(), ERROR, i, and NULL.

Referenced by uuid_in().

{
    const char *src = source;
    bool        braces = false;
    int         i;

    if (src[0] == '{')
    {
        src++;
        braces = true;
    }

    for (i = 0; i < UUID_LEN; i++)
    {
        char        str_buf[3];

        if (src[0] == '\0' || src[1] == '\0')
            goto syntax_error;
        memcpy(str_buf, src, 2);
        if (!isxdigit((unsigned char) str_buf[0]) ||
            !isxdigit((unsigned char) str_buf[1]))
            goto syntax_error;

        str_buf[2] = '\0';
        uuid->data[i] = (unsigned char) strtoul(str_buf, NULL, 16);
        src += 2;
        if (src[0] == '-' && (i % 2) == 1 && i < UUID_LEN - 1)
            src++;
    }

    if (braces)
    {
        if (*src != '}')
            goto syntax_error;
        src++;
    }

    if (*src != '\0')
        goto syntax_error;

    return;

syntax_error:
    ereport(ERROR,
            (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
             errmsg("invalid input syntax for uuid: \"%s\"",
                    source)));
}

Datum uuid_cmp ( PG_FUNCTION_ARGS   ) 

Definition at line 217 of file uuid.c.

References PG_GETARG_UUID_P, PG_RETURN_INT32, and uuid_internal_cmp().

Datum uuid_eq ( PG_FUNCTION_ARGS   ) 

Definition at line 180 of file uuid.c.

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

Datum uuid_ge ( PG_FUNCTION_ARGS   ) 

Definition at line 189 of file uuid.c.

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

Datum uuid_gt ( PG_FUNCTION_ARGS   ) 

Definition at line 198 of file uuid.c.

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

Datum uuid_hash ( PG_FUNCTION_ARGS   ) 

Definition at line 227 of file uuid.c.

References pg_uuid_t::data, hash_any(), PG_GETARG_UUID_P, and UUID_LEN.

{
    pg_uuid_t  *key = PG_GETARG_UUID_P(0);

    return hash_any(key->data, UUID_LEN);
}

Datum uuid_in ( PG_FUNCTION_ARGS   ) 

Definition at line 34 of file uuid.c.

References palloc(), PG_GETARG_CSTRING, PG_RETURN_UUID_P, and string_to_uuid().

Referenced by special_uuid_value(), and uuid_generate_internal().

{
    char       *uuid_str = PG_GETARG_CSTRING(0);
    pg_uuid_t  *uuid;

    uuid = (pg_uuid_t *) palloc(sizeof(*uuid));
    string_to_uuid(uuid_str, uuid);
    PG_RETURN_UUID_P(uuid);
}

static int uuid_internal_cmp ( const pg_uuid_t arg1,
const pg_uuid_t arg2 
) [static]

Definition at line 156 of file uuid.c.

References pg_uuid_t::data, memcmp(), and UUID_LEN.

Referenced by uuid_cmp(), uuid_eq(), uuid_ge(), uuid_gt(), uuid_le(), uuid_lt(), and uuid_ne().

{
    return memcmp(arg1->data, arg2->data, UUID_LEN);
}

Datum uuid_le ( PG_FUNCTION_ARGS   ) 

Definition at line 171 of file uuid.c.

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

Datum uuid_lt ( PG_FUNCTION_ARGS   ) 

Definition at line 162 of file uuid.c.

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

Datum uuid_ne ( PG_FUNCTION_ARGS   ) 

Definition at line 207 of file uuid.c.

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

Datum uuid_out ( PG_FUNCTION_ARGS   ) 

Definition at line 45 of file uuid.c.

References appendStringInfoChar(), buf, StringInfoData::data, pg_uuid_t::data, i, initStringInfo(), PG_GETARG_UUID_P, and PG_RETURN_CSTRING.

Referenced by uuid_generate_v35_internal().

{
    pg_uuid_t  *uuid = PG_GETARG_UUID_P(0);
    static const char hex_chars[] = "0123456789abcdef";
    StringInfoData buf;
    int         i;

    initStringInfo(&buf);
    for (i = 0; i < UUID_LEN; i++)
    {
        int         hi;
        int         lo;

        /*
         * We print uuid values as a string of 8, 4, 4, 4, and then 12
         * hexadecimal characters, with each group is separated by a hyphen
         * ("-"). Therefore, add the hyphens at the appropriate places here.
         */
        if (i == 4 || i == 6 || i == 8 || i == 10)
            appendStringInfoChar(&buf, '-');

        hi = uuid->data[i] >> 4;
        lo = uuid->data[i] & 0x0F;

        appendStringInfoChar(&buf, hex_chars[hi]);
        appendStringInfoChar(&buf, hex_chars[lo]);
    }

    PG_RETURN_CSTRING(buf.data);
}

Datum uuid_recv ( PG_FUNCTION_ARGS   ) 
Datum uuid_send ( PG_FUNCTION_ARGS   )