gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

251 行
6.3KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * jsonpath.h
  4. * Definitions for jsonpath datatype
  5. *
  6. * Copyright (c) 2019, PostgreSQL Global Development Group
  7. *
  8. * IDENTIFICATION
  9. * src/include/utils/jsonpath.h
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #ifndef JSONPATH_H
  14. #define JSONPATH_H
  15. #include "fmgr.h"
  16. #include "utils/jsonb.h"
  17. #include "nodes/pg_list.h"
  18. typedef struct
  19. {
  20. int32 vl_len_; /* varlena header (do not touch directly!) */
  21. uint32 header; /* version and flags (see below) */
  22. char data[FLEXIBLE_ARRAY_MEMBER];
  23. } JsonPath;
  24. #define JSONPATH_VERSION (0x01)
  25. #define JSONPATH_LAX (0x80000000)
  26. #define JSONPATH_HDRSZ (offsetof(JsonPath, data))
  27. #define DatumGetJsonPathP(d) ((JsonPath *) DatumGetPointer(PG_DETOAST_DATUM(d)))
  28. #define DatumGetJsonPathPCopy(d) ((JsonPath *) DatumGetPointer(PG_DETOAST_DATUM_COPY(d)))
  29. #define PG_GETARG_JSONPATH_P(x) DatumGetJsonPathP(PG_GETARG_DATUM(x))
  30. #define PG_GETARG_JSONPATH_P_COPY(x) DatumGetJsonPathPCopy(PG_GETARG_DATUM(x))
  31. #define PG_RETURN_JSONPATH_P(p) PG_RETURN_POINTER(p)
  32. #define jspIsScalar(type) ((type) >= jpiNull && (type) <= jpiBool)
  33. /*
  34. * All node's type of jsonpath expression
  35. */
  36. typedef enum JsonPathItemType
  37. {
  38. jpiNull = jbvNull, /* NULL literal */
  39. jpiString = jbvString, /* string literal */
  40. jpiNumeric = jbvNumeric, /* numeric literal */
  41. jpiBool = jbvBool, /* boolean literal: TRUE or FALSE */
  42. jpiAnd, /* predicate && predicate */
  43. jpiOr, /* predicate || predicate */
  44. jpiNot, /* ! predicate */
  45. jpiIsUnknown, /* (predicate) IS UNKNOWN */
  46. jpiEqual, /* expr == expr */
  47. jpiNotEqual, /* expr != expr */
  48. jpiLess, /* expr < expr */
  49. jpiGreater, /* expr > expr */
  50. jpiLessOrEqual, /* expr <= expr */
  51. jpiGreaterOrEqual, /* expr >= expr */
  52. jpiAdd, /* expr + expr */
  53. jpiSub, /* expr - expr */
  54. jpiMul, /* expr * expr */
  55. jpiDiv, /* expr / expr */
  56. jpiMod, /* expr % expr */
  57. jpiPlus, /* + expr */
  58. jpiMinus, /* - expr */
  59. jpiAnyArray, /* [*] */
  60. jpiAnyKey, /* .* */
  61. jpiIndexArray, /* [subscript, ...] */
  62. jpiAny, /* .** */
  63. jpiKey, /* .key */
  64. jpiCurrent, /* @ */
  65. jpiRoot, /* $ */
  66. jpiVariable, /* $variable */
  67. jpiFilter, /* ? (predicate) */
  68. jpiExists, /* EXISTS (expr) predicate */
  69. jpiType, /* .type() item method */
  70. jpiSize, /* .size() item method */
  71. jpiAbs, /* .abs() item method */
  72. jpiFloor, /* .floor() item method */
  73. jpiCeiling, /* .ceiling() item method */
  74. jpiDouble, /* .double() item method */
  75. jpiKeyValue, /* .keyvalue() item method */
  76. jpiSubscript, /* array subscript: 'expr' or 'expr TO expr' */
  77. jpiLast, /* LAST array subscript */
  78. jpiStartsWith, /* STARTS WITH predicate */
  79. jpiLikeRegex, /* LIKE_REGEX predicate */
  80. } JsonPathItemType;
  81. /* XQuery regex mode flags for LIKE_REGEX predicate */
  82. #define JSP_REGEX_ICASE 0x01 /* i flag, case insensitive */
  83. #define JSP_REGEX_DOTALL 0x02 /* s flag, dot matches newline */
  84. #define JSP_REGEX_MLINE 0x04 /* m flag, ^/$ match at newlines */
  85. #define JSP_REGEX_WSPACE 0x08 /* x flag, ignore whitespace in pattern */
  86. #define JSP_REGEX_QUOTE 0x10 /* q flag, no special characters */
  87. /*
  88. * Support functions to parse/construct binary value.
  89. * Unlike many other representation of expression the first/main
  90. * node is not an operation but left operand of expression. That
  91. * allows to implement cheap follow-path descending in jsonb
  92. * structure and then execute operator with right operand
  93. */
  94. typedef struct JsonPathItem
  95. {
  96. JsonPathItemType type;
  97. /* position form base to next node */
  98. int32 nextPos;
  99. /*
  100. * pointer into JsonPath value to current node, all positions of current
  101. * are relative to this base
  102. */
  103. char *base;
  104. union
  105. {
  106. /* classic operator with two operands: and, or etc */
  107. struct
  108. {
  109. int32 left;
  110. int32 right;
  111. } args;
  112. /* any unary operation */
  113. int32 arg;
  114. /* storage for jpiIndexArray: indexes of array */
  115. struct
  116. {
  117. int32 nelems;
  118. struct
  119. {
  120. int32 from;
  121. int32 to;
  122. } *elems;
  123. } array;
  124. /* jpiAny: levels */
  125. struct
  126. {
  127. uint32 first;
  128. uint32 last;
  129. } anybounds;
  130. struct
  131. {
  132. char *data; /* for bool, numeric and string/key */
  133. int32 datalen; /* filled only for string/key */
  134. } value;
  135. struct
  136. {
  137. int32 expr;
  138. char *pattern;
  139. int32 patternlen;
  140. uint32 flags;
  141. } like_regex;
  142. } content;
  143. } JsonPathItem;
  144. #define jspHasNext(jsp) ((jsp)->nextPos > 0)
  145. extern void jspInit(JsonPathItem *v, JsonPath *js);
  146. extern void jspInitByBuffer(JsonPathItem *v, char *base, int32 pos);
  147. extern bool jspGetNext(JsonPathItem *v, JsonPathItem *a);
  148. extern void jspGetArg(JsonPathItem *v, JsonPathItem *a);
  149. extern void jspGetLeftArg(JsonPathItem *v, JsonPathItem *a);
  150. extern void jspGetRightArg(JsonPathItem *v, JsonPathItem *a);
  151. extern Numeric jspGetNumeric(JsonPathItem *v);
  152. extern bool jspGetBool(JsonPathItem *v);
  153. extern char *jspGetString(JsonPathItem *v, int32 *len);
  154. extern bool jspGetArraySubscript(JsonPathItem *v, JsonPathItem *from,
  155. JsonPathItem *to, int i);
  156. extern const char *jspOperationName(JsonPathItemType type);
  157. /*
  158. * Parsing support data structures.
  159. */
  160. typedef struct JsonPathParseItem JsonPathParseItem;
  161. struct JsonPathParseItem
  162. {
  163. JsonPathItemType type;
  164. JsonPathParseItem *next; /* next in path */
  165. union
  166. {
  167. /* classic operator with two operands: and, or etc */
  168. struct
  169. {
  170. JsonPathParseItem *left;
  171. JsonPathParseItem *right;
  172. } args;
  173. /* any unary operation */
  174. JsonPathParseItem *arg;
  175. /* storage for jpiIndexArray: indexes of array */
  176. struct
  177. {
  178. int nelems;
  179. struct
  180. {
  181. JsonPathParseItem *from;
  182. JsonPathParseItem *to;
  183. } *elems;
  184. } array;
  185. /* jpiAny: levels */
  186. struct
  187. {
  188. uint32 first;
  189. uint32 last;
  190. } anybounds;
  191. struct
  192. {
  193. JsonPathParseItem *expr;
  194. char *pattern; /* could not be not null-terminated */
  195. uint32 patternlen;
  196. uint32 flags;
  197. } like_regex;
  198. /* scalars */
  199. Numeric numeric;
  200. bool boolean;
  201. struct
  202. {
  203. uint32 len;
  204. char *val; /* could not be not null-terminated */
  205. } string;
  206. } value;
  207. };
  208. typedef struct JsonPathParseResult
  209. {
  210. JsonPathParseItem *expr;
  211. bool lax;
  212. } JsonPathParseResult;
  213. extern JsonPathParseResult *parsejsonpath(const char *str, int len);
  214. extern int jspConvertRegexFlags(uint32 xflags);
  215. #endif
上海开阖软件有限公司 沪ICP备12045867号-1