Class: Trix51::TupleSet
- Inherits:
-
Object
- Object
- Trix51::TupleSet
- Defined in:
- lib/trix51db.rb
Overview
This class represents a set of records which can either be a table, query result, or the result of a group expression.
Instance Attribute Summary collapse
-
#classname ⇒ Object
Key accessors for the tupleset class.
-
#dbref ⇒ Object
Key accessors for the tupleset class.
-
#indices ⇒ Object
Key accessors for the tupleset class.
-
#tablename ⇒ Object
Key accessors for the tupleset class.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Returns table record at index.
-
#all ⇒ Object
Returns all the records.
-
#args_to_hash(*args) ⇒ Object
:nodoc:.
-
#build_sorted_keys(field, dir = 'asc') ⇒ Object
:nodoc:.
-
#build_temp_idx(field) ⇒ Object
:nodoc:.
-
#check_required_fields(hashdata) ⇒ Object
:nodoc:.
-
#clone_structure(tupleset) ⇒ Object
:nodoc:.
-
#column_add(name, spec) ⇒ Object
Add a column to the table.
-
#column_remove(name) ⇒ Object
Remove a column from the table.
-
#column_update(name, spec) ⇒ Object
Update the specification for a column.
-
#comparison(value, operand, target) ⇒ Object
:nodoc:.
-
#convert_type(value, typedef) ⇒ Object
Converts a value to the specified type.
-
#count ⇒ Object
Returns the number of records in the table or result set.
-
#create_index(fieldname) ⇒ Object
Creates an index on the specified field.
-
#default(fieldname) ⇒ Object
Returns the default for the specified field.
-
#delete(*args) ⇒ Object
Deletes records matching the query.
-
#delete_hash(hashdata) ⇒ Object
delete records matching the specified hash ==== Example num_deleted = animal.delete_hash( { name: ‘cow’ } ).
-
#distinct(*keylist) ⇒ Object
Returns a new result set containing a list of the unique values.
-
#dump ⇒ Object
Produces a neatly formatted dump of the records (all fields).
-
#dump_filtered(*fieldnames) ⇒ Object
Produces a neatly formatted dump of the records (selected fields).
-
#each ⇒ Object
Interate over records in the table or resultset.
-
#each_key ⇒ Object
Interates through each key in the dbm store.
-
#each_pair ⇒ Object
Iterates through each key, value for the table.
-
#each_record ⇒ Object
Iterates through each Trix51::Tuple in the table or result set.
-
#each_value ⇒ Object
Interates through each Trix51::Tuple in the table or result set.
-
#empty ⇒ Object
Deletes all records in the table.
-
#empty_rec ⇒ Object
:nodoc:.
-
#eval_in_context(hashdata, expression) ⇒ Object
:nodoc:.
-
#exists?(fieldname) ⇒ Boolean
Returns true / false if the table contains field.
-
#find_or_create(*args) ⇒ Object
Find (select) or create a record with the named arguments.
-
#find_or_create_seq(fieldname) ⇒ Object
:nodoc:.
-
#first(*args) ⇒ Object
Returns the first record matching the query (Trix51::Tuple).
-
#get_hash_key(hashdata) ⇒ Object
:nodoc:.
-
#group(*args) ⇒ Object
Groups records by the array of fields specified in the by: clause, and returns the aggregated results for the fields specified in the calculate: clause.
-
#group_key(hashdata, keylist) ⇒ Object
:nodoc:.
-
#having(*args) ⇒ Object
Alias for select().
-
#hint_criteria_to_operand_target(criteria) ⇒ Object
:nodoc:.
-
#hint_matches_by_index(taglist, field, operand, target) ⇒ Object
:nodoc:.
-
#hint_matches_by_recordscan(taglist, field, operand, target) ⇒ Object
:nodoc:.
-
#index_add(urid, record) ⇒ Object
:nodoc:.
-
#index_build(field) ⇒ Object
Rebuilds the index for the given field.
-
#index_chk_unique(fieldname, fieldvalue) ⇒ Object
:nodoc:.
-
#index_chk_unique_update(fieldname, fieldvalue, urid) ⇒ Object
:nodoc:.
-
#index_remove(urid) ⇒ Object
:nodoc:.
-
#index_update(oldurid, newurid, record) ⇒ Object
:nodoc:.
-
#indexed?(field) ⇒ Boolean
returns true or false if a table field is indexed.
-
#indices_load(path) ⇒ Object
:nodoc:.
-
#indices_save(path) ⇒ Object
:nodoc:.
-
#init_record(urid) ⇒ Object
:nodoc:.
-
#initialize(tablename, dataref, options) ⇒ TupleSet
constructor
Creates a new TupleSet object, the core class for tables / query results.
-
#insert(*args) ⇒ Object
Inserts a record into the database using named arguments.
-
#insert_hash(hashdata) ⇒ Object
insert record into the database using a hash ==== Example daisy = animal.insert_hash( { name: ‘cow’, species_id: 2 } ).
-
#key?(field) ⇒ Boolean
returns true or false if a table field forms part of the record key.
-
#key_fields ⇒ Object
Returns the key fields for the table.
-
#length ⇒ Object
Returns the number of records in the table or result set.
-
#limit(count) ⇒ Object
Returns a new result set contain only the first count records.
-
#meta ⇒ Object
:nodoc:.
-
#meta=(meta) ⇒ Object
:nodoc:.
-
#meta_prefix ⇒ Object
:nodoc:.
-
#meta_record?(key) ⇒ Boolean
:nodoc:.
-
#method_missing(meth, *args) ⇒ Object
:nodoc:.
-
#push(record) ⇒ Object
:nodoc:.
-
#random ⇒ Object
Returns a random record from the table or result set.
-
#record_keys ⇒ Object
:nodoc:.
-
#record_prefix ⇒ Object
:nodoc:.
-
#select(*args) ⇒ Object
Returns a Trix51::ResultSet containing records matching the query.
-
#select_hash(criteria) ⇒ Object
Return records based on the specified query.
-
#seq_prefix(fieldname) ⇒ Object
:nodoc:.
-
#seq_record?(key) ⇒ Boolean
:nodoc:.
-
#size ⇒ Object
Returns the number of records in the database.
-
#sort(field, dir = 'asc') ⇒ Object
Sorts records by specified field and ordering, returning a new result set.
-
#sorted_keys ⇒ Object
:nodoc:.
-
#structure ⇒ Object
:nodoc:.
-
#table? ⇒ Boolean
Returns true if the given object is a table, false otherwise.
-
#table_record?(key) ⇒ Boolean
:nodoc:.
-
#tuple_by_key(*args) ⇒ Object
:nodoc:.
-
#unique?(field) ⇒ Boolean
returns true or false if a table field must be unique.
-
#update(*args) ⇒ Object
Updates records matching the query.
-
#update_hash(hashdata) ⇒ Object
Updates any records matching the values in hash ==== Example animal.update_hash ( { name: ‘cow’ } ) do |record| record.name = ‘sheep’ # baaa? end.
Constructor Details
#initialize(tablename, dataref, options) ⇒ TupleSet
Creates a new TupleSet object, the core class for tables / query results.
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/trix51db.rb', line 482 def initialize( tablename, dataref, ) @tablename = tablename @dbref = dataref @options = {} @indices = {} @sort_order = nil @options = @updateable = false @meta = nil if self.table? then #pp "table" @updateable = true @meta = self. else @meta = {} #pp "query or view" @meta[:tablename] = '__query' self.clone_structure( @options[:tableref] ) @meta[:classname] = 'Trix51::Tuple' end #pp @meta @classname = @meta[:classname] || 'Trix51::Tuple' #pp @classname if @classname != 'Trix51::Tuple' then eval( " class #{@classname} < Trix51::Tuple end ", BND ) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
:nodoc:
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 |
# File 'lib/trix51db.rb', line 932 def method_missing( meth, *args ) #:nodoc: name = meth.to_s if m = name.match( /^select_by_(.+)/ ) then fields = m[1].split('_and_') h = {} while fields.length > 0 do n = fields.shift v = args.shift h[n] = v end return self.select_hash( h ) end if m = name.match( /^delete_by_(.+)/ ) then fields = m[1].split('_and_') h = {} while fields.length > 0 do n = fields.shift v = args.shift h[n] = v end return self.delete_hash( h ) end if m = name.match( /^update_by_(.+)/ ) then fields = m[1].split('_and_') h = {} while fields.length > 0 do n = fields.shift v = args.shift h[n] = v end matches = self.select_hash( h ) matches.each { |record| yield record record.update if record.has_updates? } return matches.length end end |
Instance Attribute Details
#classname ⇒ Object
Key accessors for the tupleset class.
1818 1819 1820 |
# File 'lib/trix51db.rb', line 1818 def classname @classname end |
#dbref ⇒ Object
Key accessors for the tupleset class.
1818 1819 1820 |
# File 'lib/trix51db.rb', line 1818 def dbref @dbref end |
#indices ⇒ Object
Key accessors for the tupleset class.
1818 1819 1820 |
# File 'lib/trix51db.rb', line 1818 def indices @indices end |
#tablename ⇒ Object
Key accessors for the tupleset class.
1818 1819 1820 |
# File 'lib/trix51db.rb', line 1818 def tablename @tablename end |
Instance Method Details
#[](index) ⇒ Object
Returns table record at index.
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 |
# File 'lib/trix51db.rb', line 1629 def [](index) keys = [] self.sorted_keys.each do |k| keys.push( k ) if self.table_record?(k) end return nil if (keys.count == 0) or (keys.count <= index) urid = keys[ index ] return init_record( urid ) end |
#all ⇒ Object
Returns all the records.
832 833 834 |
# File 'lib/trix51db.rb', line 832 def all return self.select_hash( {} ) end |
#args_to_hash(*args) ⇒ Object
:nodoc:
846 847 848 849 850 851 852 853 854 855 |
# File 'lib/trix51db.rb', line 846 def args_to_hash( *args ) #:nodoc: h = {} args.each { |rec| rec.each_pair { |k,v| h[k] = v } } #pp h return h end |
#build_sorted_keys(field, dir = 'asc') ⇒ Object
:nodoc:
545 546 547 548 549 550 551 552 553 554 555 556 557 |
# File 'lib/trix51db.rb', line 545 def build_sorted_keys( field, dir='asc' ) #:nodoc: # sort records by list of fields idx = self.build_temp_idx( field ) sk = [] idx.keys.sort.each do |fieldvalue| keyref = idx[fieldvalue] keyref.each do |key| sk.push( key ) end end return sk if dir == 'asc' return sk.reverse if dir == 'desc' end |
#build_temp_idx(field) ⇒ Object
:nodoc:
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
# File 'lib/trix51db.rb', line 521 def build_temp_idx( field ) #:nodoc: if (self.indexed?( field )) and (not self.indices[field].nil?) and (field != :random) then return @indices[field].clone end res = {} self.dbref.each_pair { |k, v| next unless self.table_record?( k ) record = {} if v.class.name == 'String' then record = Marshal.load( v ) else record = v.data end rv = record[field] if field != :random rv = rand() if field == :random rlist = res[rv] || [] rlist.push( k ) res[rv] = rlist } return res end |
#check_required_fields(hashdata) ⇒ Object
:nodoc:
892 893 894 895 896 897 898 899 900 901 |
# File 'lib/trix51db.rb', line 892 def check_required_fields( hashdata ) #:nodoc: # make sure all required fields are populated self.structure.each_key { |key| next if (not self.structure[key][:key] == true) and (not self.structure[key][:required] == true) raise Trix51::ConstraintError, "required or key field #{self.tablename}.#{key} is nil" if hashdata[key].nil? raise Trix51::ConstraintError, "unique constraint violated for field #{self.tablename}.#{key} [#{hashdata[key]}]" if not self.index_chk_unique( key, hashdata[key] ) } end |
#clone_structure(tupleset) ⇒ Object
:nodoc:
587 588 589 |
# File 'lib/trix51db.rb', line 587 def clone_structure( tupleset ) #:nodoc: @meta[:structure] = tupleset.structure.clone end |
#column_add(name, spec) ⇒ Object
Add a column to the table.
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 |
# File 'lib/trix51db.rb', line 1140 def column_add( name, spec ) if not @meta[:structure][name].nil? then raise Trix51::ExistsError, "Column #{name} already exists" end # add in and reparse @meta[:structure][name] = spec self. = @meta @meta = self. #pp @meta need_rebuild = false # now assign default @dbref.each_pair do |k,v| next unless self.table_record?( k ) record = Marshal.load( v ) record[name] = self.default( name ) v = Marshal.dump( record ) newk = self.get_hash_key( record ) if newk != k then @dbref.delete(k) need_rebuild = true end @dbref[ newk ] = v end @indices.clear if need_rebuild end |
#column_remove(name) ⇒ Object
Remove a column from the table.
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 |
# File 'lib/trix51db.rb', line 1176 def column_remove( name ) if @meta[:structure][name].nil? then raise Trix51::NotFoundError, "Column #{name} does not exist" end # add in and reparse @meta[:structure].delete(name) self. = @meta @meta = self. need_rebuild = false # now assign default @dbref.each_pair do |k,v| next unless self.table_record?( k ) record = Marshal.load( v ) record.delete( name ) if record.has_key?( name ) v = Marshal.dump( record ) newk = self.get_hash_key( record ) if newk != k then @dbref.delete(k) need_rebuild = true end @dbref[ newk ] = v end @indices.clear if need_rebuild end |
#column_update(name, spec) ⇒ Object
Update the specification for a column.
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 |
# File 'lib/trix51db.rb', line 1209 def column_update( name, spec ) if @meta[:structure][name].nil? then raise Trix51::NotFoundError, "Column #{name} does not exist" end oldspec = @meta[:structure][name] newspec = Marshal.load( Marshal.dump( spec ) ) uval = {} ukey = {} @dbref.each_pair do |k,v| next unless self.table_record?( k ) record = Marshal.load( v ) value = record[name] # test field uniqueness if newspec[:unique] then raise Trix51::ConstraintError, "Field definition change would create a unique key violation condition" if not uval[value].nil? uval[value] = 1 end # type check nrecord = record.clone if newspec[:datatype] != oldspec[:datatype] then begin nrecord[name] = self.convert_type( value, newspec[:datatype] ).to_s #pp nrecord newurid = self.get_hash_key( nrecord ) if not ukey[newurid].nil? then raise Trix51::ConstraintError, "Field definition change would violate uniqueness of keys" end ukey[newurid] = 1 rescue raise Trix51::TypeError, "Type conversion from #{oldspec[:datatype]} to #{newspec[:datatype]} would barf..." end end end # update will be okay @meta[:structure][name] = newspec self. = @meta @meta = self. @indices.delete(name) if not @indices[name].nil? @dbref.each_pair do |oldurid,v| next unless oldurid.match( self.record_prefix ) old_record = Marshal.load( v ) new_record = old_record.clone if oldspec[name] != newspec[name] then new_record[name] = self.convert_type( old_record[name], newspec[:datatype] ) end newurid = self.get_hash_key( new_record ) if newurid != oldurid then @dbref.delete(oldurid) @dbref[newurid] = Marshal.dump( new_record ) else @dbref[oldurid] = Marshal.dump( new_record ) #self.index_update( oldurid, newurid, new_record[name] ) end end self.index_build( name ) if self.indexed?(name) end |
#comparison(value, operand, target) ⇒ Object
:nodoc:
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
# File 'lib/trix51db.rb', line 682 def comparison( value, operand, target ) #:nodoc: #puts "[#{value}] #{operand.to_s} [#{target}]" if operand.to_s == 'is' then return (value.to_s == target.to_s) elsif operand.to_s == 'not' then return (value.to_s != target.to_s) elsif operand.to_s == 'like' then target = '^' + target.gsub( /\%/, '.*' ) + '$' return (value.to_s.match( target )) elsif operand.to_s == 'unlike' then target = '^' + target.gsub( /\%/, '.*' ) + '$' return (not value.to_s.match( target )) elsif operand.to_s == 'under' then return (value.to_f < target.to_f) elsif operand.to_s == 'over' then return (value.to_f > target.to_f) elsif operand.to_s == 'before' then return (value.to_s < target.to_s) elsif operand.to_s == 'after' then return (value.to_s > target.to_s) elsif operand.to_s == 'between' then raise Trix51::TypeError, "BETWEEN clause requires two values" unless (target.class.name == 'Array') and (target.length > 1) s = target.sort lower_bound = s.shift upper_bound = s.pop return ( ( value.to_s >= lower_bound.to_s ) and ( value.to_s <= upper_bound.to_s ) ) elsif operand.to_s == 'in' then raise Trix51::TypeError, "IN clause requires an array" unless (target.class.name == 'Array') and (target.length >= 1) return (target.include?( value )) end end |
#convert_type(value, typedef) ⇒ Object
Converts a value to the specified type.
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 |
# File 'lib/trix51db.rb', line 1281 def convert_type( value, typedef ) newvalue = nil if typedef == 'string' then return value.to_s end if typedef == 'integer' then return value.to_i end if typedef == 'float' then return value.to_f end if typedef == 'datetime' then return Time.parse(value.to_s).to_s end end |
#count ⇒ Object
Returns the number of records in the table or result set.
1358 1359 1360 |
# File 'lib/trix51db.rb', line 1358 def count return self.size end |
#create_index(fieldname) ⇒ Object
Creates an index on the specified field.
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 |
# File 'lib/trix51db.rb', line 1780 def create_index( fieldname ) fieldname = fieldname raise Trix51::NotFoundError, "Table '#{self.tablename}' does not have a field '#{fieldname}'" unless self.exists?(fieldname) raise Trix51::ExistsError, "Table '#{self.tablename}' already has an index on field '#{fieldname}'" if self.indexed?(fieldname) @meta = self. self.structure[fieldname][:indexed] = true self. = @meta self.index_build( fieldname ) end |
#default(fieldname) ⇒ Object
Returns the default for the specified field.
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 |
# File 'lib/trix51db.rb', line 596 def default( fieldname ) unless self.exists?( fieldname) then raise Trix51::NotFoundError, "field not found: #{fieldname} in table #{@meta[:tablename]}" end ref = self.structure[fieldname] #pp ref if (ref[:autoincrement] == true) and (ref[:datatype] == 'integer') then return self.find_or_create_seq( fieldname ) end if (not ref[:default].nil?) then d = ref[:default] if m = d.to_s.match( /^\#\{(.+)\}$/ ) then return eval(m[1]) end return d end return nil end |
#delete(*args) ⇒ Object
Deletes records matching the query.
Example
count = animal.delete( species_id: 2 )
789 790 791 |
# File 'lib/trix51db.rb', line 789 def delete( *args ) return self.delete_hash( args_to_hash(*args) ) end |
#delete_hash(hashdata) ⇒ Object
delete records matching the specified hash
Example
num_deleted = animal.delete_hash( {
name: ‘cow’
} )
736 737 738 739 740 741 742 743 744 745 746 747 748 |
# File 'lib/trix51db.rb', line 736 def delete_hash( hashdata ) #:nodoc: matches = self.select_hash( hashdata ) matches.each_record do |record| urid = record.urid @dbref.delete( urid ) self.index_remove( urid ) end return matches.length end |
#distinct(*keylist) ⇒ Object
Returns a new result set containing a list of the unique values.
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 |
# File 'lib/trix51db.rb', line 1643 def distinct( *keylist ) structure = {} keylist.each do |key| key = key structure[key] = self.structure[key] structure[key][:key] = true end agg_records = {} self.each_record do |record| nurid = self.group_key( record.data, keylist ) next unless agg_records[nurid].nil? agg_records[nurid] = {} keylist.each do |key| key = key agg_records[nurid][key] = record.data[key] end end res = Trix51::ResultSet.new( 'aggregate_'+self.tablename, {}, { tableref: self, structure: structure } ) = res. [:structure] = structure res. = agg_records.each_pair do |k,v| res.dbref[k] = Trix51::Tuple.new( self, k, v ) end return res end |
#dump ⇒ Object
Produces a neatly formatted dump of the records (all fields)
1403 1404 1405 1406 |
# File 'lib/trix51db.rb', line 1403 def dump fields = self.structure.keys self.dump_filtered( *fields ) end |
#dump_filtered(*fieldnames) ⇒ Object
Produces a neatly formatted dump of the records (selected fields)
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 |
# File 'lib/trix51db.rb', line 1409 def dump_filtered( *fieldnames ) widths = {} #fieldnames = self.structure.keys.sort if fieldnames.length == 0 lines = [] tmp = [] fieldnames.each do |fn| tmp.push( fn ) if self.exists?( fn ) end fieldnames = tmp fieldnames.each do |field| widths[field] = field.length+2 str = '' (field.length+1).times do |n| str = str + '-' end lines.push( str ) end @dbref.each_pair do |k, v| #pp v next unless self.table_record?(k) record = nil if self.table? then record = Marshal.load( v ) else record = v.data end fieldnames.each do |field| fv = record[field] || 'nil' widths[field] = fv.to_s.length+2 if fv.to_s.length+2 > widths[field] end end # create formatstr formatstr = '' fieldnames.each do |field| formatstr = formatstr + " %-#{widths[field]}s|" end puts sprintf "#{formatstr}", *fieldnames puts sprintf "#{formatstr}", *lines self.each_record do |v| record = v.data values = [] fieldnames.each do |field| values.push( record[field] || 'nil' ) end puts sprintf "#{formatstr}", *values end #pp @dbref end |
#each ⇒ Object
Interate over records in the table or resultset. Any updates are automatically updated in the database.
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 |
# File 'lib/trix51db.rb', line 1304 def each self.sorted_keys.each do |k| next unless k.match( Trix51::RECORD_PREFIX ) r = init_record(k) yield record r.update if r.has_updates? end end |
#each_key ⇒ Object
Interates through each key in the dbm store.
1326 1327 1328 1329 1330 |
# File 'lib/trix51db.rb', line 1326 def each_key self.sorted_keys.each do |k| yield k if self.table_record?(k) end end |
#each_pair ⇒ Object
Iterates through each key, value for the table.
1316 1317 1318 1319 1320 1321 1322 1323 |
# File 'lib/trix51db.rb', line 1316 def each_pair self.sorted_keys.each do |k| next unless self.table_record?(k) yield k, init_record(k) end end |
#each_record ⇒ Object
Iterates through each Trix51::Tuple in the table or result set.
1343 1344 1345 1346 1347 1348 1349 1350 |
# File 'lib/trix51db.rb', line 1343 def each_record self.sorted_keys.each do |k| next unless k.match( self.record_prefix ) r = init_record(k) yield r r.update if r.has_updates? end end |
#each_value ⇒ Object
Interates through each Trix51::Tuple in the table or result set.
1333 1334 1335 1336 1337 1338 1339 1340 |
# File 'lib/trix51db.rb', line 1333 def each_value self.sorted_keys.each do |k| next unless k.match( self.record_prefix ) r = init_record(k) yield r r.update if r.has_updates? end end |
#empty ⇒ Object
Deletes all records in the table.
837 838 839 |
# File 'lib/trix51db.rb', line 837 def empty return self.delete_hash( {} ) end |
#empty_rec ⇒ Object
:nodoc:
924 925 926 927 928 929 930 |
# File 'lib/trix51db.rb', line 924 def empty_rec #:nodoc: ref = {} self.structure.each_key { |key| ref[key] = self.default( key ) } return ref end |
#eval_in_context(hashdata, expression) ⇒ Object
:nodoc:
717 718 719 720 721 722 723 724 725 726 727 728 729 |
# File 'lib/trix51db.rb', line 717 def eval_in_context( hashdata, expression ) #:nodoc: inits = [] hashdata.each_pair do |k,v| next if v.nil? str = '' str = %Q|#{k} = #{v}| if v.class.name != 'String' str = %Q{#{k} = %Q|#{v}|} if v.class.name == 'String' inits.push( str ) end inits.push( expression ) str = inits.join("\n") return eval( str ) end |
#exists?(fieldname) ⇒ Boolean
Returns true / false if the table contains field
623 624 625 |
# File 'lib/trix51db.rb', line 623 def exists?( fieldname ) return (not self.structure[fieldname].nil?) end |
#find_or_create(*args) ⇒ Object
Find (select) or create a record with the named arguments.
987 988 989 990 991 992 993 994 995 996 997 |
# File 'lib/trix51db.rb', line 987 def find_or_create( *args ) res = self.first( *args ) if res.nil? then return self.insert_hash( args_to_hash(*args) ) end return res end |
#find_or_create_seq(fieldname) ⇒ Object
:nodoc:
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 |
# File 'lib/trix51db.rb', line 627 def find_or_create_seq( fieldname ) #:nodoc: seq_name =self.seq_prefix( fieldname ) #puts "Sequence is called: #{seq_name}" if @dbref[seq_name].nil? then @dbref[seq_name] = '2' return 1 else v = @dbref[seq_name].to_i @dbref[seq_name] = (v+1).to_s return v end end |
#first(*args) ⇒ Object
Returns the first record matching the query (Trix51::Tuple)
821 822 823 824 825 826 827 828 829 |
# File 'lib/trix51db.rb', line 821 def first( *args ) res = self.select_hash( args_to_hash(*args) ) if res.count > 0 then res.each_record do |x| return x end return nil end end |
#get_hash_key(hashdata) ⇒ Object
:nodoc:
913 914 915 916 917 918 919 920 921 922 |
# File 'lib/trix51db.rb', line 913 def get_hash_key( hashdata ) #:nodoc: keys = self.key_fields v = [] keys.each { |k| v.push( hashdata[k] ) } return self.record_prefix + ':' + v.join( Trix51::KEY_JOIN ) end |
#group(*args) ⇒ Object
Groups records by the array of fields specified in the by: clause, and returns the aggregated results for the fields specified in the calculate: clause.
Group calculations
-
sum:
- Sum of the field values -
max:
- Max of the field values -
min:
- Min of the field values -
avg:
- Average of the field values -
count:
- Count of the field values
Example
animal.group( by: [ :species_id ], calculate: { number_of_eyes: 'sum' } ).dump
species_id |sum_number_of_eyes |
----------- |------------------- |
1 | 28 |
2 | 40 |
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 |
# File 'lib/trix51db.rb', line 1537 def group( *args ) params = args.shift #pp params keylist = params[:by] fields = params[:calculate] collected_data = {} structure = {} agg_records = {} keylist.each do |key| structure[key] = self.structure[key] structure[key][:key] = true end fields.each_pair do |field,function| new_field = function.to_s + '_' + field.to_s structure[new_field.to_sym] = { :datatype => 'string' } end self.each_record do |record| nurid = self.group_key( record.data, keylist ) agg_records[nurid] = {} keylist.each do |key| key = key agg_records[nurid][key] = record.data[key] end # for this record, aggregate fields.each_key do |field| collected_data[nurid] = {} if collected_data[nurid].nil? collected_data[nurid][field] = [] if collected_data[nurid][field].nil? values = collected_data[nurid][field] if self.structure[field][:datatype] == 'calculated' then values.push( self.eval_in_context( record.data, self.structure[field][:calculation] ) ) else values.push( record.data[field] ) end collected_data[nurid][field] = values end # now collapse the data into hashes collected_data.each_key do |nurid| # by key, process each function # copy keylist fields in fields.each_pair do |field,function| field = field function = function new_field = (function.to_s + '_' + field.to_s).to_sym values = collected_data[nurid][field] if function == 'sum' then agg_records[nurid][new_field] = values.reduce(:+) elsif function == 'avg' then agg_records[nurid][new_field] = values.reduce(:+) / values.count elsif function == 'min' then agg_records[nurid][new_field] = values.min elsif function == 'max' then agg_records[nurid][new_field] = values.max elsif function == 'count' then agg_records[nurid][new_field] = values.count else raise Trix51::NotFoundError, "unknown group function #{function}" end end end end res = Trix51::ResultSet.new( 'aggregate_'+self.tablename, {}, { tableref: self, structure: structure } ) = res. [:structure] = structure res. = agg_records.each_pair do |k,v| res.dbref[k] = Trix51::Tuple.new( self, k, v ) end return res end |
#group_key(hashdata, keylist) ⇒ Object
:nodoc:
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 |
# File 'lib/trix51db.rb', line 1507 def group_key( hashdata, keylist ) #:nodoc: keycode = [] keylist.each do |key| if not hashdata[key].nil? then keycode.push( hashdata[key].to_s ) else keycode.push( 'nil' ) end end return Trix51::RECORD_PREFIX+'aggregate_'+self.tablename+':'+keycode.join(':') end |
#having(*args) ⇒ Object
Alias for select()
816 817 818 |
# File 'lib/trix51db.rb', line 816 def having( *args ) return self.select_hash( args_to_hash(*args) ) end |
#hint_criteria_to_operand_target(criteria) ⇒ Object
:nodoc:
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 |
# File 'lib/trix51db.rb', line 1738 def hint_criteria_to_operand_target( criteria ) #:nodoc: if criteria.class.name != 'Hash' then return [ 'is', criteria ] else operand = '' target = '' criteria.each_pair do |k, v| operand = k.to_s target = v end return [ operand, target ] end end |
#hint_matches_by_index(taglist, field, operand, target) ⇒ Object
:nodoc:
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 |
# File 'lib/trix51db.rb', line 1698 def hint_matches_by_index( taglist, field, operand, target ) #:nodoc: newlist = [] # match the criteria against each key in the index, collecting any matches if they are in the original # taglist self.index_build(field) if self.indices[field].nil? self.indices[field].each_pair do |fieldvalue, urid_list| if comparison( fieldvalue, operand, target ) then matches = urid_list & taglist newlist.push( *matches ) if matches.length > 0 end end return newlist end |
#hint_matches_by_recordscan(taglist, field, operand, target) ⇒ Object
:nodoc:
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 |
# File 'lib/trix51db.rb', line 1716 def hint_matches_by_recordscan( taglist, field, operand, target ) #:nodoc: newlist = [] taglist.each do |urid| v = @dbref[urid] record = nil if v.class.name == 'String' then record = Marshal.load( v ) else record = v.data end if self.structure[field][:datatype] == 'calculated' then record[field] = self.eval_in_context( record, self.structure[field][:calculation] ) end newlist.push( urid ) if comparison( record[field], operand, target ) end return newlist end |
#index_add(urid, record) ⇒ Object
:nodoc:
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 |
# File 'lib/trix51db.rb', line 1035 def index_add( urid, record ) #:nodoc: record.keys.each { |fieldname| next unless self.indexed?(fieldname) # check index exists if self.indices[fieldname].nil? then self.index_build( fieldname ) else # partial add rv = record[fieldname] rlist = self.indices[fieldname][rv] || [] rlist.push( urid ) self.indices[fieldname][rv] = rlist end } end |
#index_build(field) ⇒ Object
Rebuilds the index for the given field.
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 |
# File 'lib/trix51db.rb', line 1018 def index_build( field ) if not self.indexed?(field) then raise Trix51::NotFoundError, "field #{field} is not an indexed field" end #puts "Building index for field #{field}" res = {} self.dbref.each_pair { |k, v| next unless self.table_record?( k ) record = Marshal.load( v ) rv = record[field] rlist = res[rv] || [] rlist.push( k ) res[rv] = rlist } @indices[field] = res end |
#index_chk_unique(fieldname, fieldvalue) ⇒ Object
:nodoc:
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 |
# File 'lib/trix51db.rb', line 1073 def index_chk_unique( fieldname, fieldvalue )#:nodoc: #puts fieldname + '=' + fieldvalue.to_s if self.indexed?( fieldname ) == false then return true end if self.unique?( fieldname) == false then return true end if fieldvalue.nil? then return false end if self.indices[fieldname].nil? then self.index_build( fieldname ) end if self.indices[fieldname][fieldvalue].nil? then return true end if self.indices[fieldname][fieldvalue].length == 0 then return true end return false end |
#index_chk_unique_update(fieldname, fieldvalue, urid) ⇒ Object
:nodoc:
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 |
# File 'lib/trix51db.rb', line 1104 def index_chk_unique_update( fieldname, fieldvalue, urid ) #:nodoc: #puts fieldname + '=' + fieldvalue.to_s if self.indexed?( fieldname ) == false then return true end if self.unique?( fieldname) == false then return true end if fieldvalue.nil? then return false end if self.indices[fieldname].nil? then self.index_build( fieldname ) end if self.indices[fieldname][fieldvalue].nil? then return true end if self.indices[fieldname][fieldvalue].length == 0 then return true end if (self.indices[fieldname][fieldvalue].length == 1) and (self.indices[fieldname][fieldvalue][0] == urid) then return true end return false end |
#index_remove(urid) ⇒ Object
:nodoc:
1065 1066 1067 1068 1069 1070 1071 |
# File 'lib/trix51db.rb', line 1065 def index_remove( urid ) #:nodoc: self.indices.each_pair { |fieldname, index| index.each_value { |rlist| rlist.delete( urid ) } } end |
#index_update(oldurid, newurid, record) ⇒ Object
:nodoc:
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 |
# File 'lib/trix51db.rb', line 1051 def index_update( oldurid, newurid, record ) #:nodoc: record.keys.each { |fieldname| next unless self.indexed?(fieldname) # check index exists if self.indices[fieldname].nil? then self.index_build( fieldname ) else # partial update self.index_remove( oldurid ) self.index_add( newurid, record ) end } end |
#indexed?(field) ⇒ Boolean
returns true or false if a table field is indexed.
1000 1001 1002 1003 |
# File 'lib/trix51db.rb', line 1000 def indexed?( field ) return false unless self.exists?( field ) return ( (self.key?(field)) or (self.unique?(field)) or ( (not self.structure[field][:indexed].nil?) and (self.structure[field][:indexed] == true) ) ) end |
#indices_load(path) ⇒ Object
:nodoc:
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 |
# File 'lib/trix51db.rb', line 1800 def indices_load( path ) #:nodoc: filename = path+'/'+self.tablename.to_s+'.idx' if File.exists?( filename ) then #puts "*** Load index: #{filename}" File.open( filename, 'r' ) do |file| self.indices = Marshal.load( file ) end else #puts "*** Generating index for #{self.tablename}" self.structure.keys.each { |field| self.index_build( field ) if self.indexed?(field) } end #pp self.indices end |
#indices_save(path) ⇒ Object
:nodoc:
1792 1793 1794 1795 1796 1797 1798 |
# File 'lib/trix51db.rb', line 1792 def indices_save( path ) #:nodoc: filename = path+'/'+self.tablename.to_s+'.idx' #puts "*** Save index: #{filename}" File.open( filename, 'w' ) do |file| Marshal.dump( self.indices, file ) end end |
#init_record(urid) ⇒ Object
:nodoc:
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 |
# File 'lib/trix51db.rb', line 874 def init_record( urid ) #:nodoc: #pp self.table? #pp @classname unless self.table? then return @dbref[ urid ] end r = nil if (@classname == 'Trix51::Tuple') or (Trix51.defer_classref == true) then r = Trix51::Tuple.new( self, urid ) else r = eval( "#{@classname}.new( self, urid )" ) end return r end |
#insert(*args) ⇒ Object
Inserts a record into the database using named arguments.
842 843 844 |
# File 'lib/trix51db.rb', line 842 def insert( *args ) return self.insert_hash( args_to_hash(*args) ) end |
#insert_hash(hashdata) ⇒ Object
insert record into the database using a hash
Example
daisy = animal.insert_hash( {
name: 'cow',
species_id: 2
} )
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 |
# File 'lib/trix51db.rb', line 649 def insert_hash( hashdata ) #:nodoc: ref = self.empty_rec.merge( hashdata ) { |key, oldval, newval| (not newval.nil?) ? newval : oldval } #pp ref flt = {} ref.each_key { |key| flt[key] = ref[key] if self.exists?( key ) } #pp flt self.check_required_fields( flt ) urid = self.get_hash_key( flt ) #puts "Record key: #{urid}" raise Trix51::ConstraintError, "unique constraint has been violated (#{urid})" if @dbref[urid] @dbref[urid] = Marshal.dump( flt ) #puts "INSERT "+@dbref[urid] self.index_add( urid, flt ) return self.init_record( urid ) end |
#key?(field) ⇒ Boolean
returns true or false if a table field forms part of the record key
1012 1013 1014 1015 |
# File 'lib/trix51db.rb', line 1012 def key?( field ) return false unless self.exists?( field ) return ( (not self.structure[field][:key].nil?) and (self.structure[field][:key] == true) ) end |
#key_fields ⇒ Object
Returns the key fields for the table
904 905 906 907 908 909 910 911 |
# File 'lib/trix51db.rb', line 904 def key_fields res = [] self.structure.keys.sort.each { |key| #puts key res.push(key) if self.structure[key][:key] == true } return res end |
#length ⇒ Object
Returns the number of records in the table or result set.
1363 1364 1365 |
# File 'lib/trix51db.rb', line 1363 def length return self.size end |
#limit(count) ⇒ Object
Returns a new result set contain only the first count records.
Example
animal.select( number_of_legs: 4 ).sort( :name ).limit(2).dump_filtered( :name )
name |
----- |
aardvark |
bullock |
1687 1688 1689 1690 1691 1692 1693 1694 1695 |
# File 'lib/trix51db.rb', line 1687 def limit( count ) res = Trix51::ResultSet.new( self.tablename, {}, { tableref: self } ) done = 0 self.each_record do |record| res.dbref[ record.urid ] = record if done < count done = done + 1 end return res end |
#meta ⇒ Object
:nodoc:
1391 1392 1393 1394 1395 1396 |
# File 'lib/trix51db.rb', line 1391 def #:nodoc: if @meta.nil? then @meta = Marshal.load( @dbref[self.] ) end return @meta end |
#meta=(meta) ⇒ Object
:nodoc:
1398 1399 1400 |
# File 'lib/trix51db.rb', line 1398 def () #:nodoc: @dbref[self. ] = Marshal.dump( ) end |
#meta_prefix ⇒ Object
:nodoc:
1383 1384 1385 |
# File 'lib/trix51db.rb', line 1383 def #:nodoc: return Trix51::META_PREFIX+self.tablename.to_s end |
#meta_record?(key) ⇒ Boolean
:nodoc:
1375 1376 1377 |
# File 'lib/trix51db.rb', line 1375 def ( key ) #:nodoc: return (key.match(Trix51::META_PREFIX)) end |
#push(record) ⇒ Object
:nodoc:
1352 1353 1354 1355 |
# File 'lib/trix51db.rb', line 1352 def push( record ) #:nodoc: #puts "pushing record with urid = #{record.urid}" @dbref[ record.urid ] = record end |
#random ⇒ Object
Returns a random record from the table or result set.
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 |
# File 'lib/trix51db.rb', line 1470 def random keys = [] @dbref.each_key do |k| keys.push( k ) if self.table_record?(k) end return nil if keys.count == 0 urid = keys[ rand( keys.count ) ] return init_record( urid ) end |
#record_keys ⇒ Object
:nodoc:
570 571 572 573 574 575 576 |
# File 'lib/trix51db.rb', line 570 def record_keys #:nodoc: list = [] @dbref.each_key do |k| list.push( k ) if self.table_record?(k) end return list end |
#record_prefix ⇒ Object
:nodoc:
1367 1368 1369 |
# File 'lib/trix51db.rb', line 1367 def record_prefix #:nodoc: return Trix51::RECORD_PREFIX+self.tablename.to_s end |
#select(*args) ⇒ Object
Returns a Trix51::ResultSet containing records matching the query.
Example
records = animal.select(
name: { like: 'mon%' },
created_date: { between: [ '2012-04-11', 2012-12-31' ],
number_of_eyes: 2,
number_of_legs: { in: [2,4] }
)
Query operands
-
is:
- true if value matches -
in:
- true if value is in specified list -
not:
- true if value does not match -
under:
- true if value is less than -
over:
- true if value is greater than -
like:
- true if value matches pattern ( ‘%’ acts as wildcard ) -
unlike:
- true if value does not match pattern ( ‘%’ acts as wildcard ) -
between:
- true if value is between specified values
811 812 813 |
# File 'lib/trix51db.rb', line 811 def select( *args ) return self.select_hash( args_to_hash(*args) ) end |
#select_hash(criteria) ⇒ Object
Return records based on the specified query.
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 |
# File 'lib/trix51db.rb', line 1753 def select_hash( criteria ) #:nodoc: #criteria = self.args_to_hash( *args ) taglist = self.record_keys criteria.each_pair do |field, value| break if taglist.length == 0 if self.indexed?(field) then #puts "#{field} will use index" taglist = self.hint_matches_by_index( taglist, field, *hint_criteria_to_operand_target(value) ) else #puts "#{field} will use record level scan (less efficient)" taglist = self.hint_matches_by_recordscan( taglist, field, *hint_criteria_to_operand_target(value) ) end end res = Trix51::ResultSet.new( self.tablename, {}, { tableref: self } ) taglist.each do |key| res.push( self.init_record( key ) ) end return res end |
#seq_prefix(fieldname) ⇒ Object
:nodoc:
1387 1388 1389 |
# File 'lib/trix51db.rb', line 1387 def seq_prefix( fieldname ) #:nodoc: return Trix51::SEQ_PREFIX+self.tablename.to_s+'_'+fieldname.to_s+'_seq' end |
#seq_record?(key) ⇒ Boolean
:nodoc:
1379 1380 1381 |
# File 'lib/trix51db.rb', line 1379 def seq_record?( key ) #:nodoc: return (key.match(Trix51::SEQ_PREFIX)) end |
#size ⇒ Object
Returns the number of records in the database.
579 580 581 582 583 584 585 |
# File 'lib/trix51db.rb', line 579 def size c = 0 @dbref.each_key do |k| c = c + 1 if k.match( self.record_prefix ) end return c end |
#sort(field, dir = 'asc') ⇒ Object
Sorts records by specified field and ordering, returning a new result set. Special symbol :random returns records in a randomized ordering.
Example
animal.select( number_of_legs: 4 ).sort( :name ).dump_filtered( :name )
name |
----- |
aardvark |
bullock |
cow |
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 |
# File 'lib/trix51db.rb', line 1495 def sort( field, dir='asc' ) res = Trix51::ResultSet.new( self.tablename, {}, { tableref: self, sorted: true, sortfield: field, sortorder: dir } ) self.each_key do |k| res.push( init_record( k ) ) end return res end |
#sorted_keys ⇒ Object
:nodoc:
559 560 561 562 563 564 565 566 567 568 |
# File 'lib/trix51db.rb', line 559 def sorted_keys #:nodoc: if (not @options[:sorted].nil?) and (@options[:sorted] == true) then #return self.build_sorted_keys( @options['sortfield'].to_s, @options['sortorder'] ) if @sort_order.nil? then @sort_order = self.build_sorted_keys( @options[:sortfield], @options[:sortorder] ) end return @sort_order end return @dbref.keys end |
#structure ⇒ Object
:nodoc:
591 592 593 |
# File 'lib/trix51db.rb', line 591 def structure #:nodoc: return @meta[:structure] end |
#table? ⇒ Boolean
Returns true if the given object is a table, false otherwise
1298 1299 1300 |
# File 'lib/trix51db.rb', line 1298 def table? return (@dbref.class.name == 'GDBM') end |
#table_record?(key) ⇒ Boolean
:nodoc:
1371 1372 1373 |
# File 'lib/trix51db.rb', line 1371 def table_record?( key ) #:nodoc: return (key.match(self.record_prefix)) end |
#tuple_by_key(*args) ⇒ Object
:nodoc:
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 |
# File 'lib/trix51db.rb', line 857 def tuple_by_key( *args ) #:nodoc: keys = self.key_fields h = {} keys.each { |key| h[key] = args.shift } #pp h urid = self.get_hash_key( h ) s = dbref[urid] return Marshal.load( s ) if self.table? and not s.nil? return s if not s.nil? return nil end |
#unique?(field) ⇒ Boolean
returns true or false if a table field must be unique
1006 1007 1008 1009 |
# File 'lib/trix51db.rb', line 1006 def unique?( field ) return false unless self.exists?( field ) return ( (not self.structure[field][:unique].nil?) and (self.structure[field][:unique] == true) ) end |
#update(*args) ⇒ Object
Updates records matching the query.
Example
animal.update( number_of_eyes: 2, number_of_legs: 4 ) do |record|
record.number_of_eyes = 8
end
772 773 774 775 776 777 778 779 780 781 782 783 784 |
# File 'lib/trix51db.rb', line 772 def update( *args ) hashdata = args_to_hash( *args ) matches = self.select_hash( hashdata ) matches.each { |record| yield record record.update if record.has_updates? } return matches.length end |
#update_hash(hashdata) ⇒ Object
Updates any records matching the values in hash
Example
animal.update_hash ( { name: 'cow' } ) do |record|
record.name = ‘sheep’ # baaa?
end
755 756 757 758 759 760 761 762 763 764 765 |
# File 'lib/trix51db.rb', line 755 def update_hash( hashdata ) #:nodoc: matches = self.select_hash( hashdata ) matches.each { |record| yield record record.update if record.has_updates? } return matches.length end |