def test_register_globally(self): HstoreAdapter.get_oids(self.conn) register_hstore(self.conn, globally=True) conn2 = self.connect() try: cur2 = self.conn.cursor() cur2.execute("select 'a => b'::hstore") r = cur2.fetchone() self.assert_(isinstance(r[0], dict)) finally: conn2.close()
def skip_if_no_hstore_(self): from psycopg2.extras import HstoreAdapter oids = HstoreAdapter.get_oids(self.conn) if oids is None or not oids[0]: return self.skipTest("hstore not available in test database") return f(self)
def register_hstore_on_connection_creation(connection, sender, *args, **kwargs): dbname = connection.alias if dbname not in _oids: oids1, oids2 = HstoreAdapter.get_oids(connection.connection) if not oids1 and not oids2: raise DatabaseError("hstore isn't installed on this database") _oids[dbname] = (oids1[0], oids2[0]) oid, array_oid = _oids[dbname] register_hstore(connection.connection, globally=True, oid=oid, array_oid=array_oid)
def register_hstore(conn_or_curs, globally=False, _unicode=False, oid=None, array_oid=None): from psycopg2.extras import HstoreAdapter from psycopg2 import extensions as _ext import psycopg2 import sys import re as regex from .fields import HStoreDict def cast(s, cur, _bsdec=regex.compile(r"\\(.)")): if sys.version_info[0] < 3 and _unicode: result = HstoreAdapter.parse_unicode(s, cur) else: result = HstoreAdapter.parse(s, cur, _bsdec) return HStoreDict(result) 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]) HSTORE = _ext.new_type(oid, str("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, str("HSTOREARRAY"), HSTORE) _ext.register_type(HSTOREARRAY, not globally and conn_or_curs or None)
def register_hstore(conn_or_curs, globally=False, unicode=False, oid=None, array_oid=None): from psycopg2.extras import HstoreAdapter from psycopg2 import extensions as _ext import psycopg2 import sys import re as regex from .fields import HStoreDict def cast(s, cur, _bsdec=regex.compile(r"\\(.)")): if sys.version_info[0] < 3 and unicode: result = HstoreAdapter.parse_unicode(s, cur) else: result = HstoreAdapter.parse(s, cur, _bsdec) return HStoreDict(result) 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]) HSTORE = _ext.new_type(oid, str("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, str("HSTOREARRAY"), HSTORE) _ext.register_type(HSTOREARRAY, not globally and conn_or_curs or None)
def register_hstore_on_connection_creation(connection, sender, *args, **kwargs): oid = HstoreAdapter.get_oids(connection.connection) if oid is None or not oid[0]: if connection.connection.server_version < 90000: raise psycopg2.ProgrammingError("Database version not supported") elif connection.connection.server_version < 90100: pg_config = subprocess.Popen(["pg_config", "--sharedir"], stdout=subprocess.PIPE) share_dir = pg_config.communicate()[0].strip('\r\n ') hstore_sql = os.path.join(share_dir, 'contrib', 'hstore.sql') statements = re.compile(r";[ \t]*$", re.M) cursor = connection.cursor() with open(hstore_sql, 'U') as fp: for statement in statements.split(fp.read().decode(settings.FILE_CHARSET)): statement = re.sub(ur"--.*([\n\Z]|$)", "", statement).strip() if statement: cursor.execute(statement + u";") else: cursor = connection.cursor() cursor.execute("CREATE EXTENSION hstore;") register_hstore(connection.connection, globally=True)
def test_register_globally(self): from psycopg2.extras import register_hstore, HstoreAdapter oids = HstoreAdapter.get_oids(self.conn) try: register_hstore(self.conn, globally=True) conn2 = psycopg2.connect(dsn) try: cur2 = self.conn.cursor() cur2.execute("select 'a => b'::hstore") r = cur2.fetchone() self.assert_(isinstance(r[0], dict)) finally: conn2.close() finally: psycopg2.extensions.string_types.pop(oids[0][0]) # verify the caster is not around anymore cur = self.conn.cursor() cur.execute("select 'a => b'::hstore") r = cur.fetchone() self.assert_(isinstance(r[0], str))
def test_register_globally(self): from psycopg2.extras import register_hstore, HstoreAdapter oids = HstoreAdapter.get_oids(self.conn) try: register_hstore(self.conn, globally=True) conn2 = psycopg2.connect(dsn) try: cur2 = self.conn.cursor() cur2.execute("select 'a => b'::hstore") r = cur2.fetchone() self.assertTrue(isinstance(r[0], dict)) finally: conn2.close() finally: psycopg2.extensions.string_types.pop(oids[0][0]) # verify the caster is not around anymore cur = self.conn.cursor() cur.execute("select 'a => b'::hstore") r = cur.fetchone() self.assertTrue(isinstance(r[0], str))
def register_hstore_on_connection_creation(connection, sender, *args, **kwargs): oid = HstoreAdapter.get_oids(connection.connection) if oid is None or not oid[0]: if connection.connection.server_version < 90000: raise psycopg2.ProgrammingError("Database version not supported") elif connection.connection.server_version < 90100: pg_config = subprocess.Popen(["pg_config", "--sharedir"], stdout=subprocess.PIPE) share_dir = pg_config.communicate()[0].strip('\r\n ') hstore_sql = os.path.join(share_dir, 'contrib', 'hstore.sql') statements = re.compile(r";[ \t]*$", re.M) cursor = connection.cursor() with open(hstore_sql, 'U') as fp: for statement in statements.split(fp.read().decode( settings.FILE_CHARSET)): statement = re.sub(ur"--.*([\n\Z]|$)", "", statement).strip() if statement: cursor.execute(statement + u";") else: cursor = connection.cursor() cursor.execute("CREATE EXTENSION hstore;") register_hstore(connection.connection, globally=True)