Skip to content

SQL: Use cratedb-sqlparse for implementing read-only mode #14

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions cratedb_mcp/util/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import typing as t

import cratedb_sqlparse
import sqlparse
from sqlparse.tokens import Keyword

Expand Down Expand Up @@ -40,6 +41,7 @@ class SqlStatementClassifier:
expression: str
permit_all: bool = False

_parsed_cratedb: t.Any = dataclasses.field(init=False, default=None)
_parsed_sqlparse: t.Any = dataclasses.field(init=False, default=None)

def __post_init__(self) -> None:
Expand All @@ -48,6 +50,14 @@ def __post_init__(self) -> None:
if self.expression:
self.expression = self.expression.strip()

def parse_cratedb(self):
"""
Parse expression using `cratedb-sqlparse` library.
"""
if self._parsed_cratedb is None:
self._parsed_cratedb = cratedb_sqlparse.sqlparse(self.expression)
return self._parsed_cratedb

def parse_sqlparse(self) -> t.List[sqlparse.sql.Statement]:
"""
Parse expression using traditional `sqlparse` library.
Expand Down Expand Up @@ -85,8 +95,8 @@ def operation(self) -> str:
"""
The SQL operation: SELECT, INSERT, UPDATE, DELETE, CREATE, etc.
"""
parsed = self.parse_sqlparse()
return parsed[0].get_type().upper()
parsed = self.parse_cratedb()
return parsed[0].type.upper()

@property
def is_camouflage(self) -> bool:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies = [
"attrs",
"cachetools<6",
"cratedb-about==0.0.4",
"cratedb-sqlparse==0.0.14",
"hishel<0.2",
"mcp[cli]>=1.5.0",
"sqlparse<0.6",
Expand Down