1- //! Module for parsing sql and comments and returning only comments connected to
2- //! statements
1+ //! Module for parsing sql and comments and returning `table` and `column` information, including comments
32use sqlparser:: ast:: { Spanned , Statement } ;
43
54use crate :: { ast:: ParsedSqlFile , comments:: Comments } ;
@@ -104,11 +103,14 @@ impl SqlDocs {
104103 #[ allow( clippy:: single_match) ]
105104 match statement {
106105 Statement :: CreateTable ( table) => {
107- let table_start_line = table. span ( ) . start . line ;
106+ let table_start = table. span ( ) . start . line ;
108107 let mut column_docs = Vec :: new ( ) ;
109108 for column in & table. columns {
110109 let column_start = column. span ( ) . start . line ;
111- let column_leading = comments. leading_comment ( column_start) ;
110+ dbg ! ( & column_start) ;
111+ dbg ! ( & column. span( ) . end. line) ;
112+ dbg ! ( & column. name) ;
113+ let column_leading = comments. leading_comment_column ( column_start) ;
112114 let column_name = column. name . value . clone ( ) ;
113115 let column_doc = match column_leading {
114116 Some ( col_comment) => {
@@ -118,7 +120,10 @@ impl SqlDocs {
118120 } ;
119121 column_docs. push ( column_doc) ;
120122 }
121- let table_leading = comments. leading_comment ( table_start_line) ;
123+ let table_leading = comments. leading_comment ( table_start-1 ) ;
124+ dbg ! ( & table_start) ;
125+ dbg ! ( & table. span( ) . end. line) ;
126+ dbg ! ( & table. name) ;
122127 let table_doc = match table_leading {
123128 Some ( comment) => {
124129 TableDoc :: new (
@@ -161,83 +166,199 @@ mod tests {
161166 assert_eq ! ( sql_doc. tables( ) . first( ) . unwrap( ) . name( ) , "user" ) ;
162167 assert_eq ! ( sql_doc. tables( ) . first( ) . unwrap( ) . columns( ) . first( ) . unwrap( ) . name( ) , "id" ) ;
163168 }
164- #[ test]
165- fn generate_docs_from_mixed_comments ( ) {
169+
170+ #[ test]
171+ fn generate_docs_files ( ) {
166172 use std:: path:: Path ;
167173
168- use crate :: { ast:: ParsedSqlFileSet , files:: SqlFileSet , comments:: Comments } ;
174+ use crate :: { ast:: ParsedSqlFileSet , comments:: Comments , files:: SqlFileSet } ;
175+
169176 let path = Path :: new ( "sql_files" ) ;
170177 let set = SqlFileSet :: new ( path, None ) . unwrap ( ) ;
171178 let parsed_set = ParsedSqlFileSet :: parse_all ( set) . unwrap ( ) ;
172- for file in parsed_set. files ( ) . iter ( ) {
179+
180+ for file in parsed_set. files ( ) {
173181 let comments = Comments :: parse_all_comments_from_file ( file) . unwrap ( ) ;
174182 let docs = SqlDocs :: from_parsed_file ( file, & comments) ;
183+
175184 match file. file ( ) . path ( ) . to_str ( ) . unwrap ( ) {
176185 "sql_files/with_single_line_comments.sql" => {
177- let expected = [
178- "Users table stores user account information" ,
179- "Primary key" ,
180- "Username for login" ,
181- "Email address" ,
182- "When the user registered" ,
183- "Posts table stores blog posts" ,
184- "Primary key" ,
185- "Post title" ,
186- "Foreign key linking to users" ,
187- "Main body text" ,
188- "When the post was created" ,
189- ] ;
190- assert_eq ! ( expected. len( ) , comments. comments( ) . len( ) ) ;
191- comments
192- . comments ( )
193- . iter ( )
194- . enumerate ( )
195- . for_each ( |( i, c) | assert_eq ! ( expected[ i] , c. kind( ) . comment( ) ) ) ;
186+ let expected = SqlDocs :: new ( vec ! [
187+ TableDoc :: new(
188+ "users" . to_string( ) ,
189+ Some ( "Users table stores user account information" . to_string( ) ) ,
190+ vec![
191+ ColumnDoc :: new( "id" . to_string( ) , Some ( "Primary key" . to_string( ) ) ) ,
192+ ColumnDoc :: new(
193+ "username" . to_string( ) ,
194+ Some ( "Username for login" . to_string( ) ) ,
195+ ) ,
196+ ColumnDoc :: new(
197+ "email" . to_string( ) ,
198+ Some ( "Email address" . to_string( ) ) ,
199+ ) ,
200+ ColumnDoc :: new(
201+ "created_at" . to_string( ) ,
202+ Some ( "When the user registered" . to_string( ) ) ,
203+ ) ,
204+ ] ,
205+ ) ,
206+ TableDoc :: new(
207+ "posts" . to_string( ) ,
208+ Some ( "Posts table stores blog posts" . to_string( ) ) ,
209+ vec![
210+ ColumnDoc :: new( "id" . to_string( ) , Some ( "Primary key" . to_string( ) ) ) ,
211+ ColumnDoc :: new(
212+ "title" . to_string( ) ,
213+ Some ( "Post title" . to_string( ) ) ,
214+ ) ,
215+ ColumnDoc :: new(
216+ "user_id" . to_string( ) ,
217+ Some ( "Foreign key linking to users" . to_string( ) ) ,
218+ ) ,
219+ ColumnDoc :: new(
220+ "body" . to_string( ) ,
221+ Some ( "Main body text" . to_string( ) ) ,
222+ ) ,
223+ ColumnDoc :: new(
224+ "published_at" . to_string( ) ,
225+ Some ( "When the post was created" . to_string( ) ) ,
226+ ) ,
227+ ] ,
228+ ) ,
229+ ] ) ;
230+
231+ assert_eq ! ( docs, expected) ;
196232 }
197233 "sql_files/with_multiline_comments.sql" => {
198- let expected = [
199- "Users table stores user account information \n multiline" ,
200- "Primary key \n multiline" ,
201- "Username for login \n multiline" ,
202- "Email address \n multiline" ,
203- "When the user registered \n multiline" ,
204- "Posts table stores blog posts \n multiline" ,
205- "Primary key \n multiline" ,
206- "Post title \n multiline" ,
207- "Foreign key linking to users \n multiline" ,
208- "Main body text \n multiline" ,
209- "When the post was created \n multiline" ,
210- ] ;
211- assert_eq ! ( expected. len( ) , comments. comments( ) . len( ) ) ;
212- comments
213- . comments ( )
214- . iter ( )
215- . enumerate ( )
216- . for_each ( |( i, c) | assert_eq ! ( expected[ i] , c. kind( ) . comment( ) ) ) ;
234+ let expected = SqlDocs :: new ( vec ! [
235+ TableDoc :: new(
236+ "users" . to_string( ) ,
237+ Some ( "Users table stores user account information \n multiline" . to_string( ) ) ,
238+ vec![
239+ ColumnDoc :: new(
240+ "id" . to_string( ) ,
241+ Some ( "Primary key \n multiline" . to_string( ) ) ,
242+ ) ,
243+ ColumnDoc :: new(
244+ "username" . to_string( ) ,
245+ Some ( "Username for login \n multiline" . to_string( ) ) ,
246+ ) ,
247+ ColumnDoc :: new(
248+ "email" . to_string( ) ,
249+ Some ( "Email address \n multiline" . to_string( ) ) ,
250+ ) ,
251+ ColumnDoc :: new(
252+ "created_at" . to_string( ) ,
253+ Some ( "When the user registered \n multiline" . to_string( ) ) ,
254+ ) ,
255+ ] ,
256+ ) ,
257+ TableDoc :: new(
258+ "posts" . to_string( ) ,
259+ Some ( "Posts table stores blog posts \n multiline" . to_string( ) ) ,
260+ vec![
261+ ColumnDoc :: new(
262+ "id" . to_string( ) ,
263+ Some ( "Primary key \n multiline" . to_string( ) ) ,
264+ ) ,
265+ ColumnDoc :: new(
266+ "title" . to_string( ) ,
267+ Some ( "Post title \n multiline" . to_string( ) ) ,
268+ ) ,
269+ ColumnDoc :: new(
270+ "user_id" . to_string( ) ,
271+ Some ( "Foreign key linking to users \n multiline" . to_string( ) ) ,
272+ ) ,
273+ ColumnDoc :: new(
274+ "body" . to_string( ) ,
275+ Some ( "Main body text \n multiline" . to_string( ) ) ,
276+ ) ,
277+ ColumnDoc :: new(
278+ "published_at" . to_string( ) ,
279+ Some ( "When the post was created \n multiline" . to_string( ) ) ,
280+ ) ,
281+ ] ,
282+ ) ,
283+ ] ) ;
284+
285+ assert_eq ! ( docs, expected) ;
217286 }
218287 "sql_files/with_mixed_comments.sql" => {
219- let expected = [
220- "Users table stores user account information" ,
221- "Primary key" ,
222- "Username for login" ,
223- "Email address" ,
224- "When the user registered" ,
225- "Posts table stores blog posts" ,
226- "Primary key" ,
227- "Post title" ,
228- "Foreign key linking to users" ,
229- "Main body text" ,
230- "When the post was created" ,
231- ] ;
232- assert_eq ! ( expected. len( ) , comments. comments( ) . len( ) ) ;
233- comments
234- . comments ( )
235- . iter ( )
236- . enumerate ( )
237- . for_each ( |( i, c) | assert_eq ! ( expected[ i] , c. kind( ) . comment( ) ) ) ;
288+ // Mixed should still produce the same final docs as the pure single-line version.
289+ let expected = SqlDocs :: new ( vec ! [
290+ TableDoc :: new(
291+ "users" . to_string( ) ,
292+ Some ( "Users table stores user account information" . to_string( ) ) ,
293+ vec![
294+ ColumnDoc :: new( "id" . to_string( ) , Some ( "Primary key" . to_string( ) ) ) ,
295+ ColumnDoc :: new(
296+ "username" . to_string( ) ,
297+ Some ( "Username for login" . to_string( ) ) ,
298+ ) ,
299+ ColumnDoc :: new(
300+ "email" . to_string( ) ,
301+ Some ( "Email address" . to_string( ) ) ,
302+ ) ,
303+ ColumnDoc :: new(
304+ "created_at" . to_string( ) ,
305+ Some ( "When the user registered" . to_string( ) ) ,
306+ ) ,
307+ ] ,
308+ ) ,
309+ TableDoc :: new(
310+ "posts" . to_string( ) ,
311+ Some ( "Posts table stores blog posts" . to_string( ) ) ,
312+ vec![
313+ ColumnDoc :: new( "id" . to_string( ) , Some ( "Primary key" . to_string( ) ) ) ,
314+ ColumnDoc :: new(
315+ "title" . to_string( ) ,
316+ Some ( "Post title" . to_string( ) ) ,
317+ ) ,
318+ ColumnDoc :: new(
319+ "user_id" . to_string( ) ,
320+ Some ( "Foreign key linking to users" . to_string( ) ) ,
321+ ) ,
322+ ColumnDoc :: new(
323+ "body" . to_string( ) ,
324+ Some ( "Main body text" . to_string( ) ) ,
325+ ) ,
326+ ColumnDoc :: new(
327+ "published_at" . to_string( ) ,
328+ Some ( "When the post was created" . to_string( ) ) ,
329+ ) ,
330+ ] ,
331+ ) ,
332+ ] ) ;
333+
334+ assert_eq ! ( docs, expected) ;
238335 }
239336 "sql_files/without_comments.sql" => {
240- assert_eq ! ( 0 , comments. comments( ) . len( ) ) ;
337+ let expected = SqlDocs :: new ( vec ! [
338+ TableDoc :: new(
339+ "users" . to_string( ) ,
340+ None ,
341+ vec![
342+ ColumnDoc :: new( "id" . to_string( ) , None ) ,
343+ ColumnDoc :: new( "username" . to_string( ) , None ) ,
344+ ColumnDoc :: new( "email" . to_string( ) , None ) ,
345+ ColumnDoc :: new( "created_at" . to_string( ) , None ) ,
346+ ] ,
347+ ) ,
348+ TableDoc :: new(
349+ "posts" . to_string( ) ,
350+ None ,
351+ vec![
352+ ColumnDoc :: new( "id" . to_string( ) , None ) ,
353+ ColumnDoc :: new( "title" . to_string( ) , None ) ,
354+ ColumnDoc :: new( "user_id" . to_string( ) , None ) ,
355+ ColumnDoc :: new( "body" . to_string( ) , None ) ,
356+ ColumnDoc :: new( "published_at" . to_string( ) , None ) ,
357+ ] ,
358+ ) ,
359+ ] ) ;
360+
361+ assert_eq ! ( docs, expected) ;
241362 }
242363 _ => {
243364 unreachable ! (
@@ -246,7 +367,7 @@ mod tests {
246367 )
247368 }
248369 }
249-
250370 }
251371 }
372+
252373}
0 commit comments