3 #include "postgres_fe.h"
7 static void output_escaped_str(char *cmd
, bool quoted
);
10 output_line_number(void)
12 char *line
= hashline_number();
14 fprintf(yyout
, "%s", line
);
19 output_simple_statement(char *stmt
)
21 output_escaped_str(stmt
, false);
28 * store the whenever action here
30 struct when when_error
,
35 print_action(struct when
* w
)
40 fprintf(yyout
, "sqlprint();");
43 fprintf(yyout
, "goto %s;", w
->command
);
46 fprintf(yyout
, "%s;", w
->command
);
49 fprintf(yyout
, "exit (1);");
52 fprintf(yyout
, "break;");
55 fprintf(yyout
, "{/* %d not implemented yet */}", w
->code
);
61 whenever_action(int mode
)
63 if ((mode
& 1) == 1 && when_nf
.code
!= W_NOTHING
)
66 fprintf(yyout
, "\nif (sqlca.sqlcode == ECPG_NOT_FOUND) ");
67 print_action(&when_nf
);
69 if (when_warn
.code
!= W_NOTHING
)
72 fprintf(yyout
, "\nif (sqlca.sqlwarn[0] == 'W') ");
73 print_action(&when_warn
);
75 if (when_error
.code
!= W_NOTHING
)
78 fprintf(yyout
, "\nif (sqlca.sqlcode < 0) ");
79 print_action(&when_error
);
91 /* do not print line numbers if we are in debug mode */
98 char *line
= mm_alloc(strlen("\n#line %d \"%s\"\n") + sizeof(int) * CHAR_BIT
* 10 / 3 + strlen(input_filename
));
100 sprintf(line
, "\n#line %d \"%s\"\n", yylineno
, input_filename
);
109 output_statement(char *stmt
, int whenever_mode
, enum ECPG_statement_type st
)
112 fprintf(yyout
, "{ ECPGdo(__LINE__, %d, %d, %s, %d, ", compat
, force_indicator
, connection
? connection
: "NULL", questionmarks
);
113 if (st
== ECPGst_normal
)
116 fputs("ECPGst_prepnormal, \"", yyout
);
118 fputs("ECPGst_normal, \"", yyout
);
120 output_escaped_str(stmt
, false);
121 fputs("\", ", yyout
);
124 fprintf(yyout
, "%d, %s, ", st
, stmt
);
126 /* dump variables to C file */
127 dump_variables(argsinsert
, 1);
128 fputs("ECPGt_EOIT, ", yyout
);
129 dump_variables(argsresult
, 1);
130 fputs("ECPGt_EORT);", yyout
);
133 whenever_action(whenever_mode
| 2);
135 if (connection
!= NULL
)
140 output_prepare_statement(char *name
, char *stmt
)
142 fprintf(yyout
, "{ ECPGprepare(__LINE__, %s, %d, ", connection
? connection
: "NULL", questionmarks
);
143 output_escaped_str(name
, true);
145 output_escaped_str(stmt
, true);
149 if (connection
!= NULL
)
154 output_deallocate_prepare_statement(char *name
)
156 const char *con
= connection
? connection
: "NULL";
158 if (strcmp(name
, "all"))
160 fprintf(yyout
, "{ ECPGdeallocate(__LINE__, %d, %s, ", compat
, con
);
161 output_escaped_str(name
, true);
165 fprintf(yyout
, "{ ECPGdeallocate_all(__LINE__, %d, %s);", compat
, con
);
169 if (connection
!= NULL
)
174 output_escaped_str(char *str
, bool quoted
)
177 int len
= strlen(str
);
179 if (quoted
&& str
[0] == '\"' && str
[len
- 1] == '\"') /* do not escape quotes
180 * at beginning and end
181 * if quoted string */
188 /* output this char by char as we have to filter " and \n */
192 fputs("\\\"", yyout
);
193 else if (str
[i
] == '\n')
194 fputs("\\\n", yyout
);
195 else if (str
[i
] == '\\')
200 * check whether this is a continuation line if it is, do not
201 * output anything because newlines are escaped anyway
204 /* accept blanks after the '\' as some other compilers do too */
208 } while (str
[j
] == ' ' || str
[j
] == '\t');
210 if ((str
[j
] != '\n') && (str
[j
] != '\r' || str
[j
+ 1] != '\n')) /* not followed by a
212 fputs("\\\\", yyout
);
214 else if (str
[i
] == '\r' && str
[i
+ 1] == '\n')
216 fputs("\\\r\n", yyout
);
220 fputc(str
[i
], yyout
);
223 if (quoted
&& str
[0] == '\"' && str
[len
] == '\"')