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<Resource> GetResourcesByParentId(Guid id) {
|
---|
44 | return DataContext.ExecuteQuery<Resource>(GetResourcesByParentIdQuery, id);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public IEnumerable<Guid> GetResourceIdsByParentId(Guid id) {
|
---|
48 | return DataContext.ExecuteQuery<Guid>(GetResourceIdsByParentIdQuery, id);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public IEnumerable<Resource> GetResourcesByChildId(Guid id) {
|
---|
52 | return DataContext.ExecuteQuery<Resource>(GetResourcesByChildIdQuery, id);
|
---|
53 | }
|
---|
54 |
|
---|
55 | public IEnumerable<Guid> GetResourceIdsByChildId(Guid id) {
|
---|
56 | return DataContext.ExecuteQuery<Guid>(GetResourceIdsByChildIdQuery, id);
|
---|
57 | }
|
---|
58 |
|
---|
59 | #region Compiled queries
|
---|
60 | private static readonly Func<DataContext, Guid, Resource> GetByIdQuery =
|
---|
61 | CompiledQuery.Compile((DataContext db, Guid resourceId) =>
|
---|
62 | (from resource in db.GetTable<Resource>()
|
---|
63 | where resource.ResourceId == resourceId
|
---|
64 | select resource).SingleOrDefault());
|
---|
65 |
|
---|
66 | private static readonly Func<DataContext, string, Resource> GetByNameQuery =
|
---|
67 | CompiledQuery.Compile((DataContext db, string name) =>
|
---|
68 | (from resource in db.GetTable<Resource>()
|
---|
69 | where resource.Name == name
|
---|
70 | select resource).FirstOrDefault());
|
---|
71 | #endregion
|
---|
72 |
|
---|
73 | #region String queries
|
---|
74 | private const string GetResourcesByParentIdQuery = @"
|
---|
75 | WITH rtree AS
|
---|
76 | (
|
---|
77 | SELECT ResourceId, ParentResourceId
|
---|
78 | FROM [Resource]
|
---|
79 | UNION ALL
|
---|
80 | SELECT rt.ResourceId, r.ParentResourceId
|
---|
81 | FROM [Resource] r
|
---|
82 | JOIN rtree rt ON rt.ParentResourceId = r.ResourceId AND r.ParentResourceId <> r.ResourceId AND rt.ParentResourceId <> rt.ResourceId
|
---|
83 | )
|
---|
84 | SELECT DISTINCT res.*
|
---|
85 | FROM rtree, [Resource] res
|
---|
86 | WHERE rtree.ParentResourceId = ({0})
|
---|
87 | AND rtree.ResourceId = res.ResourceId
|
---|
88 | ";
|
---|
89 | private const string GetResourceIdsByParentIdQuery = @"
|
---|
90 | WITH rtree AS
|
---|
91 | (
|
---|
92 | SELECT ResourceId, ParentResourceId
|
---|
93 | FROM [Resource]
|
---|
94 | UNION ALL
|
---|
95 | SELECT rt.ResourceId, r.ParentResourceId
|
---|
96 | FROM [Resource] r
|
---|
97 | JOIN rtree rt ON rt.ParentResourceId = r.ResourceId AND r.ParentResourceId <> r.ResourceId AND rt.ParentResourceId <> rt.ResourceId
|
---|
98 | )
|
---|
99 | SELECT DISTINCT rtree.ResourceId
|
---|
100 | FROM rtree
|
---|
101 | WHERE rtree.ParentResourceId = ({0})
|
---|
102 | ";
|
---|
103 | private const string GetResourcesByChildIdQuery = @"
|
---|
104 | WITH rtree AS
|
---|
105 | (
|
---|
106 | SELECT ResourceId, ParentResourceId
|
---|
107 | FROM [Resource]
|
---|
108 | UNION ALL
|
---|
109 | SELECT rt.ResourceId, r.ParentResourceId
|
---|
110 | FROM [Resource] r
|
---|
111 | JOIN rtree rt ON rt.ParentResourceId = r.ResourceId AND r.ParentResourceId <> r.ResourceId AND rt.ParentResourceId <> rt.ResourceId
|
---|
112 | )
|
---|
113 | SELECT DISTINCT res.*
|
---|
114 | FROM rtree, [Resource] res
|
---|
115 | WHERE rtree.ResourceId = ({0})
|
---|
116 | AND rtree.ParentResourceId = res.ResourceId
|
---|
117 | ";
|
---|
118 | private const string GetResourceIdsByChildIdQuery = @"
|
---|
119 | WITH rtree AS
|
---|
120 | (
|
---|
121 | SELECT ResourceId, ParentResourceId
|
---|
122 | FROM [Resource]
|
---|
123 | UNION ALL
|
---|
124 | SELECT rt.ResourceId, r.ParentResourceId
|
---|
125 | FROM [Resource] r
|
---|
126 | JOIN rtree rt ON rt.ParentResourceId = r.ResourceId AND r.ParentResourceId <> r.ResourceId AND rt.ParentResourceId <> rt.ResourceId
|
---|
127 | )
|
---|
128 | SELECT DISTINCT rtree.ParentResourceId
|
---|
129 | FROM rtree
|
---|
130 | WHERE rtree.ResourceId = ({0})
|
---|
131 | ";
|
---|
132 | #endregion
|
---|
133 | }
|
---|
134 | }
|
---|