Add OleDb connection method

This commit is contained in:
Jeremy Wu 2020-05-18 21:31:28 +10:00
parent ff03f7d284
commit 5484b30514
2 changed files with 58 additions and 11 deletions

View file

@ -17,6 +17,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.OleDb" Version="4.7.1" />
<PackageReference Include="tlbimp-Microsoft.Search.Interop" Version="1.0.0" />
</ItemGroup>

View file

@ -1,12 +1,16 @@
using Microsoft.Search.Interop;
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
{
public class QueryConstructor
{
public OleDbConnection conn;
public OleDbCommand command;
public OleDbDataReader dataReaderResults;
private Settings _settings;
private const string SystemIndex = "SystemIndex";
@ -18,14 +22,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
public CSearchQueryHelper CreateBaseQuery()
{
// This uses the Microsoft.Search.Interop assembly
CSearchManager manager = new CSearchManager();
// SystemIndex catalog is the default catalog in Windows
ISearchCatalogManager catalogManager = manager.GetCatalog(SystemIndex);
// Get the ISearchQueryHelper which will help us to translate AQS --> SQL necessary to query the indexer
var baseQuery = catalogManager.GetQueryHelper();
var baseQuery = CreateQueryHelper();
// Set the number of results we want. Don't set this property if all results are needed.
baseQuery.QueryMaxResults = _settings.MaxResult;
@ -33,7 +30,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
// Set list of columns we want to display, getting the path presently
baseQuery.QuerySelectColumns = "System.FileName, System.ItemPathDisplay";
// Filter based on folder/file name
// Filter based on file name
baseQuery.QueryContentProperties = "System.FileName";
// Set sorting order
@ -42,6 +39,20 @@ namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
return baseQuery;
}
internal CSearchQueryHelper CreateQueryHelper()
{
// This uses the Microsoft.Search.Interop assembly
var manager = new CSearchManager();
// SystemIndex catalog is the default catalog in Windows
var catalogManager = manager.GetCatalog(SystemIndex);
// Get the ISearchQueryHelper which will help us to translate AQS --> SQL necessary to query the indexer
var queryHelper = catalogManager.GetQueryHelper();
return queryHelper;
}
///<summary>
/// Set the required WHERE clause restriction to search on the first level of a specified directory.
///</summary>
@ -67,7 +78,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
///</summary>
public string QueryForAllFilesAndFolders(string userSearchString)
{
// Generate SQL from our parameters, converting the userQuery from AQS->WHERE clause
// Generate SQL from constructed parameters, converting the userSearchString from AQS->WHERE clause
return CreateBaseQuery().GenerateSQLFromUserQuery(userSearchString) + " AND " + QueryWhereRestrictionsForAllFilesAndFoldersSearch();
}
@ -78,5 +89,40 @@ namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
{
return $"scope='file:'";
}
internal List<Result> ExecuteWindowsIndexSearch(string query)
{
var results = new List<Result>();
using (conn = new OleDbConnection(CreateQueryHelper().ConnectionString))
{
conn.Open();
using (command = new OleDbCommand(query, conn))
{
// Results return as an OleDbDataReader.
using (dataReaderResults = command.ExecuteReader())
{
if (dataReaderResults.HasRows)
{
while (dataReaderResults.Read())
{
if (dataReaderResults.GetValue(0) != DBNull.Value && dataReaderResults.GetValue(1) != DBNull.Value)
{
var result = new Result
{
Title = dataReaderResults.GetString(0),
SubTitle = dataReaderResults.GetString(1)
};
results.Add(result);
}
}
}
}
}
}
return results;
}
}
}