Class: Toolhound::Base
- Inherits:
-
Object
show all
- Includes:
- Util
- Defined in:
- lib/toolhound-ruby/base.rb
Direct Known Subclasses
Inventory, InventoryItem, Job, Manufacturer, Project, PurchaseOrder, PurchaseOrderItem, Rental, RentalCharge, RentalItem, Transaction, Vendor
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/toolhound-ruby/base.rb', line 199
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/toolhound-ruby/base.rb', line 156
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/toolhound-ruby/base.rb', line 228
def _build_where(wheres)
arr = []
case wheres.class.to_s
when "String"
arr << 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
301
302
303
304
|
# File 'lib/toolhound-ruby/base.rb', line 301
def build_and_query(options, query_options = {})
sql = build_sql(options)
results = query(sql, query_options)
end
|
#build_joins(joins) ⇒ Object
195
196
197
|
# File 'lib/toolhound-ruby/base.rb', line 195
def build_joins(joins)
_build_joins(joins).join(" ")
end
|
#build_selects(selects) ⇒ Object
152
153
154
|
# File 'lib/toolhound-ruby/base.rb', line 152
def build_selects(selects)
_build_selects(selects).join(", ")
end
|
#build_sql(obj) ⇒ Object
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
# File 'lib/toolhound-ruby/base.rb', line 307
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
223
224
225
226
|
# File 'lib/toolhound-ruby/base.rb', line 223
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
|
281
282
283
284
285
286
287
288
289
290
|
# File 'lib/toolhound-ruby/base.rb', line 281
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
|
139
140
141
142
143
144
145
|
# File 'lib/toolhound-ruby/base.rb', line 139
def formatted_table_name(table)
table = table.to_s
unless /^tbl/i =~ table
table = "tbl#{camelize(table, true)}"
end
camelize(table, false)
end
|
147
148
149
150
|
# File 'lib/toolhound-ruby/base.rb', line 147
def formmatted_column_name(column)
column = column.to_s
camelize(column, false)
end
|
#get_operator(op, value) ⇒ Object
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# File 'lib/toolhound-ruby/base.rb', line 263
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
|
#insert(table, variables) ⇒ Object
105
106
107
108
|
# File 'lib/toolhound-ruby/base.rb', line 105
def insert(table, variables)
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
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/toolhound-ruby/base.rb', line 110
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
|
#procedure(procedure_name, variables = {}) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/toolhound-ruby/base.rb', line 90
def procedure(procedure_name, variables = {})
vars = transform_procedure_variables(variables)
pairs = vars.map {|pair| pair.join(" = ") }
vars_query = pairs.join(", ")
sql = "EXECUTE dbo.#{procedure_name} #{vars_query};"
results = connection.execute(sql)
end
|
#query(query, options = {}) ⇒ Object
292
293
294
295
296
297
298
299
|
# File 'lib/toolhound-ruby/base.rb', line 292
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
|
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
# File 'lib/toolhound-ruby/base.rb', line 364
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
|
333
334
335
336
337
338
339
340
341
342
343
344
|
# File 'lib/toolhound-ruby/base.rb', line 333
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
|
358
359
360
361
362
|
# File 'lib/toolhound-ruby/base.rb', line 358
def transform_procedure_key(key)
key = key.to_s
key = camelize(key, true)
"@p_#{key}"
end
|
354
355
356
|
# File 'lib/toolhound-ruby/base.rb', line 354
def transform_procedure_value(value)
"'#{value}'"
end
|
346
347
348
349
350
351
352
|
# File 'lib/toolhound-ruby/base.rb', line 346
def transform_procedure_variables(variables)
vars = []
variables.each do |key, value|
vars << [transform_procedure_key(key), transform_procedure_value(value)]
end
vars
end
|