12 releases

0.4.7 Jan 20, 2025
0.4.6 Jan 20, 2025
0.2.2 Dec 10, 2024
0.1.0 Dec 4, 2024

#160 in Procedural macros

Download history 129/week @ 2024-11-30 301/week @ 2024-12-07 54/week @ 2024-12-14 2/week @ 2024-12-21 13/week @ 2024-12-28 48/week @ 2025-01-04 421/week @ 2025-01-11 583/week @ 2025-01-18 7/week @ 2025-01-25

1,066 downloads per month

MIT license

93KB
2K SLoC

sqltk

sqltk is a toolkit for analysis and transformation of SQL statements, built on top of sqlparser.

Features

  • A comprehensive Visitor trait and implementations for all sqlparser AST node types.

  • A Transform trait for rewriting ASTs (sqltk does not provide a VisitorMut trait).

Comprehensive Visitor trait with more useful AST traversal order

sqlparser's Visitor implementation only contains callbacks for a handful of AST node types.

In contrast, sqltk's implementation will invoke Visitor::enter and Visitor::exit for all sqlparser node types.

Additionally, sqltk traverses the AST in an order that is useful for semantic analysis - specifically any node that might be referred to by another node will be visited before a node that might refer to it.

This means your Visitor implementations can safely assume that any semantic dependencies of the node being visited have already been visited.

For example, in a SELECT statement the FROM clause will be visited before the projection or the WHERE clause etc.

The analysis that determines AST traversal order happens at compile time (see packages/sqltk-codegen).

Transform trait

The Transform trait contains a single method imaginitively named transform. Which takes a reference to the original AST node and an owned clone of the node as arguments. Edits are applied to the owned node and returned in a Result.

The reason for this existence of this trait is so that metadata about nodes (from a previous analysis step) which inform the transformation process can be held in the type that implements Transform. These will be regular Rust shared references to AST nodes (and therefore read-only). Which would prevent mutation of the nodes in-place because Rust will not allow coexistence of &node and &mut node.

sqlparser's VisitorMut::visit_mut method accepts a &mut node argument, thus preventing coexistance of references to nodes in another data structure - which rules out the use of some patterns for associating metadata with those nodes.

Transformation begins at the leaf nodes of the AST (AKA depth-first) and ends at the root node.

Getting started

Add sqltk to your Cargo project

cargo add sqltk

build.rs

The build.rs derives implementations of the Visitable, Visitor, Transformable, Transform & Semantic traits.

It works by analysing the source of the sqlparser crate and extracting metadata about all of the AST types.

Regular derive macros cannot derive code for foreign types, which means this unidiomatic approach is needed.

About

sqltk is maintained by CipherStash and is a core component of Proxy, our encryption-in-use database proxy.

Dependencies

~3–5MB
~99K SLoC