• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

HicServices / RDMP / 6237307473

19 Sep 2023 04:02PM UTC coverage: 57.015% (-0.4%) from 57.44%
6237307473

push

github

web-flow
Feature/rc4 (#1570)

* Syntax tidying
* Dependency updates
* Event handling singletons (ThrowImmediately and co)

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: James A Sutherland <>
Co-authored-by: James Friel <jfriel001@dundee.ac.uk>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

10734 of 20259 branches covered (0.0%)

Branch coverage included in aggregate %.

5922 of 5922 new or added lines in 565 files covered. (100.0%)

30687 of 52390 relevant lines covered (58.57%)

7361.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/Rdmp.Core/CommandExecution/AtomicCommands/ExecuteCommandQueryPlatformDatabase.cs
1
// Copyright (c) The University of Dundee 2018-2019
2
// This file is part of the Research Data Management Platform (RDMP).
3
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4
// RDMP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.
6

7
using System;
8
using System.IO;
9
using System.Linq;
10
using FAnsi.Discovery;
11
using Rdmp.Core.Curation.Data;
12
using Rdmp.Core.Databases;
13
using Rdmp.Core.DataViewing;
14
using Rdmp.Core.MapsDirectlyToDatabaseTable;
15
using Rdmp.Core.MapsDirectlyToDatabaseTable.Versioning;
16
using Rdmp.Core.Repositories;
17
using Rdmp.Core.Repositories.Construction;
18
using Rdmp.Core.ReusableLibraryCode.DataAccess;
19

20
namespace Rdmp.Core.CommandExecution.AtomicCommands;
21

22
/// <summary>
23
/// Runs a query on a one of the RDMP platform databases and returns the results
24
/// </summary>
25
public class ExecuteCommandQueryPlatformDatabase : ExecuteCommandViewDataBase
26
{
27
    private string _query;
28
    private readonly FileInfo _toFile;
29
    private DiscoveredTable _table;
30

31
    [UseWithObjectConstructor]
32
    public ExecuteCommandQueryPlatformDatabase(IBasicActivateItems activator,
33
        [DemandsInitialization(
34
            "Database type e.g. DataExport, Catalogue, QueryCaching, LoggingDatabase etc (See all IPatcher implementations)")]
35
        string databaseType,
36
        [DemandsInitialization(
37
            "Optional SQL query to execute on the database.  Or null to query the first table in the db.")]
38
        string query = null,
39
        [DemandsInitialization(ToFileDescription)]
40
        FileInfo toFile = null) : base(activator, toFile)
×
41
    {
42
        _query = query;
×
43
        _toFile = toFile;
×
44

45
        var patcherType = MEF.GetTypes<IPatcher>().FirstOrDefault(t => t.Name.Equals(databaseType) || t.Name.Equals(
×
46
            $"{databaseType}Patcher"));
×
47

48
        if (patcherType == null)
×
49
        {
50
            SetImpossible($"Could not find Type called {databaseType} or {databaseType}Patcher");
×
51
            return;
×
52
        }
53

54
        DiscoveredDatabase db;
55

56
        if (patcherType == typeof(DataExportPatcher))
×
57
        {
58
            db = GetDatabase(BasicActivator.RepositoryLocator.DataExportRepository);
×
59

60
            _query ??= "Select * from Project";
×
61
            _table = db?.ExpectTable("Project");
×
62
            return;
×
63
        }
64

65
        if (patcherType == typeof(CataloguePatcher))
×
66
        {
67
            db = GetDatabase(BasicActivator.RepositoryLocator.CatalogueRepository);
×
68

69
            _query ??= "Select * from Catalogue";
×
70
            _table = db?.ExpectTable("Catalogue");
×
71
            return;
×
72
        }
73

74
        var eds = BasicActivator.RepositoryLocator.CatalogueRepository.GetAllObjects<ExternalDatabaseServer>();
×
75

76
        var patcher = (IPatcher)Activator.CreateInstance(patcherType);
×
77
        db = GetDatabase(eds.Where(e => e.WasCreatedBy(patcher)).ToArray());
×
78

79
        if (db == null) return;
×
80

81
        SetTargetDatabase(db);
×
82
    }
×
83

84
    public ExecuteCommandQueryPlatformDatabase(IBasicActivateItems activator,
85
        ExternalDatabaseServer eds) : base(activator, null)
×
86
    {
87
        DiscoveredDatabase db;
88

89
        try
90
        {
91
            db = eds.Discover(DataAccessContext.InternalDataProcessing);
×
92
        }
×
93
        catch (Exception)
×
94
        {
95
            SetImpossible("Not a queryable SQL database");
×
96
            return;
×
97
        }
98

99
        SetTargetDatabase(db);
×
100
    }
×
101

102

103
    private DiscoveredDatabase GetDatabase(IRepository repository)
104
    {
105
        if (repository is TableRepository tableRepo) return tableRepo.DiscoveredServer?.GetCurrentDatabase();
×
106

107
        SetImpossible("Repository was not a database repo");
×
108
        return null;
×
109
    }
110

111
    private DiscoveredDatabase GetDatabase(ExternalDatabaseServer[] eds)
112
    {
113
        if (eds.Length == 0)
×
114
        {
115
            SetImpossible("Could not find any databases of the requested Type");
×
116
            return null;
×
117
        }
118

119
        if (eds.Length > 1)
×
120
        {
121
            SetImpossible($"Found {eds.Length} databases of the requested Type");
×
122
            return null;
×
123
        }
124

125
        return eds[0].Discover(DataAccessContext.InternalDataProcessing);
×
126
    }
127

128

129
    private void SetTargetDatabase(DiscoveredDatabase database)
130
    {
131
        _table = database.DiscoverTables(false).FirstOrDefault();
×
132

133
        if (_table == null) SetImpossible("Database was empty");
×
134
    }
×
135

136

137
    protected override IViewSQLAndResultsCollection GetCollection() =>
138
        new ArbitraryTableExtractionUICollection(_table)
×
139
        {
×
140
            OverrideSql = _query
×
141
        };
×
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc