CUBRID Engine  latest
unloaddb.c
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  * unloaddb.c - emits database object definitions in object loader format
21  */
22 
23 #ident "$Id$"
24 
25 #include "config.h"
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 
31 #include "porting.h"
32 #include "authenticate.h"
33 #include "db.h"
34 #include "extract_schema.hpp"
35 #include "message_catalog.h"
36 #include "environment_variable.h"
37 #include "printer.hpp"
38 #include "schema_manager.h"
39 #include "locator_cl.h"
40 #include "unloaddb.h"
41 #include "load_object.h"
42 #include "utility.h"
43 #include "util_func.h"
44 
46 const char *output_dirname = NULL;
48 FILE *output_file = NULL;
49 TEXT_OUTPUT object_output = { NULL, NULL, 0, 0, NULL };
50 
52 int page_size = 4096;
53 int cached_pages = 100;
54 int64_t est_size = 0;
56 int debug_flag = 0;
57 bool verbose_flag = false;
58 bool include_references = false;
59 
60 bool required_class_only = false;
61 bool datafile_per_class = false;
64 
65 int lo_count = 0;
66 
68 bool do_schema = false;
69 bool do_objects = false;
70 bool ignore_err_flag = false;
71 
72 /*
73  * unload_usage() - print an usage of the unload-utility
74  * return: void
75  */
76 static void
77 unload_usage (const char *argv0)
78 {
79  const char *exec_name;
80 
81  exec_name = basename ((char *) argv0);
82  fprintf (stderr, msgcat_message (MSGCAT_CATALOG_UTILS, MSGCAT_UTIL_SET_UNLOADDB, 60), exec_name);
84 }
85 
86 /*
87  * unloaddb - main function
88  * return: 0 if successful, non zero if error.
89  * argc(in): number of command line arguments
90  * argv(in): array containing command line arguments
91  */
92 int
94 {
95  char output_filename_schema[PATH_MAX * 2];
96  UTIL_ARG_MAP *arg_map = arg->arg_map;
97  const char *exec_name = arg->command_name;
98  char er_msg_file[PATH_MAX];
99  int error;
100  int status = 0;
101  int i;
102  char *user, *password;
103  int au_save;
104  EMIT_STORAGE_ORDER order;
105  extract_context unload_context;
106 
107  if (utility_get_option_string_table_size (arg_map) != 1)
108  {
109  unload_usage (arg->argv0);
110  return -1;
111  }
112 
127  user = utility_get_option_string_value (arg_map, UNLOAD_USER_S, 0);
128  password = utility_get_option_string_value (arg_map, UNLOAD_PASSWORD_S, 0);
130  {
131  order = FOLLOW_STORAGE_ORDER;
132  }
133  else
134  {
135  order = FOLLOW_ATTRIBUTE_ORDER;
136  }
137 
138  /* depreciated */
140 
141 
142  if (database_name == NULL)
143  {
144  status = 1;
145  /* TODO: print usage */
147  goto end;
148  }
149 
150  if (!output_prefix)
151  {
153  }
154 
155  /* create here the first filename to raise error early in case output file is incorrect */
156  if (create_filename_schema (output_dirname, output_prefix, output_filename_schema,
157  sizeof (output_filename_schema)) != 0)
158  {
160  goto end;
161  }
162 
163  /* error message log file */
164  snprintf (er_msg_file, sizeof (er_msg_file) - 1, "%s_%s.err", database_name, exec_name);
165  er_init (er_msg_file, ER_NEVER_EXIT);
166 
168 
169  /*
170  * Open db
171  */
172  if (user == NULL || user[0] == '\0')
173  {
174  user = (char *) "DBA";
175  }
176 
177  error = db_restart_ex (exec_name, database_name, user, password, NULL, DB_CLIENT_TYPE_ADMIN_UTILITY);
178  if (error == NO_ERROR)
179  {
180  /* pass */
181  }
182  else if (password == NULL && db_error_code () == ER_AU_INVALID_PASSWORD)
183  {
184  /* console input a password */
185  password =
187  error = db_restart_ex (exec_name, database_name, user, password, NULL, DB_CLIENT_TYPE_ADMIN_UTILITY);
188  if (error != NO_ERROR)
189  {
190  PRINT_AND_LOG_ERR_MSG ("%s: %s\n", exec_name, db_error_string (3));
191  status = error;
192  goto end;
193  }
194  }
195  else if (error != NO_ERROR)
196  {
197  /* error */
198  PRINT_AND_LOG_ERR_MSG ("%s: %s\n", exec_name, db_error_string (3));
199  status = error;
200  goto end;
201  }
202 
204 
205  if (!status)
206  {
208  }
209 
210  if (!input_filename)
211  {
212  required_class_only = false;
213  }
215  {
216  include_references = false;
217  fprintf (stdout, "warning: '-ir' option is ignored.\n");
218  fflush (stdout);
219  }
220 
222  if (input_filename)
223  {
224  /* It may not be needed */
226  {
228  status = 1;
229  goto end;
230  }
231  }
232 
233  if (class_table == NULL)
234  {
236  status = 1;
237  goto end;
238  }
239 
240  req_class_table = (DB_OBJECT **) malloc (DB_SIZEOF (void *) * class_table->num);
241  if (req_class_table == NULL)
242  {
244  status = 1;
245  goto end;
246  }
247 
248  for (i = 0; i < class_table->num; ++i)
249  {
250  req_class_table[i] = NULL;
251  }
252 
253  if (get_requested_classes (input_filename, req_class_table) != 0)
254  {
256  status = 1;
257  goto end;
258  }
259 
260  if (input_filename)
261  {
262  int error;
263 
264  for (i = 0; req_class_table[i]; i++)
265  {
266  error = au_fetch_class (req_class_table[i], NULL, AU_FETCH_READ, AU_SELECT);
267  if (error != NO_ERROR)
268  {
269  /* A required class is not granted. */
270  MOBJ object = NULL;
271 
272  ws_find (req_class_table[i], &object);
273  if (object != NULL)
274  {
275  PRINT_AND_LOG_ERR_MSG ("%s: %s\n", sm_ch_name (object), db_error_string (3));
276  }
277  status = 1;
278  }
279  }
280 
281  if (status != 0)
282  {
283  goto end;
284  }
285  }
286 
287  if (!status && (do_schema || !do_objects))
288  {
289  char indexes_output_filename[PATH_MAX * 2];
290  char trigger_output_filename[PATH_MAX * 2];
291 
292  if (create_filename_schema (output_dirname, output_prefix, output_filename_schema,
293  sizeof (output_filename_schema)) != 0)
294  {
296  goto end;
297  }
298 
299  if (create_filename_trigger (output_dirname, output_prefix, trigger_output_filename,
300  sizeof (trigger_output_filename)) != 0)
301  {
303  goto end;
304  }
305 
306  if (create_filename_indexes (output_dirname, output_prefix, indexes_output_filename,
307  sizeof (indexes_output_filename)) != 0)
308  {
310  goto end;
311  }
312 
313  /* do authorization as well in extractschema () */
314  unload_context.do_auth = 1;
315  unload_context.storage_order = order;
316  unload_context.exec_name = exec_name;
317  if (extract_classes_to_file (unload_context, output_filename_schema) != 0)
318  {
319  status = 1;
320  }
321 
322  if (!status && extract_triggers_to_file (unload_context, trigger_output_filename) != 0)
323  {
324  status = 1;
325  }
326 
327  if (!status && extract_indexes_to_file (unload_context, indexes_output_filename) != 0)
328  {
329  status = 1;
330  }
331 
332  unload_context.clear_schema_workspace ();
333  }
334 
335  AU_SAVE_AND_ENABLE (au_save);
336  if (!status && (do_objects || !do_schema))
337  {
338  if (extract_objects (exec_name, output_dirname, output_prefix))
339  {
340  status = 1;
341  }
342  }
343  AU_RESTORE (au_save);
344 
345  /* if an error occur, print error message */
346  if (status)
347  {
348  if (db_error_code () != NO_ERROR)
349  {
350  PRINT_AND_LOG_ERR_MSG ("%s: %s\n", exec_name, db_error_string (3));
351  }
352  }
353 
354  /*
355  * Shutdown db
356  */
357  error = db_shutdown ();
358  if (error != NO_ERROR)
359  {
360  PRINT_AND_LOG_ERR_MSG ("%s: %s\n", exec_name, db_error_string (3));
361  status = error;
362  }
363 
364 end:
365  if (class_table)
366  {
367  locator_free_list_mops (class_table);
368  }
369  if (req_class_table)
370  {
371  free_and_init (req_class_table);
372  }
373 
374  unload_context.clear_schema_workspace ();
375 
376  return status;
377 }
static void unload_usage(const char *argv0)
Definition: unloaddb.c:77
#define UNLOAD_INPUT_CLASS_ONLY_S
Definition: utility.h:1282
int get_requested_classes(const char *input_filename, DB_OBJECT *class_list[])
char * hash_filename
Definition: unloaddb.c:55
int page_size
Definition: unloaddb.c:52
#define NO_ERROR
Definition: error_code.h:46
int debug_flag
Definition: unloaddb.c:56
EMIT_STORAGE_ORDER storage_order
#define UNLOAD_HASH_FILE_S
Definition: utility.h:1298
#define PRINT_AND_LOG_ERR_MSG(...)
Definition: util_func.h:49
#define AU_SAVE_AND_ENABLE(save)
Definition: authenticate.h:119
int sysprm_set_force(const char *pname, const char *pvalue)
char * MOBJ
Definition: work_space.h:174
#define UNLOAD_INPUT_CLASS_FILE_S
Definition: utility.h:1278
int extract_triggers_to_file(extract_context &ctxt, const char *output_filename)
char * output_prefix
Definition: unloaddb.c:67
#define UNLOAD_VERBOSE_S
Definition: utility.h:1300
int db_shutdown(void)
Definition: db_admin.c:964
TEXT_OUTPUT object_output
Definition: unloaddb.c:49
#define UNLOAD_OUTPUT_PATH_S
Definition: utility.h:1290
bool include_references
Definition: unloaddb.c:58
char * database_name
Definition: unloaddb.c:45
#define AU_RESTORE(save)
Definition: authenticate.h:133
int extract_classes_to_file(extract_context &ctxt, const char *output_filename)
FILE * output_file
Definition: unloaddb.c:48
void locator_free_list_mops(LIST_MOPS *mops)
Definition: locator_cl.c:2978
LIST_MOPS * locator_get_all_mops(MOP class_mop, DB_FETCH_MODE purpose, LC_FETCH_VERSION_TYPE *force_fetch_version_type)
Definition: locator_cl.c:2839
int util_log_write_errid(int message_id,...)
Definition: util_func.c:468
int create_filename_trigger(const char *output_dirname, const char *output_prefix, char *output_filename_p, const size_t filename_size)
int db_set_lock_timeout(int seconds)
Definition: db_admin.c:1496
int er_init(const char *msglog_filename, int exit_ask)
const char * output_dirname
Definition: unloaddb.c:46
bool datafile_per_class
Definition: unloaddb.c:61
int utility_get_option_int_value(UTIL_ARG_MAP *arg_map, int arg_ch)
Definition: util_common.c:227
#define UNLOAD_OUTPUT_PREFIX_S
Definition: utility.h:1296
#define UNLOAD_ESTIMATED_SIZE_S
Definition: utility.h:1286
int db_restart_ex(const char *program, const char *db_name, const char *db_user, const char *db_password, const char *preferred_hosts, int client_type)
Definition: db_admin.c:934
#define UNLOAD_KEEP_STORAGE_ORDER_S
Definition: utility.h:1314
const char * sm_ch_name(const MOBJ clobj)
bool utility_get_option_bool_value(UTIL_ARG_MAP *arg_map, int arg_ch)
Definition: util_common.c:245
int prm_get_integer_value(PARAM_ID prm_id)
int au_fetch_class(MOP op, SM_CLASS **class_ptr, AU_FETCHMODE fetchmode, DB_AUTH type)
int db_error_code(void)
Definition: db_admin.c:2143
#define UNLOAD_DATAFILE_PER_CLASS_S
Definition: utility.h:1308
int create_filename_indexes(const char *output_dirname, const char *output_prefix, char *output_filename_p, const size_t filename_size)
const char * db_error_string(int level)
Definition: db_admin.c:2116
#define DB_SIZEOF(val)
Definition: memory_alloc.h:54
#define NULL
Definition: freelistheap.h:34
int extract_objects(const char *exec_name, const char *output_dirname, const char *output_prefix)
int extract_indexes_to_file(extract_context &ctxt, const char *output_filename)
char * input_filename
Definition: unloaddb.c:47
#define UNLOAD_USE_DELIMITER_S
Definition: utility.h:1302
int utility_get_option_string_table_size(UTIL_ARG_MAP *arg_map)
Definition: util_common.c:305
int unloaddb(UTIL_FUNCTION_ARG *arg)
Definition: unloaddb.c:93
static void error(const char *msg)
Definition: gencat.c:331
#define ER_AU_INVALID_PASSWORD
Definition: error_code.h:235
int locator_decache_all_lock_instances(MOP class_mop)
Definition: locator_cl.c:3293
#define UNLOAD_INCLUDE_REFERENCE_S
Definition: utility.h:1280
#define AU_SELECT
Definition: authenticate.h:69
bool verbose_flag
Definition: unloaddb.c:57
UTIL_ARG_MAP * arg_map
Definition: utility.h:1698
#define UNLOAD_CACHED_PAGES_S
Definition: utility.h:1288
#define free_and_init(ptr)
Definition: memory_alloc.h:147
TEXT_OUTPUT * obj_out
Definition: unloaddb.c:51
#define UNLOAD_USER_S
Definition: utility.h:1310
int64_t est_size
Definition: unloaddb.c:54
int cached_pages
Definition: unloaddb.c:53
bool prm_get_bool_value(PARAM_ID prm_id)
#define UNLOAD_DATA_ONLY_S
Definition: utility.h:1294
bool required_class_only
Definition: unloaddb.c:60
int util_log_write_errstr(const char *format,...)
Definition: util_func.c:493
char * basename(const char *path)
Definition: porting.c:1132
int create_filename_schema(const char *output_dirname, const char *output_prefix, char *output_filename_p, const size_t filename_size)
int i
Definition: dynamic_load.c:954
char * msgcat_message(int cat_id, int set_id, int msg_id)
const char * prm_get_name(PARAM_ID prm_id)
bool ignore_err_flag
Definition: unloaddb.c:70
void clear_schema_workspace(void)
bool do_objects
Definition: unloaddb.c:69
int lo_count
Definition: unloaddb.c:65
MOP sm_Root_class_mop
bool do_schema
Definition: unloaddb.c:68
#define OPTION_STRING_TABLE
Definition: utility.h:813
#define UNLOAD_LO_COUNT_S
Definition: utility.h:1284
#define UNLOAD_PASSWORD_S
Definition: utility.h:1312
int ws_find(MOP mop, MOBJ *obj)
Definition: work_space.c:3112
const char * exec_name
DB_OBJECT ** req_class_table
Definition: unloaddb.c:63
const char * command_name
Definition: utility.h:1699
#define UNLOAD_SCHEMA_ONLY_S
Definition: utility.h:1292
char * utility_get_option_string_value(UTIL_ARG_MAP *arg_map, int arg_ch, int index)
Definition: util_common.c:266
EMIT_STORAGE_ORDER
#define MSGCAT_CATALOG_UTILS