pub struct Parser { /* private fields */ }Expand description
Parser for MySQL and MariaDB DDL.
Feed SQL in one or more chunks, then read the parse tree, the compact table model, or JSON Schema documents.
use sql_ddl_to_json_schema::Parser;
let mut parser = Parser::new("mysql").unwrap();
parser.feed("CREATE TABLE t (id INT PRIMARY KEY);");
let tables = parser.parse_compact().unwrap();
assert_eq!(tables[0]["name"], "t");Implementations§
Source§impl Parser
impl Parser
Sourcepub fn new(dialect: &str) -> Result<Self, Error>
pub fn new(dialect: &str) -> Result<Self, Error>
Build a parser for the given dialect.
An empty string, "mysql", or "mariadb" selects the MySQL grammar.
Any other value returns Error::UnsupportedDialect.
Sourcepub fn feed(&mut self, chunk: &str) -> &mut Self
pub fn feed(&mut self, chunk: &str) -> &mut Self
Feed a chunk of SQL. Splits input into statements at unquoted, unescaped
semicolons and keeps cross-chunk state. Returns &mut Self for chaining.
Sourcepub fn parse(&mut self) -> Result<Value, Error>
pub fn parse(&mut self) -> Result<Value, Error>
Drain the buffered statements and parse each into a P_DDS node.
Returns the parse tree root { id: "MAIN", def: [...] }. This consumes
the pending statements, so a second call with no new input returns an
empty tree. On a parse error the line number is reported in stream
coordinates and all state resets.
use sql_ddl_to_json_schema::Parser;
let mut parser = Parser::new("mysql").unwrap();
parser.feed("CREATE TABLE t (id INT);");
let first = parser.parse().unwrap();
assert_eq!(first["def"].as_array().unwrap().len(), 1);
// The statements are drained. A second call sees nothing.
let second = parser.parse().unwrap();
assert!(second["def"].as_array().unwrap().is_empty());Sourcepub fn parse_compact(&mut self) -> Result<Vec<Value>, Error>
pub fn parse_compact(&mut self) -> Result<Vec<Value>, Error>
Drain the buffered statements and build the compact table model.
Consumes the pending statements. To format a tree you already hold, use
compact_from_tree.
Sourcepub fn parse_json_schema(
&mut self,
options: JsonSchemaOptions,
) -> Result<Vec<Value>, Error>
pub fn parse_json_schema( &mut self, options: JsonSchemaOptions, ) -> Result<Vec<Value>, Error>
Drain the buffered statements and build JSON Schema draft-07 documents, one per table.
Consumes the pending statements. Pass JsonSchemaOptions::default for
the default { use_ref: true }. To format tables you already hold, use
json_schema_from_tables.