Skip to content

Commit 8747ac1

Browse files
author
James William Pye
committed
Add db.do() method.
1 parent 44e0014 commit 8747ac1

8 files changed

Lines changed: 52 additions & 5 deletions

File tree

postgresql/api.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,18 @@ def settings(self) -> Settings:
899899
A `Settings` instance bound to the `Database`.
900900
"""
901901

902+
@abstractmethod
903+
def do(source, language = None) -> None:
904+
"""
905+
Execute a DO statement using the given source. Always returns `None` and
906+
raise a `postgresql.exceptions.Error` subclass on error.
907+
908+
If `language` is `None`, no language will be given to the DO statement,
909+
allowing the default configured language to be used.
910+
911+
Likely to be a function of Connection.execute.
912+
"""
913+
902914
@abstractmethod
903915
def execute(sql) -> None:
904916
"""

postgresql/bin/pg_python.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ def command(argv = sys.argv):
9090
# New built-ins
9191
'connector' : connector,
9292
'db' : connection,
93+
'do' : connection.do,
9394
'prepare' : connection.prepare,
94-
# XXX: being replaced by sqlexec.
95-
'execute' : connection.execute,
9695

9796
'sqlexec' : connection.execute,
9897
'settings' : connection.settings,

postgresql/documentation/bin.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ with the following built-ins:
2828
``proc``
2929
``db.proc``
3030

31+
``do``
32+
``db.do``, execute a single DO statement.
33+
3134
``sqlexec``
3235
``db.execute``, execute multiple SQL statements (``None`` is always returned)
3336

34-
3537
pg_python Usage
3638
---------------
3739

postgresql/documentation/changes.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changes
22
=======
33

4+
1.0 in development
5+
------------------
6+
7+
* Added db.do() method for DO-statement support(convenience method).
8+
* **DEPRECATION**: Removed 2PC support documentation.
9+
410
0.9.2 released on 2009-12-14
511
----------------------------
612

@@ -60,7 +66,6 @@ Changes
6066
* Document msghook attributes, and use postgresql.sys.errformat for
6167
postgresql.exceptions.Error.__str__.
6268

63-
6469
0.8.2 released on 2009-06-13
6570
----------------------------
6671

@@ -69,7 +74,6 @@ Changes
6974
qualified names. [Reported by Dallas Morisett]
7075
* Fix DB-API setting of rowcount after execute. [Reported by Mike Bayer; 1010643]
7176

72-
7377
0.8.1 released on 2009-04-30
7478
----------------------------
7579

postgresql/documentation/driver.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ The methods and properties on the connection object are ready for use:
332332
This is used in cases where the cursor was declared on the server. See
333333
`Cursors`_ for more information.
334334

335+
``db.do(source[, language = None])``
336+
Execute a DO statement on the server. If `language` is `None`, none will be
337+
specified and the `default_do_language` database setting will be used by
338+
PostgreSQL.
339+
335340
``db.execute(sql_statements_string)``
336341
Run a block of SQL on the server. This method returns `None` unless an error
337342
occurs. If errors occur, the processing of the statements will stop and the

postgresql/driver/pq3.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,12 @@ def execute(self, query : str) -> None:
17021702
self._pq_push(q, self)
17031703
self._pq_complete()
17041704

1705+
def do(self, source : str, language : str = None) -> None:
1706+
sql = "DO " + pg_str.quote_literal(source)
1707+
if language is not None:
1708+
sql = sql + " LANGUAGE " + pg_str.quote_ident(language)
1709+
self.execute(sql + ";")
1710+
17051711
def xact(self, gid = None, isolation = None, mode = None):
17061712
x = Transaction(self, gid = gid, isolation = isolation, mode = mode)
17071713
return x

postgresql/lib/libsys.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,6 @@ SELECT pg_catalog.pg_relation_size($1::text)::bigint
133133

134134
[pg_reload_conf]
135135
SELECT pg_reload_conf()
136+
137+
[languages::column]
138+
SELECT lanname FROM pg_catalog.pg_language

postgresql/test/test_driver.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,5 +1365,21 @@ def testSettings(self):
13651365
dall.sort(key=itemgetter(0))
13661366
self.failUnlessEqual(dall, all)
13671367

1368+
def testDo(self):
1369+
# plpgsql is expected to be available.
1370+
if self.db.version_info[:2] < (8,5):
1371+
return
1372+
if 'plpgsql' not in self.db.sys.languages():
1373+
self.db.execute("CREATE LANGUAGE plpgsql")
1374+
self.db.do(
1375+
"BEGIN CREATE TEMP TABLE do_tmp_table(i int, t text); END",
1376+
language = 'plpgsql')
1377+
self.failUnlessEqual(len(self.db.prepare("SELECT * FROM do_tmp_table")()), 0)
1378+
# now, with the default language.
1379+
self.db.settings["default_do_language"] = 'plpgsql'
1380+
self.db.do(
1381+
"BEGIN INSERT INTO do_tmp_table VALUES (100, 'foo'); END")
1382+
self.failUnlessEqual(len(self.db.prepare("SELECT * FROM do_tmp_table")()), 1)
1383+
13681384
if __name__ == '__main__':
13691385
unittest.main()

0 commit comments

Comments
 (0)