Skip to content
This repository has been archived by the owner on Apr 23, 2018. It is now read-only.

Commit

Permalink
Use namedtuple
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed May 27, 2013
1 parent cc56953 commit 66f7f64
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions pystil/websocket.py
Expand Up @@ -7,8 +7,6 @@
from sqlalchemy.sql.compiler import SQLCompiler
from pystil.utils import visit_to_table_line
from sqlalchemy import func, desc
from sqlalchemy.dialects.postgresql.psycopg2 import PGExecutionContext_psycopg2
from sqlalchemy.engine.result import ResultProxy
from datetime import datetime
from functools import partial
import psycopg2
Expand Down Expand Up @@ -109,13 +107,6 @@ def on_message(self, message):
dialect = query.session.bind.dialect
compiler = SQLCompiler(dialect, query.statement)
compiler.compile()

self.context = PGExecutionContext_psycopg2()
self.context.dialect = dialect
self.context.root_connection = query.session.bind
self.context.engine = self.application.db_engine
self.context._translate_colname = None
self.context.result_map = compiler.result_map
self.execute(compiler.string, compiler.params)

def execute(self, query, parameters):
Expand All @@ -125,7 +116,9 @@ def execute(self, query, parameters):
log.warn('No connection')
return adb._ioloop.add_callback(partial(self.execute, query))
self.connection = self.momoko_connection.connection
self.cursor = self.context.cursor = self.connection.cursor()

self.cursor = self.connection.cursor(
cursor_factory=psycopg2.extras.NamedTupleCursor)
self.cursor.execute(
'BEGIN;'
'DECLARE visit_cur SCROLL CURSOR FOR '
Expand Down Expand Up @@ -154,14 +147,15 @@ def io_callback(self, fd=None, events=None):
self.momoko_connection.ioloop.remove_handler(
self.momoko_connection.fileno)
return
rows = ResultProxy(self.context).fetchmany()
rows = self.cursor.fetchmany()
if not rows:
self.terminated = True
self.cursor.execute('CLOSE visit_cur; ROLLBACK;')
else:
try:
for row in rows:
self.write_message('VISIT|' + visit_to_table_line(row))
self.write_message(
'VISIT|' + visit_to_table_line(row))
except:
log.exception('During write')
self.terminated = True
Expand Down

0 comments on commit 66f7f64

Please sign in to comment.