Skip to content

Commit 299fa78

Browse files
Merge pull request #208 from fsprojects/refactoring-datatable-constructors
Refactoring - getting rid of datatable constructors mess
2 parents 09f5eea + fbbdcbf commit 299fa78

File tree

3 files changed

+35
-41
lines changed

3 files changed

+35
-41
lines changed

src/SqlClient/DataTable.fs

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,10 @@ open FSharp.Data.SqlClient
88

99
[<Sealed>]
1010
[<CompilerMessageAttribute("This API supports the FSharp.Data.SqlClient infrastructure and is not intended to be used directly from your code.", 101, IsHidden = true)>]
11-
type DataTable<'T when 'T :> DataRow> private (tableName, knownSelectCommand, getDesignTimeConnection:Lazy<_>) =
12-
inherit DataTable(tableName)
11+
type DataTable<'T when 'T :> DataRow>(selectCommand: SqlCommand, ?connectionString: Lazy<string>) =
12+
inherit DataTable()
1313

1414
let rows = base.Rows
15-
let getSelectCommand maybeRuntimeConnection maybeRuntimeTransaction =
16-
let makeSelectCommand connection =
17-
let selectCommand = new SqlCommand("SELECT * FROM " + tableName)
18-
selectCommand.Connection <- connection
19-
selectCommand
20-
21-
if Option.isSome knownSelectCommand
22-
then
23-
knownSelectCommand.Value
24-
else
25-
match maybeRuntimeTransaction, maybeRuntimeConnection with
26-
| Some (tran:SqlTransaction), _ ->
27-
let command = makeSelectCommand tran.Connection
28-
command.Transaction <- tran
29-
command
30-
| None, Some connection ->
31-
makeSelectCommand connection
32-
| _ -> makeSelectCommand (getDesignTimeConnection.Value)
33-
34-
new (tableName, getDesignTimeConnection) = new DataTable<'T>(tableName, None, getDesignTimeConnection)
35-
new (createCommand) = new DataTable<'T>(null, Some createCommand, lazy(createCommand.Connection))
3615

3716
member __.Rows : IList<'T> = {
3817
new IList<'T> with
@@ -59,9 +38,18 @@ type DataTable<'T when 'T :> DataRow> private (tableName, knownSelectCommand, ge
5938

6039
member __.NewRow(): 'T = downcast base.NewRow()
6140

41+
member private this.IsDirectTable = this.TableName <> null
42+
6243
member this.Update(?connection, ?transaction, ?batchSize) =
6344

64-
let selectCommand = getSelectCommand connection transaction
45+
connection |> Option.iter selectCommand.set_Connection
46+
transaction |> Option.iter selectCommand.set_Transaction
47+
48+
if selectCommand.Connection = null && this.IsDirectTable
49+
then
50+
assert(connectionString.IsSome)
51+
selectCommand.Connection <- new SqlConnection( connectionString.Value.Value)
52+
6553
use dataAdapter = new SqlDataAdapter(selectCommand)
6654
use commandBuilder = new SqlCommandBuilder(dataAdapter)
6755
use __ = dataAdapter.RowUpdating.Subscribe(fun args ->
@@ -88,24 +76,29 @@ type DataTable<'T when 'T :> DataRow> private (tableName, knownSelectCommand, ge
8876
cmd.UpdatedRowSource <- UpdateRowSource.FirstReturnedRecord
8977
)
9078

91-
connection |> Option.iter dataAdapter.SelectCommand.set_Connection
92-
transaction |> Option.iter dataAdapter.SelectCommand.set_Transaction
9379
batchSize |> Option.iter dataAdapter.set_UpdateBatchSize
9480

9581
dataAdapter.Update(this)
9682

9783
member this.BulkCopy(?connection, ?copyOptions, ?transaction, ?batchSize, ?timeout: TimeSpan) =
9884

99-
let selectCommand = getSelectCommand connection transaction
100-
let connection = defaultArg connection selectCommand.Connection
101-
use __ = connection.UseLocally()
102-
use bulkCopy =
103-
new SqlBulkCopy(
104-
connection,
105-
copyOptions = defaultArg copyOptions SqlBulkCopyOptions.Default,
106-
externalTransaction = defaultArg transaction selectCommand.Transaction
107-
)
108-
bulkCopy.DestinationTableName <- tableName
85+
let conn', tran' =
86+
match connection, transaction with
87+
| _, Some(t: SqlTransaction) -> t.Connection, t
88+
| Some c, None -> c, null
89+
| None, None ->
90+
if this.IsDirectTable
91+
then
92+
assert(connectionString.IsSome)
93+
new SqlConnection(connectionString.Value.Value), null
94+
else
95+
selectCommand.Connection, selectCommand.Transaction
96+
97+
use __ = conn'.UseLocally()
98+
let options = defaultArg copyOptions SqlBulkCopyOptions.Default
99+
use bulkCopy = new SqlBulkCopy(conn', options, tran')
100+
bulkCopy.DestinationTableName <- this.TableName
109101
batchSize |> Option.iter bulkCopy.set_BatchSize
110102
timeout |> Option.iter (fun x -> bulkCopy.BulkCopyTimeout <- int x.TotalSeconds)
111-
bulkCopy.WriteToServer this
103+
bulkCopy.WriteToServer this
104+

src/SqlClient/SqlClient.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
<Compile Include="SqlCommandProvider.fs" />
8585
<Compile Include="SqlClientProvider.fs" />
8686
<Compile Include="SqlEnumProvider.fs" />
87-
<Compile Include="Scripts\Scratchpad.fsx" />
87+
<None Include="Scripts\Scratchpad.fsx" />
8888
<None Include="Scripts\XE.fsx" />
8989
<None Include="packages.config" />
9090
</ItemGroup>

src/SqlClient/SqlClientProvider.fs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,10 @@ type public SqlProgrammabilityProvider(config : TypeProviderConfig) as this =
370370

371371
<@@
372372

373-
let connection = lazy
374-
new SqlConnection( %%connectionString.RunTimeValueExpr(config.IsHostedExecution))
375-
let table = new DataTable<DataRow>(twoPartTableName, connection)
373+
let connectionString = lazy %%connectionString.RunTimeValueExpr(config.IsHostedExecution)
374+
let selectCommand = new SqlCommand("SELECT * FROM " + twoPartTableName)
375+
let table = new DataTable<DataRow>(selectCommand, connectionString)
376+
table.TableName <- twoPartTableName
376377

377378
let primaryKey = ResizeArray()
378379
for line in serializedSchema.Split('\n') do

0 commit comments

Comments
 (0)