Changeset 954 for trunk


Ignore:
Timestamp:
Jan 3, 2019, 5:35:30 PM (7 months ago)
Author:
cito
Message:

Make name optional in DB methods for prepared statements

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/pg.py

    r953 r954  
    18701870            command, parameters, types, inline))
    18711871
    1872     def query_prepared(self, name, *args):
     1872    def query_prepared(self, name=None, *args):
    18731873        """Execute a prepared SQL statement.
    18741874
    18751875        This works like the query() method, but you need to pass the name of
    18761876        a prepared statement that you have already created with prepare().
     1877        If you pass no parameters or pass an empty name, then the last unnamed
     1878        statement will be executed.
    18771879        """
    18781880        if not self.db:
    18791881            raise _int_error('Connection is not valid')
     1882        if name is None:
     1883            name = ''
    18801884        if args:
    18811885            self._do_debug('EXECUTE', name, args)
     
    18841888        return self.db.query_prepared(name)
    18851889
    1886     def prepare(self, name, command):
     1890    def prepare(self, command, name=None):
    18871891        """Create a prepared SQL statement.
    18881892
    1889         This creates a prepared statement with the given name for the given
    1890         command for later execution with the query_prepared() method.
    1891         The name can be "" to create an unnamed statement, in which case any
    1892         pre-existing unnamed statement is automatically replaced; otherwise
    1893         it is an error if the statement name is already defined in the current
    1894         database session.
     1893        This creates a prepared statement for the given command with the
     1894        the given name for later execution with the query_prepared() method.
     1895        The name can be empty or left out to create an unnamed statement,
     1896        in which case any pre-existing unnamed statement is automatically
     1897        replaced; otherwise it is an error if the statement name is already
     1898        defined in the current database session.
    18951899
    18961900        If any parameters are used, they can be referred to in the query as
     
    18991903        if not self.db:
    19001904            raise _int_error('Connection is not valid')
     1905        if name is None:
     1906            name = ''
    19011907        self._do_debug('prepare', name, command)
    19021908        return self.db.prepare(name, command)
    19031909
    1904     def describe_prepared(self, name):
     1910    def describe_prepared(self, name=None):
    19051911        """Describe a prepared SQL statement.
    19061912
    19071913        This method returns a Query object describing the result columns of
    1908         the prepared statement with the given name.
    1909         """
     1914        the prepared statement with the given name. If you do not specify a
     1915        name, then the last unnamed statement will be described.
     1916        """
     1917        if name is None:
     1918            name = ''
    19101919        return self.db.describe_prepared(name)
    19111920
     
    19141923
    19151924        This deallocates a previously prepared SQL statement with the given
    1916         name, or deallocates all prepared statements. Prepared statements are
    1917         also deallocated automatically when the current session ends.
     1925        name, or deallocates all prepared statements if you do not specify a
     1926        name. Note that prepared statements are also deallocated automatically
     1927        when the current session ends.
    19181928        """
    19191929        q = "DEALLOCATE %s" % (name or 'ALL',)
  • trunk/tests/test_classic_dbwrapper.py

    r953 r954  
    970970        self.assertEqual(r, 42)
    971971
     972    def testPrepare(self):
     973        p = self.db.prepare
     974        self.assertIsNone(p("select 'hello'", 'my query'))
     975        self.assertIsNone(p("select 'world'", 'my other query'))
     976        self.assertRaises(pg.ProgrammingError,
     977            p, 'my query', "select 'hello, too'")
     978
     979    def testPrepareUnnamed(self):
     980        p = self.db.prepare
     981        self.assertIsNone(p("select null"))
     982        self.assertIsNone(p("select null", None))
     983        self.assertIsNone(p("select null", ''))
     984        self.assertIsNone(p("select null", name=None))
     985        self.assertIsNone(p("select null", name=''))
     986
    972987    def testQueryPreparedWithoutParams(self):
    973988        p = self.db.prepare
    974         p('q1', "select 17")
    975         p('q2', "select 42")
     989        p("select 17", 'q1')
     990        p("select 42", 'q2')
    976991        f = self.db.query_prepared
    977992        r = f('q1').getresult()[0][0]
     
    982997    def testQueryPreparedWithParams(self):
    983998        p = self.db.prepare
    984         p('sum', "select 1 + $1 + $2 + $3")
    985         p('cat', "select initcap($1) || ', ' || $2 || '!'")
     999        p("select 1 + $1 + $2 + $3", 'sum')
     1000        p("select initcap($1) || ', ' || $2 || '!'", 'cat')
    9861001        f = self.db.query_prepared
    9871002        r = f('sum', 2, 3, 5).getresult()[0][0]
     
    9901005        self.assertEqual(r, 'Hello, world!')
    9911006
    992     def testPrepare(self):
     1007    def testQueryPreparedUnnamedWithOutParams(self):
    9931008        p = self.db.prepare
    994         self.assertIsNone(p('',  "select null"))
    995         self.assertIsNone(p('myquery', "select 'hello'"))
    996         self.assertIsNone(p('myquery2', "select 'world'"))
    997         self.assertRaises(pg.ProgrammingError,
    998             p, 'myquery', "select 'hello, too'")
     1009        p("select 'no name'")
     1010        f = self.db.query_prepared
     1011        r = f().getresult()[0][0]
     1012        self.assertEqual(r, 'no name')
     1013        r = f(None).getresult()[0][0]
     1014        self.assertEqual(r, 'no name')
     1015        r = f('').getresult()[0][0]
     1016        self.assertEqual(r, 'no name')
     1017
     1018    def testQueryPreparedUnnamedWithParams(self):
     1019        p = self.db.prepare
     1020        p("select 1 + $1 + $2")
     1021        f = self.db.query_prepared
     1022        r = f(None, 2, 3).getresult()[0][0]
     1023        self.assertEqual(r, 6)
     1024        r = f('', 2, 3).getresult()[0][0]
     1025        self.assertEqual(r, 6)
    9991026
    10001027    def testDescribePrepared(self):
    1001         self.db.prepare('count', 'select 1 as first, 2 as second')
     1028        self.db.prepare("select 1 as first, 2 as second", 'count')
    10021029        f = self.db.describe_prepared
    10031030        r = f('count').listfields()
    10041031        self.assertEqual(r, ('first', 'second'))
     1032
     1033    def testDescribePreparedUnnamed(self):
     1034        self.db.prepare("select null as anon")
     1035        f = self.db.describe_prepared
     1036        r = f().listfields()
     1037        self.assertEqual(r, ('anon',))
     1038        r = f(None).listfields()
     1039        self.assertEqual(r, ('anon',))
     1040        r = f('').listfields()
     1041        self.assertEqual(r, ('anon',))
    10051042
    10061043    def testDeletePrepared(self):
     
    10101047        self.assertRaises(e, f, 'myquery')
    10111048        p = self.db.prepare
    1012         p('q1', "select 1")
    1013         p('q2', "select 2")
     1049        p("select 1", 'q1')
     1050        p("select 2", 'q2')
    10141051        f('q1')
    10151052        f('q2')
    10161053        self.assertRaises(e, f, 'q1')
    10171054        self.assertRaises(e, f, 'q2')
    1018         p('q1', "select 1")
    1019         p('q2', "select 2")
     1055        p("select 1", 'q1')
     1056        p("select 2", 'q2')
    10201057        f()
    10211058        self.assertRaises(e, f, 'q1')
Note: See TracChangeset for help on using the changeset viewer.