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

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

Return bools from Postgres as Python bools by default

This has been done by the DB-API 2 module already and was configurable
in the classic module since a long time, but by default it was disabled.
It is now time to flip the switch and have this activated by default in
version 5.0. You can still restore the old behavior with set_bool(False).

File size: 22.3 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 activated, but you can disable this with
413the :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
426PostgreSQL.  By default, conversion to bool objects is activated,
427but you can disable this by calling ``set_bool(True)``.
428
429.. versionadded:: 4.2
430
431.. versionchanged:: 5.0
432   Boolean values had been returned as string by default in earlier versions.
433
434get/set_array -- whether arrays are returned as list objects
435-------------------------------------------------------------
436
437.. function:: get_array()
438
439    Check whether arrays are returned as list objects
440
441    :returns: whether or not list objects will be returned
442    :rtype: bool
443
444This function checks whether PyGreSQL returns PostgreSQL arrays converted
445to Python list objects, or simply as text in the internal special output
446syntax of PostgreSQL.  By default, conversion to list objects is activated,
447but you can disable this with the :func:`set_array` function.
448
449.. versionadded:: 5.0
450
451.. function:: set_array(on)
452
453    Set whether arrays are returned as list objects
454
455    :param on: whether or not list objects shall be returned
456
457This function can be used to specify whether PyGreSQL shall return PostgreSQL
458arrays converted to Python list objects, or simply as text in the internal
459special output syntax of PostgreSQL.  By default, conversion to list objects
460is activated, but you can disable this by calling ``set_array(False)``.
461
462.. versionadded:: 5.0
463
464.. versionchanged:: 5.0
465   Arrays had been always returned as text strings only in earlier versions.
466
467get/set_bytea_escaped -- whether bytea data is returned escaped
468---------------------------------------------------------------
469
470.. function:: get_bytea_escaped()
471
472    Check whether bytea values are returned as escaped strings
473
474    :returns: whether or not bytea objects will be returned escaped
475    :rtype: bool
476
477This function checks whether PyGreSQL returns PostgreSQL ``bytea`` values in
478escaped form or in unescaped from as byte strings.  By default, bytea values
479will be returned unescaped as byte strings, but you can change this with the
480:func:`set_bytea_escaped` function.
481
482.. versionadded:: 5.0
483
484.. function:: set_bytea_escaped(on)
485
486    Set whether bytea values are returned as escaped strings
487
488    :param on: whether or not bytea objects shall be returned escaped
489
490This function can be used to specify whether PyGreSQL shall return
491PostgreSQL ``bytea`` values in escaped form or in unescaped from as byte
492strings.  By default, bytea values will be returned unescaped as byte
493strings, but you can change this by calling ``set_bytea_escaped(True)``.
494
495.. versionadded:: 5.0
496
497.. versionchanged:: 5.0
498   Bytea data had been returned in escaped form by default in earlier versions.
499
500get/set_jsondecode -- decoding JSON format
501------------------------------------------
502
503.. function:: get_jsondecode()
504
505    Get the function that deserializes JSON formatted strings
506
507This returns the function used by PyGreSQL to construct Python objects
508from JSON formatted strings.
509
510.. function:: set_jsondecode(func)
511
512    Set a function that will deserialize JSON formatted strings
513
514    :param func: the function to be used for deserializing JSON strings
515
516You can use this if you do not want to deserialize JSON strings coming
517in from the database, or if want to use a different function than the
518standard function :func:`json.loads` or if you want to use it with parameters
519different from the default ones.  If you set this function to *None*, then
520the automatic deserialization of JSON strings will be deactivated.
521
522.. versionadded:: 5.0
523
524.. versionchanged:: 5.0
525   JSON data had been always returned as text strings in earlier versions.
526
527get/set_cast_hook -- fallback typecast function
528-----------------------------------------------
529
530.. function:: get_cast_hook()
531
532    Get the function that handles all external typecasting
533
534This returns the callback function used by PyGreSQL to provide plug-in
535Python typecast functions.
536
537.. function:: set_cast_hook(func)
538
539    Set a function that will handle all external typecasting
540
541    :param func: the function to be used as a callback
542
543If you set this function to *None*, then only the typecast functions
544implemented in the C extension module are enabled.  You normally would
545not want to change this.  Instead, you can use :func:`get_typecast` and
546:func:`set_typecast` to add or change the plug-in Python typecast functions.
547
548.. versionadded:: 5.0
549
550get/set_typecast -- custom typecasting
551--------------------------------------
552
553PyGreSQL uses typecast functions to cast the raw data coming from the
554database to Python objects suitable for the particular database type.
555These functions take a single string argument that represents the data
556to be casted and must return the casted value.
557
558PyGreSQL provides through its C extension module basic typecast functions
559for the common database types, but if you want to add more typecast functions,
560you can set these using the following functions.
561
562.. method:: get_typecast(typ)
563
564    Get the global cast function for the given database type
565
566    :param str typ: PostgreSQL type name
567    :returns: the typecast function for the specified type
568    :rtype: function or None
569
570.. versionadded:: 5.0
571
572.. method:: set_typecast(typ, cast)
573
574    Set a global typecast function for the given database type(s)
575
576    :param typ: PostgreSQL type name or list of type names
577    :type typ: str or list
578    :param cast: the typecast function to be set for the specified type(s)
579    :type typ: str or int
580
581.. versionadded:: 5.0
582
583Note that database connections cache types and their cast functions using
584connection specific :class:`DbTypes` objects.  You can also get, set and
585reset typecast functions on the connection level using the methods
586:meth:`DbTypes.get_typecast`, :meth:`DbTypes.set_typecast` and
587:meth:`DbTypes.reset_typecast` of the :attr:`DB.dbtypes` object.  This will
588not affect other connections or future connections.  In order to be sure
589a global change is picked up by a running connection, you must reopen it or
590call :meth:`DbTypes.reset_typecast` on the :attr:`DB.dbtypes` object.
591
592Also note that the typecasting for all of the basic types happens already
593in the C extension module.  The typecast functions that can be set with
594the above methods are only called for the types that are not already
595supported by the C extension module.
596
597Type helpers
598------------
599
600The module provides the following type helper functions.  You can wrap
601parameters with these functions when passing them to :meth:`DB.query_formatted`
602in order to give PyGreSQL a hint about the type of the parameters.
603
604.. function:: Bytea(bytes)
605
606    A wrapper for holding a bytea value
607
608.. function:: Json(obj)
609
610    A wrapper for holding an object serializable to JSON
611
612.. function:: Literal(sql)
613
614    A wrapper for holding a literal SQL string
615
616Module constants
617----------------
618
619Some constants are defined in the module dictionary.
620They are intended to be used as parameters for methods calls.
621You should refer to the libpq description in the PostgreSQL user manual
622for more information about them. These constants are:
623
624.. data:: version
625.. data:: __version__
626
627    constants that give the current version
628
629.. data:: INV_READ
630.. data:: INV_WRITE
631
632    large objects access modes,
633    used by :meth:`Connection.locreate` and :meth:`LargeObject.open`
634
635.. data:: SEEK_SET
636.. data:: SEEK_CUR
637.. data:: SEEK_END
638
639    positional flags, used by :meth:`LargeObject.seek`
640
641.. data:: TRANS_IDLE
642.. data:: TRANS_ACTIVE
643.. data:: TRANS_INTRANS
644.. data:: TRANS_INERROR
645.. data:: TRANS_UNKNOWN
646
647    transaction states, used by :meth:`Connection.transaction`
Note: See TracBrowser for help on using the repository browser.