Class: CrudService::Dal
- Inherits:
-
Object
- Object
- CrudService::Dal
- Defined in:
- lib/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
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#log ⇒ Object
Returns the value of attribute log.
-
#memcache ⇒ Object
Returns the value of attribute memcache.
-
#mysql ⇒ Object
Returns the value of attribute mysql.
-
#primary_key ⇒ Object
Returns the value of attribute primary_key.
-
#relations ⇒ Object
Returns the value of attribute relations.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
-
#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.
-
#build_equal_condition(v) ⇒ Object
Return an escaped condition string for the value v.
-
#build_fields(query) ⇒ Object
Get fields.
-
#build_fields_with_ns(query, ns) ⇒ Object
Get fields with a namespace.
-
#build_insert(data) ⇒ Object
Build SQL INSERT fragment from data.
-
#build_select_fields(fields, ns) ⇒ Object
Get fields.
-
#build_update(data) ⇒ Object
Build SQL UPDATE fragment from data.
-
#build_where(query) ⇒ Object
Build a simple where clause from the given query.
- #build_where_ns(query, ns) ⇒ Object
-
#cached_query(query, tables) ⇒ Object
Execute a Query, reading from cache if enabled.
-
#delete_by_primary_key(primary_key) ⇒ Object
Delete a record by its primary key from data.
-
#escape_str_field(str) ⇒ Object
Escape a field name.
-
#escape_value(v) ⇒ Object
Return an escaped SQL string for the value v.
-
#exists_by_primary_key?(primary_key) ⇒ Boolean
Return true if a key exists.
-
#expire_table_cache(table_names) ⇒ Object
Expire a table cache by incrementing the table version.
-
#get_all_by_query(query) ⇒ Object
Get All records via a query.
-
#get_all_by_query_as_hash(query) ⇒ Object
Get all records for this entity and map ids to a hash.
-
#get_all_related_tables ⇒ Object
Return an array of all related tables plus this table.
-
#get_excludes(query) ⇒ Object
Get excludes.
-
#get_has_many_relation_query_sql(relation, query) ⇒ Object
Get the SQL query for a has_many relation.
-
#get_has_many_through_relation_query_sql(relation, query) ⇒ Object
Get the SQL query for a has_many_through relation.
-
#get_has_one_relation_query_sql(relation, query) ⇒ Object
Get the SQL query for a has_one relation.
-
#get_includes(query) ⇒ Object
Get includes.
-
#get_one(query) ⇒ Object
Get one record via a query.
-
#get_relation_data_as_hash(query) ⇒ Object
Get data for included relations for a query.
-
#get_relation_query_sql(relation, query) ⇒ Object
Get sql to load relation.
-
#get_relation_tables(relation) ⇒ Object
Get an array of table names involved in a relation query.
-
#initialize(mysql, memcache = nil, log) ⇒ Dal
constructor
Create an instance.
-
#insert(data) ⇒ Object
Create a record from data.
- #map_in_included_relations!(result, query) ⇒ Object
-
#map_to_hash_by_primary_key(result) ⇒ Object
Map a result array to a hash by primary key.
-
#map_to_hash_of_arrays_by_key(result, key) ⇒ Object
Map a result array to a hash of arrays by a specific key.
- #remove_key_from_hash_of_arrays!(hash, key) ⇒ Object
-
#update_by_primary_key(primary_key, data) ⇒ Object
Update a record by its primary key from data.
-
#valid_insert?(data) ⇒ Boolean
Return true if an object is valid for create.
-
#valid_query?(query) ⇒ Boolean
Determine if all fields and includes in the query are available.
-
#valid_update?(data) ⇒ Boolean
Return true if an object is valid for update.
Constructor Details
#initialize(mysql, memcache = nil, log) ⇒ Dal
Create an instance.
13 14 15 16 17 |
# File 'lib/dal.rb', line 13 def initialize(mysql, memcache = nil, log) @mysql = mysql @memcache = memcache @log = log end |
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
10 11 12 |
# File 'lib/dal.rb', line 10 def fields @fields end |
#log ⇒ Object
Returns the value of attribute log.
10 11 12 |
# File 'lib/dal.rb', line 10 def log @log end |
#memcache ⇒ Object
Returns the value of attribute memcache.
10 11 12 |
# File 'lib/dal.rb', line 10 def memcache @memcache end |
#mysql ⇒ Object
Returns the value of attribute mysql.
10 11 12 |
# File 'lib/dal.rb', line 10 def mysql @mysql end |
#primary_key ⇒ Object
Returns the value of attribute primary_key.
10 11 12 |
# File 'lib/dal.rb', line 10 def primary_key @primary_key end |
#relations ⇒ Object
Returns the value of attribute relations.
10 11 12 |
# File 'lib/dal.rb', line 10 def relations @relations end |
#table_name ⇒ Object
Returns the value of attribute table_name.
10 11 12 |
# File 'lib/dal.rb', line 10 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
279 280 281 282 283 284 |
# File 'lib/dal.rb', line 279 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
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/dal.rb', line 139 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
163 164 165 |
# File 'lib/dal.rb', line 163 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
168 169 170 |
# File 'lib/dal.rb', line 168 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
119 120 121 122 123 124 125 126 127 |
# File 'lib/dal.rb', line 119 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
153 154 155 156 157 158 159 160 |
# File 'lib/dal.rb', line 153 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
130 131 132 133 134 135 136 |
# File 'lib/dal.rb', line 130 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
98 99 100 101 102 103 104 105 106 |
# File 'lib/dal.rb', line 98 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
108 109 110 111 112 113 114 115 116 |
# File 'lib/dal.rb', line 108 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.
20 21 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 |
# File 'lib/dal.rb', line 20 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
450 451 452 453 454 455 456 457 458 459 460 461 462 |
# File 'lib/dal.rb', line 450 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() true end |
#escape_str_field(str) ⇒ Object
Escape a field name
187 188 189 190 |
# File 'lib/dal.rb', line 187 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
173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/dal.rb', line 173 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
374 375 376 377 378 |
# File 'lib/dal.rb', line 374 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
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/dal.rb', line 357 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
200 201 202 203 204 205 |
# File 'lib/dal.rb', line 200 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
208 209 210 |
# File 'lib/dal.rb', line 208 def get_all_by_query_as_hash(query) map_to_hash_by_primary_key(get_all_by_query(query)) end |
#get_all_related_tables ⇒ Object
Return an array of all related tables plus this table
465 466 467 468 469 470 471 472 |
# File 'lib/dal.rb', line 465 def 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
293 294 295 296 |
# File 'lib/dal.rb', line 293 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
323 324 325 326 327 328 329 330 |
# File 'lib/dal.rb', line 323 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
333 334 335 336 337 338 339 340 |
# File 'lib/dal.rb', line 333 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
313 314 315 316 317 318 319 320 |
# File 'lib/dal.rb', line 313 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
287 288 289 290 |
# File 'lib/dal.rb', line 287 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
193 194 195 196 197 |
# File 'lib/dal.rb', line 193 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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/dal.rb', line 227 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
299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/dal.rb', line 299 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
343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/dal.rb', line 343 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
418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/dal.rb', line 418 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_one({@primary_key => data[@primary_key]}) end |
#map_in_included_relations!(result, query) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/dal.rb', line 212 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
256 257 258 259 260 261 262 263 264 |
# File 'lib/dal.rb', line 256 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
267 268 269 270 271 272 273 274 275 276 |
# File 'lib/dal.rb', line 267 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
246 247 248 249 250 251 252 253 |
# File 'lib/dal.rb', line 246 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
434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/dal.rb', line 434 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_one({@primary_key => primary_key}) end |
#valid_insert?(data) ⇒ Boolean
Return true if an object is valid for create
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/dal.rb', line 381 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
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/dal.rb', line 78 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
402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/dal.rb', line 402 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 |