File: test_strip_sql.cpp

package info (click to toggle)
mysql-gui-tools 5.0r14%2BopenSUSE-2.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 116,956 kB
  • ctags: 48,715
  • sloc: sql: 341,918; pascal: 276,698; ansic: 91,020; cpp: 90,451; objc: 33,236; sh: 29,481; yacc: 10,756; xml: 10,589; java: 10,079; php: 2,806; python: 2,092; makefile: 1,783; perl: 4
file content (219 lines) | stat: -rw-r--r-- 7,124 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// test_strip_sql.cpp : Defines the entry point for the console application.
//

#ifdef _WINDOWS_
#include "io.h"
#endif
#include <fcntl.h>
#include "myx_public_interface.h"
#include "myx_qb_public_interface.h"

///////////////////////////////////////////////////////////////////////////////
void print_usage()
{
  printf("usage : test_strip_sql.exe "
     " <unstripped_query_file> <copy_mode> <language> <edited_query_file>\n");
}

///////////////////////////////////////////////////////////////////////////////
int read_file(const char * filename, char * buf, size_t buf_size)
{
  FILE * file= fopen(filename,"rb");
  if (file==NULL)
  {
    fprintf(stderr,"can't open file \"%s\"\n",filename);
    return -1;
  }
  else
  {
    size_t read= fread(buf,1,buf_size-1,file);
    buf[read]= 0;
    return 0;
  }
}

char unstripped_query[10000];
char edited_query[10000];

///////////////////////////////////////////////////////////////////////////////
void test(const char * unstripped_query,
          MYX_Q_SQL_STRIPPED_COPY_MODE copy_mode,
          MYX_Q_SQL_STRIPPED_CODE_LANGUAGE language,
          const char * edited_query)
{  
  printf("---------------------------------------------- unstripped_query :\n"
         "-----------------------------------------------------------------\n"
         "%s\n"
         "-----------------------------------------------------------------\n",
         unstripped_query);

  MYX_Q_SQL_STRIPPED * stripped_sql= myx_strip_embedded_sql(unstripped_query,
                                                            language,
                                                            copy_mode);
  printf("------------------------------------------------ stripped_query :\n"
         "-----------------------------------------------------------------\n"
         "%s\n"
         "-----------------------------------------------------------------\n",
         stripped_sql->query_stripped);
  char * result_text= myx_reconstruct_embedded_sql(stripped_sql,edited_query);

  printf("-------------------------------------------------- edited_query :\n"
         "-----------------------------------------------------------------\n"
         "%s\n"
         "-----------------------------------------------------------------\n",
         edited_query);
  
  printf("-------------------------------------------------------- result :\n"
         "-----------------------------------------------------------------\n"
         "%s\n"
         "-----------------------------------------------------------------\n",
         result_text);
  g_free(result_text);
  myx_free_stripped_sql(stripped_sql);
}

///////////////////////////////////////////////////////////////////////////////
int test_strings(const char * unstripped_query,
                 const char * str_copy_mode,
                 const char * str_language,
                 const char * edited_query)
{
  printf("%s,%s\n",str_copy_mode,str_language);

  MYX_Q_SQL_STRIPPED_CODE_LANGUAGE language= 
    !strcmp(str_language,"PHP")  ? MYX_QSSCL_PHP  :
    !strcmp(str_language,"JAVA") ? MYX_QSSCL_JAVA :
                                  (MYX_Q_SQL_STRIPPED_CODE_LANGUAGE)-1;
  if (language==-1)
  {
    fprintf(stderr,"wrong language \"%s\", "
                   "should be MYX_QSSCL_PHP | MYX_QSSCL_JAVA\n",
                   str_language);
    return -1;
  }

  MYX_Q_SQL_STRIPPED_COPY_MODE copy_mode=
    !strcmp(str_copy_mode,"QB_MENU")    ? MYX_QSSCM_QB_MENU :
    !strcmp(str_copy_mode,"KEYSTROKES") ? MYX_QSSCM_KEYSTROKES :
    !strcmp(str_copy_mode,"MESSAGE")    ? MYX_QSSCM_MESSAGE :
                                         (MYX_Q_SQL_STRIPPED_COPY_MODE)-1;
  if (copy_mode==-1)
  {
    fprintf(stderr,"wrong copy_mode \"%s\", "
                   "should be MYX_QSSCM_QB_MENU | MYX_QSSCM_KEYSTROKES "
                   "| MYX_QSSCM_MESSAGE\n", str_copy_mode);
    return -1;
  }

#ifdef _WINDOWS_
  _setmode( _fileno( stdout ), _O_BINARY );
#endif
  test(unstripped_query,copy_mode,language,edited_query);

  return 0;
}

///////////////////////////////////////////////////////////////////////////////
int test_files(const char * unstripped_query_file,
               const char * str_copy_mode,
               const char * str_language,
               const char * edited_query_file)
{
  int res;
  if ((res= read_file(unstripped_query_file,
                      unstripped_query,sizeof(unstripped_query))) || 
      (res= read_file(edited_query_file,edited_query,sizeof(edited_query))))
  {
    return res;
  }
  
  return test_strings(unstripped_query,str_copy_mode,
                      str_language,edited_query);
}

///////////////////////////////////////////////////////////////////////////////
void prepare_line(char * line)
{
  if (line)
  {
    size_t len= strlen(line);
    while (len && (line[len-1]=='\r' || line[len-1]=='\n'))
      line[--len]= 0;
  }
}

///////////////////////////////////////////////////////////////////////////////
char separator_buffer[100];
char buffer[1000];
char copy_mode_buffer[100];
char language_buffer[100];

int test_file(FILE * file)
{
  while (!feof(file))
  {
    while (fgets(separator_buffer,sizeof(separator_buffer)-1,file) && 
           (separator_buffer[0] == '\r' || separator_buffer[0] == '\n'));
    if (separator_buffer[0] == '\r' || separator_buffer[0] == '\n')
      return 1;
    unstripped_query[0]= 0;
    while (fgets(buffer,sizeof(buffer)-1,file))
    {
      if (!strcmp(buffer,separator_buffer))
        break;
      strcat(unstripped_query,buffer);
    }
    prepare_line(unstripped_query);
    if (!fgets(copy_mode_buffer,sizeof(copy_mode_buffer)-1,file) ||
        !fgets(language_buffer,sizeof(language_buffer)-1,file) ||
        !fgets(separator_buffer,sizeof(separator_buffer)-1,file))
    {
      return 1;
    }
    prepare_line(copy_mode_buffer);
    prepare_line(language_buffer);
    
    edited_query[0]= 0;
    while (fgets(buffer,sizeof(buffer)-1,file))
    {
      if (!strcmp(buffer,separator_buffer))
        break;
      strcat(edited_query,buffer);
    }
    prepare_line(edited_query);
    test_strings(unstripped_query,language_buffer,
                 copy_mode_buffer,edited_query);
    printf("\n\n");
  }
  return 0;
}

///////////////////////////////////////////////////////////////////////////////
int test_file(const char * filename)
{
  FILE * file= fopen(filename,"rb");
  if (!file)
  {
    printf("can't open file \'%s\'\n",filename);
    return 1;
  }
  else
  {
    int res= test_file(file);
    fclose(file);
    return 0;
  }
}

///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
  switch (argc)
  {
  case 2:   return test_file(argv[1]);
  case 5:   return test_files(argv[1],argv[2],argv[3],argv[4]);
  default:  print_usage();  return 0;
  }
}

///////////////////////////////////////////////////////////////////////////////