CUBRID Engine  latest
broker_log_sql_list.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_sql_list.c -
22  */
23 
24 #ident "$Id$"
25 
26 #include <stdio.h>
27 #include <errno.h>
28 #include <sys/stat.h>
29 #include <stdlib.h>
30 #include <fcntl.h>
31 #include <string.h>
32 
33 #include "cas_common.h"
34 #include "broker_log_sql_list.h"
35 #include "broker_log_util.h"
36 #include "log_top_string.h"
37 
38 #define IS_NEXT_QUERY(STR) (strncmp(STR, "=================", 10) == 0)
39 
40 #define LINE_BUF_SIZE 10000
41 
42 static void sql_info_init (T_SQL_INFO * sql_info);
43 static int sql_info_add (const char *sql, char *sql_tag);
44 static int comp_func (const void *arg1, const void *arg2);
45 static void sql_change_comp_form (char *src, char *dest);
46 
48 static int num_sql_list = 0;
49 
50 int
51 sql_list_make (char *list_file)
52 {
53  FILE *fp = NULL;
54  char *linebuf;
55  T_STRING *sql_buf = NULL;
56  T_STRING *linebuf_tstr = NULL;
57  int lineno = 1;
58  char sql_tag[LINE_BUF_SIZE];
59 
60  sql_buf = t_string_make (1);
61  linebuf_tstr = t_string_make (1000);
62  if (sql_buf == NULL || linebuf_tstr == NULL)
63  {
64  fprintf (stderr, "malloc error\n");
65  goto error;
66  }
67 
68  fp = fopen (list_file, "r");
69  if (fp == NULL)
70  {
71  fprintf (stderr, "%s:%s\n", list_file, strerror (errno));
72  goto error;
73  }
74 
75  if (ut_get_line (fp, linebuf_tstr, &linebuf, &lineno) <= 0)
76  goto error;
77  if (!IS_NEXT_QUERY (linebuf))
78  {
79  fprintf (stderr, "%s,%d:file format error\n", list_file, lineno);
80  goto error;
81  }
82 
83  while (1)
84  {
85  if (ut_get_line (fp, linebuf_tstr, &linebuf, &lineno) <= 0)
86  break;
87 
88  if (linebuf[strlen (linebuf) - 1] == '\n')
89  linebuf[strlen (linebuf) - 1] = '\0';
90 
91  strcpy (sql_tag, linebuf);
92 
93  t_string_clear (sql_buf);
94  while (1)
95  {
96  if (ut_get_line (fp, linebuf_tstr, &linebuf, &lineno) <= 0)
97  break;
98 
99  if (IS_NEXT_QUERY (linebuf))
100  {
101  break;
102  }
103 
104  if (t_string_add (sql_buf, linebuf, (int) strlen (linebuf)) < 0)
105  {
106  fprintf (stderr, "malloc error\n");
107  goto error;
108  }
109  }
110 
111  sql_change_comp_form (t_string_str (sql_buf), t_string_str (sql_buf));
112 
113  if (sql_info_add (t_string_str (sql_buf), sql_tag) < 0)
114  {
115  goto error;
116  }
117  }
118 
119  fclose (fp);
120 
121  qsort (sql_list, num_sql_list, sizeof (T_SQL_INFO), comp_func);
122 
123  t_string_free (sql_buf);
124  t_string_free (linebuf_tstr);
125  return 0;
126 
127 error:
128  t_string_free (sql_buf);
129  t_string_free (linebuf_tstr);
130  if (fp)
131  fclose (fp);
132  return -1;
133 }
134 
135 int
136 sql_info_write (char *src_sql, char *q_name, FILE * fp)
137 {
138  int i;
139  char *sql;
140  T_SQL_INFO tmp_sql_info;
141  T_SQL_INFO *search_p;
142 
143  if (sql_list == NULL)
144  return 0;
145 
146  sql = strdup (src_sql);
147  if (sql == NULL)
148  {
149  fprintf (stderr, "sql_info_write():%s\n", strerror (errno));
150  return 0;
151  }
152  sql_change_comp_form (sql, sql);
153 
154  tmp_sql_info.sql = sql;
155 
156  search_p = (T_SQL_INFO *) bsearch (&tmp_sql_info, sql_list, num_sql_list, sizeof (T_SQL_INFO), comp_func);
157 
158  FREE_MEM (sql);
159 
160  if (search_p == NULL)
161  return 0;
162 
163  for (i = 0; i < search_p->num_file; i++)
164  {
165  fprintf (fp, "%s\n", search_p->filename[i]);
166  }
167 
168  return 1;
169 }
170 
171 static int
172 sql_info_add (const char *sql, char *sql_tag)
173 {
174  int si_idx = -1;
175  int i;
176 
177  for (i = 0; i < num_sql_list; i++)
178  {
179  if (strcmp (sql_list[i].sql, sql) == 0)
180  {
181  si_idx = i;
182  break;
183  }
184  }
185 
186  if (si_idx == -1)
187  {
188  sql_list = (T_SQL_INFO *) REALLOC (sql_list, sizeof (T_SQL_INFO) * (num_sql_list + 1));
189  if (sql_list == NULL)
190  {
191  fprintf (stderr, "%s\n", strerror (errno));
192  return -1;
193  }
194 
195  si_idx = num_sql_list;
196  sql_info_init (&sql_list[si_idx]);
197  sql_list[si_idx].sql = strdup (sql);
198  if (sql_list[si_idx].sql == NULL)
199  {
200  fprintf (stderr, "%s\n", strerror (errno));
201  return -1;
202  }
203  num_sql_list++;
204  }
205 
206  sql_list[si_idx].filename =
207  (char **) REALLOC (sql_list[si_idx].filename, sizeof (char *) * (sql_list[si_idx].num_file + 1));
208  if (sql_list[si_idx].filename == NULL)
209  {
210  fprintf (stderr, "%s\n", strerror (errno));
211  return -1;
212  }
213 
214  sql_list[si_idx].filename[sql_list[si_idx].num_file] = strdup (sql_tag);
215  if (sql_list[si_idx].filename[sql_list[si_idx].num_file] == NULL)
216  {
217  fprintf (stderr, "%s\n", strerror (errno));
218  return -1;
219  }
220  sql_list[si_idx].num_file++;
221  return 0;
222 }
223 
224 static void
226 {
227  memset (sql_info, 0, sizeof (T_SQL_INFO));
228 }
229 
230 static int
231 comp_func (const void *arg1, const void *arg2)
232 {
233  return (strcmp (((T_SQL_INFO *) arg1)->sql, ((T_SQL_INFO *) arg2)->sql));
234 }
235 
236 static void
237 sql_change_comp_form (char *src, char *dest)
238 {
239  int write_space_flag = 0;
240  char *p, *q;
241 
242  for (p = src, q = dest; *p; p++)
243  {
244  if (*p == '\r' || *p == '\n' || *p == '\t' || *p == ' ')
245  {
246  if (write_space_flag)
247  {
248  *q++ = ' ';
249  write_space_flag = 0;
250  }
251  }
252  else
253  {
254  write_space_flag = 1;
255  *q++ = *p;
256  }
257  }
258  *q = '\0';
259 
260  ut_trim (dest);
261 }
int sql_info_write(char *src_sql, char *q_name, FILE *fp)
int sql_list_make(char *list_file)
int t_string_add(T_STRING *t_str, char *str, int str_len)
void t_string_free(T_STRING *t_str)
char * t_string_str(T_STRING *t_str)
static void sql_info_init(T_SQL_INFO *sql_info)
static T_SQL_INFO * sql_list
static int sql_info_add(const char *sql, char *sql_tag)
#define NULL
Definition: freelistheap.h:34
#define FREE_MEM(PTR)
Definition: cas_common.h:58
#define IS_NEXT_QUERY(STR)
static int comp_func(const void *arg1, const void *arg2)
#define LINE_BUF_SIZE
static void error(const char *msg)
Definition: gencat.c:331
#define strlen(s1)
Definition: intl_support.c:43
static void sql_change_comp_form(char *src, char *dest)
int i
Definition: dynamic_load.c:954
T_STRING * t_string_make(int init_size)
char * strdup(const char *str)
Definition: porting.c:901
static int num_sql_list
char * ut_trim(char *str)
void t_string_clear(T_STRING *t_str)
int ut_get_line(FILE *fp, T_STRING *t_str, char **out_str, int *lineno)
const char ** p
Definition: dynamic_load.c:945
#define REALLOC(PTR, SIZE)
Definition: cas_common.h:54