Class: KBTable

Inherits:
Object show all
Includes:
DRb::DRbUndumped, KBTypeConversionsMixin
Defined in:
lib/kirbybase.rb

Overview


KBTable


Constant Summary collapse

VALID_FIELD_TYPES =
[:String, :Integer, :Float, :Boolean, :Date, :Time,
:DateTime, :Memo, :Blob, :ResultSet, :YAML]
VALID_DEFAULT_TYPES =
[:String, :Integer, :Float, :Boolean, :Date,
:Time, :DateTime, :YAML]
VALID_INDEX_TYPES =
[:String, :Integer, :Float, :Boolean, :Date, :Time,
:DateTime]

Constants included from KBTypeConversionsMixin

KBTypeConversionsMixin::ENCODE_RE, KBTypeConversionsMixin::KB_NIL, KBTypeConversionsMixin::UNENCODE_RE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from KBTypeConversionsMixin

#convert_to_encoded_string, #convert_to_native_type

Constructor Details

#initialize(db, name, filename) ⇒ KBTable


initialize


++ This has been declared private so user’s cannot create new instances of KBTable from their application. A user gets a handle to a KBTable instance by calling KirbyBase#get_table for an existing table or KirbyBase.create_table for a new table.



2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
# File 'lib/kirbybase.rb', line 2014

def initialize(db, name, filename)
    @db = db
    @name = name
    @filename = filename
    @encrypted = false
    @lookup_key = :recno
    @idx_timestamps = {}
    @idx_arrs = {}

    # Alias delete_all to clear method.

    alias delete_all clear

    update_header_vars
    create_indexes
    create_table_class unless @db.server?
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



1918
1919
1920
# File 'lib/kirbybase.rb', line 1918

def db
  @db
end

#del_ctrObject (readonly)

Returns the value of attribute del_ctr.



1918
1919
1920
# File 'lib/kirbybase.rb', line 1918

def del_ctr
  @del_ctr
end

#filenameObject (readonly)

Returns the value of attribute filename.



1918
1919
1920
# File 'lib/kirbybase.rb', line 1918

def filename
  @filename
end

#last_rec_noObject (readonly)

Returns the value of attribute last_rec_no.



1918
1919
1920
# File 'lib/kirbybase.rb', line 1918

def last_rec_no
  @last_rec_no
end

#lookup_keyObject (readonly)

Returns the value of attribute lookup_key.



1918
1919
1920
# File 'lib/kirbybase.rb', line 1918

def lookup_key
  @lookup_key
end

#nameObject (readonly)

Returns the value of attribute name.



1918
1919
1920
# File 'lib/kirbybase.rb', line 1918

def name
  @name
end

#table_classObject (readonly)

Returns the value of attribute table_class.



1918
1919
1920
# File 'lib/kirbybase.rb', line 1918

def table_class
  @table_class
end

Class Method Details

.create_called_from_database_instance(db, name, filename) ⇒ Object


create_called_from_database_instance


++ Return a new instance of KBTable. Should never be called directly by your application. Should only be called from KirbyBase#get_table.



2001
2002
2003
# File 'lib/kirbybase.rb', line 2001

def KBTable.create_called_from_database_instance(db, name, filename)
    return new(db, name, filename)
end

.valid_data_type?(data_type, value) ⇒ Boolean


KBTable.valid_data_type?


++ Return true if data is correct type, false otherwise.

data_type

Symbol specifying data type.

value

Value to convert to String.

Returns:

  • (Boolean)


1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
# File 'lib/kirbybase.rb', line 1942

def KBTable.valid_data_type?(data_type, value)
    case data_type
    when /:String|:Blob/
        return false unless value.respond_to?(:to_str)
    when :Memo
        return false unless value.is_a?(KBMemo) 
    when :Blob
        return false unless value.is_a?(KBBlob) 
    when :Boolean
        return false unless value.is_a?(TrueClass) or value.is_a?(
         FalseClass)
    when :Integer
        return false unless value.respond_to?(:to_int)
    when :Float
        return false unless value.respond_to?(:to_f)
    when :Time
        return false unless value.is_a?(Time)     
    when :Date
        return false unless value.is_a?(Date)
    when :DateTime
        return false unless value.is_a?(DateTime)
    when :YAML
        return false unless value.respond_to?(:to_yaml)
    end

    return true
end

.valid_default_type?(field_type) ⇒ Boolean


KBTable.valid_default_type?


++ Return true if valid default type.

field_type

Symbol specifying field type.

Returns:

  • (Boolean)


1978
1979
1980
# File 'lib/kirbybase.rb', line 1978

def KBTable.valid_default_type?(field_type)
    VALID_DEFAULT_TYPES.include?(field_type)
end

.valid_field_type?(field_type) ⇒ Boolean


KBTable.valid_field_type?


++ Return true if valid field type.

field_type

Symbol specifying field type.

Returns:

  • (Boolean)


1929
1930
1931
# File 'lib/kirbybase.rb', line 1929

def KBTable.valid_field_type?(field_type)
    VALID_FIELD_TYPES.include?(field_type)
end

.valid_index_type?(field_type) ⇒ Boolean


KBTable.valid_index_type?


++ Return true if valid index type.

field_type

Symbol specifying field type.

Returns:

  • (Boolean)


1990
1991
1992
# File 'lib/kirbybase.rb', line 1990

def KBTable.valid_index_type?(field_type)
    VALID_INDEX_TYPES.include?(field_type)
end

Instance Method Details

#[](*index) ⇒ Object



++ Return the record(s) whose recno field is included in index.

index

Array of Integer(s) specifying recno(s) you wish to select.



2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
# File 'lib/kirbybase.rb', line 2360

def [](*index)
    return nil if index[0].nil?

    return get_match_by_recno(:select, @field_names, index[0]) if \
     index.size == 1

    recs = select_by_recno_index(*@field_names) { |r|
        index.include?(r.recno)
    }

    return recs
end

#[]=(index, updates) ⇒ Object


[]=


++ Update record whose recno field equals index.

index

Integer specifying recno you wish to select.

updates

Hash, Struct, or Array containing updates.



2232
2233
2234
# File 'lib/kirbybase.rb', line 2232

def []=(index, updates)
    return update(updates) { |r| r.recno == index }
end

#add_column(col_name, col_type, after = nil) ⇒ Object


add_column


++ Add a column to table.

Make sure you are executing this method while in single-user mode (i.e. not running in client/server mode).

col_name

Symbol of column name to add.

col_type

Symbol (or Hash if includes field extras) of column type to add.

after

Symbol of column name that you want to add this column after.



2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
# File 'lib/kirbybase.rb', line 2531

def add_column(col_name, col_type, after=nil)
    raise "Do not execute this method in client/server mode!" if \
     @db.client?

    raise "Invalid column name in 'after': #{after}" unless after.nil? \
     or @field_names.include?(after)

    raise "Invalid column name in 'after': #{after}" if after == :recno

    raise "Column name cannot be recno!" if col_name == :recno
    
    raise "Column name already exists!" if @field_names.include?(
     col_name)

    # Does this new column have field extras (i.e. Index, Lookup, etc.)

    if col_type.is_a?(Hash)
        temp_type = col_type[:DataType]
    else
        temp_type = col_type
    end

    raise 'Invalid field type: %s' % temp_type unless \
     KBTable.valid_field_type?(temp_type)

    field_def = @db.build_header_field_string(col_name, col_type)

    @db.engine.add_column(self, field_def, after)

    # Need to reinitialize the table instance and associated indexes.

    @db.engine.remove_recno_index(@name)
    @db.engine.remove_indexes(@name)

    update_header_vars
    create_indexes
    create_table_class unless @db.server?
end

#add_index(*col_names) ⇒ Object


add_index


++ Add an index to a column.

Make sure you are executing this method while in single-user mode (i.e. not running in client/server mode).

col_names

Array containing column name(s) of new index.



2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
# File 'lib/kirbybase.rb', line 2610

def add_index(*col_names)
    raise "Do not execute this method in client/server mode!" if \
     @db.client?

    col_names.each do |c|
        raise "Invalid column name: #{c}" unless \
         @field_names.include?(c)
        
        raise "recno column cannot be indexed!" if c == :recno

        raise "Column already indexed: #{c}" unless \
         @field_indexes[@field_names.index(c)].nil?
    end
    
    last_index_no_used = 0
    @field_indexes.each do |i|
        next if i.nil?
        index_no = i[-1..-1].to_i
        last_index_no_used = index_no if index_no > last_index_no_used
    end
    
    @db.engine.add_index(self, col_names, last_index_no_used+1)

    # Need to reinitialize the table instance and associated indexes.

    @db.engine.remove_recno_index(@name)
    @db.engine.remove_indexes(@name)

    update_header_vars
    create_indexes
    create_table_class unless @db.server?
end

#change_column_default_value(col_name, value) ⇒ Object


change_column_default_value


++ Change a column’s default value.

Make sure you are executing this method while in single-user mode (i.e. not running in client/server mode).

col_name

Symbol of column name.

value

New default value for column.



2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
# File 'lib/kirbybase.rb', line 2690

def change_column_default_value(col_name, value)
    raise "Do not execute this method in client/server mode!" if \
     @db.client?

    raise ":recno cannot have a default value!" if col_name == :recno

    raise 'Invalid column name: ' % col_name unless \
     @field_names.include?(col_name)

    raise 'Cannot set default value for this type: ' + \
     '%s' % @field_types.index(col_name) unless \
     KBTable.valid_default_type?(
      @field_types[@field_names.index(col_name)])
    
    if value.nil?
        @db.engine.change_column_default_value(self, col_name, nil)
    else
        @db.engine.change_column_default_value(self, col_name,
         convert_to_encoded_string(
          @field_types[@field_names.index(col_name)], value))
    end
        
    # Need to reinitialize the table instance and associated indexes.

    @db.engine.remove_recno_index(@name)
    @db.engine.remove_indexes(@name)

    update_header_vars
    create_indexes
    create_table_class unless @db.server?
end

#change_column_required(col_name, required) ⇒ Object


change_column_required


++ Change whether a column is required.

Make sure you are executing this method while in single-user mode (i.e. not running in client/server mode).

col_name

Symbol of column name.

required

true or false.



2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
# File 'lib/kirbybase.rb', line 2733

def change_column_required(col_name, required)
    raise "Do not execute this method in client/server mode!" if \
     @db.client?

    raise ":recno is always required!" if col_name == :recno

    raise 'Invalid column name: ' % col_name unless \
     @field_names.include?(col_name)
    
    raise 'Required must be either true or false!' unless \
     [true, false].include?(required)
    
    @db.engine.change_column_required(self, col_name, required)
        
    # Need to reinitialize the table instance and associated indexes.

    @db.engine.remove_recno_index(@name)
    @db.engine.remove_indexes(@name)

    update_header_vars
    create_indexes
    create_table_class unless @db.server?
end

#change_column_type(col_name, col_type) ⇒ Object


change_column_type


++ Change a column’s type.

Make sure you are executing this method while in single-user mode (i.e. not running in client/server mode).

col_name

Symbol of column name.

col_type

Symbol of new column type.



2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
# File 'lib/kirbybase.rb', line 2494

def change_column_type(col_name, col_type)
    raise "Do not execute this method in client/server mode!" if \
     @db.client?

    raise "Cannot change type for recno column!" if col_name == :recno
    raise 'Invalid column name: ' % col_name unless \
     @field_names.include?(col_name)
    
    raise 'Invalid field type: %s' % col_type unless \
     KBTable.valid_field_type?(col_type)

    @db.engine.change_column_type(self, col_name, col_type)

    # Need to reinitialize the table instance and associated indexes.

    @db.engine.remove_recno_index(@name)
    @db.engine.remove_indexes(@name)

    update_header_vars
    create_indexes
    create_table_class unless @db.server?
end

#clear(reset_recno_ctr = true) ⇒ Object


clear


++ Delete all records from table. You can also use #delete_all.

reset_recno_ctr

true/false specifying whether recno counter should be reset to 0.



2343
2344
2345
2346
2347
2348
2349
2350
# File 'lib/kirbybase.rb', line 2343

def clear(reset_recno_ctr=true)
    recs_deleted = delete { true }
    pack

    @db.engine.reset_recno_ctr(self) if reset_recno_ctr
    update_header_vars
    return recs_deleted
end

#delete(&select_cond) ⇒ Object


delete


++ Delete records from table and return # deleted.

select_cond

Proc containing code to select records.

Raises:

  • (ArgumentError)


2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
# File 'lib/kirbybase.rb', line 2319

def delete(&select_cond)
    raise ArgumentError, 'Must specify select condition code ' + \
     'block.  To delete all records, use #clear instead.' if \
     select_cond.nil?

    # Get all records that match the selection criteria and

    # return them in an array.

    result_set = get_matches(:delete, [:recno], select_cond)

    @db.engine.delete_records(self, result_set)

    # Return the number of records deleted.

    return result_set.size
end

#drop_column(col_name) ⇒ Object


drop_column


++ Drop a column from table.

Make sure you are executing this method while in single-user mode (i.e. not running in client/server mode).

col_name

Symbol of column name to add.



2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
# File 'lib/kirbybase.rb', line 2579

def drop_column(col_name)
    raise "Do not execute this method in client/server mode!" if \
     @db.client?

    raise 'Invalid column name: ' % col_name unless \
     @field_names.include?(col_name)

    raise "Cannot drop :recno column!" if col_name == :recno

    @db.engine.drop_column(self, col_name)

    # Need to reinitialize the table instance and associated indexes.

    @db.engine.remove_recno_index(@name)
    @db.engine.remove_indexes(@name)

    update_header_vars
    create_indexes
    create_table_class unless @db.server?
end

#drop_index(*col_names) ⇒ Object


drop_index


++ Drop an index on a column(s).

Make sure you are executing this method while in single-user mode (i.e. not running in client/server mode).

col_names

Array containing column name(s) of new index.



2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
# File 'lib/kirbybase.rb', line 2653

def drop_index(*col_names)
    raise "Do not execute this method in client/server mode!" if \
     @db.client?

    col_names.each do |c|
        raise "Invalid column name: #{c}" unless \
         @field_names.include?(c)
        
        raise "recno column index cannot be dropped!" if c == :recno

        raise "Column not indexed: #{c}" if \
         @field_indexes[@field_names.index(c)].nil?
    end
    
    @db.engine.drop_index(self, col_names)

    # Need to reinitialize the table instance and associated indexes.

    @db.engine.remove_recno_index(@name)
    @db.engine.remove_indexes(@name)

    update_header_vars
    create_indexes
    create_table_class unless @db.server?
end

#encrypted?Boolean


encrypted?


++ Returns true if table is encrypted.

Returns:

  • (Boolean)


2037
2038
2039
2040
2041
2042
2043
# File 'lib/kirbybase.rb', line 2037

def encrypted?
    if @encrypted
        return true
    else
        return false
    end
end

#field_defaultsObject


field_defaults


++ Return array containing table field defaults.



2091
2092
2093
# File 'lib/kirbybase.rb', line 2091

def field_defaults
    return @field_defaults
end

#field_extrasObject


field_extras


++ Return array containing table field extras.



2071
2072
2073
# File 'lib/kirbybase.rb', line 2071

def field_extras
    return @field_extras
end

#field_indexesObject


field_indexes


++ Return array containing table field indexes.



2081
2082
2083
# File 'lib/kirbybase.rb', line 2081

def field_indexes
    return @field_indexes
end

#field_namesObject


field_names


++ Return array containing table field names.



2051
2052
2053
# File 'lib/kirbybase.rb', line 2051

def field_names
    return @field_names
end

#field_requiredsObject


field_requireds


++ Return array containing table field requireds.



2101
2102
2103
# File 'lib/kirbybase.rb', line 2101

def field_requireds
    return @field_requireds
end

#field_typesObject


field_types


++ Return array containing table field types.



2061
2062
2063
# File 'lib/kirbybase.rb', line 2061

def field_types
    return @field_types
end

#import_csv(csv_filename) ⇒ Object


import_csv


++ Import csv file into table.

csv_filename

filename of csv file to import.



2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
# File 'lib/kirbybase.rb', line 2774

def import_csv(csv_filename)
    records_inserted = 0
    tbl_rec = @table_class.new(self)

    # read with FasterCSV if loaded, or the standard CSV otherwise

    (defined?(FasterCSV) ? FasterCSV : CSV).foreach(csv_filename
     ) do |row|
        tbl_rec.populate([nil] + row)
        insert(tbl_rec)
        records_inserted += 1
    end
    return records_inserted
end

#insert(*data, &insert_proc) ⇒ Object


insert


++ Insert a new record into a table, return unique record number.

data

Array, Hash, Struct instance containing field values of new record.

insert_proc

Proc instance containing insert code. This and the data parameter are mutually exclusive.



2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
# File 'lib/kirbybase.rb', line 2116

def insert(*data, &insert_proc)
    raise 'Cannot specify both a hash/array/struct and a ' + \
     'proc for method #insert!' unless data.empty? or insert_proc.nil?

    raise 'Must specify either hash/array/struct or insert ' + \
     'proc for method #insert!' if data.empty? and insert_proc.nil?

    # Update the header variables.

    update_header_vars

    # Convert input, which could be a proc, an array, a hash, or a

    # Struct into a common format (i.e. hash).

    if data.empty?
        input_rec = convert_input_data(insert_proc)
    else
        input_rec = convert_input_data(data)
    end

    # Check the field values to make sure they are proper types.

    validate_input(input_rec)

    input_rec = Struct.new(*field_names).new(*field_names.zip(
     @field_defaults).collect do |fn, fd|
        if input_rec.has_key?(fn)
            input_rec[fn]
        else
            fd
        end
    end)

    check_required_fields(input_rec)

    check_against_input_for_specials(input_rec)

    new_recno = @db.engine.insert_record(self, @field_names.zip(
     @field_types).collect do |fn, ft|
        convert_to_encoded_string(ft, input_rec[fn])
    end)

    # If there are any associated memo/blob fields, save their values.

    input_rec.each { |r| r.write_to_file if r.is_a?(KBMemo) } if \
     @field_types.include?(:Memo)
    input_rec.each { |r| r.write_to_file if r.is_a?(KBBlob) } if \
     @field_types.include?(:Blob)
                    
    return new_recno
end

#packObject


pack


++ Remove blank records from table, return total removed.



2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
# File 'lib/kirbybase.rb', line 2430

def pack
    raise "Do not execute this method in client/server mode!" if \
     @db.client?

    lines_deleted = @db.engine.pack_table(self)

    update_header_vars

    @db.engine.remove_recno_index(@name)
    @db.engine.remove_indexes(@name)
    create_indexes
    create_table_class unless @db.server?

    return lines_deleted
end

#rename_column(old_col_name, new_col_name) ⇒ Object


rename_column


++ Rename a column.

Make sure you are executing this method while in single-user mode (i.e. not running in client/server mode).

old_col_name

Symbol of old column name.

new_col_name

Symbol of new column name.



2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
# File 'lib/kirbybase.rb', line 2458

def rename_column(old_col_name, new_col_name)
    raise "Do not execute this method in client/server mode!" if \
     @db.client?

    raise "Cannot rename recno column!" if old_col_name == :recno
    raise "Cannot give column name of recno!" if new_col_name == :recno

   raise 'Invalid column name to rename: ' % old_col_name unless \
     @field_names.include?(old_col_name)
    
   raise 'New column name already exists: ' % new_col_name if \
     @field_names.include?(new_col_name)

    @db.engine.rename_column(self, old_col_name, new_col_name)

    # Need to reinitialize the table instance and associated indexes.

    @db.engine.remove_recno_index(@name)
    @db.engine.remove_indexes(@name)

    update_header_vars
    create_indexes
    create_table_class unless @db.server?
end

#select(*filter, &select_cond) ⇒ Object


select


++ Return array of records (Structs) matching select conditions.

filter

List of field names (Symbols) to include in result set.

select_cond

Proc containing select code.



2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
# File 'lib/kirbybase.rb', line 2382

def select(*filter, &select_cond)
    # Declare these variables before the code block so they don't go

    # after the code block is done.

    result_set = []

    # Validate that all names in filter are valid field names.

    validate_filter(filter)

    filter = @field_names if filter.empty?

    # Get all records that match the selection criteria and

    # return them in an array of Struct instances.

    return get_matches(:select, filter, select_cond)
end

#select_by_recno_index(*filter, &select_cond) ⇒ Object


select_by_recno_index


++ Return array of records (Structs) matching select conditions. Select condition block should not contain references to any table column except :recno. If you need to select by other table columns than just :recno, use #select instead.

filter

List of field names (Symbols) to include in result set.

select_cond

Proc containing select code.



2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
# File 'lib/kirbybase.rb', line 2409

def select_by_recno_index(*filter, &select_cond)
    # Declare these variables before the code block so they don't go

    # after the code block is done.

    result_set = []

    # Validate that all names in filter are valid field names.

    validate_filter(filter)

    filter = @field_names if filter.empty?

    # Get all records that match the selection criteria and

    # return them in an array of Struct instances.

    return get_matches_by_recno_index(:select, filter, select_cond)
end

#set(recs, data) ⇒ Object


set


++ Set fields of records to updated values. Returns number of records updated.

recs

Array of records (Structs) that will be updated.

data

Hash, Struct, Proc containing updates.



2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
# File 'lib/kirbybase.rb', line 2246

def set(recs, data)
    # If updates are not in the form of a Proc, convert updates, which

    # could be an array, a hash, or a Struct into a common format (i.e.

    # hash).

    update_rec = convert_input_data(data) unless data.is_a?(Proc)

    updated_recs = []

    # For each one of the recs that matched the update query, apply the

    # updates to it and write it back to the database table.

    recs.each do |rec|
        temp_rec = rec.dup

        if data.is_a?(Proc)
            begin
                data.call(temp_rec)
            rescue NoMethodError
                raise 'Invalid field name in code block: %s' % $!
            end
         else
            @field_names.each { |fn| temp_rec[fn] = update_rec.fetch(fn,
             temp_rec.send(fn)) }
        end

        # Is the user trying to change something they shouldn't?

        raise 'Cannot update recno field!' unless \
         rec.recno == temp_rec.recno
        raise 'Cannot update internal fpos field!' unless \
         rec.fpos == temp_rec.fpos
        raise 'Cannot update internal line_length field!' unless \
         rec.line_length == temp_rec.line_length

        # Are the data types of the updates correct?

        validate_input(temp_rec)

        check_required_fields(temp_rec)

        check_against_input_for_specials(temp_rec)

        # Apply updates to the record and add it to an array holding

        # updated records.  We need the fpos and line_length because

        # the engine will use them to determine where to write the

        # update and whether the updated record will fit in the old

        # record's spot.

        updated_recs << { :rec => @field_names.zip(@field_types
         ).collect { |fn, ft| convert_to_encoded_string(ft,
         temp_rec.send(fn)) }, :fpos => rec.fpos,
         :line_length => rec.line_length }
 

        # Update any associated blob/memo fields.

        temp_rec.each { |r| r.write_to_file if r.is_a?(KBMemo) } if \
         @field_types.include?(:Memo)
        temp_rec.each { |r| r.write_to_file if r.is_a?(KBBlob) } if \
         @field_types.include?(:Blob)
    end

    # Take all of the update records and write them back out to the

    # table's file.

    @db.engine.update_records(self, updated_recs)

    # Return the number of records updated.

    return recs.size
end

#total_recsObject


total_recs


++ Return total number of undeleted (blank) records in table.



2762
2763
2764
# File 'lib/kirbybase.rb', line 2762

def total_recs
    return @db.engine.get_total_recs(self)
end

#update(*updates, &select_cond) ⇒ Object


update


++ Return array of records (Structs) to be updated based on select cond.

updates

Hash or Struct containing updates.

select_cond

Proc containing code to select records to update.

Raises:

  • (ArgumentError)


2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
# File 'lib/kirbybase.rb', line 2201

def update(*updates, &select_cond)
    raise ArgumentError, "Must specify select condition code " + \
     "block.  To update all records, use #update_all instead." if \
     select_cond.nil?

    # Update the header variables.

    update_header_vars

    # Get all records that match the selection criteria and

    # return them in an array.

    result_set = get_matches(:update, @field_names, select_cond)

    # If updates is empty, this means that the user must have specified

    # the updates in KBResultSet#set, i.e.

    # tbl.update {|r| r.recno == 1}.set(:name => 'Bob')

    return result_set if updates.empty?

    # Call KBTable#set and pass it the records to be updated and the

    # updated criteria.

    set(result_set, updates)
end

#update_all(*updates, &update_proc) ⇒ Object


update_all


++ Return array of records (Structs) to be updated, in this case all records.

updates

Hash or Struct containing updates.



2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
# File 'lib/kirbybase.rb', line 2173

def update_all(*updates, &update_proc)
    raise 'Cannot specify both a hash/array/struct and a ' + \
     'proc for method #update_all!' unless updates.empty? or \
     update_proc.nil?

    raise 'Must specify either hash/array/struct or update ' + \
     'proc for method #update_all!' if updates.empty? and \
     update_proc.nil?

    # Depending on whether the user supplied an array/hash/struct or a

    # block as update criteria, we are going to call updates in one of

    # two ways.

    if updates.empty?
        update { true }.set &update_proc
    else
        update(*updates) { true }
    end
end