Class: Rmap::Table
- Inherits:
-
Object
- Object
- Rmap::Table
- Defined in:
- lib/rmap/table.rb
Constant Summary collapse
- BINARY_FILTER_METHODS =
{ 'eq' => lambda {|left,right| "#{left} = #{right}"}, 'ne' => lambda {|left,right| "#{left} != #{right}"}, 'lt' => lambda {|left,right| "#{left} < #{right}"}, 'gt' => lambda {|left,right| "#{left} > #{right}"}, 'le' => lambda {|left,right| "#{left} <= #{right}"}, 'ge' => lambda {|left,right| "#{left} >= #{right}"}, 'contains' => lambda {|left,right| "#{left} like concat('%', #{right}, '%')"}, 'ends_with' => lambda {|left,right| "#{left} like concat('%', #{right})"}, 'begins_with' => lambda {|left,right| "#{left} like concat(#{right}, '%')"}, 'year_eq' => lambda {|left,right| "year(#{left}) = #{right}"}, 'year_ne' => lambda {|left,right| "year(#{left}) != #{right}"}, 'year_lt' => lambda {|left,right| "year(#{left}) < #{right}"}, 'year_gt' => lambda {|left,right| "year(#{left}) > #{right}"}, 'year_le' => lambda {|left,right| "year(#{left}) <= #{right}"}, 'year_ge' => lambda {|left,right| "year(#{left}) >= #{right}"}, 'month_eq' => lambda {|left,right| "month(#{left}) = #{right}"}, 'month_ne' => lambda {|left,right| "month(#{left}) != #{right}"}, 'month_lt' => lambda {|left,right| "month(#{left}) < #{right}"}, 'month_gt' => lambda {|left,right| "month(#{left}) > #{right}"}, 'month_le' => lambda {|left,right| "month(#{left}) <= #{right}"}, 'month_ge' => lambda {|left,right| "month(#{left}) >= #{right}"}, 'day_eq' => lambda {|left,right| "day(#{left}) = #{right}"}, 'day_ne' => lambda {|left,right| "day(#{left}) != #{right}"}, 'day_lt' => lambda {|left,right| "day(#{left}) < #{right}"}, 'day_gt' => lambda {|left,right| "day(#{left}) > #{right}"}, 'day_le' => lambda {|left,right| "day(#{left}) <= #{right}"}, 'day_ge' => lambda {|left,right| "day(#{left}) >= #{right}"}, 'hour_eq' => lambda {|left,right| "hour(#{left}) = #{right}"}, 'hour_ne' => lambda {|left,right| "hour(#{left}) != #{right}"}, 'hour_lt' => lambda {|left,right| "hour(#{left}) < #{right}"}, 'hour_gt' => lambda {|left,right| "hour(#{left}) > #{right}"}, 'hour_le' => lambda {|left,right| "hour(#{left}) <= #{right}"}, 'hour_ge' => lambda {|left,right| "hour(#{left}) >= #{right}"}, 'minute_eq' => lambda {|left,right| "minute(#{left}) = #{right}"}, 'minute_ne' => lambda {|left,right| "minute(#{left}) != #{right}"}, 'minute_lt' => lambda {|left,right| "minute(#{left}) < #{right}"}, 'minute_gt' => lambda {|left,right| "minute(#{left}) > #{right}"}, 'minute_le' => lambda {|left,right| "minute(#{left}) <= #{right}"}, 'minute_ge' => lambda {|left,right| "minute(#{left}) >= #{right}"}, 'second_eq' => lambda {|left,right| "second(#{left}) = #{right}"}, 'second_ne' => lambda {|left,right| "second(#{left}) != #{right}"}, 'second_lt' => lambda {|left,right| "second(#{left}) < #{right}"}, 'second_gt' => lambda {|left,right| "second(#{left}) > #{right}"}, 'second_le' => lambda {|left,right| "second(#{left}) <= #{right}"}, 'second_ge' => lambda {|left,right| "second(#{left}) >= #{right}"}, }
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#page ⇒ Object
Returns the value of attribute page.
Instance Method Summary collapse
- #add(name, type, options = {}) ⇒ Object
- #all ⇒ Object
- #column?(name) ⇒ Boolean
- #column_names ⇒ Object
- #count ⇒ Object
- #define_scope(name, &block) ⇒ Object
- #delete ⇒ Object
- #drop ⇒ Object
- #each(&block) ⇒ Object
- #explain ⇒ Object
- #find(arg1) ⇒ Object
- #first ⇒ Object
- #format_sql(sql) ⇒ Object
- #generate_filter_conditions_sql ⇒ Object
- #generate_from_sql ⇒ Object
- #generate_group_by_sql ⇒ Object
- #generate_inner_join_conditions_sql ⇒ Object
- #generate_join_condition_sql(table1, table2, foreign_key) ⇒ Object
- #generate_limit_sql ⇒ Object
- #generate_order_by_sql ⇒ Object
- #generate_select_sql(expression_list_sql, without_limit = false) ⇒ Object
- #generate_table_list_sql ⇒ Object
- #generate_where_sql ⇒ Object
-
#initialize(database, name) ⇒ Table
constructor
A new instance of Table.
- #insert(hash) ⇒ Object
- #join(table, options = {}) ⇒ Object
- #method_missing(name, *args) ⇒ Object
- #order_by(sql_exression, desc = false) ⇒ Object
- #page_count ⇒ Object
- #paginate(page_size = 10) ⇒ Object
- #quote(data) ⇒ Object
- #remove(name) ⇒ Object
- #set_page(page) ⇒ Object
- #sum(sql_expression, limit = nil) ⇒ Object
- #to_s ⇒ Object
- #update(hash) ⇒ Object
Constructor Details
#initialize(database, name) ⇒ Table
Returns a new instance of Table.
54 55 56 57 58 59 60 61 62 |
# File 'lib/rmap/table.rb', line 54 def initialize(database, name) @database = database @name = name @binary_filter_methods_args = {} Table::BINARY_FILTER_METHODS.each {|name, block| @binary_filter_methods_args[name] = []} @join_list = [] @order_by_list = [] @page = 1 end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rmap/table.rb', line 85 def method_missing name, *args table = self if @database.table? name Table.new(@database, name).join(self, *args) elsif column? name all.map{|row| row.fetch(name).first} elsif column?(name.to_s.sub(/=\Z/, "")) && name.match(/\A(.*)=\Z/) all.each{|row| row.update($1 => args[0])} elsif name.match /\Afind_by_(.*)\Z/ eq($1, args[0]).first elsif name.match /\A(.*?)_(#{BINARY_FILTER_METHODS.keys.join('|')})\Z/ @binary_filter_methods_args[$2].push([$1.split(/_or_/),args[0]]) self elsif @database.instance_eval{!@scopes[table.name].nil? && !@scopes[table.name][name].nil?} instance_exec *args, &@database.instance_eval{@scopes[table.name][name]} self else super end end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
52 53 54 |
# File 'lib/rmap/table.rb', line 52 def name @name end |
#page ⇒ Object
Returns the value of attribute page.
52 53 54 |
# File 'lib/rmap/table.rb', line 52 def page @page end |
Instance Method Details
#add(name, type, options = {}) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/rmap/table.rb', line 304 def add(name, type, = {}) if !@database.table? @name @database.create @name end case type when :string @database.client.query("alter table `#{@name}` add `#{name}` varchar(255) not null") when :text @database.client.query("alter table `#{@name}` add `#{name}` longtext not null") when :binary @database.client.query("alter table `#{@name}` add `#{name}` longblob not null") when :integer @database.client.query("alter table `#{@name}` add `#{name}` int signed not null") when :foreign_key @database.client.query("alter table `#{@name}` add `#{name}` int unsigned not null") when :date @database.client.query("alter table `#{@name}` add `#{name}` date not null") when :datetime @database.client.query("alter table `#{@name}` add `#{name}` datetime not null") when :boolean @database.client.query("alter table `#{@name}` add `#{name}` enum('true', 'false') not null") when :decimal @database.client.query("alter table `#{@name}` add `#{name}` decimal not null") end if type == :foreign_key || [:index] == true @database.client.query("alter table `#{@name}` add index(`#{name}`)") end end |
#all ⇒ Object
263 264 265 266 267 268 269 |
# File 'lib/rmap/table.rb', line 263 def all out = [] @database.client.query(generate_select_sql('id'), :as => :hash).each do |row| out.push(Row.new(@database, @name, row['id'])) end out end |
#column?(name) ⇒ Boolean
81 82 83 |
# File 'lib/rmap/table.rb', line 81 def column?(name) @database.client.query("describe #{@name} `#{name}`").count > 0 end |
#column_names ⇒ Object
341 342 343 |
# File 'lib/rmap/table.rb', line 341 def column_names @database.client.query("describe `#{@name}`", :as => :hash).map {|row| row['Field']} end |
#count ⇒ Object
259 260 261 |
# File 'lib/rmap/table.rb', line 259 def count @database.client.query(generate_select_sql('id')).count end |
#define_scope(name, &block) ⇒ Object
349 350 351 352 353 354 355 356 357 |
# File 'lib/rmap/table.rb', line 349 def define_scope(name, &block) table = self @database.instance_eval do if @scopes[table.name].nil? @scopes[table.name] = {} end @scopes[table.name][name.to_sym] = block end end |
#delete ⇒ Object
284 285 286 |
# File 'lib/rmap/table.rb', line 284 def delete each {|row| row.delete} end |
#drop ⇒ Object
299 300 301 302 |
# File 'lib/rmap/table.rb', line 299 def drop eval("@table_names = nil", @database.bindings) @database.client.query("drop table `#{@name}`") end |
#each(&block) ⇒ Object
271 272 273 274 |
# File 'lib/rmap/table.rb', line 271 def each &block all.each &block nil end |
#explain ⇒ Object
255 256 257 |
# File 'lib/rmap/table.rb', line 255 def explain generate_select_sql('id').strip end |
#find(arg1) ⇒ Object
378 379 380 381 382 383 384 385 386 387 |
# File 'lib/rmap/table.rb', line 378 def find(arg1) if arg1.respond_to? :to_hash arg1.to_hash.each do |k,v| self.eq(k, v) end else self.id_eq(arg1) end first end |
#first ⇒ Object
276 277 278 |
# File 'lib/rmap/table.rb', line 276 def first all.first end |
#format_sql(sql) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/rmap/table.rb', line 106 def format_sql(sql) out_buffer = [] sql = sql.to_s while sql.length > 0 if sql.match(/\A(\s+|\d+\.\d+|\d+|\w+\()(.*)/) out_buffer.push($1) sql = $2 elsif sql.match(/\A(\w+)\.(\w+)(.*)/) out_buffer.push("`#{$1}`.`#{$2}`") sql = $3 elsif sql.match(/\A(\w+)(.*)/) out_buffer.push("`#{@name}`.`#{$1}`") sql = $2 else sql.match(/\A(.)(.*)/) out_buffer.push($1) sql = $2 end end out_buffer.join end |
#generate_filter_conditions_sql ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/rmap/table.rb', line 132 def generate_filter_conditions_sql and_sql = [] Table::BINARY_FILTER_METHODS.each do |name, block| @binary_filter_methods_args[name].each do |args| or_sql = [] if args[0].class.name == 'Array' args[0].each do |left| or_sql.push(block.call(format_sql(left.to_s), quote(args[1]))) end elsif args[1].class.name == 'Array' args[1].each do |right| or_sql.push(block.call(format_sql(args[0].to_s), quote(right))) end else or_sql.push(block.call(format_sql(args[0].to_s), quote(args[1]))) end and_sql.push("(" + or_sql.join(' or ') + ")") end end @join_list.each do |join| and_sql.push(join[:table].generate_filter_conditions_sql) end and_sql.join(' and ') end |
#generate_from_sql ⇒ Object
169 170 171 |
# File 'lib/rmap/table.rb', line 169 def generate_from_sql "from " + generate_table_list_sql end |
#generate_group_by_sql ⇒ Object
218 219 220 221 222 |
# File 'lib/rmap/table.rb', line 218 def generate_group_by_sql if @join_list.count > 0 "group by #{name}.id" end end |
#generate_inner_join_conditions_sql ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/rmap/table.rb', line 189 def generate_inner_join_conditions_sql out = [] @join_list.each do |join| out.push(generate_join_condition_sql(self, join[:table], join[:options][:using])) inner_join_conditions_sql = join[:table].generate_inner_join_conditions_sql if inner_join_conditions_sql != '' out.push(inner_join_conditions_sql) end end out.join(" and ") end |
#generate_join_condition_sql(table1, table2, foreign_key) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/rmap/table.rb', line 173 def generate_join_condition_sql(table1, table2, foreign_key) if !foreign_key.nil? if table2.column? foreign_key "#{table1.name}.id = #{table2.name}.#{foreign_key}" else "#{table1.name}.#{foreign_key} = #{table2.name}.id" end else if table2.column? "#{to_singular(table1.name)}_id" "#{table1.name}.id = #{table2.name}.#{to_singular(table1.name)}_id" else "#{table1.name}.#{to_singular(table2.name)}_id = #{table2.name}.id" end end end |
#generate_limit_sql ⇒ Object
247 248 249 |
# File 'lib/rmap/table.rb', line 247 def generate_limit_sql @page_size.nil? ? "" : "limit #{(@page - 1) * @page_size}, #{@page_size}" end |
#generate_order_by_sql ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/rmap/table.rb', line 224 def generate_order_by_sql out = [] @order_by_list.each do |order_by| (sql_expression, desc) = order_by if desc out.push "#{format_sql(sql_expression)} desc" else out.push "#{format_sql(sql_expression)} asc" end end @join_list.each do |join| order_by_sql = join[:table].generate_order_by_sql if order_by_sql != '' out.push(order_by_sql) end end out = out.join(', ') if out != '' out = "order by #{out}" end out end |
#generate_select_sql(expression_list_sql, without_limit = false) ⇒ Object
251 252 253 |
# File 'lib/rmap/table.rb', line 251 def generate_select_sql(expression_list_sql, without_limit = false) "select #{format_sql(expression_list_sql)} #{generate_from_sql} #{generate_where_sql} #{generate_group_by_sql} #{generate_order_by_sql} #{without_limit ? "" : generate_limit_sql}" end |
#generate_table_list_sql ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/rmap/table.rb', line 157 def generate_table_list_sql out = [] out.push(name) @join_list.each do |join| table_list_sql = join[:table].generate_table_list_sql if table_list_sql != '' out.push(table_list_sql) end end out.join(', ') end |
#generate_where_sql ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/rmap/table.rb', line 201 def generate_where_sql out = [] inner_join_conditions_sql = generate_inner_join_conditions_sql if inner_join_conditions_sql != '' out.push inner_join_conditions_sql end filter_conditions_sql = generate_filter_conditions_sql if filter_conditions_sql != '' out.push filter_conditions_sql end out = out.join(' and ') if out != '' out = "where #{out}" end out end |
#insert(hash) ⇒ Object
288 289 290 291 |
# File 'lib/rmap/table.rb', line 288 def insert(hash) @database.client.query("insert into `#{@name}`(#{(hash.map{|k,v| "`#{k}`"}).join(', ')}) values(#{(hash.map{|k,v| quote(v)}).join(', ')})") @database.client.last_id end |
#join(table, options = {}) ⇒ Object
71 72 73 74 |
# File 'lib/rmap/table.rb', line 71 def join(table, = {}) @join_list.push({:table => table, :options => }); self end |
#order_by(sql_exression, desc = false) ⇒ Object
76 77 78 79 |
# File 'lib/rmap/table.rb', line 76 def order_by(sql_exression, desc = false) @order_by_list.push([sql_exression, desc]) self end |
#page_count ⇒ Object
369 370 371 372 373 374 375 376 |
# File 'lib/rmap/table.rb', line 369 def page_count count_without_limit = @database.client.query(generate_select_sql('id', true)).count if !@page_size.nil? (count_without_limit.to_f / @page_size).ceil else count_without_limit > 0 ? 1 : 0 end end |
#paginate(page_size = 10) ⇒ Object
359 360 361 362 |
# File 'lib/rmap/table.rb', line 359 def paginate(page_size = 10) @page_size = page_size self end |
#quote(data) ⇒ Object
128 129 130 |
# File 'lib/rmap/table.rb', line 128 def quote(data) "'" + @database.client.escape(data.to_s) + "'" end |
#remove(name) ⇒ Object
337 338 339 |
# File 'lib/rmap/table.rb', line 337 def remove(name) @database.client.query("alter table `#{@name}` drop `#{name}`") end |
#set_page(page) ⇒ Object
364 365 366 367 |
# File 'lib/rmap/table.rb', line 364 def set_page(page) @page = page.to_i self end |
#sum(sql_expression, limit = nil) ⇒ Object
293 294 295 296 297 |
# File 'lib/rmap/table.rb', line 293 def sum(sql_expression, limit = nil) out = 0 @database.client.query(generate_select_sql("sum(#{sql_expression})", limit), :as => :array).each{|row| out += row.first} out end |
#to_s ⇒ Object
345 346 347 |
# File 'lib/rmap/table.rb', line 345 def to_s all.to_s end |
#update(hash) ⇒ Object
280 281 282 |
# File 'lib/rmap/table.rb', line 280 def update(hash) all.each {|row| row.update(hash)} end |