Very cool!


On Wed, Apr 16, 2014 at 3:23 PM, Daniel McDonald
<notifications@github.com>wrote:

> In qiita_db/sql_connection.py:
>
> > @@ -118,15 +121,15 @@ def execute_fetchone(self, sql, sql_args=None, pgcursor=None):
> >          if not pgcursor:
> >                  pgcursor = self._dflt_cursor
> >          # Check that sql arguments have the correct type
> > -        _check_sql_args(sql_args)
> > +        self._check_sql_args(sql_args)
> >          # Execute the query
> >          try:
>
> Sounds good.
>
> @squirrelo <https://github.com/squirrelo>, @josenavas<https://github.com/josenavas>and
> @adamrp <https://github.com/adamrp> , how does this look for managing the
> cursors? db below is an instance of the greengenes database (which doesn't
> here, just needed a connect instance to play with), but doesn't matter
> right now. What's cool is that foo (ie, the execute* methods here)
> creates a cursor, does something and then yields it. What's awesome is that
> the yielded cursor can than handle calls to whatever fetch method, and is
> automatically closed once you leave the context.
>
> 15:18:13 (mcdonadt@Daniels-MacBook-Pro-2):/Users/mcdonadt> @contextmanagerdef foo(sql):
>     with db.con.cursor() as cur:
>         cur.execute(sql)
>         yield cur
>    ....:
> 15:18:22 (mcdonadt@Daniels-MacBook-Pro-2):/Users/mcdonadt> with foo("select count(1) from record") as c:
>     print c.fetchall()
>    ....:[(36L,)]15:18:24 (mcdonadt@Daniels-MacBook-Pro-2):/Users/mcdonadt> cOut[18]: <cursor object at 0x107f8c050; closed: -1>
>
> —
> Reply to this email directly or view it on GitHub<https://github.com/biocore/qiita/pull/29/files#r11708127>
> .
>