Class: CrudService::GenericDal

Inherits:
Object
  • Object
show all
Defined in:
lib/generic_dal.rb

Overview

This class creates an instance of a generic DAL (Data Access Layer) with cache capability from the provided mysql client, logger and optionally memcache client. Your should extend this class to provide configuration for your dal, please see the README file at github.com/tomcully/crud-service

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mysql, memcache = nil, log) ⇒ GenericDal

Create an instance.



15
16
17
18
19
# File 'lib/generic_dal.rb', line 15

def initialize(mysql, memcache = nil, log) 
  @mysql = mysql
  @memcache = memcache
  @log = log
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



12
13
14
# File 'lib/generic_dal.rb', line 12

def fields
  @fields
end

#logObject

Returns the value of attribute log.



12
13
14
# File 'lib/generic_dal.rb', line 12

def log
  @log
end

#memcacheObject

Returns the value of attribute memcache.



12
13
14
# File 'lib/generic_dal.rb', line 12

def memcache
  @memcache
end

#mysqlObject

Returns the value of attribute mysql.



12
13
14
# File 'lib/generic_dal.rb', line 12

def mysql
  @mysql
end

#primary_keyObject

Returns the value of attribute primary_key.



12
13
14
# File 'lib/generic_dal.rb', line 12

def primary_key
  @primary_key
end

#relationsObject

Returns the value of attribute relations.



12
13
14
# File 'lib/generic_dal.rb', line 12

def relations
  @relations
end

#table_nameObject

Returns the value of attribute table_name.



12
13
14
# File 'lib/generic_dal.rb', line 12

def table_name
  @table_name
end

Instance Method Details

#add_field_from_map!(result, map, field_name, key_name) ⇒ Object

Add a field to each record from map using another field as a key



282
283
284
285
286
287
# File 'lib/generic_dal.rb', line 282

def add_field_from_map!(result, map, field_name, key_name)
  out = []
  result.each do |record|
    record[field_name] = map[record[key_name]] if map.has_key?(record[key_name])
  end
end

#build_equal_condition(v) ⇒ Object

Return an escaped condition string for the value v



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/generic_dal.rb', line 142

def build_equal_condition(v) 
  if v.nil?
    # Nulls (nil)
    return "IS NULL"
  elsif v.kind_of? Integer or v.kind_of? Float
    # Integers / Floats
    return "= "+v.to_s
  else
    # Everything Else
    return "= '#{@mysql.escape(v.to_s)}'" 
  end
end

#build_fields(query) ⇒ Object

Get fields



166
167
168
# File 'lib/generic_dal.rb', line 166

def build_fields(query)
  build_select_fields(@fields.keys - get_excludes(query),nil)
end

#build_fields_with_ns(query, ns) ⇒ Object

Get fields with a namespace



171
172
173
# File 'lib/generic_dal.rb', line 171

def build_fields_with_ns(query, ns)
  build_select_fields(@fields.keys - get_excludes(query),ns)
end

#build_insert(data) ⇒ Object

Build SQL INSERT fragment from data



122
123
124
125
126
127
128
129
130
# File 'lib/generic_dal.rb', line 122

def build_insert(data)
  fields = ""
  values = ""
  data.each do |k,v|
    fields += "`#{escape_str_field(k)}`, "
    values += escape_value(v)+", "
  end
  "("+fields.chomp(', ')+") VALUES ("+values.chomp(', ')+")"
end

#build_select_fields(fields, ns) ⇒ Object

Get fields



156
157
158
159
160
161
162
163
# File 'lib/generic_dal.rb', line 156

def build_select_fields(fields,ns)
  select = ""
  fields.each do |k|
    select += "`#{ns}`." unless ns.nil?
    select += "`#{k}`,"
  end
  select.chomp(',')
end

#build_update(data) ⇒ Object

Build SQL UPDATE fragment from data



133
134
135
136
137
138
139
# File 'lib/generic_dal.rb', line 133

def build_update(data)
  sql = ""
  data.each do |k,v|
    sql += "`#{escape_str_field(k)}` = "+escape_value(v)+", "
  end
  sql.chomp(", ")
end

#build_where(query) ⇒ Object

Build a simple where clause from the given query



101
102
103
104
105
106
107
108
109
# File 'lib/generic_dal.rb', line 101

def build_where(query)
  where = ""
  query.each_pair do |k, v| 
    if (k!='include' and k!='exclude')
      where += "(`#{escape_str_field(k)}` #{build_equal_condition(v)}) AND "
    end
  end
  where.chomp(' AND ')
end

#build_where_ns(query, ns) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/generic_dal.rb', line 111

def build_where_ns(query,ns)
  where = ""
  query.each_pair do |k, v| 
    if (k!='include' and k!='exclude')
      where += "(`#{ns}`.`#{escape_str_field(k)}` #{build_equal_condition(v)}) AND "
    end
  end
  where.chomp(' AND ')
end

#cached_query(query, tables) ⇒ Object

Execute a Query, reading from cache if enabled.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/generic_dal.rb', line 22

def cached_query(query, tables)

  unless @memcache.nil?

    unless tables.include? @table_name
      tables.push @table_name
      tables.sort!
    end

    # Get Table versions
    table_versions = ""

    tables.each do |table|
      tbversion = @memcache.get(table+"-version")
      if tbversion.nil?
        expire_table_cache([table]) 
        tbversion = 1
      end
      table_versions += table+"-"+tbversion.to_s
    end

    # Get the Query Hash
    querymd5 = "geoservice-"+Digest::MD5.hexdigest(query+":"+table_versions)

    # Read Cache and return if hit
    results = @memcache.get querymd5

    unless results.nil?
      return results 
    end

  end

  # Perform the Query
  begin
    queryresult = @mysql.query(query)
  rescue Exception => e
    @log.error("#{e}")
    return []
  end

  # Collate Results
  results = []
  unless queryresult.nil? or queryresult.count == 0
    queryresult.each do |h|
      results.push h
    end
  end

  unless @memcache.nil?
    # Write to Cache
    @memcache.set querymd5, results
  end

  # Return results
  results
end

#delete_by_primary_key(primary_key) ⇒ Object

Delete a record by its primary key from data



453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/generic_dal.rb', line 453

def delete_by_primary_key(primary_key)
  query = "DELETE FROM `#{@table_name}` WHERE "+build_where({@primary_key => primary_key})
  
  begin
    queryresult = @mysql.query(query)
  rescue Exception => e
    @log.error("#{e}")
    return false
  end

  expire_table_cache(get_all_related_tables)
  true
end

#escape_str_field(str) ⇒ Object

Escape a field name



190
191
192
193
# File 'lib/generic_dal.rb', line 190

def escape_str_field(str)
  str = str.to_s.sub(/\`/,'')
  @mysql.escape(str)
end

#escape_value(v) ⇒ Object

Return an escaped SQL string for the value v



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/generic_dal.rb', line 176

def escape_value(v) 
  if v.nil?
    # Nulls (nil)
    return "NULL"
  elsif v.kind_of? Integer or v.kind_of? Float
    # Integers / Floats
    return v.to_s
  else
    # Everything Else
    return "'#{@mysql.escape(v.to_s)}'" 
  end
end

#exists_by_primary_key?(primary_key) ⇒ Boolean

Return true if a key exists

Returns:

  • (Boolean)


377
378
379
380
381
# File 'lib/generic_dal.rb', line 377

def exists_by_primary_key?(primary_key)
  qry = "SELECT COUNT(*) AS `c` FROM `#{@table_name}` WHERE "+build_where({@primary_key => primary_key})
  res = cached_query(qry,[@table_name])
  res[0]['c'] != 0
end

#expire_table_cache(table_names) ⇒ Object

Expire a table cache by incrementing the table version



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/generic_dal.rb', line 360

def expire_table_cache(table_names)
  return if @memcache.nil?

  table_names.each do |table_name|
    key = table_name+"-version"
    version = @memcache.get(key)
    if version.nil?
      @memcache.set(key,1,nil,{:raw=>true}) 
    else
      @memcache.incr(key, 1, nil)
    end
  end

  true
end

#get_all_by_query(query) ⇒ Object

Get All records via a query



203
204
205
206
207
208
# File 'lib/generic_dal.rb', line 203

def get_all_by_query(query)
  qry = "SELECT #{build_fields(query)} FROM `#{@table_name}`"
  where = build_where(query)
  qry += " WHERE #{where}" unless where.length == 0
  cached_query(qry,[@table_name])
end

#get_all_by_query_as_hash(query) ⇒ Object

Get all records for this entity and map ids to a hash



211
212
213
# File 'lib/generic_dal.rb', line 211

def get_all_by_query_as_hash(query)
  map_to_hash_by_primary_key(get_all_by_query(query))
end

Return an array of all related tables plus this table



468
469
470
471
472
473
474
475
# File 'lib/generic_dal.rb', line 468

def get_all_related_tables
  tables = [ @table_name ]
  return tables if @relations.nil?
  @relations.each do |n,r|
    tables = tables | get_relation_tables(r)
  end
  tables.sort
end

#get_excludes(query) ⇒ Object

Get excludes



296
297
298
299
# File 'lib/generic_dal.rb', line 296

def get_excludes(query)
  return [] if query.nil? or !query.has_key?('exclude') or query['exclude'].nil?
  query['exclude'].split(',')
end

#get_has_many_relation_query_sql(relation, query) ⇒ Object

Get the SQL query for a has_many relation



326
327
328
329
330
331
332
333
# File 'lib/generic_dal.rb', line 326

def get_has_many_relation_query_sql(relation, query)
  fields = build_select_fields(relation[:table_fields].split(','),'a')

  qry = "SELECT #{fields},`b`.`#{relation[:this_key]}` AS `_table_key` FROM `#{relation[:table]}` AS `a`, `#{@table_name}` AS `b` WHERE (`a`.`#{relation[:table_key]}` = `b`.`#{relation[:this_key]}`)"
  where = build_where_ns(query,'b')
  qry += " AND #{where}" unless where.length == 0
  qry
end

#get_has_many_through_relation_query_sql(relation, query) ⇒ Object

Get the SQL query for a has_many_through relation



336
337
338
339
340
341
342
343
# File 'lib/generic_dal.rb', line 336

def get_has_many_through_relation_query_sql(relation,query)
  fields = build_select_fields(relation[:table_fields].split(','),'a')

  qry = "SELECT #{fields},`c`.`#{relation[:this_key]}` AS `_table_key` FROM `#{relation[:table]}` AS `a`, `#{relation[:link_table]}` AS `b`, `#{@table_name}` AS `c` WHERE (`a`.`#{relation[:table_key]}` = `b`.`#{relation[:link_field]}` AND `b`.`#{relation[:link_key]}` = `c`.`#{relation[:this_key]}`)"
  where = build_where_ns(query,'c')
  qry += " AND #{where}" unless where.length == 0
  qry
end

#get_has_one_relation_query_sql(relation, query) ⇒ Object

Get the SQL query for a has_one relation



316
317
318
319
320
321
322
323
# File 'lib/generic_dal.rb', line 316

def get_has_one_relation_query_sql(relation, query)
  fields = build_select_fields(relation[:table_fields].split(','),'a')

  qry = "SELECT #{fields},`b`.`#{relation[:this_key]}` AS `_table_key` FROM `#{relation[:table]}` AS `a`, `#{@table_name}` AS `b` WHERE (`a`.`#{relation[:table_key]}` = `b`.`#{relation[:this_key]}`)"
  where = build_where_ns(query,'b')
  qry += " AND #{where}" unless where.length == 0
  qry
end

#get_includes(query) ⇒ Object

Get includes



290
291
292
293
# File 'lib/generic_dal.rb', line 290

def get_includes(query)
  return [] if query.nil? or !query.has_key?('include') or query['include'].nil?
  query['include'].split(',')
end

#get_one(query) ⇒ Object

Get one record via a query



196
197
198
199
200
# File 'lib/generic_dal.rb', line 196

def get_one(query)
  res = get_all_by_query(query)
  return nil if res.length == 0
  res[0]
end

#get_relation_data_as_hash(query) ⇒ Object

Get data for included relations for a query



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/generic_dal.rb', line 230

def get_relation_data_as_hash(query) 
  return {} if @relations.nil?

  includes = get_includes(query)

  reldata = {}

  @relations.each do |name, relation| 
    unless includes.find_index(name).nil?
      sql = get_relation_query_sql(relation, query)
      tables = get_relation_tables(relation)
      data = cached_query(sql,tables)
      reldata[name] = map_to_hash_of_arrays_by_key(data,'_table_key')
      remove_key_from_hash_of_arrays!(reldata[name],'_table_key')
    end
  end
  reldata
end

#get_relation_query_sql(relation, query) ⇒ Object

Get sql to load relation



302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/generic_dal.rb', line 302

def get_relation_query_sql(relation, query)
  case relation[:type]
  when :has_one
    return get_has_one_relation_query_sql(relation, query)
  when :has_many
    return get_has_many_relation_query_sql(relation, query)
  when :has_many_through
    return get_has_many_through_relation_query_sql(relation, query)
  else
    @log.error("Relation type #{relation[:type]} undefined!")
  end
end

#get_relation_tables(relation) ⇒ Object

Get an array of table names involved in a relation query



346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/generic_dal.rb', line 346

def get_relation_tables(relation) 
  case relation[:type]
  when :has_one
    return [@table_name, relation[:table]].sort
  when :has_many
    return [@table_name, relation[:table]].sort
  when :has_many_through
    return [@table_name, relation[:table], relation[:link_table]].sort
  else
    throw "Unknown Relation type #{relation.type}"
  end
end

#insert(data) ⇒ Object

Create a record from data



421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/generic_dal.rb', line 421

def insert(data)
  query = "INSERT INTO `#{@table_name}` "+build_insert(data)

  begin
    queryresult = @mysql.query(query)
  rescue Exception => e
    @log.error("#{e}")
    return false
  end

  expire_table_cache(get_all_related_tables)

  get_one({@primary_key => data[@primary_key]})
end

#map_in_included_relations!(result, query) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/generic_dal.rb', line 215

def map_in_included_relations!(result, query)
  dat = get_relation_data_as_hash(query)
  result.each do |res|
    dat.each do |name, lookup|
      res[name] = lookup[res[@relations[name][:this_key]]]
      if @relations[name][:type] == :has_one
        res[name] = res[name][0] unless res[name].nil?
      else 
        res[name] = [] if res[name].nil?
      end
    end
  end
end

#map_to_hash_by_primary_key(result) ⇒ Object

Map a result array to a hash by primary key



259
260
261
262
263
264
265
266
267
# File 'lib/generic_dal.rb', line 259

def map_to_hash_by_primary_key(result) 
  hash = {}

  result.each do |record|
    hash[record[@primary_key]] = record
  end

  hash
end

#map_to_hash_of_arrays_by_key(result, key) ⇒ Object

Map a result array to a hash of arrays by a specific key



270
271
272
273
274
275
276
277
278
279
# File 'lib/generic_dal.rb', line 270

def map_to_hash_of_arrays_by_key(result,key) 
  res = {}

  result.each do |record|
    res[record[key]] = [] unless res.has_key?(record[key])
    res[record[key]].push record
  end

  res
end

#remove_key_from_hash_of_arrays!(hash, key) ⇒ Object



249
250
251
252
253
254
255
256
# File 'lib/generic_dal.rb', line 249

def remove_key_from_hash_of_arrays!(hash,key)
  hash.each do |name,arr|
    arr.each do |record|
      record.delete(key)
    end
  end
  hash
end

#update_by_primary_key(primary_key, data) ⇒ Object

Update a record by its primary key from data



437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/generic_dal.rb', line 437

def update_by_primary_key(primary_key, data)
  query = "UPDATE `#{@table_name}` SET "+build_update(data)+" WHERE "+build_where({@primary_key => primary_key})

  begin
    queryresult = @mysql.query(query)
  rescue Exception => e
    @log.error("#{e}")
    return false
  end

  expire_table_cache(get_all_related_tables)

  get_one({@primary_key => primary_key})
end

#valid_insert?(data) ⇒ Boolean

Return true if an object is valid for create

Returns:

  • (Boolean)


384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/generic_dal.rb', line 384

def valid_insert?(data)
  return false if data.nil?
  return false if data.keys.length == 0
  
  # Required fields
  @fields.each do |k,s|
    return false if s.has_key?(:required) and s[:required] == true and !data.has_key?(k)
  end

  # Only valid fields, length checking
  data.each_key do |k|
    return false if !@fields.has_key?(k)
    return false if @fields[k].has_key?(:length) and
      !data[k].nil? and
      data[k].length > @fields[k][:length]
  end

  return true
end

#valid_query?(query) ⇒ Boolean

Determine if all fields and includes in the query are available

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/generic_dal.rb', line 81

def valid_query?(query)
  return false if query.nil?
  return true if query.keys.length == 0
  
  query.each_key do |k|
    return false if !@fields.has_key?(k) and k!='include' and k!='exclude'
  end

  get_includes(query).each do |k|
    return false if !@fields.has_key?(k) and !@relations.has_key?(k)
  end

  get_excludes(query).each do |k|
    return false if !@fields.has_key?(k)
  end

  true
end

#valid_update?(data) ⇒ Boolean

Return true if an object is valid for update

Returns:

  • (Boolean)


405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/generic_dal.rb', line 405

def valid_update?(data)
  return false if data.nil?
  return false if data.keys.length == 0

  # Only valid fields, length checking
  data.each_key do |k|
    return false if !@fields.has_key?(k)
    return false if @fields[k].has_key?(:length) and
      !data[k].nil? and
      data[k].length > @fields[k][:length]
  end

  return true
end