Skip to main content

SqlDoc

Struct SqlDoc 

Source
pub struct SqlDoc { /* private fields */ }
Expand description

Top-level documentation object containing all discovered TableDoc entries.

Implementations§

Source§

impl SqlDoc

Source

pub fn new(tables: Vec<TableDoc>) -> Self

Method for creating a new SqlDoc

Source

pub fn from_dir<P: AsRef<Path> + ?Sized>(root: &P) -> SqlDocBuilder<'_>

Creates an SqlDocBuilder that will scan a directory for SQL files and build an SqlDoc.

This is the most convenient entry point when you have a folder of .sql files. The returned builder can be further configured with builder methods before calling SqlDocBuilder::build.

§Parameters
  • root: Path to the directory containing SQL files.
§Examples
use sql_docs::sql_doc::SqlDoc;

let doc = SqlDoc::from_dir("migrations")
    .deny("migrations/old/ignore.sql")
    .build()
    .unwrap();

// Work with table docs
let users = doc.table("users", None).unwrap();
assert_eq!(users.name(), "users");
Source

pub fn from_path<P: AsRef<Path> + ?Sized>(path: &P) -> SqlDocBuilder<'_>

Creates an SqlDocBuilder from a single SQL file on disk.

Use this when you want documentation for one specific file. The resulting tables will have their path stamped from the provided file path (see tests such as build_sql_doc_from_file).

§Parameters
  • path: Path to a single SQL file.
§Examples
use sql_docs::sql_doc::SqlDoc;

let doc = SqlDoc::from_path("schema.sql")
    .build()
    .unwrap();

let t = doc.table("users", None).unwrap();
assert_eq!(t.name(), "users");
Source

pub fn from_paths<P: AsRef<Path>>(paths: &[P]) -> SqlDocBuilder<'_>

Creates an SqlDocBuilder from an explicit list of SQL file paths.

This is useful when the files you want are scattered across directories, or when you already have an exact list (e.g. selected by another tool). Each parsed table will have its path stamped based on the file it came from (see test_build_sql_doc_from_paths).

§Parameters
  • paths: Slice of paths to SQL files.
§Examples
use sql_docs::sql_doc::SqlDoc;

let paths = vec!["one.sql", "two.sql"];
let doc = SqlDoc::from_paths(&paths)
    .build()
    .unwrap();

assert!(doc.table("users", None).is_ok());
assert!(doc.table("posts", None).is_ok());
Source

pub fn builder_from_str(content: &str) -> SqlDocBuilder<'_>

Creates an SqlDocBuilder from raw SQL text.

This does not associate any filesystem path with the input, so discovered tables will have path == None (see test_builder_from_str_no_path_has_none_path).

This is handy for:

  • tests
  • parsing SQL from a network source
  • parsing SQL assembled in-memory
§Parameters
  • content: SQL text to parse.
§Examples
use sql_docs::sql_doc::SqlDoc;

let sql = r#"
    -- Users table
    CREATE TABLE users (id INTEGER PRIMARY KEY);
"#;

let doc = SqlDoc::builder_from_str(sql).build().unwrap();
let users = doc.table("users", None).unwrap();

// No backing file path when built from a string:
assert_eq!(users.path(), None);
Source

pub fn builder_from_strs_with_paths( string_with_path: &[(String, PathBuf)], ) -> SqlDocBuilder<'_>

Creates an SqlDocBuilder from from raw SQL text while preserving an associated path.

Each tuple is interpreted as:

  • String: the SQL text to parse
  • PathBuf: the path to associate with that SQL text
§Parameters
  • string_with_path: Slice of (sql, path) pairs, where sql is the SQL text and path is the path that should be attached to any discovered tables.
§Examples
use std::path::PathBuf;
use sql_docs::sql_doc::SqlDoc;

let sql_users = "CREATE TABLE users (id INTEGER PRIMARY KEY);".to_owned();
let sql_posts = "CREATE TABLE posts (id INTEGER PRIMARY KEY);".to_owned();

let p1 = PathBuf::from("a/users.sql");
let p2 = PathBuf::from("b/posts.sql");

let inputs = vec![(sql_users, p1.clone()), (sql_posts, p2.clone())];

let doc = SqlDoc::builder_from_strs_with_paths(&inputs).build().unwrap();

let users = doc.table("users", None).unwrap();
let posts = doc.table("posts", None).unwrap();

assert_eq!(users.path(), Some(p1.as_path()));
assert_eq!(posts.path(), Some(p2.as_path()));
Source

pub fn table( &self, name: &str, schema: Option<&str>, ) -> Result<&TableDoc, DocError>

Method for finding a specific TableDoc by name

§Parameters
  • the table name as a str
  • the table schema as Option of str
§Errors
Source

pub fn tables(&self) -> &[TableDoc]

Getter method for returning the &[TableDoc]

Source

pub fn tables_mut(&mut self) -> &mut [TableDoc]

Getter that returns a mutable reference to the TableDoc

Source

pub fn into_tables(self) -> Vec<TableDoc>

Method to move tables out of Structure if needed

Source

pub fn number_of_tables(&self) -> usize

Returns the number of TableDoc

Trait Implementations§

Source§

impl Clone for SqlDoc

Source§

fn clone(&self) -> SqlDoc

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SqlDoc

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for SqlDoc

Source§

type Err = DocError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for SqlDoc

Source§

fn eq(&self, other: &SqlDoc) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for SqlDoc

Source§

impl StructuralPartialEq for SqlDoc

Auto Trait Implementations§

§

impl Freeze for SqlDoc

§

impl RefUnwindSafe for SqlDoc

§

impl Send for SqlDoc

§

impl Sync for SqlDoc

§

impl Unpin for SqlDoc

§

impl UnwindSafe for SqlDoc

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.