Example #1
0
 def getconn(self, create=True):
     conn = pool.getconn(self.dsn)
     conn.set_isolation_level(int(self.tilevel))
     conn.set_client_encoding(self.encoding)
     for tc in self.typecasts:
         register_type(tc, conn)
     return conn
Example #2
0
def register_ltree(conn_or_curs, globally=False, oid=None, array_oid=None):
    """Register the ltree adapter and typecaster on the connection or cursor.
    """
    register_adapter()

    conn, curs, conn_or_curs = _solve_conn_curs(conn_or_curs)

    if oid is None:
        oid = get_oids(conn_or_curs, 'ltree')
        if oid is None or not oid[0]:
            raise psycopg2.ProgrammingError(
                "ltree type not found in the database."
            )
        else:
            array_oid = oid[1]
            oid = oid[0]

    if isinstance(oid, int):
        oid = (oid,)

    if array_oid is not None:
        if isinstance(array_oid, int):
            array_oid = (array_oid,)
        else:
            array_oid = tuple([x for x in array_oid if x])

    # create and register the typecaster
    LTREE = ext.new_type(oid, "LTREE", cast_ltree)
    ext.register_type(LTREE, not globally and conn_or_curs or None)

    if array_oid:
        LTREEARRAY = ext.new_array_type(array_oid, "LTREEARRAY", LTREE)
        ext.register_type(LTREEARRAY, not globally and conn_or_curs or None)
Example #3
0
def register_cidr(oid=None, conn_or_curs=None):
    """Create the CIDR type and an Cidr adapter.

    :param oid: oid for the PostgreSQL :sql:`cidr` type, or 2-items sequence
        with oids of the type and the array. If not specified, use PostgreSQL
        standard oids.
    :param conn_or_curs: where to register the typecaster. If not specified,
        register it globally.
    """
    if not oid:
        oid1 = 650
        oid2 = 651
    elif isinstance(oid, (list, tuple)):
        oid1, oid2 = oid
    else:
        oid1 = oid
        oid2 = 651

    _ext.CIDR = _ext.new_type((oid1, ), "CIDR",
            lambda data, cursor: data and Cidr(data) or None)
    _ext.CIDRARRAY = _ext.new_array_type((oid2, ), "CIDRARRAY", _ext.CIDR)

    _ext.register_type(_ext.CIDR, conn_or_curs)
    _ext.register_type(_ext.CIDRARRAY, conn_or_curs)

    return _ext.CIDR
Example #4
0
def register_ltree(conn):
    oid = _get_ltree_oids(conn)
    if not oid[0]:
        return False
    else:
        array_oid = oid[1]
        oid = oid[0]

    if isinstance(oid, int):
        oid = (oid,)

    if array_oid is not None:
        if isinstance(array_oid, int):
            array_oid = (array_oid,)
        else:
            array_oid = tuple([x for x in array_oid if x])

    ltree = extensions.new_type(oid, "LTREE", _cast_fn)
    extensions.register_type(ltree, None)

    if array_oid:
        ltree_array = extensions.new_array_type(array_oid, "LTREEARRAY", ltree)
        extensions.register_type(ltree_array, None)

    return True
Example #5
0
def register_inet(oid=None, conn_or_curs=None):
    """Create the INET type and an Inet adapter."""
    if not oid:
        oid = 869
    _ext.INET = _ext.new_type((oid,), "INET", lambda data, cursor: data and Inet(data) or None)
    _ext.register_type(_ext.INET, conn_or_curs)
    return _ext.INET
Example #6
0
	def __init__(self,dbname,user,host,password,port=None):
		self.dbname = dbname
		self.user = user
		self.host = host
		self.password = password
		self.status=0
		if port is None:
			parameters = "dbname='"+dbname+"' user='******' host='"+host+"' password='******'"
		else:
			parameters = "dbname='"+dbname+"' user='******' host='"+host+"' password='******' port='"+str(port)+"'"
		self.debug=''
		try:    
			#for unicode
			pg_extensions.register_type(pg_extensions.UNICODE)
			#connecting to the database
			self.conn = pg_connect(parameters)
			#Useful for operation such as drop and insert
			self.conn.set_isolation_level(0)
			#enabling utf8 encode
			self.conn.set_client_encoding('UNICODE')
			self.cur = self.conn.cursor()
			self.debug='connected to db!\t'
		except: 
			self.debug='connection failed!\t'
			self.status=1
			self.conn = None
Example #7
0
    def register_uuid(oids=None, conn_or_curs=None):
        """Create the UUID type and an uuid.UUID adapter."""
        if not oids:
            oid1 = 2950
            oid2 = 2951
        elif type(oids) == list:
            oid1, oid2 = oids
        else:
            oid1 = oids
            oid2 = 2951

        def parseUUIDARRAY(data, cursor):
            if data is None:
                return None
            elif data == "{}":
                return []
            else:
                return [((len(x) > 0 and x != "NULL") and uuid.UUID(x) or None) for x in data[1:-1].split(",")]

        _ext.UUID = _ext.new_type((oid1,), "UUID", lambda data, cursor: data and uuid.UUID(data) or None)
        _ext.UUIDARRAY = _ext.new_type((oid2,), "UUID[]", parseUUIDARRAY)

        _ext.register_type(_ext.UUID, conn_or_curs)
        _ext.register_type(_ext.UUIDARRAY, conn_or_curs)
        _ext.register_adapter(uuid.UUID, UUID_adapter)

        return _ext.UUID
Example #8
0
def register_composite(name, conn_or_curs, globally=False, factory=None):
    """Register a typecaster to convert a composite type into a tuple.

    :param name: the name of a PostgreSQL composite type, e.g. created using
        the |CREATE TYPE|_ command
    :param conn_or_curs: a connection or cursor used to find the type oid and
        components; the typecaster is registered in a scope limited to this
        object, unless *globally* is set to `!True`
    :param globally: if `!False` (default) register the typecaster only on
        *conn_or_curs*, otherwise register it globally
    :param factory: if specified it should be a `CompositeCaster` subclass: use
        it to :ref:`customize how to cast composite types <custom-composite>`
    :return: the registered `CompositeCaster` or *factory* instance
        responsible for the conversion
    """
    if factory is None:
        factory = CompositeCaster

    caster = factory._from_db(name, conn_or_curs)
    _ext.register_type(caster.typecaster, not globally and conn_or_curs or None)

    if caster.array_typecaster is not None:
        _ext.register_type(
            caster.array_typecaster, not globally and conn_or_curs or None)

    return caster
def register_ipaddress(conn_or_curs=None):
    """
    Register conversion support between `ipaddress` objects and `network types`__.

    :param conn_or_curs: the scope where to register the type casters.
        If `!None` register them globally.

    After the function is called, PostgreSQL :sql:`inet` values will be
    converted into `~ipaddress.IPv4Interface` or `~ipaddress.IPv6Interface`
    objects, :sql:`cidr` values into into `~ipaddress.IPv4Network` or
    `~ipaddress.IPv6Network`.

    .. __: https://www.postgresql.org/docs/current/static/datatype-net-types.html
    """
    global ipaddress
    import ipaddress

    global _casters
    if _casters is None:
        _casters = _make_casters()

    for c in _casters:
        register_type(c, conn_or_curs)

    for t in [ipaddress.IPv4Interface, ipaddress.IPv6Interface,
              ipaddress.IPv4Network, ipaddress.IPv6Network]:
        register_adapter(t, adapt_ipaddress)
Example #10
0
def register_uuid(oids=None, conn_or_curs=None):
    """Create the UUID type and an uuid.UUID adapter.

    :param oids: oid for the PostgreSQL :sql:`uuid` type, or 2-items sequence
        with oids of the type and the array. If not specified, use PostgreSQL
        standard oids.
    :param conn_or_curs: where to register the typecaster. If not specified,
        register it globally.
    """

    import uuid

    if not oids:
        oid1 = 2950
        oid2 = 2951
    elif isinstance(oids, (list, tuple)):
        oid1, oid2 = oids
    else:
        oid1 = oids
        oid2 = 2951

    _ext.UUID = _ext.new_type((oid1, ), "UUID",
            lambda data, cursor: data and uuid.UUID(data) or None)
    _ext.UUIDARRAY = _ext.new_array_type((oid2,), "UUID[]", _ext.UUID)

    _ext.register_type(_ext.UUID, conn_or_curs)
    _ext.register_type(_ext.UUIDARRAY, conn_or_curs)
    _ext.register_adapter(uuid.UUID, UUID_adapter)

    return _ext.UUID
Example #11
0
def register_inet(oid=None, conn_or_curs=None):
    """Create the INET type and an Inet adapter.

    :param oid: oid for the PostgreSQL :sql:`inet` type, or 2-items sequence
        with oids of the type and the array. If not specified, use PostgreSQL
        standard oids.
    :param conn_or_curs: where to register the typecaster. If not specified,
        register it globally.
    """
    import warnings
    warnings.warn(
        "the inet adapter is deprecated, it's not very useful",
        DeprecationWarning)

    if not oid:
        oid1 = 869
        oid2 = 1041
    elif isinstance(oid, (list, tuple)):
        oid1, oid2 = oid
    else:
        oid1 = oid
        oid2 = 1041

    _ext.INET = _ext.new_type((oid1, ), "INET",
            lambda data, cursor: data and Inet(data) or None)
    _ext.INETARRAY = _ext.new_array_type((oid2, ), "INETARRAY", _ext.INET)

    _ext.register_type(_ext.INET, conn_or_curs)
    _ext.register_type(_ext.INETARRAY, conn_or_curs)

    return _ext.INET
Example #12
0
def register_tstz_w_secs(oids=None, conn_or_curs=None):
    """Register alternate type caster for TIMESTAMP WITH TIME ZONE.

    The Python datetime module cannot handle time zones with
    seconds in the UTC offset. There are, however, historical
    "time zones" which contain such offsets, eg. "Asia/Calcutta".
    In many cases those offsets represent true local time.

    If you encounter "unable to parse time" on a perfectly valid
    timestamp you likely want to try this type caster. It truncates
    the seconds from the time zone data and retries casting
    the timestamp. Note that this will generate timestamps
    which are INACCURATE by the number of seconds truncated
    (unless the seconds were 00).

    <oids>
            which OIDs to use this type caster for,
            defaults to TIMESTAMP WITH TIME ZONE
    <conn_or_curs>
            a cursor or connection if you want to attach
            this type caster to that only, defaults to
            None meaning all connections and cursors
    """
    if oids is None:
        oids = (1184,)  # hardcoded from PostgreSQL headers

    _ext.TSTZ_W_SECS = _ext.new_type(oids, "TSTZ_W_SECS", _convert_tstz_w_secs)
    _ext.register_type(TSTZ_W_SECS, conn_or_curs)

    return _ext.TSTZ_W_SECS
Example #13
0
    def test_unicode(self):
        cur = self.conn.cursor()
        ext.register_type(ext.UNICODE, cur)
        snowman = u"\u2603"

        # unicode in statement
        psycopg2.extras.execute_batch(cur,
            "insert into testfast (id, data) values (%%s, %%s) -- %s" % snowman,
            [(1, 'x')])
        cur.execute("select id, data from testfast where id = 1")
        self.assertEqual(cur.fetchone(), (1, 'x'))

        # unicode in data
        psycopg2.extras.execute_batch(cur,
            "insert into testfast (id, data) values (%s, %s)",
            [(2, snowman)])
        cur.execute("select id, data from testfast where id = 2")
        self.assertEqual(cur.fetchone(), (2, snowman))

        # unicode in both
        psycopg2.extras.execute_batch(cur,
            "insert into testfast (id, data) values (%%s, %%s) -- %s" % snowman,
            [(3, snowman)])
        cur.execute("select id, data from testfast where id = 3")
        self.assertEqual(cur.fetchone(), (3, snowman))
Example #14
0
    def __init__(self, parent, id):
        register_type(UNICODE)
        conn = psycopg2.connect(CONN_STR)
        cur = conn.cursor()
        cur.execute('select * from "typeOfCredit"')
        cols = cur.description
        row = cur.fetchall()
        c = len(row)+1

        wx.Frame.__init__(self, parent, id, 'Working with Types Of Credit', \
        size=(720, 400), style=wx.DEFAULT_FRAME_STYLE)
        self.window1 = wx.SplitterWindow(self, -1, style=wx.NO_BORDER)
        self.panel1 = wx.Panel(self.window1, -1)
        self.panel2 = wx.Panel(self.window1, -1)
        self.Bind(wx.EVT_CLOSE, lambda event: self.Destroy())
        self.download = wx.Button(self.panel2, -1, 'Download')
        self.new = wx.Button(self.panel2, -1, 'New')
        self.update = wx.Button(self.panel2, -1, 'Update')
        self.delete = wx.Button(self.panel2, -1, 'Delete')
        self.grid = wx.grid.Grid(self.panel1, -1, size=(1,1))
        self.grid.CreateGrid(c, 5)
        self.grid.SetRowLabelSize(40)
        self.grid.SetColLabelSize(40)
        self.grid.SetMinSize((500, 300))
        self.grid.SetColLabelValue(0, 'id')
        self.grid.SetColSize(0, 40)
        self.grid.SetColLabelValue(1, 'Name')
        self.grid.SetColSize(1, 150)
        self.grid.SetColLabelValue(2, 'Conditions_id')
        self.grid.SetColSize(2, 150)
        self.grid.SetColLabelValue(3, 'Rate')
        self.grid.SetColSize(3, 120)
        self.grid.SetColLabelValue(4, 'Period')
        self.grid.SetColSize(4,120)
        self.Bind(wx.EVT_BUTTON, self.on_download, self.download)
        self.Bind(wx.EVT_BUTTON, self.on_new, self.new)
        self.Bind(wx.EVT_BUTTON, self.on_update, self.update)
        self.Bind(wx.EVT_BUTTON, self.on_delete, self.delete)
        self.grid.Bind(wx.EVT_KEY_DOWN, self.keydown)
        self.panel1.SetMinSize((720, 370))
        self.panel2.SetMinSize((720, 30))
        self.window1.SetMinSize((720, 400))
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer1 = wx.BoxSizer(wx.VERTICAL)
        sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer1.Add(self.grid, -1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
        sizer2.AddMany([(self.download, -1, wx.EXPAND|wx.ADJUST_MINSIZE, 0),
        (self.new, -1, wx.EXPAND|wx.ADJUST_MINSIZE, 0),
        (self.update, -1, wx.EXPAND|wx.ADJUST_MINSIZE, 0),
        (self.delete, -1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)])
        self.panel1.SetSizer(sizer1)
        self.panel2.SetSizer(sizer2)
        self.window1.SplitHorizontally(self.panel1, self.panel2)
        sizer.Add(self.window1, 1, wx.ALL|wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(sizer)
        self.Layout()
        self.Centre()
        self.printTypes()
Example #15
0
def _register_inet(oid=None, conn_or_curs=None):
    """Create the INET type and an Inet adapter."""
    from psycopg2 import extensions as _ext
    if not oid: oid = 869
    _ext.INET = _ext.new_type((oid, ), "INET",
            lambda data, cursor: data and Inet(data) or None)
    _ext.register_type(_ext.INET, conn_or_curs)
    return _ext.INET
Example #16
0
    def _cursor(self):
        """
        Returns a psycopg2 DictCursor

        """
        cur = self.conn.cursor(cursor_factory=DictCursor)
        register_type(UNICODE, cur)
        return cur
Example #17
0
 def __init__(self):
     self.connection = self._get_connection_hadler()
     self.curs = None
     self.realcurs = None
     self.rowsaffected = None
     self.realcurs_arraysize = 2000
     dbextensionss.register_type(dbextensionss.UNICODE)
     dbextensionss.register_type(dbextensionss.UNICODEARRAY)
Example #18
0
    def connect(cls, connectString):
        args = connectString.asDict(exclude=('driver',))
        args['database'] = args.pop('dbname')

        conn = psycopg2.connect(**args)
        conn.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)
        extensions.register_type(extensions.UNICODE, conn)
        return cls(conn)
Example #19
0
 def register_uuid(oid=None, conn_or_curs=None):
     """Create the UUID type and an uuid.UUID adapter."""
     if not oid: oid = 2950
     _ext.UUID = _ext.new_type((oid, ), "UUID",
             lambda data, cursor: data and uuid.UUID(data) or None)
     _ext.register_type(_ext.UUID, conn_or_curs)
     _ext.register_adapter(uuid.UUID, UUID_adapter)
     return _ext.UUID
Example #20
0
    def register_cast(cls, connection):
        cast_function = CAST_MAPPER[cls.type_name()]
        cursor = connection.cursor()
        cursor.execute(cls.sql_for_oid())
        oid = cursor.description[0][1]
        cursor.close()

        PGTYPE = new_type((oid,), cls.type_name().upper(), cast_function)
        register_type(PGTYPE)
Example #21
0
    def __init__(self, parent, id):
        register_type(UNICODE)
        conn = psycopg2.connect(CONN_STR)
        cur = conn.cursor()
        cur.execute('select personal_id, credit_id from credit where date_end < current_date')
        cols = cur.description
        row = cur.fetchall()
        c = len(row)+1

        wx.Frame.__init__(self, parent, id, 'Working with Database', \
        size=(400, 300), style=wx.DEFAULT_FRAME_STYLE)
        self.window1 = wx.SplitterWindow(self, -1, style=wx.BORDER_DEFAULT)
        self.panel1 = wx.Panel(self.window1, -1)
        self.panel2 = wx.Panel(self.window1, -1)
        self.Bind(wx.EVT_CLOSE, lambda event: self.Destroy())
        self.personal = wx.Button(self.panel2, -1, 'Personal')
        self.types = wx.Button(self.panel2, -1, 'Types')
        self.conditions = wx.Button(self.panel2, -1, 'Conditions')
        self.credits = wx.Button(self.panel2, -1, 'Credits')

        self.grid = wx.grid.Grid(self.panel1, -1, size=(1,1))
        self.grid.CreateGrid(10, 2)
        self.grid.SetRowLabelSize(40)
        self.grid.SetColLabelSize(40)
        self.grid.SetMinSize((300, 400))
        self.grid.SetColLabelValue(0, 'personal_id')
        self.grid.SetColSize(0, 100)
        self.grid.SetColLabelValue(1, 'credit_id')
        self.grid.SetColSize(1, 100)

        self.Bind(wx.EVT_BUTTON, self.on_person, self.personal)
        self.Bind(wx.EVT_BUTTON, self.on_type, self.types)
        self.Bind(wx.EVT_BUTTON, self.on_conditions, self.conditions)
        self.Bind(wx.EVT_BUTTON, self.on_credit, self.credits)

        self.panel1.SetMinSize((3000, 300))
        self.panel2.SetMinSize((100, 300))
        self.window1.SetMinSize((520, 300))
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer1 = wx.BoxSizer(wx.HORIZONTAL)
        sizer2 = wx.BoxSizer(wx.VERTICAL)

        sizer1.Add(self.grid, -1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
        sizer2.AddMany([(self.personal, -1, wx.EXPAND|wx.ADJUST_MINSIZE, 0),
        (self.types, -1, wx.EXPAND|wx.ADJUST_MINSIZE, 0),
        (self.conditions, -1, wx.EXPAND|wx.ADJUST_MINSIZE, 0),
        (self.credits, -1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)])
        self.panel1.SetSizer(sizer1)
        self.panel2.SetSizer(sizer2)
        self.window1.SplitVertically(self.panel1, self.panel2)
        sizer.Add(self.window1, 1, wx.ALL|wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(sizer)
        self.Layout()
        self.Centre()
        self.printDebt()
Example #22
0
 def getconn(self, init=True):
     # if init is False we are trying to get hold on an already existing
     # connection, so we avoid to (re)initialize it risking errors.
     conn = pool.getconn(self.dsn)
     if init:
         conn.set_isolation_level(int(self.tilevel))
         conn.set_client_encoding(self.encoding)
         for tc in self.typecasts:
             register_type(tc, conn)
     return conn
Example #23
0
File: db.py Project: zagy/psycopg2
 def init_conn(self, conn):
     # use set_session where available as in these versions
     # set_isolation_level generates an extra query.
     if psycopg2.__version__ >= '2.4.2':
         conn.set_session(isolation_level=int(self.tilevel))
     else:
         conn.set_isolation_level(int(self.tilevel))
     conn.set_client_encoding(self.encoding)
     for tc in self.typecasts:
         register_type(tc, conn)
Example #24
0
 def __init__(self):
     self.pg_host = os.environ.get('PGHOST')
     self.pg_user = os.environ.get('PGUSER')
     self.pg_password = os.environ.get('PGPASSWORD')
     self.pg_port = os.environ.get('PGPORT')
     self.pg_database = os.environ.get('PGDATABASE')
     self.connection = None
     self.connection = self._get_connection_hadler()
     dbextensionss.register_type(dbextensionss.UNICODE)
     dbextensionss.register_type(dbextensionss.UNICODEARRAY)
Example #25
0
 def __init__(self, *args):
     self.pg_database = os.environ.get('PGDATABASE')
     self.trx_flg = 0
     i = 1
     for arg in args:
         if i == 1:
             self.trx_flg = arg
     self.connection = self._get_connection_hadler()
     dbextensionss.register_type(dbextensionss.UNICODE)
     dbextensionss.register_type(dbextensionss.UNICODEARRAY)
Example #26
0
def register_range_caster(pgrange, pyrange, oid, subtype_oid, array_oid, scope=None):
    # Create an adapter for this range type
    adapter = partial(cast_raw_range_string, pyrange, subtype_oid=subtype_oid)

    # Create types using adapter
    range_type = new_type((oid,), pgrange, adapter)
    range_array_type = new_array_type((array_oid,), pgrange, range_type)

    register_type(range_type, scope)
    register_type(range_array_type, scope)
Example #27
0
def register_citext_type(dbapi_con, connection_record):
    def cast_citext(in_str, cursor):
        if in_str == None:
            return None
        return unicode(in_str, cursor.connection.encoding)
    with closing(dbapi_con.cursor()) as c:
        c.execute(b"SELECT pg_type.oid FROM pg_type WHERE typname = 'citext'")
        citext_oid = c.fetchone()
        if citext_oid != None:
            citext_type = new_type(citext_oid, b"CITEXT", cast_citext)
            register_type(citext_type)
Example #28
0
 def __init__(self, dbname):
     if sys.version < '3':
         from psycopg2.extensions import register_type, UNICODE
         register_type(UNICODE)
     self.dbconn = DbConnection(dbname)
     self.film = FilmHandler(self.dbconn)
     self.url_map = Map([
             Rule('/', endpoint='index'),
             Rule('/films', endpoint='film'),
             Rule('/film/<path:parts>', endpoint='film')
             ])
    def __init__(self):
        """ Connect to the database. """

        try:
            self._connection = pg.connect(database=config.DATABASE, user=config.USER,
                                          password=config.PASSWORD, host=config.HOST,
                                          port=config.PORT)
            self._cursor = self._connection.cursor(cursor_factory=extras.RealDictCursor)  # DictCursor does not work
            # retrieve all data as unicode
            extensions.register_type(extensions.UNICODE, self._cursor)
        except:
            raise pg.DatabaseError('Unable to connect to the database. Please check your configuration.')
Example #30
0
def register_macaddr_type():
    from psycopg2.extensions import register_adapter, new_type, register_type, new_array_type
    import psycopg2

    oid = get_type_oid("NULL::macaddr")
    PGTYPE = new_type((oid,), "macaddr", cast_macaddr)
    register_type(PGTYPE)
    register_adapter(MacAddr, adapt_macaddr)

    mac_array_oid = get_type_oid("'{}'::macaddr[]")
    array_of_mac = new_array_type((mac_array_oid, ), 'macaddr', psycopg2.STRING)
    psycopg2.extensions.register_type(array_of_mac)
Example #31
0
                'SELECT CASE WHEN NOT is_called THEN last_value '
                'ELSE last_value + %s '
                'END '
                'FROM "%s"' % (self.flavor.param, name), (increment, ))
        else:
            cursor.execute('SELECT CASE WHEN NOT is_called THEN last_value '
                           'ELSE last_value + increment_by '
                           'END '
                           'FROM "%s"' % name)
        return cursor.fetchone()[0]

    def has_channel(self):
        return True


register_type(UNICODE)
if PYDATE:
    register_type(PYDATE)
if PYDATETIME:
    register_type(PYDATETIME)
if PYTIME:
    register_type(PYTIME)
if PYINTERVAL:
    register_type(PYINTERVAL)
register_adapter(float, lambda value: AsIs(repr(value)))
register_adapter(Decimal, lambda value: AsIs(str(value)))
register_adapter(dict, Json)


def convert_json(value):
    return json.loads(value, object_hook=JSONDecoder())
Example #32
0
def register_cast(oid, typename, method):
    new_t = new_type((oid, ), typename, method)
    register_type(new_t)
Example #33
0
 def setup_unicode_extension(conn):
     extensions.register_type(extensions.UNICODE, conn)
Example #34
0
def database(env, tell_sentry):
    dburl = env.database_url
    maxconn = env.database_maxconn
    try:
        db = DB(dburl, maxconn=maxconn)
    except psycopg2.OperationalError as e:
        tell_sentry(e, {})
        pg_dir = os.environ.get('OPENSHIFT_PG_DATA_DIR')
        if pg_dir:
            # We know where the postgres data is, try to start the server ourselves
            r = call(['pg_ctl', '-D', pg_dir, 'start', '-w', '-t', '15'])
            if r == 0:
                return database(env, tell_sentry)
        db = NoDB()

    models = (
        _AccountElsewhere,
        AccountElsewhere,
        _Community,
        Community,
        ExchangeRoute,
        Participant,
        Repository,
    )
    for model in models:
        db.register_model(model)
    liberapay.billing.payday.Payday.db = db

    def adapt_money(m):
        return AsIs('(%s,%s)::currency_amount' %
                    (adapt(m.amount), adapt(m.currency)))

    register_adapter(Money, adapt_money)

    def cast_currency_amount(v, cursor):
        return None if v in (None, '(,)') else Money(*v[1:-1].split(','))

    try:
        oid = db.one("SELECT 'currency_amount'::regtype::oid")
        register_type(
            new_type((oid, ), _str('currency_amount'), cast_currency_amount))
    except psycopg2.ProgrammingError:
        pass

    def adapt_money_basket(b):
        return AsIs('(%s,%s)::currency_basket' %
                    (b.amounts['EUR'], b.amounts['USD']))

    register_adapter(MoneyBasket, adapt_money_basket)

    def cast_currency_basket(v, cursor):
        if v is None:
            return None
        eur, usd = v[1:-1].split(',')
        return MoneyBasket(EUR=Decimal(eur), USD=Decimal(usd))

    try:
        oid = db.one("SELECT 'currency_basket'::regtype::oid")
        register_type(
            new_type((oid, ), _str('currency_basket'), cast_currency_basket))
    except psycopg2.ProgrammingError:
        pass

    use_qc = not env.override_query_cache
    qc1 = QueryCache(db, threshold=(1 if use_qc else 0))
    qc5 = QueryCache(db, threshold=(5 if use_qc else 0))

    return {'db': db, 'db_qc1': qc1, 'db_qc5': qc5}
Example #35
0
from flask import Flask, redirect, url_for, render_template, request, flash, session
import os
from db_init import initialize
from psycopg2 import extensions
from forms import RegistrationForm, LoginForm

import queries

adds = []

extensions.register_type(extensions.UNICODE)
extensions.register_type(extensions.UNICODEARRAY)

app = Flask(__name__)
app.config['SECRET_KEY'] = '5791628bb0b13ce0c676dfde280ba245'

HEROKU = True

if (not HEROKU):
    os.environ[
        'DATABASE_URL'] = "dbname='postgres' user='******' host='localhost' password='******'"
    initialize(os.environ.get("DATABASE_URL"))


@app.route("/appointment/<id>", methods=["GET", "POST"])
def appointment_page(id=0, type=0):
    active = queries.run("""SELECT current_user""")
    if active[0][0] == None:
        active.pop(0)
    active_num = (len(active))
    psychologistt = queries.select("name", "psychologist", asDict=True)
Example #36
0
def database(env, tell_sentry):
    dburl = env.database_url
    maxconn = env.database_maxconn
    try:
        db = DB(dburl, maxconn=maxconn, cursor_factory=SimpleRowCursor)
    except psycopg2.OperationalError as e:
        tell_sentry(e, {}, allow_reraise=False)
        db = NoDB()

    itemgetter0 = itemgetter(0)

    def back_as_Object(cols, vals):
        return Object(zip(map(itemgetter0, cols), vals))

    db.back_as_registry[Object] = db.back_as_registry[
        'Object'] = back_as_Object

    models = (
        _AccountElsewhere,
        AccountElsewhere,
        _Community,
        Community,
        Encrypted,
        ExchangeRoute,
        Participant,
        Payin,
        Repository,
    )
    for model in models:
        db.register_model(model)
        setattr(db, model.__name__, model)
    liberapay.billing.payday.Payday.db = db

    def adapt_set(s):
        return adapt(tuple(s))

    register_adapter(set, adapt_set)

    def adapt_money(m):
        return AsIs('(%s,%s)::currency_amount' %
                    (adapt(m.amount), adapt(m.currency)))

    register_adapter(Money, adapt_money)

    def cast_currency_amount(v, cursor):
        return None if v in (None, '(,)') else Money(*v[1:-1].split(','))

    try:
        oid = db.one("SELECT 'currency_amount'::regtype::oid")
        register_type(
            new_type((oid, ), 'currency_amount', cast_currency_amount))
    except (psycopg2.ProgrammingError, NeedDatabase):
        pass

    def adapt_money_basket(b):
        return AsIs("_wrap_amounts('%s'::jsonb)" % json.dumps(
            {k: str(v)
             for k, v in b.amounts.items() if v}).replace("'", "''"))

    register_adapter(MoneyBasket, adapt_money_basket)

    def cast_currency_basket(v, cursor):
        if v is None:
            return None
        parts = v[1:-1].split(',', 2)
        if len(parts) == 2:
            eur, usd = parts
            obj = None
        else:
            eur, usd, obj = parts
        if obj:
            amounts = json.loads(obj[1:-1].replace('""', '"') if obj[0] ==
                                 '"' else obj)
            amounts = {k: Decimal(str(v)) for k, v in amounts.items()}
        else:
            amounts = {}
            if eur:
                amounts['EUR'] = Decimal(eur)
            if usd:
                amounts['USD'] = Decimal(usd)
        return MoneyBasket(**amounts)

    try:
        oid = db.one("SELECT 'currency_basket'::regtype::oid")
        register_type(
            new_type((oid, ), 'currency_basket', cast_currency_basket))
    except (psycopg2.ProgrammingError, NeedDatabase):
        pass

    use_qc = not env.override_query_cache
    qc1 = QueryCache(db, threshold=(1 if use_qc else 0))
    qc5 = QueryCache(db, threshold=(5 if use_qc else 0))

    return {'db': db, 'db_qc1': qc1, 'db_qc5': qc5}
Example #37
0
from trac.util.translation import _

try:
    import psycopg2 as psycopg
    import psycopg2.extensions
    from psycopg2 import DataError, ProgrammingError
    from psycopg2.extensions import register_type, UNICODE, \
                                    register_adapter, AsIs, QuotedString
except ImportError:
    has_psycopg = False
    psycopg = None
    psycopg2_version = None
    _libpq_pathname = None
else:
    has_psycopg = True
    register_type(UNICODE)
    register_adapter(Markup, lambda markup: QuotedString(unicode(markup)))
    register_adapter(type(empty), lambda empty: AsIs("''"))
    psycopg2_version = get_pkginfo(psycopg).get('version', psycopg.__version__)
    _libpq_pathname = None
    if not hasattr(psycopg, 'libpq_version'):
        # search path of libpq only if it is dynamically linked
        _f = _match = None
        try:
            with open(psycopg._psycopg.__file__, 'rb') as _f:
                if os.name != 'nt':
                    _match = re.search(
                        r'''
                            \0(
                            (?:/[^/\0]+)*/?
                            libpq\.(?:so\.[0-9]+|[0-9]+\.dylib)
Example #38
0
def register_hstore(conn_or_curs,
                    globally=False,
                    unicode=False,
                    oid=None,
                    array_oid=None):
    r"""Register adapter and typecaster for `!dict`\-\ |hstore| conversions.

    :param conn_or_curs: a connection or cursor: the typecaster will be
        registered only on this object unless *globally* is set to `!True`
    :param globally: register the adapter globally, not only on *conn_or_curs*
    :param unicode: if `!True`, keys and values returned from the database
        will be `!unicode` instead of `!str`. The option is not available on
        Python 3
    :param oid: the OID of the |hstore| type if known. If not, it will be
        queried on *conn_or_curs*.
    :param array_oid: the OID of the |hstore| array type if known. If not, it
        will be queried on *conn_or_curs*.

    The connection or cursor passed to the function will be used to query the
    database and look for the OID of the |hstore| type (which may be different
    across databases). If querying is not desirable (e.g. with
    :ref:`asynchronous connections <async-support>`) you may specify it in the
    *oid* parameter, which can be found using a query such as :sql:`SELECT
    'hstore'::regtype::oid`. Analogously you can obtain a value for *array_oid*
    using a query such as :sql:`SELECT 'hstore[]'::regtype::oid`.

    Note that, when passing a dictionary from Python to the database, both
    strings and unicode keys and values are supported. Dictionaries returned
    from the database have keys/values according to the *unicode* parameter.

    The |hstore| contrib module must be already installed in the database
    (executing the ``hstore.sql`` script in your ``contrib`` directory).
    Raise `~psycopg2.ProgrammingError` if the type is not found.
    """
    if oid is None:
        oid = HstoreAdapter.get_oids(conn_or_curs)
        if oid is None or not oid[0]:
            raise psycopg2.ProgrammingError(
                "hstore type not found in the database. "
                "please install it from your 'contrib/hstore.sql' file")
        else:
            array_oid = oid[1]
            oid = oid[0]

    if isinstance(oid, int):
        oid = (oid, )

    if array_oid is not None:
        if isinstance(array_oid, int):
            array_oid = (array_oid, )
        else:
            array_oid = tuple([x for x in array_oid if x])

    # create and register the typecaster
    if _sys.version_info[0] < 3 and unicode:
        cast = HstoreAdapter.parse_unicode
    else:
        cast = HstoreAdapter.parse

    HSTORE = _ext.new_type(oid, "HSTORE", cast)
    _ext.register_type(HSTORE, not globally and conn_or_curs or None)
    _ext.register_adapter(dict, HstoreAdapter)

    if array_oid:
        HSTOREARRAY = _ext.new_array_type(array_oid, "HSTOREARRAY", HSTORE)
        _ext.register_type(HSTOREARRAY, not globally and conn_or_curs or None)
Example #39
0
import psycopg2
import psycopg2.extras
import psycopg2.errorcodes
import psycopg2.extensions as ext
import sqlparse
import pgspecial as special
import select
from psycopg2.extensions import POLL_OK, POLL_READ, POLL_WRITE
from .packages.parseutils.meta import FunctionMetadata, ForeignKey
from .encodingutils import unicode2utf8, PY2, utf8tounicode

_logger = logging.getLogger(__name__)

# Cast all database input to unicode automatically.
# See http://initd.org/psycopg/docs/usage.html#unicode-handling for more info.
ext.register_type(ext.UNICODE)
ext.register_type(ext.UNICODEARRAY)
ext.register_type(ext.new_type((705, ), "UNKNOWN", ext.UNICODE))
# See https://github.com/dbcli/pgcli/issues/426 for more details.
# This registers a unicode type caster for datatype 'RECORD'.
ext.register_type(ext.new_type((2249, ), "RECORD", ext.UNICODE))

# Cast bytea fields to text. By default, this will render as hex strings with
# Postgres 9+ and as escaped binary in earlier versions.
ext.register_type(ext.new_type((17, ), 'BYTEA_TEXT', psycopg2.STRING))

# TODO: Get default timeout from pgclirc?
_WAIT_SELECT_TIMEOUT = 1


def _wait_select(conn):
Example #40
0
    def _register(self, scope=None):
        register_type(self.typecaster, scope)
        if self.array_typecaster is not None:
            register_type(self.array_typecaster, scope)

        register_adapter(self.range, self.adapter)
Example #41
0
Needed for decent postgresql access from twisted
"""
import psycopg2
from psycopg2 import *
from psycopg2.extensions import connection, register_type
from psycopg2.extras import DictCursor

"""
del connect
def connect(*args, **kwargs):
	kwargs['connection_factory'] = connection
	return _2psycopg.connect(*args, **kwargs)

class connection(_2connection):
	def cursor(self):
		return _2connection.cursor(self, cursor_factory=DictCursor)
"""
del connect
def connect(*args, **kwargs):
	kwargs['connection_factory'] = zoto_connection
	return psycopg2.connect(*args, **kwargs)

class zoto_connection(connection):
	def cursor(self):
		return connection.cursor(self, cursor_factory=DictCursor)
##
## This is required.  Without this, psycopg returns strings as latin-1 encoded strings,
## not unicode.
register_type(psycopg2.extensions.UNICODE)
Example #42
0
def register(connection):
    if isinstance(connection, extensions.cursor):
        # Retrocompat.
        cursor = connection
    else:
        cursor = connection.cursor()

    # Add MobilityDB types to PostgreSQL adapter and specify the reader function for each type.
    cursor.execute("SELECT NULL::TimestampSet")
    oid = cursor.description[0][1]
    extensions.register_type(
        extensions.new_type((oid, ), "TimestampSet",
                            TimestampSet.read_from_cursor))

    cursor.execute("SELECT NULL::Period")
    oid = cursor.description[0][1]
    extensions.register_type(
        extensions.new_type((oid, ), "Period", Period.read_from_cursor))

    cursor.execute("SELECT NULL::PeriodSet")
    oid = cursor.description[0][1]
    extensions.register_type(
        extensions.new_type((oid, ), "PeriodSet", PeriodSet.read_from_cursor))

    cursor.execute("SELECT NULL::TBOX")
    oid = cursor.description[0][1]
    extensions.register_type(
        extensions.new_type((oid, ), "TBOX", TBox.read_from_cursor))

    cursor.execute("SELECT NULL::TBool")
    oid = cursor.description[0][1]
    extensions.register_type(
        extensions.new_type((oid, ), "TBool", TBool.read_from_cursor))

    cursor.execute("SELECT NULL::TInt")
    oid = cursor.description[0][1]
    extensions.register_type(
        extensions.new_type((oid, ), "TInt", TInt.read_from_cursor))

    cursor.execute("SELECT NULL::TFloat")
    oid = cursor.description[0][1]
    extensions.register_type(
        extensions.new_type((oid, ), "TFloat", TFloat.read_from_cursor))

    cursor.execute("SELECT NULL::TText")
    oid = cursor.description[0][1]
    extensions.register_type(
        extensions.new_type((oid, ), "TText", TText.read_from_cursor))

    cursor.execute("SELECT NULL::STBOX")
    oid = cursor.description[0][1]
    extensions.register_type(
        extensions.new_type((oid, ), "STBOX", STBox.read_from_cursor))

    cursor.execute("SELECT NULL::TGeomPoint")
    oid = cursor.description[0][1]
    extensions.register_type(
        extensions.new_type((oid, ), "TGeomPoint",
                            TGeomPoint.read_from_cursor))

    cursor.execute("SELECT NULL::TGeogPoint")
    oid = cursor.description[0][1]
    extensions.register_type(
        extensions.new_type((oid, ), "TGeogPoint",
                            TGeogPoint.read_from_cursor))
Example #43
0
# http://initd.org/psycopg/docs/advanced.html#adapting-new-python-types-to-sql-syntax
# and
# http://pyopengl.sourceforge.net/pydoc/numpy.core.numerictypes.html
#
# http://numpy.sourceforge.net/numdoc/HTML/numdoc.htm
''' numpy data types:
int8 int16 int32 int64 int128
uint8 uint16 uint32 uint64 uint128
float16 float32 float64 float96 float128 float256
complex32 complex64 complex128 complex192 complex256 complex512
'''

DEC2FLOAT = new_type(
    DECIMAL.values, 'DEC2FLOAT', lambda value, curs: float(value)
    if value is not None else None)
register_type(DEC2FLOAT)


def adapt_decimal(Decimal):
    return AsIs(float)


register_adapter(Decimal, adapt_decimal)


def adapt_numpy_int8(numpy_int8):
    return AsIs(numpy_int8)


register_adapter(numpy.int8, adapt_numpy_int8)
Example #44
0
def register_flexmap(conn, types):
    """
   Create flexmap type from
   
    1) 'schema.typename' str or
    2) (typename, oid, attrs, array_oid, schema) tuple where attrs = [(attname, atttypid)]
   
   For 1), type info will be looked up in database catalog. For 2), type info is
   taken as specified.
   """

    register_hstore(conn)
    register_json(conn)

    casters = {}

    class DictComposite(CompositeCaster):
        """
      A type caster returning composite types as Python dicts
      enriched with a type field containing 'schema.typename'.
      """
        def make(self, attrs):
            o = {}
            for i in xrange(len(self.attnames)):
                if attrs[i] is not None:
                    o[self.attnames[i]] = attrs[i]
            o['type'] = self.schema + '.' + self.name
            return o

    ## create type casters for whole list
    ##
    for t in types:
        if type(t) == str:
            caster = DictComposite._from_db(t, conn)
        elif type(t) in [tuple, list]:
            caster = CompositeCaster(*t)
        else:
            raise Exception("invalid type %s in flexmap type list" % type(t))

        ## register item and array casters
        ##
        register_type(caster.typecaster, conn)
        if caster.array_typecaster is not None:
            register_type(caster.array_typecaster, conn)

        ## remember caster under 'schema.typename'
        ##
        casters['%s.%s' % (caster.schema, caster.name)] = caster

    class DictAdapter(object):
        """
      A dictionary adapter converting Python dicts to PostgreSQL
      JSON, Hstore or Composite Types depending on the dict field 'type'.      
      """
        def __init__(self, adapted):
            ## remember value to be adaptated - a Python dict
            self.adapted = adapted

            ## remember type field of dict-value to be adapted
            if adapted.has_key('type'):
                self._type = adapted['type']
                #del(adapted['type'])
            else:
                self._type = None

            ## create adapter to hstore if requested
            if self._type == 'hstore':
                self._hstoreAdapter = HstoreAdapter(adapted)

        def prepare(self, conn):
            self._conn = conn
            if self._type == 'hstore':
                self._hstoreAdapter.prepare(conn)

        def getquoted(self):
            if self._type is not None:
                if self._type == 'json':
                    return adapt(Json(self.adapted)).getquoted() + '::json'
                elif self._type == 'hstore':
                    return self._hstoreAdapter.getquoted()
                elif casters.has_key(self._type):
                    c = casters[self._type]
                    v = []
                    for n in c.attnames:
                        v.append(self.adapted.get(n, None))
                    a = adapt(tuple(v))
                    a.prepare(self._conn)
                    return a.getquoted() + '::' + self._type
                else:
                    raise psycopg2.ProgrammingError(
                        "unknown type %s in dictionary type hint" % self._type)
            else:
                raise psycopg2.ProgrammingError(
                    "dictionary is missing type hint")

    register_adapter(dict, DictAdapter)
Example #45
0
import sys
import re
from collections import defaultdict, namedtuple

# Import the Psycopg2 connector for PostgreSQL
try:
    # For CPython
    import psycopg2.extensions as psycopg2ext
    import psycopg2
except ImportError:
    # For PyPy
    import psycopg2cffi.extensions as psycopg2ext
    import psycopg2cffi as psycopg2

# Make Psycopg2 and PostgreSQL happy with UTF-8
psycopg2ext.register_type(psycopg2ext.UNICODE)
psycopg2ext.register_type(psycopg2ext.UNICODEARRAY)

# Hack to make this Python program executable from the utils subdirectory
basepath, _ = os.path.split(os.path.realpath(__file__))
_UTILS = os.sep + "utils"
if basepath.endswith(_UTILS):
    basepath = basepath[0:-len(_UTILS)]
    sys.path.append(basepath)

# Note: We can't use settings from ReynirPackage because it
# reads package resource streams, not plain text files
from settings import Settings, LineReader, ConfigError

BIN_Meaning = namedtuple("BIN_Meaning",
                         ["stofn", "utg", "ordfl", "fl", "ordmynd", "beyging"])
from pyres.horde import Khan
from requests import request
from simplejson import dumps
from tqdm import tqdm

from settings import PROXIES, PYRES
from utilities import (
    get_connection,
    get_details,
    get_sentry,
    get_total,
)

basicConfig(level=WARN)

register_type(UNICODE)
register_type(UNICODEARRAY)


class Record():

    queue = 'records'

    @staticmethod
    def perform(id):
        try:
            with closing(get_connection()) as connection:
                with closing(connection.cursor()) as cursor:
                    query = 'SELECT * FROM records WHERE id = %(id)s'
                    cursor.execute(
                        query,
 def on_connect(conn):
     extensions.register_type(extensions.UNICODE, conn)
     extensions.register_type(extensions.UNICODEARRAY, conn)