sql_ddl_to_json_schema/lib.rs
1//! Parse MySQL and MariaDB DDL into a compact table model and JSON Schema.
2//!
3//! Feed SQL Data Definition statements into a [`Parser`], then read one of
4//! three outputs:
5//!
6//! - the parse tree, via [`Parser::parse`]
7//! - the compact table model, via [`Parser::parse_compact`]
8//! - JSON Schema draft-07 documents, via [`Parser::parse_json_schema`]
9//!
10//! Each of these drains the buffered statements. To reformat a tree you already
11//! hold, use the free functions [`compact_from_tree`] and
12//! [`json_schema_from_tables`].
13//!
14//! Only DDL is supported. `SET` parses but is ignored. Database statements
15//! (`USE`, `CREATE DATABASE`, `ALTER DATABASE`, `DROP DATABASE`) parse but do
16//! not produce tables.
17//!
18//! The parser is stream friendly. Input can arrive in chunks. Statements split
19//! at unquoted, unescaped semicolons.
20//!
21//! # Example
22//!
23//! ```
24//! use sql_ddl_to_json_schema::Parser;
25//!
26//! let ddl = "CREATE TABLE users (id INT NOT NULL AUTO_INCREMENT, \
27//! nickname VARCHAR(255) NOT NULL, PRIMARY KEY (id));";
28//!
29//! let mut parser = Parser::new("mysql").unwrap();
30//! parser.feed(ddl);
31//! let schemas = parser.parse_json_schema(Default::default()).unwrap();
32//! assert_eq!(schemas[0]["$id"], "users");
33//! ```
34#![forbid(unsafe_code)]
35#![warn(missing_docs)]
36
37mod compact;
38mod grammar;
39mod json_schema;
40mod keywords;
41mod lexer;
42mod parser;
43
44pub use parser::{compact_from_tree, json_schema_from_tables, Error, JsonSchemaOptions, Parser};