Skip to content

Commit c36ccac

Browse files
committed
add cohort temp table
1 parent d1c80e0 commit c36ccac

File tree

3 files changed

+98
-12
lines changed

3 files changed

+98
-12
lines changed

Rdmp.Core/DataExport/DataExtraction/Pipeline/Sources/ExecuteDatasetExtractionSource.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
using System;
88
using System.Collections.Generic;
99
using System.Data;
10+
using System.Data.Common;
1011
using System.Diagnostics;
1112
using System.Linq;
1213
using System.Threading.Tasks;
1314
using FAnsi;
15+
using FAnsi.Discovery;
1416
using FAnsi.Discovery.QuerySyntax;
1517
using Rdmp.Core.Curation.Data;
1618
using Rdmp.Core.DataExport.Data;
@@ -99,13 +101,18 @@ public class ExecuteDatasetExtractionSource : IPluginDataFlowSource<DataTable>,
99101

100102
private DbDataCommandDataFlowSource _hostedSource;
101103

104+
private IExternalCohortTable _externalCohortTable;
105+
private string _whereSQL;
106+
private DbConnection _con;
107+
private string _uuid;
102108
protected virtual void Initialize(ExtractDatasetCommand request)
103109
{
104110
Request = request;
105111

106112
if (request == ExtractDatasetCommand.EmptyCommand)
107113
return;
108-
114+
_externalCohortTable = request.ExtractableCohort.ExternalCohortTable;
115+
_whereSQL = request.ExtractableCohort.WhereSQL();
109116
_timeSpentValidating = new Stopwatch();
110117
_timeSpentCalculatingDISTINCT = new Stopwatch();
111118
_timeSpentBuckettingDates = new Stopwatch();
@@ -156,6 +163,32 @@ private void Initialize(ExtractGlobalsCommand request)
156163

157164
private RowPeeker _peeker = new();
158165

166+
private static Random random = new Random();
167+
168+
private static string RandomString(int length)
169+
{
170+
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
171+
return new string(Enumerable.Repeat(chars, length)
172+
.Select(s => s[random.Next(s.Length)]).ToArray());
173+
}
174+
175+
private void CreateCohortTempTable(DbConnection con)
176+
{
177+
var db = _externalCohortTable.Discover();
178+
_uuid = $"#{RandomString(24)}";
179+
var sql = $"""
180+
SELECT *
181+
INTO {_uuid}
182+
FROM(
183+
SELECT * FROM {_externalCohortTable.TableName}
184+
WHERE {_whereSQL}
185+
) as cohortTempTable
186+
187+
""";
188+
using var cmd = db.Server.GetCommand(sql, con);
189+
cmd.ExecuteNonQuery();
190+
}
191+
159192
public virtual DataTable GetChunk(IDataLoadEventListener listener, GracefulCancellationToken cancellationToken)
160193
{
161194
// we are in the Global Commands case, let's return an empty DataTable (not null)
@@ -185,15 +218,18 @@ public virtual DataTable GetChunk(IDataLoadEventListener listener, GracefulCance
185218

186219
if (_hostedSource == null)
187220
{
221+
_con = DatabaseCommandHelper.GetConnection(Request.GetDistinctLiveDatabaseServer().Builder);
222+
_con.Open();
223+
CreateCohortTempTable(_con);
188224
StartAudit(Request.QueryBuilder.SQL);
189225

190226
if (Request.DatasetBundle.DataSet.DisableExtraction)
191227
throw new Exception(
192228
$"Cannot extract {Request.DatasetBundle.DataSet} because DisableExtraction is set to true");
193-
var x = GetCommandSQL(listener);
229+
194230
_hostedSource = new DbDataCommandDataFlowSource(GetCommandSQL(listener),
195231
$"ExecuteDatasetExtraction {Request.DatasetBundle.DataSet}",
196-
Request.GetDistinctLiveDatabaseServer().Builder,
232+
_con,
197233
ExecutionTimeout)
198234
{
199235
// If we are running in batches then always allow empty extractions
@@ -451,7 +487,10 @@ private string GetCommandSQL(IDataLoadEventListener listener)
451487

452488
listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Information,
453489
$"/*Decided on extraction SQL:*/{Environment.NewLine}{sql}"));
454-
490+
if(_uuid is not null)
491+
{
492+
sql = sql.Replace(_externalCohortTable.TableName, _uuid);
493+
}
455494
return sql;
456495
}
457496

Rdmp.Core/DataLoad/Engine/Pipeline/Sources/DbDataCommandDataFlowSource.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ public DbDataCommandDataFlowSource(string sql, string taskBeingPerformed, DbConn
4949
BatchSize = 10000;
5050
}
5151

52+
public DbDataCommandDataFlowSource(string sql, string taskBeingPerformed, DbConnection con,
53+
int timeout)
54+
{
55+
Sql = sql;
56+
_taskBeingPerformed = taskBeingPerformed;
57+
_con = con;
58+
_timeout = timeout;
59+
60+
BatchSize = 10000;
61+
}
62+
5263
private int _numberOfColumns;
5364

5465
private bool _firstChunk = true;
@@ -59,8 +70,11 @@ public DataTable GetChunk(IDataLoadEventListener job, GracefulCancellationToken
5970
{
6071
if (_reader == null)
6172
{
62-
_con = DatabaseCommandHelper.GetConnection(_builder);
63-
_con.Open();
73+
_con = _con ==null?DatabaseCommandHelper.GetConnection(_builder):_con;
74+
if(_con != null && _con.State == ConnectionState.Closed)
75+
{
76+
_con.Open();
77+
}
6478

6579
job.OnNotify(this, new NotifyEventArgs(ProgressEventType.Information,
6680
$"Running SQL:{Environment.NewLine}{Sql}"));
@@ -209,11 +223,13 @@ private void CloseReader(IDataLoadEventListener listener)
209223
public DataTable TryGetPreview()
210224
{
211225
var chunk = new DataTable();
212-
using var con = DatabaseCommandHelper.GetConnection(_builder);
213-
con.Open();
214-
using var da = DatabaseCommandHelper.GetDataAdapter(DatabaseCommandHelper.GetCommand(Sql, con));
226+
_con = _con == null ? DatabaseCommandHelper.GetConnection(_builder) : _con;
227+
if (_con != null && _con.State == ConnectionState.Closed)
228+
{
229+
_con.Open();
230+
}
231+
using var da = DatabaseCommandHelper.GetDataAdapter(DatabaseCommandHelper.GetCommand(Sql, _con));
215232
var read = da.Fill(0, 100, chunk);
216-
217233
return read == 0 ? null : chunk;
218234
}
219235
}

Rdmp.Core/QueryBuilding/ExtractionQueryBuilder.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
using System;
88
using System.Collections.Generic;
9+
using System.Data.Common;
910
using System.Linq;
11+
using FAnsi.Discovery;
1012
using FAnsi.Discovery.QuerySyntax;
13+
using NPOI.SS.Formula.Functions;
1114
using Rdmp.Core.CohortCommitting;
1215
using Rdmp.Core.DataExport;
1316
using Rdmp.Core.DataExport.Data;
@@ -122,7 +125,11 @@ public QueryBuilder GetSQLCommandForFullExtractionSet(ExtractDatasetCommand requ
122125
var externalCohortTable =
123126
_repository.GetObjectByID<ExternalCohortTable>(request.ExtractableCohort.ExternalCohortTable_ID);
124127
//todo here is where we want to create a temp table
125-
CreateCohortTempTable(externalCohortTable);
128+
129+
//var db = externalCohortTable.Discover();
130+
//var con = db.Server.GetConnection();
131+
//con.Open();
132+
//var tempTable = CreateCohortTempTable(externalCohortTable, request.ExtractableCohort.WhereSQL(),con.db);
126133
if (request.ExtractableCohort != null)
127134
{
128135
//the JOIN with the cohort table:
@@ -143,7 +150,31 @@ public QueryBuilder GetSQLCommandForFullExtractionSet(ExtractDatasetCommand requ
143150
return queryBuilder;
144151
}
145152

146-
private void CreateCohortTempTable(ExternalCohortTable externalCohortTable) { }
153+
//private static Random random = new Random();
154+
155+
//private static string RandomString(int length)
156+
//{
157+
// const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
158+
// return new string(Enumerable.Repeat(chars, length)
159+
// .Select(s => s[random.Next(s.Length)]).ToArray());
160+
//}
161+
162+
//private string CreateCohortTempTable(ExternalCohortTable externalCohortTable, string whereSQL,DbConnection con, DiscoveredDatabase db)
163+
//{
164+
// var uuid = $"#{RandomString(24)}";
165+
// var sql = $"""
166+
// SELECT *
167+
// INTO {uuid}
168+
// FROM(
169+
// SELECT * FROM {externalCohortTable.TableName}
170+
// WHERE {whereSQL}
171+
// ) as cohortTempTable
172+
173+
// """;
174+
// using var cmd = db.Server.GetCommand(sql, con);
175+
// cmd.ExecuteNonQuery();
176+
// return uuid;
177+
//}
147178

148179
private void HandleBatching(ExtractDatasetCommand request, QueryBuilder queryBuilder,
149180
IQuerySyntaxHelper syntaxHelper)

0 commit comments

Comments
 (0)