Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions aiomysql/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,17 +387,20 @@ def escape_string(self, s):
return s.replace("'", "''")
return escape_string(s)

def cursor(self, *cursors):
def cursor(self, echo=False, *cursors):
"""Instantiates and returns a cursor

By default, :class:`Cursor` is returned. It is possible to also give a
custom cursor through the cursor_class parameter, but it needs to
be a subclass of :class:`Cursor`

:param cursor: custom cursor class.
:param cursors: custom cursor class.
:param echo: create cursor with echo option
:returns: instance of cursor, by default :class:`Cursor`
:raises TypeError: cursor_class is not a subclass of Cursor.
"""
if echo is None:
echo = self._echo
self._ensure_alive()
self._last_usage = self._loop.time()
try:
Expand All @@ -407,14 +410,14 @@ def cursor(self, *cursors):
except TypeError:
raise TypeError('Custom cursor must be subclass of Cursor')
if cursors and len(cursors) == 1:
cur = cursors[0](self, self._echo)
cur = cursors[0](self, echo)
elif cursors:
cursor_name = ''.join(map(lambda x: x.__name__, cursors)) \
.replace('Cursor', '') + 'Cursor'
cursor_class = type(cursor_name, cursors, {})
cur = cursor_class(self, self._echo)
cur = cursor_class(self, echo)
else:
cur = self.cursorclass(self, self._echo)
cur = self.cursorclass(self, echo)
fut = self._loop.create_future()
fut.set_result(cur)
return _ContextManager(fut)
Expand Down Expand Up @@ -657,6 +660,9 @@ async def _execute_command(self, command, sql):
if isinstance(sql, str):
sql = sql.encode(self._encoding)

if self._echo:
logger.debug("ECHO: " + str(sql), extra={"command": command})

chunk_size = min(MAX_PACKET_LEN, len(sql) + 1) # +1 is for command

prelude = struct.pack('<iB', chunk_size, command)
Expand Down