File: foxpro.rb

package info (click to toggle)
ruby-dbf 4.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,732 kB
  • sloc: ruby: 1,692; makefile: 9
file content (127 lines) | stat: -rw-r--r-- 3,885 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
90
91
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
121
122
123
124
125
126
127
module DBF
  # DBF::Database::Foxpro is the primary interface to a Visual Foxpro database
  # container (.dbc file). When using this database container, long fieldnames
  # are supported, and you can reference tables directly instead of
  # instantiating Table objects yourself.
  # Table references are created based on the filename, but it this class
  # tries to correct the table filenames because they could be wrong for
  # case sensitive filesystems, e.g. when a foxpro database is uploaded to
  # a linux server.
  module Database
    class Foxpro
      # Opens a DBF::Database::Foxpro
      # Examples:
      #   # working with a database stored on the filesystem
      #   db = DBF::Database::Foxpro.new 'path_to_db/database.dbc'
      #
      #  # Calling a table
      #  contacts = db.contacts.record(0)
      #
      # @param path [String]
      def initialize(path)
        @path = path
        @dirname = File.dirname(@path)
        @db = DBF::Table.new(@path)
        @tables = extract_dbc_data
      rescue Errno::ENOENT
        raise DBF::FileNotFoundError, "file not found: #{data}"
      end

      def table_names
        @tables.keys
      end

      # Returns table with given name
      #
      # @param name [String]
      # @return [DBF::Table]
      def table(name)
        Table.new table_path(name) do |table|
          table.long_names = @tables[name]
        end
      end

      # Searches the database directory for the table's dbf file
      # and returns the absolute path. Ensures case-insensitivity
      # on any platform.
      # @param name [String]
      # @return [String]
      def table_path(name)
        glob = File.join(@dirname, "#{name}.dbf")
        path = Dir.glob(glob, File::FNM_CASEFOLD).first

        raise DBF::FileNotFoundError, "related table not found: #{name}" unless path && File.exist?(path)

        path
      end

      def method_missing(method, *args) # :nodoc:
        table_names.index(method.to_s) ? table(method.to_s) : super
      end

      def respond_to_missing?(method, *)
        table_names.index(method.to_s) || super
      end

      private

      # This method extracts the data from the database container. This is
      # just an ordinary table with a treelike structure. Field definitions
      # are in the same order as in the linked tables but only the long name
      # is provided.
      def extract_dbc_data # :nodoc:
        data = {}
        @db.each do |record|
          next unless record

          case record.objecttype
          when 'Table'
            # This is a related table
            process_table record, data
          when 'Field'
            # This is a related field. The parentid points to the table object.
            # Create using the parentid if the parentid is still unknown.
            process_field record, data
          end
        end

        Hash[
          data.values.map { |v| [v[:name], v[:fields]] }
        ]
      end

      def process_table(record, data)
        id = record.objectid
        name = record.objectname
        data[id] = table_field_hash(name)
      end

      def process_field(record, data)
        id = record.parentid
        name = 'UNKNOWN'
        field = record.objectname
        data[id] ||= table_field_hash(name)
        data[id][:fields] << field
      end

      def table_field_hash(name)
        {name: name, fields: []}
      end
    end

    class Table < DBF::Table
      attr_accessor :long_names

      def build_columns # :nodoc:
        columns = super

        # modify the column definitions to use the long names as the
        # columnname property is readonly, recreate the column definitions
        columns.map do |column|
          long_name = long_names[columns.index(column)]
          Column.new(self, long_name, column.type, column.length, column.decimal)
        end
      end
    end
  end
end