CUBRID Engine  latest
db_json_path.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * db_json_path.hpp - functions related to json paths
21  */
22 
23 #ifndef _DB_JSON_PATH_HPP_
24 #define _DB_JSON_PATH_HPP_
25 
26 #include "db_json_allocator.hpp"
28 #include "db_rapidjson.hpp"
29 
30 #include <string>
31 #include <unordered_set>
32 #include <vector>
33 
34 struct PATH_TOKEN
35 {
37  {
43  // token type used to signify json_pointers' '-' special token
45  };
46 
48  std::string m_object_key;
49  unsigned long m_array_idx;
50 
51  PATH_TOKEN ();
52  PATH_TOKEN (token_type type, unsigned long array_idx);
53  PATH_TOKEN (token_type type, std::string &&s);
54 
55  const std::string &get_object_key () const;
56  unsigned long get_array_index () const;
57 
58  bool is_wildcard () const;
59  bool static match_pattern (const PATH_TOKEN &matcher, const PATH_TOKEN &matchee);
60 };
61 
62 class JSON_PATH
63 {
64  public:
66  {
69  FULL_MATCH
70  };
71 
72  std::string dump_json_path () const;
73  int parse (const char *path);
74  JSON_PATH get_parent () const;
75 
76  JSON_VALUE *get (JSON_DOC &jd) const;
77  const JSON_VALUE *get (const JSON_DOC &jd) const;
78  std::vector<const JSON_VALUE *> extract (const JSON_DOC &) const;
79 
80  void set (JSON_DOC &jd, const JSON_VALUE &jv) const;
81  void set (JSON_VALUE &jd, const JSON_VALUE &jv, JSON_PRIVATE_MEMPOOL &allocator) const;
82  bool erase (JSON_DOC &jd) const;
83 
84  const PATH_TOKEN *get_last_token () const;
85  size_t get_token_count () const;
86  bool is_root_path () const;
87  bool is_last_array_index_less_than (size_t size) const;
88  bool is_last_token_array_index_zero () const;
89  bool points_to_array_cell () const;
90  bool parent_exists (JSON_DOC &jd) const;
91  bool contains_wildcard () const;
92 
93  void push_array_index (unsigned long idx);
94  void push_array_index_wildcard ();
95  void push_object_key (std::string &&object_key);
96  void push_object_key_wildcard ();
97  void push_double_wildcard ();
98  void pop ();
99 
100  static MATCH_RESULT match_pattern (const JSON_PATH &pattern, const JSON_PATH &path);
101 
102  private:
103  using token_containter_type = std::vector<PATH_TOKEN>;
104 
105  int from_json_pointer (const std::string &pointer_path);
106 
107  int validate_and_create_from_json_path (std::string &sql_path);
108 
109  static MATCH_RESULT match_pattern (const JSON_PATH &pattern, const token_containter_type::const_iterator &it1,
110  const JSON_PATH &path, const token_containter_type::const_iterator &it2);
111 
112  static void extract_from_subtree (const JSON_PATH &path, size_t tkn_array_offset,
113  const JSON_VALUE &jv, std::unordered_set<const JSON_VALUE *> &unique_elements,
114  std::vector<const JSON_VALUE *> &vals);
115 
117 };
118 
119 int db_json_path_unquote_object_keys (std::string &sql_path);
120 #endif /* _DB_JSON_HPP_ */
token_containter_type m_path_tokens
const std::string & get_object_key() const
static bool match_pattern(const PATH_TOKEN &matcher, const PATH_TOKEN &matchee)
int db_json_path_unquote_object_keys(std::string &sql_path)
token_type m_type
bool is_wildcard() const
rapidjson::MemoryPoolAllocator< JSON_PRIVATE_ALLOCATOR > JSON_PRIVATE_MEMPOOL
unsigned long get_array_index() const
rapidjson::GenericValue< JSON_ENCODING, JSON_PRIVATE_MEMPOOL > JSON_VALUE
std::string m_object_key
std::vector< PATH_TOKEN > token_containter_type
unsigned long m_array_idx