2020-07-30 16:39:47 -07:00
|
|
|
|
using System;
|
2020-06-18 15:42:28 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Data.OleDb;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.Plugin.Indexer.SearchHelper
|
|
|
|
|
|
{
|
2020-07-30 16:39:47 -07:00
|
|
|
|
public class OleDBSearch : ISearch, IDisposable
|
2020-06-18 15:42:28 -07:00
|
|
|
|
{
|
|
|
|
|
|
private OleDbCommand command;
|
|
|
|
|
|
private OleDbConnection conn;
|
2020-07-30 16:39:47 -07:00
|
|
|
|
private OleDbDataReader WDSResults;
|
|
|
|
|
|
private bool disposedValue;
|
|
|
|
|
|
|
|
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA2100:Review SQL queries for security vulnerabilities",
|
|
|
|
|
|
Justification = "sqlQuery does not come from user input but is generated via the ISearchQueryHelper::GenerateSqlFromUserQuery " +
|
|
|
|
|
|
" see: https://docs.microsoft.com/en-us/windows/win32/search/-search-3x-wds-qryidx-searchqueryhelper#using-the-generatesqlfromuserquery-method")]
|
2020-06-18 15:42:28 -07:00
|
|
|
|
public List<OleDBResult> Query(string connectionString, string sqlQuery)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<OleDBResult> result = new List<OleDBResult>();
|
|
|
|
|
|
|
|
|
|
|
|
using (conn = new OleDbConnection(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))
|
|
|
|
|
|
{
|
|
|
|
|
|
using (WDSResults = command.ExecuteReader())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (WDSResults.HasRows)
|
|
|
|
|
|
{
|
|
|
|
|
|
while (WDSResults.Read())
|
|
|
|
|
|
{
|
|
|
|
|
|
List<Object> fieldData = new List<object>();
|
|
|
|
|
|
for (int i = 0; i < WDSResults.FieldCount; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
fieldData.Add(WDSResults.GetValue(i));
|
|
|
|
|
|
}
|
|
|
|
|
|
result.Add(new OleDBResult(fieldData));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Checks if all the variables related to database connection have been properly disposed
|
|
|
|
|
|
public bool HaveAllDisposableItemsBeenDisposed()
|
|
|
|
|
|
{
|
|
|
|
|
|
bool commandDisposed = false;
|
|
|
|
|
|
bool connDisposed = false;
|
|
|
|
|
|
bool resultDisposed = false;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
command.ExecuteReader();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (InvalidOperationException)
|
|
|
|
|
|
{
|
|
|
|
|
|
commandDisposed = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
WDSResults.Read();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (InvalidOperationException)
|
|
|
|
|
|
{
|
|
|
|
|
|
resultDisposed = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 13:27:17 -07:00
|
|
|
|
if (conn.State == System.Data.ConnectionState.Closed)
|
2020-06-18 15:42:28 -07:00
|
|
|
|
{
|
|
|
|
|
|
connDisposed = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return commandDisposed && resultDisposed && connDisposed;
|
2020-07-30 16:39:47 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!disposedValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (disposing)
|
|
|
|
|
|
{
|
|
|
|
|
|
command?.Dispose();
|
|
|
|
|
|
conn?.Dispose();
|
|
|
|
|
|
WDSResults?.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
|
|
|
|
|
|
// TODO: set large fields to null
|
|
|
|
|
|
disposedValue = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
|
|
|
|
|
// ~OleDBSearch()
|
|
|
|
|
|
// {
|
|
|
|
|
|
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
|
|
// Dispose(disposing: false);
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
|
|
Dispose(disposing: true);
|
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
|
}
|
2020-06-18 15:42:28 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|