1
1
<!--
2
- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.165 2004/10/01 17:34:17 tgl Exp $
2
+ $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.166 2004/10/18 22:00:41 tgl Exp $
3
3
-->
4
4
5
5
<chapter id="libpq">
@@ -1055,8 +1055,9 @@ PGresult *PQexec(PGconn *conn, const char *command);
1055
1055
out-of-memory conditions or serious errors such as inability
1056
1056
to send the command to the server.
1057
1057
If a null pointer is returned, it
1058
- should be treated like a <symbol>PGRES_FATAL_ERROR</symbol> result. Use
1059
- <function>PQerrorMessage</function> to get more information about the error.
1058
+ should be treated like a <symbol>PGRES_FATAL_ERROR</symbol> result.
1059
+ Use <function>PQerrorMessage</function> to get more information
1060
+ about such errors.
1060
1061
</para>
1061
1062
</listitem>
1062
1063
</varlistentry>
@@ -1144,6 +1145,81 @@ than one nonempty command.) This is a limitation of the underlying protocol,
1144
1145
but has some usefulness as an extra defense against SQL-injection attacks.
1145
1146
</para>
1146
1147
1148
+ <para>
1149
+ <variablelist>
1150
+ <varlistentry>
1151
+ <term><function>PQprepare</function><indexterm><primary>PQprepare</></></term>
1152
+ <listitem>
1153
+ <para>
1154
+ Submits a request to create a prepared statement with the
1155
+ given parameters, and waits for completion.
1156
+ <synopsis>
1157
+ PGresult *PQprepare(PGconn *conn,
1158
+ const char *stmtName,
1159
+ const char *query,
1160
+ int nParams,
1161
+ const Oid *paramTypes);
1162
+ </synopsis>
1163
+ </para>
1164
+
1165
+ <para>
1166
+ <function>PQprepare</> creates a prepared statement for later execution with
1167
+ <function>PQexecPrepared</>.
1168
+ This feature allows commands
1169
+ that will be used repeatedly to be parsed and planned just once, rather
1170
+ than each time they are executed.
1171
+ <function>PQprepare</> is supported only in protocol 3.0 and later
1172
+ connections; it will fail when using protocol 2.0.
1173
+ </para>
1174
+
1175
+ <para>
1176
+ The function creates a prepared statement named <parameter>stmtName</>
1177
+ from the <parameter>query</> string, which must contain a single SQL command.
1178
+ <parameter>stmtName</> may be <literal>""</> to create an unnamed statement,
1179
+ in which case any pre-existing unnamed statement is automatically replaced;
1180
+ otherwise it is an error if the statement name is already defined in the
1181
+ current session.
1182
+ If any parameters are used, they are referred
1183
+ to in the query as <literal>$1</>, <literal>$2</>, etc.
1184
+ <parameter>nParams</> is the number of parameters for which types are
1185
+ pre-specified in the array <parameter>paramTypes[]</>. (The array pointer
1186
+ may be <symbol>NULL</symbol> when <parameter>nParams</> is zero.)
1187
+ <parameter>paramTypes[]</> specifies, by OID, the data types to be assigned to
1188
+ the parameter symbols. If <parameter>paramTypes</> is <symbol>NULL</symbol>,
1189
+ or any particular element in the array is zero, the server assigns a data type
1190
+ to the parameter symbol in the same way it would do for an untyped literal
1191
+ string. Also, the query may use parameter symbols with numbers higher than
1192
+ <parameter>nParams</>; data types will be inferred for these symbols as
1193
+ well.
1194
+ </para>
1195
+
1196
+ <para>
1197
+ As with <function>PQexec</>, the result is normally a
1198
+ <structname>PGresult</structname> object whose contents indicate server-side
1199
+ success or failure. A null result indicates out-of-memory or inability to
1200
+ send the command at all.
1201
+ Use <function>PQerrorMessage</function> to get more information
1202
+ about such errors.
1203
+ </para>
1204
+
1205
+ <para>
1206
+ At present, there is no way to determine the actual datatype inferred for
1207
+ any parameters whose types are not specified in <parameter>paramTypes[]</>.
1208
+ This is a <application>libpq</> omission that will probably be rectified
1209
+ in a future release.
1210
+ </para>
1211
+ </listitem>
1212
+ </varlistentry>
1213
+ </variablelist>
1214
+
1215
+ Prepared statements for use with <function>PQexecPrepared</> can also be
1216
+ created by executing SQL <command>PREPARE</> statements. (But
1217
+ <function>PQprepare</> is more flexible since it does not require
1218
+ parameter types to be pre-specified.) Also, although there is no
1219
+ <application>libpq</> function for deleting a prepared statement,
1220
+ the SQL <command>DEALLOCATE</> statement can be used for that purpose.
1221
+ </para>
1222
+
1147
1223
<para>
1148
1224
<variablelist>
1149
1225
<varlistentry>
@@ -1166,7 +1242,8 @@ PGresult *PQexecPrepared(PGconn *conn,
1166
1242
<para>
1167
1243
<function>PQexecPrepared</> is like <function>PQexecParams</>, but the
1168
1244
command to be executed is specified by naming a previously-prepared
1169
- statement, instead of giving a query string. This feature allows commands
1245
+ statement, instead of giving a query string.
1246
+ This feature allows commands
1170
1247
that will be used repeatedly to be parsed and planned just once, rather
1171
1248
than each time they are executed.
1172
1249
<function>PQexecPrepared</> is supported only in protocol 3.0 and later
@@ -1182,13 +1259,6 @@ the prepared statement's parameter types were determined when it was created).
1182
1259
</listitem>
1183
1260
</varlistentry>
1184
1261
</variablelist>
1185
-
1186
- Presently, prepared statements for use with <function>PQexecPrepared</>
1187
- must be set up by executing an SQL <command>PREPARE</> command,
1188
- which is typically sent with <function>PQexec</> (though any of
1189
- <application>libpq</>'s query-submission functions may be used).
1190
- A lower-level interface for preparing statements may be offered in a
1191
- future release.
1192
1262
</para>
1193
1263
1194
1264
<para>
@@ -2270,10 +2340,15 @@ discarded by <function>PQexec</function>.
2270
2340
Applications that do not like these limitations can instead use the
2271
2341
underlying functions that <function>PQexec</function> is built from:
2272
2342
<function>PQsendQuery</function> and <function>PQgetResult</function>.
2273
- There are also <function>PQsendQueryParams</function> and
2274
- <function>PQsendQueryPrepared</function>, which can be used with
2275
- <function>PQgetResult</function> to duplicate the functionality of
2276
- <function>PQexecParams</function> and <function>PQexecPrepared</function>
2343
+ There are also
2344
+ <function>PQsendQueryParams</function>,
2345
+ <function>PQsendPrepare</function>, and
2346
+ <function>PQsendQueryPrepared</function>,
2347
+ which can be used with <function>PQgetResult</function> to duplicate the
2348
+ functionality of
2349
+ <function>PQexecParams</function>,
2350
+ <function>PQprepare</function>, and
2351
+ <function>PQexecPrepared</function>
2277
2352
respectively.
2278
2353
2279
2354
<variablelist>
@@ -2325,6 +2400,33 @@ int PQsendQueryParams(PGconn *conn,
2325
2400
</listitem>
2326
2401
</varlistentry>
2327
2402
2403
+ <varlistentry>
2404
+ <term><function>PQsendPrepare</><indexterm><primary>PQsendPrepare</></></term>
2405
+ <listitem>
2406
+ <para>
2407
+ Sends a request to create a prepared statement with the given
2408
+ parameters, without waiting for completion.
2409
+ <synopsis>
2410
+ int PQsendPrepare(PGconn *conn,
2411
+ const char *stmtName,
2412
+ const char *query,
2413
+ int nParams,
2414
+ const Oid *paramTypes);
2415
+ </synopsis>
2416
+
2417
+ This is an asynchronous version of <function>PQprepare</>: it
2418
+ returns 1 if it was able to dispatch the request, and 0 if not.
2419
+ After a successful call, call <function>PQgetResult</function>
2420
+ to determine whether the server successfully created the prepared
2421
+ statement.
2422
+ The function's parameters are handled identically to
2423
+ <function>PQprepare</function>. Like
2424
+ <function>PQprepare</function>, it will not work on 2.0-protocol
2425
+ connections.
2426
+ </para>
2427
+ </listitem>
2428
+ </varlistentry>
2429
+
2328
2430
<varlistentry>
2329
2431
<term><function>PQsendQueryPrepared</function><indexterm><primary>PQsendQueryPrepared</></></term>
2330
2432
<listitem>
@@ -2358,7 +2460,8 @@ int PQsendQueryPrepared(PGconn *conn,
2358
2460
<para>
2359
2461
Waits for the next result from a prior
2360
2462
<function>PQsendQuery</function>,
2361
- <function>PQsendQueryParams</function>, or
2463
+ <function>PQsendQueryParams</function>,
2464
+ <function>PQsendPrepare</function>, or
2362
2465
<function>PQsendQueryPrepared</function> call,
2363
2466
and returns it. A null pointer is returned when the command is complete
2364
2467
and there will be no more results.
0 commit comments