source: trunk/docs/contents/pg/module.rst @ 804

Last change on this file since 804 was 804, checked in by cito, 4 years ago

Make the automatic conversion to arrays configurable

The automatic conversion of arrays to lists can now be
disabled with the set_array() method.

File size: 22.2 KB
Line 
1Module functions and constants
2==============================
3
4.. py:currentmodule:: pg
5
6The :mod:`pg` module defines a few functions that allow to connect
7to a database and to define "default variables" that override
8the environment variables used by PostgreSQL.
9
10These "default variables" were designed to allow you to handle general
11connection parameters without heavy code in your programs. You can prompt the
12user for a value, put it in the default variable, and forget it, without
13having to modify your environment. The support for default variables can be
14disabled by setting the ``-DNO_DEF_VAR`` option in the Python setup file.
15Methods relative to this are specified by the tag [DV].
16
17All variables are set to ``None`` at module initialization, specifying that
18standard environment variables should be used.
19
20connect -- Open a PostgreSQL connection
21---------------------------------------
22
23.. function:: connect([dbname], [host], [port], [opt], [user], [passwd])
24
25    Open a :mod:`pg` connection
26
27    :param dbname: name of connected database (*None* = :data:`defbase`)
28    :type str: str or None
29    :param host: name of the server host (*None* = :data:`defhost`)
30    :type host:  str or None
31    :param port: port used by the database server (-1 = :data:`defport`)
32    :type port: int
33    :param opt: connection options (*None* = :data:`defopt`)
34    :type opt: str or None
35    :param user: PostgreSQL user (*None* = :data:`defuser`)
36    :type user: str or None
37    :param passwd: password for user (*None* = :data:`defpasswd`)
38    :type passwd: str or None
39    :returns: If successful, the :class:`Connection` handling the connection
40    :rtype: :class:`Connection`
41    :raises TypeError: bad argument type, or too many arguments
42    :raises SyntaxError: duplicate argument definition
43    :raises pg.InternalError: some error occurred during pg connection definition
44    :raises Exception: (all exceptions relative to object allocation)
45
46This function opens a connection to a specified database on a given
47PostgreSQL server. You can use keywords here, as described in the
48Python tutorial. The names of the keywords are the name of the
49parameters given in the syntax line. For a precise description
50of the parameters, please refer to the PostgreSQL user manual.
51
52Example::
53
54    import pg
55
56    con1 = pg.connect('testdb', 'myhost', 5432, None, None, 'bob', None)
57    con2 = pg.connect(dbname='testdb', host='localhost', user='bob')
58
59get/set_defhost -- default server host [DV]
60-------------------------------------------
61
62.. function:: get_defhost(host)
63
64    Get the default host
65
66    :returns: the current default host specification
67    :rtype: str or None
68    :raises TypeError: too many arguments
69
70This method returns the current default host specification,
71or ``None`` if the environment variables should be used.
72Environment variables won't be looked up.
73
74.. function:: set_defhost(host)
75
76    Set the default host
77
78    :param host: the new default host specification
79    :type host: str or None
80    :returns: the previous default host specification
81    :rtype: str or None
82    :raises TypeError: bad argument type, or too many arguments
83
84This methods sets the default host value for new connections.
85If ``None`` is supplied as parameter, environment variables will
86be used in future connections. It returns the previous setting
87for default host.
88
89get/set_defport -- default server port [DV]
90-------------------------------------------
91
92.. function:: get_defport()
93
94    Get the default port
95
96    :returns: the current default port specification
97    :rtype: int
98    :raises TypeError: too many arguments
99
100This method returns the current default port specification,
101or ``None`` if the environment variables should be used.
102Environment variables won't be looked up.
103
104.. function::  set_defport(port)
105
106    Set the default port
107
108    :param port: the new default port
109    :type port: int
110    :returns: previous default port specification
111    :rtype: int or None
112
113This methods sets the default port value for new connections. If -1 is
114supplied as parameter, environment variables will be used in future
115connections. It returns the previous setting for default port.
116
117get/set_defopt --  default connection options [DV]
118--------------------------------------------------
119
120.. function:: get_defopt()
121
122    Get the default connection options
123
124    :returns: the current default options specification
125    :rtype: str or None
126    :raises TypeError: too many arguments
127
128This method returns the current default connection options specification,
129or ``None`` if the environment variables should be used. Environment variables
130won't be looked up.
131
132.. function:: set_defopt(options)
133
134    Set the default connection options
135
136    :param options: the new default connection options
137    :type options: str or None
138    :returns: previous default options specification
139    :rtype: str or None
140    :raises TypeError: bad argument type, or too many arguments
141
142This methods sets the default connection options value for new connections.
143If ``None`` is supplied as parameter, environment variables will be used in
144future connections. It returns the previous setting for default options.
145
146get/set_defbase -- default database name [DV]
147---------------------------------------------
148
149.. function:: get_defbase()
150
151    Get the default database name
152
153    :returns: the current default database name specification
154    :rtype: str or None
155    :raises TypeError: too many arguments
156
157This method returns the current default database name specification, or
158``None`` if the environment variables should be used. Environment variables
159won't be looked up.
160
161.. function:: set_defbase(base)
162
163    Set the default database name
164
165    :param base: the new default base name
166    :type base: str or None
167    :returns: the previous default database name specification
168    :rtype: str or None
169    :raises TypeError: bad argument type, or too many arguments
170
171This method sets the default database name value for new connections. If
172``None`` is supplied as parameter, environment variables will be used in
173future connections. It returns the previous setting for default host.
174
175get/set_defuser -- default database user [DV]
176---------------------------------------------
177
178.. function:: get_defuser()
179
180    Get the default database user
181
182    :returns: the current default database user specification
183    :rtype: str or None
184    :raises TypeError: too many arguments
185
186This method returns the current default database user specification, or
187``None`` if the environment variables should be used. Environment variables
188won't be looked up.
189
190.. function:: set_defuser(user)
191
192    Set the default database user
193
194    :param user: the new default database user
195    :type base: str or None
196    :returns: the previous default database user specification
197    :rtype: str or None
198    :raises TypeError: bad argument type, or too many arguments
199
200This method sets the default database user name for new connections. If
201``None`` is supplied as parameter, environment variables will be used in
202future connections. It returns the previous setting for default host.
203
204get/set_defpasswd -- default database password [DV]
205---------------------------------------------------
206
207.. function:: get_defpasswd()
208
209    Get the default database password
210
211    :returns: the current default database password specification
212    :rtype: str or None
213    :raises TypeError: too many arguments
214
215This method returns the current default database password specification, or
216``None`` if the environment variables should be used. Environment variables
217won't be looked up.
218
219.. function:: set_defpasswd(passwd)
220
221    Set the default database password
222
223    :param passwd: the new default database password
224    :type base: str or None
225    :returns: the previous default database password specification
226    :rtype: str or None
227    :raises TypeError: bad argument type, or too many arguments
228
229This method sets the default database password for new connections. If
230``None`` is supplied as parameter, environment variables will be used in
231future connections. It returns the previous setting for default host.
232
233escape_string -- escape a string for use within SQL
234---------------------------------------------------
235
236.. function:: escape_string(string)
237
238    Escape a string for use within SQL
239
240    :param str string: the string that is to be escaped
241    :returns: the escaped string
242    :rtype: str
243    :raises TypeError: bad argument type, or too many arguments
244
245This function escapes a string for use within an SQL command.
246This is useful when inserting data values as literal constants
247in SQL commands. Certain characters (such as quotes and backslashes)
248must be escaped to prevent them from being interpreted specially
249by the SQL parser. :func:`escape_string` performs this operation.
250Note that there is also a :class:`Connection` method with the same name
251which takes connection properties into account.
252
253.. note::
254
255   It is especially important to do proper escaping when
256   handling strings that were received from an untrustworthy source.
257   Otherwise there is a security risk: you are vulnerable to "SQL injection"
258   attacks wherein unwanted SQL commands are fed to your database.
259
260Example::
261
262    name = input("Name? ")
263    phone = con.query("select phone from employees where name='%s'"
264        % escape_string(name)).getresult()
265
266escape_bytea -- escape binary data for use within SQL
267-----------------------------------------------------
268
269.. function:: escape_bytea(datastring)
270
271    escape binary data for use within SQL as type ``bytea``
272
273    :param str datastring: string containing the binary data that is to be escaped
274    :returns: the escaped string
275    :rtype: str
276    :raises TypeError: bad argument type, or too many arguments
277
278Escapes binary data for use within an SQL command with the type ``bytea``.
279As with :func:`escape_string`, this is only used when inserting data directly
280into an SQL command string.
281
282Note that there is also a :class:`Connection` method with the same name
283which takes connection properties into account.
284
285Example::
286
287    picture = open('garfield.gif', 'rb').read()
288    con.query("update pictures set img='%s' where name='Garfield'"
289        % escape_bytea(picture))
290
291unescape_bytea -- unescape data that has been retrieved as text
292---------------------------------------------------------------
293
294.. function:: unescape_bytea(string)
295
296    Unescape ``bytea`` data that has been retrieved as text
297
298    :param str datastring: the ``bytea`` data string that has been retrieved as text
299    :returns: byte string containing the binary data
300    :rtype: bytes
301    :raises TypeError: bad argument type, or too many arguments
302
303Converts an escaped string representation of binary data stored as ``bytea``
304into the raw byte string representing the binary data  -- this is the reverse
305of :func:`escape_bytea`.  Since the :class:`Query` results will already
306return unescaped byte strings, you normally don't have to use this method.
307
308Note that there is also a :class:`DB` method with the same name
309which does exactly the same.
310
311get/set_namedresult -- conversion to named tuples
312-------------------------------------------------
313
314.. function:: get_namedresult()
315
316    Get the function that converts to named tuples
317
318This returns the function used by PyGreSQL to construct the result of the
319:meth:`Query.namedresult` method.
320
321.. versionadded:: 4.1
322
323.. function:: set_namedresult(func)
324
325    Set a function that will convert to named tuples
326
327    :param func: the function to be used to convert results to named tuples
328
329You can use this if you want to create different kinds of named tuples
330returned by the :meth:`Query.namedresult` method.  If you set this function
331to *None*, then it will become equal to :meth:`Query.getresult`.
332
333.. versionadded:: 4.1
334
335get/set_decimal -- decimal type to be used for numeric values
336-------------------------------------------------------------
337
338.. function:: get_decimal()
339
340    Get the decimal type to be used for numeric values
341
342    :returns: the Python class used for PostgreSQL numeric values
343    :rtype: class
344
345This function returns the Python class that is used by PyGreSQL to hold
346PostgreSQL numeric values. The default class is :class:`decimal.Decimal`
347if available, otherwise the :class:`float` type is used.
348
349.. function:: set_decimal(cls)
350
351    Set a decimal type to be used for numeric values
352
353    :param class cls: the Python class to be used for PostgreSQL numeric values
354
355This function can be used to specify the Python class that shall
356be used by PyGreSQL to hold PostgreSQL numeric values.
357The default class is :class:`decimal.Decimal` if available,
358otherwise the :class:`float` type is used.
359
360get/set_decimal_point -- decimal mark used for monetary values
361--------------------------------------------------------------
362
363.. function:: get_decimal_point()
364
365    Get the decimal mark used for monetary values
366
367    :returns: string with one character representing the decimal mark
368    :rtype: str
369
370This function returns the decimal mark used by PyGreSQL to interpret
371PostgreSQL monetary values when converting them to decimal numbers.
372The default setting is ``'.'`` as a decimal point. This setting is not
373adapted automatically to the locale used by PostGreSQL, but you can use
374:func:`set_decimal()` to set a different decimal mark manually.  A return
375value of ``None`` means monetary values are not interpreted as decimal
376numbers, but returned as strings including the formatting and currency.
377
378.. versionadded:: 4.1.1
379
380.. function:: set_decimal_point(string)
381
382    Specify which decimal mark is used for interpreting monetary values
383
384    :param str string: string with one character representing the decimal mark
385
386This function can be used to specify the decimal mark used by PyGreSQL
387to interpret PostgreSQL monetary values. The default value is '.' as
388a decimal point. This value is not adapted automatically to the locale
389used by PostGreSQL, so if you are dealing with a database set to a
390locale that uses a ``','`` instead of ``'.'`` as the decimal point,
391then you need to call ``set_decimal(',')`` to have PyGreSQL interpret
392monetary values correctly. If you don't want money values to be converted
393to decimal numbers, then you can call ``set_decimal(None)``, which will
394cause PyGreSQL to return monetary values as strings including their
395formatting and currency.
396
397.. versionadded:: 4.1.1
398
399get/set_bool -- whether boolean values are returned as bool objects
400-------------------------------------------------------------------
401
402.. function:: get_bool()
403
404    Check whether boolean values are returned as bool objects
405
406    :returns: whether or not bool objects will be returned
407    :rtype: bool
408
409This function checks whether PyGreSQL returns PostgreSQL boolean
410values converted to Python bool objects, or as ``'f'`` and ``'t'``
411strings which are the values used internally by PostgreSQL. By default,
412conversion to bool objects is not activated, but you can enable
413this with the :func:`set_bool` function.
414
415.. versionadded:: 4.2
416
417.. function:: set_bool(on)
418
419    Set whether boolean values are returned as bool objects
420
421    :param on: whether or not bool objects shall be returned
422
423This function can be used to specify whether PyGreSQL shall return
424PostgreSQL boolean values converted to Python bool objects, or as
425``'f'`` and ``'t'`` strings which are the values used internally by PostgreSQL.
426By default, conversion to bool objects is not activated, but you can
427enable this by calling ``set_bool(True)``.
428
429.. versionadded:: 4.2
430
431get/set_array -- whether arrays are returned as list objects
432-------------------------------------------------------------
433
434.. function:: get_array()
435
436    Check whether arrays are returned as list objects
437
438    :returns: whether or not list objects will be returned
439    :rtype: bool
440
441This function checks whether PyGreSQL returns PostgreSQL arrays converted
442to Python list objects, or simply as text in the internal special output
443syntax of PostgreSQL.  By default, conversion to list objects is activated,
444but you can disable this with the :func:`set_array` function.
445
446.. versionadded:: 5.0
447
448.. function:: set_array(on)
449
450    Set whether arrays are returned as list objects
451
452    :param on: whether or not list objects shall be returned
453
454This function can be used to specify whether PyGreSQL shall return PostgreSQL
455arrays converted to Python list objects, or simply as text in the internal
456special output syntax of PostgreSQL.  By default, conversion to list objects
457is activated, but you can disable this by calling ``set_array(False)``.
458
459.. versionadded:: 5.0
460
461.. versionchanged:: 5.0
462   Arrays had been returned as text strings only in earlier versions.
463
464get/set_bytea_escaped -- whether bytea data is returned escaped
465---------------------------------------------------------------
466
467.. function:: get_bytea_escaped()
468
469    Check whether bytea values are returned as escaped strings
470
471    :returns: whether or not bytea objects will be returned escaped
472    :rtype: bool
473
474This function checks whether PyGreSQL returns PostgreSQL ``bytea`` values in
475escaped form or in unescaped from as byte strings.  By default, bytea values
476will be returned unescaped as byte strings, but you can change this with the
477:func:`set_bytea_escaped` function.
478
479.. versionadded:: 5.0
480
481.. function:: set_bytea_escaped(on)
482
483    Set whether bytea values are returned as escaped strings
484
485    :param on: whether or not bytea objects shall be returned escaped
486
487This function can be used to specify whether PyGreSQL shall return
488PostgreSQL ``bytea`` values in escaped form or in unescaped from as byte
489strings.  By default, bytea values will be returned unescaped as byte
490strings, but you can change this by calling ``set_bytea_escaped(True)``.
491
492.. versionadded:: 5.0
493
494.. versionchanged:: 5.0
495   Bytea data had been returned in escaped form in earlier versions.
496
497get/set_jsondecode -- decoding JSON format
498------------------------------------------
499
500.. function:: get_jsondecode()
501
502    Get the function that deserializes JSON formatted strings
503
504This returns the function used by PyGreSQL to construct Python objects
505from JSON formatted strings.
506
507.. function:: set_jsondecode(func)
508
509    Set a function that will deserialize JSON formatted strings
510
511    :param func: the function to be used for deserializing JSON strings
512
513You can use this if you do not want to deserialize JSON strings coming
514in from the database, or if want to use a different function than the
515standard function :func:`json.loads` or if you want to use it with parameters
516different from the default ones.  If you set this function to *None*, then
517the automatic deserialization of JSON strings will be deactivated.
518
519.. versionadded:: 5.0
520
521.. versionchanged:: 5.0
522   JSON data had been returned in as text strings in earlier versions.
523
524get/set_cast_hook -- fallback typecast function
525-----------------------------------------------
526
527.. function:: get_cast_hook()
528
529    Get the function that handles all external typecasting
530
531This returns the callback function used by PyGreSQL to provide plug-in
532Python typecast functions.
533
534.. function:: set_cast_hook(func)
535
536    Set a function that will handle all external typecasting
537
538    :param func: the function to be used as a callback
539
540If you set this function to *None*, then only the typecast functions
541implemented in the C extension module are enabled.  You normally would
542not want to change this.  Instead, you can use :func:`get_typecast` and
543:func:`set_typecast` to add or change the plug-in Python typecast functions.
544
545.. versionadded:: 5.0
546
547get/set_typecast -- custom typecasting
548--------------------------------------
549
550PyGreSQL uses typecast functions to cast the raw data coming from the
551database to Python objects suitable for the particular database type.
552These functions take a single string argument that represents the data
553to be casted and must return the casted value.
554
555PyGreSQL provides through its C extension module basic typecast functions
556for the common database types, but if you want to add more typecast functions,
557you can set these using the following functions.
558
559.. method:: get_typecast(typ)
560
561    Get the global cast function for the given database type
562
563    :param str typ: PostgreSQL type name
564    :returns: the typecast function for the specified type
565    :rtype: function or None
566
567.. versionadded:: 5.0
568
569.. method:: set_typecast(typ, cast)
570
571    Set a global typecast function for the given database type(s)
572
573    :param typ: PostgreSQL type name or list of type names
574    :type typ: str or list
575    :param cast: the typecast function to be set for the specified type(s)
576    :type typ: str or int
577
578.. versionadded:: 5.0
579
580Note that database connections cache types and their cast functions using
581connection specific :class:`DbTypes` objects.  You can also get, set and
582reset typecast functions on the connection level using the methods
583:meth:`DbTypes.get_typecast`, :meth:`DbTypes.set_typecast` and
584:meth:`DbTypes.reset_typecast` of the :attr:`DB.dbtypes` object.  This will
585not affect other connections or future connections.  In order to be sure
586a global change is picked up by a running connection, you must reopen it or
587call :meth:`DbTypes.reset_typecast` on the :attr:`DB.dbtypes` object.
588
589Also note that the typecasting for all of the basic types happens already
590in the C extension module.  The typecast functions that can be set with
591the above methods are only called for the types that are not already
592supported by the C extension module.
593
594Type helpers
595------------
596
597The module provides the following type helper functions.  You can wrap
598parameters with these functions when passing them to :meth:`DB.query_formatted`
599in order to give PyGreSQL a hint about the type of the parameters.
600
601.. function:: Bytea(bytes)
602
603    A wrapper for holding a bytea value
604
605.. function:: Json(obj)
606
607    A wrapper for holding an object serializable to JSON
608
609.. function:: Literal(sql)
610
611    A wrapper for holding a literal SQL string
612
613Module constants
614----------------
615
616Some constants are defined in the module dictionary.
617They are intended to be used as parameters for methods calls.
618You should refer to the libpq description in the PostgreSQL user manual
619for more information about them. These constants are:
620
621.. data:: version
622.. data:: __version__
623
624    constants that give the current version
625
626.. data:: INV_READ
627.. data:: INV_WRITE
628
629    large objects access modes,
630    used by :meth:`Connection.locreate` and :meth:`LargeObject.open`
631
632.. data:: SEEK_SET
633.. data:: SEEK_CUR
634.. data:: SEEK_END
635
636    positional flags, used by :meth:`LargeObject.seek`
637
638.. data:: TRANS_IDLE
639.. data:: TRANS_ACTIVE
640.. data:: TRANS_INTRANS
641.. data:: TRANS_INERROR
642.. data:: TRANS_UNKNOWN
643
644    transaction states, used by :meth:`Connection.transaction`
Note: See TracBrowser for help on using the repository browser.