Skip to main content

SQL grammar validator for SingleStore queries

Project description

sql-validator-python

sql-validator-python is a Python library for validating SQL queries using the SingleStore Language Server (version 0.1.3 and later). It provides a convenient interface to check SQL syntax and semantics by communicating with a SingleStore language server instance over TCP or WebSocket (with optional TLS support).

Features

  • Validate SQL queries for correctness using SingleStore's language server
  • Supports TCP, WS, and WSS (WebSocket Secure) connections
  • Flexible connection configuration (host/port or connection string)
  • TLS/SSL support for secure connections
  • Returns detailed validation results, including error tokens and suggestions

Installation

Install via pip (after cloning or packaging):

pip install sql-validator-python

Or add to your pyproject.toml as a dependency.

Usage

from sql_validator import Validator, ConnectionMode

# Example: Connect using TCP
validator = Validator(
	connection_mode=ConnectionMode.TCP,
	server_host="localhost",
	server_port=9000
)

# Example: Connect using a connection string
# validator = Validator(connection_string="tcp://localhost:9000")

result = validator.validate("SELECT * FROM my_table;")
if result.is_valid:
	print("Query is valid!")
else:
	print(f"Error: {result.error_token} at {result.error_token_position}")
	if result.alternative_tokens:
		print("Suggestions:", result.alternative_tokens)

API

Validator

Initialization

You can initialize a Validator in two ways:

  • By specifying connection_mode, server_host, and server_port:

    • connection_mode: ConnectionMode.TCP, ConnectionMode.WS, or ConnectionMode.WSS
    • server_host: Hostname of the language server
    • server_port: Port number
    • tls: Enable TLS (for WSS or secure TCP)
    • tls_ca_file: Path to CA certificate (optional)
    • tls_insecure_skip_verify: Skip TLS verification (optional)
  • By providing a connection_string (e.g., tcp://localhost:9000, ws://host:port, wss://host:port)

Methods

  • validate(query: str, version: str = "") -> ValidationResult
    • Validates the given SQL query.
    • version (optional): Database version string (format: major.minor.build, or empty/"default").
    • Returns a ValidationResult object.

Static Methods

  • _validate_version(version: str) -> bool
    • Checks if the version string matches the required pattern (major.minor.build).

ValidationResult

  • is_valid: bool — Whether the query is valid
  • error_token: Optional[str] — The token that caused the error (if any)
  • error_token_position: Optional[TokenPosition] — Position of the error token
  • alternative_tokens: Optional[CompletionList] — Suggestions for fixing the error

Example

from sql_validator import Validator
validator = Validator(connection_string="tcp://localhost:9000")
result = validator.validate("SELECT foo FROM bar;")
if result.is_valid:
	print("Valid!")
else:
	print("Invalid SQL:", result.error_token)

Notes

  • This library is intended to be used with a running SingleStore Language Server instance (version 0.1.3 or later).
  • It does not start or manage the server process itself.
  • Documentation for the SingleStore Language Server can be found at: https://github.com/singlestore-labs/language-server

License

This project is licensed under the terms of the Apache License, Version 2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

singlestore_sql_validator-0.0.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

singlestore_sql_validator-0.0.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file singlestore_sql_validator-0.0.1.tar.gz.

File metadata

  • Download URL: singlestore_sql_validator-0.0.1.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.11.15 Linux/6.17.0-1013-azure

File hashes

Hashes for singlestore_sql_validator-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2c1cce3991f03724911c64b169e2f5a1ca792955cefcfb28484b604e0f97a23f
MD5 506d0babe6e2f09dccf5183d7e9f0422
BLAKE2b-256 177d149dc236e256aa7fa50226787d49b8dffa5821cb654cba78160f2924082e

See more details on using hashes here.

File details

Details for the file singlestore_sql_validator-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for singlestore_sql_validator-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 743bfeeb61e6294cb106e49ea4ee8654b710b65571a5238ad16bda17da034891
MD5 3aa9bbeae43d0c210d93c1e0299d895b
BLAKE2b-256 b7c06d4b63f81ac927758364b6944119993f8fe8abb7fa7e225192829ce75c38

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page