Skip to content

Commit 5b391f3

Browse files
committed
fsharp-refactor code clean-up.
1 parent 94f3e47 commit 5b391f3

8 files changed

Lines changed: 46 additions & 43 deletions

File tree

‎src/SQLProvider.Fable.Core/Dialect.fs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ module Dialect =
6161
while i < sql.Length do
6262
if sql.[i] = '@' && i + 1 < sql.Length && isNameChar sql.[i + 1] then
6363
let start = i + 1
64-
let mutable last = start
64+
let rec advanceLast last =
65+
if last < sql.Length && isNameChar sql.[last] then advanceLast (last + 1) else last
6566

66-
while last < sql.Length && isNameChar sql.[last] do
67-
last <- last + 1
67+
let last = advanceLast start
6868

6969
let name = sql.Substring(start, last - start)
7070

‎src/SQLProvider.Fable.Core/Row.fs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ let tryOrdinal (rs: ResultSet) (name: string) =
4949

5050
let ordinal (rs: ResultSet) (name: string) =
5151
match tryOrdinal rs name with
52-
| -1 -> failwith ($"No column named '{name}' in result set")
52+
| -1 -> failwith $"No column named '{name}' in result set"
5353
| i -> i
5454

5555
/// Raw value of a named column.
@@ -71,7 +71,7 @@ let private wrongType (name: string) (expected: string) (v: SqlValue) : 'T =
7171
)
7272

7373
let private nullNotAllowed (name: string) (expected: string) : 'T =
74-
failwith ($"Column '{name}' is NULL; use the *Opt reader to allow it ({expected})")
74+
failwith $"Column '{name}' is NULL; use the *Opt reader to allow it ({expected})"
7575

7676
// --- int64 --------------------------------------------------------------
7777

@@ -153,7 +153,7 @@ let decimalOpt (r: SqlRow) (name: string) : decimal option =
153153
| SqlText t ->
154154
match Convert.tryParseDecimal t with
155155
| Some v -> Some v
156-
| None -> failwith ($"Column '{name}': '{t}' is not a decimal")
156+
| None -> failwith $"Column '{name}': '{t}' is not a decimal"
157157
| v -> wrongType name "decimal" v
158158

159159
let decimal (r: SqlRow) (name: string) : decimal =
@@ -169,7 +169,7 @@ let dateTimeOpt (r: SqlRow) (name: string) : System.DateTime option =
169169
| SqlText t ->
170170
match Convert.tryParseDate t with
171171
| Some v -> Some v
172-
| None -> failwith ($"Column '{name}': '{t}' is not an ISO-8601 date")
172+
| None -> failwith $"Column '{name}': '{t}' is not an ISO-8601 date"
173173
| v -> wrongType name "date" v
174174

175175
let dateTime (r: SqlRow) (name: string) : System.DateTime =
@@ -185,7 +185,7 @@ let guidOpt (r: SqlRow) (name: string) : System.Guid option =
185185
| SqlText t ->
186186
match Convert.tryParseGuid t with
187187
| Some v -> Some v
188-
| None -> failwith ($"Column '{name}': '{t}' is not a GUID")
188+
| None -> failwith $"Column '{name}': '{t}' is not a GUID"
189189
| v -> wrongType name "guid" v
190190

191191
let guid (r: SqlRow) (name: string) : System.Guid =

‎src/SQLProvider.Fable.Core/SqlGen.fs‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ module SqlGen =
128128
| [ first; second ] -> first, second
129129
| _ -> failwith ("SqlGen: " + string fn + " takes exactly two arguments")
130130

131+
let private renderSource (b: Builder) (s: Source) =
132+
let append (t: string) = write b t
133+
134+
if s.Alias = s.Table then
135+
append s.Table
136+
else
137+
append $"{s.Table} AS {s.Alias}"
138+
131139
/// `.Date`. SQLite and MySQL have DATE(); PostgreSQL's DATE() is not the
132140
/// same thing, so it uses the truncation SQLProvider uses too.
133141
@@ -139,7 +147,7 @@ module SqlGen =
139147
match e with
140148
| ColumnRef(table, column) ->
141149
if b.Qualify then
142-
append ($"{table}.{column}")
150+
append $"{table}.{column}"
143151
else
144152
append column
145153

@@ -500,11 +508,11 @@ module SqlGen =
500508
| Sqlite ->
501509
// STRFTIME returns text, so cast back to a number to keep
502510
// comparisons behaving like the other engines.
503-
append ($"CAST(STRFTIME('{strftime}', ")
511+
append $"CAST(STRFTIME('{strftime}', "
504512
renderExpr b arg
505513
append ") AS INTEGER)"
506514
| Postgres ->
507-
append ($"EXTRACT({name} FROM ")
515+
append $"EXTRACT({name} FROM "
508516
renderExpr b arg
509517
append ")"
510518
| MySql
@@ -587,17 +595,9 @@ module SqlGen =
587595

588596
append "DATETIME("
589597
renderExpr b inner
590-
append ($", '{signed} {unitName}s')")
598+
append $", '{signed} {unitName}s')"
591599

592-
and private renderSource (b: Builder) (s: Source) =
593-
let append (t: string) = write b t
594-
595-
if s.Alias = s.Table then
596-
append s.Table
597-
else
598-
append ($"{s.Table} AS {s.Alias}")
599-
600-
/// Renders a query into an existing builder, so a subquery shares the outer
600+
/// Renders a query into an existing builder, so a subquery shares the outer
601601
/// statement's parameter list and stays numbered in the order the SQL
602602
/// mentions it.
603603
/// One SELECT through its HAVING -- everything that belongs to a single
@@ -763,7 +763,7 @@ module SqlGen =
763763

764764
let renderInsert (vendor: Vendor) (i: Insert) : string * SqlParam[] =
765765
if i.Assignments.Length = 0 then
766-
failwith "SqlGen: the insert sets no columns"
766+
failwith $"SqlGen: the insert sets no columns, calling renderInsert with vendor: {vendor}"
767767

768768
// Columns are bare here, not qualified: an INSERT names exactly one
769769
// table and every engine rejects `INSERT INTO t (t.c) ..`.
@@ -798,10 +798,10 @@ module SqlGen =
798798
/// would stop the statement being atomic.
799799
let renderInsertMany (vendor: Vendor) (i: InsertMany) : string * SqlParam[] =
800800
if i.Columns.Length = 0 then
801-
failwith "SqlGen: the insert sets no columns"
801+
failwith $"SqlGen: the insert sets no columns, calling renderInsertMany with vendor: {vendor}"
802802

803803
if i.Rows.Length = 0 then
804-
failwith "SqlGen: the insert has no rows"
804+
failwith $"SqlGen: the insert has no rows, calling renderInsertMany with vendor: {vendor}"
805805

806806
let b = newBuilder vendor false
807807
write b ($"INSERT INTO {i.Table} (")
@@ -857,10 +857,10 @@ module SqlGen =
857857

858858
let renderUpdate (vendor: Vendor) (u: Update) : string * SqlParam[] =
859859
if u.Assignments.Length = 0 then
860-
failwith "SqlGen: the update sets no columns"
860+
failwith $"SqlGen: the update sets no columns, calling renderUpdate with vendor: {vendor}"
861861

862862
if u.Where.IsNone && not u.Unconditional then
863-
failwith "SqlGen: the update has no WHERE and would rewrite every row -- say Update.all if that is meant"
863+
failwith $"SqlGen: the update has no WHERE and would rewrite every row -- say Update.all if that is meant, calling renderUpdate with vendor: {vendor}"
864864

865865
let b = newBuilder vendor false
866866
write b ($"UPDATE {u.Table} SET ")
@@ -876,7 +876,7 @@ module SqlGen =
876876

877877
let renderDelete (vendor: Vendor) (d: Delete) : string * SqlParam[] =
878878
if d.Where.IsNone && not d.Unconditional then
879-
failwith "SqlGen: the delete has no WHERE and would empty the table -- say Delete.all if that is meant"
879+
failwith $"SqlGen: the delete has no WHERE and would empty the table -- say Delete.all if that is meant, calling renderDelete with vendor: {vendor}"
880880

881881
let b = newBuilder vendor false
882882
write b ("DELETE FROM " + d.Table)

‎src/SQLProvider.Fable.Core/SqlValue.fs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ module Convert =
224224

225225
// optional ".fffffff"
226226
if s.Length > 19 && s.[19] = '.' then
227-
let mutable last = 20
227+
let rec advanceLast last =
228+
if last < s.Length && isDigit s.[last] then advanceLast (last + 1) else last
228229

229-
while last < s.Length && isDigit s.[last] do
230-
last <- last + 1
230+
let last = advanceLast 20
231231

232232
let digits = s.Substring(20, last - 20)
233233

‎src/SQLProvider.Fable.Design/CodeGen.fs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ module CodeGen =
131131
line ("/// " + t.Name + (if t.IsView then " (view -- readable, not writable)" else ""))
132132
line ("module " + identifier t.Name + " =")
133133
line ""
134-
line ($" let table = \"{t.Name}\"")
134+
line $" let table = \"{t.Name}\""
135135
line ""
136136

137137
for c in t.Columns do

‎tests/SQLProvider.Fable.Tests.Net/CodeGenTests.fs‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ open Microsoft.Data.Sqlite
1111
open SQLProvider.Fable
1212
open SQLProvider.Fable.Ado
1313
open SQLProvider.Fable.Design
14+
open System.IO
1415

1516
let private northwindish =
1617
[ "CREATE TABLE Customer (
@@ -198,10 +199,10 @@ let ``an awkward column name is escaped rather than renamed`` () =
198199
/// Walks up from the test assembly to the repo root, so the golden file can be
199200
/// found without hard-coding a path.
200201
let private repoRoot () =
201-
let mutable dir = System.IO.DirectoryInfo(System.AppContext.BaseDirectory)
202+
let mutable dir = DirectoryInfo(System.AppContext.BaseDirectory)
202203

203204
while (not (isNull dir))
204-
&& not (System.IO.File.Exists(System.IO.Path.Combine(dir.FullName, "SQLProvider.Fable.slnx"))) do
205+
&& not (File.Exists(Path.Combine(dir.FullName, "SQLProvider.Fable.slnx"))) do
205206
dir <- dir.Parent
206207

207208
if isNull dir then
@@ -210,7 +211,7 @@ let private repoRoot () =
210211
dir.FullName
211212

212213
let private goldenPath () =
213-
System.IO.Path.Combine(repoRoot (), "tests", "SQLProvider.Fable.SmokeTests", "GeneratedSchema.fs")
214+
Path.Combine(repoRoot (), "tests", "SQLProvider.Fable.SmokeTests", "GeneratedSchema.fs")
214215

215216
/// Generates from the fixture and compares against the file checked in at
216217
/// `tests/SQLProvider.Fable.SmokeTests/GeneratedSchema.fs`.
@@ -233,11 +234,11 @@ let ``the generated file is up to date`` () =
233234
let path = goldenPath ()
234235

235236
if System.Environment.GetEnvironmentVariable "SQLPROVIDER_FABLE_REGENERATE" = "1" then
236-
System.IO.File.WriteAllText(path, source)
237+
File.WriteAllText(path, source)
237238

238239
let existing =
239-
if System.IO.File.Exists path then
240-
System.IO.File.ReadAllText(path).Replace("\r\n", "\n")
240+
if File.Exists path then
241+
File.ReadAllText(path).Replace("\r\n", "\n")
241242
else
242243
""
243244

‎tests/SQLProvider.Fable.Tests.Net/Tests.fs‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ open Microsoft.Data.Sqlite
55
open SQLProvider.Fable
66
open SQLProvider.Fable.Ado
77
open SQLProvider.Fable.SmokeTests
8+
open System.Globalization
89

910
let private assertAllPassed (results: Harness.TestResult[]) =
1011
let failures =
@@ -64,10 +65,10 @@ let ``shared smoke suite passes with numbered placeholders`` () =
6465
/// conversion, this fails and the en-GB run above would not have noticed.
6566
[<Fact>]
6667
let ``shared smoke suite passes under a comma-separator locale`` () =
67-
let original = System.Globalization.CultureInfo.CurrentCulture
68+
let original = CultureInfo.CurrentCulture
6869

6970
try
70-
System.Globalization.CultureInfo.CurrentCulture <- System.Globalization.CultureInfo "fi-FI"
71+
CultureInfo.CurrentCulture <- CultureInfo "fi-FI"
7172

7273
use connection = new SqliteConnection("Data Source=:memory:")
7374
connection.Open()
@@ -78,18 +79,18 @@ let ``shared smoke suite passes under a comma-separator locale`` () =
7879
|> Async.RunSynchronously
7980
|> assertAllPassed
8081
finally
81-
System.Globalization.CultureInfo.CurrentCulture <- original
82+
CultureInfo.CurrentCulture <- original
8283

8384
/// Column lookup must not depend on the locale either. Turkish lowers 'I' to a
8485
/// dotless 'ı', so a culture-sensitive fold stops "CustomerId" matching the
8586
/// "customerid" PostgreSQL hands back for an unquoted column -- which is the
8687
/// everyday case, not an exotic one, for anyone running under tr-TR.
8788
[<Fact>]
8889
let ``column lookup is case-insensitive under the Turkish locale`` () =
89-
let original = System.Globalization.CultureInfo.CurrentCulture
90+
let original = CultureInfo.CurrentCulture
9091

9192
try
92-
System.Globalization.CultureInfo.CurrentCulture <- System.Globalization.CultureInfo "tr-TR"
93+
CultureInfo.CurrentCulture <- CultureInfo "tr-TR"
9394

9495
let rs =
9596
{ Columns = [| "customerid" |]
@@ -101,4 +102,4 @@ let ``column lookup is case-insensitive under the Turkish locale`` () =
101102
| None -> failwith "the result set has a row"
102103
| Some row -> Assert.Equal(42L, Row.int64 row "CustomerId")
103104
finally
104-
System.Globalization.CultureInfo.CurrentCulture <- original
105+
CultureInfo.CurrentCulture <- original

‎tests/SQLProvider.Fable.Tests.Readme/ReadmeSamples.fs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module Order =
4646
let OrderId = Col.int64 table "OrderId"
4747
let CustomerId = Col.int64 table "CustomerId"
4848

49+
[<Struct>]
4950
type Row = { OrderId: int64 }
5051

5152
let qualified = [| "Orders_OrderId", OrderId.E |]

0 commit comments

Comments
 (0)