コード例 #1
0
ファイル: _range.py プロジェクト: chaunceyjiang/uxdb
    def getquoted(self):
        if self.name is None:
            raise NotImplementedError(
                'RangeAdapter must be subclassed overriding its name '
                'or the getquoted() method')

        r = self.adapted
        if r.isempty:
            return b"'empty'::" + self.name.encode('utf8')

        if r.lower is not None:
            a = adapt(r.lower)
            if hasattr(a, 'prepare'):
                a.prepare(self._conn)
            lower = a.getquoted()
        else:
            lower = b'NULL'

        if r.upper is not None:
            a = adapt(r.upper)
            if hasattr(a, 'prepare'):
                a.prepare(self._conn)
            upper = a.getquoted()
        else:
            upper = b'NULL'

        return self.name.encode('utf8') + b'(' + lower + b', ' + upper \
            + b", '" + r._bounds.encode('utf8') + b"')"
コード例 #2
0
    def test_adapt_subtype(self):
        class Sub(str):
            pass

        s1 = "hel'lo"
        s2 = Sub(s1)
        self.assertEqual(adapt(s1).getquoted(), adapt(s2).getquoted())
コード例 #3
0
ファイル: extras.py プロジェクト: chaunceyjiang/uxdb
    def _getquoted_9(self):
        """Use the hstore(text[], text[]) function."""
        if not self.wrapped:
            return b"''::hstore"

        k = _ext.adapt(list(self.wrapped.keys()))
        k.prepare(self.conn)
        v = _ext.adapt(list(self.wrapped.values()))
        v.prepare(self.conn)
        return b"hstore(" + k.getquoted() + b", " + v.getquoted() + b")"
コード例 #4
0
ファイル: test_quote.py プロジェクト: chaunceyjiang/uxdb
 def test_set_encoding(self):
     # Note: this works-ish mostly in case when the standard db connection
     # we test with is utf8, otherwise the encoding chosen by PQescapeString
     # may give bad results.
     snowman = u"\u2603"
     a = adapt(snowman)
     a.encoding = 'utf8'
     self.assertEqual(a.encoding, 'utf8')
     self.assertEqual(a.getquoted(), b"'\xe2\x98\x83'")
コード例 #5
0
    def test_conform_subclass_precedence(self):
        class foo(tuple):
            def __conform__(self, proto):
                return self

            def getquoted(self):
                return 'bar'

        self.assertEqual(adapt(foo((1, 2, 3))).getquoted(), 'bar')
コード例 #6
0
    def test_adapt_subtype_3(self):
        class A:
            pass

        class B(A):
            pass

        register_adapter(A, lambda a: AsIs("a"))
        self.assertEqual(b"a", adapt(B()).getquoted())
コード例 #7
0
ファイル: test_quote.py プロジェクト: chaunceyjiang/uxdb
    def test_connection_wins_anyway(self):
        snowman = u"\u2603"
        a = adapt(snowman)
        a.encoding = 'latin9'

        self.conn.set_client_encoding('utf8')
        a.prepare(self.conn)

        self.assertEqual(a.encoding, 'utf_8')
        self.assertQuotedEqual(a.getquoted(), b"'\xe2\x98\x83'")
コード例 #8
0
ファイル: _range.py プロジェクト: chaunceyjiang/uxdb
    def getquoted(self):
        r = self.adapted
        if r.isempty:
            return b"'empty'"

        if not r.lower_inf:
            # not exactly: we are relying that none of these object is really
            # quoted (they are numbers). Also, I'm lazy and not preparing the
            # adapter because I assume encoding doesn't matter for these
            # objects.
            lower = adapt(r.lower).getquoted().decode('ascii')
        else:
            lower = ''

        if not r.upper_inf:
            upper = adapt(r.upper).getquoted().decode('ascii')
        else:
            upper = ''

        return ("'%s%s,%s%s'" %
                (r._bounds[0], lower, upper, r._bounds[1])).encode('ascii')
コード例 #9
0
    def test_adapt_most_specific(self):
        class A(object):
            pass

        class B(A):
            pass

        class C(B):
            pass

        register_adapter(A, lambda a: AsIs("a"))
        register_adapter(B, lambda b: AsIs("b"))
        self.assertEqual(b'b', adapt(C()).getquoted())
コード例 #10
0
    def as_string(self, context):
        # is it a connection or cursor?
        if isinstance(context, ext.connection):
            conn = context
        elif isinstance(context, ext.cursor):
            conn = context.connection
        else:
            raise TypeError("context must be a connection or a cursor")

        a = ext.adapt(self._wrapped)
        if hasattr(a, 'prepare'):
            a.prepare(conn)

        rv = a.getquoted()
        if PY3 and isinstance(rv, bytes):
            rv = rv.decode(ext.encodings[conn.encoding])

        return rv
コード例 #11
0
ファイル: test_quote.py プロジェクト: chaunceyjiang/uxdb
 def test_encoding_error(self):
     snowman = u"\u2603"
     a = adapt(snowman)
     self.assertRaises(UnicodeEncodeError, a.getquoted)
コード例 #12
0
ファイル: test_quote.py プロジェクト: chaunceyjiang/uxdb
 def test_encoding_default(self):
     a = adapt("hello")
     self.assertEqual(a.encoding, 'latin1')
     self.assertEqual(a.getquoted(), b"'hello'")