feat(control): add form extended lookup column search
This commit is contained in:
@@ -50,11 +50,14 @@ namespace Lskj.Control
|
||||
public int Version;
|
||||
public string SourceSql;
|
||||
public string Keyword;
|
||||
public string SearchField;
|
||||
public string ConnectionString;
|
||||
public bool SchemaOnly;
|
||||
}
|
||||
|
||||
private sealed class QueryResult
|
||||
{
|
||||
public DataTable Schema;
|
||||
public DataTable Table;
|
||||
public Exception Error;
|
||||
}
|
||||
@@ -302,10 +305,20 @@ namespace Lskj.Control
|
||||
popupEdit.Properties.PopupFormSize = popupSize;
|
||||
popupEdit.Properties.PopupFormMinSize = popupSize;
|
||||
popup.PrepareForOpen();
|
||||
string sourceSql = BuildSourceSql();
|
||||
DataTable cachedSchema = TryGetSchema(sourceSql);
|
||||
if (cachedSchema != null)
|
||||
{
|
||||
ConfigureSearchColumns(cachedSchema);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(EditValue))
|
||||
{
|
||||
StartSearch(EditValue);
|
||||
QueueSearch(sourceSql, EditValue, string.Empty, false);
|
||||
}
|
||||
else if (cachedSchema == null)
|
||||
{
|
||||
QueueSearch(sourceSql, string.Empty, string.Empty, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -336,7 +349,7 @@ namespace Lskj.Control
|
||||
private void PopupEdit_Closed(object sender, ClosedEventArgs e)
|
||||
{
|
||||
CancelSearch();
|
||||
popup.ResultGrid.DataSource = null;
|
||||
popup.ClearResultAndFilter();
|
||||
RefreshDisplayText();
|
||||
}
|
||||
|
||||
@@ -346,13 +359,14 @@ namespace Lskj.Control
|
||||
{
|
||||
ValidateBusinessConfiguration();
|
||||
string keyword = popup.SearchText;
|
||||
string searchField = popup.SelectedSearchField;
|
||||
popup.ClearResultAndFilter();
|
||||
if (string.IsNullOrWhiteSpace(keyword))
|
||||
{
|
||||
CancelSearch();
|
||||
popup.ResultGrid.DataSource = null;
|
||||
return;
|
||||
}
|
||||
StartSearch(keyword);
|
||||
QueueSearch(BuildSourceSql(), keyword, searchField, false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -613,13 +627,15 @@ namespace Lskj.Control
|
||||
return Model.SourceSql;
|
||||
}
|
||||
|
||||
private void StartSearch(string keyword)
|
||||
private void QueueSearch(string sourceSql, string keyword, string searchField, bool schemaOnly)
|
||||
{
|
||||
SearchRequest request = new SearchRequest();
|
||||
request.Version = Interlocked.Increment(ref queryVersion);
|
||||
request.SourceSql = BuildSourceSql();
|
||||
request.SourceSql = sourceSql;
|
||||
request.Keyword = keyword;
|
||||
request.SearchField = searchField;
|
||||
request.ConnectionString = SqlHelper._connection.ConnectionString;
|
||||
request.SchemaOnly = schemaOnly;
|
||||
|
||||
bool startWorker = false;
|
||||
lock (querySyncRoot)
|
||||
@@ -653,27 +669,29 @@ namespace Lskj.Control
|
||||
}
|
||||
}
|
||||
|
||||
QueryResult result = Query(
|
||||
request.SourceSql,
|
||||
request.Keyword,
|
||||
request.ConnectionString);
|
||||
DeliverResult(request.Version, result);
|
||||
QueryResult result = Query(request);
|
||||
DeliverResult(request, result);
|
||||
}
|
||||
}
|
||||
|
||||
private QueryResult Query(string sourceSql, string keyword, string connectionString)
|
||||
private QueryResult Query(SearchRequest request)
|
||||
{
|
||||
QueryResult result = new QueryResult();
|
||||
try
|
||||
{
|
||||
DataTable sourceSchema = GetSchema(sourceSql, connectionString);
|
||||
DataTable sourceSchema = GetSchema(request.SourceSql, request.ConnectionString);
|
||||
result.Schema = sourceSchema;
|
||||
IList<ExtendedReturnFieldMapping> mappings = ExtendedReturnSupport.ParseResultFields(Model.ResultFields);
|
||||
ValidateSourceColumns(sourceSchema.Columns, mappings);
|
||||
string searchSql = ExtendedReturnSupport.BuildSearchSql(sourceSql, sourceSchema.Columns, MaxRows);
|
||||
result.Table = ExecuteQuery(
|
||||
searchSql,
|
||||
ExtendedReturnSupport.BuildLikeParameterValue(keyword),
|
||||
connectionString);
|
||||
if (!request.SchemaOnly)
|
||||
{
|
||||
string searchSql = ExtendedReturnSupport.BuildSearchSql(
|
||||
request.SourceSql, sourceSchema.Columns, MaxRows, request.SearchField);
|
||||
result.Table = ExecuteQuery(
|
||||
searchSql,
|
||||
ExtendedReturnSupport.BuildLikeParameterValue(request.Keyword),
|
||||
request.ConnectionString);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -682,6 +700,18 @@ namespace Lskj.Control
|
||||
return result;
|
||||
}
|
||||
|
||||
private DataTable TryGetSchema(string sourceSql)
|
||||
{
|
||||
lock (schemaSyncRoot)
|
||||
{
|
||||
if (schema != null && string.Equals(schemaSql, sourceSql, StringComparison.Ordinal))
|
||||
{
|
||||
return schema;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private DataTable GetSchema(string sourceSql, string connectionString)
|
||||
{
|
||||
lock (schemaSyncRoot)
|
||||
@@ -730,21 +760,25 @@ namespace Lskj.Control
|
||||
return table;
|
||||
}
|
||||
|
||||
private void DeliverResult(int version, QueryResult result)
|
||||
private void DeliverResult(SearchRequest request, QueryResult result)
|
||||
{
|
||||
if (disposed || !IsHandleCreated) return;
|
||||
|
||||
MethodInvoker deliver = new MethodInvoker(delegate
|
||||
{
|
||||
if (disposed || IsDisposed || version != queryVersion || !popupEdit.IsPopupOpen) return;
|
||||
if (disposed || IsDisposed || request.Version != queryVersion || !popupEdit.IsPopupOpen) return;
|
||||
ConfigureSearchColumns(result.Schema);
|
||||
if (result.Error != null)
|
||||
{
|
||||
MessageUtil.Show("扩展搜索查询失败:" + result.Error.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
popup.ResultGrid.DataSource = result.Table;
|
||||
ConfigureResultColumns(result.Table);
|
||||
if (result.Table != null)
|
||||
{
|
||||
popup.ResultGrid.DataSource = result.Table;
|
||||
ConfigureResultColumns(result.Table);
|
||||
}
|
||||
});
|
||||
|
||||
try
|
||||
@@ -759,6 +793,24 @@ namespace Lskj.Control
|
||||
}
|
||||
}
|
||||
|
||||
private void ConfigureSearchColumns(DataTable sourceSchema)
|
||||
{
|
||||
if (sourceSchema == null) return;
|
||||
|
||||
List<KeyValuePair<string, string>> options = new List<KeyValuePair<string, string>>();
|
||||
IList<ExtendedReturnFieldMapping> mappings =
|
||||
ExtendedReturnSupport.ParseResultFields(Model.ResultFields);
|
||||
foreach (DataColumn column in sourceSchema.Columns)
|
||||
{
|
||||
if (!ExtendedReturnSupport.IsSearchableColumn(column.ColumnName)) continue;
|
||||
|
||||
options.Add(new KeyValuePair<string, string>(
|
||||
column.ColumnName,
|
||||
GetColumnCaption(column.ColumnName, mappings)));
|
||||
}
|
||||
popup.SetSearchColumns(options);
|
||||
}
|
||||
|
||||
private void ConfigureResultColumns(DataTable table)
|
||||
{
|
||||
if (table == null) return;
|
||||
|
||||
Reference in New Issue
Block a user