| 1 | /*------------------------------------------------------------------------- |
| 2 | * |
| 3 | * value.h |
| 4 | * interface for PGValue nodes |
| 5 | * |
| 6 | * |
| 7 | * Copyright (c) 2003-2017, PostgreSQL Global Development PGGroup |
| 8 | * |
| 9 | * src/include/nodes/value.h |
| 10 | * |
| 11 | *------------------------------------------------------------------------- |
| 12 | */ |
| 13 | |
| 14 | #pragma once |
| 15 | |
| 16 | #include "nodes/nodes.hpp" |
| 17 | |
| 18 | namespace duckdb_libpgquery { |
| 19 | |
| 20 | /*---------------------- |
| 21 | * PGValue node |
| 22 | * |
| 23 | * The same PGValue struct is used for five node types: duckdb_libpgquery::T_PGInteger, |
| 24 | * duckdb_libpgquery::T_PGFloat, duckdb_libpgquery::T_PGString, duckdb_libpgquery::T_PGBitString, T_Null. |
| 25 | * |
| 26 | * Integral values are actually represented by a machine integer, |
| 27 | * but both floats and strings are represented as strings. |
| 28 | * Using duckdb_libpgquery::T_PGFloat as the node type simply indicates that |
| 29 | * the contents of the string look like a valid numeric literal. |
| 30 | * |
| 31 | * (Before Postgres 7.0, we used a double to represent duckdb_libpgquery::T_PGFloat, |
| 32 | * but that creates loss-of-precision problems when the value is |
| 33 | * ultimately destined to be converted to NUMERIC. Since PGValue nodes |
| 34 | * are only used in the parsing process, not for runtime data, it's |
| 35 | * better to use the more general representation.) |
| 36 | * |
| 37 | * Note that an integer-looking string will get lexed as duckdb_libpgquery::T_PGFloat if |
| 38 | * the value is too large to fit in a 'long'. |
| 39 | * |
| 40 | * Nulls, of course, don't need the value part at all. |
| 41 | *---------------------- |
| 42 | */ |
| 43 | typedef struct PGValue { |
| 44 | PGNodeTag type; /* tag appropriately (eg. duckdb_libpgquery::T_PGString) */ |
| 45 | union ValUnion { |
| 46 | long ival; /* machine integer */ |
| 47 | char *str; /* string */ |
| 48 | } val; |
| 49 | } PGValue; |
| 50 | |
| 51 | #define intVal(v) (((PGValue *)(v))->val.ival) |
| 52 | #define floatVal(v) atof(((PGValue *)(v))->val.str) |
| 53 | #define strVal(v) (((PGValue *)(v))->val.str) |
| 54 | |
| 55 | PGValue *makeInteger(long i); |
| 56 | PGValue *makeFloat(char *numericStr); |
| 57 | PGValue *makeString(const char *str); |
| 58 | PGValue *makeBitString(char *str); |
| 59 | |
| 60 | } |