Module: Selection

Included in:
BlocRecord::Base
Defined in:
lib/bloc_record/selection.rb

Instance Method Summary collapse

Instance Method Details

#allObject



79
80
81
82
83
84
85
# 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) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# 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) ⇒ Object



27
28
29
30
31
32
33
34
# 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) ⇒ Object



18
19
20
21
22
23
24
25
# 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

#firstObject



59
60
61
62
63
64
65
66
67
# 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) ⇒ Object



164
165
166
# File 'lib/bloc_record/selection.rb', line 164

def group(*args)
  group_by_ids(nil, args)
end

#group_by_ids(ids, args) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# 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) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# 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

#lastObject



69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



156
157
158
159
160
161
162
# 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) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# 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) ⇒ Object



146
147
148
149
150
151
152
153
154
# 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) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# 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_oneObject



49
50
51
52
53
54
55
56
57
# 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) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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