Skip to content

Commit c3664c0

Browse files
committed
Add macros for error result fields to libpq.
1 parent b4ab39f commit c3664c0

File tree

4 files changed

+146
-16
lines changed

4 files changed

+146
-16
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 127 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.131 2003/08/13 16:29:03 tgl Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.132 2003/08/24 18:36:38 petere Exp $
33
-->
44

55
<chapter id="libpq">
@@ -1283,20 +1283,138 @@ Returns an individual field of an error report.
12831283
<synopsis>
12841284
char *PQresultErrorField(const PGresult *res, int fieldcode);
12851285
</synopsis>
1286-
<parameter>fieldcode</> is an error field identifier defined by the
1287-
<productname>PostgreSQL</> protocol (see <xref
1288-
linkend="protocol-error-fields">), for example <literal>'C'</> for
1289-
the SQLSTATE error code. NULL is returned if the
1286+
<parameter>fieldcode</> is an error field identifier; see the symbols
1287+
listed below. <symbol>NULL</symbol> is returned if the
12901288
<structname>PGresult</structname> is not an error or warning result,
12911289
or does not include the specified field. Field values will normally
12921290
not include a trailing newline.
12931291
</para>
12941292

12951293
<para>
1296-
Errors generated internally by libpq will have severity and primary message,
1297-
but typically no other fields. Errors returned by a pre-3.0-protocol server
1298-
will include severity and primary message, and sometimes a detail message,
1299-
but no other fields.
1294+
The following field codes are available:
1295+
<variablelist>
1296+
1297+
<varlistentry>
1298+
<term><symbol>PG_DIAG_SEVERITY</></term>
1299+
<listitem>
1300+
<para>
1301+
The severity; the field contents are <literal>ERROR</>,
1302+
<literal>FATAL</>, or <literal>PANIC</> (in an error message), or
1303+
<literal>WARNING</>, <literal>NOTICE</>, <literal>DEBUG</>,
1304+
<literal>INFO</>, or <literal>LOG</> (in a notice message), or a
1305+
localized translation of one of these. Always present.
1306+
</para>
1307+
</listitem>
1308+
</varlistentry>
1309+
1310+
<varlistentry>
1311+
<term><symbol>PG_DIAG_SQLSTATE</>
1312+
</term>
1313+
<listitem>
1314+
<para>
1315+
The SQLSTATE code for the error (a 5-character string following SQL
1316+
spec conventions). Not localizable. Always present.
1317+
</para>
1318+
</listitem>
1319+
</varlistentry>
1320+
1321+
<varlistentry>
1322+
<term><symbol>PG_DIAG_MESSAGE_PRIMARY</></term>
1323+
<listitem>
1324+
<para>
1325+
The primary human-readable error message (typically one line). Always
1326+
present.
1327+
</para>
1328+
</listitem>
1329+
</varlistentry>
1330+
1331+
<varlistentry>
1332+
<term><symbol>PG_DIAG_MESSAGE_DETAIL</></term>
1333+
<listitem>
1334+
<para>
1335+
Detail: an optional secondary error message carrying more detail about
1336+
the problem. May run to multiple lines.
1337+
</para>
1338+
</listitem>
1339+
</varlistentry>
1340+
1341+
<varlistentry>
1342+
<term><symbol>PG_DIAG_MESSAGE_HINT</></term>
1343+
<listitem>
1344+
<para>
1345+
Hint: an optional suggestion what to do about the problem. This is
1346+
intended to differ from detail in that it offers advice (potentially
1347+
inappropriate) rather than hard facts. May run to multiple lines.
1348+
</para>
1349+
</listitem>
1350+
</varlistentry>
1351+
1352+
<varlistentry>
1353+
<term><symbol>PG_DIAG_STATEMENT_POSITION</></term>
1354+
<listitem>
1355+
<para>
1356+
A string containing a decimal integer indicating an error cursor
1357+
position as an index into the original statement string. The first
1358+
character has index 1, and positions are measured in characters not
1359+
bytes.
1360+
</para>
1361+
</listitem>
1362+
</varlistentry>
1363+
1364+
<varlistentry>
1365+
<term><symbol>PG_DIAG_CONTEXT</></term>
1366+
<listitem>
1367+
<para>
1368+
An indication of the context in which the error occurred. Presently
1369+
this includes a call stack traceback of active PL functions. The
1370+
trace is one entry per line, most recent first.
1371+
</para>
1372+
</listitem>
1373+
</varlistentry>
1374+
1375+
<varlistentry>
1376+
<term><symbol>PG_DIAG_SOURCE_FILE</></term>
1377+
<listitem>
1378+
<para>
1379+
The file name of the source-code location where the error was
1380+
reported.
1381+
</para>
1382+
</listitem>
1383+
</varlistentry>
1384+
1385+
<varlistentry>
1386+
<term><symbol>PG_DIAG_SOURCE_LINE</></term>
1387+
<listitem>
1388+
<para>
1389+
The line number of the source-code location where the error was
1390+
reported.
1391+
</para>
1392+
</listitem>
1393+
</varlistentry>
1394+
1395+
<varlistentry>
1396+
<term><symbol>PG_DIAG_SOURCE_FUNCTION</></term>
1397+
<listitem>
1398+
<para>
1399+
The name of the source-code function reporting the error.
1400+
</para>
1401+
</listitem>
1402+
</varlistentry>
1403+
</variablelist>
1404+
</para>
1405+
1406+
<para>
1407+
The client is responsible for formatting displayed information to meet
1408+
its needs; in particular it should break long lines as needed.
1409+
Newline characters appearing in the error message fields should be
1410+
treated as paragraph breaks, not line breaks.
1411+
</para>
1412+
1413+
<para>
1414+
Errors generated internally by <application>libpq</application> will
1415+
have severity and primary message, but typically no other fields.
1416+
Errors returned by a pre-3.0-protocol server will include severity and
1417+
primary message, and sometimes a detail message, but no other fields.
13001418
</para>
13011419

13021420
<para>

src/interfaces/ecpg/ecpglib/connect.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.16 2003/08/08 13:16:20 petere Exp $ */
1+
/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.17 2003/08/24 18:36:38 petere Exp $ */
22

33
#define POSTGRES_ECPG_INTERNAL
44
#include "postgres_fe.h"
@@ -158,8 +158,8 @@ ECPGsetconn(int lineno, const char *connection_name)
158158
static void
159159
ECPGnoticeReceiver(void *arg, const PGresult *result)
160160
{
161-
char *sqlstate = PQresultErrorField(result, 'C');
162-
char *message = PQresultErrorField(result, 'M');
161+
char *sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
162+
char *message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
163163
struct sqlca_t *sqlca = ECPGget_sqlca();
164164

165165
int sqlcode;

src/interfaces/ecpg/ecpglib/error.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/error.c,v 1.7 2003/08/08 13:16:20 petere Exp $ */
1+
/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/error.c,v 1.8 2003/08/24 18:36:38 petere Exp $ */
22

33
#define POSTGRES_ECPG_INTERNAL
44
#include "postgres_fe.h"
@@ -164,10 +164,10 @@ ECPGraise_backend(int line, PGresult *result, PGconn *conn, int compat)
164164

165165
if (result)
166166
{
167-
sqlstate = PQresultErrorField(result, 'C');
167+
sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
168168
if (sqlstate == NULL)
169169
sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
170-
message = PQresultErrorField(result, 'M');
170+
message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
171171
}
172172
else
173173
{

src/interfaces/libpq/libpq-fe.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: libpq-fe.h,v 1.98 2003/08/13 16:29:03 tgl Exp $
10+
* $Id: libpq-fe.h,v 1.99 2003/08/24 18:36:38 petere Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -104,6 +104,18 @@ typedef enum
104104
PQERRORS_VERBOSE /* all the facts, ma'am */
105105
} PGVerbosity;
106106

107+
/* for PQresultErrorField() */
108+
#define PG_DIAG_SEVERITY 'S'
109+
#define PG_DIAG_SQLSTATE 'C'
110+
#define PG_DIAG_MESSAGE_PRIMARY 'M'
111+
#define PG_DIAG_MESSAGE_DETAIL 'D'
112+
#define PG_DIAG_MESSAGE_HINT 'H'
113+
#define PG_DIAG_STATEMENT_POSITION 'P'
114+
#define PG_DIAG_CONTEXT 'W'
115+
#define PG_DIAG_SOURCE_FILE 'F'
116+
#define PG_DIAG_SOURCE_LINE 'L'
117+
#define PG_DIAG_SOURCE_FUNCTION 'R'
118+
107119
/* PGconn encapsulates a connection to the backend.
108120
* The contents of this struct are not supposed to be known to applications.
109121
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy