Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle DESCRIBE SELECT #61

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions mysql_mimic/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ async def _show(self, expression: exp.Show) -> AllowedResult:
async def _describe_middleware(self, q: Query) -> AllowedResult:
"""Intercept DESCRIBE statements"""
if isinstance(q.expression, exp.Describe):
if isinstance(q.expression.this, exp.Select):
# Mysql parse treats EXPLAIN SELECT as a DESCRIBE SELECT statement
return await q.next()
name = q.expression.this.name
show = self.dialect().parse(f"SHOW COLUMNS FROM {name}")[0]
return await self._show(show) if isinstance(show, exp.Show) else None
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ async def query(
self.waiting.set()
await self.pause.wait()
self.waiting.clear()

if isinstance(expression, exp.Describe):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this?
I'm not sure what the assert is doing below. It must be intended for the execute call.

assert isinstance(expression.this, exp.Select)
sql = expression.this.sql()
return [(sql,)], ["sql"]

assert isinstance(expression, exp.Select)
self.last_query_attrs = attrs
if self.echo:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,14 @@ async def test_query_attributes(
},
],
),
(
"describe SELECT b FROM a",
[
{
"sql": "SELECT b FROM a",
},
],
),
(
"show tables from information_schema like 'k%'",
[
Expand Down
Loading