Python psycopg2.extensions.ISQLQuote() Examples

The following are 14 code examples of psycopg2.extensions.ISQLQuote(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module psycopg2.extensions , or try the search function .
Example #1
Source File: test_types_extras.py    From syntheticmass with Apache License 2.0 6 votes vote down vote up
def test_none_fast_path(self):
        # the None adapter is not actually invoked in regular adaptation

        class WonkyAdapter(object):
            def __init__(self, obj): pass
            def getquoted(self): return "NOPE!"

        curs = self.conn.cursor()

        orig_adapter = ext.adapters[type(None), ext.ISQLQuote]
        try:
            ext.register_adapter(type(None), WonkyAdapter)
            self.assertEqual(ext.adapt(None).getquoted(), "NOPE!")

            s = curs.mogrify("SELECT %s;", (None,))
            self.assertEqual(b("SELECT NULL;"), s)

        finally:
            ext.register_adapter(type(None), orig_adapter) 
Example #2
Source File: adapter.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def __conform__(self, proto):
        # Does the given protocol conform to what Psycopg2 expects?
        if proto == ISQLQuote:
            return self
        else:
            raise Exception('Error implementing psycopg2 protocol. Is psycopg2 installed?') 
Example #3
Source File: extras.py    From aws-workshop with MIT License 5 votes vote down vote up
def __conform__(self, proto):
        if proto is _ext.ISQLQuote:
            return self 
Example #4
Source File: extras.py    From aws-workshop with MIT License 5 votes vote down vote up
def __conform__(self, proto):
        if proto is _ext.ISQLQuote:
            return self 
Example #5
Source File: _range.py    From aws-workshop with MIT License 5 votes vote down vote up
def __conform__(self, proto):
        if self._proto is ISQLQuote:
            return self 
Example #6
Source File: adapter.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def __conform__(self, proto):
        # Does the given protocol conform to what Psycopg2 expects?
        if proto == ISQLQuote:
            return self
        else:
            raise Exception('Error implementing psycopg2 protocol. Is psycopg2 installed?') 
Example #7
Source File: test_types_extras.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def test_adapt_fail(self):
        class Foo(object): pass
        self.assertRaises(psycopg2.ProgrammingError,
            psycopg2.extensions.adapt, Foo(), ext.ISQLQuote, None)
        try:
            psycopg2.extensions.adapt(Foo(), ext.ISQLQuote, None)
        except psycopg2.ProgrammingError, err:
            self.failUnless(str(err) == "can't adapt type 'Foo'") 
Example #8
Source File: test_types_extras.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def test_register_on_dict(self):
        from psycopg2.extras import Json
        psycopg2.extensions.register_adapter(dict, Json)

        try:
            curs = self.conn.cursor()
            obj = {'a': 123}
            self.assertEqual(curs.mogrify("%s", (obj,)),
                b("""'{"a": 123}'"""))
        finally:
           del psycopg2.extensions.adapters[dict, ext.ISQLQuote] 
Example #9
Source File: test_types_extras.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def test_register_range_adapter(self):
        from psycopg2.extras import Range, register_range
        cur = self.conn.cursor()
        cur.execute("create type textrange as range (subtype=text)")
        rc = register_range('textrange', 'TextRange', cur)

        TextRange = rc.range
        self.assert_(issubclass(TextRange, Range))
        self.assertEqual(TextRange.__name__, 'TextRange')

        r = TextRange('a', 'b', '(]')
        cur.execute("select %s", (r,))
        r1 = cur.fetchone()[0]
        self.assertEqual(r1.lower, 'a')
        self.assertEqual(r1.upper, 'b')
        self.assert_(not r1.lower_inc)
        self.assert_(r1.upper_inc)

        cur.execute("select %s", ([r,r,r],))
        rs = cur.fetchone()[0]
        self.assertEqual(len(rs), 3)
        for r1 in rs:
            self.assertEqual(r1.lower, 'a')
            self.assertEqual(r1.upper, 'b')
            self.assert_(not r1.lower_inc)
            self.assert_(r1.upper_inc)

        # clear the adapters to allow precise count by scripts/refcounter.py
        del ext.adapters[rc.range, ext.ISQLQuote] 
Example #10
Source File: extras.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def __conform__(self, proto):
        if proto is _ext.ISQLQuote:
            return self 
Example #11
Source File: extras.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def __conform__(self, proto):
        if proto is _ext.ISQLQuote:
            return self 
Example #12
Source File: _range.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def __conform__(self, proto):
        if self._proto is ISQLQuote:
            return self 
Example #13
Source File: test_fields.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_conform_accepts_ISQLQuote(self):
        mac = MAC(factory.make_mac_address())
        self.assertEqual(mac, mac.__conform__(ISQLQuote)) 
Example #14
Source File: test_types_extras.py    From syntheticmass with Apache License 2.0 4 votes vote down vote up
def test_range_escaping(self):
        from psycopg2.extras import register_range
        cur = self.conn.cursor()
        cur.execute("create type textrange as range (subtype=text)")
        rc = register_range('textrange', 'TextRange', cur)

        TextRange = rc.range
        cur.execute("""
            create table rangetest (
                id integer primary key,
                range textrange)""")

        bounds = [ '[)', '(]', '()', '[]' ]
        ranges = [ TextRange(low, up, bounds[i % 4])
            for i, (low, up) in enumerate(zip(
                [None] + map(chr, range(1, 128)),
                map(chr, range(1,128)) + [None],
                ))]
        ranges.append(TextRange())
        ranges.append(TextRange(empty=True))

        errs = 0
        for i, r in enumerate(ranges):
            # not all the ranges make sense:
            # fun fact: select ascii('#') < ascii('$'), '#' < '$'
            # yelds... t, f! At least in en_GB.UTF-8 collation.
            # which seems suggesting a supremacy of the pound on the dollar.
            # So some of these ranges will fail to insert. Be prepared but...
            try:
                cur.execute("""
                    savepoint x;
                    insert into rangetest (id, range) values (%s, %s);
                    """, (i, r))
            except psycopg2.DataError:
                errs += 1
                cur.execute("rollback to savepoint x;")

        # ...not too many errors! in the above collate there are 17 errors:
        # assume in other collates we won't find more than 30
        self.assert_(errs < 30,
            "too many collate errors. Is the test working?")

        cur.execute("select id, range from rangetest order by id")
        for i, r in cur:
            self.assertEqual(ranges[i].lower, r.lower)
            self.assertEqual(ranges[i].upper, r.upper)
            self.assertEqual(ranges[i].lower_inc, r.lower_inc)
            self.assertEqual(ranges[i].upper_inc, r.upper_inc)
            self.assertEqual(ranges[i].lower_inf, r.lower_inf)
            self.assertEqual(ranges[i].upper_inf, r.upper_inf)

        # clear the adapters to allow precise count by scripts/refcounter.py
        del ext.adapters[TextRange, ext.ISQLQuote]