root/trunk/README

Revision 1266, 20.5 kB (checked in by jtv, 3 years ago)

Documentation improvements, thanks Andrew Maclean

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 Introduction
2
3 Welcome to libpqxx, the C++ API to the PostgreSQL database management system.
4
5 This package requires PostgreSQL to be installed--including the C headers for
6 client development.  The library builds on top of PostgreSQL's standard C API,
7 libpq, though this fact is almost completely hidden from programs that use
8 libpqxx.
9
10 Further information, as well as updates, a mailing list, and a bug reporting
11 system can be found at http://pqxx.org/
12
13 There are also ready-made libpqxx packages available for several systems: Debian
14 packages are available on any Debian download mirror, Mark Round maintains a
15 Blastwave package for Solaris, and Arjen Baart has made RPM packages (source and
16 i386 binaries) available on
17
18         http://www.andromeda.nl/UNIX/packages/
19
20 You may want to check these before going to the trouble of building libpqxx
21 yourself.
22
23
24 Getting Started
25
26 All of this applies only to operating systems that are either part of, or
27 closely resemble, or support the standard interfaces of, the Unix family.  This
28 includes not just the "real" Unices (AIX, HP-UX, Irix, Solaris) but also MacOS X
29 (starting with 10.2, a.k.a. Jaguar) as well as GNU/Linux, the various BSD
30 operating systems, and Microsoft Windows with a Unix-like environment such as
31 Cygwin or MinGW installed.  There is a separate section below for Windows users
32 without such an environment.
33
34 For the Unix-like systems the procedure is essentially the standard "configure,
35 make, make install" sequence, except that in most cases some extra work needs to
36 be done for the optional "make check" step.
37
38 Let's just step through the full procedure for a Unix-like system.  You may be
39 familiar with most of it:
40
41
42 ./configure     # (plus suitable preparation, to set up the build environment)
43 make            # (to build the library)
44
45 # Run library's self-test suite; in this example, connect to DBMS running on
46 # local Unix socket in /tmp, and use database pqxx as a testing ground.  See
47 # below for more details on how to set test parameters.
48
49 make PGHOST=/tmp PGDATABASE=pqxx check
50 make install    # (with superuser privileges, to install the library; see below)
51
52
53 Once this has succeeded, you should be able to link your own programs with
54 libpqxx and run them with confidence.
55
56 If it hasn't succeeded, or isn't quite what you want, it's time to move on to
57 the fineprint we hinted at.
58
59
60 1. Configure
61
62 A word on that "suitable preparation."  In older versions of libpqxx, you would
63 need to specify the location of the PostgreSQL header and library files in some
64 cases; when left alone, the script had a builtin list of usual locations to
65 check.  This is no longer necessary; the configure script will use another
66 script provided by Postgres to find them automatically.
67
68 In the new setup, however, the configure script must be able to find and run
69 this new script.  It's called pg_config, and it should be in the bin/
70 subdirectory of wherever Postgres is installed on your system.  Make sure this
71 directory is represented in your executable path before you run the configure
72 script, or it will fail with a message like:
73
74 configure: error: PostgreSQL configuration script pg_config was not found.
75
76 Or if you don't want to have pg_config in your path for whatever reason, or you
77 have multiple PostgreSQL installations on your system (each with their own copy
78 of pg_config, naturally) and wish to override the default version, add an option
79 like
80
81         PG_CONFIG=/home/me/postgres/bin/pg_config
82
83 to your "configure" command line, where /home/me/postgres/bin/pg_config is an
84 example of where your preferred copy of pg_config might be.  This would indicate
85 to the configure script that you wish to build a libpqxx based on the postgres
86 version found in /home/me/postgres.
87
88 Finally, if you wish to install the libpqxx you're going to build in a custom
89 location, such as your home directory /home/me, you can specify this to the
90 configure script by calling it with the --prefix option, e.g.:
91
92         ./configure --prefix=/home/me
93
94 This can be handy if you don't have administrator rights on the machine you're
95 working on!
96
97 The configure scripts supports many other options to tweak how and where libpqxx
98 is to be built and installed; try the --help option to get an overview if you're
99 interested.
100
101 If configuration just absolutely plain won't work for whatever reason: take a
102 look in the config/sample-headers/ directory.  Here you will find configuration
103 headers for various compilers and libpq versions.  Pick the config-internal-*.h
104 and config-public-*.h headers for the compiler and libpq version most closely
105 matching your own, and see if they work for you.
106
107
108 2. Make
109
110 One problem some people have run into at this stage is that the header files for
111 PostgreSQL need the OpenSSL header files to work.  If this happens to you, make
112 sure openssl is installed and try again.
113
114 Otherwise, if the "make" part of the build procedure fails, you're probably
115 using a different compiler or compiler version than I am.  If your C++ compiler
116 is more than a few years old, and it's not one of the highly standard-compliant
117 ones like gcc or Comeau, just forget it.  It won't work until you get an
118 up-to-date compiler.
119
120 If, on the other hand, you're using a shiny new compiler and you're getting
121 errors or warnings, please let me know about them so I can fix them.
122
123
124 3. Make Check
125
126 This part is optional.  It is recommended that you do this just to make sure
127 that you've got a working library, but there is one Very Important Caveat that
128 could affect your existing data.  See below.
129
130 "Make check" is where you compile and run the regression test that typically
131 exercises all public methods in the library.  Obviously something or other will
132 have to go wrong at this point, right?
133
134 Indeed.  The "make check" procedure needs a database to play with.  In this
135 database, it will create two tables "pqxxevents" and "pqxxorgevents," as well as
136 some tables for its own use.  All have names prefixed with pqxx.  CAUTION: if
137 this database already contains tables with any of these names, either the check
138 will fail, or worse, your original data will be erased.
139
140 So choose your test database with at least a little caution.  Obviously the
141 safest thing to do is to set up a separate database for this.
142
143 To direct the regression test to the right database, set some or all of the
144 following environment variables as needed before running "make check" (see
145 description above):
146
147         PGDATABASE      (name of database; defaults to your user name)
148         PGHOST          (database server; defaults to local machine)
149         PGPORT          (PostgreSQL port to connect to; default is 5432)
150         PGUSER          (your PostgreSQL user ID; defaults to your login name)
151         PGPASSWORD      (your PostgreSQL password, if needed)
152
153 Further environment variables that may be of use to you are documented in the
154 libpq documentation and in the manpage for Postgres' command-line client, psql.
155
156 Setting environment variables works differently depending on your shell, but
157 try (preferably in this order):
158     export VARIABLE=value
159 or
160     VARIABLE=value
161     export VARIABLE
162 or
163     set VARIABLE=value
164
165 Try printing the variable afterwards to make sure.  The command is normally
166     echo $VARIABLE
167 and, if you set the variable successfully, should print the value you assigned.
168 It will print nothing if you failed to set the variable.
169
170 Should you have trouble finding the socket for a database running locally on a
171 Unix socket, try locating the socket in the filesystem.  It should appear as a
172 "file" called .s.PGSQL.5432 (if you are new to Unix-like systems, be warned that
173 files whose name start with a dot are not displayed by default).  Set PGHOST to
174 the directory where this "file" resides, as an absolute path--e.g. "/tmp".  On
175 most Unix-like systems, it will be located in /tmp, /var/run, or if your system
176 adheres to the FHS (the Filesystem Hierarchy Standard for GNU/Linux systems), in
177 /var/run/postgresql.  Be sure to use the absolute path; the leading slash tells
178 libpq that this is not a network address but a local Unix socket.
179
180 If you've taken care of all this, it should get you through the regression test
181 to ensure that everything's working properly.  If it isn't, and it's not
182 something you can fix by tweaking your PostgreSQL setup, let me know about it
183 and I'll try to fix it.
184
185
186 4. Make Install
187
188 This is supposed to install the libpqxx library and header files to your
189 system.  It took me and many others some time to get this to actually work, so
190 please take care to check that it really does work and that especially the
191 headers are really installed to your system.
192
193 The library and headers are installed to /usr/local/ by default, in their
194 respective subdirectories include/ and lib/.  The default installation location
195 has changed in release 2.2.0; it was /usr/local/pqxx before.
196
197 Assuming this succeeds, you should now be able to build your own programs by
198 adding /usr/local/pqxx/include to your include path, and /usr/local/pqxx/lib to
199 your library search path.  See the documentation and the test programs for more
200 information on using libpqxx.
201
202 One other thing here that may not work is that, if you link with the dynamic
203 version of the library, your program may fail to run because the run-time
204 loader cannot find the library.  You can get around this by (i) linking to the
205 static version of the library, or (ii) adding a link in /usr/local/lib to the
206 dynamic libpqxx library, or (iii) adding libpqxx's lib/ directory to your
207 loader's search path before running your program.  This is in decreasing order
208 of preference, so try (i) first, and only resort to (iii) if both (i) and (ii)
209 fail.  On Unix-like systems including GNU/Linux, the loader's search path can be
210 extended by setting the LD_LIBRARY_PATH variable.
211
212 Enjoy!
213
214
215 Building on Windows
216
217 Project files for Visual C++ are provided in the win32 directory, along with
218 some other Windows-specific material.  You'll need at least version 7 of the
219 VC compiler (also called VC.NET), earlier versions being too severely flawed to
220 build serious post-1996 C++ code.  Some of the 7.x versions will work; others
221 may not.  Versions of the library have also been built with Intel's compiler
222 running as a Visual C++ plugin.
223
224 Instead of going this route, you may want to try if the Unix build procedure
225 works for you instead, e.g. using the Cygwin tools.  If you don't, or it
226 doesn't, you'll need to copy the most appropriate compile-time configuration
227 files from various subdirectories in config/example-headers/ to include/pqxx.
228 You may need to tweak them manually to define the exact features your system,
229 compiler, and PostgreSQL versions support.  Normally the configure script would
230 do this for you; in theory it should be possible to run the configure script
231 with e.g. Visual C++ as your compiler.
232
233 Before trying to compile with Visual C++, you'll at least need to copy the file
234 win32/common-sample to win32/common, and edit the latter to reflect the proper
235 paths to your PostgreSQL headers and the libpq library.  See the win32
236 subdirectory for more documentation.
237
238
239 Manual Configuration: config-*-*.h
240
241 Normally, on any vaguely Unix-like system, the configuration headers (called
242 config-internal-*.h for the library's internal use, config-public-*.h for
243 both the library and client programs) are generated from config.h.in.  All these
244 files, once generated, are situated in the include/pqxx/ directory.
245
246 The configitems file lists all configuration items and where they go; but see
247 win32/INSTALL.txt for a detailed description of how these files work.
248
249 Getting the compiler-related configuration right can take several stages of
250 trying to build, looking at error messages, looking for configuration items that
251 may be related, changing them, and building again.  If nothing seems to help,
252 try the libpqxx mailing list or register a bug report or support request on the
253 website.  Be sure to read the FAQ though, because there are some known problems
254 with various compilers.
255
256
257 Windows-Specific Build Problems
258
259 If you're using Microsoft's compiler, you may find that some features of the
260 library (such as reverse iterators) are left out.  This is because Visual C++ is
261 not able to compile all of the library's code, and so the preprocessor was used
262 to disable such code if use of this compiler is detected.  Several other
263 workarounds for compiler bugs are automatically switched on for Visual C++,
264 regardless of whether you use the configure script.
265
266 Another problem specific to Windows is that apparently it doesn't let you free
267 memory in a DLL that was allocated in the main program or in another DLL, or
268 vice versa.  This can cause trouble when setting Noticers to process error or
269 warning output; which is one reason why recommended practice is to build libpqxx
270 as a static library, not a DLL.  When using Windows you must also take care to
271 set all Noticers from the same context where the Connection is created.
272
273
274 Documentation
275
276 The best information on programming with libpqxx can be found in the header
277 files themselves, in the include/pqxx/ directory.  Comments and declarations
278 from these headers are automatically extracted, using a program called Doxygen,
279 to form the HTML reference documentation that can be found in the
280 doc/html/Reference/ directory.  You'll want to start off by reading about the
281 connection class and the transaction_base class (though in practice you'll
282 mostly use the convenience typedef "work").
283
284 To get a good start in programming libpqxx, however, you may prefer to have some
285 introductory explanation and code examples.  The former can be found in the
286 tutorial, doc/html/Tutorial which currently is not being actively maintained but
287 does cover the basics accurately.  The tutorial is generated from the input
288 file doc/libpqxx.sgml at release time.
289
290 For some quick examples, take a look at the test programs in the test/
291 directory.  If you don't know how a certain function or class is used, try
292 searching the test programs for that name.  This may be the quickest way to a
293 working example.
294
295 When reading the test programs, please keep in mind that these do a lot of
296 checking and verifying to see that the library works correctly.  This is done
297 at test time precisely so that you won't need to do that in your own code.  You
298 are not meant to imitate all those safety checks yourself; libpqxx encourages a
299 relatively care-free programming style.  Rely on the exception mechanism for
300 error handling and concentrate on your goals, not the plumbing.
301
302
303 Programming with libpqxx
304
305 Your first program will involve the libpqxx classes connection (see headers
306 "pqxx/connection_base.hxx" and "pqxx/connection.hxx"), and work (a convenience
307 typedef for transaction<> which conforms to the interface defined in
308 "pqxx/transaction_base.hxx").
309
310 These "*.hxx" headers are not the ones you include in your program.  Instead,
311 include the versions without filename suffix (i.e. "pqxx/connection_base" etc.)
312 and they will include the .hxx files for you.  This was done so that includes
313 are in standard C++ style (as in <iostream> etc.), but an editor will still
314 recognize them as files containing C++ code.
315
316 Continuing the list of classes, you will most likely also need the result class
317 ("pqxx/result.hxx").  In a nutshell, you create a "connection" based on a
318 Postgres connection string (see below), create a "work" in the context of that
319 connection, and run one or more queries on the work which return "result"
320 objects.  The results are containers of rows of data, each of which you can
321 treat as an array of strings: one for each field in the row.  It's that simple.
322
323 Here is a simple example program to get you going, with full error handling:
324
325         #include <iostream>
326         #include <pqxx/pqxx>
327
328         using namespace std;
329         using namespace pqxx;
330
331         int main()
332         {
333                 try
334                 {
335                         connection C;
336                         cout << "Connected to " << C.dbname() << endl;
337                         work W(C);
338
339                         result R = W.exec("SELECT name FROM employee");
340
341                         cout << "Found " << R.size() << "employees:" << endl;
342                         for (result::const_iterator r = R.begin();
343                              r != R.end();
344                              ++r)
345                         {
346                                 cout << r[0].c_str() << endl;
347                         }
348
349                         cout << "Doubling all employees' salaries..." << endl;
350                         W.exec("UPDATE employee SET salary=salary*2");
351
352                         cout << "Making changes definite: ";
353                         W.commit();
354                         cout << "ok." << endl;
355                 }
356                 catch (const exception &e)
357                 {
358                         cerr << e.what() << endl;
359                         return 1;
360                 }
361                 return 0;
362         }
363
364
365 Connection strings
366
367 Postgres connection strings state which database server you wish to connect to,
368 under which username, using which password, and so on.  Their format is defined
369 in the documentation for libpq, the C client interface for PostgreSQL.
370 Alternatively, these values may be defined by setting certain environment
371 variables as documented in e.g. the manual for psql, the command line interface
372 to PostgreSQL.  Again the definitions are the same for libpqxx-based programs.
373
374 The connection strings and variables are not fully and definitively documented
375 here; this document will tell you just enough to get going.  Check the
376 PostgreSQL documentation for authoritative information.
377
378 The connection string consists of attribute=value pairs separated by spaces,
379 e.g. "user=john password=1x2y3z4".  The valid attributes include:
380
381 host
382         Name of server to connect to, or the full file path (beginning with a
383         slash) to a Unix-domain socket on the local machine.  Defaults to
384         "/tmp".  Equivalent to (but overrides) environment variable PGHOST.
385
386 hostaddr
387         IP address of a server to connect to; mutually exclusive with "host".
388
389 port
390         Port number at the server host to connect to, or socket file name
391         extension for Unix-domain connections.  Equivalent to (but overrides)
392         environment variable PGPORT.
393
394 dbname
395         Name of the database to connect to.  A single server may host multiple
396         databases.  Defaults to the same name as the current user's name.
397         Equivalent to (but overrides) environment variable PGDATABASE.
398
399 user
400         User name to connect under.  This defaults to the name of the current
401         user, although PostgreSQL users are not necessarily the same thing as
402         system users.
403
404 requiressl
405         If set to 1, demands an encrypted SSL connection (and fails if no SSL
406         connection can be created).
407
408 Settings in the connection strings override the environment variables, which in
409 turn override the default, on a variable-by-variable basis.  You only need to
410 define those variables that require non-default values.
411
412 Linking with libpqxx
413
414 To link your final program, make sure you link to both the C-level libpq library
415 and the actual C++ library, libpqxx.  On most Unix-style compilers, this can be
416 done using the options
417
418         -lpq -lpqxx
419
420 while linking.  Note that both libraries must be in your link path, so the
421 linker knows where to find them.  Any dynamic libraries you use must also be in
422 a place where the loader can find them when loading your program at runtime.
423
424 Some users have reported problems using the above syntax, however, particularly
425 when multiple versions of libpqxx are partially or incorrectly installed on the
426 system.  If you get massive link errors, try removing the "-lpqxx" argument from
427 the command line and replacing it with the name of the libpqxx library binary
428 instead.  That's typically libpqxx.a, but you'll have to add the path to its
429 location as well, e.g. /usr/local/pqxx/lib/libpqxx.a.  This will ensure that the
430 linker will use that exact version of the library rather than one found
431 elsewhere on the system, and eliminate worries about the exact right version of
432 the library being installed with your program..
433
434
435 APPENDIX A - Links
436
437 Apple MacOS X   http://www.apple.com/macosx/
438 BSD             http://www.bsd.org/
439 C++             http://www.cs.rpi.edu/~musser/stl-book/
440 Cygwin          http://cygwin.com/
441 Doxygen         http://www.stack.nl/~dimitri/doxygen/
442 gcc             http://gcc.gnu.org/
443 Google          http://www.google.com/
444 libpq           http://candle.pha.pa.us/main/writings/pgsql/sgml/libpq.html
445 libpqxx         http://pqxx.org/
446 Linux           http://www.linux.org/
447 MinGW           http://www.mingw.org/
448 OpenSSL         http://www.openssl.org/
449 PostgreSQL      http://www.postgresql.org/
450 zlib            http://www.zlib.org/
451
452
453 APPENDIX B - Projects Using libpqxx
454
455 This list is far from complete.  It is known that there are many other projects
456 using libpqxx that are not included here.  Some of them may be proprietary, or
457 even have no names.
458
459 Inclusion does not imply endorsement.  For all I know, the people running the
460 Google projects may be unhappy with libpqxx--or perhaps they may have stopped
461 using it by the time you read this!  But obviously I'll do my best to ensure
462 that this does not happen.  On to the list:
463
464 As found on Google:
465
466 DocConversion           http://docconversion.sourceforge.net/
467 Genea                   http://savannah.nongnu.org/projects/genea/
468 Gnucomo                 http://www.gnucomo.org/
469 MapServer               http://mapserver.gis.umn.edu/
470 QHacc                   http://qhacc.sourceforge.net/
471 Vocal / Mascarpone      http://www.vovida.org/
472
473
474 Confirmed by authors:
475
476 OKE                     http://www.liacs.nl/home/bsamwel/oke/prerelease-0.10/
477 KOffice / Kexi          http://www.kexi-project.org/
478 KPoGre                  http://kpogre.sourceforge.net/
479 Once MMORPG             http://sourceforge.net/projects/once/
480 Scippy                  http://dicomlib.swri.ca/scippy.html
481
Note: See TracBrowser for help on using the browser.