Skip to content

Commit 0a210c0

Browse files
committed
code cleanup with fsharp-refactor tool
1 parent 37fc7ac commit 0a210c0

14 files changed

Lines changed: 105 additions & 108 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ module Insert =
8888
/// would otherwise take the previous row's value for it.
8989
let combine (inserts: Insert list) : InsertMany =
9090
match inserts with
91-
| [] -> failwith "Insert.combine: no rows"
91+
| [] -> failwith $"Insert.combine: no rows, calling combine with inserts: {inserts}"
9292
| first :: _ ->
9393
let columns = first.Assignments |> Array.map (fun a -> a.Column)
9494

@@ -104,7 +104,7 @@ module Insert =
104104
let theseColumns = i.Assignments |> Array.map (fun a -> a.Column)
105105

106106
if theseColumns.Length <> columns.Length then
107-
failwith "Insert.combine: rows set different numbers of columns"
107+
failwith $"Insert.combine: rows set different numbers of columns, calling combine with inserts: {inserts}"
108108

109109
Array.iteri
110110
(fun n (c: string) ->

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module Dialect =
7474

7575
match style with
7676
| Numbered -> out.Append('$').Append(ordered.Count) |> ignore
77-
| _ -> out.Append('?') |> ignore
77+
| Named | Positional -> out.Append('?') |> ignore
7878

7979
i <- last
8080
| None ->

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ module Db =
3737
/// `tryHead` when an empty result is an answer rather than a bug.
3838
let head (conn: ISqlConnector) (q: Query) : Async<SqlRow> =
3939
async {
40-
let! row = tryHead conn q
4140

42-
match row with
41+
match! tryHead conn q with
4342
| Some r -> return r
4443
| None -> return failwith "head: the query returned no rows"
4544
}
@@ -62,9 +61,8 @@ module Db =
6261
/// LINQ's `exactlyOne`.
6362
let exactlyOne (conn: ISqlConnector) (q: Query) : Async<SqlRow> =
6463
async {
65-
let! row = tryExactlyOne conn q
6664

67-
match row with
65+
match! tryExactlyOne conn q with
6866
| Some r -> return r
6967
| None -> return failwith "exactlyOne: the query returned no rows"
7068
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ module Col =
234234
let int64 table name : Column<int64> = make table name SqlInt
235235

236236
let int table name : Column<int> =
237-
make table name (fun v -> SqlInt(widen v))
237+
make table name (widen >> SqlInt)
238238

239239
let text table name : Column<string> = make table name SqlText
240240
let float table name : Column<float> = make table name SqlFloat
@@ -355,8 +355,7 @@ module Expr =
355355
// Indexed rather than `for ch in value`: a string has no enumerator on
356356
// Fable's Rust target (G27), and indexing is what the rest of this
357357
// library already uses to walk one.
358-
for i in 0 .. value.Length - 1 do
359-
let ch = value.[i]
358+
for ch in value do
360359

361360
if ch = '%' || ch = '_' || ch = likeEscapeChar then
362361
parts.Add(string likeEscapeChar)

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

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -76,54 +76,54 @@ module SqlGen =
7676
/// reordering. Returns None where the engine has no equivalent.
7777
let private fnName (vendor: Vendor) (fn: SqlFn) =
7878
match fn, vendor with
79-
| Upper, _ -> Some "UPPER"
80-
| Lower, _ -> Some "LOWER"
81-
| Trim, _ -> Some "TRIM"
82-
| Replace, _ -> Some "REPLACE"
83-
| Abs, _ -> Some "ABS"
84-
85-
| Length, Sqlite -> Some "LENGTH"
86-
| Length, Postgres -> Some "CHAR_LENGTH"
87-
| Length, MySql -> Some "CHAR_LENGTH"
88-
| Length, Generic -> Some "LENGTH"
89-
90-
| Substring, MySql -> Some "MID"
91-
| Substring, Sqlite -> Some "SUBSTR"
92-
| Substring, _ -> Some "SUBSTRING"
93-
94-
| IndexOf, Postgres -> Some "STRPOS"
95-
| IndexOf, MySql -> Some "LOCATE"
96-
| IndexOf, Sqlite -> Some "INSTR"
97-
| IndexOf, Generic -> Some "INSTR"
79+
| Upper, _ -> ValueSome "UPPER"
80+
| Lower, _ -> ValueSome "LOWER"
81+
| Trim, _ -> ValueSome "TRIM"
82+
| Replace, _ -> ValueSome "REPLACE"
83+
| Abs, _ -> ValueSome "ABS"
84+
85+
| Length, Sqlite -> ValueSome "LENGTH"
86+
| Length, Postgres -> ValueSome "CHAR_LENGTH"
87+
| Length, MySql -> ValueSome "CHAR_LENGTH"
88+
| Length, Generic -> ValueSome "LENGTH"
89+
90+
| Substring, MySql -> ValueSome "MID"
91+
| Substring, Sqlite -> ValueSome "SUBSTR"
92+
| Substring, _ -> ValueSome "SUBSTRING"
93+
94+
| IndexOf, Postgres -> ValueSome "STRPOS"
95+
| IndexOf, MySql -> ValueSome "LOCATE"
96+
| IndexOf, Sqlite -> ValueSome "INSTR"
97+
| IndexOf, Generic -> ValueSome "INSTR"
9898

9999
// Every engine's plain-rounding function has the same name.
100-
| Round, _ -> Some "ROUND"
100+
| Round, _ -> ValueSome "ROUND"
101101

102102
// The rest are not plain function calls on every engine, so they are
103103
// handled separately below and never reach this table.
104-
| (Year | Month | Day | Hour | Minute | Second | DateOnly), _ -> None
105-
| (Ceiling | Floor | RoundTo _ | Truncate | Greatest | Least), _ -> None
106-
| (CastText | CastInt | DateDiffDays | DateDiffSecs), _ -> None
104+
| (Year | Month | Day | Hour | Minute | Second | DateOnly), _ -> ValueNone
105+
| (Ceiling | Floor | RoundTo _ | Truncate | Greatest | Least), _ -> ValueNone
106+
| (CastText | CastInt | DateDiffDays | DateDiffSecs), _ -> ValueNone
107107

108108
/// SQLite has no YEAR(); it has STRFTIME with a format string. PostgreSQL
109109
/// wants EXTRACT(field FROM x). MySQL has the plain function.
110110
let private datePartName (fn: SqlFn) =
111111
match fn with
112-
| Year -> Some("YEAR", "%Y")
113-
| Month -> Some("MONTH", "%m")
114-
| Day -> Some("DAY", "%d")
115-
| Hour -> Some("HOUR", "%H")
116-
| Minute -> Some("MINUTE", "%M")
117-
| Second -> Some("SECOND", "%S")
118-
| _ -> None
112+
| Year -> ValueSome("YEAR", "%Y")
113+
| Month -> ValueSome("MONTH", "%m")
114+
| Day -> ValueSome("DAY", "%d")
115+
| Hour -> ValueSome("HOUR", "%H")
116+
| Minute -> ValueSome("MINUTE", "%M")
117+
| Second -> ValueSome("SECOND", "%S")
118+
| _ -> ValueNone
119119

120120
let rec private renderExpr (b: Builder) (e: SqlExpr) =
121121
let append (s: string) = write b s
122122

123123
match e with
124124
| ColumnRef(table, column) ->
125125
if b.Qualify then
126-
append (table + "." + column)
126+
append ($"{table}.{column}")
127127
else
128128
append column
129129

@@ -385,7 +385,7 @@ module SqlGen =
385385
match b.Vendor with
386386
| Sqlite
387387
| Generic -> append (sqliteName + "(")
388-
| _ -> append (name + "(")
388+
| Postgres | MySql -> append (name + "(")
389389

390390
renderExpr b first
391391
append ", "
@@ -491,29 +491,29 @@ module SqlGen =
491491
let append (s: string) = write b s
492492

493493
match datePartName fn with
494-
| Some(name, strftime) ->
494+
| ValueSome(name, strftime) ->
495495
let arg = oneArg fn args
496496

497497
(match b.Vendor with
498498
| Sqlite ->
499499
// STRFTIME returns text, so cast back to a number to keep
500500
// comparisons behaving like the other engines.
501-
append ("CAST(STRFTIME('" + strftime + "', ")
501+
append ($"CAST(STRFTIME('{strftime}', ")
502502
renderExpr b arg
503503
append ") AS INTEGER)"
504504
| Postgres ->
505-
append ("EXTRACT(" + name + " FROM ")
505+
append ($"EXTRACT({name} FROM ")
506506
renderExpr b arg
507507
append ")"
508508
| MySql
509509
| Generic ->
510510
append (name + "(")
511511
renderExpr b arg
512512
append ")")
513-
| None ->
513+
| ValueNone ->
514514
match fnName b.Vendor fn with
515-
| None -> failwith ("SqlGen: no SQL mapping for " + string fn)
516-
| Some name ->
515+
| ValueNone -> failwith ("SqlGen: no SQL mapping for " + string fn)
516+
| ValueSome name ->
517517
// PostgreSQL's STRPOS takes (haystack, needle) like .NET's
518518
// IndexOf; MySQL's LOCATE takes (needle, haystack).
519519
let args =
@@ -585,15 +585,15 @@ module SqlGen =
585585

586586
append "DATETIME("
587587
renderExpr b inner
588-
append (", '" + signed + " " + unitName + "s')")
588+
append ($", '{signed} {unitName}s')")
589589

590590
and private renderSource (b: Builder) (s: Source) =
591591
let append (t: string) = write b t
592592

593593
if s.Alias = s.Table then
594594
append s.Table
595595
else
596-
append (s.Table + " AS " + s.Alias)
596+
append ($"{s.Table} AS {s.Alias}")
597597

598598
/// Renders a query into an existing builder, so a subquery shares the outer
599599
/// statement's parameter list and stays numbered in the order the SQL
@@ -766,7 +766,7 @@ module SqlGen =
766766
// Columns are bare here, not qualified: an INSERT names exactly one
767767
// table and every engine rejects `INSERT INTO t (t.c) ..`.
768768
let b = newBuilder vendor false
769-
write b ("INSERT INTO " + i.Table + " (")
769+
write b ($"INSERT INTO {i.Table} (")
770770

771771
i.Assignments
772772
|> Array.iteri (fun n a ->
@@ -802,7 +802,7 @@ module SqlGen =
802802
failwith "SqlGen: the insert has no rows"
803803

804804
let b = newBuilder vendor false
805-
write b ("INSERT INTO " + i.Table + " (")
805+
write b ($"INSERT INTO {i.Table} (")
806806

807807
i.Columns
808808
|> Array.iteri (fun n c ->
@@ -840,7 +840,7 @@ module SqlGen =
840840
match vendor with
841841
| Postgres ->
842842
let sql, ps = renderInsert vendor i
843-
Some(sql + " RETURNING " + keyColumn, ps)
843+
Some($"{sql} RETURNING {keyColumn}", ps)
844844
| _ -> None
845845

846846
/// The query that reports the key the last insert on this connection
@@ -861,7 +861,7 @@ module SqlGen =
861861
failwith "SqlGen: the update has no WHERE and would rewrite every row -- say Update.all if that is meant"
862862

863863
let b = newBuilder vendor false
864-
write b ("UPDATE " + u.Table + " SET ")
864+
write b ($"UPDATE {u.Table} SET ")
865865
renderAssignments b u.Assignments
866866

867867
match u.Where with

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ module Convert =
121121
// range instead of overflowing at 19.
122122
let mutable mantissa = 0M
123123

124-
for i in 0 .. intPart.Length - 1 do
125-
mantissa <- mantissa * 10M + decimal (int intPart.[i] - int '0')
124+
for x in intPart do
125+
mantissa <- mantissa * 10M + decimal (int x - int '0')
126126

127-
for i in 0 .. fracPart.Length - 1 do
128-
mantissa <- mantissa * 10M + decimal (int fracPart.[i] - int '0')
127+
for x in fracPart do
128+
mantissa <- mantissa * 10M + decimal (int x - int '0')
129129

130130
let mutable scale = 1M
131131

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ module CodeGen =
6767
/// The identifier a name becomes in the generated source.
6868
let identifier (name: string) =
6969
if keywords.Contains name then
70-
"``" + name + "``"
70+
$"``{name}``"
7171
else
7272
// Anything that is not a plain identifier gets backticks too, which
7373
// covers spaces and punctuation without renaming the column.
@@ -76,7 +76,7 @@ module CodeGen =
7676
&& (System.Char.IsLetter name.[0] || name.[0] = '_')
7777
&& name |> Seq.forall (fun c -> System.Char.IsLetterOrDigit c || c = '_')
7878

79-
if isPlain then name else "``" + name + "``"
79+
if isPlain then name else $"``{name}``"
8080

8181
let private colFactory (kind: ColumnKind) =
8282
match kind with
@@ -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
@@ -265,7 +265,7 @@ module CodeGen =
265265
let condition =
266266
fk.Columns
267267
|> Array.map pairExpr
268-
|> Array.reduce (fun acc part -> "Expr.andAlso (" + acc + ") (" + part + ")")
268+
|> Array.reduce (fun acc part -> $"Expr.andAlso ({acc}) ({part})")
269269

270270
line (" /// " + (fk.Columns |> Array.map pairDoc |> String.concat ", "))
271271
line (" let " + identifier name + " = " + condition)

‎tests/Beam/Program.fs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ let private convertChecks () =
3030
Harness.isTrue "guid round-trips" (Convert.tryParseGuid "0f8fad5b-d9cb-469f-a165-70867728950e" = Some theId)
3131
)
3232

33-
results.Add(Harness.isTrue "junk decimal is rejected" (Convert.tryParseDecimal "1,5" = None))
34-
results.Add(Harness.isTrue "junk date is rejected" (Convert.tryParseDate "not a date" = None))
33+
results.Add(Harness.isTrue "junk decimal is rejected" ((Convert.tryParseDecimal "1,5") |> Option.isNone))
34+
results.Add(Harness.isTrue "junk date is rejected" ((Convert.tryParseDate "not a date") |> Option.isNone))
3535

3636
results.ToArray()
3737

@@ -51,7 +51,7 @@ let private rowChecks () =
5151
results.Add(Harness.check "text column" "Alfreds" (Row.text rows.[0] "Name"))
5252
results.Add(Harness.check "int column" "2" (string (Row.int rows.[1] "Id")))
5353
results.Add(Harness.check "case-insensitive lookup" "Alfreds" (Row.text rows.[0] "nAmE"))
54-
results.Add(Harness.isTrue "null maps to None" (Row.textOpt rows.[0] "Country" = None))
54+
results.Add(Harness.isTrue "null maps to None" ((Row.textOpt rows.[0] "Country") |> Option.isNone))
5555
results.Add(Harness.isTrue "non-null maps to Some" (Row.textOpt rows.[1] "Country" = Some "Sweden"))
5656
// An integer storage class in a float column has to widen, not fail.
5757
results.Add(Harness.isTrue "int widens to float" (Row.float rows.[1] "Balance" = 250.0))

‎tests/SQLProvider.Fable.SmokeTests/DialectTests.fs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let private render (sql: string, values: SqlValue[]) =
2020
| other -> SqlValue.typeName other)
2121
|> String.concat ","
2222

23-
sql + " | " + vs
23+
$"{sql} | {vs}"
2424

2525
let run () : TestResult[] =
2626
let results = ResizeArray<TestResult>()

‎tests/SQLProvider.Fable.SmokeTests/Harness.fs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ let check name expected actual =
2424
if expected = actual then
2525
pass name
2626
else
27-
fail name ("expected " + expected + ", got " + actual)
27+
fail name ($"expected {expected}, got {actual}")
2828

2929
/// Asserts a condition without stringifying it. `string true` is "True" on
3030
/// .NET and Rust but "true" on JavaScript, so a boolean must never reach
@@ -45,7 +45,7 @@ let report (results: TestResult[]) =
4545
if r.Passed then
4646
" PASS " + r.Name
4747
else
48-
" FAIL " + r.Name + " -- " + r.Detail)
48+
$" FAIL {r.Name} -- {r.Detail}")
4949

5050
let summary =
5151
" "

0 commit comments

Comments
 (0)