1import datetime 2from time import localtime 3 4from psycopg2cffi import extensions 5from psycopg2cffi import tz 6from psycopg2cffi._impl.adapters import Binary, Date, Time, Timestamp 7from psycopg2cffi._impl.adapters import DateFromTicks, TimeFromTicks 8from psycopg2cffi._impl.adapters import TimestampFromTicks 9from psycopg2cffi._impl.connection import _connect 10from psycopg2cffi._impl.exceptions import * 11from psycopg2cffi._impl.typecasts import BINARY, DATETIME, NUMBER, ROWID, STRING 12 13__version__ = '2.9.0' 14apilevel = '2.0' 15paramstyle = 'pyformat' 16threadsafety = 2 17 18import psycopg2cffi.extensions as _ext 19_ext.register_adapter(tuple, _ext.SQL_IN) 20_ext.register_adapter(type(None), _ext.NoneAdapter) 21 22 23import re 24 25def _param_escape(s, 26 re_escape=re.compile(r"([\\'])"), 27 re_space=re.compile(r'\s')): 28 """ 29 Apply the escaping rule required by PQconnectdb 30 """ 31 if not s: return "''" 32 33 s = re_escape.sub(r'\\\1', s) 34 if re_space.search(s): 35 s = "'" + s + "'" 36 37 return s 38 39del re 40 41 42def connect(dsn=None, 43 database=None, user=None, password=None, host=None, port=None, 44 connection_factory=None, cursor_factory=None, **kwargs): 45 """ 46 Create a new database connection. 47 48 The connection parameters can be specified either as a string: 49 50 conn = psycopg2.connect("dbname=test user=postgres password=secret") 51 52 or using a set of keyword arguments: 53 54 conn = psycopg2.connect(database="test", user="postgres", password="secret") 55 56 The basic connection parameters are: 57 58 - *dbname*: the database name (only in dsn string) 59 - *database*: the database name (only as keyword argument) 60 - *user*: user name used to authenticate 61 - *password*: password used to authenticate 62 - *host*: database host address (defaults to UNIX socket if not provided) 63 - *port*: connection port number (defaults to 5432 if not provided) 64 65 Using the *connection_factory* parameter a different class or connections 66 factory can be specified. It should be a callable object taking a dsn 67 argument. 68 69 Using the *cursor_factory* parameter, a new default cursor factory will be 70 used by cursor(). 71 72 Using *async*=True an asynchronous connection will be created. *async_* is 73 a valid alias (for Python versions where *async* is a keyword). 74 75 Any other keyword parameter will be passed to the underlying client 76 library: the list of supported parameters depends on the library version. 77 78 """ 79 items = [] 80 if database is not None: 81 items.append(('dbname', database)) 82 if user is not None: 83 items.append(('user', user)) 84 if password is not None: 85 items.append(('password', password)) 86 if host is not None: 87 items.append(('host', host)) 88 if port is not None: 89 items.append(('port', port)) 90 91 async_ = False 92 if 'async' in kwargs: 93 async_ = kwargs.pop('async') 94 if 'async_' in kwargs: 95 async_ = kwargs.pop('async_') 96 97 items.extend([(k, v) for (k, v) in kwargs.items() if v is not None]) 98 99 if dsn is not None and items: 100 raise TypeError( 101 "'%s' is an invalid keyword argument when the dsn is specified" 102 % items[0][0]) 103 104 if dsn is None: 105 if not items: 106 raise TypeError('missing dsn and no parameters') 107 else: 108 dsn = " ".join(["%s=%s" % (k, _param_escape(str(v))) 109 for (k, v) in items]) 110 111 conn = _connect(dsn, connection_factory=connection_factory, async_=async_) 112 if cursor_factory is not None: 113 conn.cursor_factory = cursor_factory 114 115 return conn 116