How to use the psycopg2.extensions.register_type function in psycopg2

To help you get started, we’ve selected a few psycopg2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github oberstet / scratchbox / python / psycopg2 / test8.py View on Github external
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
github uyuni-project / uyuni / backend / server / rhnSQL / driver_postgresql.py View on Github external
dsndata['host'] = self.host
                dsndata['port'] = self.port
            if self.sslmode is not None and self.sslmode == 'verify-full' and self.sslrootcert is not None:
                dsndata['sslmode'] = self.sslmode
                dsndata['sslrootcert'] = self.sslrootcert
            elif self.sslmode is not None:
                raise AttributeError("Only sslmode=\"verify-full\" (or None) is supported.")
            if self.sslmode is not None and self.sslrootcert is None:
                raise AttributeError("Attribute sslrootcert needs to be set if sslmode is set.")

            self.dbh = psycopg2.connect(" ".join("%s=%s" % (k, re.escape(str(v))) for k, v in list(dsndata.items())))

            # convert all DECIMAL types to float (let Python to choose one)
            DEC2INTFLOAT = psycopg2.extensions.new_type(psycopg2._psycopg.DECIMAL.values,
                                                        'DEC2INTFLOAT', decimal2intfloat)
            psycopg2.extensions.register_type(DEC2INTFLOAT)
        except psycopg2.Error:
            e = sys.exc_info()[1]
            if reconnect > 0:
                # Try one more time:
                return self.connect(reconnect=reconnect - 1)

            # Failed reconnect, time to error out:
            raise_with_tb(sql_base.SQLConnectError(
                self.database, e.pgcode, e.pgerror,
                "All attempts to connect to the database failed"), sys.exc_info()[2])
github poderopedia / plug-and-play-1.0-RC / template_app / modules / web / db.py View on Github external
def __init__(self, **keywords):
        if 'pw' in keywords:
            keywords['password'] = keywords.pop('pw')
            
        db_module = import_driver(["psycopg2", "psycopg", "pgdb"], preferred=keywords.pop('driver', None))
        if db_module.__name__ == "psycopg2":
            import psycopg2.extensions
            psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)

        # if db is not provided postgres driver will take it from PGDATABASE environment variable
        if 'db' in keywords:
            keywords['database'] = keywords.pop('db')
        
        self.dbname = "postgres"
        self.paramstyle = db_module.paramstyle
        DB.__init__(self, db_module, keywords)
        self.supports_multiple_insert = True
        self._sequences = None
github mideind / Greynir / bindb.py View on Github external
# For CPython
    import psycopg2.extensions as psycopg2ext
    import psycopg2
except ImportError:
    # For PyPy
    import psycopg2cffi.extensions as psycopg2ext
    import psycopg2cffi as psycopg2

from settings import Settings, Abbreviations, AdjectiveTemplate, \
    Meanings, StaticPhrases, StemPreferences
from dawgdictionary import Wordbase
from bincompress import BIN_Compressed

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


# Size of LRU/LFU caches for word lookups
CACHE_SIZE = 512
CACHE_SIZE_MEANINGS = 2048 # Most common lookup function (meanings of a particular word form)
CACHE_SIZE_UNDECLINABLE = 2048

# Named tuple for word meanings fetched from the BÍN database (lexicon)
BIN_Meaning = namedtuple('BIN_Meaning', ['stofn', 'utg', 'ordfl', 'fl', 'ordmynd', 'beyging'])
# Compact string representation
BIN_Meaning.__str__ = BIN_Meaning.__repr__ = lambda self: "(stofn='{0}', {2}/{3}/{1}, ordmynd='{4}' {5})" \
    .format(self.stofn, self.utg, self.ordfl, self.fl, self.ordmynd, self.beyging)


class BIN_Db:
github evidence-surveillance / trial2rev / dblib.py View on Github external
#!/usr/bin/python
# -*- coding: utf-8 -*-
import config
import psycopg2
import psycopg2.extensions
import time

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

USER_DB = config.USER_DB
DB_NAME = config.DB_NAME
USER_DB_PASS = config.USER_DB_PASS
DB_HOST = config.DB_HOST
DB_PORT = config.DB_PORT


def create_con(VERBOSE):
    """
    connect to the database
    @param VERBOSE: True/False
    @raise: DatabaseError if connection fails 3 times
    @return: connection to db
    """
    retries = 3
github coleifer / peewee / peewee.py View on Github external
def _connect(self):
        conn = psycopg2.connect(database=self.database, **self.connect_params)
        if self._register_unicode:
            pg_extensions.register_type(pg_extensions.UNICODE, conn)
            pg_extensions.register_type(pg_extensions.UNICODEARRAY, conn)
        return conn
github LMFDB / lmfdb / lmfdb / backend / database.py View on Github external
def setup_connection(conn):
    # We want to use unicode everywhere
    register_type(UNICODE, conn)
    register_type(UNICODEARRAY, conn)
    conn.set_client_encoding("UTF8")
    cur = conn.cursor()
    cur.execute("SELECT NULL::numeric")
    oid = cur.description[0][1]
    NUMERIC = new_type((oid,), "NUMERIC", numeric_converter)
    cur.execute("SELECT NULL::numeric[]")
    oid = cur.description[0][1]
    NUMERICL = new_array_type((oid,), "NUMERIC[]", NUMERIC)
    register_type(NUMERIC, conn)
    register_type(NUMERICL, conn)
    register_adapter(Integer, AsIs)
    register_adapter(RealNumber, RealEncoder)
    register_adapter(dict, Json)
    register_json(conn, loads=Json.loads)
github dheerajchand / ubuntu-django-nginx-ansible / projectenv / lib / python2.7 / site-packages / psycopg2 / extras.py View on Github external
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 `
    :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
github anotherjesse / firenomics / service / vendor / web / db.py View on Github external
def get_db_module(self):
        try: 
            import psycopg2 as db
            import psycopg2.extensions
            psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
        except ImportError: 
            try: 
                import psycopg as db
            except ImportError: 
                import pgdb as db
        return db
github netguy204 / roku_media_server / MyMedia / pysrc / web / db.py View on Github external
def __init__(self, **keywords):
        if 'pw' in keywords:
            keywords['password'] = keywords.pop('pw')
            
        db_module = import_driver(["psycopg2", "psycopg", "pgdb"], preferred=keywords.pop('driver', None))
        if db_module.__name__ == "psycopg2":
            import psycopg2.extensions
            psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)

        # if db is not provided postgres driver will take it from PGDATABASE environment variable
        if 'db' in keywords:
            keywords['database'] = keywords.pop('db')
        
        self.dbname = "postgres"
        self.paramstyle = db_module.paramstyle
        DB.__init__(self, db_module, keywords)
        self.supports_multiple_insert = True
        self._sequences = None