forked from chrisoldwood/sql2doxygen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql2doxygen.ps1
More file actions
507 lines (410 loc) · 13 KB
/
sql2doxygen.ps1
File metadata and controls
507 lines (410 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
################################################################################
# \file sql2doxygen.ps1
# \brief Convert the SQL file into something Doxygen can handle.
# \author Chris Oldwood (gort@cix.co.uk | www.chrisoldwood.com)
# \version 0.2
#
# This is a Doxygen filter that takes a .SQL file (T-SQL) and transforms it into
# C-like code so that Doxygen can then parse it. The current conversion assumes
# that the Doxygen output will be optimised for C and not Java.
################################################################################
################################################################################
# Configure error handling.
set-strictmode -version Latest
$ErrorActionPreference = 'stop'
trap
{
write-error $_ -erroraction continue
exit 1
}
################################################################################
# Write a line of output terminated with a CR/LF. The built-in write-host
# cmdlet does not play nicely with stdout redirection (it only appends a CR).
# The write-output cmdlet also fails with redirection as text is wrapped at the
# width of the parent console window so we have to do it manually.
function write-line([string] $line)
{
write-host -nonewline ("{0}`r`n" -f $line)
}
################################################################################
# Validate the command line.
if ( ($args.count -ne 1) -or ($args[0] -eq '--help') )
{
if ($args[0] -eq '--help')
{
write-line "sql2doxygen v0.2"
write-line "(C) Chris Oldwood 2011 (gort@cix.co.uk)"
}
else
{
write-line "ERROR: Invalid command line"
}
write-line ""
write-line "USAGE: sql2doxygen.ps1 <file.sql>"
write-line ""
write-line "Doxygen configuration:-"
write-line "INPUT_FILTER = `"PowerShell.exe -File \Path\To\sql2doxygen.ps1`""
exit 1
}
################################################################################
# Regular expressions used to parse the code.
$namepart_pattern = '[\w\[\]]+' # [name]
$name_pattern = '[\w.\[\]]+' # [schema].[name]
$fullname_re = "(?<schema>$namepart_pattern).(?<name>$namepart_pattern)" # name|schema.name
$indent_re = '^(?<indent>\s*)' # Leading whitespace
$column_name_re = "(?<column_name>$name_pattern)" # Identifier
$type_name_re = "(?<type_name>[\w.\[\]()]+)" # Identifier
$parameter_name_re = "(?<parameter_name>@$name_pattern)" # @Identifier
$comment_re = '(?<comment>/\*.+\*/\s*$|--.+$|\s*$)' # /* ... */ or -- ... or none
$create_table_re = 'create\s+table' # create table
$table_name_re = "(?<table_name>$name_pattern)" # Identifier
$create_function_re = 'create\s+function' # create function
$function_name_re = "(?<function_name>$name_pattern)" # Identifier
$create_procedure_re = 'create\s+procedure' # create procedure
$procedure_name_re = "(?<procedure_name>$name_pattern)" # Identifier
$create_type_re = 'create\s+type' # create type
$alias_name_re = "(?<alias_name>$name_pattern)" # Identifier
################################################################################
# Detect a line consisting of nothing but whitespace.
function is-blank-line($line)
{
if ($line -match '^\s*$')
{
return $true
}
return $false
}
################################################################################
# Parse the schema name from the SQL style identifier.
function parse-schemaname($identifier)
{
$schema = 'dbo'
if ( ($identifier -match '\.') -and ($identifier -match $fullname_re) )
{
$schema = $matches.schema
}
$schema = transform-identifier $schema
return $schema
}
################################################################################
# Parse the object name from the SQL style identifier.
function parse-objectname($identifier)
{
$object = $identifier
if ( ($identifier -match '\.') -and ($identifier -match $fullname_re) )
{
$object = $matches.name
}
$object = transform-identifier $object
return $object
}
################################################################################
# Convert a single-line SQL-style comment into the C-style equivalent.
function transform-sql-comment($line)
{
$line = $line -replace '--!','//!'
$line = $line -replace '---','///'
$line = $line -replace '--','//'
return $line
}
################################################################################
# Transform the SQL identifier into one compatible with the C++ language.
function transform-identifier($identifier)
{
$identifier = $identifier -replace '\[|\]',''
$identifier = $identifier -replace '\.','::'
return $identifier
}
################################################################################
# Transform the SQL type name into one compatible with the C++ language. Types
# are similar to identifiers in format but you have parametersied variants, such
# as varchar(10).
function transform-type($type)
{
$type = transform-identifier $type
$type = $type -replace '\(','['
$type = $type -replace '\)',']'
return $type
}
################################################################################
# Handle C-style comments. These start with /* and end with */. The doxygen
# variants starts with /** or /*!.
function is-c-style-comment($line)
{
if ($line -match '^/\*[*!]')
{
return $true
}
return $false
}
function write-c-style-comment($enumerator)
{
$line = $enumerator.value.current
write-line $line
# Single comment?
if ($line -match '\*/')
{
return
}
# Multi-line comment.
while ($enumerator.value.movenext())
{
$line = $enumerator.value.current
write-line $line
if ($line -match '\*/')
{
return
}
}
}
################################################################################
# Handle SQL-style comments. These start with -- or for the doxygen variants --!
# or ---. These are transformed into the single-line // C-style comment.
function is-sql-style-comment($line)
{
if ($line -match '^--')
{
return $true
}
return $false
}
function write-sql-style-comment($line)
{
if ($line -match '^----+')
{
$line = $line -replace '-','/'
}
$line = transform-sql-comment $line
write-line $line
}
################################################################################
# Handle table definitions. This assumes that the "create table" keywords and
# the table name are all on a single line. The column definitions also must
# not span lines.
function is-table-definition($line)
{
if ($line -match "$indent_re$create_table_re")
{
return $true
}
return $false
}
function write-table-definition($enumerator)
{
if ($line -notmatch "$indent_re$create_table_re\s+$table_name_re")
{
return
}
$schema = parse-schemaname $matches.table_name
$name = parse-objectname $matches.table_name
$line = "struct $name"
write-line $line
while ($enumerator.value.movenext())
{
$line = $enumerator.value.current
# Handle column definitions
if ($line -match "$indent_re$column_name_re\s+$type_name_re[\s,\w]*$comment_re")
{
$indent = $matches.indent
$column = transform-identifier $matches.column_name
$type_schema = parse-schemaname $matches.type_name
$type_name = transform-type (parse-objectname $matches.type_name)
$comment = $matches.comment
$line = $indent + $type_name + ' ' + $column + '; ' + $comment
}
# Transform table body delimiters
$line = $line -replace '^\(', '{'
$line = $line -replace '^\);?', '};'
$line = transform-sql-comment $line
write-line $line
# End of definition?
if ($line -match '};')
{
return
}
}
}
################################################################################
# Handle the set of function/procedure parameters.
function write-parameters($enumerator)
{
$separator = '';
while ($enumerator.value.movenext())
{
$line = $enumerator.value.current
if ($line -match "$indent_re$parameter_name_re\s+$type_name_re[\s,]*$comment_re")
{
$indent = $matches.indent
$param = transform-identifier $matches.parameter_name
$type_schema = parse-schemaname $matches.type_name
$type_name = transform-type (parse-objectname $matches.type_name)
$comment = transform-sql-comment $matches.comment
$line = $indent + $separator + $type_name + ' ' + $param + ' ' + $comment
if ($separator -eq '')
{
$separator = ','
}
}
if ($line -match '^\)$')
{
return
}
write-output $line
}
}
################################################################################
# Handle user-defined function and stored procedure definitions.
function is-function-definition($line)
{
if ($line -match "$indent_re$create_function_re")
{
return $true
}
return $false
}
function is-procedure-definition($line)
{
if ($line -match "$indent_re$create_procedure_re")
{
return $true
}
return $false
}
$function = 'function'
$procedure = 'procedure'
function write-fn_or_proc-definition($enumerator, $fn_or_proc)
{
if ($fn_or_proc -eq $function)
{
if ($line -notmatch "$indent_re$create_function_re\s+$function_name_re")
{
return
}
$schema = parse-schemaname $matches.function_name
$name = parse-objectname $matches.function_name
$returnType = ''
# Return type specified with function name?
if ($line -match ".*returns\s+$type_name_re$")
{
$returnType = transform-type $matches.type_name
}
}
elseif ($fn_or_proc -eq $procedure)
{
if ($line -notmatch "$indent_re$create_procedure_re\s+$procedure_name_re")
{
return
}
$schema = parse-schemaname $matches.procedure_name
$name = parse-objectname $matches.procedure_name
$returnType = 'int'
}
$argsList = $null
while ($enumerator.value.movenext())
{
$line = $enumerator.value.current
# transform body delimiters
if ($fn_or_proc -eq $function)
{
$line = $line -replace '^begin', '{'
$line = $line -replace '^end;?', '}'
}
elseif ($fn_or_proc -eq $procedure)
{
$line = $line -replace '^as', '{'
$line = $line -replace '^go', '}'
}
# Write signature if start of body
if ($line -match '^{$')
{
if ($argsList -eq $null)
{
write-line ($returnType + ' ' + "$name" + '();')
}
else
{
write-line ($returnType + ' ' + "$name")
write-line '('
$argsList | foreach { write-line $_ }
write-line ');'
}
}
# Handle argument list
elseif ($line -match '^\($')
{
$argsList = write-parameters $enumerator
}
elseif ($line -match '^as$')
{
# Discard
}
# Return type specified after function name/arguments?
elseif ($line -match ".*returns\s+$type_name_re$")
{
$returnType = transform-type $matches.type_name
}
# End of definition?
if ($line -match '}')
{
return
}
}
}
################################################################################
# Handle user-defined type definitions.
function is-type-definition($line)
{
if ($line -match "$indent_re$create_type_re")
{
return $true
}
return $false
}
function write-type-definition($enumerator)
{
if ($line -notmatch "$indent_re$create_type_re\s+$alias_name_re\s+from\s+$type_name_re")
{
return
}
$alias_schema = parse-schemaname $matches.alias_name
$alias_name = transform-type (parse-objectname $matches.alias_name)
$type_schema = parse-schemaname $matches.type_name
$type_name = transform-type (parse-objectname $matches.type_name)
write-line "typedef $type_name $alias_name;"
}
################################################################################
# Main parsing loop.
$lines = get-content $args[0]
$enumerator = $lines.getenumerator()
while ($enumerator.movenext())
{
$line = $enumerator.current
if (is-blank-line $line -eq $true)
{
write-line $line
}
elseif (is-c-style-comment $line -eq $true)
{
write-c-style-comment $(get-variable -name enumerator)
}
elseif (is-sql-style-comment $line -eq $true)
{
write-sql-style-comment $line
}
elseif (is-table-definition $line -eq $true)
{
write-table-definition $(get-variable -name enumerator)
}
elseif (is-function-definition $line -eq $true)
{
write-fn_or_proc-definition $(get-variable -name enumerator) $function
}
elseif (is-procedure-definition $line -eq $true)
{
write-fn_or_proc-definition $(get-variable -name enumerator) $procedure
}
elseif (is-type-definition $line -eq $true)
{
write-type-definition $(get-variable -name enumerator)
}
}