def test_adapt_9(self):
        if self.conn.server_version < 90000:
            return self.skipTest("skipping dict adaptation with PG 9 syntax")

        from psycopg2.extras import HstoreAdapter

        o = {"a": "1", "b": "'", "c": None}
        if self.conn.encoding == "UTF8":
            o["d"] = u"\xe0"

        a = HstoreAdapter(o)
        a.prepare(self.conn)
        q = a.getquoted()

        m = re.match(b(r"hstore\(ARRAY\[([^\]]+)\], ARRAY\[([^\]]+)\]\)"), q)
        self.assert_(m, repr(q))

        kk = m.group(1).split(b(", "))
        vv = m.group(2).split(b(", "))
        ii = zip(kk, vv)
        ii.sort()

        def f(*args):
            return tuple([filter_scs(self.conn, s) for s in args])

        self.assertEqual(len(ii), len(o))
        self.assertEqual(ii[0], f(b("E'a'"), b("E'1'")))
        self.assertEqual(ii[1], f(b("E'b'"), b("E''''")))
        self.assertEqual(ii[2], f(b("E'c'"), b("NULL")))
        if "d" in o:
            encc = u"\xe0".encode(psycopg2.extensions.encodings[self.conn.encoding])
            self.assertEqual(ii[3], f(b("E'd'"), b("E'") + encc + b("'")))
示例#2
0
    def test_adapt_9(self):
        if self.conn.server_version < 90000:
            return self.skipTest("skipping dict adaptation with PG 9 syntax")

        from psycopg2.extras import HstoreAdapter

        o = {'a': '1', 'b': "'", 'c': None}
        if self.conn.encoding == 'UTF8':
            o['d'] = '\xe0'

        a = HstoreAdapter(o)
        a.prepare(self.conn)
        q = a.getquoted()

        m = re.match(br'hstore\(ARRAY\[([^\]]+)\], ARRAY\[([^\]]+)\]\)', q)
        self.assertTrue(m, repr(q))

        kk = m.group(1).split(b", ")
        vv = m.group(2).split(b", ")
        ii = list(zip(kk, vv))
        ii.sort()

        def f(*args):
            return tuple([filter_scs(self.conn, s) for s in args])

        self.assertEqual(len(ii), len(o))
        self.assertEqual(ii[0], f(b"E'a'", b"E'1'"))
        self.assertEqual(ii[1], f(b"E'b'", b"E''''"))
        self.assertEqual(ii[2], f(b"E'c'", b"NULL"))
        if 'd' in o:
            encc = '\xe0'.encode(
                psycopg2.extensions.encodings[self.conn.encoding])
            self.assertEqual(ii[3], f(b"E'd'", b"E'" + encc + b"'"))
    def test_adapt_8(self):
        if self.conn.server_version >= 90000:
            return self.skipTest("skipping dict adaptation with PG pre-9 syntax")

        from psycopg2.extras import HstoreAdapter

        o = {"a": "1", "b": "'", "c": None}
        if self.conn.encoding == "UTF8":
            o["d"] = u"\xe0"

        a = HstoreAdapter(o)
        a.prepare(self.conn)
        q = a.getquoted()

        self.assert_(q.startswith(b("((")), q)
        ii = q[1:-1].split(b("||"))
        ii.sort()

        self.assertEqual(len(ii), len(o))
        self.assertEqual(ii[0], filter_scs(self.conn, b("(E'a' => E'1')")))
        self.assertEqual(ii[1], filter_scs(self.conn, b("(E'b' => E'''')")))
        self.assertEqual(ii[2], filter_scs(self.conn, b("(E'c' => NULL)")))
        if "d" in o:
            encc = u"\xe0".encode(psycopg2.extensions.encodings[self.conn.encoding])
            self.assertEqual(ii[3], filter_scs(self.conn, b("(E'd' => E'") + encc + b("')")))
示例#4
0
    def test_adapt_9(self):
        if self.conn.info.server_version < 90000:
            return self.skipTest("skipping dict adaptation with PG 9 syntax")

        o = {"a": "1", "b": "'", "c": None}
        if self.conn.encoding == "UTF8":
            o["d"] = u"\xe0"

        a = HstoreAdapter(o)
        a.prepare(self.conn)
        q = a.getquoted()

        m = re.match(br"hstore\(ARRAY\[([^\]]+)\], ARRAY\[([^\]]+)\]\)", q)
        self.assert_(m, repr(q))

        kk = m.group(1).split(b",")
        vv = m.group(2).split(b",")
        ii = list(zip(kk, vv))
        ii.sort()

        self.assertEqual(len(ii), len(o))
        self.assertQuotedEqual(ii[0][0], b"'a'")
        self.assertQuotedEqual(ii[0][1], b"'1'")
        self.assertQuotedEqual(ii[1][0], b"'b'")
        self.assertQuotedEqual(ii[1][1], b"''''")
        self.assertQuotedEqual(ii[2][0], b"'c'")
        self.assertQuotedEqual(ii[2][1], b"NULL")
        if "d" in o:
            encc = u"\xe0".encode(
                psycopg2.extensions.encodings[self.conn.encoding])
            self.assertQuotedEqual(ii[3][0], b"'d'")
            self.assertQuotedEqual(ii[3][1], b"'" + encc + b"'")
示例#5
0
    def test_adapt_8(self):
        if self.conn.server_version >= 90000:
            return self.skipTest(
                "skipping dict adaptation with PG pre-9 syntax")

        from psycopg2.extras import HstoreAdapter

        o = {'a': '1', 'b': "'", 'c': None}
        if self.conn.encoding == 'UTF8':
            o['d'] = '\xe0'

        a = HstoreAdapter(o)
        a.prepare(self.conn)
        q = a.getquoted()

        self.assertTrue(q.startswith(b"(("), q)
        ii = q[1:-1].split(b"||")
        ii.sort()

        self.assertEqual(len(ii), len(o))
        self.assertEqual(ii[0], filter_scs(self.conn, b"(E'a' => E'1')"))
        self.assertEqual(ii[1], filter_scs(self.conn, b"(E'b' => E'''')"))
        self.assertEqual(ii[2], filter_scs(self.conn, b"(E'c' => NULL)"))
        if 'd' in o:
            encc = '\xe0'.encode(
                psycopg2.extensions.encodings[self.conn.encoding])
            self.assertEqual(
                ii[3], filter_scs(self.conn, b"(E'd' => E'" + encc + b"')"))
示例#6
0
    def test_adapt_8(self):
        if self.conn.info.server_version >= 90000:
            return self.skipTest(
                "skipping dict adaptation with PG pre-9 syntax")

        o = {"a": "1", "b": "'", "c": None}
        if self.conn.encoding == "UTF8":
            o["d"] = u"\xe0"

        a = HstoreAdapter(o)
        a.prepare(self.conn)
        q = a.getquoted()

        self.assert_(q.startswith(b"(("), q)
        ii = q[1:-1].split(b"||")
        ii.sort()

        self.assertEqual(len(ii), len(o))
        self.assertQuotedEqual(ii[0], b"('a' => '1')")
        self.assertQuotedEqual(ii[1], b"('b' => '''')")
        self.assertQuotedEqual(ii[2], b"('c' => NULL)")
        if "d" in o:
            encc = u"\xe0".encode(
                psycopg2.extensions.encodings[self.conn.encoding])
            self.assertQuotedEqual(ii[3], b"('d' => '" + encc + b"')")
示例#7
0
    def test_adapt_9(self):
        if self.conn.server_version < 90000:
            return self.skipTest("skipping dict adaptation with PG 9 syntax")

        from psycopg2.extras import HstoreAdapter

        o = {'a': '1', 'b': "'", 'c': None}
        if self.conn.encoding == 'UTF8':
            o['d'] = u'\xe0'

        a = HstoreAdapter(o)
        a.prepare(self.conn)
        q = a.getquoted()

        m = re.match(r'hstore\(ARRAY\[([^\]]+)\], ARRAY\[([^\]]+)\]\)', q)
        self.assert_(m, repr(q))

        kk = m.group(1).split(", ")
        vv = m.group(2).split(", ")
        ii = zip(kk, vv)
        ii.sort()

        self.assertEqual(len(ii), len(o))
        self.assertEqual(ii[0], ("E'a'", "E'1'"))
        self.assertEqual(ii[1], ("E'b'", "E''''"))
        self.assertEqual(ii[2], ("E'c'", "NULL"))
        if 'd' in o:
            encc = u'\xe0'.encode(psycopg2.extensions.encodings[self.conn.encoding])
            self.assertEqual(ii[3], ("E'd'", "E'%s'" % encc))
示例#8
0
    def test_adapt_8(self):
        if self.conn.server_version >= 90000:
            return self.skipTest("skipping dict adaptation with PG pre-9 syntax")

        from psycopg2.extras import HstoreAdapter

        o = {'a': '1', 'b': "'", 'c': None}
        if self.conn.encoding == 'UTF8':
            o['d'] = u'\xe0'

        a = HstoreAdapter(o)
        a.prepare(self.conn)
        q = a.getquoted()

        self.assert_(q.startswith("(("), q)
        self.assert_(q.endswith("))"), q)
        ii = q[1:-1].split("||")
        ii.sort()

        self.assertEqual(len(ii), len(o))
        self.assertEqual(ii[0], filter_scs(self.conn, "(E'a' => E'1')"))
        self.assertEqual(ii[1], filter_scs(self.conn, "(E'b' => E'''')"))
        self.assertEqual(ii[2], filter_scs(self.conn, "(E'c' => NULL)"))
        if 'd' in o:
            encc = u'\xe0'.encode(psycopg2.extensions.encodings[self.conn.encoding])
            self.assertEqual(ii[3], filter_scs(self.conn, "(E'd' => E'%s')" % encc))
示例#9
0
 def __str__(self):
     if self.connection:
         result = self.dumps()
         from psycopg2.extras import HstoreAdapter
         value = HstoreAdapter(result)
         value.prepare(self.connection.connection)
         return value.getquoted()
     else:
         return super(HStoreDict, self).__str__()
示例#10
0
    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")
示例#11
0
 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")