Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Services.OKB/3.3/QueryService.cs @ 6514

Last change on this file since 6514 was 4381, checked in by swagner, 14 years ago

Worked on OKB data model and services (#1174).

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Data;
25using System.Linq;
26using System.ServiceModel;
27using HeuristicLab.Services.OKB.AttributeSelection;
28using HeuristicLab.Services.OKB.DataAccess;
29
30namespace HeuristicLab.Services.OKB {
31  /// <summary>
32  /// Implementation of the <see cref="IQueryService"/>.
33  /// </summary>
34  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, IncludeExceptionDetailInFaults = true)]
35  public class QueryService : IQueryService, IDisposable {
36    /// <summary>
37    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
38    /// </summary>
39    public void Dispose() {
40      Terminate();
41    }
42
43    private DataSetBuilder dataSetBuilder;
44    private OKBDataContext okb;
45    private IEnumerator<DataRow> dataEnumerator;
46
47    /// <summary>
48    /// Gets a list of all possible attribute selectors.
49    /// </summary>
50    /// <returns>
51    /// An array of <see cref="Server.AttributeSelector"/>s
52    /// </returns>
53    public AttributeSelector[] GetAllAttributeSelectors() {
54      using (OKBDataContext okb = new OKBDataContext()) {
55        return RunAttributeSelector.GetAllAttributeSelectors(okb).Select(s => new AttributeSelector(s)).ToArray();
56      }
57    }
58
59    /// <summary>
60    /// Prepares the a query using the specified selection criteria.
61    /// </summary>
62    /// <param name="selectors">The list of <see cref="Server.AttributeSelector"/>s.</param>
63    /// <returns>
64    /// An empty <see cref="DataTable"/> containing only the column headers.
65    /// </returns>
66    public DataTable PrepareQuery(AttributeSelector[] selectors) {
67      using (OKBDataContext okb = new OKBDataContext()) {
68        if (selectors.Length == 0)
69          throw new FaultException("No columns selected");
70        IEnumerable<RunAttributeSelector> checkedSelectors =
71          selectors.Select(s => new RunAttributeSelector(new OKBDataContext(okb.Connection.ConnectionString), s)).ToList();
72        dataSetBuilder = new DataSetBuilder(okb, checkedSelectors);
73        dataEnumerator = dataSetBuilder.GetEnumerator();
74        return dataSetBuilder.Runs;
75      }
76    }
77
78    /// <summary>
79    /// Gets the number of matching rows for the prepared query
80    /// (see <see cref="IQueryService.PrepareQuery"/>).
81    /// </summary>
82    /// <returns>The number of matching rows.</returns>
83    public int GetCount() {
84      return dataSetBuilder.GetCount();
85    }
86
87    /// <summary>
88    /// Gets the next rows.
89    /// </summary>
90    /// <param name="maxNRows">The max N rows.</param>
91    /// <returns></returns>
92    public DataTable GetNextRows(int maxNRows) {
93      DataTable runs = dataSetBuilder.Runs.Clone();
94      for (int i = 0; i < maxNRows; i++) {
95        if (!dataEnumerator.MoveNext())
96          break;
97        DataRow row = runs.NewRow();
98        row.ItemArray = (object[])dataEnumerator.Current.ItemArray.Clone();
99        runs.Rows.Add(row);
100      }
101      return runs;
102    }
103
104    /// <summary>
105    /// Terminates the session and closes the connection.
106    /// </summary>
107    public void Terminate() {
108      dataSetBuilder = null;
109      if (dataEnumerator != null) {
110        dataEnumerator.Dispose();
111        dataEnumerator = null;
112      }
113      if (okb != null) {
114        okb.Dispose();
115        okb = null;
116      }
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.