recursifier 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/recursifier/version.rb +5 -0
- data/lib/recursifier.rb +56 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9377da0704e74c9cff7c6f8b170c2f95601af14e6fc707b1a377137862bb7c12
|
4
|
+
data.tar.gz: 47da562f93257c3e83c4db12c57ae474954aaebf0f3a12c5e082859617478764
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 449d00f6335ca6c1f4c8c527eaddc7c60932e95225253dfe25e44f4d88fe150e9c92953537bf88c0789a39712571b5f6438215bac0d31ef02c1dfedaea2734cd
|
7
|
+
data.tar.gz: f508864e8bee585a63f6f96e4cd2664ccc45f231d5e453f84fb7ffd4d5b02f3aabde5ce356ff07d70f1d91e00cb7665d6ae4088c503536059eebfb95a57096f9
|
data/lib/recursifier.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "recursifier/version"
|
4
|
+
|
5
|
+
module Recursifier
|
6
|
+
module RecursiveQuery
|
7
|
+
|
8
|
+
class InvalidQueryError < StandardError; end
|
9
|
+
|
10
|
+
def self.fetch_hierarchy(table_name, parent_matching_column, sub_matching_column, start_id, filters = {}, selected_columns = [], max_depth = 10, current_depth = 0)
|
11
|
+
|
12
|
+
# Validate inputs
|
13
|
+
raise InvalidQueryError, "Table name must be a string" unless table_name.is_a?(String)
|
14
|
+
raise InvalidQueryError, "Selected columns must be an array of strings" unless selected_columns.is_a?(Array) && selected_columns.all? { |col| col.is_a?(String) }
|
15
|
+
|
16
|
+
begin
|
17
|
+
# Check if the current depth exceeds the maximum depth limit
|
18
|
+
if current_depth > max_depth
|
19
|
+
return { error: "Maximum recursion depth exceeded" }
|
20
|
+
end
|
21
|
+
|
22
|
+
# Convert filters hash into SQL conditions
|
23
|
+
filter_conditions = filters.map { |key, value| "#{key} = '#{value}'" }.join(' AND ')
|
24
|
+
|
25
|
+
# Convert selected_columns array into comma-separated column names
|
26
|
+
selected_columns_str = selected_columns.empty? ? '*' : selected_columns.join(', ')
|
27
|
+
|
28
|
+
# Constructing the recursive query with additional filters and selected columns
|
29
|
+
query = <<-SQL
|
30
|
+
WITH RECURSIVE hierarchy AS (
|
31
|
+
SELECT #{selected_columns_str}, 1 AS depth FROM #{table_name} WHERE #{parent_matching_column} = #{start_id} #{filter_conditions}
|
32
|
+
UNION ALL
|
33
|
+
SELECT #{selected_columns_str}, h.depth + 1 FROM #{table_name} t
|
34
|
+
JOIN hierarchy h ON t.#{sub_matching_column} = h.#{parent_matching_column} WHERE h.depth < #{max_depth}
|
35
|
+
)
|
36
|
+
SELECT #{selected_columns_str} FROM hierarchy;
|
37
|
+
SQL
|
38
|
+
|
39
|
+
# Executing the query and return the result
|
40
|
+
ActiveRecord::Base.connection.execute(query)
|
41
|
+
|
42
|
+
rescue ActiveRecord::StatementInvalid => e
|
43
|
+
if e.message.include?("statement timeout")
|
44
|
+
# Handle statement timeout error
|
45
|
+
error_message = "Statement Timeout: #{e.message}"
|
46
|
+
else
|
47
|
+
# Handle other errors
|
48
|
+
error_message = "Error: #{e.message}"
|
49
|
+
end
|
50
|
+
|
51
|
+
# Return the error message as part of the response
|
52
|
+
{ error: error_message }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: recursifier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jana
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-03-09 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Recursifier is a Ruby gem designed to simplify and optimize recursive
|
14
|
+
querying of hierarchical data in PostgreSQL databases, particularly within Ruby
|
15
|
+
on Rails applications. It provides a convenient interface for executing recursive
|
16
|
+
queries, allowing users to fetch hierarchical data from their database tables in
|
17
|
+
a hierarchical manner.
|
18
|
+
email:
|
19
|
+
- shanmugamjanarthan24@gmail.com
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- "./lib/recursifier.rb"
|
25
|
+
- "./lib/recursifier/version.rb"
|
26
|
+
homepage: https://github.com/janarthanan-shanmugam/recursifier
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata:
|
30
|
+
allowed_push_host: https://rubygems.org
|
31
|
+
homepage_uri: https://github.com/janarthanan-shanmugam/recursifier
|
32
|
+
source_code_uri: https://github.com/janarthanan-shanmugam/recursifier
|
33
|
+
changelog_uri: https://github.com/janarthanan-shanmugam/recursifier
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 2.6.0
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubygems_version: 3.4.20
|
50
|
+
signing_key:
|
51
|
+
specification_version: 4
|
52
|
+
summary: This gem is used to return the recursive data from the database in optimal
|
53
|
+
way
|
54
|
+
test_files: []
|