update sqlitesource
This commit is contained in:
parent
71fee9897b
commit
2e1480d778
@ -154,6 +154,12 @@ class MongoDBSource(object):
|
|||||||
for doc in self.collection.find(self.spec):
|
for doc in self.collection.find(self.spec):
|
||||||
yield dict(doc)
|
yield dict(doc)
|
||||||
|
|
||||||
|
# dict_factory for sqlite source
|
||||||
|
def dict_factory(cursor, row):
|
||||||
|
d = { }
|
||||||
|
for idx, col in enumerate(cursor.description):
|
||||||
|
d[col[0]] = row[idx]
|
||||||
|
return d
|
||||||
|
|
||||||
class SqliteSource(object):
|
class SqliteSource(object):
|
||||||
""" Source that reads from a sqlite database.
|
""" Source that reads from a sqlite database.
|
||||||
@ -164,35 +170,32 @@ class SqliteSource(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, dbpath, query, args=None, conn_params=None):
|
def __init__(self, dbpath, query, args=None, conn_params=None):
|
||||||
|
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
self._dbpath = dbpath
|
self._dbpath = dbpath
|
||||||
self._query = query
|
self._query = query
|
||||||
self._args = args or []
|
self._args = args or []
|
||||||
self._conn_params = conn_params or []
|
self._conn_params = conn_params or []
|
||||||
|
|
||||||
def _process_query(self):
|
# setup connection
|
||||||
|
self._conn = sqlite3.connect(self._dbpath)
|
||||||
import sqlite3
|
self._conn.row_factory = dict_factory
|
||||||
|
|
||||||
def dict_factory(cursor, row):
|
|
||||||
d = { }
|
|
||||||
for idx, col in enumerate(cursor.description):
|
|
||||||
d[col[0]] = row[idx]
|
|
||||||
return d
|
|
||||||
|
|
||||||
conn = sqlite3.connect(self._dbpath)
|
|
||||||
conn.row_factory = dict_factory
|
|
||||||
|
|
||||||
if self._conn_params:
|
if self._conn_params:
|
||||||
for param, value in self._conn_params.iteritems():
|
for param, value in self._conn_params.iteritems():
|
||||||
setattr(conn, param, value)
|
setattr(self._conn, param, value)
|
||||||
|
|
||||||
cursor = conn.cursor()
|
def _process_query(self):
|
||||||
|
|
||||||
|
cursor = self._conn.cursor()
|
||||||
|
|
||||||
for row in cursor.execute(self._query, self._args):
|
for row in cursor.execute(self._query, self._args):
|
||||||
yield row
|
yield row
|
||||||
|
|
||||||
cursor.close()
|
cursor.close()
|
||||||
conn.close()
|
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return self._process_query()
|
return self._process_query()
|
||||||
|
|
||||||
|
def done(self):
|
||||||
|
self._conn.close()
|
||||||
|
Loading…
Reference in New Issue
Block a user