CUBRID Engine  latest
load_error_handler.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  * load_error_handler.cpp - Error handling class for loaddb functionality
21  */
22 
23 #include "load_error_handler.hpp"
24 
25 #include "load_driver.hpp"
26 #include "load_sa_loader.hpp"
27 #if defined (SERVER_MODE)
28 #include "load_session.hpp"
29 #endif
30 #include "message_catalog.h"
31 #include "thread_manager.hpp"
32 #include "error_manager.h"
33 
34 #include <algorithm>
35 
36 namespace cubload
37 {
38 
39 #if defined (SERVER_MODE)
40  error_handler::error_handler (session &session)
41  : m_current_line_has_error (false)
42  , m_session (session)
43  {
44  m_syntax_check = m_session.get_args ().syntax_check;
45  }
46 #endif
47 
48  int
50  {
51  cubthread::entry &thread_ref = cubthread::get_entry ();
52  assert (thread_ref.m_loaddb_driver != NULL);
53 
54  // We actually don't need the increment since we already incremented during update_start_line
55  return thread_ref.m_loaddb_driver->get_start_line ();
56  }
57 
58  int
60  {
61  cubthread::entry &thread_ref = cubthread::get_entry ();
62  assert (thread_ref.m_loaddb_driver != NULL);
63 
64  return thread_ref.m_loaddb_driver->get_scanner ().lineno ();
65  }
66 
67  char *
69  {
71  }
72 
73  void
75  {
76  if (!is_last_error_filtered ())
77  {
78  std::string empty;
79  log_error_message (empty, true);
80  }
81  }
82 
83  void
84  error_handler::on_syntax_failure (bool use_scanner_line)
85  {
86 #if defined (SERVER_MODE)
87  if (m_syntax_check)
88  {
89  // just log er_msg ()
90  std::string er_msg;
91  log_error_message (er_msg, false, use_scanner_line);
92  er_clear ();
93  return;
94  }
95 #endif
96  if (!is_last_error_filtered ())
97  {
98  std::string empty;
99  log_error_message (empty, true, use_scanner_line);
100  }
101  }
102 
103  void
104  error_handler::log_error_message (std::string &err_msg, bool fail, bool is_syntax_error)
105  {
106 #if defined (SERVER_MODE)
108  {
109  // if there is an error set via er_set then report it as well
110  int lineno;
111  if (!is_syntax_error)
112  {
113  lineno = get_driver_lineno ();
114  }
115  else
116  {
117  lineno = get_scanner_lineno ();
118  }
119 
120  err_msg.append (format (get_message_from_catalog (LOADDB_MSG_LINE), lineno));
121  err_msg.append (std::string (er_msg ()));
122  err_msg.append ("\n");
123  }
124 
125  m_session.on_error (err_msg);
126 
127  if (fail)
128  {
129  m_session.fail ();
130  }
131 #elif defined (SA_MODE)
132  if (fail)
133  {
135  }
136  else
137  {
139  }
140 
141  fprintf (stderr, "%s", err_msg.c_str ());
142 #endif
143  }
144 
145 #if defined (SERVER_MODE)
146  bool
147  error_handler::is_error_filtered (int err_id)
148  {
149  std::vector<int> ignored_errors = m_session.get_args ().m_ignored_errors;
150  bool is_filtered = false;
151 
152  is_filtered = std::find (ignored_errors.begin (), ignored_errors.end (), err_id) != ignored_errors.end ();
153 
154  return is_filtered;
155  }
156 #endif //SERVER_MODE
157 
158  bool
160  {
161 #if defined (SERVER_MODE)
162  int err = er_errid ();
163 
164  bool is_filtered = is_error_filtered (err);
165 
166  // Clear the error if it is filtered
167  if (is_filtered)
168  {
169  er_clear ();
170  set_error_on_current_line (true);
171  }
172 
173  return is_filtered;
174 #endif
175  return false;
176  }
177 
178  bool
180  {
181  return m_current_line_has_error;
182  }
183 
184  void
186  {
187  m_current_line_has_error = has_error;
188  }
189 
190 } // namespace cubload
#define NO_ERROR
Definition: error_code.h:46
void on_syntax_failure(bool use_scanner_line=false)
void ldr_increment_fails()
static int fail(const char *message)
MSGCAT_LOADDB_MSG
Definition: utility.h:438
int er_errid(void)
void ldr_increment_err_total()
static char * get_message_from_catalog(MSGCAT_LOADDB_MSG msg_id)
scanner & get_scanner()
int er_get_severity(void)
#define assert(x)
cubload::driver * m_loaddb_driver
#define NULL
Definition: freelistheap.h:34
const char * er_msg(void)
#define err(fd,...)
Definition: porting.h:431
void log_error_message(std::string &err_msg, bool fail, bool is_syntax_error=false)
bool er_has_error(void)
void er_clear(void)
void set_error_on_current_line(bool has_error)
char * msgcat_message(int cat_id, int set_id, int msg_id)
entry & get_entry(void)
#define MSGCAT_CATALOG_UTILS