Class: Jackcess::Table
- Inherits:
-
Object
- Object
- Jackcess::Table
- Includes:
- Enumerable
- Defined in:
- lib/jackcess/table.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#java_table ⇒ Object
readonly
Returns the value of attribute java_table.
Instance Method Summary collapse
-
#add_row(data) ⇒ Object
Add a new row.
-
#any?(criteria) ⇒ Boolean
Check if any rows match the given criteria.
-
#column(name) ⇒ Object
Get column by name.
-
#columns ⇒ Object
Get all columns.
-
#count(criteria = nil) ⇒ Integer
Count rows matching the given criteria.
-
#each ⇒ Object
Iterate through all rows.
-
#find_by_id(id) ⇒ Object
Find row by primary key.
-
#find_first(criteria) ⇒ Row?
Find the first row matching the given criteria.
-
#indexes ⇒ Object
Get all indexes.
-
#initialize(java_table, database) ⇒ Table
constructor
A new instance of Table.
-
#name ⇒ Object
Get table name.
-
#row_count ⇒ Object
(also: #size, #length)
Get row count.
-
#where(criteria) ⇒ Array<Row>
Find rows matching the given criteria.
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
#database ⇒ Object (readonly)
Returns the value of attribute database.
7 8 9 |
# File 'lib/jackcess/table.rb', line 7 def database @database end |
#java_table ⇒ Object (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
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.
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
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 |
#columns ⇒ Object
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.
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 |
#each ⇒ Object
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.
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 |
#indexes ⇒ Object
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 |
#name ⇒ Object
Get table name
15 16 17 |
# File 'lib/jackcess/table.rb', line 15 def name @java_table.get_name end |
#row_count ⇒ Object 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.
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 |