mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-01-18 06:29:44 +08:00
Refactored the code to make it unit testable
This commit is contained in:
parent
7164260f6e
commit
69758b80a5
@ -7,7 +7,63 @@ namespace Wox.Plugin.Indexer.SearchHelper
|
|||||||
{
|
{
|
||||||
public class WindowsSearchAPI
|
public class WindowsSearchAPI
|
||||||
{
|
{
|
||||||
public IEnumerable<SearchResult> Search(string keyword, string pattern = "*", int offset = 0, int maxCount = 100)
|
private OleDbConnection conn;
|
||||||
|
private OleDbCommand command;
|
||||||
|
private OleDbDataReader WDSResults;
|
||||||
|
|
||||||
|
public IEnumerable<SearchResult> ExecuteQuery(ISearchQueryHelper queryHelper, string keyword)
|
||||||
|
{
|
||||||
|
// Generate SQL from our parameters, converting the userQuery from AQS->WHERE clause
|
||||||
|
string sqlQuery = queryHelper.GenerateSQLFromUserQuery(keyword);
|
||||||
|
|
||||||
|
// --- Perform the query ---
|
||||||
|
// create an OleDbConnection object which connects to the indexer provider with the windows application
|
||||||
|
using (conn = new OleDbConnection(queryHelper.ConnectionString))
|
||||||
|
{
|
||||||
|
// open the connection
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
// now create an OleDB command object with the query we built above and the connection we just opened.
|
||||||
|
using (command = new OleDbCommand(sqlQuery, conn))
|
||||||
|
{
|
||||||
|
// execute the command, which returns the results as an OleDbDataReader.
|
||||||
|
using (WDSResults = command.ExecuteReader())
|
||||||
|
{
|
||||||
|
while (WDSResults.Read())
|
||||||
|
{
|
||||||
|
// col 0 is our path in display format
|
||||||
|
Console.WriteLine("{0}", WDSResults.GetString(0));
|
||||||
|
var result = new SearchResult { Path = WDSResults.GetString(0) };
|
||||||
|
|
||||||
|
yield return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setQueryHelper(ref ISearchQueryHelper queryHelper, string pattern)
|
||||||
|
{
|
||||||
|
// convert file pattern if it is not '*'. Don't create restriction for '*' as it includes all files.
|
||||||
|
if (pattern != "*")
|
||||||
|
{
|
||||||
|
pattern = pattern.Replace("*", "%");
|
||||||
|
pattern = pattern.Replace("?", "_");
|
||||||
|
|
||||||
|
if (pattern.Contains("%") || pattern.Contains("_"))
|
||||||
|
{
|
||||||
|
queryHelper.QueryWhereRestrictions += " AND System.FileName LIKE '" + pattern + "' ";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// if there are no wildcards we can use a contains which is much faster as it uses the index
|
||||||
|
queryHelper.QueryWhereRestrictions += " AND Contains(System.FileName, '" + pattern + "') ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<SearchResult> Search(string keyword, string pattern = "*", int maxCount = 100)
|
||||||
{
|
{
|
||||||
// This uses the Microsoft.Search.Interop assembly
|
// This uses the Microsoft.Search.Interop assembly
|
||||||
CSearchManager manager = new CSearchManager();
|
CSearchManager manager = new CSearchManager();
|
||||||
@ -27,55 +83,12 @@ namespace Wox.Plugin.Indexer.SearchHelper
|
|||||||
// Set additional query restriction
|
// Set additional query restriction
|
||||||
queryHelper.QueryWhereRestrictions = "AND scope='file:'";
|
queryHelper.QueryWhereRestrictions = "AND scope='file:'";
|
||||||
|
|
||||||
// convert file pattern if it is not '*'. Don't create restriction for '*' as it includes all files.
|
|
||||||
if (pattern != "*")
|
|
||||||
{
|
|
||||||
pattern = pattern.Replace("*", "%");
|
|
||||||
pattern = pattern.Replace("?", "_");
|
|
||||||
|
|
||||||
if (pattern.Contains("%") || pattern.Contains("_"))
|
|
||||||
{
|
|
||||||
queryHelper.QueryWhereRestrictions += " AND System.FileName LIKE '" + pattern + "' ";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// if there are no wildcards we can use a contains which is much faster as it uses the index
|
|
||||||
queryHelper.QueryWhereRestrictions += " AND Contains(System.FileName, '" + pattern + "') ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sorting order
|
// Set sorting order
|
||||||
queryHelper.QuerySorting = "System.DateModified DESC";
|
queryHelper.QuerySorting = "System.DateModified DESC";
|
||||||
|
|
||||||
// Generate SQL from our parameters, converting the userQuery from AQS->WHERE clause
|
setQueryHelper(ref queryHelper, pattern);
|
||||||
string sqlQuery = queryHelper.GenerateSQLFromUserQuery(keyword);
|
|
||||||
|
|
||||||
// --- Perform the query ---
|
|
||||||
// create an OleDbConnection object which connects to the indexer provider with the windows application
|
|
||||||
using (System.Data.OleDb.OleDbConnection conn = new OleDbConnection(queryHelper.ConnectionString))
|
|
||||||
{
|
|
||||||
// open the connection
|
|
||||||
conn.Open();
|
|
||||||
|
|
||||||
// now create an OleDB command object with the query we built above and the connection we just opened.
|
|
||||||
using (OleDbCommand command = new OleDbCommand(sqlQuery, conn))
|
|
||||||
{
|
|
||||||
// execute the command, which returns the results as an OleDbDataReader.
|
|
||||||
using (OleDbDataReader WDSResults = command.ExecuteReader())
|
|
||||||
{
|
|
||||||
while (WDSResults.Read())
|
|
||||||
{
|
|
||||||
// col 0 is our path in display format
|
|
||||||
Console.WriteLine("{0}", WDSResults.GetString(0));
|
|
||||||
var result = new SearchResult { Path = WDSResults.GetString(0) };
|
|
||||||
|
|
||||||
yield return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return ExecuteQuery(queryHelper, keyword);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user