Closed GoogleCodeExporter closed 9 years ago
Here's a patch that makes pymssql.Connection into a context manager:
vagrant@lucid64:~/dev/hg-repos/pymssql$ hg diff pymssql.pyx diff -r 54554641cc7e pymssql.pyx --- a/pymssql.pyx Mon Aug 12 22:12:54 2013 -0700 +++ b/pymssql.pyx Thu Aug 15 09:33:27 2013 -0700 @@ -213,6 +213,12 @@ self._conn.execute_non_query('%s TRAN' % tran_type) self._autocommit = status
Original comment by msabr...@gmail.com
on 15 Aug 2013 at 4:37
Issue 62 has been merged into this issue.
Original comment by msabr...@gmail.com
on 15 Aug 2013 at 5:44
This patch makes both pymssql.Connection and pymssql.Cursor into context
managers:
diff -r 54554641cc7e pymssql.pyx --- a/pymssql.pyx Mon Aug 12 22:12:54 2013 -0700 +++ b/pymssql.pyx Thu Aug 15 09:43:39 2013 -0700 @@ -213,6 +213,12 @@ self._conn.execute_non_query('%s TRAN' % tran_type) self._autocommit = status
self.close()
+ def close(self): """ Close the connection to the databsae. Implicitly rolls back all @@ -333,6 +339,12 @@ """ return self
We should have tests for these.
Original comment by msabr...@gmail.com
on 18 Aug 2013 at 5:42
And here are some tests:
"""
Test context managers -- i.e.: the `with` statement
"""
try:
import unittest2 as unittest
except ImportError:
import unittest
from pymssql import InterfaceError
from .helpers import pymssqlconn
class TestContextManagers(unittest.TestCase):
def test_pymssql_Connection_with(self):
with pymssqlconn() as conn:
cursor = conn.cursor()
cursor.execute("SELECT @@version AS version")
self.assertIsNotNone(conn._conn)
with self.assertRaises(InterfaceError) as context:
self.assertIsNotNone(conn._conn)
self.assertEqual(str(context.exception), "Connection is closed.")
def test_pymssql_Cursor_with(self):
conn = pymssqlconn()
with conn.cursor() as cursor:
cursor.execute("SELECT @@version AS version")
self.assertIsNotNone(conn._conn)
self.assertIsNotNone(cursor)
with self.assertRaises(InterfaceError) as context:
cursor.execute("SELECT @@version AS version")
self.assertEqual(str(context.exception), "Cursor is closed.")
Original comment by msabr...@gmail.com
on 18 Aug 2013 at 5:44
Fixed by:
https://code.google.com/p/pymssql/source/detail?r=5303a5a787e28a0c29f5400a314ee1
a518a749ad
It would be great if folks could verify...
Original comment by msabr...@gmail.com
on 19 Aug 2013 at 4:56
Original issue reported on code.google.com by
airdrik@gmail.com
on 13 Aug 2013 at 6:05