Class: Jackcess::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/jackcess/row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/jackcess/row.rb', line 6

def data
  @data
end

#java_rowObject (readonly)

Returns the value of attribute java_row.



5
6
7
# File 'lib/jackcess/row.rb', line 5

def java_row
  @java_row
end

#tableObject (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

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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

#deleteObject

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.message}"
  rescue Java::JavaLang::Exception => e
    raise DatabaseError, "Failed to delete row: #{e.message}"
  end
end

#dirty?Boolean

Check if row has been modified

Returns:

  • (Boolean)


138
139
140
# File 'lib/jackcess/row.rb', line 138

def dirty?
  @dirty
end

#inspectObject



169
170
171
# File 'lib/jackcess/row.rb', line 169

def inspect
  "#<Jackcess::Row #{@data.inspect}>"
end

#keysObject

Get all column names



123
124
125
# File 'lib/jackcess/row.rb', line 123

def keys
  @data.keys
end

#reloadObject

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.message}"
  rescue Java::JavaLang::Exception => e
    raise DatabaseError, "Failed to reload row: #{e.message}"
  end
end

#saveObject

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.message}"
  rescue Java::JavaLang::Exception => e
    raise DatabaseError, "Failed to save row: #{e.message}"
  end
end

#to_hObject

Convert to hash



133
134
135
# File 'lib/jackcess/row.rb', line 133

def to_h
  @data.dup
end

#valuesObject

Get all values



128
129
130
# File 'lib/jackcess/row.rb', line 128

def values
  @data.values
end