Changeset 842


Ignore:
Timestamp:
Feb 8, 2016, 4:02:10 PM (4 years ago)
Author:
cito
Message:

Treat percent signs in SQL strings always the same

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/contents/changelog.rst

    r841 r842  
    9393      also supported, but yield only an ordinary tuple containing text strings.
    9494    - New type helpers Interval() and Uuid() have been added.
     95    - SQL commands are always handled as if they include parameters, i.e.
     96      literal percent signs must always be doubled. This consistent behavior
     97      is necessary for using pgdb with wrappers like SQLAlchemy.
    9598- Changes concerning both modules:
    9699    - PyGreSQL now tries to raise more specific and appropriate subclasses of
  • trunk/pgdb.py

    r841 r842  
    796796
    797797        This function works for both mappings and sequences.
     798
     799        The function should be used even when there are no parameters,
     800        so that we have a consistent behavior regarding percent signs.
    798801        """
    799         if isinstance(parameters, dict):
    800             parameters = _quotedict(parameters)
    801             parameters.quote = self._quote
    802         else:
    803             parameters = tuple(map(self._quote, parameters))
     802        if parameters:
     803            if isinstance(parameters, dict):
     804                parameters = _quotedict(parameters)
     805                parameters.quote = self._quote
     806            else:
     807                parameters = tuple(map(self._quote, parameters))
     808        else:
     809            parameters = {}
    804810        return string % parameters
    805811
     
    885891            for parameters in seq_of_parameters:
    886892                sql = operation
    887                 if parameters:
    888                     sql = self._quoteparams(sql, parameters)
     893                sql = self._quoteparams(sql, parameters)
    889894                rows = self._src.execute(sql)
    890895                if rows:  # true if not DML
  • trunk/tests/test_dbapi20.py

    r841 r842  
    9090        self.assertIn('.', v)
    9191        self.assertEqual(pgdb.__version__, v)
     92
     93    def test_percent_sign(self):
     94        con = self._connect()
     95        cur = con.cursor()
     96        cur.execute("select %s, 'a %% sign'", ('a % sign',))
     97        self.assertEqual(cur.fetchone(), ('a % sign', 'a % sign'))
     98        cur.execute("select 'a %% sign'")
     99        self.assertEqual(cur.fetchone(), ('a % sign',))
    92100
    93101    def test_callproc_no_params(self):
Note: See TracChangeset for help on using the changeset viewer.