Skip to content

Commit 8f622ec

Browse files
committed
Updated documentation for SqlDoc creation methods and bumped version
1 parent 32a3fab commit 8f622ec

File tree

5 files changed

+130
-11
lines changed

5 files changed

+130
-11
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sql_docs"
3-
version = "1.0.11"
3+
version = "1.1.0"
44
edition = "2024"
55
description = "A crate for parsing comments from sql files and using them for documentation generation"
66
documentation = "https://docs.rs/sql_docs"

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ These are the primary entry points most users will interact with:
9494
* [`SqlDocBuilder::build`](https://docs.rs/sql-docs/latest/sql_docs/sql_doc/struct.SqlDocBuilder.html#method.build) Finalize the builder and produce a [`SqlDoc`].
9595
* [`SqlDocBuilder::deny`](https://docs.rs/sql-docs/latest/sql_docs/sql_doc/struct.SqlDocBuilder.html#method.deny) Exclude specific files by full path.
9696
* [`SqlDocBuilder::flatten_multiline`](https://docs.rs/sql-docs/latest/sql_docs/sql_doc/struct.SqlDocBuilder.html#method.flatten_multiline) Flatten multiline comments into a single line.
97+
* [`SqlDocBuilder::collect_single_nearest`](https://docs.rs/sql-docs/latest/sql_docs/sql_doc/struct.SqlDocBuilder.html#method.collect_single_nearest) Collect only the nearest leading comment.
9798

9899
## Use Cases
99100

@@ -102,7 +103,7 @@ This crate is designed for generating documentation from SQL schemas by attachin
102103
* Tables
103104
* Columns
104105

105-
using **only comments that immediately precede** the items they describe.
106+
using **only comments that immediately precede** the items they describe, with the ability to differentiate between multiple leading comments per statement or collect only the nearest comment preceding a statement.
106107

107108
This makes it well-suited for:
108109

@@ -116,4 +117,5 @@ This makes it well-suited for:
116117
* Inline and interstitial comments are intentionally ignored.
117118
* Comment attachment is line-based and deterministic.
118119
* One SQL file may define multiple tables.
119-
* No database connection is required.
120+
* No database connection is required.
121+
* `sql_doc` items are sorted by `table` name and `column` name, supporting binary searching

fuzz/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sql_doc.rs

Lines changed: 123 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,55 @@ impl SqlDoc {
3030
Self { tables }
3131
}
3232

33-
/// Method for generating builder from a directory.
34-
pub fn from_dir<P: AsRef<Path>>(root: &P) -> SqlDocBuilder<'_> {
33+
/// Creates an [`SqlDocBuilder`] that will scan a directory for SQL files and build an [`SqlDoc`].
34+
///
35+
/// This is the most convenient entry point when you have a folder of `.sql` files.
36+
/// The returned builder can be further configured with builder methods before calling [`SqlDocBuilder::build`].
37+
///
38+
/// # Parameters
39+
/// - `root`: Path to the directory containing SQL files.
40+
///
41+
/// # Examples
42+
/// ```no_run
43+
/// use sql_docs::sql_doc::SqlDoc;
44+
///
45+
/// let doc = SqlDoc::from_dir("migrations")
46+
/// .deny("migrations/old/ignore.sql")
47+
/// .build()
48+
/// .unwrap();
49+
///
50+
/// // Work with table docs
51+
/// let users = doc.table("users", None).unwrap();
52+
/// assert_eq!(users.name(), "users");
53+
/// ```
54+
pub fn from_dir<P: AsRef<Path> + ?Sized>(root: &P) -> SqlDocBuilder<'_> {
3555
SqlDocBuilder {
3656
source: SqlFileDocSource::Dir(root.as_ref().to_path_buf()),
3757
deny: Vec::new(),
3858
multiline_flat: MultiFlatten::default(),
3959
leading_type: LeadingCommentCapture::default(),
4060
}
4161
}
42-
/// Method for generating builder from a [`Path`] of a single file
62+
63+
/// Creates an [`SqlDocBuilder`] from a single SQL file on disk.
64+
///
65+
/// Use this when you want documentation for one specific file. The resulting tables will have their
66+
/// `path` stamped from the provided file path (see tests such as `build_sql_doc_from_file`).
67+
///
68+
/// # Parameters
69+
/// - `path`: Path to a single SQL file.
70+
///
71+
/// # Examples
72+
/// ```no_run
73+
/// use sql_docs::sql_doc::SqlDoc;
74+
///
75+
/// let doc = SqlDoc::from_path("schema.sql")
76+
/// .build()
77+
/// .unwrap();
78+
///
79+
/// let t = doc.table("users", None).unwrap();
80+
/// assert_eq!(t.name(), "users");
81+
/// ```
4382
pub fn from_path<P: AsRef<Path> + ?Sized>(path: &P) -> SqlDocBuilder<'_> {
4483
SqlDocBuilder {
4584
source: SqlFileDocSource::File(path.as_ref().to_path_buf()),
@@ -49,7 +88,27 @@ impl SqlDoc {
4988
}
5089
}
5190

52-
/// Method for generating builder from a [`[Path]`]
91+
/// Creates an [`SqlDocBuilder`] from an explicit list of SQL file paths.
92+
///
93+
/// This is useful when the files you want are scattered across directories, or when you already
94+
/// have an exact list (e.g. selected by another tool). Each parsed table will have its `path`
95+
/// stamped based on the file it came from (see `test_build_sql_doc_from_paths`).
96+
///
97+
/// # Parameters
98+
/// - `paths`: Slice of paths to SQL files.
99+
///
100+
/// # Examples
101+
/// ```no_run
102+
/// use sql_docs::sql_doc::SqlDoc;
103+
///
104+
/// let paths = vec!["one.sql", "two.sql"];
105+
/// let doc = SqlDoc::from_paths(&paths)
106+
/// .build()
107+
/// .unwrap();
108+
///
109+
/// assert!(doc.table("users", None).is_ok());
110+
/// assert!(doc.table("posts", None).is_ok());
111+
/// ```
53112
pub fn from_paths<P: AsRef<Path>>(paths: &[P]) -> SqlDocBuilder<'_> {
54113
SqlDocBuilder {
55114
source: SqlFileDocSource::Files(
@@ -61,7 +120,34 @@ impl SqlDoc {
61120
}
62121
}
63122

64-
/// Creates a builder from SQL text (no filesystem path is associated) from a [`str`]
123+
/// Creates an [`SqlDocBuilder`] from raw SQL text.
124+
///
125+
/// This does **not** associate any filesystem path with the input, so discovered tables will have
126+
/// `path == None` (see `test_builder_from_str_no_path_has_none_path`).
127+
///
128+
/// This is handy for:
129+
/// - tests
130+
/// - parsing SQL from a network source
131+
/// - parsing SQL assembled in-memory
132+
///
133+
/// # Parameters
134+
/// - `content`: SQL text to parse.
135+
///
136+
/// # Examples
137+
/// ```
138+
/// use sql_docs::sql_doc::SqlDoc;
139+
///
140+
/// let sql = r#"
141+
/// -- Users table
142+
/// CREATE TABLE users (id INTEGER PRIMARY KEY);
143+
/// "#;
144+
///
145+
/// let doc = SqlDoc::builder_from_str(sql).build().unwrap();
146+
/// let users = doc.table("users", None).unwrap();
147+
///
148+
/// // No backing file path when built from a string:
149+
/// assert_eq!(users.path(), None);
150+
/// ```
65151
#[must_use]
66152
pub fn builder_from_str(content: &str) -> SqlDocBuilder<'_> {
67153
SqlDocBuilder {
@@ -72,7 +158,38 @@ impl SqlDoc {
72158
}
73159
}
74160

75-
/// Creates a builder from a vec of tuples containing the `sql` as [`String`] and the path as [`PathBuf`]
161+
/// Creates an [`SqlDocBuilder`] from from raw SQL text while preserving an associated path.
162+
///
163+
/// Each tuple is interpreted as:
164+
/// - `String`: the SQL text to parse
165+
/// - `PathBuf`: the path to associate with that SQL text
166+
///
167+
///
168+
/// # Parameters
169+
/// - `string_with_path`: Slice of `(sql, path)` pairs, where `sql` is the SQL text and `path` is
170+
/// the path that should be attached to any discovered tables.
171+
///
172+
/// # Examples
173+
/// ```
174+
/// use std::path::PathBuf;
175+
/// use sql_docs::sql_doc::SqlDoc;
176+
///
177+
/// let sql_users = "CREATE TABLE users (id INTEGER PRIMARY KEY);".to_owned();
178+
/// let sql_posts = "CREATE TABLE posts (id INTEGER PRIMARY KEY);".to_owned();
179+
///
180+
/// let p1 = PathBuf::from("a/users.sql");
181+
/// let p2 = PathBuf::from("b/posts.sql");
182+
///
183+
/// let inputs = vec![(sql_users, p1.clone()), (sql_posts, p2.clone())];
184+
///
185+
/// let doc = SqlDoc::builder_from_strs_with_paths(&inputs).build().unwrap();
186+
///
187+
/// let users = doc.table("users", None).unwrap();
188+
/// let posts = doc.table("posts", None).unwrap();
189+
///
190+
/// assert_eq!(users.path(), Some(p1.as_path()));
191+
/// assert_eq!(posts.path(), Some(p2.as_path()));
192+
/// ```
76193
#[must_use]
77194
pub fn builder_from_strs_with_paths(
78195
string_with_path: &[(String, PathBuf)],

0 commit comments

Comments
 (0)