class Matrack::FetchQueries
Public Class Methods
all()
click to toggle source
# File lib/orm/fetch_queries.rb, line 19 def all execute "SELECT * FROM #{table_name}" end
execute(query)
click to toggle source
# File lib/orm/fetch_queries.rb, line 12 def execute(query) objs = [] rows = db_conn.execute query rows.each{|row| objs << self.new(row.reject!{ |k| !k.is_a? String }) } objs end
find(id)
click to toggle source
# File lib/orm/fetch_queries.rb, line 23 def find(id) execute("SELECT * FROM #{table_name} WHERE (#{uniq_id} = #{id})").first end
find_by(col, val)
click to toggle source
# File lib/orm/fetch_queries.rb, line 40 def find_by(col, val) execute("SELECT * FROM #{table_name} WHERE (#{col} = '#{val}') LIMIT 1").first end
find_cols(col_hash)
click to toggle source
firstname: “name”, lastname: “last”
# File lib/orm/fetch_queries.rb, line 46 def find_cols(col_hash) clause = col_hash.map{|k,v| "#{k.to_s} = " "'#{v}'"}.join(" AND ") execute("SELECT * FROM #{table_name} WHERE (#{clause}) LIMIT 1").first end
first()
click to toggle source
# File lib/orm/fetch_queries.rb, line 36 def first execute("SELECT * FROM #{table_name} LIMIT(1)").first end
last()
click to toggle source
# File lib/orm/fetch_queries.rb, line 27 def last offset = count - 1 execute("SELECT * FROM #{table_name} LIMIT(1) OFFSET(#{offset})").first end
limit(num)
click to toggle source
# File lib/orm/fetch_queries.rb, line 32 def limit(num) execute "SELECT * FROM #{table_name} LIMIT(#{num})" end
table_name()
click to toggle source
# File lib/orm/fetch_queries.rb, line 4 def table_name self.to_s.to_snake_case + "s" end
uniq_id()
click to toggle source
# File lib/orm/fetch_queries.rb, line 8 def uniq_id "#{self.to_s.to_snake_case}_id" end