Class: Toolhound::Base
- Inherits:
-
Object
show all
- Includes:
- Util
- Defined in:
- lib/toolhound-ruby/base.rb
Constant Summary
collapse
- DB_TYPE_REGEX =
/^(int|dec|var|bol|dte|bin)/
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Util
#acronym_regex, #acronyms, #camelize, #demodulize, #underscore
Constructor Details
#initialize(client, options = {}) ⇒ Base
def self.connection
@connection ||= Toolhound.connection
end
41
42
43
44
45
|
# File 'lib/toolhound-ruby/base.rb', line 41
def initialize(client, options = {})
@client = client
end
|
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
7
8
9
|
# File 'lib/toolhound-ruby/base.rb', line 7
def connection
@connection
end
|
Class Method Details
.primary_key ⇒ Object
13
14
15
|
# File 'lib/toolhound-ruby/base.rb', line 13
def self.primary_key
@@primary_key[self.name]
end
|
.primary_key=(primary_key) ⇒ Object
22
23
24
25
|
# File 'lib/toolhound-ruby/base.rb', line 22
def self.primary_key=(primary_key)
@@primary_key ||= {}
@@primary_key[self.name] = primary_key
end
|
.rename_attributes(hash) ⇒ Object
28
29
30
31
|
# File 'lib/toolhound-ruby/base.rb', line 28
def self.rename_attributes(hash)
hash = Hash[hash.map {|key, value| [key.to_s, value] }]
@@rename_attributes = hash
end
|
.renamed_attributes ⇒ Object
33
34
35
|
# File 'lib/toolhound-ruby/base.rb', line 33
def self.renamed_attributes
@@rename_attributes ||= {}
end
|
.table_name ⇒ Object
9
10
11
|
# File 'lib/toolhound-ruby/base.rb', line 9
def self.table_name
@@table_name[self.name]
end
|
.table_name=(table_name) ⇒ Object
17
18
19
20
|
# File 'lib/toolhound-ruby/base.rb', line 17
def self.table_name=(table_name)
@@table_name ||= {}
@@table_name[self.name] = table_name
end
|
Instance Method Details
#_build_joins(joins) ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/toolhound-ruby/base.rb', line 179
def _build_joins(joins)
join_types = {
inner: "INNER JOIN",
left: "LEFT OUTER JOIN",
left_outer: "LEFT OUTER JOIN",
left_inner: "LEFT INNER JOIN",
right: "RIGHT OUTER JOIN",
right_outer: "RIGHT OUTER JOIN",
right_inner: "RIGHT INNER JOIN",
}
joins
end
|
#_build_selects(selects) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/toolhound-ruby/base.rb', line 136
def _build_selects(selects)
arr = []
aggs = {
count: "COUNT",
max: "MAX",
min: "MIN",
average: "AVERAGE"
}
selects.each do |table, values|
values.each do |v|
if v.is_a? Hash
end
if v.is_a? Hash
select = "#{formatted_table_name(table)}.#{formmatted_column_name(v.first[0])}"
options = v.first[1]
if options.is_a? Hash
if options[:agg]
select = "#{aggs[options[:agg]]}(#{select})"
end
if options[:as]
select += " AS #{formmatted_column_name(options[:as])}"
end
else
select += " AS #{formmatted_column_name(options)}"
end
else
select = "#{formatted_table_name(table)}.#{formmatted_column_name(v)}"
end
arr << select
end
end
arr
end
|
#_build_where(wheres) ⇒ Object
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/toolhound-ruby/base.rb', line 208
def _build_where(wheres)
arr = []
case wheres.class.to_s
when "String"
return wheres
when "Hash"
wheres.each do |k, v|
table, column = formatted_table_and_column(k)
op = :eq
if v.is_a? Hash
op = v.delete :op
v = v.delete :value
end
arr << "#{table}.#{column} #{get_operator(op, v)}"
end
when "Array"
wheres.each do |v|
arr += _build_where(v)
end
end
arr
end
|
#all(options = {}) ⇒ Object
76
77
78
79
|
# File 'lib/toolhound-ruby/base.rb', line 76
def all(options = {})
options = merge_options({selects: default_selects, joins: default_joins, where: default_wheres}, options)
build_and_query options
end
|
#build_and_query(options, query_options = {}) ⇒ Object
281
282
283
284
|
# File 'lib/toolhound-ruby/base.rb', line 281
def build_and_query(options, query_options = {})
sql = build_sql(options)
results = query(sql, query_options)
end
|
#build_joins(joins) ⇒ Object
175
176
177
|
# File 'lib/toolhound-ruby/base.rb', line 175
def build_joins(joins)
_build_joins(joins).join(" ")
end
|
#build_selects(selects) ⇒ Object
132
133
134
|
# File 'lib/toolhound-ruby/base.rb', line 132
def build_selects(selects)
_build_selects(selects).join(", ")
end
|
#build_sql(obj) ⇒ Object
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
# File 'lib/toolhound-ruby/base.rb', line 287
def build_sql(obj)
limit = obj[:limit]
selects = obj[:selects] ? build_selects(obj[:selects]) : "*"
joins = obj[:joins] ? build_joins(obj[:joins]) : ""
from = obj[:from] ? formatted_table_name(obj[:from]) : table_name
where = obj[:where] ? build_where(obj[:where]) : nil
order = obj[:order]
limit_str = limit ? "TOP(#{limit})" : ""
where_str = where.length > 0 ? "WHERE #{where}" : ""
order_str = order ? "ORDER BY #{order}" : ""
sql = "SELECT #{limit_str} #{selects} FROM #{from} #{joins} #{where_str} #{order_str}"
puts sql
sql
end
|
#build_where(wheres) ⇒ Object
203
204
205
206
|
# File 'lib/toolhound-ruby/base.rb', line 203
def build_where(wheres)
arr = _build_where(wheres)
arr.join(" AND ")
end
|
#default_joins ⇒ Object
72
73
74
|
# File 'lib/toolhound-ruby/base.rb', line 72
def default_joins
[]
end
|
#default_selects ⇒ Object
69
70
71
|
# File 'lib/toolhound-ruby/base.rb', line 69
def default_selects
{}
end
|
#default_wheres ⇒ Object
65
66
67
|
# File 'lib/toolhound-ruby/base.rb', line 65
def default_wheres
[]
end
|
#find(id, options = {}) ⇒ Object
81
82
83
84
85
86
87
88
|
# File 'lib/toolhound-ruby/base.rb', line 81
def find(id, options = {})
wheres = default_wheres + [{:"#{primary_key}" => id}]
options = merge_options({limit: 1, selects: default_selects, joins: default_joins, where: wheres}, options)
results = build_and_query options
results.first
end
|
261
262
263
264
265
266
267
268
269
270
|
# File 'lib/toolhound-ruby/base.rb', line 261
def formatted_table_and_column(key)
key = key.to_s
first, second = key.split(".")
if first && second
[formatted_table_name(first), formmatted_column_name(second)]
elsif(first)
[table_name, formmatted_column_name(first)]
end
end
|
119
120
121
122
123
124
125
|
# File 'lib/toolhound-ruby/base.rb', line 119
def formatted_table_name(table)
table = table.to_s
unless /^tbl/i =~ table
table = "tbl#{camelize(table, true)}"
end
camelize(table, false)
end
|
127
128
129
130
|
# File 'lib/toolhound-ruby/base.rb', line 127
def formmatted_column_name(column)
column = column.to_s
camelize(column, false)
end
|
#get_operator(op, value) ⇒ Object
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
# File 'lib/toolhound-ruby/base.rb', line 243
def get_operator(op, value)
operators = {eq: "=", gt: ">", gte: ">=", lt: "<", lte: "<=", ne: "!=", in: "IN", nin: "NOT IN", like: "LIKE", between: "BETWEEN"}
ops = operators.values
operator = operators[op]
unless operator
operator = ops.include?(op) ? op : "="
end
if operator == "IN" || operator == "NOT IN"
value = "(#{value})"
elsif operator == "BETWEEN"
value = "'#{value[0]}' AND '#{value[1]}'"
else
value = "'#{value}'"
end
"#{operator} #{value}"
end
|
#locale ⇒ Object
61
62
63
|
# File 'lib/toolhound-ruby/base.rb', line 61
def locale
@locale ||= "EN-US"
end
|
#merge_options(defaults, options = {}) ⇒ Object
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/toolhound-ruby/base.rb', line 90
def merge_options(defaults, options = {})
where = options.delete :where
selects = options.delete :selects
joins = options.delete :joins
defaults[:where] = (defaults[:where] || []) + (where || [])
defaults[:selects] = defaults[:selects].merge(selects || {})
defaults[:joins] = defaults[:joins] + (joins || [])
defaults.merge options
end
|
#primary_key ⇒ Object
56
57
58
59
|
# File 'lib/toolhound-ruby/base.rb', line 56
def primary_key
id = self.class.primary_key || "int#{demodulize(self.class.name)}ID"
formmatted_column_name(id)
end
|
#query(query, options) ⇒ Object
272
273
274
275
276
277
278
279
|
# File 'lib/toolhound-ruby/base.rb', line 272
def query(query, options)
data = []
results = connection.execute(query)
results.each(:cache_rows => false) do |row|
data << transform_attributes(row)
end
data
end
|
#table_name ⇒ Object
52
53
54
55
|
# File 'lib/toolhound-ruby/base.rb', line 52
def table_name
name = self.class.table_name || demodulize(self.class.name)
formatted_table_name(name)
end
|
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
# File 'lib/toolhound-ruby/base.rb', line 326
def transform_attribute_key(key)
renamed = self.class.renamed_attributes
if renamed.include? key
renamed[key].to_sym
else
if DB_TYPE_REGEX =~ key
word = key[3..key.length]
underscore(word).to_sym
else
underscore(key).to_sym
end
end
end
|
313
314
315
316
317
318
319
320
321
322
323
324
|
# File 'lib/toolhound-ruby/base.rb', line 313
def transform_attributes(attrs)
hash = {}
attrs.each do |k, v|
key = transform_attribute_key(k)
if hash.include? key
hash[:"#{key}1"] = v
else
hash[key] = v
end
end
hash
end
|