Skip to content

Commit c708e57

Browse files
committed
Displaying query result name in query result tab page header
1 parent ec7e508 commit c708e57

18 files changed

+103
-215
lines changed

DataCommander.Providers/AsyncDataAdapter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,8 @@ private void Fill(AsyncDataAdapterCommand asyncDataAdapterCommand)
200200
{
201201
Assert.IsNotNull(asyncDataAdapterCommand);
202202

203-
var command = asyncDataAdapterCommand.Command;
204-
205203
Exception exception = null;
204+
var command = asyncDataAdapterCommand.Command;
206205

207206
try
208207
{
@@ -221,7 +220,11 @@ private void Fill(AsyncDataAdapterCommand asyncDataAdapterCommand)
221220
{
222221
var schemaTable = dataReader.GetSchemaTable();
223222
if (schemaTable != null)
223+
{
224224
Log.Trace("schemaTable:\r\n{0}", schemaTable.ToStringTableString());
225+
if (asyncDataAdapterCommand.Query != null)
226+
schemaTable.TableName = asyncDataAdapterCommand.Query.Results[tableIndex].Name;
227+
}
225228

226229
ReadTable(dataReader, schemaTable, tableIndex);
227230
}

DataCommander.Providers/AsyncDataAdapterCommand.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ internal sealed class AsyncDataAdapterCommand
66
{
77
public readonly int LineIndex;
88
public readonly IDbCommand Command;
9+
public readonly QueryConfiguration.Query Query;
910

10-
public AsyncDataAdapterCommand(int lineIndex, IDbCommand command)
11+
public AsyncDataAdapterCommand(int lineIndex, IDbCommand command, QueryConfiguration.Query query)
1112
{
1213
LineIndex = lineIndex;
1314
Command = command;
15+
Query = query;
1416
}
1517
}
1618
}

DataCommander.Providers/DataCommander.Providers.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@
311311
<Compile Include="ResultWriter\FieldTypeDictionary.cs" />
312312
<Compile Include="ResultWriter\HtmlEntity.cs" />
313313
<Compile Include="ResultWriter\MyDataObject.cs" />
314-
<Compile Include="ResultWriter\QueryConfiguration\Parameter.cs" />
315-
<Compile Include="ResultWriter\QueryConfiguration\Query.cs" />
316-
<Compile Include="ResultWriter\QueryConfiguration\Result.cs" />
314+
<Compile Include="QueryConfiguration\Parameter.cs" />
315+
<Compile Include="QueryConfiguration\Query.cs" />
316+
<Compile Include="QueryConfiguration\Result.cs" />
317317
<Compile Include="ResultWriter\SqlBulkCopyResultWriter.cs" />
318318
<Compile Include="ResultWriter\CopyResultWriter.cs" />
319319
<Compile Include="ResultWriter\DataGridViewResultWriter.cs" />

DataCommander.Providers/Query/QueryForm.cs

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
45
using System.ComponentModel;
56
using System.Data;
67
using System.Data.Common;
@@ -30,6 +31,8 @@
3031
using Foundation.Text;
3132
using Foundation.Threading;
3233
using Foundation.Windows.Forms;
34+
using Newtonsoft.Json;
35+
using Parameter = DataCommander.Providers.QueryConfiguration.Parameter;
3336
using Timer = System.Windows.Forms.Timer;
3437

3538
namespace DataCommander.Providers.Query
@@ -1542,23 +1545,23 @@ private void ExecuteQuery()
15421545
var statements = Provider.GetStatements(query);
15431546
Log.Write(LogLevel.Trace, "Query:\r\n{0}", query);
15441547
IEnumerable<AsyncDataAdapterCommand> commands;
1548+
QueryConfiguration.Query queryConfiguration = null;
15451549

15461550
if (statements.Count == 1)
15471551
{
1548-
_sqlStatement = new SqlParser(statements[0].CommandText);
1552+
GetQueryConfiguration(statements[0].CommandText, out queryConfiguration, out var commandText);
1553+
_sqlStatement = new SqlParser(commandText);
15491554
var command = _sqlStatement.CreateCommand(Provider, Connection, _commandType, _commandTimeout);
15501555
command.Transaction = _transaction;
1551-
commands = new[]
1552-
{
1553-
new AsyncDataAdapterCommand(0, command)
1554-
};
1556+
commands = new AsyncDataAdapterCommand(0, command, queryConfiguration).ItemToArray();
15551557
}
15561558
else
15571559
commands =
15581560
from statement in statements
15591561
select new AsyncDataAdapterCommand(
15601562
statement.LineIndex,
1561-
Connection.Connection.CreateCommand(new CreateCommandRequest(statement.CommandText, null, CommandType.Text, _commandTimeout, _transaction)));
1563+
Connection.Connection.CreateCommand(new CreateCommandRequest(statement.CommandText, null, CommandType.Text, _commandTimeout, _transaction)),
1564+
null);
15621565

15631566
int maxRecords;
15641567
IResultWriter resultWriter = null;
@@ -1568,7 +1571,7 @@ from statement in statements
15681571
case ResultWriterType.DataGrid:
15691572
case ResultWriterType.ListView:
15701573
maxRecords = int.MaxValue;
1571-
_dataSetResultWriter = new DataSetResultWriter(AddInfoMessage, this, _showSchemaTable);
1574+
_dataSetResultWriter = new DataSetResultWriter(AddInfoMessage, _showSchemaTable);
15721575
resultWriter = _dataSetResultWriter;
15731576
break;
15741577

@@ -1579,13 +1582,13 @@ from statement in statements
15791582

15801583
case ResultWriterType.Html:
15811584
maxRecords = _htmlMaxRecords;
1582-
_dataSetResultWriter = new DataSetResultWriter(AddInfoMessage, this, _showSchemaTable);
1585+
_dataSetResultWriter = new DataSetResultWriter(AddInfoMessage, _showSchemaTable);
15831586
resultWriter = _dataSetResultWriter;
15841587
break;
15851588

15861589
case ResultWriterType.Rtf:
15871590
maxRecords = _wordMaxRecords;
1588-
_dataSetResultWriter = new DataSetResultWriter(AddInfoMessage, this, _showSchemaTable);
1591+
_dataSetResultWriter = new DataSetResultWriter(AddInfoMessage, _showSchemaTable);
15891592
resultWriter = _dataSetResultWriter;
15901593
break;
15911594

@@ -1658,6 +1661,37 @@ from statement in statements
16581661
}
16591662
}
16601663

1664+
private static void GetQueryConfiguration(string commandText, out QueryConfiguration.Query query, out string parameterizedCommandText)
1665+
{
1666+
if (commandText.StartsWith("/* Query"))
1667+
{
1668+
var index = commandText.IndexOf("*/");
1669+
var json = commandText.Substring(10, index - 10);
1670+
query = JsonConvert.DeserializeObject<QueryConfiguration.Query>(json);
1671+
commandText = commandText.Substring(index + 4);
1672+
1673+
var stringBuilder = new StringBuilder();
1674+
stringBuilder.Append(ToCommandText(query.Parameters));
1675+
stringBuilder.Append("\r\n");
1676+
stringBuilder.Append(commandText);
1677+
1678+
parameterizedCommandText = stringBuilder.ToString();
1679+
}
1680+
else
1681+
{
1682+
query = null;
1683+
parameterizedCommandText = commandText;
1684+
}
1685+
}
1686+
1687+
private static string ToCommandText(ReadOnlyCollection<Parameter> parameters)
1688+
{
1689+
var stringBuilder = new StringBuilder();
1690+
foreach (var parameter in parameters)
1691+
stringBuilder.Append($"declare @{parameter.Name} {parameter.DataType}{parameter.Value}\r\n");
1692+
return stringBuilder.ToString();
1693+
}
1694+
16611695
private void ShowDataTableText(DataTable dataTable)
16621696
{
16631697
var textBox = new RichTextBox();
@@ -3661,12 +3695,12 @@ public void OpenTable(string query)
36613695
_stopwatch.Start();
36623696
_timer.Start();
36633697
const int maxRecords = int.MaxValue;
3664-
_dataSetResultWriter = new DataSetResultWriter(AddInfoMessage, this, _showSchemaTable);
3698+
_dataSetResultWriter = new DataSetResultWriter(AddInfoMessage, _showSchemaTable);
36653699
IResultWriter resultWriter = _dataSetResultWriter;
36663700
_dataAdapter = new AsyncDataAdapter(Provider,
36673701
new[]
36683702
{
3669-
new AsyncDataAdapterCommand(0,_command)
3703+
new AsyncDataAdapterCommand(0, _command, null)
36703704
},
36713705
maxRecords, _rowBlockSize, resultWriter, EndFillInvoker, WriteEndInvoker);
36723706
_dataAdapter.Start();
@@ -3689,11 +3723,9 @@ private void createSqlCeDatabaseToolStripMenuItem_Click(object sender, EventArgs
36893723
var maxRecords = int.MaxValue;
36903724
var tableName = sqlStatement.FindTableName();
36913725
var sqlCeResultWriter = new SqlCeResultWriter(_textBoxWriter, tableName);
3692-
IAsyncDataAdapter asyncDataAdatper = new AsyncDataAdapter(Provider,
3693-
new[]
3694-
{
3695-
new AsyncDataAdapterCommand(0,_command)
3696-
},
3726+
IAsyncDataAdapter asyncDataAdatper = new AsyncDataAdapter(
3727+
Provider,
3728+
new AsyncDataAdapterCommand(0, _command, null).ItemToArray(),
36973729
maxRecords, _rowBlockSize, sqlCeResultWriter, EndFillInvoker, WriteEndInvoker);
36983730
asyncDataAdatper.Start();
36993731
}
@@ -3881,7 +3913,7 @@ internal void CopyTable()
38813913
_dataAdapter = new AsyncDataAdapter(Provider,
38823914
new[]
38833915
{
3884-
new AsyncDataAdapterCommand(0,_command)
3916+
new AsyncDataAdapterCommand(0, _command, null)
38853917
},
38863918
maxRecords, rowBlockSize, resultWriter, EndFillInvoker, WriteEndInvoker);
38873919
_dataAdapter.Start();

DataCommander.Providers/ResultWriter/QueryConfiguration/Parameter.cs renamed to DataCommander.Providers/QueryConfiguration/Parameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Data;
22

3-
namespace DataCommander.Providers.ResultWriter.QueryConfiguration
3+
namespace DataCommander.Providers.QueryConfiguration
44
{
55
public class Parameter
66
{

DataCommander.Providers/ResultWriter/QueryConfiguration/Query.cs renamed to DataCommander.Providers/QueryConfiguration/Query.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.ObjectModel;
22

3-
namespace DataCommander.Providers.ResultWriter.QueryConfiguration
3+
namespace DataCommander.Providers.QueryConfiguration
44
{
55
public class Query
66
{

DataCommander.Providers/ResultWriter/QueryConfiguration/Result.cs renamed to DataCommander.Providers/QueryConfiguration/Result.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace DataCommander.Providers.ResultWriter.QueryConfiguration
1+
namespace DataCommander.Providers.QueryConfiguration
22
{
33
public class Result
44
{

DataCommander.Providers/ResultWriter/CopyResultWriter.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,9 @@ void IResultWriter.Begin(IProvider provider)
6464
_logResultWriter.Begin(provider);
6565
}
6666

67-
void IResultWriter.BeforeExecuteReader(AsyncDataAdapterCommand command)
68-
{
69-
_logResultWriter.BeforeExecuteReader(command);
70-
}
71-
72-
void IResultWriter.AfterExecuteReader(int fieldCount)
73-
{
74-
_logResultWriter.AfterExecuteReader(fieldCount);
75-
}
76-
77-
void IResultWriter.AfterCloseReader(int affectedRows)
78-
{
79-
_logResultWriter.AfterCloseReader(affectedRows);
80-
}
67+
void IResultWriter.BeforeExecuteReader(AsyncDataAdapterCommand command) => _logResultWriter.BeforeExecuteReader(command);
68+
void IResultWriter.AfterExecuteReader(int fieldCount) => _logResultWriter.AfterExecuteReader(fieldCount);
69+
void IResultWriter.AfterCloseReader(int affectedRows) => _logResultWriter.AfterCloseReader(affectedRows);
8170

8271
void IResultWriter.WriteTableBegin(DataTable schemaTable)
8372
{

DataCommander.Providers/ResultWriter/DataSetResultWriter.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using DataCommander.Providers.Query;
2-
using System;
1+
using System;
32
using System.Data;
43
using DataCommander.Providers.Connection;
54
using Foundation.Data;
@@ -8,29 +7,21 @@
87

98
namespace DataCommander.Providers.ResultWriter
109
{
11-
/// <summary>
12-
/// Summary description for DataSetResultWriter.
13-
/// </summary>
1410
internal sealed class DataSetResultWriter : IResultWriter
1511
{
1612
#region Private Fields
1713

1814
private readonly IResultWriter _logResultWriter;
19-
private QueryForm _queryForm;
2015
private readonly bool _showShemaTable;
2116
private IProvider _provider;
2217
private DataTable _dataTable;
2318
private int _rowIndex;
2419

2520
#endregion
2621

27-
public DataSetResultWriter(
28-
Action<InfoMessage> addInfoMessage,
29-
QueryForm queryForm,
30-
bool showShemaTable)
22+
public DataSetResultWriter(Action<InfoMessage> addInfoMessage, bool showShemaTable)
3123
{
3224
_logResultWriter = new LogResultWriter(addInfoMessage);
33-
_queryForm = queryForm;
3425
_showShemaTable = showShemaTable;
3526
}
3627

DataCommander.Providers/ResultWriter/ExcelResultWriter.cs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Data;
43
using System.Diagnostics;
54
using System.IO;
@@ -37,10 +36,7 @@ void IResultWriter.Begin(IProvider provider)
3736
_provider = provider;
3837
}
3938

40-
void IResultWriter.BeforeExecuteReader(AsyncDataAdapterCommand command)
41-
{
42-
_logResultWriter.BeforeExecuteReader(command);
43-
}
39+
void IResultWriter.BeforeExecuteReader(AsyncDataAdapterCommand command) => _logResultWriter.BeforeExecuteReader(command);
4440

4541
void IResultWriter.AfterExecuteReader(int fieldCount)
4642
{
@@ -50,26 +46,16 @@ void IResultWriter.AfterExecuteReader(int fieldCount)
5046
_excelPackage = new ExcelPackage(new FileInfo(fileName));
5147
}
5248

53-
void IResultWriter.AfterCloseReader(int affectedRows)
54-
{
55-
_logResultWriter.AfterCloseReader(affectedRows);
56-
}
49+
void IResultWriter.AfterCloseReader(int affectedRows) => _logResultWriter.AfterCloseReader(affectedRows);
5750

5851
void IResultWriter.WriteTableBegin(DataTable schemaTable)
5952
{
6053
_logResultWriter.WriteTableBegin(schemaTable);
6154
CreateTable(schemaTable);
6255
}
6356

64-
void IResultWriter.FirstRowReadBegin()
65-
{
66-
_logResultWriter.FirstRowReadBegin();
67-
}
68-
69-
void IResultWriter.FirstRowReadEnd(string[] dataTypeNames)
70-
{
71-
_logResultWriter.FirstRowReadEnd(dataTypeNames);
72-
}
57+
void IResultWriter.FirstRowReadBegin() => _logResultWriter.FirstRowReadBegin();
58+
void IResultWriter.FirstRowReadEnd(string[] dataTypeNames) => _logResultWriter.FirstRowReadEnd(dataTypeNames);
7359

7460
void IResultWriter.WriteRows(object[][] rows, int rowCount)
7561
{
@@ -90,10 +76,7 @@ void IResultWriter.WriteRows(object[][] rows, int rowCount)
9076
_rowCount += rowCount;
9177
}
9278

93-
void IResultWriter.WriteTableEnd()
94-
{
95-
_logResultWriter.WriteTableEnd();
96-
}
79+
void IResultWriter.WriteTableEnd() => _logResultWriter.WriteTableEnd();
9780

9881
void IResultWriter.WriteParameters(IDataParameterCollection parameters)
9982
{

DataCommander.Providers/ResultWriter/IResultWriter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
using System.Collections.Generic;
2-
using DataCommander.Providers.Connection;
1+
using System.Data;
32

43
namespace DataCommander.Providers.ResultWriter
54
{
6-
using System.Data;
7-
85
internal interface IResultWriter
96
{
107
void Begin(IProvider provider);

DataCommander.Providers/ResultWriter/InsertScriptFileWriter.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Data;
43
using System.Data.Common;
54
using System.IO;
65
using System.Text;
7-
using DataCommander.Providers.Connection;
86
using DataCommander.Providers.FieldNamespace;
97
using DataCommander.Providers.Query;
108
using Foundation.Assertions;

0 commit comments

Comments
 (0)