Top Level Namespace

Defined Under Namespace

Modules: ActionController, Parser, RDL, Sequel Classes: ASTVisitor, ActiveRecord_Relation, BigDecimal, Class, DBType, Diagnostic, JoinTable, Module, Object, RDLRailtie, SeqIdent, SeqQualIdent, SequelDB, SimpleDelegator, Table, WrapCall

Instance Method Summary collapse

Instance Method Details

#build_string_from_precise_string(args) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/types/rails/active_record/sql-strings.rb', line 148

def build_string_from_precise_string(args)
  str = args[0]
  raise "Bad type!" unless str.is_a? RDL::Type::PreciseStringType
  # TODO: handles only non-interpolated strings for now
  base_query = str.vals[0]

  # Get rid of SQL functions here, that just ends up confusing the parser anyway
  base_query.gsub!('LOWER(', '(')

  counter = 1
  if args[1].is_a? RDL::Type::FiniteHashType
    # the query has named params
    args[1].elts.keys.each { |k| base_query.gsub!(":#{k}", counter.to_s); counter += 1 }
  else
    # the query has ? symbols
    args[1..-1].each { |t| base_query.sub!('?', counter.to_s); counter += 1}
  end
  base_query
end

#handle_sql_strings(trec, targs) ⇒ Object



93
94
95
96
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/types/rails/active_record/sql-strings.rb', line 93

def handle_sql_strings(trec, targs)
  parser = SQLParser::Parser.new

  case trec
  when RDL::Type::GenericType
    if trec.base.klass == ActiveRecord_Relation
      handle_sql_strings trec.params[0], targs
    elsif trec.base.klass == JoinTable
      # works only for the base class right now, need to extend for the params as well
      base_klass = trec.params[0]
      joined_with = trec.params[1]
      case joined_with
      when RDL::Type::UnionType
        joined_with.types.each do |klass|
          # add the joining association column on this
          sql_query = "SELECT * FROM `#{base_klass.name.tableize}` INNER JOIN `#{klass.name.tableize}` ON a.id = b.a_id WHERE #{build_string_from_precise_string(targs)}"
          # puts sql_query
          begin
            ast = parser.scan_str(sql_query)
          rescue Racc::ParseError => e
            # puts "There was a parse error with above query, moving on"
            return
          end
          search_cond = ast.query_expression.table_expression.where_clause.search_condition
          visitor = ASTVisitor.new base_klass.name.tableize, targs
          visitor.visit(search_cond)
        end
      else
        # TODO
        # puts "== TODO =="
      end
    else
      # puts "UNEXPECTED #{trec}, #{targs}"
    end
  when RDL::Type::NominalType
    base_klass = trec
    sql_query = "SELECT * FROM `#{base_klass.name.tableize}` WHERE #{build_string_from_precise_string(targs)}"
    # puts sql_query
    ast = parser.scan_str(sql_query)
    search_cond = ast.query_expression.table_expression.where_clause.search_condition
    visitor = ASTVisitor.new base_klass.name.tableize, targs
    visitor.visit(search_cond)
  when RDL::Type::SingletonType
    base_klass = trec
    sql_query = "SELECT * FROM `#{base_klass.val.to_s.tableize}` WHERE #{build_string_from_precise_string(targs)}"
    # puts sql_query
    ast = parser.scan_str(sql_query)
    search_cond = ast.query_expression.table_expression.where_clause.search_condition
    visitor = ASTVisitor.new base_klass.val.to_s.tableize, targs
    visitor.visit(search_cond)
  else
    # puts "UNEXPECTED #{trec}, #{targs}"
  end
end

#ObjectObject

needed due to type casting



45
# File 'lib/rdl/types/rdl_types.rb', line 45

RDL.type "Object", '__getobj__', "() -> self", wrap: false, effect: [:+, :+]