querylet-rails 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/querylet_rails/model/queryable.rb +75 -32
- data/lib/querylet_rails/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 302e8d10adf702f7225a143ff065b6db8bc67a126371f3f84e3113cf781a356d
|
4
|
+
data.tar.gz: 2e9b82dbbf9cc7d016a94392c8e75c5e22060fa6f362b80f1c65cad86f15962f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4c915bf73424ae790eddada5e967e2bb02037071ab00b76f40c4e30a444740374d7b3f830ea03994bee5f8a8ec9fdf4b32faa1804a379022f90e106d379b914
|
7
|
+
data.tar.gz: 35490fe8cde047492b1ff02d09bb8d15ef29e00575fe03138cf7a63c92b5c185b90d7eb1e90a5ad3743d796139578207440ee04e54ec7be8132fae531740a45c
|
@@ -4,7 +4,6 @@ module QueryletRails
|
|
4
4
|
module Model
|
5
5
|
module Queryable
|
6
6
|
extend ActiveSupport::Concern
|
7
|
-
# override this function
|
8
7
|
|
9
8
|
def query relative_path, data={}
|
10
9
|
self.class.query relative_path, data
|
@@ -34,18 +33,74 @@ module QueryletRails
|
|
34
33
|
self.class.select_paginate query, attrs, count
|
35
34
|
end
|
36
35
|
|
37
|
-
|
36
|
+
# This will fetch the current tenant id
|
37
|
+
def current_tenant_id
|
38
|
+
self.class.current_tenant_id
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_tenant_id
|
42
|
+
self.class.set_tenant_id
|
43
|
+
end
|
44
|
+
|
45
|
+
def reset_tenant_id
|
46
|
+
self.class.reset_tenant_id
|
47
|
+
end
|
48
|
+
|
49
|
+
# This will apply the current tenant_id on the creation of a new model
|
50
|
+
def initialize_tenant_id
|
51
|
+
if new_record?
|
52
|
+
if has_attribute?(:tenant_id) && self.tenant_id.nil?
|
53
|
+
self.tenant_id = current_tenant_id
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
included do
|
59
|
+
after_initialize :initialize_tenant_id
|
60
|
+
end
|
61
|
+
|
62
|
+
class_methods do
|
63
|
+
# override this function
|
38
64
|
def query_root
|
39
65
|
Rails.root
|
40
66
|
end
|
41
67
|
|
42
|
-
def
|
68
|
+
def _query_wrap_object template
|
69
|
+
<<-HEREDOC.chomp
|
70
|
+
(SELECT COALESCE(row_to_json(object_row),'{}'::json) FROM (
|
71
|
+
#{template.to_s.chomp}
|
72
|
+
) object_row);
|
73
|
+
HEREDOC
|
74
|
+
end
|
75
|
+
|
76
|
+
def _query_wrap_array template
|
77
|
+
<<-HEREDOC.chomp
|
78
|
+
(SELECT COALESCE(array_to_json(array_agg(row_to_json(array_row))),'[]'::json) FROM (
|
79
|
+
#{template.to_s.chomp}
|
80
|
+
) array_row);
|
81
|
+
HEREDOC
|
82
|
+
end
|
83
|
+
|
84
|
+
def current_tenant_id
|
85
|
+
self.connection.execute('SHOW my.tenant_id').getvalue(0, 0).to_i
|
86
|
+
end
|
87
|
+
|
88
|
+
def set_tenant_id new_tenant_id
|
89
|
+
raise "tenant_id not an valid value" unless new_tenant_id.is_a?(Integer)
|
90
|
+
self.connection.execute "SET SESSION my.tenant_id = #{new_tenant_id}"
|
91
|
+
end
|
92
|
+
|
93
|
+
def reset_tenant_id
|
94
|
+
self.connection.execute "RESET my.tenant_id"
|
95
|
+
end
|
96
|
+
|
97
|
+
def _query relative_path, data={}
|
43
98
|
file_path = self.query_root.join('app','queries',relative_path + '.sql')
|
44
99
|
template = File.read file_path
|
45
|
-
|
100
|
+
_query_compile_template template, data
|
46
101
|
end
|
47
102
|
|
48
|
-
def
|
103
|
+
def _query_compile_template template, data={}
|
49
104
|
querylet = Querylet::Querylet.new path: self.query_root.join('app','queries').to_s
|
50
105
|
begin
|
51
106
|
querylet.compile(template).call(data)
|
@@ -61,59 +116,47 @@ module QueryletRails
|
|
61
116
|
end
|
62
117
|
end
|
63
118
|
|
119
|
+
def query relative_path, data={}
|
120
|
+
_query relative_path, data
|
121
|
+
end
|
122
|
+
|
64
123
|
def select_value relative_path, data={}
|
65
|
-
sql =
|
124
|
+
sql = _query relative_path, data
|
66
125
|
self.connection.select_value sql
|
67
126
|
end
|
68
127
|
|
69
128
|
def select_values relative_path, data={}
|
70
|
-
sql =
|
129
|
+
sql = _query relative_path, data
|
71
130
|
self.connection.select_values sql
|
72
131
|
end
|
73
132
|
|
74
133
|
def select_array relative_path, data={}
|
75
|
-
|
76
|
-
sql =
|
134
|
+
sql = _query relative_path, data
|
135
|
+
sql = self._query_wrap_array sql
|
77
136
|
self.connection.select_value sql
|
78
137
|
end
|
79
138
|
|
80
139
|
def select_object relative_path, data={}
|
81
|
-
|
82
|
-
sql =
|
140
|
+
sql = _query relative_path, data
|
141
|
+
sql = self._query_wrap_object sql
|
83
142
|
self.connection.select_value sql
|
84
143
|
end
|
85
144
|
|
86
145
|
def select_all relative_path, data={}
|
87
|
-
sql =
|
146
|
+
sql = _query relative_path, data
|
88
147
|
self.connection.select_all sql
|
89
148
|
end
|
90
149
|
|
91
|
-
def query_wrap_object template
|
92
|
-
<<-HEREDOC.chomp
|
93
|
-
(SELECT COALESCE(row_to_json(object_row),'{}'::json) FROM (
|
94
|
-
#{template.to_s.chomp}
|
95
|
-
) object_row)
|
96
|
-
HEREDOC
|
97
|
-
end
|
98
|
-
|
99
|
-
def query_wrap_array template
|
100
|
-
<<-HEREDOC.chomp
|
101
|
-
(SELECT COALESCE(array_to_json(array_agg(row_to_json(array_row))),'[]'::json) FROM (
|
102
|
-
#{template.to_s.chomp}
|
103
|
-
) array_row)
|
104
|
-
HEREDOC
|
105
|
-
end
|
106
|
-
|
107
150
|
def select_paginate relative_path, data, count
|
108
151
|
file_path = self.query_root.join('app','queries',relative_path + '.sql')
|
109
152
|
template = File.read file_path
|
110
153
|
if count
|
111
|
-
sql = self.
|
112
|
-
sql = self.
|
154
|
+
sql = self._query_wrap_object template
|
155
|
+
sql = self._query_compile_template sql, data
|
113
156
|
self.connection.select_value sql
|
114
157
|
else
|
115
|
-
sql = self.
|
116
|
-
sql = self.
|
158
|
+
sql = self._query_wrap_array template
|
159
|
+
sql = self._query_compile_template sql, data
|
117
160
|
self.connection.select_value sql
|
118
161
|
end
|
119
162
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: querylet-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TeacherSeat
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: querylet
|
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
82
|
- !ruby/object:Gem::Version
|
83
83
|
version: '0'
|
84
84
|
requirements: []
|
85
|
-
rubygems_version: 3.1.
|
85
|
+
rubygems_version: 3.1.4
|
86
86
|
signing_key:
|
87
87
|
specification_version: 4
|
88
88
|
summary: Querylet Rails
|