Ruby bindings for polyglot-sql, a Rust-based SQL transpiler supporting 30+ database dialects. Built with Magnus for Rust ↔ Ruby FFI via native extensions.
Two-layer architecture following the catkins/monty-rb and catkins/slatedb-rb patterns:
lib.rs— Magnus init entry point, registers module-level functions with underscore prefixes (_transpile,_parse, etc.)dialect.rs— Maps Ruby string/symbol dialect names topolyglot_sql::dialects::DialectTypeenum valueserrors.rs— Defines Ruby exception hierarchy (Polyglot::Error,ParseError,GenerateError,UnsupportedError) using thread-local storage pattern
lib/polyglot.rb— Main entry point, loads native extension, defines idiomatic Ruby API with keyword argumentslib/polyglot/version.rb— Gem version constantlib/polyglot/validation_result.rb—ValidationResultandValidationErrorwrapper classes
- JSON bridge for AST: The
ExpressionAST type (600+ variants) is serialized to/from JSON viaserde_json. Ruby receivesHash/Arrayrepresentations. This avoids mapping hundreds of Rust enum variants to Ruby classes. - Keyword arguments: Ruby methods use keyword args (
from:,to:,dialect:) while Rust methods use positional args with underscore prefixes. - Symbol/string dialect names: Both
:postgresand"postgres"are accepted; normalized via.to_sin Ruby and case-insensitive matching in Rust.
# Install dependencies
bundle install
# Compile native extension + run tests
bundle exec rake
# Just compile
bundle exec rake compile
# Just run tests
bundle exec rake spec
# Lint
bundle exec standardrb
# Lint with auto-fix
bundle exec standardrb --fix- Add Rust function in
ext/polyglot_rb/src/lib.rs - Register it with
module.define_singleton_method("_method_name", function!(method, N))?; - Add Ruby wrapper in
lib/polyglot.rbwith keyword arguments and documentation - Add RSpec tests in
spec/ - Run
bundle exec raketo compile and test
- polyglot-sql — Core Rust SQL transpiler crate
- magnus — Rust ↔ Ruby FFI bridge
- rb-sys — Ruby native extension build system
- serde_json — JSON serialization for AST interchange
- RSpec for Ruby-level tests (
spec/) - StandardRB for Ruby linting
- Tests cover: transpilation, parsing, generation, formatting, validation, error handling, dialect support