1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5
>C++ Applications</TITLE
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
10
HREF="mailto:pgsql-docs@postgresql.org"><LINK
12
TITLE="PostgreSQL 9.1beta1 Documentation"
13
HREF="index.html"><LINK
15
TITLE="ECPG - Embedded SQL in C"
16
HREF="ecpg.html"><LINK
19
HREF="ecpg-lo.html"><LINK
21
TITLE="Embedded SQL Commands"
22
HREF="ecpg-sql-commands.html"><LINK
25
HREF="stylesheet.css"><META
26
HTTP-EQUIV="Content-Type"
27
CONTENT="text/html; charset=ISO-8859-1"><META
29
CONTENT="2011-04-27T21:20:33"></HEAD
35
SUMMARY="Header navigation table"
47
>PostgreSQL 9.1beta1 Documentation</A
66
TITLE="ECPG - Embedded SQL in C"
86
TITLE="ECPG - Embedded SQL in C"
95
TITLE="Embedded SQL Commands"
96
HREF="ecpg-sql-commands.html"
117
> ECPG has some limited support for C++ applications. This section
118
describes some caveats.
124
> preprocessor takes an input file
125
written in C (or something like C) and embedded SQL commands,
126
converts the embedded SQL commands into C language chunks, and
127
finally generates a <TT
130
> file. The header file
131
declarations of the library functions used by the C language chunks
135
> generates are wrapped
138
>extern "C" { ... }</TT
139
> blocks when used under
140
C++, so they should work seamlessly in C++.
143
> In general, however, the <TT
147
understands C; it does not handle the special syntax and reserved
148
words of the C++ language. So, some embedded SQL code written in
149
C++ application code that uses complicated features specific to C++
150
might fail to be preprocessed correctly or might not work as
154
> A safe way to use the embedded SQL code in a C++ application is
155
hiding the ECPG calls in a C module, which the C++ application code
156
calls into to access the database, and linking that together with
157
the rest of the C++ code. See <A
158
HREF="ecpg-cpp.html#ECPG-CPP-AND-C"
168
NAME="ECPG-CPP-SCOPE"
169
>33.13.1. Scope for Host Variables</A
175
> preprocessor understands the scope of
176
variables in C. In the C language, this is rather simple because
177
the scopes of variables is based on their code blocks. In C++,
178
however, the class member variables are referenced in a different
179
code block from the declared position, so
183
> preprocessor will not understand the
184
scope of the class member variables.
187
> For example, in the following case, the <TT
191
preprocessor cannot find any declaration for the
199
method, so an error will occur.
202
CLASS="PROGRAMLISTING"
205
EXEC SQL BEGIN DECLARE SECTION;
207
EXEC SQL END DECLARE SECTION;
217
EXEC SQL CONNECT TO testdb1;
222
EXEC SQL SELECT current_database() INTO :dbname;
223
printf("current_database = %s\n", dbname);
228
EXEC SQL DISCONNECT ALL;
232
This code will result in an error like this:
237
>ecpg test_cpp.pgc</KBD
239
test_cpp.pgc:28: ERROR: variable "dbname" is not declared</PRE
243
> To avoid this scope issue, the <TT
247
could be modified to use a local variable as intermediate storage.
248
But this approach is only a poor workaround, because it uglifies
249
the code and reduces performance.
252
CLASS="PROGRAMLISTING"
253
>void TestCpp::test()
255
EXEC SQL BEGIN DECLARE SECTION;
257
EXEC SQL END DECLARE SECTION;
259
EXEC SQL SELECT current_database() INTO :tmp;
260
strlcpy(dbname, tmp, sizeof(tmp));
262
printf("current_database = %s\n", dbname);
272
NAME="ECPG-CPP-AND-C"
273
>33.13.2. C++ Application Development with External C Module</A
276
> If you understand these technical limitations of
280
> preprocessor in C++, you might come to
281
the conclusion that linking C objects and C++ objects at the link
282
stage to enable C++ applications to use ECPG features could be
283
better than writing some embedded SQL commands in C++ code
284
directly. This section describes a way to separate some embedded
285
SQL commands from C++ application code with a simple example. In
286
this example, the application is implemented in C++, while C and
287
ECPG is used to connect to the PostgreSQL server.
290
> Three kinds of files have to be created: a C file
294
>), a header file, and a C++ file:
308
> A sub-routine module to execute SQL commands embedded in C.
309
It is going to be converted
313
> by the preprocessor.
316
CLASS="PROGRAMLISTING"
317
>#include "test_mod.h"
318
#include <stdio.h>
323
EXEC SQL CONNECT TO testdb1;
329
EXEC SQL BEGIN DECLARE SECTION;
331
EXEC SQL END DECLARE SECTION;
333
EXEC SQL SELECT current_database() INTO :dbname;
334
printf("current_database = %s\n", dbname);
340
EXEC SQL DISCONNECT ALL;
352
> A header file with declarations of the functions in the C
356
>). It is included by
360
>. This file has to have an
364
> block around the declarations,
365
because it will be linked from the C++ module.
368
CLASS="PROGRAMLISTING"
375
void db_disconnect();
390
> The main code for the application, including
394
> routine, and in this example a
398
CLASS="PROGRAMLISTING"
399
>#include "test_mod.h"
428
TestCpp *t = new TestCpp();
441
> To build the application, proceed as follows. Convert
460
> with the C compiler:
462
CLASS="PROGRAMLISTING"
463
>ecpg -o test_mod.c test_mod.pgc
464
cc -c test_mod.c -o test_mod.o</PRE
475
> with the C++ compiler:.
477
CLASS="PROGRAMLISTING"
478
>c++ -c test_cpp.cpp -o test_cpp.o</PRE
482
> Finally, link these object files, <TT
489
>, into one executable, using the C++
492
CLASS="PROGRAMLISTING"
493
>c++ test_cpp.o test_mod.o -lecpg -o test_cpp</PRE
503
SUMMARY="Footer navigation table"
532
HREF="ecpg-sql-commands.html"
556
>Embedded SQL Commands</TD
b'\\ No newline at end of file'