pgtoolkit/pgmanager.py
author Radek Brich <radek.brich@devl.cz>
Sun, 27 Nov 2011 17:02:42 +0100
changeset 19 e526ca146fa9
parent 9 2fcc8ef0b97d
child 20 73f0d53fef6b
permissions -rw-r--r--
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
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
#
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
# Requires: Python 2.6, psycopg2
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
#
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
# Copyright (c) 2010, 2011  Radek Brich <radek.brich@devl.cz>
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
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
PgManager wraps psycopg2 connect function, adding following features:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
 * Manage database connection parameters - link connection parameters
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
   to an unique identifier, retrieve connection object by this identifier
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
 * Connection pooling - connections with same identifier are pooled and reused
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
 * Easy query using the with statement - retrieve cursor directly by connection
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
   identifier, don't worry about connections
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
 * Dict rows - cursor has additional methods like fetchall_dict(), which
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
   returns dict row instead of ordinary list-like row
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
Example:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
import pgmanager
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
pgm = pgmanager.get_instance()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
pgm.create_conn(hostaddr='127.0.0.1', dbname='postgres')
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
with pgm.cursor() as curs:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
    curs.execute('SELECT now() AS now')
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
    row = curs.fetchone_dict()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
    print row.now
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
First, we have obtained PgManager instance. This is like calling
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
PgManager(), although in our example the instance is global. That means
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
getting the instance in another module brings us all the defined connections
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
etc.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
9
2fcc8ef0b97d Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
    62
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
    63
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
    64
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
    65
the connection is returned to the pool or closed (depending on number of connections
2fcc8ef0b97d Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents: 8
diff changeset
    66
in pool and on setting of keep_open parameter).
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
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
    69
using item or attribute access, that is row['now'] or row.now.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
"""
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
from contextlib import contextmanager
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
import logging
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
import threading
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
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
class PgManagerError(Exception):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
    pass
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
class ConnectionInfo:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
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
    91
    def __init__(self, dsn, isolation_level=None, keep_alive=True,
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
    92
        init_statement=None, keep_open=1):
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
    93
        
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
        self.dsn = dsn
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
        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
    96
        self.keep_alive = keep_alive
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
        self.init_statement = init_statement
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
        self.keep_open = keep_open
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
class RowDict(dict):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
    def __getattr__(self, key):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
        return self[key]
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
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   107
class Cursor(psycopg2.extensions.cursor):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   109
    def execute(self, query, args=None):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   110
        try:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   111
            return super(Cursor, self).execute(query, args)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   112
        finally:
1
ddce8990b976 Fix pgmanager logging in Python3.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   113
            log.debug(self.query.decode('utf8'))
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   114
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   115
    def callproc(self, procname, args=None):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   116
        try:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   117
            return super(Cursor, self).callproc(procname, args)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   118
        finally:
1
ddce8990b976 Fix pgmanager logging in Python3.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   119
            log.debug(self.query.decode('utf8'))
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   120
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   121
    def row_dict(self, row, lstrip=None):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   122
        adjustname = lambda a: a
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   123
        if lstrip:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   124
            adjustname = lambda a: a.lstrip(lstrip)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   125
        return RowDict(zip([adjustname(desc[0]) for desc in self.description], row))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   126
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   127
    def fetchone_dict(self, lstrip=None):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   128
        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
   129
        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
   130
            return None
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   131
        return self.row_dict(row, lstrip)
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 fetchall_dict(self, lstrip=None):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   134
        rows = super(Cursor, self).fetchall()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   135
        return [self.row_dict(row, lstrip) for row in rows]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   136
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   137
    def fetchone_adapted(self):
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   138
        '''Like fetchone() but values are quoted for direct inclusion in SQL query.
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   139
        
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   140
        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
   141
        by the query. Use mogrify() for simple cases.
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   142
        
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   143
        '''
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   144
        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
   145
        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
   146
            return None
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   147
        return [self.mogrify('%s', [x]).decode('utf8') for x in row]
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   148
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   149
    def fetchall_adapted(self):
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   150
        '''Like fetchall() but values are quoted for direct inclusion in SQL query.'''
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   151
        rows = super(Cursor, self).fetchall()
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
diff changeset
   152
        return [[self.mogrify('%s', [x]).decode('utf8') for x in row] for row in rows]
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 4
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 Connection(psycopg2.extensions.connection):
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 cursor(self, name=None):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   158
        if name is None:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   159
            return super(Connection, self).cursor(cursor_factory=Cursor)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   160
        else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   161
            return super(Connection, self).cursor(name, cursor_factory=Cursor)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   162
8
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   163
    def keep_alive(self):
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   164
        '''Set socket to keepalive mode. Must be called before any query.'''
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   165
        sock = socket.fromfd(self.fileno(), socket.AF_INET, socket.SOCK_STREAM) 
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   166
        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
   167
        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
   168
            # 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
   169
            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
   170
            # 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
   171
            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
   172
            # 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
   173
            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
   174
        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
   175
            pass
8
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   176
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   177
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   178
class PgManager:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   179
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   180
    def __init__(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   181
        self.conn_known = {}  # available connections
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   182
        self.conn_pool = {}
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   183
        self.lock = threading.Lock()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   184
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   185
    def __del__(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   186
        for conn in tuple(self.conn_known.keys()):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   187
            self.destroy_conn(conn)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   188
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
   189
    def create_conn(self, name='default', isolation_level=None, keep_alive=True, dsn=None, **kw):
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
   190
        '''Create named connection.
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
   191
        
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
   192
        name -- name for connection (default is "default")
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
   193
        isolation_level -- "autocommit", "read_committed", "serializable" or None for driver default
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
   194
        keep_alive -- set socket to keepalive mode
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
   195
        dsn -- string with connection parameters (dsn means Data Source Name)
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
   196
        
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
   197
        Alternative for dsn is keyword args (same names as in dsn).
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
   198
        
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
   199
        '''
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   200
        if name in self.conn_known:
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   201
            raise PgManagerError('Connection name "%s" already registered.' % name)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   202
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   203
        if dsn is None:
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
   204
            dsn = ' '.join([x[0]+'='+str(x[1]) for x in kw.items() if x[1] is not None])
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   205
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   206
        isolation_level = self._normalize_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
   207
        ci = ConnectionInfo(dsn, isolation_level, keep_alive)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   208
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   209
        self.conn_known[name] = ci
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   210
        self.conn_pool[name] = []
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   211
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   212
    def close_conn(self, name='default'):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   213
        '''Close all connections of given name.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   214
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   215
        Connection credentials are still saved.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   216
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   217
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   218
        while len(self.conn_pool[name]):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   219
            conn = self.conn_pool[name].pop()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   220
            conn.close()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   221
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   222
    def destroy_conn(self, name='default'):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   223
        '''Destroy connection.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   224
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   225
        Counterpart of create_conn.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   226
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   227
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   228
        if not name in self.conn_known:
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   229
            raise PgManagerError('Connection name "%s" not registered.' % name)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   230
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   231
        self.close_conn(name)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   232
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   233
        del self.conn_known[name]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   234
        del self.conn_pool[name]
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 get_conn(self, name='default'):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   237
        '''Get connection of name 'name' from pool.'''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   238
        self.lock.acquire()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   239
        try:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   240
            if not name in self.conn_known:
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   241
                raise PgManagerError("Connection name '%s' not registered." % name)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   242
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   243
            conn = None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   244
            while len(self.conn_pool[name]) and conn is None:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   245
                conn = self.conn_pool[name].pop()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   246
                if conn.closed:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   247
                    conn = None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   248
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   249
            if conn is None:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   250
                ci = self.conn_known[name]
8
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   251
                conn = self._connect(ci)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   252
        finally:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   253
            self.lock.release()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   254
        return conn
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   255
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   256
    def put_conn(self, conn, name='default'):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   257
        '''Put connection back to pool.
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
        Name must be same as used for get_conn,
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   260
        otherwise things become broken.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   261
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   262
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   263
        self.lock.acquire()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   264
        try:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   265
            if not name in self.conn_known:
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   266
                raise PgManagerError("Connection name '%s' not registered." % name)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   267
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   268
            if len(self.conn_pool[name]) >= self.conn_known[name].keep_open:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   269
                conn.close()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   270
                return
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   271
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   272
            if conn.get_transaction_status() == psycopg2.extensions.TRANSACTION_STATUS_UNKNOWN:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   273
                conn.close()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   274
                return
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   275
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   276
            # connection returned to the pool must not be in transaction
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   277
            if conn.get_transaction_status() != psycopg2.extensions.TRANSACTION_STATUS_IDLE:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   278
                conn.rollback()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   279
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   280
            self.conn_pool[name].append(conn)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   281
        finally:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   282
            self.lock.release()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   283
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   284
    @contextmanager
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   285
    def cursor(self, name='default'):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   286
        '''Cursor context.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   287
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   288
        Uses any connection of name 'name' from pool
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   289
        and returns cursor for that connection.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   290
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   291
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   292
        conn = self.get_conn(name)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   293
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   294
        try:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   295
            curs = conn.cursor()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   296
            yield curs
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   297
        finally:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   298
            curs.close()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   299
            self.put_conn(conn, name)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   300
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   301
    def wait_for_notify(self, name='default', timeout=5):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   302
        '''Wait for asynchronous notifies, return the last one.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   303
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   304
        Returns None on timeout.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   305
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   306
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   307
        conn = self.get_conn(name)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   308
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   309
        try:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   310
            # any residual notify?
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   311
            # then return it, that should not break anything
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   312
            if conn.notifies:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   313
                return conn.notifies.pop()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   314
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   315
            if select.select([conn], [], [], timeout) == ([], [], []):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   316
                # timeout
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   317
                return None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   318
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   319
                conn.poll()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   320
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   321
                # return just the last notify (we do not care for older ones)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   322
                if conn.notifies:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   323
                    return conn.notifies.pop()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   324
                return None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   325
        finally:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   326
            # clean notifies
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   327
            while conn.notifies:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   328
                conn.notifies.pop()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   329
            self.put_conn(conn, name)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   330
8
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   331
    def _connect(self, ci):
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   332
        conn = psycopg2.connect(ci.dsn, connection_factory=Connection)
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
   333
        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
   334
            conn.keep_alive()
8
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   335
        if not ci.isolation_level is None:
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   336
            conn.set_isolation_level(ci.isolation_level)
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   337
        if ci.init_statement:
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   338
            curs = conn.cursor()
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   339
            curs.execute(ci.init_statement)
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   340
            curs.close()
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   341
        return conn
2911935c524d pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   342
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   343
    def _normalize_isolation_level(self, level):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   344
        if type(level) == str:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   345
            if level.lower() == 'autocommit':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   346
                return psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   347
            if level.lower() == 'read_committed':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   348
                return psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   349
            if level.lower() == 'serializable':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   350
                return psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   351
            raise PgManagerError('Unknown isolation level name: "%s"', level)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   352
        return level
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   353
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   354
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   355
try:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   356
    NullHandler = logging.NullHandler
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   357
except AttributeError:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   358
    class NullHandler(logging.Handler):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   359
        def emit(self, record):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   360
            pass
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   361
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   362
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   363
log = logging.getLogger("pgmanager")
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   364
log.addHandler(NullHandler())
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   365
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   366
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   367
instance = None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   368
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   369
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   370
def get_instance():
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   371
    global instance
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   372
    if instance is None:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   373
        instance = PgManager()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   374
    return instance
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   375
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   376