module Selection
Public Instance Methods
all()
click to toggle source
# File lib/bloc_record/selection.rb, line 79 def all rows = connection.execute <<-SQL SELECT #{columns.join ","} FROM #{table}; SQL rows_to_array(rows) end
find(*ids)
click to toggle source
# File lib/bloc_record/selection.rb, line 4 def find(*ids) if ids.length == 1 find_one(ids.first) else rows = connection.execute <<-SQL SELECT #{columns.join ","} FROM #{table} WHERE id IN (#{ids.join(",")}); SQL rows_to_array(rows) end end
find_by(attribute, value)
click to toggle source
# File lib/bloc_record/selection.rb, line 27 def find_by(attribute, value) row = connection.get_first_row( "SELECT #{columns.join ","} FROM #{table} WHERE #{attribute} = ?;", BlocRecord::Utility.sql_strings(value) ) init_object_from_row(row) end
find_one(id)
click to toggle source
# File lib/bloc_record/selection.rb, line 18 def find_one(id) row = connection.get_first_row <<-SQL SELECT #{columns.join ","} FROM #{table} WHERE id = #{id}; SQL init_object_from_row(row) end
first()
click to toggle source
# File lib/bloc_record/selection.rb, line 59 def first row = connection.get_first_row <<-SQL SELECT #{columns.join ","} FROM #{table} ORDER BY id ASC LIMIT 1; SQL init_object_from_row(row) end
group(*args)
click to toggle source
# File lib/bloc_record/selection.rb, line 164 def group(*args) group_by_ids(nil, args) end
group_by_ids(ids, args)
click to toggle source
# File lib/bloc_record/selection.rb, line 168 def group_by_ids(ids, args) if args.count > 1 conditions = args.join(", ") else conditions = args.first end where_clause = ids.nil? ? "" : "WHERE id IN (#{ids.join(",")})" rows = connection.execute( "SELECT * FROM #{table} #{where_clause} GROUP BY ?;", conditions.to_s ) rows_to_array(rows) end
join(*args)
click to toggle source
# File lib/bloc_record/selection.rb, line 124 def join(*args) if args.count > 1 joins = args.map { |arg| "INNER JOIN #{arg} ON #{arg}.#{table}_id = #{table}.id"}.join(" ") rows = connection.execute <<-SQL SELECT * FROM #{table} #{joins} SQL else case args.first when String rows = connection.execute <<-SQL SELECT * FROM #{table} #{BlocRecord::Utility.sql_strings(args.first)}; SQL when Symbol rows = connection.execute <<-SQL SELECT * FROM #{table} INNER JOIN #{args.first} ON #{args.first}.#{table}_id = #{table}.id SQL end end rows_to_array(rows) end
last()
click to toggle source
# File lib/bloc_record/selection.rb, line 69 def last row = connection.get_first_row <<-SQL SELECT #{columns.join ","} FROM #{table} ORDER BY id DESC LIMIT 1; SQL init_object_from_row(row) end
limit(value, offset=0)
click to toggle source
# File lib/bloc_record/selection.rb, line 156 def limit(value, offset=0) rows = connection.execute( "SELECT * FROM #{table} LIMIT ? OFFSET ?;", [value, offset] ) rows_to_array(rows) end
order(*args)
click to toggle source
# File lib/bloc_record/selection.rb, line 111 def order(*args) if args.count > 1 order = args.join(",") else order = args.first.to_s end rows = connection.execute( "SELECT * FROM #{table} ORDER BY ?;", order ) rows_to_array(rows) end
select(*fields)
click to toggle source
# File lib/bloc_record/selection.rb, line 146 def select(*fields) rows = connection.execute( "SELECT ? FROM #{table};", fields.join(", ") ) collection = BlocRecord::Collection.new rows.each { |row| collection << new(Hash[fields.zip(row)]) } collection end
take(num=1)
click to toggle source
# File lib/bloc_record/selection.rb, line 36 def take(num=1) if num > 1 rows = connection.execute( "SELECT #{columns.join ","} FROM #{table} ORDER BY random() LIMIT ?;", num ) rows_to_array(rows) else take_one end end
take_one()
click to toggle source
# File lib/bloc_record/selection.rb, line 49 def take_one row = connection.get_first_row <<-SQL SELECT #{columns.join ","} FROM #{table} ORDER BY random() LIMIT 1; SQL init_object_from_row(row) end
where(*args)
click to toggle source
# File lib/bloc_record/selection.rb, line 87 def where(*args) if args.count > 1 expression = args.shift params = args else case args.first when String expression = "?" params = args.first when Hash params = BlocRecord::Utility.convert_keys(args.first) expression = params.keys.map {|key| "#{key}=:#{key}"}.join(" and ") end end sql = <<-SQL SELECT #{columns.join ","} FROM #{table} WHERE #{expression}; SQL rows = connection.execute(sql, params) rows_to_array(rows) end
Private Instance Methods
init_object_from_row(row)
click to toggle source
# File lib/bloc_record/selection.rb, line 187 def init_object_from_row(row) if row data = Hash[columns.zip(row)] new(data) end end
rows_to_array(rows)
click to toggle source
# File lib/bloc_record/selection.rb, line 194 def rows_to_array(rows) collection = BlocRecord::Collection.new rows.each { |row| collection << new(Hash[columns.zip(row)]) } collection end