Class: DBF::Table
- Inherits:
-
Object
- Object
- DBF::Table
- Extended by:
- Forwardable
- Includes:
- Schema, Enumerable
- Defined in:
- lib/dbf/table.rb
Overview
DBF::Table is the primary interface to a single DBF file and provides methods for enumerating and searching the records.
Direct Known Subclasses
Constant Summary collapse
- DBASE2_HEADER_SIZE =
8
- DBASE3_HEADER_SIZE =
32
- DBASE7_HEADER_SIZE =
68
- VERSIONS =
{ '02' => 'FoxBase', '03' => 'dBase III without memo file', '04' => 'dBase IV without memo file', '05' => 'dBase V without memo file', '07' => 'Visual Objects 1.x', '30' => 'Visual FoxPro', '32' => 'Visual FoxPro with field type Varchar or Varbinary', '31' => 'Visual FoxPro with AutoIncrement field', '43' => 'dBASE IV SQL table files, no memo', '63' => 'dBASE IV SQL system files, no memo', '7b' => 'dBase IV with memo file', '83' => 'dBase III with memo file', '87' => 'Visual Objects 1.x with memo file', '8b' => 'dBase IV with memo file', '8c' => 'dBase 7', '8e' => 'dBase IV with SQL table', 'cb' => 'dBASE IV SQL table files, with memo', 'f5' => 'FoxPro with memo file', 'fb' => 'FoxPro without memo file' }.freeze
- FOXPRO_VERSIONS =
{ '30' => 'Visual FoxPro', '31' => 'Visual FoxPro with AutoIncrement field', 'f5' => 'FoxPro with memo file', 'fb' => 'FoxPro without memo file' }.freeze
Constants included from Schema
Schema::FORMATS, Schema::OTHER_DATA_TYPES
Instance Attribute Summary collapse
-
#encoding ⇒ Object
Returns the value of attribute encoding.
- #name ⇒ String
Instance Method Summary collapse
-
#close ⇒ TrueClass, FalseClass
Closes the table and memo file.
- #closed? ⇒ TrueClass, FalseClass
-
#column_names ⇒ String
Column names.
-
#columns ⇒ Array
All columns.
-
#each {|nil, DBF::Record| ... } ⇒ Object
Calls block once for each record in the table.
-
#encode_string(string) ⇒ String
Encode string.
- #filename ⇒ String
-
#find(command, options = {}) {|optional, DBF::Record, NilClass| ... } ⇒ Object
Find records using a simple ActiveRecord-like syntax.
- #has_memo_file? ⇒ TrueClass, FalseClass
-
#header_encoding ⇒ Encoding
Encoding specified in the file header.
-
#initialize(data, memo = nil, encoding = nil) {|_self| ... } ⇒ Table
constructor
Opens a DBF::Table Examples: # working with a file stored on the filesystem table = DBF::Table.new ‘data.dbf’.
-
#record(index) ⇒ DBF::Record, NilClass
(also: #row)
Retrieve a record by index number.
-
#to_csv(path = nil) ⇒ Object
Dumps all records to a CSV file.
-
#version_description ⇒ String
Human readable version description.
Methods included from Schema
#activerecord_schema, #activerecord_schema_definition, #json_schema, #number_data_type, #schema, #schema_data_type, #schema_name, #sequel_schema, #sequel_schema_definition, #string_data_format
Constructor Details
#initialize(data, memo = nil, encoding = nil) {|_self| ... } ⇒ Table
Opens a DBF::Table Examples:
# working with a file stored on the filesystem
table = DBF::Table.new 'data.dbf'
# working with a misnamed memo file
table = DBF::Table.new 'data.dbf', 'memo.dbt'
# working with a dbf in memory
table = DBF::Table.new StringIO.new(dbf_data)
# working with a dbf and memo in memory
table = DBF::Table.new StringIO.new(dbf_data), StringIO.new(memo_data)
# working with a dbf overriding specified in the dbf encoding
table = DBF::Table.new 'data.dbf', nil, 'cp437'
table = DBF::Table.new 'data.dbf', 'memo.dbt', Encoding::US_ASCII
77 78 79 80 81 82 |
# File 'lib/dbf/table.rb', line 77 def initialize(data, memo = nil, encoding = nil) @data = open_data(data) @encoding = encoding || header.encoding || Encoding.default_external @memo = open_memo(data, memo) yield self if block_given? end |
Instance Attribute Details
#encoding ⇒ Object
Returns the value of attribute encoding.
48 49 50 |
# File 'lib/dbf/table.rb', line 48 def encoding @encoding end |
#name ⇒ String
174 175 176 |
# File 'lib/dbf/table.rb', line 174 def name @name ||= filename && File.basename(filename, '.*') end |
Instance Method Details
#close ⇒ TrueClass, FalseClass
Closes the table and memo file
87 88 89 90 |
# File 'lib/dbf/table.rb', line 87 def close @data.close @memo&.close end |
#closed? ⇒ TrueClass, FalseClass
93 94 95 96 97 98 99 |
# File 'lib/dbf/table.rb', line 93 def closed? if @memo @data.closed? && @memo.closed? else @data.closed? end end |
#column_names ⇒ String
Column names
104 105 106 |
# File 'lib/dbf/table.rb', line 104 def column_names @column_names ||= columns.map(&:name) end |
#columns ⇒ Array
All columns
111 112 113 |
# File 'lib/dbf/table.rb', line 111 def columns @columns ||= build_columns end |
#each {|nil, DBF::Record| ... } ⇒ Object
Calls block once for each record in the table. The record may be nil if the record has been marked as deleted.
119 120 121 |
# File 'lib/dbf/table.rb', line 119 def each record_count.times { |i| yield record(i) } end |
#encode_string(string) ⇒ String
Encode string
218 219 220 |
# File 'lib/dbf/table.rb', line 218 def encode_string(string) # :nodoc: string.force_encoding(@encoding).encode(Encoding.default_external, undef: :replace, invalid: :replace) end |
#filename ⇒ String
124 125 126 127 128 |
# File 'lib/dbf/table.rb', line 124 def filename return unless @data.respond_to?(:path) File.basename(@data.path) end |
#find(command, options = {}) {|optional, DBF::Record, NilClass| ... } ⇒ Object
Find records using a simple ActiveRecord-like syntax.
Examples:
table = DBF::Table.new 'mydata.dbf'
# Find record number 5
table.find(5)
# Find all records for Keith Morrison
table.find :all, first_name: "Keith", last_name: "Morrison"
# Find first record
table.find :first, first_name: "Keith"
The command may be a record index, :all, or :first. options is optional and, if specified, should be a hash where the keys correspond to column names in the database. The values will be matched exactly with the value in the database. If you specify more than one key, all values must match in order for the record to be returned. The equivalent SQL would be “WHERE key1 = ‘value1’ AND key2 = ‘value2’”.
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/dbf/table.rb', line 155 def find(command, = {}, &block) case command when Integer record(command) when Array command.map { |i| record(i) } when :all find_all(, &block) when :first find_first() end end |
#has_memo_file? ⇒ TrueClass, FalseClass
169 170 171 |
# File 'lib/dbf/table.rb', line 169 def has_memo_file? !!@memo end |
#header_encoding ⇒ Encoding
Encoding specified in the file header
225 226 227 |
# File 'lib/dbf/table.rb', line 225 def header_encoding header.encoding end |
#record(index) ⇒ DBF::Record, NilClass Also known as: row
Retrieve a record by index number. The record will be nil if it has been deleted, but not yet pruned from the database.
184 185 186 187 188 189 190 191 192 |
# File 'lib/dbf/table.rb', line 184 def record(index) raise DBF::NoColumnsDefined, 'The DBF file has no columns defined' if columns.empty? seek_to_record(index) return nil if deleted_record? record_data = @data.read(record_length) DBF::Record.new(record_data, columns, version, @memo) end |
#to_csv(path = nil) ⇒ Object
Dumps all records to a CSV file. If no filename is given then CSV is output to STDOUT.
200 201 202 203 204 205 |
# File 'lib/dbf/table.rb', line 200 def to_csv(path = nil) out_io = path ? File.open(path, 'w') : $stdout csv = CSV.new(out_io, force_quotes: true) csv << column_names each { |record| csv << record.to_a } end |
#version_description ⇒ String
Human readable version description
210 211 212 |
# File 'lib/dbf/table.rb', line 210 def version_description VERSIONS[version] end |