Class: JunglePath::Query::SQLString

Inherits:
Object
  • Object
show all
Defined in:
lib/jungle_path/query/sql_string.rb

Class Method Summary collapse

Class Method Details

.generate(engine, entity, select = nil, from = nil, where = nil, sort = nil, sort_ids = nil, aliases = nil, symbols = nil, primary_key_field_count = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jungle_path/query/sql_string.rb', line 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)
  primary_key_field_count = [] unless primary_key_field_count
  aliases = {} unless aliases
  symbols = [] unless symbols
  outer = true unless select
  select = [] unless select
  from = [] unless from
  where = [] unless where
  sort = [] unless sort
  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.
  sort_original = [] unless sort_original

  generate_from(from, entity, aliases, engine.tables, symbols)
  generate_fields(engine, entity, select, from, where, sort, sort_ids, aliases, symbols, primary_key_field_count)
  generate_filter(entity, where)
  generate_sort(entity, sort)

  indent = "\n  "

  if outer
    hook_in_filters engine, select, from, where, sort, entity
    hook_in_table_filters engine, select, from, where, sort, entity
    sql = generate_sql(select, from, where, sort, entity, engine.apply_limit_offset_to_sql)
    return sql, aliases, symbols, sort_ids, primary_key_field_count
  else
    return select, from
  end
end

.generate_fields(engine, entity, select, from, where, sort, sort_ids, aliases, symbols, primary_key_field_count) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/jungle_path/query/sql_string.rb', line 58

def self.generate_fields(engine, entity, select, from, where, sort, sort_ids, aliases, symbols, primary_key_field_count)
  if entity.fields and entity.fields.length > 0
    entity.fields.each do |field|
      if field.is_entity?
        select, from = generate(engine, field, select, from, where, sort, sort_ids, aliases, symbols, primary_key_field_count)
      else
        if field.is_primary_key_column?
          sort_ids << "#{entity.alias_}.#{field.name}".to_sym
        end
        puts "#{entity.alias_}.#{field.name} as \"#{entity.alias_}.#{field.name}\" is_secure? #{field.is_secure?}."
        if !field.is_secure? or (field.is_secure? and engine.identity.authorization_filter.is_root? or engine.identity.authorization_filter.is_user_admin?)
          select << "#{entity.alias_}.#{field.name} as \"#{entity.alias_}.#{field.name}\""
        end
      end
    end
    #puts "entity.name: #{entity.name}."
  #else
  #  select = "*"
  end
end

.generate_filter(entity, where) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/jungle_path/query/sql_string.rb', line 79

def self.generate_filter(entity, where)
  if entity.filter and entity.filter.length > 0
    where << "and" if where.length > 0
    entity.filter.each do |filter|
      where << filter
    end
  end
end

.generate_from(from, entity, aliases, tables, symbols) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jungle_path/query/sql_string.rb', line 39

def self.generate_from(from, entity, aliases, tables, symbols)
  if from.length == 0
    from << From.new(nil, entity.node.name, entity.alias_, nil, nil, nil, entity.parameters)
    aliases[entity.alias_.to_sym] = AliasInfo.new(entity.name, entity.alias_.to_sym, nil, tables[entity.node.name.to_sym].primary_key_columns.count)
  else
    if entity.left_join
      join_text = "left join"
    else
      join_text = "join"
    end
    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, entity.parameters)
    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)
      #puts "aliases: #{aliases}."
    if entity.fields.count > entity.fields_that_are_entities_count
      symbols << entity.find_symbol
    end
  end
end

.generate_sort(entity, sort) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/jungle_path/query/sql_string.rb', line 88

def self.generate_sort(entity, sort)
  if entity.sort and entity.sort.length > 0
    #sort << ", " if sort.length > 0
    entity.sort.each do |s|
      sort << s.value
    end
  end
end

.generate_sql(select, from, where, sort, entity, apply_limit_offset_to_sql) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/jungle_path/query/sql_string.rb', line 150

def self.generate_sql(select, from, where, sort, entity, apply_limit_offset_to_sql)
  lf = "\n"
  indent_comma = ",\n  "
  sql = "select\n  #{select.join(indent_comma)}\nfrom #{from.join(lf)}"
  if where and where.length > 0
    sql = sql + "\nwhere\n  #{where.join(' ')}"
  end
  if sort and sort.length > 0
    sql =  sql + "\norder by\n  #{sort.join(', ')}"
  end
  if apply_limit_offset_to_sql
    if entity.limit and entity.limit > 0
      sql = sql + "\nlimit #{entity.limit}"
    end
    if entity.offset and entity.offset > 0
      sql = sql + "\noffset #{entity.offset}"
    end
  end
  sql
end

.hook_in_filters(engine, select, from, where, sort, entity) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/jungle_path/query/sql_string.rb', line 97

def self.hook_in_filters(engine, select, from, where, sort, entity)
  puts "hook_in_filters QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFf"
  #binding.pry
  if engine.identity and engine.identity.query_filters
    engine.identity.query_filters.each do |filter|
      puts "hook_in_filters: filter.table_name: #{filter.table_name}."
      from.each do |frm|
        puts "hook_in_filters: frm.table_name: #{frm.table_name}."
        if frm.table_name == filter.table_name
          table = engine.tables[frm.table_name]
          where << "and" if where.length > 0
          where << filter.fk_in_query(frm.table_alias, table.primary_key_columns.values[0].name)
        # don't think I need this part:
        #else
        #  table = engine.tables[frm.table_name]
        #  table.foreign_key_columns.values.each do |column|
        #    if column.foreign_key_table_name == filter.table_name
        #      where << "and" if where.length > 0
        #      where << filter.fk_in_query(frm.table_alias, column.name)
        #    end
        #  end
        end
      end
    end
  end
end

.hook_in_table_filters(engine, select, from, where, sort, entity) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/jungle_path/query/sql_string.rb', line 124

def self.hook_in_table_filters(engine, select, from, where, sort, entity)
  puts "hook_in_table_filters QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFf"
  #binding.pry
  if engine.identity and engine.identity.table_filters
    from.each do |frm|
      filter = engine.identity.table_filters[frm.table_name]
      if filter
        puts "hook_in_table_filters: frm.table_name: #{frm.table_name}."
        replacement_table = engine.tables[filter[:replacement]]
        puts "replacement_table: #{replacement_table}"
        table_replacement_text = nil
        if replacement_table.view
          puts "has view"
          #also run any pre query hook for view:
          replacement_table.view.run_pre_query_hook(engine.identity, replacement_table, engine.db, frm.parameters)
          table_replacement_text = replacement_table.view.build_call(engine.identity, replacement_table, frm.parameters)
        else
          puts "no view"
          table_replacement_text = "#{replacement_table.table_name}"
        end
        frm.table_replacement_text = table_replacement_text
      end
    end
  end
end