Skip to main content

Parser

Struct Parser 

Source
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

Source

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.

Source

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.

Source

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());
Source

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.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.