CUBRID Engine  latest
access_json_table.cpp
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 // access_json_table.cpp - implementation of structures required to access json table spec type.
21 //
22 
23 #include "access_json_table.hpp"
24 
25 #include "db_json.hpp"
26 #include "dbtype.h"
27 #include "error_code.h"
28 #include "error_manager.h"
31 #include "object_primitive.h"
32 
33 #include <cassert>
34 
35 namespace cubxasl
36 {
37  namespace json_table
38  {
39 
40  int
41  column::trigger_on_error (const JSON_DOC &input, const TP_DOMAIN_STATUS &status_cast, db_value &value_out)
42  {
43  (void) pr_clear_value (&value_out);
44  (void) db_make_null (&value_out);
45 
46  switch (m_on_error.m_behavior)
47  {
49  er_clear ();
50  return NO_ERROR;
51 
53  {
55 
57  unique_ptr_json_body.get (), m_path, m_column_name,
59 
61  }
62 
65  er_clear ();
67  {
68  assert (false);
69  }
70  return NO_ERROR;
71 
72  default:
73  assert (false);
74  return ER_FAILED;
75  }
76  }
77 
78  int
80  {
81  (void) pr_clear_value (&value_out);
82  (void) db_make_null (&value_out);
83 
84  switch (m_on_empty.m_behavior)
85  {
87  return NO_ERROR;
88 
92 
96  {
97  assert (false);
98  }
99  return NO_ERROR;
100 
101  default:
102  assert (false);
103  return ER_FAILED;
104  }
105  }
106 
108  {
109  init ();
110  }
111 
112  void
114  {
115  m_domain = NULL;
116  m_path = NULL;
124  }
125 
126  int
128  {
129  int error_code = NO_ERROR;
130  JSON_DOC_STORE docp;
132 
133  error_code = db_json_extract_document_from_path (&input, m_path, docp);
134  if (error_code != NO_ERROR)
135  {
136  ASSERT_ERROR ();
138  return ER_FAILED;
139  }
140 
141  if (docp.is_null ())
142  {
144  if (error_code != NO_ERROR)
145  {
146  ASSERT_ERROR ();
147  }
148  return error_code;
149  }
150 
151  // clear previous output_value
154 
156  if (status_cast != TP_DOMAIN_STATUS::DOMAIN_COMPATIBLE)
157  {
158  error_code = trigger_on_error (input, status_cast, *m_output_value_pointer);
159  if (error_code != NO_ERROR)
160  {
161  ASSERT_ERROR ();
162  }
163  }
164 
165  return error_code;
166  }
167 
168  int
170  {
171  int error_code = NO_ERROR;
172  bool result = false;
174 
175  error_code = db_json_contains_path (&input, std::vector<std::string> (1, m_path), false, result);
176  if (error_code != NO_ERROR)
177  {
178  ASSERT_ERROR ();
180  return ER_FAILED;
181  }
182 
183  db_make_short (m_output_value_pointer, result ? 1 : 0);
184 
186  if (status_cast != TP_DOMAIN_STATUS::DOMAIN_COMPATIBLE)
187  {
188  return ER_FAILED;
189  }
190 
191  return error_code;
192  }
193 
194  int
195  column::evaluate_ordinality (size_t ordinality)
196  {
198 
199  db_make_int (m_output_value_pointer, (int) ordinality);
200 
201  return NO_ERROR;
202  }
203 
204  int
205  column::evaluate (const JSON_DOC &input, size_t ordinality)
206  {
208 
211 
212  int error_code = NO_ERROR;
213 
214  switch (m_function)
215  {
217  error_code = evaluate_extract (input);
218  break;
220  error_code = evaluate_exists (input);
221  break;
223  error_code = evaluate_ordinality (ordinality);
224  break;
225  default:
226  return ER_FAILED;
227  }
228 
229  return error_code;
230  }
231 
232  void
233  column::clear_xasl (bool is_final_clear /* = true */)
234  {
235  if (is_final_clear)
236  {
239  }
240 
242  {
245  }
246  }
247 
248  node::node (void)
249  {
250  init ();
251  }
252 
253  void
255  {
256  m_path = NULL;
257  init_ordinality ();
258  m_output_columns = NULL;
259  m_output_columns_size = 0;
260  m_nested_nodes = NULL;
261  m_nested_nodes_size = 0;
262  m_id = 0;
263  m_iterator = NULL;
264  m_is_iterable_node = false;
265  }
266 
267  void
268  node::clear_columns (bool is_final_clear)
269  {
270  init_ordinality ();
271  for (size_t i = 0; i < m_output_columns_size; ++i)
272  {
273  m_output_columns[i].clear_xasl (is_final_clear);
274  }
275  }
276 
277  void
278  node::clear_iterators (bool is_final_clear)
279  {
280  if (is_final_clear)
281  {
282  db_json_delete_json_iterator (m_iterator);
283  }
284  else
285  {
286  db_json_clear_json_iterator (m_iterator);
287  }
288 
289  for (size_t i = 0; i < m_nested_nodes_size; ++i)
290  {
291  m_nested_nodes[i].clear_iterators (is_final_clear);
292  }
293  }
294 
295  void
296  node::clear_xasl (bool is_final_clear /* = true */)
297  {
298  clear_columns (is_final_clear);
299 
300  for (size_t i = 0; i < m_nested_nodes_size; ++i)
301  {
302  m_nested_nodes[i].clear_xasl (is_final_clear);
303  }
304  }
305 
306  void
308  {
309  if (m_is_iterable_node)
310  {
311  m_iterator = db_json_create_iterator (DB_JSON_TYPE::DB_JSON_ARRAY);
312  }
313  }
314 
315  void
317  {
318  m_ordinality = 1;
319  }
320 
322  {
323  init ();
324  }
325 
326  void
328  {
329  m_root_node = NULL;
330  m_json_reguvar = NULL;
331  m_node_count = 0;
332  }
333 
334  void
335  spec_node::clear_xasl (bool is_final_clear /* = true */)
336  {
337  // todo reguvar
338  m_root_node->clear_xasl (is_final_clear);
339  }
340 
341  } // namespace json_table
342 } // namespace cubxasl
void clear_xasl(bool is_final_clear=true)
#define NO_ERROR
Definition: error_code.h:46
int evaluate_ordinality(size_t ordinality)
void db_json_delete_json_iterator(JSON_ITERATOR *&json_itr)
Definition: db_json.cpp:978
#define ASSERT_ERROR()
int evaluate_extract(const JSON_DOC &input)
struct db_value * m_default_value
void db_make_json_from_doc_store_and_release(DB_VALUE &value, JSON_DOC_STORE &doc_store)
Definition: db_json.cpp:3333
JSON_ITERATOR * db_json_create_iterator(const DB_JSON_TYPE &type)
Definition: db_json.cpp:963
json_table_column_behavior m_on_empty
#define ER_FAILED
Definition: error_code.h:47
void clear_xasl(bool is_final_clear=true)
int db_json_extract_document_from_path(const JSON_DOC *document, const std::string &path, JSON_DOC_STORE &result, bool allow_wildcards)
Definition: db_json.cpp:1153
#define ER_JSON_TABLE_ON_ERROR_INCOMP_DOMAIN
Definition: error_code.h:1592
int trigger_on_error(const JSON_DOC &input, const TP_DOMAIN_STATUS &status_cast, db_value &value_out)
enum tp_domain_status TP_DOMAIN_STATUS
json_table_column_behavior m_on_error
int db_make_short(DB_VALUE *value, const DB_C_SHORT num)
void er_set(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
#define assert(x)
static int input()
Definition: cnvlex.c:1661
json_table_column_function m_function
int evaluate_exists(const JSON_DOC &input)
TP_DOMAIN_STATUS tp_value_cast(const DB_VALUE *src, DB_VALUE *dest, const TP_DOMAIN *desired_domain, bool implicit_coercion)
#define TP_DOMAIN_TYPE(dom)
#define NULL
Definition: freelistheap.h:34
struct pr_type * type
Definition: object_domain.h:76
const char * pr_type_name(DB_TYPE id)
int pr_clear_value(DB_VALUE *value)
int trigger_on_empty(db_value &value_out)
bool db_value_is_null(const DB_VALUE *value)
#define ARG_FILE_LINE
Definition: error_manager.h:44
int evaluate(const JSON_DOC &input, size_t ordinality)
int pr_clone_value(const DB_VALUE *src, DB_VALUE *dest)
void clear_columns(bool is_final_clear)
void db_json_clear_json_iterator(JSON_ITERATOR *&json_itr)
Definition: db_json.cpp:985
enum json_table_column_behavior_type m_behavior
void er_clear(void)
int i
Definition: dynamic_load.c:954
int db_make_null(DB_VALUE *value)
DB_TYPE id
void clear_xasl(bool is_final_clear=true)
int db_json_contains_path(const JSON_DOC *document, const std::vector< std::string > &paths, bool find_all, bool &result)
Definition: db_json.cpp:1265
int db_make_int(DB_VALUE *value, const int num)
void clear_iterators(bool is_final_clear)
#define ER_JSON_TABLE_ON_EMPTY_ERROR
Definition: error_code.h:1591
char * db_json_get_raw_json_body_from_document(const JSON_DOC *doc)
Definition: db_json.cpp:1357