Skip to content

Commit 93aca10

Browse files
committed
improved query history form
1 parent 71f5a05 commit 93aca10

6 files changed

Lines changed: 466 additions & 256 deletions

File tree

AxialSqlTools/AxialSqlTools.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,14 @@
9898
<DesignTime>True</DesignTime>
9999
<DependentUpon>Resources.resx</DependentUpon>
100100
</Compile>
101+
<Compile Include="QueryHistory\QueryHistoryRecord.cs" />
102+
<Compile Include="QueryHistory\QueryHistoryViewModel.cs" />
101103
<Compile Include="QueryHistory\QueryHistoryWindow.cs" />
102104
<Compile Include="QueryHistory\QueryHistoryWindowCommand.cs" />
103105
<Compile Include="QueryHistory\QueryHistoryWindowControl.xaml.cs">
104106
<DependentUpon>QueryHistoryWindowControl.xaml</DependentUpon>
105107
</Compile>
108+
<Compile Include="QueryHistory\RelayCommand.cs" />
106109
<Compile Include="SqlServerBuilds\SqlServerBuildsWindow.cs" />
107110
<Compile Include="SqlServerBuilds\SqlServerBuildsWindowCommand.cs" />
108111
<Compile Include="SqlServerBuilds\SqlServerBuildsWindowControl.xaml.cs">
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace AxialSqlTools
4+
{
5+
public class QueryHistoryRecord
6+
{
7+
public int Id { get; set; }
8+
public DateTime Date { get; set; }
9+
public DateTime FinishTime { get; set; }
10+
public string ElapsedTime { get; set; }
11+
public long TotalRowsReturned { get; set; }
12+
public string ExecResult { get; set; }
13+
public string QueryText { get; set; }
14+
public string QueryTextShort { get; set; }
15+
public string DataSource { get; set; }
16+
public string DatabaseName { get; set; }
17+
public string LoginName { get; set; }
18+
public string WorkstationId { get; set; }
19+
}
20+
}
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
using Microsoft.Data.SqlClient; // Make sure you have Microsoft.Data.SqlClient
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
5+
using System.ComponentModel;
6+
using System.Data;
7+
using System.Windows;
8+
using System.Windows.Input;
9+
using static AxialSqlTools.QueryHistoryWindowControl;
10+
11+
namespace AxialSqlTools
12+
{
13+
public class QueryHistoryViewModel : INotifyPropertyChanged
14+
{
15+
// Underlying full list (before filtering)
16+
private List<QueryHistoryRecord> _allRecords;
17+
18+
// ObservableCollection bound to DataGrid
19+
public ObservableCollection<QueryHistoryRecord> QueryHistoryRecords { get; }
20+
21+
private QueryHistoryRecord _selectedRecord;
22+
public QueryHistoryRecord SelectedRecord
23+
{
24+
get => _selectedRecord;
25+
set
26+
{
27+
if (_selectedRecord != value)
28+
{
29+
_selectedRecord = value;
30+
OnPropertyChanged(nameof(SelectedRecord));
31+
}
32+
}
33+
}
34+
35+
// ========== FILTER PROPERTIES ==========
36+
private DateTime? _filterFromDate;
37+
public DateTime? FilterFromDate
38+
{
39+
get => _filterFromDate;
40+
set
41+
{
42+
if (_filterFromDate != value)
43+
{
44+
_filterFromDate = value;
45+
OnPropertyChanged(nameof(FilterFromDate));
46+
}
47+
}
48+
}
49+
50+
private DateTime? _filterToDate;
51+
public DateTime? FilterToDate
52+
{
53+
get => _filterToDate;
54+
set
55+
{
56+
if (_filterToDate != value)
57+
{
58+
_filterToDate = value;
59+
OnPropertyChanged(nameof(FilterToDate));
60+
}
61+
}
62+
}
63+
64+
private string _filterServer;
65+
public string FilterServer
66+
{
67+
get => _filterServer;
68+
set
69+
{
70+
if (_filterServer != value)
71+
{
72+
_filterServer = value;
73+
OnPropertyChanged(nameof(FilterServer));
74+
}
75+
}
76+
}
77+
78+
private string _filterDatabase;
79+
public string FilterDatabase
80+
{
81+
get => _filterDatabase;
82+
set
83+
{
84+
if (_filterDatabase != value)
85+
{
86+
_filterDatabase = value;
87+
OnPropertyChanged(nameof(FilterDatabase));
88+
}
89+
}
90+
}
91+
92+
private string _filterQueryText;
93+
public string FilterQueryText
94+
{
95+
get => _filterQueryText;
96+
set
97+
{
98+
if (_filterQueryText != value)
99+
{
100+
_filterQueryText = value;
101+
OnPropertyChanged(nameof(FilterQueryText));
102+
}
103+
}
104+
}
105+
106+
// Commands
107+
public ICommand RefreshCommand { get; }
108+
public ICommand ClearFilterCommand { get; }
109+
110+
public QueryHistoryViewModel()
111+
{
112+
QueryHistoryRecords = new ObservableCollection<QueryHistoryRecord>();
113+
RefreshCommand = new RelayCommand(RefreshData);
114+
ClearFilterCommand = new RelayCommand(ClearAllFilters);
115+
116+
// Optionally, initialize filters to today’s date range or leave null
117+
// FilterFromDate = DateTime.Today.AddDays(-7);
118+
// FilterToDate = DateTime.Today;
119+
120+
RefreshData();
121+
}
122+
123+
private void ClearAllFilters()
124+
{
125+
FilterFromDate = null;
126+
FilterToDate = null;
127+
FilterServer = string.Empty;
128+
FilterDatabase = string.Empty;
129+
FilterQueryText = string.Empty;
130+
RefreshData();
131+
}
132+
133+
private void RefreshData()
134+
{
135+
_allRecords = new List<QueryHistoryRecord>();
136+
137+
// Retrieve connection and table settings (update as needed).
138+
string connectionString = SettingsManager.GetQueryHistoryConnectionString();
139+
string qhTableName = SettingsManager.GetQueryHistoryTableNameOrDefault();
140+
141+
// Base SELECT
142+
string sql = $@"
143+
SELECT TOP 1000
144+
[QueryID],
145+
[StartTime],
146+
[FinishTime],
147+
[ElapsedTime],
148+
[TotalRowsReturned],
149+
[ExecResult],
150+
[QueryText],
151+
[DataSource],
152+
[DatabaseName],
153+
[LoginName],
154+
[WorkstationId]
155+
FROM {qhTableName} ";
156+
157+
// Build WHERE clauses dynamically
158+
var whereClauses = new List<string>();
159+
var parameters = new List<SqlParameter>();
160+
161+
if (FilterFromDate.HasValue)
162+
{
163+
whereClauses.Add("[StartTime] >= @FromDate");
164+
parameters.Add(new SqlParameter("@FromDate", SqlDbType.DateTime) { Value = FilterFromDate.Value.Date });
165+
}
166+
if (FilterToDate.HasValue)
167+
{
168+
// Include the entire day for the “To” date (set time to 23:59:59)
169+
DateTime endOfDay = FilterToDate.Value.Date.AddDays(1).AddSeconds(-1);
170+
whereClauses.Add("[StartTime] <= @ToDate");
171+
parameters.Add(new SqlParameter("@ToDate", SqlDbType.DateTime) { Value = endOfDay });
172+
}
173+
if (!string.IsNullOrWhiteSpace(FilterServer))
174+
{
175+
whereClauses.Add("[DataSource] LIKE @Server");
176+
parameters.Add(new SqlParameter("@Server", SqlDbType.NVarChar, 256) { Value = "%" + FilterServer + "%" });
177+
}
178+
if (!string.IsNullOrWhiteSpace(FilterDatabase))
179+
{
180+
whereClauses.Add("[DatabaseName] LIKE @Database");
181+
parameters.Add(new SqlParameter("@Database", SqlDbType.NVarChar, 256) { Value = "%" + FilterDatabase + "%" });
182+
}
183+
if (!string.IsNullOrWhiteSpace(FilterQueryText))
184+
{
185+
whereClauses.Add("[QueryText] LIKE @QueryText");
186+
parameters.Add(new SqlParameter("@QueryText", SqlDbType.NVarChar) { Value = "%" + FilterQueryText + "%" });
187+
}
188+
189+
if (whereClauses.Count > 0)
190+
{
191+
sql += "WHERE " + string.Join(" AND ", whereClauses) + " ";
192+
}
193+
194+
sql += "ORDER BY [QueryID] DESC;";
195+
196+
try
197+
{
198+
using (SqlConnection conn = new SqlConnection(connectionString))
199+
{
200+
conn.Open();
201+
using (SqlCommand cmd = new SqlCommand(sql, conn))
202+
{
203+
foreach (var p in parameters)
204+
cmd.Parameters.Add(p);
205+
206+
using (SqlDataReader reader = cmd.ExecuteReader())
207+
{
208+
while (reader.Read())
209+
{
210+
var record = new QueryHistoryRecord
211+
{
212+
Id = reader.GetInt32(reader.GetOrdinal("QueryID")),
213+
Date = reader.GetDateTime(reader.GetOrdinal("StartTime")),
214+
FinishTime = reader.GetDateTime(reader.GetOrdinal("FinishTime")),
215+
ElapsedTime = reader.GetString(reader.GetOrdinal("ElapsedTime")),
216+
TotalRowsReturned = reader.GetInt64(reader.GetOrdinal("TotalRowsReturned")),
217+
ExecResult = reader.GetString(reader.GetOrdinal("ExecResult")),
218+
QueryText = reader.GetString(reader.GetOrdinal("QueryText")),
219+
DataSource = reader.GetString(reader.GetOrdinal("DataSource")),
220+
DatabaseName = reader.GetString(reader.GetOrdinal("DatabaseName")),
221+
LoginName = reader.GetString(reader.GetOrdinal("LoginName")),
222+
WorkstationId = reader.GetString(reader.GetOrdinal("WorkstationId"))
223+
};
224+
225+
// Create a short (ellipsized) version of QueryText
226+
record.QueryTextShort = record.QueryText.Length > 100
227+
? record.QueryText.Substring(0, 100)
228+
: record.QueryText;
229+
230+
_allRecords.Add(record);
231+
}
232+
}
233+
}
234+
}
235+
}
236+
catch (Exception ex)
237+
{
238+
MessageBox.Show("Error loading data: " + ex.Message,
239+
"Error",
240+
MessageBoxButton.OK,
241+
MessageBoxImage.Error);
242+
}
243+
244+
// Push into the ObservableCollection
245+
QueryHistoryRecords.Clear();
246+
foreach (var rec in _allRecords)
247+
{
248+
QueryHistoryRecords.Add(rec);
249+
}
250+
}
251+
252+
public event PropertyChangedEventHandler PropertyChanged;
253+
protected void OnPropertyChanged(string propName)
254+
{
255+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
256+
}
257+
}
258+
}

0 commit comments

Comments
 (0)