jungle_path 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +21 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +22 -0
- data/README.md +5 -0
- data/jungle_path.gemspec +43 -0
- data/lib/jungle_path/api/helpers/auth.rb +45 -0
- data/lib/jungle_path/api/helpers/auth_local_user.rb +284 -0
- data/lib/jungle_path/api/helpers/auth_old.rb +232 -0
- data/lib/jungle_path/api/helpers/data_cache.rb +20 -0
- data/lib/jungle_path/api/helpers/defaults.rb +83 -0
- data/lib/jungle_path/api/helpers/logging.rb +36 -0
- data/lib/jungle_path/api/helpers/query_filters.rb +15 -0
- data/lib/jungle_path/api/helpers/rescues.rb +15 -0
- data/lib/jungle_path/api/helpers/result.rb +16 -0
- data/lib/jungle_path/api/helpers/standard_apis.rb +280 -0
- data/lib/jungle_path/api/helpers.rb +16 -0
- data/lib/jungle_path/api/template.erb +35 -0
- data/lib/jungle_path/api.rb +5 -0
- data/lib/jungle_path/app/a.gitignore +1 -0
- data/lib/jungle_path/app/api/server_base.rb +95 -0
- data/lib/jungle_path/app/api/server_custom.rb +121 -0
- data/lib/jungle_path/app/api/server_gen.rb +11 -0
- data/lib/jungle_path/app/auth/authorization.rb +96 -0
- data/lib/jungle_path/app/config/a.gitignore +1 -0
- data/lib/jungle_path/app/config/config.rb +240 -0
- data/lib/jungle_path/app/config/override.rb +3 -0
- data/lib/jungle_path/app/config.ru +28 -0
- data/lib/jungle_path/app/logs/log_files_go_here +0 -0
- data/lib/jungle_path/app/run.sh +4 -0
- data/lib/jungle_path/app/schemas/schema.rb +21 -0
- data/lib/jungle_path/app/schemas/schema_all_in_one.rb +181 -0
- data/lib/jungle_path/app.rb +8 -0
- data/lib/jungle_path/authentication/auth_provider/default.rb +83 -0
- data/lib/jungle_path/authentication/auth_provider.rb +7 -0
- data/lib/jungle_path/authentication/data_provider/default.rb +144 -0
- data/lib/jungle_path/authentication/data_provider.rb +7 -0
- data/lib/jungle_path/authentication/helpers.rb +19 -0
- data/lib/jungle_path/authentication/identity.rb +30 -0
- data/lib/jungle_path/authentication/password_hash.rb +124 -0
- data/lib/jungle_path/authentication.rb +9 -0
- data/lib/jungle_path/authorization/filter.rb +106 -0
- data/lib/jungle_path/authorization/paths.rb +71 -0
- data/lib/jungle_path/authorization.rb +5 -0
- data/lib/jungle_path/cache.rb +36 -0
- data/lib/jungle_path/config.rb +65 -0
- data/lib/jungle_path/controller/authentication.rb +129 -0
- data/lib/jungle_path/controller/base.rb +193 -0
- data/lib/jungle_path/controller/helpers.rb +47 -0
- data/lib/jungle_path/controller/template.erb +14 -0
- data/lib/jungle_path/controller.rb +7 -0
- data/lib/jungle_path/db_access/import/db_dir.rb +74 -0
- data/lib/jungle_path/db_access/import/delete.rb +30 -0
- data/lib/jungle_path/db_access/import/insert.rb +168 -0
- data/lib/jungle_path/db_access/import/schema.rb +34 -0
- data/lib/jungle_path/db_access/import/select.rb +68 -0
- data/lib/jungle_path/db_access/import.rb +15 -0
- data/lib/jungle_path/db_access/io/chunked_file_reader.rb +62 -0
- data/lib/jungle_path/db_access/io/config.rb +19 -0
- data/lib/jungle_path/db_access/io/copy.rb +73 -0
- data/lib/jungle_path/db_access/io/db.rb +82 -0
- data/lib/jungle_path/db_access/io/delete.rb +23 -0
- data/lib/jungle_path/db_access/io/init_db.rb +39 -0
- data/lib/jungle_path/db_access/io/insert.rb +24 -0
- data/lib/jungle_path/db_access/io/schema.rb +21 -0
- data/lib/jungle_path/db_access/io/select.rb +44 -0
- data/lib/jungle_path/db_access/io/update.rb +36 -0
- data/lib/jungle_path/db_access/io.rb +104 -0
- data/lib/jungle_path/db_model/column.rb +186 -0
- data/lib/jungle_path/db_model/params.rb +60 -0
- data/lib/jungle_path/db_model/schema.rb +100 -0
- data/lib/jungle_path/db_model/string.rb +9 -0
- data/lib/jungle_path/db_model/table.rb +307 -0
- data/lib/jungle_path/db_model.rb +34 -0
- data/lib/jungle_path/exceptions.rb +10 -0
- data/lib/jungle_path/gen/api.rb +52 -0
- data/lib/jungle_path/gen/controller.rb +0 -0
- data/lib/jungle_path/gen/db.rb +0 -0
- data/lib/jungle_path/gen/schema.rb +47 -0
- data/lib/jungle_path/gen/schema_tree/filter.rb +33 -0
- data/lib/jungle_path/gen/schema_tree/match_columns.rb +54 -0
- data/lib/jungle_path/gen/schema_tree/match_table_data.rb +22 -0
- data/lib/jungle_path/gen/schema_tree/match_tables.rb +70 -0
- data/lib/jungle_path/gen/schema_tree/node.rb +39 -0
- data/lib/jungle_path/gen/schema_tree.rb +105 -0
- data/lib/jungle_path/gen.rb +9 -0
- data/lib/jungle_path/json/base.rb +29 -0
- data/lib/jungle_path/json/time.rb +8 -0
- data/lib/jungle_path/json.rb +6 -0
- data/lib/jungle_path/logging.rb +23 -0
- data/lib/jungle_path/query/alias_info.rb +16 -0
- data/lib/jungle_path/query/engine.rb +878 -0
- data/lib/jungle_path/query/entity.rb +141 -0
- data/lib/jungle_path/query/field.rb +28 -0
- data/lib/jungle_path/query/field_primary_key.rb +27 -0
- data/lib/jungle_path/query/filter.rb +34 -0
- data/lib/jungle_path/query/float_value.rb +16 -0
- data/lib/jungle_path/query/from.rb +33 -0
- data/lib/jungle_path/query/int_value.rb +16 -0
- data/lib/jungle_path/query/limit.rb +19 -0
- data/lib/jungle_path/query/nested_hash_sorter.rb +94 -0
- data/lib/jungle_path/query/operator.rb +17 -0
- data/lib/jungle_path/query/query.rb +23 -0
- data/lib/jungle_path/query/sort_field.rb +34 -0
- data/lib/jungle_path/query/sql_string.rb +145 -0
- data/lib/jungle_path/query/string_value.rb +16 -0
- data/lib/jungle_path/query.rb +19 -0
- data/lib/jungle_path/rack/basic_credentials.rb +70 -0
- data/lib/jungle_path/rack/json_body_parser.rb +41 -0
- data/lib/jungle_path/rack.rb +6 -0
- data/lib/jungle_path/schema/auth.rb +83 -0
- data/lib/jungle_path/schema/base.rb +6 -0
- data/lib/jungle_path/schema/db.rb +10 -0
- data/lib/jungle_path/schema/version.rb +19 -0
- data/lib/jungle_path/schema.rb +8 -0
- data/lib/jungle_path/sql/auth_local_user.rb +5 -0
- data/lib/jungle_path/sql/general.rb +10 -0
- data/lib/jungle_path/sql/helpers.rb +11 -0
- data/lib/jungle_path/sql/key.rb +107 -0
- data/lib/jungle_path/sql/query_filter.rb +5 -0
- data/lib/jungle_path/sql/role.rb +5 -0
- data/lib/jungle_path/sql/user.rb +35 -0
- data/lib/jungle_path/sql/user_role.rb +5 -0
- data/lib/jungle_path/sql.rb +12 -0
- data/lib/jungle_path.rb +13 -0
- data/test.rb +33 -0
- data/test2.rb +15 -0
- metadata +200 -0
@@ -0,0 +1,141 @@
|
|
1
|
+
# query.rb
|
2
|
+
#require 'pry-byebug'
|
3
|
+
#require 'pp'
|
4
|
+
#use: binding.pry where you want a break point.
|
5
|
+
require 'set'
|
6
|
+
|
7
|
+
module JunglePath
|
8
|
+
module Query
|
9
|
+
class Entity
|
10
|
+
attr_accessor :name, :alias_, :node, :parent, :symbol, :fields, :filter, :sort, :limit, :offset, :field_node, :join_field_override
|
11
|
+
attr_reader :left_join
|
12
|
+
def initialize(token, alias_, node, parent, symbol)
|
13
|
+
@name = Entity.get_name_from_token(token)
|
14
|
+
if token[-1] == "@"
|
15
|
+
@left_join = true
|
16
|
+
else
|
17
|
+
@left_join = false
|
18
|
+
end
|
19
|
+
if token.include? '.'
|
20
|
+
@join_field_override = token.split('.')[1]
|
21
|
+
if @join_field_override[-1] == '@'
|
22
|
+
@join_field_override = @join_field_override[0..-2]
|
23
|
+
end
|
24
|
+
else
|
25
|
+
@join_field_override = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
@alias_ = alias_
|
29
|
+
@node = node
|
30
|
+
@parent = parent
|
31
|
+
@symbol = symbol
|
32
|
+
@fields = nil
|
33
|
+
@filter = nil
|
34
|
+
@sort = nil
|
35
|
+
@limit = nil
|
36
|
+
@offset = nil
|
37
|
+
@field_node = nil #If this entity object is created from within the field list put the field's node here.
|
38
|
+
end
|
39
|
+
|
40
|
+
def is_entity?
|
41
|
+
true
|
42
|
+
end
|
43
|
+
|
44
|
+
def find_field(name)
|
45
|
+
if fields
|
46
|
+
fields.each do |field|
|
47
|
+
if field.name == name
|
48
|
+
return field
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
|
55
|
+
def find_entity(name)
|
56
|
+
field = find_field(name)
|
57
|
+
if field
|
58
|
+
return field
|
59
|
+
end
|
60
|
+
|
61
|
+
if fields
|
62
|
+
fields.each do |f|
|
63
|
+
if f.is_entity?
|
64
|
+
field = f.find_entity(name)
|
65
|
+
if field
|
66
|
+
return f
|
67
|
+
#return field
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
field
|
73
|
+
end
|
74
|
+
|
75
|
+
def find_symbol
|
76
|
+
symbol = node.symbol
|
77
|
+
p = parent
|
78
|
+
while true do
|
79
|
+
if p.fields.count == p.fields_that_are_entities_count and p.parent
|
80
|
+
symbol = p.node.symbol
|
81
|
+
p = p.parent
|
82
|
+
else
|
83
|
+
break
|
84
|
+
end
|
85
|
+
end
|
86
|
+
symbol
|
87
|
+
end
|
88
|
+
|
89
|
+
def find_parent_alias
|
90
|
+
#binding.pry
|
91
|
+
alias_ = nil
|
92
|
+
p = parent
|
93
|
+
while p and p.fields.count == p.fields_that_are_entities_count do
|
94
|
+
p = p.parent
|
95
|
+
end
|
96
|
+
alias_ = p.alias_ if p
|
97
|
+
alias_ = alias_.to_sym if alias_
|
98
|
+
alias_
|
99
|
+
end
|
100
|
+
|
101
|
+
def fields_that_are_entities_count
|
102
|
+
count = 0
|
103
|
+
fields.each do |field|
|
104
|
+
if field.is_entity?
|
105
|
+
count += 1
|
106
|
+
end
|
107
|
+
end
|
108
|
+
count
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.get_name_from_token(token)
|
112
|
+
if token[-1] == "@"
|
113
|
+
token = token[0..-2]
|
114
|
+
end
|
115
|
+
if token.include? '.'
|
116
|
+
token = token.split('.')[0]
|
117
|
+
end
|
118
|
+
token = token.downcase.to_sym
|
119
|
+
end
|
120
|
+
|
121
|
+
def self.select_node_from_array_by_token(nodes, token)
|
122
|
+
# find closest matching node.
|
123
|
+
# see gen_node_tree.rb lines 60+...
|
124
|
+
node = nodes[0] # default
|
125
|
+
if token.include? '.'
|
126
|
+
name = token.split('.')[1]
|
127
|
+
if name[-1] == '@'
|
128
|
+
name = name[0..-2]
|
129
|
+
end
|
130
|
+
nodes.each do |n|
|
131
|
+
if n.child_table_join_column_name.to_s.start_with?(name)
|
132
|
+
node = n #found match
|
133
|
+
break
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
node #return node.
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# query.rb
|
2
|
+
#require 'pry-byebug'
|
3
|
+
#require 'pp'
|
4
|
+
#use: binding.pry where you want a break point.
|
5
|
+
require 'set'
|
6
|
+
|
7
|
+
module JunglePath
|
8
|
+
module Query
|
9
|
+
class Field
|
10
|
+
attr_accessor :name, :node, :parent
|
11
|
+
def initialize(name, node, parent, is_secure = false)
|
12
|
+
@name = name.downcase.to_sym
|
13
|
+
@node = node
|
14
|
+
@parent = parent
|
15
|
+
@is_secure = is_secure
|
16
|
+
end
|
17
|
+
def is_entity?
|
18
|
+
false
|
19
|
+
end
|
20
|
+
def is_primary_key_column?
|
21
|
+
false
|
22
|
+
end
|
23
|
+
def is_secure?
|
24
|
+
@is_secure
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# query.rb
|
2
|
+
#require 'pry-byebug'
|
3
|
+
#require 'pp'
|
4
|
+
#use: binding.pry where you want a break point.
|
5
|
+
require 'set'
|
6
|
+
|
7
|
+
module JunglePath
|
8
|
+
module Query
|
9
|
+
class FieldPrimaryKey
|
10
|
+
attr_accessor :name, :node, :parent
|
11
|
+
def initialize(name, node, parent)
|
12
|
+
@name = name.downcase.to_sym
|
13
|
+
@node = node
|
14
|
+
@parent = parent
|
15
|
+
end
|
16
|
+
def is_entity?
|
17
|
+
false
|
18
|
+
end
|
19
|
+
def is_primary_key_column?
|
20
|
+
true
|
21
|
+
end
|
22
|
+
def is_secure?
|
23
|
+
false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# query.rb
|
2
|
+
#require 'pry-byebug'
|
3
|
+
#require 'pp'
|
4
|
+
#use: binding.pry where you want a break point.
|
5
|
+
require 'set'
|
6
|
+
|
7
|
+
module JunglePath
|
8
|
+
module Query
|
9
|
+
class Filter
|
10
|
+
attr_reader :table_name, :pk_filter_query, :not_in
|
11
|
+
def initialize table_name, pk_filter_query, not_in=false
|
12
|
+
@table_name = table_name.to_sym
|
13
|
+
@pk_filter_query = pk_filter_query
|
14
|
+
@not_in = not_in
|
15
|
+
end
|
16
|
+
|
17
|
+
def fk_in_query table_alias, fk_column_name
|
18
|
+
if @not_in
|
19
|
+
"#{table_alias}.#{fk_column_name} not in (#{@pk_filter_query})"
|
20
|
+
else
|
21
|
+
"#{table_alias}.#{fk_column_name} in (#{@pk_filter_query})"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_hash
|
26
|
+
{table_name: @table_name, pk_filter_query: @pk_filter_query, not_in: @not_in}
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_h
|
30
|
+
to_hash
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# query.rb
|
2
|
+
#require 'pry-byebug'
|
3
|
+
#require 'pp'
|
4
|
+
#use: binding.pry where you want a break point.
|
5
|
+
require 'set'
|
6
|
+
|
7
|
+
module JunglePath
|
8
|
+
module Query
|
9
|
+
class FloatValue
|
10
|
+
#attr_accessor :value
|
11
|
+
def self.parse(value)
|
12
|
+
value.to_f
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# query.rb
|
2
|
+
#require 'pry-byebug'
|
3
|
+
#require 'pp'
|
4
|
+
#use: binding.pry where you want a break point.
|
5
|
+
require 'set'
|
6
|
+
|
7
|
+
module JunglePath
|
8
|
+
module Query
|
9
|
+
class From
|
10
|
+
attr_reader :join_text, :table_name, :table_alias, :on_a_column_name, :on_b_alias, :on_b_column_name
|
11
|
+
def initialize join_text, table_name, table_alias, on_a_column_name=nil, on_b_alias=nil, on_b_column_name=nil
|
12
|
+
@join_text = join_text
|
13
|
+
@table_name = table_name.to_sym
|
14
|
+
@table_alias = table_alias
|
15
|
+
@on_a_column_name = on_a_column_name
|
16
|
+
@on_b_alias = on_b_alias
|
17
|
+
@on_b_column_name = on_b_column_name
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
if join_text == "" or join_text == nil
|
22
|
+
"\"#{@table_name}\" #{@table_alias}"
|
23
|
+
else
|
24
|
+
" #{@join_text} \"#{@table_name}\" #{@table_alias} on #{@table_alias}.#{@on_a_column_name} = #{@on_b_alias}.#{@on_b_column_name}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_str
|
29
|
+
to_s
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# query.rb
|
2
|
+
#require 'pry-byebug'
|
3
|
+
#require 'pp'
|
4
|
+
#use: binding.pry where you want a break point.
|
5
|
+
require 'set'
|
6
|
+
|
7
|
+
module JunglePath
|
8
|
+
module Query
|
9
|
+
class IntValue
|
10
|
+
#attr_accessor :value
|
11
|
+
def self.parse(value)
|
12
|
+
value.to_i
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# query.rb
|
2
|
+
#require 'pry-byebug'
|
3
|
+
#require 'pp'
|
4
|
+
#use: binding.pry where you want a break point.
|
5
|
+
|
6
|
+
module JunglePath
|
7
|
+
module Query
|
8
|
+
class Limit
|
9
|
+
#attr_accessor :value
|
10
|
+
def self.parse (token)
|
11
|
+
value = token[1..-2].strip
|
12
|
+
if value =~ /^[0-9]*$/
|
13
|
+
return value.to_i
|
14
|
+
end
|
15
|
+
raise "Found invalid limit or offset: #{token}. Limits and offsets must be integers. Such as [10]."
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
module JunglePath
|
2
|
+
module Query
|
3
|
+
class NestedHashSorter
|
4
|
+
def initialize(paths, sort_orders)
|
5
|
+
@paths = paths
|
6
|
+
@sort_orders = sort_orders
|
7
|
+
end
|
8
|
+
|
9
|
+
def sort(a, b)
|
10
|
+
#value = get_array_left(a, b) <=> get_array_right(a, b)
|
11
|
+
#can't do the above if some of the array values can be nil. Instead look at each array item individually and handle any nils:
|
12
|
+
#puts 'in sort'
|
13
|
+
value = sort_handle_nils(get_array_left(a, b), get_array_right(a, b))
|
14
|
+
value
|
15
|
+
end
|
16
|
+
|
17
|
+
def sort_handle_nils(array_a, array_b)
|
18
|
+
#puts 'in sort_handle_nils'
|
19
|
+
#puts "array_a: #{array_a}."
|
20
|
+
#puts "array_b: #{array_b}."
|
21
|
+
i = 0
|
22
|
+
value = 0
|
23
|
+
while i < array_a.length and value == 0
|
24
|
+
v = 0
|
25
|
+
a = array_a[i]
|
26
|
+
b = array_b[i]
|
27
|
+
#puts "a: #{a}."
|
28
|
+
#puts "b: #{b}."
|
29
|
+
a = 1 if a.class == TrueClass
|
30
|
+
a = 0 if a.class == FalseClass
|
31
|
+
b = 1 if b.class == TrueClass
|
32
|
+
b = 0 if b.class == FalseClass
|
33
|
+
if a == nil and b == nil
|
34
|
+
v = 0
|
35
|
+
elsif a == nil
|
36
|
+
v = -1
|
37
|
+
elsif b == nil
|
38
|
+
v = 1
|
39
|
+
else
|
40
|
+
v = a <=> b
|
41
|
+
end
|
42
|
+
value = v
|
43
|
+
i = i + 1
|
44
|
+
end
|
45
|
+
value
|
46
|
+
end
|
47
|
+
|
48
|
+
def get_array_left(a, b)
|
49
|
+
values = []
|
50
|
+
@paths.each_index do |i|
|
51
|
+
keys = @paths[i]
|
52
|
+
sort_order = @sort_orders[i]
|
53
|
+
if sort_order == :asc
|
54
|
+
values << get_value(a, keys)
|
55
|
+
else
|
56
|
+
values << get_value(b, keys)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
#puts "left values: #{values}."
|
60
|
+
values
|
61
|
+
end
|
62
|
+
|
63
|
+
def get_array_right(a, b)
|
64
|
+
values = []
|
65
|
+
@paths.each_index do |i|
|
66
|
+
keys = @paths[i]
|
67
|
+
sort_order = @sort_orders[i]
|
68
|
+
if sort_order == :asc
|
69
|
+
values << get_value(b, keys)
|
70
|
+
else
|
71
|
+
values << get_value(a, keys)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
#puts "right values: #{values}."
|
75
|
+
values
|
76
|
+
end
|
77
|
+
|
78
|
+
def get_value(hash, keys)
|
79
|
+
#extract a value from a nested hash
|
80
|
+
#puts "get_value:\n hash: #{hash}\n keys: #{keys}."
|
81
|
+
#puts ""
|
82
|
+
h = hash
|
83
|
+
keys.each do |key|
|
84
|
+
if h.class == Array
|
85
|
+
h = h[0][key]
|
86
|
+
else
|
87
|
+
h = h[key]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
h
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# query.rb
|
2
|
+
#require 'pry-byebug'
|
3
|
+
#require 'pp'
|
4
|
+
#use: binding.pry where you want a break point.
|
5
|
+
require 'set'
|
6
|
+
require 'jungle_path/query/engine'
|
7
|
+
|
8
|
+
module JunglePath
|
9
|
+
module Query
|
10
|
+
class Operator
|
11
|
+
#attr_accessor :value
|
12
|
+
def self.parse(value)
|
13
|
+
JunglePath::Query::Engine.operator_map[value]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# query.rb
|
2
|
+
#require 'pry-byebug'
|
3
|
+
#require 'pp'
|
4
|
+
#use: binding.pry where you want a break point.
|
5
|
+
require 'set'
|
6
|
+
|
7
|
+
module JunglePath
|
8
|
+
module Query
|
9
|
+
class Query
|
10
|
+
attr_reader :root, :sql, :values, :aliases, :symbols, :sort_ids, :primary_key_field_count, :apply_limit_offset_to_sql
|
11
|
+
def initialize(root, sql, values, aliases, symbols, sort_ids, primary_key_field_count, apply_limit_offset_to_sql=false)
|
12
|
+
@root = root
|
13
|
+
@sql = sql
|
14
|
+
@values = values
|
15
|
+
@aliases = aliases
|
16
|
+
@symbols = symbols
|
17
|
+
@sort_ids = sort_ids
|
18
|
+
@primary_key_field_count = primary_key_field_count
|
19
|
+
@apply_limit_offset_to_sql = apply_limit_offset_to_sql
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# query.rb
|
2
|
+
#require 'pry-byebug'
|
3
|
+
#require 'pp'
|
4
|
+
#use: binding.pry where you want a break point.
|
5
|
+
|
6
|
+
module JunglePath
|
7
|
+
module Query
|
8
|
+
class SortField
|
9
|
+
attr_accessor :string_value, :field_name, :entities, :sort # :asc or :desc
|
10
|
+
#def initialize(string_value, field_name, entities, sort_order=nil)
|
11
|
+
def initialize(string_value, field_name, entities, sort=nil)
|
12
|
+
@string_value = string_value
|
13
|
+
@field_name = field_name
|
14
|
+
@entities = entities
|
15
|
+
if sort
|
16
|
+
@sort = sort.to_sym
|
17
|
+
else
|
18
|
+
@sort = :asc
|
19
|
+
end
|
20
|
+
#puts "sort_field.string_value: #{@string_value}."
|
21
|
+
#puts "sort_field.field_name: #{@field_name}."
|
22
|
+
#puts "sort_field.entities: #{@entities}."
|
23
|
+
end
|
24
|
+
|
25
|
+
def value
|
26
|
+
if @sort == "asc"
|
27
|
+
@string_value
|
28
|
+
else
|
29
|
+
"#{@string_value} #{@sort}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# query.rb
|
2
|
+
#require 'pry-byebug'
|
3
|
+
#require 'pp'
|
4
|
+
#use: binding.pry where you want a break point.
|
5
|
+
require 'set'
|
6
|
+
|
7
|
+
module JunglePath
|
8
|
+
module Query
|
9
|
+
class SQLString
|
10
|
+
def self.generate(engine, entity, select=nil, from=nil, where=nil, sort=nil, sort_ids=nil, aliases=nil, symbols=nil, primary_key_field_count=nil)
|
11
|
+
primary_key_field_count = [] unless primary_key_field_count
|
12
|
+
aliases = {} unless aliases
|
13
|
+
symbols = [] unless symbols
|
14
|
+
outer = true unless select
|
15
|
+
select = [] unless select
|
16
|
+
from = [] unless from
|
17
|
+
where = [] unless where
|
18
|
+
sort = [] unless sort
|
19
|
+
sort_ids = [] unless sort_ids # need to sort sql query in id order for each table with fields in select list. This is so objects with same ID can be combinded.
|
20
|
+
sort_original = [] unless sort_original
|
21
|
+
|
22
|
+
generate_from(from, entity, aliases, engine.tables, symbols)
|
23
|
+
generate_fields(engine, entity, select, from, where, sort, sort_ids, aliases, symbols, primary_key_field_count)
|
24
|
+
generate_filter(entity, where)
|
25
|
+
generate_sort(entity, sort)
|
26
|
+
|
27
|
+
indent = "\n "
|
28
|
+
|
29
|
+
if outer
|
30
|
+
hook_in_filters engine, select, from, where, sort, entity
|
31
|
+
sql = generate_sql(select, from, where, sort, entity, engine.apply_limit_offset_to_sql)
|
32
|
+
return sql, aliases, symbols, sort_ids, primary_key_field_count
|
33
|
+
else
|
34
|
+
return select, from
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.generate_from(from, entity, aliases, tables, symbols)
|
39
|
+
if from.length == 0
|
40
|
+
from << From.new(nil, entity.node.name, entity.alias_)
|
41
|
+
aliases[entity.alias_.to_sym] = AliasInfo.new(entity.name, entity.alias_.to_sym, nil, tables[entity.node.name.to_sym].primary_key_columns.count)
|
42
|
+
else
|
43
|
+
if entity.left_join
|
44
|
+
join_text = "left join"
|
45
|
+
else
|
46
|
+
join_text = "join"
|
47
|
+
end
|
48
|
+
from << From.new(join_text, entity.node.child_table_name, entity.alias_, entity.node.child_table_join_column_name, entity.parent.alias_, entity.node.parent_table_join_column_name)
|
49
|
+
aliases[entity.alias_.to_sym] = AliasInfo.new(entity.name, entity.alias_.to_sym, entity.find_parent_alias, tables[entity.node.child_table_name.to_sym].primary_key_columns.count)
|
50
|
+
#puts "aliases: #{aliases}."
|
51
|
+
if entity.fields.count > entity.fields_that_are_entities_count
|
52
|
+
symbols << entity.find_symbol
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.generate_fields(engine, entity, select, from, where, sort, sort_ids, aliases, symbols, primary_key_field_count)
|
58
|
+
if entity.fields and entity.fields.length > 0
|
59
|
+
entity.fields.each do |field|
|
60
|
+
if field.is_entity?
|
61
|
+
select, from = generate(engine, field, select, from, where, sort, sort_ids, aliases, symbols, primary_key_field_count)
|
62
|
+
else
|
63
|
+
if field.is_primary_key_column?
|
64
|
+
sort_ids << "#{entity.alias_}.#{field.name}".to_sym
|
65
|
+
end
|
66
|
+
puts "#{entity.alias_}.#{field.name} as \"#{entity.alias_}.#{field.name}\" is_secure? #{field.is_secure?}."
|
67
|
+
if !field.is_secure? or (field.is_secure? and engine.user.auth.is_root? or engine.user.auth.is_user_admin?)
|
68
|
+
select << "#{entity.alias_}.#{field.name} as \"#{entity.alias_}.#{field.name}\""
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
#puts "entity.name: #{entity.name}."
|
73
|
+
#else
|
74
|
+
# select = "*"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.generate_filter(entity, where)
|
79
|
+
if entity.filter and entity.filter.length > 0
|
80
|
+
where << "and" if where.length > 0
|
81
|
+
entity.filter.each do |filter|
|
82
|
+
where << filter
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.generate_sort(entity, sort)
|
88
|
+
if entity.sort and entity.sort.length > 0
|
89
|
+
#sort << ", " if sort.length > 0
|
90
|
+
entity.sort.each do |s|
|
91
|
+
sort << s.value
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.hook_in_filters(engine, select, from, where, sort, entity)
|
97
|
+
puts "hook_in_filters QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFf"
|
98
|
+
#binding.pry
|
99
|
+
if engine.user and engine.user.query_filters
|
100
|
+
engine.user.query_filters.each do |filter|
|
101
|
+
puts "hook_in_filters: filter.table_name: #{filter.table_name}."
|
102
|
+
from.each do |frm|
|
103
|
+
puts "hook_in_filters: frm.table_name: #{frm.table_name}."
|
104
|
+
if frm.table_name == filter.table_name
|
105
|
+
table = engine.tables[frm.table_name]
|
106
|
+
where << "and" if where.length > 0
|
107
|
+
where << filter.fk_in_query(frm.table_alias, table.primary_key_columns.values[0].name)
|
108
|
+
# don't think I need this part:
|
109
|
+
#else
|
110
|
+
# table = engine.tables[frm.table_name]
|
111
|
+
# table.foreign_key_columns.values.each do |column|
|
112
|
+
# if column.foreign_key_table_name == filter.table_name
|
113
|
+
# where << "and" if where.length > 0
|
114
|
+
# where << filter.fk_in_query(frm.table_alias, column.name)
|
115
|
+
# end
|
116
|
+
# end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def self.generate_sql(select, from, where, sort, entity, apply_limit_offset_to_sql)
|
124
|
+
lf = "\n"
|
125
|
+
indent_comma = ",\n "
|
126
|
+
sql = "select\n #{select.join(indent_comma)}\nfrom #{from.join(lf)}"
|
127
|
+
if where and where.length > 0
|
128
|
+
sql = sql + "\nwhere\n #{where.join(' ')}"
|
129
|
+
end
|
130
|
+
if sort and sort.length > 0
|
131
|
+
sql = sql + "\norder by\n #{sort.join(', ')}"
|
132
|
+
end
|
133
|
+
if apply_limit_offset_to_sql
|
134
|
+
if entity.limit and entity.limit > 0
|
135
|
+
sql = sql + "\nlimit #{entity.limit}"
|
136
|
+
end
|
137
|
+
if entity.offset and entity.offset > 0
|
138
|
+
sql = sql + "\noffset #{entity.offset}"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
sql
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# query.rb
|
2
|
+
#require 'pry-byebug'
|
3
|
+
#require 'pp'
|
4
|
+
#use: binding.pry where you want a break point.
|
5
|
+
require 'set'
|
6
|
+
|
7
|
+
module JunglePath
|
8
|
+
module Query
|
9
|
+
class StringValue #also used for date literals
|
10
|
+
#attr_accessor :value
|
11
|
+
def self.parse(value)
|
12
|
+
value[1..-2] #strip out beginning and ending quotes.
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module JunglePath
|
2
|
+
module Query
|
3
|
+
require 'jungle_path/query/alias_info'
|
4
|
+
require 'jungle_path/query/engine'
|
5
|
+
require 'jungle_path/query/entity'
|
6
|
+
require 'jungle_path/query/field_primary_key'
|
7
|
+
require 'jungle_path/query/field'
|
8
|
+
require 'jungle_path/query/filter'
|
9
|
+
require 'jungle_path/query/float_value'
|
10
|
+
require 'jungle_path/query/from'
|
11
|
+
require 'jungle_path/query/int_value'
|
12
|
+
require 'jungle_path/query/limit'
|
13
|
+
require 'jungle_path/query/operator'
|
14
|
+
require 'jungle_path/query/query'
|
15
|
+
require 'jungle_path/query/sort_field'
|
16
|
+
require 'jungle_path/query/sql_string'
|
17
|
+
require 'jungle_path/query/string_value'
|
18
|
+
end
|
19
|
+
end
|