Skip to content

Commit 46cbc6b

Browse files
author
Igor Pesic
committed
Bug#36783534 Fix comp_sql to allow for single literal generation
- adds an optional as_single_string argument to comp_sql script - if that argument is "true", then the generated header file will not be an array of char pointers with nullptr at the end, but a single constexpr const char[] - reason for change: in some cases this script is used to load non-SQL files and instead of currently concatenating all the literals at run-time and save them in std::string, we would prefer to already have a ready literal at compile time Change-Id: Icc2c8d4623f45dcd9ea06aefa63714801bed3876
1 parent accdda0 commit 46cbc6b

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

scripts/comp_sql.cc

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <stdint.h>
3333
#include <stdio.h>
3434
#include <stdlib.h>
35+
#include <cstring>
3536

3637
#include "my_compiler.h"
3738
#include "mysql/psi/mysql_file.h"
@@ -86,7 +87,7 @@ static char *fgets_fn(char *buffer, size_t size, MYSQL_FILE *input,
8687
return line;
8788
}
8889

89-
static void print_query(FILE *out, const char *query) {
90+
static void print_query(FILE *out, const char *query, bool as_single_string) {
9091
const char *ptr = query;
9192
int column = 0;
9293

@@ -125,7 +126,12 @@ static void print_query(FILE *out, const char *query) {
125126
}
126127
ptr++;
127128
}
128-
fprintf(out, "\\n\",\n");
129+
if (as_single_string) {
130+
// no commas, since it is a single literal
131+
fprintf(out, "\\n\"\n");
132+
} else {
133+
fprintf(out, "\\n\",\n");
134+
}
129135
}
130136

131137
int main(int argc, char *argv[]) {
@@ -137,8 +143,14 @@ int main(int argc, char *argv[]) {
137143
size_t query_length = 0;
138144
bootstrap_parser_state parser_state;
139145

140-
if (argc != 4)
141-
die("Usage: comp_sql <struct_name> <sql_filename> <c_filename>");
146+
if (argc < 4 || argc > 5)
147+
die("Usage: comp_sql <struct_name> <sql_filename> <c_filename> "
148+
"[<as_single_string>]");
149+
bool as_single_string = false;
150+
if (argc == 5) {
151+
char *as_single_string_arg = argv[4];
152+
if (std::strcmp(as_single_string_arg, "true") == 0) as_single_string = true;
153+
}
142154

143155
/* Open input and output file */
144156
if (!(in = fopen(infile_name, "r")))
@@ -152,7 +164,12 @@ int main(int argc, char *argv[]) {
152164
" Do not edit this file, it is automatically generated from:\n");
153165
fprintf(out, " <%s>\n", infile_name);
154166
fprintf(out, "*/\n");
155-
fprintf(out, "const char* %s[]={\n", struct_name);
167+
if (as_single_string) {
168+
// single literal can also be constexpr
169+
fprintf(out, "constexpr const char %s[]=\n", struct_name);
170+
} else {
171+
fprintf(out, "const char* %s[]={\n", struct_name);
172+
}
156173

157174
parser_state.init(infile_name);
158175

@@ -173,10 +190,15 @@ int main(int argc, char *argv[]) {
173190
parser_state.report_error_details(parser_die);
174191
}
175192

176-
print_query(out, query);
193+
print_query(out, query, as_single_string);
177194
}
178195

179-
fprintf(out, "nullptr\n};\n");
196+
if (as_single_string) {
197+
// no need for nullptr and bracket in a single literal
198+
fprintf(out, ";\n");
199+
} else {
200+
fprintf(out, "nullptr\n};\n");
201+
}
180202

181203
fclose(in);
182204
fclose(out);

0 commit comments

Comments
 (0)