Class: Jackcess::Database
- Inherits:
-
Object
- Object
- Jackcess::Database
- Defined in:
- lib/jackcess/database.rb
Overview
Represents a Microsoft Access database file (.mdb or .accdb).
This class provides the main entry point for interacting with Access databases through the Jackcess library. It supports opening existing databases, creating new ones, and managing tables within the database.
Defined Under Namespace
Classes: TableBuilderDSL
Instance Attribute Summary collapse
-
#java_database ⇒ Java::ComHealthmarketscienceJackcess::Database
readonly
The underlying Java database object from Jackcess.
Class Method Summary collapse
-
.create(path, format = :v2000) {|db| ... } ⇒ Database
Creates a new Access database file.
-
.open(path, options = {}) {|db| ... } ⇒ Database
Opens an existing Access database file.
Instance Method Summary collapse
-
#close ⇒ nil
Closes the database and flushes any pending changes to disk.
-
#closed? ⇒ Boolean
Checks whether the database has been closed.
-
#create_table(name) {|t| ... } ⇒ Table
Creates a new table in the database.
-
#flush ⇒ nil
Flushes any pending changes to disk.
-
#initialize(java_database) ⇒ Database
constructor
private
Creates a new Database instance wrapping a Java database object.
-
#table(name) ⇒ Table
Retrieves a table by name from the database.
-
#table_names ⇒ Array<String>
Returns a sorted array of all table names in the database.
-
#transaction { ... } ⇒ Object
Executes a block within a transaction context.
Constructor Details
#initialize(java_database) ⇒ Database
34 35 36 37 |
# File 'lib/jackcess/database.rb', line 34 def initialize(java_database) @java_database = java_database @closed = false end |
Instance Attribute Details
#java_database ⇒ Java::ComHealthmarketscienceJackcess::Database (readonly)
The underlying Java database object from Jackcess
27 28 29 |
# File 'lib/jackcess/database.rb', line 27 def java_database @java_database end |
Class Method Details
.create(path, format = :v2000) {|db| ... } ⇒ Database
Creates a new Access database file.
This method creates a new Microsoft Access database file in the specified format. If a block is given, the database will be automatically closed when the block exits.
132 133 134 135 136 137 138 139 140 141 142 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 168 169 170 171 |
# File 'lib/jackcess/database.rb', line 132 def self.create(path, format = :v2000) raise ArgumentError, "Database path cannot be nil" if path.nil? raise ArgumentError, "Database path must be a String" unless path.is_a?(String) raise ArgumentError, "Database path cannot be empty" if path.empty? raise ArgumentError, "Database format cannot be nil" if format.nil? path = File.(path) begin file_format = case format when :v2000 com.healthmarketscience.jackcess.Database::FileFormat::V2000 when :v2003 com.healthmarketscience.jackcess.Database::FileFormat::V2003 when :v2007 com.healthmarketscience.jackcess.Database::FileFormat::V2007 when :v2010 com.healthmarketscience.jackcess.Database::FileFormat::V2010 else raise DatabaseError, "Unknown database format: #{format}. Valid formats are: :v2000, :v2003, :v2007, :v2010" end java_db = DatabaseBuilder.create(file_format, java.io.File.new(path)) db = new(java_db) if block_given? begin yield db ensure db.close end else db end rescue Java::JavaIo::IOException => e raise DatabaseError, "Failed to create database '#{path}': #{e.}" rescue Java::JavaLang::Exception => e raise DatabaseError, "Failed to create database '#{path}': #{e.}" end end |
.open(path, options = {}) {|db| ... } ⇒ Database
Opens an existing Access database file.
This method opens an existing Microsoft Access database file (.mdb or .accdb) for reading and/or writing. If a block is given, the database will be automatically closed when the block exits, even if an exception is raised.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/jackcess/database.rb', line 70 def self.open(path, = {}) raise ArgumentError, "Database path cannot be nil" if path.nil? raise ArgumentError, "Database path must be a String" unless path.is_a?(String) raise ArgumentError, "Database path cannot be empty" if path.empty? path = File.(path) raise DatabaseError, "Database file not found: #{path}" unless File.exist?(path) raise DatabaseError, "Path is a directory, not a file: #{path}" if File.directory?(path) begin builder = DatabaseBuilder.new(java.io.File.new(path)) builder.set_read_only([:readonly]) if .key?(:readonly) builder.set_auto_sync([:auto_sync]) if .key?(:auto_sync) java_db = builder.open db = new(java_db) if block_given? begin yield db ensure db.close end else db end rescue Java::JavaIo::IOException => e raise DatabaseError, "Failed to open database '#{path}': #{e.}" rescue Java::JavaLang::Exception => e raise DatabaseError, "Failed to open database '#{path}': #{e.}" end end |
Instance Method Details
#close ⇒ nil
Closes the database and flushes any pending changes to disk.
After calling this method, the database cannot be used. Calling close on an already-closed database is safe and will not raise an error.
305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/jackcess/database.rb', line 305 def close return if @closed begin @java_database.close @closed = true rescue Java::JavaIo::IOException => e # Log the error but mark as closed to prevent further operations @closed = true raise DatabaseError, "Failed to close database cleanly: #{e.}" end end |
#closed? ⇒ Boolean
Checks whether the database has been closed.
327 328 329 |
# File 'lib/jackcess/database.rb', line 327 def closed? @closed end |
#create_table(name) {|t| ... } ⇒ Table
Creates a new table in the database.
This method uses a DSL (Domain-Specific Language) for defining the table schema. The block receives a table builder object that supports column and primary_key methods.
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/jackcess/database.rb', line 246 def create_table(name, &block) check_closed! raise ArgumentError, "Table name cannot be nil" if name.nil? raise ArgumentError, "Table name must be a String or Symbol" unless name.is_a?(String) || name.is_a?(Symbol) raise ArgumentError, "Table name cannot be empty" if name.to_s.empty? begin builder = TableBuilder.new(name) table_builder = TableBuilderDSL.new(builder) table_builder.instance_eval(&block) if block_given? java_table = builder.to_table(@java_database) Table.new(java_table, self) rescue Java::JavaIo::IOException => e raise DatabaseError, "Failed to create table '#{name}': #{e.}" rescue Java::JavaLang::Exception => e raise DatabaseError, "Failed to create table '#{name}': #{e.}" end end |
#flush ⇒ nil
Flushes any pending changes to disk.
This method forces all buffered changes to be written to the database file. It’s generally not necessary to call this manually unless you need to ensure changes are persisted immediately.
345 346 347 348 349 350 351 352 353 |
# File 'lib/jackcess/database.rb', line 345 def flush check_closed! begin @java_database.flush rescue Java::JavaIo::IOException => e raise DatabaseError, "Failed to flush database: #{e.}" end end |
#table(name) ⇒ Table
Retrieves a table by name from the database.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/jackcess/database.rb', line 186 def table(name) check_closed! raise ArgumentError, "Table name cannot be nil" if name.nil? raise ArgumentError, "Table name must be a String or Symbol" unless name.is_a?(String) || name.is_a?(Symbol) name = name.to_s begin java_table = @java_database.get_table(name) raise TableNotFoundError, "Table not found: #{name}" if java_table.nil? Table.new(java_table, self) rescue Java::JavaIo::IOException => e raise DatabaseError, "Failed to access table '#{name}': #{e.}" end end |
#table_names ⇒ Array<String>
Returns a sorted array of all table names in the database.
211 212 213 214 215 216 217 218 219 |
# File 'lib/jackcess/database.rb', line 211 def table_names check_closed! begin @java_database.get_table_names.to_a.sort rescue Java::JavaIo::IOException => e raise DatabaseError, "Failed to get table names: #{e.}" end end |
#transaction { ... } ⇒ Object
Executes a block within a transaction context.
Note: Jackcess doesn’t have explicit ACID transaction support like traditional databases. This method provides a semantic grouping for operations and will wrap exceptions in DatabaseError. For true atomicity, consider implementing checkpointing or using database-level features if available.
282 283 284 285 286 287 288 289 290 |
# File 'lib/jackcess/database.rb', line 282 def transaction check_closed! # Note: Jackcess doesn't have explicit transaction support like traditional databases # This is a placeholder for future enhancement yield rescue StandardError => e raise DatabaseError, "Transaction failed: #{e.}" end |