Class: Rquerypad
- Inherits:
-
Object
- Object
- Rquerypad
- Defined in:
- lib/rquerypad.rb
Class Method Summary collapse
- .options_key(obj, options) ⇒ Object
- .prepare(debug = $RQUERYPAD_DEBUG) ⇒ Object
- .prepare_with_debug ⇒ Object
- .process_inner_join(sql, inner_joins) ⇒ Object
Instance Method Summary collapse
- #abstract_association(str) ⇒ Object
- #cut_end_underscore(str) ⇒ Object
- #cut_end_underscore!(str) ⇒ Object
- #improve_conditions(options) ⇒ Object
- #improve_options!(option_hash) ⇒ Object
- #improve_ordergroup(fields) ⇒ Object
-
#initialize(obj) ⇒ Rquerypad
constructor
A new instance of Rquerypad.
- #process_single(key) ⇒ Object
- #raise_error(name, owner) ⇒ Object
-
#remove_dup_includes(options) ⇒ Object
suppose => :b => {:b => :c }, will remove => :b.
- #transfer_table_id(name, owner = @owner) ⇒ Object
- #transfer_table_name(name, owner = @owner) ⇒ Object
Constructor Details
#initialize(obj) ⇒ Rquerypad
Returns a new instance of Rquerypad.
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rquerypad.rb', line 115 def initialize(obj) @owner = obj @class_name = obj.to_s.scan(/(\w*::)*([^\(]*)/)[0][1] # #@table_name = @class_name.pluralize.underscore @table_name = obj.table_name @tnwithdot = @table_name + "." @new_include = [] @old_include = nil @inner_joins = [] end |
Class Method Details
.options_key(obj, options) ⇒ Object
109 110 111 112 113 |
# File 'lib/rquerypad.rb', line 109 def (obj, ) key = obj.to_s + .to_a.to_s key = MD5.hexdigest(key) if key.length > 100 key end |
.prepare(debug = $RQUERYPAD_DEBUG) ⇒ Object
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rquerypad.rb', line 11 def prepare(debug = $RQUERYPAD_DEBUG) return nil unless @prepared.nil? @prepared = true ActiveRecord::Base.class_eval %{ class << self def group_tables(options) group = [options[:group], scope(:find, :group) ].join(", ") return [] unless group && group.is_a?(String) group.scan(/([\.a-zA-Z_]+).?\./).flatten end def include_eager_group?(options, tables = nil) ((tables || group_tables(options)) - [table_name]).any? end def references_eager_loaded_tables?(options) include_eager_order?(options) || include_eager_conditions?(options) || include_eager_select?(options) || include_eager_group?(options) end end } ActiveRecord::Base.class_eval %{ require "rquerypad" class << self @@rquerypad_cache = {} alias_method(:old_find_4rqp, :find) unless method_defined?(:old_find_4rqp) alias_method(:old_cfswia_4rqp, :construct_finder_sql_with_included_associations) unless method_defined?(:old_cfswia_4rqp) VALID_FIND_OPTIONS << :inner_joins def find(*args) options = #{if [].respond_to?(:extract_options!) then "args.extract_options!" else "extract_options_from_args!(args)" end} if !options.empty? && (options.include?(:conditions) || options.include?(:group) || options.include?(:order)) # if (!options.empty? && (!options[:conditions].nil? || !options[:group].nil? || !options[:order].nil?)) #{"p 'original options:', options" if debug} cache_key = Rquerypad.options_key(self, options) #p "cache_key: #\{cache_key\}" #p "rquerypad_cache[cache_key]: #\{rquerypad_cache[cache_key]\}" if rquerypad_cache[cache_key].nil? #{"p 'process for:', options" if debug} options = Rquerypad.new(self).improve_options!(options) rquerypad_cache[cache_key] = Marshal.dump(options) #p "rquerypad_cache[cache_key]: #\{rquerypad_cache[cache_key]\}" else options = Marshal.load(rquerypad_cache[cache_key]) end #{"p 'new options:', options" if debug} end args += [options] unless options.empty? old_find_4rqp *args end def construct_finder_sql_with_included_associations(options, join_dependency) sql = old_cfswia_4rqp(options, join_dependency) #{"p('original sql', sql) unless options[:inner_joins].nil?" if debug} sql = Rquerypad.process_inner_join(sql, options[:inner_joins]) unless options[:inner_joins].nil? #{"p('new sql with inner join', sql) unless options[:inner_joins].nil?" if debug} sql end def rquerypad_cache @@rquerypad_cache end end } ActiveRecord::Calculations.class_eval %{ require "rquerypad" module ClassMethods @@rquerypad_cache = {} alias_method(:old_construct_calculation_sql_4rqp, :construct_calculation_sql) unless method_defined?(:old_construct_calculation_sql_4rqp) CALCULATIONS_OPTIONS << :inner_joins def construct_calculation_sql(operation, column_name, options) if !options.empty? && (options.include?(:conditions) || options.include?(:group) || options.include?(:order)) #{"p 'original options:', options" if debug} cache_key = Rquerypad.options_key(self, options) if rquerypad_cache[cache_key].nil? #{"p 'process for:', options" if debug} options = Rquerypad.new(self).improve_options!(options) rquerypad_cache[cache_key] = Marshal.dump(options) else options = Marshal.load(rquerypad_cache[cache_key]) end #{"p 'new options:', options" if debug} end sql = old_construct_calculation_sql_4rqp(operation, column_name, options) #{"p('original sql', sql) unless options[:inner_joins].nil?" if debug} sql = Rquerypad.process_inner_join(sql, options[:inner_joins]) unless options[:inner_joins].nil? #{"p('new sql with inner join', sql) unless options[:inner_joins].nil?" if debug} sql end def rquerypad_cache @@rquerypad_cache end end } end |
.prepare_with_debug ⇒ Object
8 9 10 |
# File 'lib/rquerypad.rb', line 8 def prepare_with_debug prepare(true) end |
.process_inner_join(sql, inner_joins) ⇒ Object
104 105 106 107 |
# File 'lib/rquerypad.rb', line 104 def process_inner_join(sql, inner_joins) inner_joins.each {|i| sql.gsub!(/LEFT OUTER JOIN [`]?#{i}[`]?/, "INNER JOIN #{i}")} unless inner_joins.empty? sql end |
Instance Method Details
#abstract_association(str) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/rquerypad.rb', line 247 def abstract_association(str) result = nil owner = @owner names = str.split(".") return str if names.size == 2 && names[0] == @table_name # #seperate assocations/tables and field tables, field = names[0..-2], names[-1] owners = [] # #get relevant owner for each table tables.each_index do |i| # #tables[i] = tables[i].pluralize if i == 0 owners[i] = owner else tname = cut_end_underscore(tables[i-1]) r = owners[i-1].reflections[tname.to_sym]. owners[i] = r[:class_name].nil? ? eval(owners[i-1].to_s.gsub(/\w*$/, "")+tname.singularize.camelize) : eval("#{r[:class_name]}") end end owners.reverse! tables.reverse! tables.each_index do |i| if tables[i][-1, 1] == "_" tables[i].reverse!.sub!("_", "").reverse! @inner_joins << transfer_table_name(tables[i], owners[i]) end end # #process special id field in a belongs_to association if field == "id" if owners[0].reflections[tables[0].to_sym].macro.to_s == "belongs_to" result = transfer_table_id(tables[0], owners[0]) @inner_joins.delete(tables[0]) if @inner_joins.include?(tables[0]) unless owners[1].nil? result = transfer_table_name(tables[1], owners[1]) + "." + result end tables.delete_at(0) owners.delete_at(0) return result if tables.empty? end end # #get include if tables.length == 1 && tables[0] != @table_name @new_include << tables[0].to_sym unless @new_include.include?(tables[0].to_sym) else tables_clone = tables[0..-1] value = tables_clone.shift.to_sym until tables_clone.empty? hashes = {} hashes[tables_clone.shift.to_sym] = value value = hashes end @new_include << hashes unless hashes.nil? || @new_include.include?(hashes) end result ||= transfer_table_name(tables[0], owners[0]) + "." + field end |
#cut_end_underscore(str) ⇒ Object
323 324 325 326 |
# File 'lib/rquerypad.rb', line 323 def cut_end_underscore(str) str = str.reverse.sub("_", "").reverse if str[-1, 1] == "_" str end |
#cut_end_underscore!(str) ⇒ Object
328 329 330 |
# File 'lib/rquerypad.rb', line 328 def cut_end_underscore!(str) str.reverse!.sub!("_", "").reverse! if str[-1, 1] == "_" end |
#improve_conditions(options) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/rquerypad.rb', line 176 def improve_conditions() = nil if .is_a?(Hash) # #work around frozen issue = {} until .empty? key, value = .shift [process_single(key.to_s)] = value end else if .is_a?(String) = [] else = .dup end str = [0] return nil if str.nil? qp = /['"][^."]*['"]/ temp = str.scan(qp) # #replace string in quote to avoid unecessary processing str = str.gsub(qp, "'[??]'") unless temp.empty? str = str.gsub(/(([\w\(\)]+\.)+\w+)[ !><=]/) do |n| # #cut last char and abstract association for include if n =~ /^\(/ '(' + abstract_association(n[1..-2]) + n[-1, 1] else abstract_association(n[0..-2]) + n[-1, 1] end end # #add @table_name on single field str = str.gsub(/[\.:]?[`"']?\w+[`"']?[\.]?/) {|x| (x =~ /\D+/).nil? || x[-1, 1] == "." || x[0, 1] == ":" || ["and", "or", "is", "null", "not", "like", "in"].include?(x.downcase) ? x : @tnwithdot + x} str = str.gsub(/\.#{@table_name}\./, ".") # #recover string in quote unless temp.empty? i = -1 str = str.gsub(/\'\[\?\?\]\'/) do i += 1 temp[i] end end [0] = str end end |
#improve_options!(option_hash) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/rquerypad.rb', line 126 def (option_hash) = {} option_hash.each do |key, value| if value.blank? [key] = value next end key_str = key.to_s if key_str == "conditions" [key] = improve_conditions(value) elsif (key_str == "order" || key_str == "group" || key_str == "group_field") [key] = improve_ordergroup(value).join(", ") else [key] = value if (key_str == "include") @old_include = value end end end return if @new_include.empty? # #generate new include unless @new_include.nil? [:include] ||= [] [:include] = [[:include] ] unless [:include] .is_a?(Array) [:include] += @new_include end [:include] = remove_dup_includes([:include]) [:inner_joins] = @inner_joins unless @inner_joins.empty? end |
#improve_ordergroup(fields) ⇒ Object
236 237 238 239 240 241 242 243 244 245 |
# File 'lib/rquerypad.rb', line 236 def improve_ordergroup(fields) result = [] if fields.is_a?(Array) result += fields else result << fields end result.each_index {|i| result[i] = process_single(result[i])} result end |
#process_single(key) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/rquerypad.rb', line 222 def process_single(key) return key.sub('=', '') if key[0, 1] == '=' if key.include?(".") if /^(.+)[ ]/ =~ key key = key.sub("#$1", abstract_association("#$1")) else key = key.sub(/^.+$/, abstract_association(key)) end else key = key.sub(/^.+$/, @tnwithdot + key) end key end |
#raise_error(name, owner) ⇒ Object
305 306 307 308 309 310 311 |
# File 'lib/rquerypad.rb', line 305 def raise_error(name, owner) = "Rquerypad error\n" << "owner.reflections:\n" << owner.reflections.inspect << '\n\nnot found by key:' << name.to_sym.to_s raise end |
#remove_dup_includes(options) ⇒ Object
suppose => :b => {:b => :c }, will remove => :b
161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/rquerypad.rb', line 161 def remove_dup_includes() result = {} .each {|o| result[o.inspect.gsub(/[\{\}\":]/, '') + "=>"] = o} result = result.sort.reverse keep_keys = [] = [] result.each do |o| skip = false keep_keys.each {|k| skip = true if (k.index(o[0]) == 0)} << o[1] unless skip keep_keys << o[0] end end |
#transfer_table_id(name, owner = @owner) ⇒ Object
318 319 320 321 |
# File 'lib/rquerypad.rb', line 318 def transfer_table_id(name, owner = @owner) raise_error(name, owner) if owner.reflections[name.to_sym].nil? owner.reflections[name.to_sym].class_name.gsub(/([\w]+::)*/, "").underscore + "_id" end |
#transfer_table_name(name, owner = @owner) ⇒ Object
313 314 315 316 |
# File 'lib/rquerypad.rb', line 313 def transfer_table_name(name, owner = @owner) raise_error(name, owner) if owner.reflections[name.to_sym].nil? owner.reflections[name.to_sym].class_name.gsub(/([\w]+::)*/, "").pluralize.underscore end |