CUBRID Engine  latest
broker_log_converter.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 /*
21  * broker_log_converter.c -
22  */
23 
24 #ident "$Id$"
25 
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #if !defined(WINDOWS)
30 #include <unistd.h>
31 #endif
32 
33 #include "cubrid_getopt.h"
34 #include "cas_common.h"
35 #include "cas_cci.h"
36 #include "broker_log_util.h"
37 
38 static int get_args (int argc, char *argv[]);
39 static int open_file (char *infilename, char *outfilename, FILE ** infp, FILE ** outfp);
40 static int log_converter (FILE * infp, FILE * outfp);
41 static void close_file (FILE * infp, FILE * outfp);
42 static int log_bind_value (char *str, int bind_len, int lineno, FILE * outfp);
43 
44 static char add_query_info = 0;
45 static char add_query_id = 0;
46 static char *infilename = NULL;
47 
48 int
49 main (int argc, char *argv[])
50 {
51  int start_arg;
52  char *outfilename = NULL;
53  FILE *outfp, *infp;
54  int res;
55 
56  if ((start_arg = get_args (argc, argv)) < 0)
57  return -1;
58 
59  infilename = argv[start_arg];
60  if (start_arg + 1 <= argc)
61  outfilename = argv[start_arg + 1];
62 
63  if (open_file (infilename, outfilename, &infp, &outfp) < 0)
64  return -1;
65 
66  res = log_converter (infp, outfp);
67 
68  close_file (infp, outfp);
69  return res;
70 }
71 
72 static int
73 log_converter (FILE * infp, FILE * outfp)
74 {
75  char *linebuf;
76  char query_flag = 0;
77  int lineno = 0;
78  char *msg_p;
79  T_STRING *linebuf_tstr = NULL;
80  int execute_flag = 0;
81  int prepare_flag = 0;
82  char in_execute = 0;
83  int exec_h_id = 0;
84  int bind_len = 0;
85  int query_id = 0;
86 
87  linebuf_tstr = t_string_make (1000);
88  if (linebuf_tstr == NULL)
89  {
90  fprintf (stderr, "malloc error\n");
91  goto error;
92  }
93 
94  while (1)
95  {
96  if (ut_get_line (infp, linebuf_tstr, NULL, NULL) < 0)
97  {
98  fprintf (stderr, "malloc error\n");
99  goto error;
100  }
101  if (t_string_len (linebuf_tstr) <= 0)
102  break;
103  linebuf = t_string_str (linebuf_tstr);
104  lineno++;
105 
106  if (linebuf[strlen (linebuf) - 1] == '\n')
107  linebuf[strlen (linebuf) - 1] = '\0';
108 
109  if (is_cas_log (linebuf))
110  {
111  if (query_flag)
112  {
113  fprintf (outfp, "\n");
114  fprintf (outfp, "P %d %d\n", exec_h_id, prepare_flag);
115  }
116  query_flag = 0;
117 
118  msg_p = get_msg_start_ptr (linebuf);
119  if (strncmp (msg_p, "execute", 7) == 0)
120  {
121  msg_p = ut_get_execute_type (msg_p, &prepare_flag, &execute_flag);
122  if (msg_p == NULL)
123  {
124  in_execute = 0;
125  continue;
126  }
127  if (strncmp (msg_p, "srv_h_id ", 9) == 0)
128  {
129  char *endp;
130  int result = 0;
131 
132  in_execute = 1;
133  msg_p += 9;
134 
135  result = str_to_int32 (&exec_h_id, &endp, msg_p, 10);
136  if (result != 0)
137  {
138  in_execute = 0;
139  continue;
140  }
141  msg_p = endp + 1;
142 
143  fprintf (outfp, "Q ");
144  if (add_query_info == 1 && prepare_flag != 0x40)
145  {
146  fprintf (outfp, "/* %s */ ", infilename);
147  }
148  if (add_query_id == 1)
149  {
150  fprintf (outfp, "/* QUERY_ID %d */ ", query_id++);
151  }
152 
153  fprintf (outfp, "%s%c", msg_p, CAS_RUN_NEW_LINE_CHAR);
154  query_flag = 1;
155  }
156  else
157  {
158  if (in_execute == 1)
159  {
160  fprintf (outfp, "E %d %d\n", exec_h_id, execute_flag);
161  fprintf (outfp, "C %d\n", exec_h_id);
162  fprintf (outfp, "T\n");
163  }
164  in_execute = 0;
165  }
166  }
167  else if (strncmp (msg_p, "bind ", 5) == 0)
168  {
169  bind_len = t_string_bind_len (linebuf_tstr);
170  if (log_bind_value (msg_p, bind_len, lineno, outfp) < 0)
171  goto error;
172  }
173  }
174  else if (query_flag)
175  {
176  fprintf (outfp, "%s%c ", linebuf, CAS_RUN_NEW_LINE_CHAR);
177  }
178  }
179 
180  FREE_MEM (linebuf_tstr);
181  return 0;
182 
183 error:
184  FREE_MEM (linebuf_tstr);
185  return -1;
186 }
187 
188 static int
189 open_file (char *infilename, char *outfilename, FILE ** infp, FILE ** outfp)
190 {
191  if (strcmp (infilename, "-") == 0)
192  {
193  *infp = stdin;
194  }
195  else
196  {
197  *infp = fopen (infilename, "r");
198  if (*infp == NULL)
199  {
200  fprintf (stderr, "fopen error[%s]\n", infilename);
201  return -1;
202  }
203  }
204 
205  if (outfilename == NULL)
206  {
207  *outfp = stdout;
208  }
209  else
210  {
211  *outfp = fopen (outfilename, "w");
212  if (*outfp == NULL)
213  {
214  fprintf (stderr, "fopen error[%s]\n", outfilename);
215  return -1;
216  }
217  }
218 
219  return 0;
220 }
221 
222 static void
223 close_file (FILE * infp, FILE * outfp)
224 {
225  if (infp != stdin)
226  fclose (infp);
227 
228  fflush (outfp);
229  if (outfp != stdout)
230  fclose (outfp);
231 }
232 
233 static int
234 log_bind_value (char *str, int bind_len, int lineno, FILE * outfp)
235 {
236  char *p, *q, *r;
237  char *value_p;
238  int type;
239 
240  p = strchr (str, ':');
241  if (p == NULL)
242  {
243  fprintf (stderr, "log error [line:%d]\n", lineno);
244  return -1;
245  }
246  p += 2;
247  q = strchr (p, ' ');
248  if (q == NULL)
249  {
250  if (strcmp (p, "NULL") == 0)
251  {
252  value_p = (char *) "";
253  }
254  else
255  {
256  fprintf (stderr, "log error [line:%d]\n", lineno);
257  return -1;
258  }
259  }
260  else
261  {
262  if (bind_len > 0)
263  {
264  r = strchr (q, ')');
265  if (r == NULL)
266  {
267  fprintf (stderr, "log error [line:%d]\n", lineno);
268  return -1;
269  }
270  *q = '\0';
271  *r = '\0';
272  value_p = r + 1;
273  }
274  else
275  {
276  *q = '\0';
277  value_p = q + 1;
278  }
279  }
280 
281  if (strcmp (p, "NULL") == 0)
282  {
283  type = CCI_U_TYPE_NULL;
284  }
285  else if (strcmp (p, "CHAR") == 0)
286  {
287  type = CCI_U_TYPE_CHAR;
288  }
289  else if (strcmp (p, "VARCHAR") == 0)
290  {
291  type = CCI_U_TYPE_STRING;
292  }
293  else if (strcmp (p, "NCHAR") == 0)
294  {
295  type = CCI_U_TYPE_NCHAR;
296  }
297  else if (strcmp (p, "VARNCHAR") == 0)
298  {
299  type = CCI_U_TYPE_VARNCHAR;
300  }
301  else if (strcmp (p, "BIT") == 0)
302  {
303  type = CCI_U_TYPE_BIT;
304  }
305  else if (strcmp (p, "VARBIT") == 0)
306  {
307  type = CCI_U_TYPE_VARBIT;
308  }
309  else if (strcmp (p, "NUMERIC") == 0)
310  {
311  type = CCI_U_TYPE_NUMERIC;
312  }
313  else if (strcmp (p, "UBIGINT") == 0)
314  {
315  type = CCI_U_TYPE_UBIGINT;
316  }
317  else if (strcmp (p, "BIGINT") == 0)
318  {
319  type = CCI_U_TYPE_BIGINT;
320  }
321  else if (strcmp (p, "UINT") == 0)
322  {
323  type = CCI_U_TYPE_UINT;
324  }
325  else if (strcmp (p, "INT") == 0)
326  {
327  type = CCI_U_TYPE_INT;
328  }
329  else if (strcmp (p, "USHORT") == 0)
330  {
331  type = CCI_U_TYPE_USHORT;
332  }
333  else if (strcmp (p, "SHORT") == 0)
334  {
335  type = CCI_U_TYPE_SHORT;
336  }
337  else if (strcmp (p, "MONETARY") == 0)
338  {
339  type = CCI_U_TYPE_MONETARY;
340  }
341  else if (strcmp (p, "FLOAT") == 0)
342  {
343  type = CCI_U_TYPE_FLOAT;
344  }
345  else if (strcmp (p, "DOUBLE") == 0)
346  {
347  type = CCI_U_TYPE_DOUBLE;
348  }
349  else if (strcmp (p, "DATE") == 0)
350  {
351  type = CCI_U_TYPE_DATE;
352  }
353  else if (strcmp (p, "TIME") == 0)
354  {
355  type = CCI_U_TYPE_TIME;
356  }
357  else if (strcmp (p, "TIMESTAMP") == 0)
358  {
359  type = CCI_U_TYPE_TIMESTAMP;
360  }
361  else if (strcmp (p, "DATETIME") == 0)
362  {
363  type = CCI_U_TYPE_DATETIME;
364  }
365  else if (strcmp (p, "TIMESTAMPTZ") == 0)
366  {
367  type = CCI_U_TYPE_TIMESTAMPTZ;
368  }
369  else if (strcmp (p, "DATETIMETZ") == 0)
370  {
371  type = CCI_U_TYPE_DATETIMETZ;
372  }
373  else if (strcmp (p, "OBJECT") == 0)
374  {
375  type = CCI_U_TYPE_OBJECT;
376  }
377  else if (strcmp (p, "BLOB") == 0)
378  {
379  type = CCI_U_TYPE_NULL;
380  fprintf (stderr, "%s\nBLOB type is not implemented. Replaced with NULL\n", value_p);
381  value_p = (char *) "";
382  bind_len = 0;
383  /* type = CCI_U_TYPE_BLOB; */
384  }
385  else if (strcmp (p, "CLOB") == 0)
386  {
387  type = CCI_U_TYPE_NULL;
388  fprintf (stderr, "%s\nCLOB type is not implemented. Replaced with NULL\n", value_p);
389  value_p = (char *) "";
390  bind_len = 0;
391  /* type = CCI_U_TYPE_CLOB; */
392  }
393  else if (strcmp (p, "ENUM") == 0)
394  {
395  type = CCI_U_TYPE_ENUM;
396  }
397  else if (strcmp (p, "JSON") == 0)
398  {
399  type = CCI_U_TYPE_JSON;
400  }
401  else
402  {
403  fprintf (stderr, "log error [line:%d]\n", lineno);
404  return -1;
405  }
406 
407  if (bind_len > 0)
408  {
409  fprintf (outfp, "B %d %d %s\n", type, bind_len, value_p);
410  }
411  else
412  {
413  fprintf (outfp, "B %d %s\n", type, value_p);
414  }
415  return 0;
416 }
417 
418 static int
419 get_args (int argc, char *argv[])
420 {
421  int c;
422 
423  while ((c = getopt (argc, argv, "iq")) != EOF)
424  {
425  switch (c)
426  {
427  case 'q':
428  add_query_info = 1;
429  break;
430  case 'i':
431  add_query_id = 1;
432  break;
433  default:
434  goto usage;
435  }
436  }
437 
438  if (optind + 1 >= argc)
439  goto usage;
440 
441  return optind;
442 
443 usage:
444  fprintf (stderr,
445  "usage : %s [OPTION] infile outfile\n" "\n" "valid options:\n"
446  " -i add a unique id to each query as a comment.\n", argv[0]);
447  return -1;
448 }
DllImport int optind
static void close_file(FILE *infp, FILE *outfp)
char * get_msg_start_ptr(char *linebuf)
int getopt(int, char *const *, const char *)
int argc
Definition: dynamic_load.c:951
static char add_query_id
static int log_converter(FILE *infp, FILE *outfp)
int t_string_len(T_STRING *t_str)
char * t_string_str(T_STRING *t_str)
int main(int argc, char *argv[])
int is_cas_log(char *str)
#define NULL
Definition: freelistheap.h:34
int str_to_int32(int *ret_p, char **end_p, const char *str_p, int base)
Definition: porting.c:2346
int t_string_bind_len(T_STRING *t_str)
#define FREE_MEM(PTR)
Definition: cas_common.h:58
static char add_query_info
static char * infilename
static void error(const char *msg)
Definition: gencat.c:331
const char ** argv
Definition: dynamic_load.c:952
#define strlen(s1)
Definition: intl_support.c:43
T_STRING * t_string_make(int init_size)
char * ut_get_execute_type(char *msg_p, int *prepare_flag, int *execute_flag)
static int get_args(int argc, char *argv[])
void usage(void)
static int open_file(char *infilename, char *outfilename, FILE **infp, FILE **outfp)
#define CAS_RUN_NEW_LINE_CHAR
int ut_get_line(FILE *fp, T_STRING *t_str, char **out_str, int *lineno)
const char ** p
Definition: dynamic_load.c:945
static int log_bind_value(char *str, int bind_len, int lineno, FILE *outfp)