Class: Jackcess::Table

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/jackcess/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(java_table, database) ⇒ Table

Returns a new instance of Table.



9
10
11
12
# File 'lib/jackcess/table.rb', line 9

def initialize(java_table, database)
  @java_table = java_table
  @database = database
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



7
8
9
# File 'lib/jackcess/table.rb', line 7

def database
  @database
end

#java_tableObject (readonly)

Returns the value of attribute java_table.



7
8
9
# File 'lib/jackcess/table.rb', line 7

def java_table
  @java_table
end

Instance Method Details

#add_row(data) ⇒ Object

Add a new row

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jackcess/table.rb', line 36

def add_row(data)
  raise ArgumentError, "Data cannot be nil" if data.nil?
  raise ArgumentError, "Data must be a Hash" unless data.is_a?(Hash)

  begin
    java_row_data = java.util.HashMap.new
    data.each do |key, value|
      java_row_data.put(key.to_s, TypeConverter.to_java(value))
    end

    java_row = @java_table.add_row_from_map(java_row_data)
    Row.new(java_row, self)
  rescue Java::JavaIo::IOException => e
    raise DatabaseError, "Failed to add row to table '#{name}': #{e.message}"
  rescue Java::JavaLang::Exception => e
    raise DatabaseError, "Failed to add row to table '#{name}': #{e.message}"
  end
end

#any?(criteria) ⇒ Boolean

Check if any rows match the given criteria.

Examples:

Check if any active users exist

table.any?('Active' => true) # => true

Parameters:

  • criteria (Hash)

    Column name/value pairs to match

Returns:

  • (Boolean)

    true if at least one row matches, false otherwise

Raises:

  • (ArgumentError)


175
176
177
178
179
180
# File 'lib/jackcess/table.rb', line 175

def any?(criteria)
  raise ArgumentError, "Criteria cannot be nil" if criteria.nil?
  raise ArgumentError, "Criteria must be a Hash" unless criteria.is_a?(Hash)

  !find_first(criteria).nil?
end

#column(name) ⇒ Object

Get column by name

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jackcess/table.rb', line 85

def column(name)
  raise ArgumentError, "Column name cannot be nil" if name.nil?
  raise ArgumentError, "Column name must be a String or Symbol" unless name.is_a?(String) || name.is_a?(Symbol)

  name = name.to_s
  begin
    java_col = @java_table.get_column(name)
    raise ColumnNotFoundError, "Column not found: #{name}" if java_col.nil?

    Column.new(java_col)
  rescue Java::JavaLang::IllegalArgumentException => e
    raise ColumnNotFoundError, "Column not found: #{name}"
  end
end

#columnsObject

Get all columns



80
81
82
# File 'lib/jackcess/table.rb', line 80

def columns
  @java_table.get_columns.map { |java_col| Column.new(java_col) }
end

#count(criteria = nil) ⇒ Integer

Count rows matching the given criteria.

Examples:

Count all rows

table.count # => 100

Count filtered rows

table.count('Active' => true) # => 75

Parameters:

  • criteria (Hash, nil) (defaults to: nil)

    Column name/value pairs to match (nil counts all rows)

Returns:

  • (Integer)

    The number of matching rows

Raises:

  • (ArgumentError)


159
160
161
162
163
164
165
# File 'lib/jackcess/table.rb', line 159

def count(criteria = nil)
  return row_count if criteria.nil?

  raise ArgumentError, "Criteria must be a Hash" unless criteria.is_a?(Hash)

  where(criteria).size
end

#eachObject

Iterate through all rows



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jackcess/table.rb', line 20

def each
  return enum_for(:each) unless block_given?

  begin
    # Create a new cursor for iteration to avoid state issues
    cursor = com.healthmarketscience.jackcess.CursorBuilder.create_cursor(@java_table)
    while cursor.move_to_next_row
      java_row = cursor.get_current_row
      yield Row.new(java_row, self)
    end
  rescue Java::JavaIo::IOException => e
    raise DatabaseError, "Failed to iterate rows: #{e.message}"
  end
end

#find_by_id(id) ⇒ Object

Find row by primary key



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/jackcess/table.rb', line 56

def find_by_id(id)
  begin
    primary_key_index = @java_table.get_primary_key_index

    if primary_key_index.nil?
      # No primary key, do a linear search
      each do |row|
        return row if row[primary_key_columns.first] == id
      end
      nil
    else
      # Use index search
      search_map = java.util.HashMap.new
      search_map.put(primary_key_index.get_columns.first.get_name, TypeConverter.to_java(id))

      java_row = com.healthmarketscience.jackcess.CursorBuilder.find_row(@java_table, search_map)
      java_row ? Row.new(java_row, self) : nil
    end
  rescue Java::JavaIo::IOException => e
    raise DatabaseError, "Failed to find row: #{e.message}"
  end
end

#find_first(criteria) ⇒ Row?

Find the first row matching the given criteria.

Examples:

Find first user by name

user = table.find_first('Name' => 'Alice')

Parameters:

  • criteria (Hash)

    Column name/value pairs to match

Returns:

  • (Row, nil)

    The first matching row, or nil if no match

Raises:

  • (ArgumentError)


141
142
143
144
145
146
# File 'lib/jackcess/table.rb', line 141

def find_first(criteria)
  raise ArgumentError, "Criteria cannot be nil" if criteria.nil?
  raise ArgumentError, "Criteria must be a Hash" unless criteria.is_a?(Hash)

  detect { |row| criteria.all? { |key, value| row[key.to_s] == value } }
end

#indexesObject

Get all indexes



101
102
103
# File 'lib/jackcess/table.rb', line 101

def indexes
  @java_table.get_indexes.map { |java_idx| Index.new(java_idx) }
end

#nameObject

Get table name



15
16
17
# File 'lib/jackcess/table.rb', line 15

def name
  @java_table.get_name
end

#row_countObject Also known as: size, length

Get row count



106
107
108
# File 'lib/jackcess/table.rb', line 106

def row_count
  @java_table.get_row_count
end

#where(criteria) ⇒ Array<Row>

Find rows matching the given criteria.

This method provides a simple filtering interface for finding rows. For more complex queries, iterate through rows with #each and filter manually.

Examples:

Find users by name

users = table.where('Name' => 'Alice')

Find with multiple criteria

users = table.where('Name' => 'Bob', 'Active' => true)

Parameters:

  • criteria (Hash)

    Column name/value pairs to match

Returns:

  • (Array<Row>)

    Array of matching rows

Raises:

  • (ArgumentError)


126
127
128
129
130
131
# File 'lib/jackcess/table.rb', line 126

def where(criteria)
  raise ArgumentError, "Criteria cannot be nil" if criteria.nil?
  raise ArgumentError, "Criteria must be a Hash" unless criteria.is_a?(Hash)

  select { |row| criteria.all? { |key, value| row[key.to_s] == value } }
end