2020-08-17 10:00:56 -07:00
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System ;
using System.Collections.Generic ;
using System.Data.OleDb ;
namespace Microsoft.Plugin.Indexer.SearchHelper
{
2020-08-19 17:28:19 -07:00
public class OleDBSearch : ISearch
2020-08-17 10:00:56 -07:00
{
[ 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" ) ]
public List < OleDBResult > Query ( string connectionString , string sqlQuery )
{
List < OleDBResult > result = new List < OleDBResult > ( ) ;
2020-08-19 17:28:19 -07:00
using ( var conn = new OleDbConnection ( connectionString ) )
2020-08-17 10:00:56 -07:00
{
// open the connection
conn . Open ( ) ;
2020-08-19 17:28:19 -07:00
// now create an OleDB command object with the query we built above and the connection we just opened.
using ( var command = new OleDbCommand ( sqlQuery , conn ) )
2020-08-17 10:00:56 -07:00
{
2020-08-19 17:28:19 -07:00
using ( var wDSResults = command . ExecuteReader ( ) )
2020-08-17 10:00:56 -07:00
{
2020-08-19 17:28:19 -07:00
if ( ! wDSResults . IsClosed & & wDSResults . HasRows )
2020-08-17 10:00:56 -07:00
{
2020-08-25 12:07:17 -07:00
while ( ! wDSResults . IsClosed & & wDSResults . Read ( ) )
2020-08-17 10:00:56 -07:00
{
2020-08-19 17:28:19 -07:00
List < object > fieldData = new List < object > ( wDSResults . FieldCount ) ;
for ( int i = 0 ; i < wDSResults . FieldCount ; i + + )
2020-08-17 10:00:56 -07:00
{
2020-08-19 17:28:19 -07:00
fieldData . Add ( wDSResults . GetValue ( i ) ) ;
2020-08-17 10:00:56 -07:00
}
2020-08-19 17:28:19 -07:00
result . Add ( new OleDBResult ( fieldData ) ) ;
2020-08-17 10:00:56 -07:00
}
}
}
}
}
return result ;
}
}
}