pgtoolkit/pgmanager.py
author Radek Brich <radek.brich@devl.cz>
Thu, 21 Mar 2013 17:40:11 +0100
changeset 80 b830c6f7b30a
parent 77 2cfef775f518
child 81 f709b1c7ca0c
permissions -rw-r--r--
PgManager: add support for str, repr of RowDict.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
#
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
# PgManager - manage database connections
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
#
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
     5
# Requires: Python 3.2, psycopg2
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
#
9
2fcc8ef0b97d Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
     7
# Part of pgtoolkit
2fcc8ef0b97d Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
     8
# http://hg.devl.cz/pgtoolkit
2fcc8ef0b97d Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
     9
#
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 70
diff changeset
    10
# Copyright (c) 2010, 2011, 2012, 2013  Radek Brich <radek.brich@devl.cz>
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
#
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
# Permission is hereby granted, free of charge, to any person obtaining a copy
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
# of this software and associated documentation files (the "Software"), to deal
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
# in the Software without restriction, including without limitation the rights
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
# copies of the Software, and to permit persons to whom the Software is
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
# furnished to do so, subject to the following conditions:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
#
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
# The above copyright notice and this permission notice shall be included in
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
# all copies or substantial portions of the Software.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
#
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
# THE SOFTWARE.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
"""Postgres database connection manager
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 70
diff changeset
    32
PgManager wraps psycopg2, adding following features:
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
70
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
    34
 * Save and reuse database connection parameters
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
70
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
    36
 * Connection pooling
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
70
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
    38
 * Easy query using the with statement
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
70
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
    40
 * Dictionary rows
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
    41
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
    42
Example usage:
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 70
diff changeset
    44
    from pgtoolkit import pgmanager
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
70
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
    46
    pgm = pgmanager.get_instance()
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
    47
    pgm.create_conn(hostaddr='127.0.0.1', dbname='postgres')
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
70
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
    49
    with pgm.cursor() as curs:
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
    50
        curs.execute('SELECT now() AS now')
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
    51
        row = curs.fetchone_dict()
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
    52
        print(row.now)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
First, we have obtained PgManager instance. This is like calling
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
PgManager(), although in our example the instance is global. That means
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
getting the instance in another module brings us all the defined connections
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
etc.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
9
2fcc8ef0b97d Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
    59
On second line we have created connection named 'default' (this name can be left out).
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
The with statement obtains connection (actually connects to database when needed),
9
2fcc8ef0b97d Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
    61
then returns cursor for this connection. At the end of with statement,
2fcc8ef0b97d Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
    62
the connection is returned to the pool or closed (depending on number of connections
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
    63
in pool and on setting of pool_size parameter).
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
The row returned by fetchone_dict() is special dict object, which can be accessed
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
using item or attribute access, that is row['now'] or row.now.
70
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
    67
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
"""
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
from contextlib import contextmanager
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
    71
from collections import OrderedDict
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
import logging
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
import threading
24
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
    74
import multiprocessing
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
import select
8
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
    76
import socket
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
import psycopg2
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
import psycopg2.extensions
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
19
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    81
from psycopg2 import DatabaseError, IntegrityError, OperationalError
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
26
7f219da7ab71 Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    84
log_sql = logging.getLogger("pgmanager_sql")
7f219da7ab71 Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    85
log_notices = logging.getLogger("pgmanager_notices")
75
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
    86
log_sql.addHandler(logging.NullHandler())
39f777341db4 MyManager: Add Cursor.mogrify(). Fix query logging. Update tests.
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
    87
# NullHandler not needed for notices which are INFO level only
20
73f0d53fef6b PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents: 19
diff changeset
    88
73f0d53fef6b PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents: 19
diff changeset
    89
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
class PgManagerError(Exception):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
    pass
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
class ConnectionInfo:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
    97
    def __init__(self, name, dsn, isolation_level=None, keep_alive=True,
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
    98
                 init_statement=None, pool_size=1):
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
    99
        self.name = name  # connection name is logged with SQL queries
42
9e3775460792 PgManager: update comments.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
   100
        self.dsn = dsn  # dsn or string with connection parameters
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
        self.isolation_level = isolation_level
19
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   102
        self.keep_alive = keep_alive
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
        self.init_statement = init_statement
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   104
        self.pool_size = pool_size
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   106
45
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
   107
class RowDict:
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   108
    """Special read-only dictionary used for rows returned from queries.
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   109
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   110
    Initialization is same as for dict:
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   111
        row = RowDict([('id', 123), ('name', 'hello')])
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   112
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   113
    Allows key and attribute access to contained items:
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   114
        row['id']
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   115
        row.id
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   116
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   117
    Items keep order in which columns where returned from database.
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   118
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   119
    Tuple style access is also supported:
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   120
        row[0]
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   121
        id, name = row
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   122
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   123
    """
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   124
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   125
    def __init__(self, data):
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   126
        self._dict = OrderedDict(data)
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   127
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   128
    def __getitem__(self, key):
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   129
        if isinstance(key, int):
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   130
            return tuple(self._dict.values())[key]
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   131
        return self._dict[key]
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   132
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   133
    def __getattr__(self, key):
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   134
        try:
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   135
            return self._dict[key]
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   136
        except KeyError:
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   137
            raise AttributeError(key)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   138
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   139
    def __contains__(self, key):
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   140
        return key in self._dict
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   141
48
b82c7c2fb5af PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   142
    def keys(self):
b82c7c2fb5af PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   143
        return self._dict.keys()
b82c7c2fb5af PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   144
54
291473ab847c Fix PgDataDiff.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   145
    def values(self):
291473ab847c Fix PgDataDiff.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   146
        return self._dict.values()
291473ab847c Fix PgDataDiff.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   147
55
adc1615d8fc5 Fix PgDataDiff.
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   148
    def items(self):
adc1615d8fc5 Fix PgDataDiff.
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   149
        return self._dict.items()
adc1615d8fc5 Fix PgDataDiff.
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
   150
80
b830c6f7b30a PgManager: add support for str, repr of RowDict.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   151
    def __repr__(self):
b830c6f7b30a PgManager: add support for str, repr of RowDict.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   152
        return 'RowDict(%s)' % str(self._dict.items())
b830c6f7b30a PgManager: add support for str, repr of RowDict.
Radek Brich <radek.brich@devl.cz>
parents: 77
diff changeset
   153
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   154
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   155
class Cursor(psycopg2.extensions.cursor):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   156
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   157
    def execute(self, query, args=None):
48
b82c7c2fb5af PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   158
        # log query before executing
b82c7c2fb5af PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   159
        self._log_query(query, args)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   160
        try:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   161
            return super(Cursor, self).execute(query, args)
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   162
        except DatabaseError:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   163
            self._log_exception()
46
037410ef2b6b Fix query logging.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
   164
            raise
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   165
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   166
    def callproc(self, procname, args=None):
48
b82c7c2fb5af PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   167
        # log query before executing (not query actually executed but should correspond)
b82c7c2fb5af PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   168
        self._log_query(self._build_callproc_query(procname, len(args)), args)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   169
        try:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   170
            return super(Cursor, self).callproc(procname, args)
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   171
        except DatabaseError:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   172
            self._log_exception()
46
037410ef2b6b Fix query logging.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
   173
            raise
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   174
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   175
    def row_dict(self, row, lstrip=None):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   176
        adjustname = lambda a: a
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   177
        if lstrip:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   178
            adjustname = lambda a: a.lstrip(lstrip)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   179
        return RowDict(zip([adjustname(desc[0]) for desc in self.description], row))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   180
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   181
    def fetchone_dict(self, lstrip=None):
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   182
        '''Return one row as OrderedDict'''
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   183
        row = super(Cursor, self).fetchone()
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   184
        if row is None:
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   185
            return None
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   186
        return self.row_dict(row, lstrip)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   187
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   188
    def fetchall_dict(self, lstrip=None):
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   189
        '''Return all rows as OrderedDict'''
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   190
        rows = super(Cursor, self).fetchall()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   191
        return [self.row_dict(row, lstrip) for row in rows]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   192
41
6aad5e35efe8 PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   193
    def adapt(self, row):
6aad5e35efe8 PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   194
        if isinstance(row, RowDict):
6aad5e35efe8 PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   195
            # dict
54
291473ab847c Fix PgDataDiff.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   196
            adapted = dict()
41
6aad5e35efe8 PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   197
            for key in row.keys():
6aad5e35efe8 PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   198
                adapted[key] = self.mogrify('%s', [row[key]]).decode('utf8')
54
291473ab847c Fix PgDataDiff.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   199
            return RowDict(adapted)
41
6aad5e35efe8 PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   200
        else:
6aad5e35efe8 PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   201
            # list
54
291473ab847c Fix PgDataDiff.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   202
            return [self.mogrify('%s', [x]).decode('utf8') for x in row]
41
6aad5e35efe8 PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   203
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   204
    def fetchone_adapted(self, lstrip=None):
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   205
        '''Like fetchone_dict() but values are quoted for direct inclusion in SQL query.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   206
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   207
        This is useful when you need to generate SQL script from data returned
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   208
        by the query. Use mogrify() for simple cases.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   209
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   210
        '''
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   211
        row = super(Cursor, self).fetchone()
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   212
        if row is None:
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   213
            return None
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   214
        return self.row_dict([self.mogrify('%s', [x]).decode('utf8') for x in row], lstrip)
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   215
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   216
    def fetchall_adapted(self, lstrip=None):
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   217
        '''Like fetchall_dict() but values are quoted for direct inclusion in SQL query.'''
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   218
        rows = super(Cursor, self).fetchall()
31
c2e6e24b83d9 Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents: 26
diff changeset
   219
        return [self.row_dict([self.mogrify('%s', [x]).decode('utf8') for x in row], lstrip) for row in rows]
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   220
46
037410ef2b6b Fix query logging.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
   221
    def _log_query(self, query='?', args=None):
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   222
        name = self.connection.name if hasattr(self.connection, 'name') else '-'
46
037410ef2b6b Fix query logging.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
   223
        query = self.query or self.mogrify(query, args)
77
2cfef775f518 PgManager, MyManager: Change log level of SQL queries to debug. Fix MyManager: Wrong query logged when exception occurs.
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
   224
        log_sql.debug('[%s] %s' % (name, query.decode('utf8')))
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   225
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   226
    def _log_exception(self):
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   227
        name = self.connection.name if hasattr(self.connection, 'name') else '-'
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   228
        log_sql.exception('[%s] exception:' % (name,))
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   229
46
037410ef2b6b Fix query logging.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
   230
    def _build_callproc_query(self, procname, num_args):
48
b82c7c2fb5af PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   231
        return 'SELECT * FROM %s(%s)' % (procname, ', '.join(['%s'] * num_args))
46
037410ef2b6b Fix query logging.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
   232
037410ef2b6b Fix query logging.
Radek Brich <radek.brich@devl.cz>
parents: 45
diff changeset
   233
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   234
class Connection(psycopg2.extensions.connection):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   235
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   236
    def cursor(self, name=None):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   237
        if name is None:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   238
            return super(Connection, self).cursor(cursor_factory=Cursor)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   239
        else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   240
            return super(Connection, self).cursor(name, cursor_factory=Cursor)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   241
8
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   242
    def keep_alive(self):
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   243
        '''Set socket to keepalive mode. Must be called before any query.'''
37
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   244
        sock = socket.fromfd(self.fileno(), socket.AF_INET, socket.SOCK_STREAM)
8
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   245
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
19
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   246
        try:
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   247
            # Maximum keep-alive probes before asuming the connection is lost
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   248
            sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   249
            # Interval (in seconds) between keep-alive probes
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   250
            sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 2)
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   251
            # Maximum idle time (in seconds) before start sending keep-alive probes
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   252
            sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 10)
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   253
        except socket.error:
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   254
            pass
37
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   255
        # close duplicated fd, options set for socket stays
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   256
        sock.close()
8
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   257
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   258
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   259
class PgManager:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   260
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   261
    def __init__(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   262
        self.conn_known = {}  # available connections
24
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   263
        self.conn_pool = {}  # active connetions
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   264
        self.lock = threading.Lock()  # mutual exclusion for threads
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   265
        self.pid = multiprocessing.current_process().pid  # forking check
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   266
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   267
    def __del__(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   268
        for conn in tuple(self.conn_known.keys()):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   269
            self.destroy_conn(conn)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   270
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   271
    def create_conn(self, name='default', isolation_level=None, keep_alive=True, init_statement=None,
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   272
                    pool_size=1, dsn=None, **kwargs):
19
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   273
        '''Create named connection.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   274
70
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   275
        *name* -- name for connection
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   276
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   277
        *pool_size* -- how many connections will be kept open in pool.
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   278
        More connections will still be created but they will be closed by put_conn.
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   279
        `None` will disable pool, get_conn() will then always return same connection.
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   280
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   281
        *isolation_level* -- `"autocommit"`, `"read_committed"`, `"serializable"` or `None` for driver default
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   282
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   283
        *keep_alive* -- set socket to keepalive mode
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   284
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   285
        *dsn* -- connection string (parameters or data source name)
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   286
42
9e3775460792 PgManager: update comments.
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
   287
        Other keyword args are used as connection parameters.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   288
19
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   289
        '''
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   290
        if name in self.conn_known:
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   291
            raise PgManagerError('Connection name "%s" already registered.' % name)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   292
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   293
        if dsn is None:
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   294
            dsn = ' '.join([x[0]+'='+str(x[1]) for x in kwargs.items() if x[1] is not None])
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   295
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   296
        isolation_level = self._normalize_isolation_level(isolation_level)
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   297
        ci = ConnectionInfo(name, dsn, isolation_level, keep_alive, init_statement, pool_size)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   298
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   299
        self.conn_known[name] = ci
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   300
        self.conn_pool[name] = []
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   301
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   302
    def create_conn_listen(self, name, channel, dsn=None, copy_dsn=None, **kwargs):
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   303
        '''Create connection listening for notifies.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   304
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   305
        Disables pool. If you want to use pool, create other connection for that.
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   306
        This connection can be used as usual: conn.cursor() etc.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   307
        Don't use PgManager's cursor() and put_conn().
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   308
70
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   309
        *name* -- name for connection
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   310
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   311
        *channel* -- listen on this channel
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   312
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   313
        *copy_dsn* -- specify name of other connection and its dsn will be used
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   314
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   315
        Other parameters forwarded to create_conn().
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   316
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   317
        '''
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   318
        if dsn is None and copy_dsn:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   319
            try:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   320
                dsn = self.conn_known[copy_dsn].dsn
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   321
            except KeyError:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   322
                raise PgManagerError("Connection name '%s' not registered." % copy_dsn)
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   323
        listen_query = "LISTEN " + channel
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   324
        self.create_conn(name=name, pool_size=None, isolation_level='autocommit', init_statement=listen_query,
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   325
            dsn=dsn, **kwargs)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   326
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   327
    def close_conn(self, name='default'):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   328
        '''Close all connections of given name.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   329
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   330
        Connection credentials are still saved.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   331
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   332
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   333
        while len(self.conn_pool[name]):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   334
            conn = self.conn_pool[name].pop()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   335
            conn.close()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   336
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   337
    def destroy_conn(self, name='default'):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   338
        '''Destroy connection.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   339
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   340
        Counterpart of create_conn.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   341
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   342
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   343
        if not name in self.conn_known:
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   344
            raise PgManagerError('Connection name "%s" not registered.' % name)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   345
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   346
        self.close_conn(name)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   347
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   348
        del self.conn_known[name]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   349
        del self.conn_pool[name]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   350
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   351
    def get_conn(self, name='default'):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   352
        '''Get connection of name 'name' from pool.'''
24
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   353
        self._check_fork()
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   354
        self.lock.acquire()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   355
        try:
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   356
            try:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   357
                ci = self.conn_known[name]
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   358
            except KeyError:
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   359
                raise PgManagerError("Connection name '%s' not registered." % name)
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   360
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   361
            # no pool, just one static connection
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   362
            if ci.pool_size is None:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   363
                # check for existing connection
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   364
                try:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   365
                    conn = self.conn_pool[name][0]
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   366
                    if conn.closed:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   367
                        conn = None
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   368
                except IndexError:
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   369
                    conn = None
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   370
                    self.conn_pool[name].append(conn)
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   371
                # if no existing connection is valid, connect new one and save it
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   372
                if conn is None:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   373
                    conn = self._connect(ci)
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   374
                    self.conn_pool[name][0] = conn
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   375
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   376
            # connection from pool
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   377
            else:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   378
                conn = None
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   379
                while len(self.conn_pool[name]) and conn is None:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   380
                    conn = self.conn_pool[name].pop()
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   381
                    if conn.closed:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   382
                        conn = None
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   383
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   384
                if conn is None:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   385
                    conn = self._connect(ci)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   386
        finally:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   387
            self.lock.release()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   388
        return conn
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   389
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   390
    def put_conn(self, conn, name='default'):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   391
        '''Put connection back to pool.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   392
70
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   393
        *name* must be same as used for get_conn, otherwise things become broken.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   394
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   395
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   396
        self.lock.acquire()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   397
        try:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   398
            if not name in self.conn_known:
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   399
                raise PgManagerError("Connection name '%s' not registered." % name)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   400
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   401
            if len(self.conn_pool[name]) >= self.conn_known[name].pool_size:
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   402
                conn.close()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   403
                return
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   404
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   405
            if conn.get_transaction_status() == psycopg2.extensions.TRANSACTION_STATUS_UNKNOWN:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   406
                conn.close()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   407
                return
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   408
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   409
            # connection returned to the pool must not be in transaction
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   410
            if conn.get_transaction_status() != psycopg2.extensions.TRANSACTION_STATUS_IDLE:
33
bd0beda49bcb PgManager: log connection name with queries. BatchUpdateTool: handle some possible exceptions and try reconnect to database.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
   411
                try:
bd0beda49bcb PgManager: log connection name with queries. BatchUpdateTool: handle some possible exceptions and try reconnect to database.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
   412
                    conn.rollback()
bd0beda49bcb PgManager: log connection name with queries. BatchUpdateTool: handle some possible exceptions and try reconnect to database.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
   413
                except OperationalError:
bd0beda49bcb PgManager: log connection name with queries. BatchUpdateTool: handle some possible exceptions and try reconnect to database.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
   414
                    if not conn.closed:
bd0beda49bcb PgManager: log connection name with queries. BatchUpdateTool: handle some possible exceptions and try reconnect to database.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
   415
                        conn.close()
bd0beda49bcb PgManager: log connection name with queries. BatchUpdateTool: handle some possible exceptions and try reconnect to database.
Radek Brich <radek.brich@devl.cz>
parents: 31
diff changeset
   416
                    return
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   417
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   418
            self.conn_pool[name].append(conn)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   419
        finally:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   420
            self.lock.release()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   421
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   422
    @contextmanager
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   423
    def cursor(self, name='default'):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   424
        '''Cursor context.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   425
70
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   426
        Uses any connection info with *name* from pool
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   427
        and returns cursor for that connection.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   428
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   429
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   430
        conn = self.get_conn(name)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   431
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   432
        try:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   433
            curs = conn.cursor()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   434
            yield curs
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   435
        finally:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   436
            curs.close()
26
7f219da7ab71 Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   437
            self.log_notices(conn)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   438
            self.put_conn(conn, name)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   439
26
7f219da7ab71 Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   440
    def log_notices(self, conn):
7f219da7ab71 Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   441
        for notice in conn.notices:
7f219da7ab71 Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   442
            log_notices.info(notice.rstrip())
7f219da7ab71 Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   443
        conn.notices[:] = []
7f219da7ab71 Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
   444
70
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   445
    def wait_for_notify(self, name='default', timeout=None):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   446
        '''Wait for asynchronous notifies, return the last one.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   447
70
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   448
        *name* -- name of connection, must be created using `create_conn_listen()`
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   449
70
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   450
        *timeout* -- in seconds, floating point (`None` means wait forever)
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   451
77e65040711c Update docs.
Radek Brich <radek.brich@devl.cz>
parents: 55
diff changeset
   452
        Returns `None` on timeout.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   453
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   454
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   455
        conn = self.get_conn(name)
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   456
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   457
        # return any notifies on stack
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   458
        if conn.notifies:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   459
            return conn.notifies.pop()
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   460
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   461
        if select.select([conn], [], [], timeout) == ([], [], []):
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   462
            # timeout
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   463
            return None
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   464
        else:
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   465
            conn.poll()
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   466
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   467
            # return just the last notify (we do not care for older ones)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   468
            if conn.notifies:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   469
                return conn.notifies.pop()
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   470
            return None
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   471
8
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   472
    def _connect(self, ci):
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   473
        conn = psycopg2.connect(ci.dsn, connection_factory=Connection)
36
e67101c22e83 pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents: 33
diff changeset
   474
        conn.name = ci.name
19
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   475
        if ci.keep_alive:
e526ca146fa9 Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   476
            conn.keep_alive()
8
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   477
        if not ci.isolation_level is None:
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   478
            conn.set_isolation_level(ci.isolation_level)
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   479
        if ci.init_statement:
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   480
            curs = conn.cursor()
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   481
            curs.execute(ci.init_statement)
71
4251068a251a Update MyManager.
Radek Brich <radek.brich@devl.cz>
parents: 70
diff changeset
   482
            curs.connection.commit()
8
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   483
            curs.close()
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   484
        return conn
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   485
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   486
    def _normalize_isolation_level(self, level):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   487
        if type(level) == str:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   488
            if level.lower() == 'autocommit':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   489
                return psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   490
            if level.lower() == 'read_committed':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   491
                return psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   492
            if level.lower() == 'serializable':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   493
                return psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   494
            raise PgManagerError('Unknown isolation level name: "%s"', level)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   495
        return level
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   496
24
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   497
    def _check_fork(self):
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   498
        '''Check if process was forked (PID has changed).
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   499
24
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   500
        If it was, clean parent's connections.
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   501
        New connections are created for children.
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   502
        Known connection credentials are inherited, but not shared.
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   503
24
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   504
        '''
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   505
        if self.pid == multiprocessing.current_process().pid:
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   506
            # PID has not changed
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   507
            return
43
a921669e913a PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   508
24
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   509
        # update saved PID
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   510
        self.pid = multiprocessing.current_process().pid
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   511
        # reinitialize lock
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   512
        self.lock = threading.Lock()
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   513
        # clean parent's connections
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   514
        for name in self.conn_pool:
5664afa530e5 PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents: 23
diff changeset
   515
            self.conn_pool[name] = []
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   516
20
73f0d53fef6b PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents: 19
diff changeset
   517
    @classmethod
73f0d53fef6b PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents: 19
diff changeset
   518
    def get_instance(cls):
73f0d53fef6b PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents: 19
diff changeset
   519
        if not hasattr(cls, '_instance'):
73f0d53fef6b PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents: 19
diff changeset
   520
            cls._instance = cls()
73f0d53fef6b PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents: 19
diff changeset
   521
        return cls._instance
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   522
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   523
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   524
def get_instance():
20
73f0d53fef6b PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents: 19
diff changeset
   525
    return PgManager.get_instance()
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   526