Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix query with lots of nullable columns creating stack overflow #10

Merged
merged 6 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/DbTests/DbTests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<None Include="SQL\TempTableWithLengthTypes.sql" />
<None Include="SQL\TempTableWithMaxLengthTypes.sql" />
<None Include="SQL\NominalResult.sql" />
<None Include="SQL\ManyColumns.sql" />
<None Include="facil.yaml" />
<Compile Include="Config.fs" />
<Compile Include="Utils.fs" />
Expand Down
13 changes: 13 additions & 0 deletions src/DbTests/ExecuteTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2807,6 +2807,19 @@ let execTests =
)
]

testList (nameof DbGen.Scripts.ManyColumns) [
yield!
allExecuteMethodsAsSingle<DbGen.Scripts.ManyColumns, _>
|> List.map (fun (name, exec) ->
testCase name <| fun () ->
let res =
DbGen.Scripts.ManyColumns
.WithConnection(Config.connStr)
|> exec
test <@ res.Value.Column1 = None @>
test <@ res.Value.Column600 = None @>
)
]

testList (nameof DbGen.Scripts.NormalParams) [
yield!
Expand Down
12 changes: 12 additions & 0 deletions src/DbTests/SQL/ManyColumns.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DECLARE @numCols INT = 600
DECLARE @sql NVARCHAR(MAX) = 'SELECT '

DECLARE @colNo INT = 1

WHILE (@colNo <= @numCols)
BEGIN
SET @sql += 'Column' + CAST(@colNo AS NVARCHAR) + ' = NULL' + CASE @colNo WHEN @numCols THEN '' ELSE ', ' END
SET @colNo += 1
END

EXEC sp_executesql @sql
26 changes: 18 additions & 8 deletions src/Facil.Generator/Render.fs
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,19 @@ let private renderProcOrScript (cfg: RuleSet) (tableDtos: TableDto list) (execut
""

$"let getItem (reader: SqlDataReader){getItemReturnTypeExpr} ="
yield! indent [
for c in cols do
if c.IsNullable then
$"let ``{c.Name.Value}`` = if reader.IsDBNull ``ordinal_{c.Name.Value}`` then {outOptionNone} else reader.{c.TypeInfo.SqlDataReaderGetMethodName} ``ordinal_{c.Name.Value}`` |> {outOptionSome}"
else
$"let ``{c.Name.Value}`` = reader.{c.TypeInfo.SqlDataReaderGetMethodName} ``ordinal_{c.Name.Value}``"
]

yield! indent [
getItemRecordStart
yield! indent [
for c in cols do
if c.IsNullable then
$"``{getColName c}`` = if reader.IsDBNull ``ordinal_{c.Name.Value}`` then {outOptionNone} else reader.{c.TypeInfo.SqlDataReaderGetMethodName} ``ordinal_{c.Name.Value}`` |> {outOptionSome}"
else
$"``{getColName c}`` = reader.{c.TypeInfo.SqlDataReaderGetMethodName} ``ordinal_{c.Name.Value}``"
$"``{getColName c}`` = ``{c.Name.Value}``"
]
getItemRecordEnd
]
Expand Down Expand Up @@ -484,14 +489,19 @@ let private renderProcOrScript (cfg: RuleSet) (tableDtos: TableDto list) (execut
""

$"let getItem (reader: SqlDataReader){getItemReturnTypeExpr} ="
yield! indent [
for c in cols do
if c.IsNullable then
$"let ``{c.Name.Value}`` = if reader.IsDBNull ``ordinal_{c.Name.Value}`` then {outOptionNone} else reader.{c.TypeInfo.SqlDataReaderGetMethodName} ``ordinal_{c.Name.Value}`` |> {outOptionSome}"
else
$"let ``{c.Name.Value}`` = reader.{c.TypeInfo.SqlDataReaderGetMethodName} ``ordinal_{c.Name.Value}``"
]

yield! indent [
getItemRecordStart
yield! indent [
for c in cols do
if c.IsNullable then
$"``{getColName c}`` = if reader.IsDBNull ``ordinal_{c.Name.Value}`` then {outOptionNone} else reader.{c.TypeInfo.SqlDataReaderGetMethodName} ``ordinal_{c.Name.Value}`` |> {outOptionSome}"
else
$"``{getColName c}`` = reader.{c.TypeInfo.SqlDataReaderGetMethodName} ``ordinal_{c.Name.Value}``"
$"``{getColName c}`` = ``{c.Name.Value}``"
]
getItemRecordEnd
]
Expand Down