Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

dirty implementation of ancestors in sql

ulferts52220651fc6f
diff --git a/lib/api/decorators/sql/hal.rb b/lib/api/decorators/sql/hal.rb
index e2275181ad9..74de1447c36 100644
--- a/lib/api/decorators/sql/hal.rb
+++ b/lib/api/decorators/sql/hal.rb
@@ -80,44 +80,59 @@ module API
scope
end
- def link(name, column: nil, path: nil, title: nil, href: nil, join: nil, render_if: nil, **additional_properties)
+ def link(name,
+ column: nil,
+ path: nil,
+ title: nil,
+ href: nil,
+ join: nil,
+ render_if: nil,
+ sql: nil,
+ **additional_properties)
links[name] = { column: column,
path: path,
title: title,
join: join,
href: href,
render_if: render_if,
+ sql: sql,
additional_properties: additional_properties }
end
def links_selects(select, walker_result)
selected_links(select)
.map do |name, link|
- title = link[:title] ? link[:title].call : "#{name}.name"
-
- link_attributes = ["'href'", link_href(link, name, walker_result)]
-
- if title
- link_attributes += ["'title'", title]
- end
-
- (link[:additional_properties] || {}).each do |key, value|
- link_attributes += ["'#{key}'", value]
- end
-
- if link[:render_if]
+ if link[:sql]
<<-SQL.squish
- '#{name}',
- CASE WHEN #{link[:render_if].call(walker_result)} THEN
- json_build_object(#{link_attributes.join(', ')})
- ELSE
- NULL
- END
+ '#{name}', #{link[:sql].call}
SQL
else
- <<-SQL.squish
- '#{name}', json_build_object(#{link_attributes.join(', ')})
- SQL
+ title = link[:title] ? link[:title].call : "#{name}.name"
+
+ link_attributes = ["'href'", link_href(link, name, walker_result)]
+
+ if title
+ link_attributes += ["'title'", title]
+ end
+
+ (link[:additional_properties] || {}).each do |key, value|
+ link_attributes += ["'#{key}'", value]
+ end
+
+ if link[:render_if]
+ <<-SQL.squish
+ '#{name}',
+ CASE WHEN #{link[:render_if].call(walker_result)} THEN
+ json_build_object(#{link_attributes.join(', ')})
+ ELSE
+ NULL
+ END
+ SQL
+ else
+ <<-SQL.squish
+ '#{name}', json_build_object(#{link_attributes.join(', ')})
+ SQL
+ end
end
end
.join(', ')
@@ -196,7 +211,7 @@ module API
end
def select_from(walker_result)
- "(#{walker_result.scope.to_sql}) element"
+ "(#{walker_result.projection_scope.to_sql}) element"
end
def selected_links(select)
diff --git a/lib/api/decorators/sql_collection_representer.rb b/lib/api/decorators/sql_collection_representer.rb
index 859d61ed0e0..41280981dcb 100644
--- a/lib/api/decorators/sql_collection_representer.rb
+++ b/lib/api/decorators/sql_collection_representer.rb
@@ -34,16 +34,15 @@ module API
class << self
def ctes(walker_result)
{
- all_elements: walker_result.scope.to_sql,
- page_elements: "SELECT * FROM all_elements LIMIT #{sql_limit(walker_result)} OFFSET #{sql_offset(walker_result)}",
- total: "SELECT COUNT(*) from all_elements"
+ projection: walker_result.projection_scope.limit(sql_limit(walker_result)).offset(sql_offset(walker_result)).to_sql,
+ total: walker_result.filter_scope.reselect('COUNT(*)').except(:order).to_sql
}
end
private
def select_from(walker_result)
- "page_elements"
+ "projection"
end
def full_self_path(walker_results, overrides = {})
diff --git a/lib/api/v3/projects/project_sql_representer.rb b/lib/api/v3/projects/project_sql_representer.rb
index 09562dba04d..5f71e622736 100644
--- a/lib/api/v3/projects/project_sql_representer.rb
+++ b/lib/api/v3/projects/project_sql_representer.rb
@@ -32,6 +32,47 @@ module API
class ProjectSqlRepresenter
include API::Decorators::Sql::Hal
+ class << self
+ def ctes(walker_result)
+ {
+ ancestors: ancestors_sql(walker_result)
+ }
+ end
+
+ protected
+
+ def ancestors_sql(walker_result)
+ <<-SQL.squish
+ SELECT id, json_agg(link) ancestors
+ FROM
+ (
+ SELECT
+ origin.id,
+ json_build_object('href', format('/api/v3/projects/%s', ancestors.id), 'title', ancestors.name) link
+ FROM projects origin
+ JOIN projects ancestors
+ ON ancestors.lft < origin.lft AND ancestors.rgt > origin.rgt
+ WHERE origin.id IN (#{walker_result.filter_scope.limit(walker_result.page_size).offset((walker_result.offset - 1) * walker_result.page_size).select(:id).to_sql})
+ ORDER by origin.id, ancestors.lft
+ ) ancestors
+ GROUP BY id
+ SQL
+ end
+ end
+
+ link :self,
+ path: { api: :project, params: %w(id) },
+ column: -> { :id },
+ title: -> { :name }
+
+ link :ancestors,
+ sql: -> { 'ancestors' },
+ join: {
+ table: :ancestors,
+ condition: 'ancestors.id = projects.id',
+ select: 'ancestors'
+ }
+
property :_type,
representation: ->(*) { "'Project'" }
diff --git a/lib/api/v3/utilities/sql_representer_walker.rb b/lib/api/v3/utilities/sql_representer_walker.rb
index b7989df5823..685a8ebe407 100644
--- a/lib/api/v3/utilities/sql_representer_walker.rb
+++ b/lib/api/v3/utilities/sql_representer_walker.rb
@@ -60,7 +60,7 @@ module API
end
embedded_depth_first([], start) do |_, stack, current_representer|
- result.scope = current_representer.joins(select_for(stack), result.scope)
+ result.projection_scope = current_representer.joins(select_for(stack), result.projection_scope)
end
embedded_depth_first([], start) do |_, _, current_representer|
diff --git a/lib/api/v3/utilities/sql_walker_results.rb b/lib/api/v3/utilities/sql_walker_results.rb
index ea07ba95453..fa28e3ec525 100644
--- a/lib/api/v3/utilities/sql_walker_results.rb
+++ b/lib/api/v3/utilities/sql_walker_results.rb
@@ -33,14 +33,16 @@ module API
module Utilities
class SqlWalkerResults
def initialize(scope, url_query:, self_path: nil, replace_map: {})
- self.scope = scope
+ self.filter_scope = scope.dup
+ self.projection_scope = scope.dup.reselect("#{scope.model.table_name}.*")
self.ctes = {}
self.self_path = self_path
self.url_query = url_query
self.replace_map = replace_map
end
- attr_accessor :scope,
+ attr_accessor :filter_scope,
+ :projection_scope,
:sql,
:selects,
:ctes,