1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Data.Linq;
|
---|
25 | using System.Linq;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Services.Hive.DataAccess.Daos {
|
---|
28 | public class ResourceDao : GenericDao<Guid, Resource> {
|
---|
29 | public ResourceDao(DataContext dataContext) : base(dataContext) { }
|
---|
30 |
|
---|
31 | public override Resource GetById(Guid id) {
|
---|
32 | return GetByIdQuery(DataContext, id);
|
---|
33 | }
|
---|
34 |
|
---|
35 | public Resource GetByName(string name) {
|
---|
36 | return GetByNameQuery(DataContext, name);
|
---|
37 | }
|
---|
38 |
|
---|
39 | public IQueryable<Resource> GetResourcesWithValidOwner() {
|
---|
40 | return Table.Where(x => x.OwnerUserId != null);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public IEnumerable<Guid> GetResourceIdsByParentId(Guid id) {
|
---|
44 | return DataContext.ExecuteQuery<Guid>(GetResourceIdsByParentIdQuery, id);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public IEnumerable<Resource> GetResourcesByParentId(Guid id) {
|
---|
48 | return DataContext.ExecuteQuery<Resource>(GetResourcesByParentIdQuery, id);
|
---|
49 | }
|
---|
50 |
|
---|
51 | #region Compiled queries
|
---|
52 | private static readonly Func<DataContext, Guid, Resource> GetByIdQuery =
|
---|
53 | CompiledQuery.Compile((DataContext db, Guid resourceId) =>
|
---|
54 | (from resource in db.GetTable<Resource>()
|
---|
55 | where resource.ResourceId == resourceId
|
---|
56 | select resource).SingleOrDefault());
|
---|
57 |
|
---|
58 | private static readonly Func<DataContext, string, Resource> GetByNameQuery =
|
---|
59 | CompiledQuery.Compile((DataContext db, string name) =>
|
---|
60 | (from resource in db.GetTable<Resource>()
|
---|
61 | where resource.Name == name
|
---|
62 | select resource).FirstOrDefault());
|
---|
63 | #endregion
|
---|
64 |
|
---|
65 | #region String queries
|
---|
66 | private const string GetResourcesByParentIdQuery = @"
|
---|
67 | WITH rtree AS
|
---|
68 | (
|
---|
69 | SELECT ResourceId, ParentResourceId
|
---|
70 | FROM [Resource]
|
---|
71 | UNION ALL
|
---|
72 | SELECT rt.ResourceId, r.ParentResourceId
|
---|
73 | FROM [Resource] r
|
---|
74 | JOIN rtree rt ON rt.ParentResourceId = r.ResourceId AND r.ParentResourceId <> r.ResourceId AND rt.ParentResourceId <> rt.ResourceId
|
---|
75 | )
|
---|
76 | SELECT DISTINCT rtree.ResourceId
|
---|
77 | FROM rtree
|
---|
78 | WHERE rtree.ParentResourceId = ({0})
|
---|
79 | ";
|
---|
80 | private const string GetResourceIdsByParentIdQuery = @"
|
---|
81 | WITH rtree AS
|
---|
82 | (
|
---|
83 | SELECT ResourceId, ParentResourceId
|
---|
84 | FROM [Resource]
|
---|
85 | UNION ALL
|
---|
86 | SELECT rt.ResourceId, r.ParentResourceId
|
---|
87 | FROM [Resource] r
|
---|
88 | JOIN rtree rt ON rt.ParentResourceId = r.ResourceId AND r.ParentResourceId <> r.ResourceId AND rt.ParentResourceId <> rt.ResourceId
|
---|
89 | )
|
---|
90 | SELECT DISTINCT res.*
|
---|
91 | FROM rtree, [Resource] res
|
---|
92 | WHERE rtree.ParentResourceId = ({0})
|
---|
93 | AND rtree.ResourceId = res.ResourceId
|
---|
94 | ";
|
---|
95 | #endregion
|
---|
96 | }
|
---|
97 | }
|
---|