Class: Jackcess::Row
- Inherits:
-
Object
- Object
- Jackcess::Row
- Defined in:
- lib/jackcess/row.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#java_row ⇒ Object
readonly
Returns the value of attribute java_row.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
-
#[](column_name) ⇒ Object
Get column value by name.
-
#[]=(column_name, value) ⇒ Object
Set column value by name.
-
#delete ⇒ Object
Delete this row.
-
#dirty? ⇒ Boolean
Check if row has been modified.
-
#initialize(java_row, table) ⇒ Row
constructor
A new instance of Row.
- #inspect ⇒ Object
-
#keys ⇒ Object
Get all column names.
-
#reload ⇒ Object
Reload row from database.
-
#save ⇒ Object
Save changes to the database.
-
#to_h ⇒ Object
Convert to hash.
-
#values ⇒ Object
Get all values.
Constructor Details
#initialize(java_row, table) ⇒ Row
Returns a new instance of Row.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/jackcess/row.rb', line 8 def initialize(java_row, table) @java_row = java_row # This is actually a Map from Jackcess @table = table @data = {} @original_data = {} @dirty = false # Convert Java map to Ruby hash if java_row java_row.each do |key, value| converted_value = TypeConverter.from_java(value) @data[key.to_s] = converted_value @original_data[key.to_s] = converted_value end end end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
6 7 8 |
# File 'lib/jackcess/row.rb', line 6 def data @data end |
#java_row ⇒ Object (readonly)
Returns the value of attribute java_row.
5 6 7 |
# File 'lib/jackcess/row.rb', line 5 def java_row @java_row end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
5 6 7 |
# File 'lib/jackcess/row.rb', line 5 def table @table end |
Instance Method Details
#[](column_name) ⇒ Object
Get column value by name
26 27 28 29 30 |
# File 'lib/jackcess/row.rb', line 26 def [](column_name) raise ArgumentError, "Column name cannot be nil" if column_name.nil? @data[column_name.to_s] end |
#[]=(column_name, value) ⇒ Object
Set column value by name
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jackcess/row.rb', line 33 def []=(column_name, value) raise ArgumentError, "Column name cannot be nil" if column_name.nil? column_name = column_name.to_s # Mark as dirty if value changed if @data[column_name] != value @dirty = true end @data[column_name] = value end |
#delete ⇒ Object
Delete this row
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/jackcess/row.rb', line 92 def delete begin # Get primary key to find the row pk_index = @table.java_table.get_primary_key_index raise DatabaseError, "Cannot delete row: table has no primary key" if pk_index.nil? pk_column_name = pk_index.get_columns.first.get_name pk_value = @data[pk_column_name] # Create a new cursor for this delete operation cursor = com.healthmarketscience.jackcess.CursorBuilder.create_cursor(@table.java_table) while cursor.move_to_next_row current_row = cursor.get_current_row current_pk = TypeConverter.from_java(current_row.get(pk_column_name)) if current_pk == pk_value cursor.deleteCurrentRow return true end end raise DatabaseError, "Row not found for deletion" rescue Java::JavaIo::IOException => e raise DatabaseError, "Failed to delete row: #{e.}" rescue Java::JavaLang::Exception => e raise DatabaseError, "Failed to delete row: #{e.}" end end |
#dirty? ⇒ Boolean
Check if row has been modified
138 139 140 |
# File 'lib/jackcess/row.rb', line 138 def dirty? @dirty end |
#inspect ⇒ Object
169 170 171 |
# File 'lib/jackcess/row.rb', line 169 def inspect "#<Jackcess::Row #{@data.inspect}>" end |
#keys ⇒ Object
Get all column names
123 124 125 |
# File 'lib/jackcess/row.rb', line 123 def keys @data.keys end |
#reload ⇒ Object
Reload row from database
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/jackcess/row.rb', line 143 def reload begin # Get primary key to find the row pk_index = @table.java_table.get_primary_key_index raise DatabaseError, "Cannot reload row: table has no primary key" if pk_index.nil? pk_column_name = pk_index.get_columns.first.get_name pk_value = @original_data[pk_column_name] # Find the row again reloaded_row = @table.find_by_id(pk_value) if reloaded_row @data = reloaded_row.data.dup @original_data = reloaded_row.data.dup @dirty = false end self rescue Java::JavaIo::IOException => e raise DatabaseError, "Failed to reload row: #{e.}" rescue Java::JavaLang::Exception => e raise DatabaseError, "Failed to reload row: #{e.}" end end |
#save ⇒ Object
Save changes to the database
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/jackcess/row.rb', line 47 def save return nil unless @dirty begin # Get primary key to find the row pk_index = @table.java_table.get_primary_key_index raise DatabaseError, "Cannot update row: table has no primary key" if pk_index.nil? pk_column_name = pk_index.get_columns.first.get_name pk_value = @data[pk_column_name] # Create a new cursor for this update operation cursor = com.healthmarketscience.jackcess.CursorBuilder.create_cursor(@table.java_table) while cursor.move_to_next_row current_row = cursor.get_current_row current_pk = TypeConverter.from_java(current_row.get(pk_column_name)) if current_pk == pk_value # Update each changed column value @data.each do |column_name, value| # Skip if value hasn't changed next if @original_data[column_name] == value column = @table.java_table.get_column(column_name) cursor.setCurrentRowValue(column, TypeConverter.to_java(value)) end # Update original data to match current data @original_data = @data.dup @dirty = false return true end end raise DatabaseError, "Row not found for update" rescue Java::JavaIo::IOException => e raise DatabaseError, "Failed to save row: #{e.}" rescue Java::JavaLang::Exception => e raise DatabaseError, "Failed to save row: #{e.}" end end |
#to_h ⇒ Object
Convert to hash
133 134 135 |
# File 'lib/jackcess/row.rb', line 133 def to_h @data.dup end |
#values ⇒ Object
Get all values
128 129 130 |
# File 'lib/jackcess/row.rb', line 128 def values @data.values end |