|
221 | 221 | server is desired, a new connection *must* be established. |
222 | 222 |
|
223 | 223 |
|
224 | | -Database Interface Entry Points |
225 | | -------------------------------- |
| 224 | +Database Interface Points |
| 225 | +------------------------- |
226 | 226 |
|
227 | | -After a connection is established, the primary interface entry points are ready |
228 | | -for use. These entry points exist as properties and methods on the connection |
| 227 | +After a connection is established, the primary interface points are ready for |
| 228 | +use. These entry points exist as properties and methods on the connection |
229 | 229 | object: |
230 | 230 |
|
231 | 231 | ``prepare(sql_statement_string)`` |
|
273 | 273 | the connection is made: |
274 | 274 |
|
275 | 275 | ``version`` |
276 | | - The results of ``SELECT version()`` |
| 276 | + The results of ``SELECT version()``. |
277 | 277 | ``version_info`` |
278 | 278 | A ``sys.version_info`` form of the ``server_version`` setting. eg. ``(8, 1, 2, |
279 | 279 | 'final', 0)``. |
|
302 | 302 |
|
303 | 303 | The ``prepare`` entry point on the connection provides the standard method for |
304 | 304 | creating a `postgersql.api.PreparedStatement` instance bound to the |
305 | | -connection(``db``): |
| 305 | +connection(``db``) from an SQL statement. |
306 | 306 |
|
307 | | - >>> ps = db.prepare("SELECT 'hello, world!'") |
| 307 | +Statement objects may also be created from a statement identifier using the |
| 308 | +``statement_from_id`` method on the connection. When this method is used, the |
| 309 | +statement must have already been prepared or an error will be raised. |
| 310 | +
|
| 311 | +
|
| 312 | +Prepared Statement Interface Points |
| 313 | +----------------------------------- |
308 | 314 |
|
309 | 315 | Prepared statements are normally executed just like functions: |
310 | 316 |
|
| 317 | + >>> ps = db.prepare("SELECT 'hello, world!'") |
311 | 318 | >>> c = ps() |
312 | 319 | >>> c.read() |
313 | 320 | [('hello, world!',)] |
314 | 321 |
|
315 | | -``c``, the object returned by ``ps.__call__``, is a cursor with a |
316 | | -`postgresql.api.Cursor` interface. |
| 322 | +``c``, the object returned by the invocation of ``ps.__call__``, is a cursor |
| 323 | +object providing a `postgresql.api.Cursor` interface. |
317 | 324 |
|
318 | 325 | .. note:: |
319 | 326 | Don't confuse PG-API cursors with DB-API 2.0 cursors. |
|
365 | 372 | rules in mind. |
366 | 373 |
|
367 | 374 |
|
| 375 | +Statement Metadata |
| 376 | +------------------ |
| 377 | +
|
| 378 | +In order to provide the appropriate type transformations, the driver must |
| 379 | +acquire metadata about the statement's parameters and results. This data is |
| 380 | +published via the following properties on the statement object: |
| 381 | +
|
| 382 | + ``sql_parameter_types`` |
| 383 | + A sequence of SQL type names specifying the types of the parameters used in |
| 384 | + the statement. |
| 385 | +
|
| 386 | + ``sql_column_types`` |
| 387 | + A sequence of SQL type names specifying the types of the columns produced by |
| 388 | + the statement. `None` if the statement does not return row-data. |
| 389 | +
|
| 390 | + ``pg_parameter_types`` |
| 391 | + A sequence of PostgreSQL type Oid's specifying the types of the parameters |
| 392 | + used in the statement. |
| 393 | +
|
| 394 | + ``pg_column_types`` |
| 395 | + A sequence of PostgreSQL type Oid's specifying the types of the columns produced by |
| 396 | + the statement. `None` if the statement does not return row-data. |
| 397 | +
|
| 398 | + ``parameter_types`` |
| 399 | + A sequence of Python types that the statement expects. |
| 400 | +
|
| 401 | + ``column_types`` |
| 402 | + A sequence of Python types that the statement will produce. |
| 403 | +
|
| 404 | + ``column_names`` |
| 405 | + A sequence of `str` objects specifying the names of the columns produced by |
| 406 | + the statement. `None` if the statement does not return row-data. |
| 407 | +
|
| 408 | +The indexes of the sequence correspond to the parameter's identifier, N+1. |
| 409 | +
|
| 410 | +In order for this information to be available, the statement must be fully |
| 411 | +prepared, so if the statement is closed, it will be re-prepared when this |
| 412 | +information is accessed. |
| 413 | +
|
| 414 | + >>> ps = db.prepare("SELECT $1::integer AS intname, $2::varchar AS chardata") |
| 415 | + >>> ps.sql_parameter_types |
| 416 | + ('integer','varchar') |
| 417 | + >>> ps.sql_column_types |
| 418 | + ('integer','varchar') |
| 419 | + >>> ps.column_names |
| 420 | + ('intname','chardata') |
| 421 | + >>> ps.column_types |
| 422 | + (<class 'int'>, <class 'str'>) |
| 423 | +
|
| 424 | +
|
368 | 425 | Parameterized Statements |
369 | 426 | ------------------------ |
370 | 427 |
|
371 | | -Statements can take parameters. In order to do this, the statement must be |
372 | | -defined using PostgreSQL's positional parameter notation. ``$1``, ``$2``, |
373 | | -``$3``, etc: |
| 428 | +Statements can take parameters. Using statement parameters is the recommended |
| 429 | +way to interrogate the database when variable information is needed to formulate |
| 430 | +a complete request. In order to do this, the statement must be defined using |
| 431 | +PostgreSQL's positional parameter notation. ``$1``, ``$2``, ``$3``, etc: |
374 | 432 |
|
375 | 433 | >>> ps = db.prepare("SELECT $1") |
376 | 434 | >>> c = ps('hello, world!') |
|
399 | 457 |
|
400 | 458 | Parameters are typed. PostgreSQL servers provide the driver with the |
401 | 459 | type information about a positional parameter, and the driver will require that |
402 | | -a given parameter is in the appropriate type. The Python types expected by the driver |
403 | | -for a given SQL and PostgreSQL type are listed in `Type Support`_. |
| 460 | +a given parameter is in the appropriate type as required by the serialization |
| 461 | +routines. The Python types expected by the driver for a given SQL and PostgreSQL |
| 462 | +type are listed in `Type Support`_. |
404 | 463 |
|
405 | 464 | This usage of Python types that are included in the standard library is not always |
406 | 465 | convenient. Notably, the `datetime` module does not provide a friendly way for a |
|
428 | 487 | conversion. |
429 | 488 |
|
430 | 489 |
|
431 | | -Statement Metadata |
432 | | ------------------- |
433 | | -
|
434 | | -In order to provide the appropriate type transformations, the driver must |
435 | | -acquire metadata about the statement's parameters and results. This data is |
436 | | -published via the following properties on the statement object: |
437 | | -
|
438 | | - ``sql_parameter_types`` |
439 | | - A sequence of SQL type names specifying the types of the parameters used in |
440 | | - the statement. |
441 | | -
|
442 | | - ``sql_column_types`` |
443 | | - A sequence of SQL type names specifying the types of the columns produced by |
444 | | - the statement. `None` if the statement does not return row-data. |
445 | | -
|
446 | | - ``pg_parameter_types`` |
447 | | - A sequence of PostgreSQL type Oid's specifying the types of the parameters |
448 | | - used in the statement. |
449 | | -
|
450 | | - ``pg_column_types`` |
451 | | - A sequence of PostgreSQL type Oid's specifying the types of the columns produced by |
452 | | - the statement. `None` if the statement does not return row-data. |
453 | | -
|
454 | | - ``parameter_types`` |
455 | | - A sequence of Python types that the statement expects. |
456 | | -
|
457 | | - ``column_types`` |
458 | | - A sequence of Python types that the statement will produce. |
459 | | -
|
460 | | - ``column_names`` |
461 | | - A sequence of `str` objects specifying the names of the columns produced by |
462 | | - the statement. `None` if the statement does not return row-data. |
463 | | -
|
464 | | -The indexes of the sequence correspond to the parameter's identifier, N+1. |
465 | | -
|
466 | | -In order for this information to be available, the statement must be fully |
467 | | -prepared, so if the statement is closed, it will be re-prepared when this |
468 | | -information is accessed. |
469 | | -
|
470 | | - >>> ps = db.prepare("SELECT $1::integer AS intname, $2::varchar AS chardata") |
471 | | - >>> ps.sql_parameter_types |
472 | | - ('integer','varchar') |
473 | | - >>> ps.sql_column_types |
474 | | - ('integer','varchar') |
475 | | - >>> ps.column_names |
476 | | - ('intname','chardata') |
477 | | - >>> ps.column_types |
478 | | - (<class 'int'>, <class 'str'>) |
479 | | -
|
480 | | -
|
481 | 490 | Inserting and DML |
482 | 491 | ----------------- |
483 | 492 |
|
|
519 | 528 |
|
520 | 529 |
|
521 | 530 | Cursors |
522 | | -------- |
| 531 | +======= |
523 | 532 |
|
524 | 533 | When a prepared statement is called, a `postgresql.api.Cursor` is created and |
525 | 534 | returned. The type of statement ultimately decides the kind of cursor used to |
526 | 535 | manage the results. |
527 | 536 |
|
| 537 | +Cursors can also be created directly from ``cursor_id``'s using the |
| 538 | +``cursor_from_id`` method on connection objects. |
| 539 | +
|
| 540 | +
|
| 541 | +Cursor Interface Points |
| 542 | +----------------------- |
| 543 | +
|
528 | 544 | For cursors that return row data, these interfaces are provided for accessing |
529 | 545 | those results: |
530 | 546 |
|
|
550 | 566 | with the ``chunksize`` attribute on the cursor object itself. This is |
551 | 567 | normally the most efficient way to get rows out of the cursor. |
552 | 568 |
|
| 569 | + ``seek(position[, whence = 0])`` |
| 570 | + When the cursor is scrollable, this seek interface can be used to move the |
| 571 | + position of the cursor. See `Scrollable Cursors`_ for more information. |
| 572 | +
|
| 573 | +
|
| 574 | +Cursor Metadata |
| 575 | +--------------- |
| 576 | +
|
| 577 | +Cursors normally share metadata with the statements that create them, so it is |
| 578 | +usually unnecessary for referencing the cursor's column descriptions directly. |
| 579 | +However, when a cursor is opened from an identifier, the cursor interface must |
| 580 | +collect the metadata itself. These attributes provide the metadata in absence of |
| 581 | +a statement object: |
| 582 | +
|
| 583 | + ``sql_column_types`` |
| 584 | + A sequence of SQL type names specifying the types of the columns produced by |
| 585 | + the cursor. `None` if the cursor does not return row-data. |
| 586 | +
|
| 587 | + ``pg_column_types`` |
| 588 | + A sequence of PostgreSQL type Oid's specifying the types of the columns produced by |
| 589 | + the cursor. `None` if the cursor does not return row-data. |
| 590 | +
|
| 591 | + ``column_types`` |
| 592 | + A sequence of Python types that the cursor will produce. |
| 593 | +
|
| 594 | + ``column_names`` |
| 595 | + A sequence of `str` objects specifying the names of the columns produced by |
| 596 | + the cursor. `None` if the cursor does not return row-data. |
| 597 | +
|
553 | 598 |
|
554 | 599 | Scrollable Cursors |
555 | | -^^^^^^^^^^^^^^^^^^ |
| 600 | +------------------ |
556 | 601 |
|
557 | 602 | By default, cursors are not scrollable. It is assumed, for performance reasons, |
558 | 603 | that the user just wants the results in a linear fashion. However, scrollable |
|
609 | 654 |
|
610 | 655 |
|
611 | 656 | COPY Cursors |
612 | | -^^^^^^^^^^^^ |
| 657 | +------------ |
613 | 658 |
|
614 | 659 | `postgresql.driver` transparently supports PostgreSQL's COPY command. To the |
615 | 660 | user, it will act exactly like a cursor that produces tuples; COPY tuples, |
|
762 | 807 | The context manager interfaces are higher level interfaces to the explicit |
763 | 808 | instruction methods provided by `postgresql.api.Transaction` objects. |
764 | 809 |
|
765 | | -Transaction Interface Entry Points |
766 | | ----------------------------------- |
| 810 | +
|
| 811 | +Transaction Interface Points |
| 812 | +---------------------------- |
767 | 813 |
|
768 | 814 | The methods available on transaction objects manage the state of the transaction |
769 | 815 | and relay any necessary instructions to the remote server in order to reflect |
|
798 | 844 | * If the transaction is configured with a `gid`, the ``prepare()`` method |
799 | 845 | *must* be invoked prior to the ``commit()``. |
800 | 846 | * If the database is in an error state when the commit() is issued, an implicit |
801 | | - rollback will occur. Usually, when the database is in an error state, an |
| 847 | + rollback will occur. Usually, when the database is in an error state, |
802 | 848 | a database exception will have been thrown, so it is likely that it will be |
803 | 849 | understood that a rollback should occur. |
804 | 850 |
|
|
0 commit comments