blackwinter-flattendb 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,176 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of flattendb, the relational database flattener. #
5
+ # #
6
+ # Copyright (C) 2007 University of Cologne, #
7
+ # Albertus-Magnus-Platz, #
8
+ # 50932 Cologne, Germany #
9
+ # #
10
+ # Authors: #
11
+ # Jens Wille <jens.wille@uni-koeln.de> #
12
+ # #
13
+ # flattendb is free software; you can redistribute it and/or modify it under #
14
+ # the terms of the GNU General Public License as published by the Free #
15
+ # Software Foundation; either version 3 of the License, or (at your option) #
16
+ # any later version. #
17
+ # #
18
+ # flattendb is distributed in the hope that it will be useful, but WITHOUT #
19
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
20
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
21
+ # more details. #
22
+ # #
23
+ # You should have received a copy of the GNU General Public License along #
24
+ # with flattendb. If not, see <http://www.gnu.org/licenses/>. #
25
+ # #
26
+ ###############################################################################
27
+ #++
28
+
29
+ require 'libxml'
30
+
31
+ require 'flattendb/base'
32
+
33
+ module FlattenDB
34
+
35
+ class MySQL < Base
36
+
37
+ JOIN_KEY = '@key'
38
+
39
+ attr_reader :document, :database, :name, :tables, :builder
40
+
41
+ def initialize(infile, outfile, config)
42
+ super
43
+
44
+ @document = XML::Document.file(@input.first)
45
+ @database = @document.root.find_first('database[@name]')
46
+ @name = @database[:name]
47
+ @tables = {}
48
+
49
+ parse
50
+ end
51
+
52
+ def flatten!(options = {}, builder_options = {})
53
+ flatten_tables!(tables, root, config)
54
+
55
+ self
56
+ end
57
+
58
+ def to_xml(output = output, builder_options = {})
59
+ initialize_builder(:xml, output, builder_options)
60
+
61
+ builder.instruct!
62
+
63
+ if tables.size > 1
64
+ builder.tag!(name) {
65
+ tables.sort.each { |table, rows|
66
+ table_to_xml(table, rows, builder)
67
+ }
68
+ }
69
+ else
70
+ (table, rows), _ = *tables # get "first" (and only) hash element
71
+ table_to_xml(name, rows, builder)
72
+ end
73
+
74
+ self
75
+ end
76
+
77
+ private
78
+
79
+ def parse
80
+ database.find('table_data[@name]').each { |table|
81
+ rows = []
82
+
83
+ table.find('row').each { |row|
84
+ fields = {}
85
+
86
+ row.find('field[@name]').each { |field|
87
+ fields[field[:name]] = field.content
88
+ }
89
+
90
+ rows << fields
91
+ }
92
+
93
+ tables[table[:name]] = rows
94
+ }
95
+ end
96
+
97
+ def flatten_tables!(tables, primary_table, config)
98
+ config.each { |foreign_table, spec|
99
+ case spec
100
+ when String
101
+ inject_foreign(tables, primary_table, foreign_table, spec)
102
+ when Array
103
+ inject_foreign(tables, primary_table, foreign_table, *spec)
104
+ when Hash
105
+ raise ArgumentError, "invalid join table spec, '#{JOIN_KEY}' missing" unless spec.has_key?(JOIN_KEY)
106
+
107
+ join_key_spec = spec.delete(JOIN_KEY)
108
+
109
+ joined_tables = tables.dup
110
+ flatten_tables!(joined_tables, foreign_table, spec)
111
+
112
+ (join_key_spec.is_a?(Hash) ? join_key_spec : { foreign_table => join_key_spec }).each { |foreign_table_name, join_key|
113
+ local_key, foreign_key = join_key
114
+
115
+ inject_foreign(tables, primary_table, foreign_table, local_key, foreign_key || local_key, joined_tables, foreign_table_name)
116
+ }
117
+ else
118
+ raise ArgumentError, "don't know how to handle spec of type '#{spec.class}'"
119
+ end
120
+ }
121
+
122
+ tables.delete_if { |table, _|
123
+ table != primary_table
124
+ }
125
+ end
126
+
127
+ def inject_foreign(tables, primary_table, foreign_table, local_key, foreign_key = local_key, foreign_tables = tables, foreign_table_name = foreign_table)
128
+ raise ArgumentError, "no such foreign table: #{foreign_table}" unless foreign_tables.has_key?(foreign_table)
129
+
130
+ foreign_rows = foreign_tables[foreign_table]
131
+
132
+ tables[primary_table].each { |row|
133
+ if row.has_key?(local_key)
134
+ foreign_content = foreign_rows.select { |foreign_row|
135
+ row[local_key] == foreign_row[foreign_key]
136
+ }
137
+
138
+ row[foreign_table_name] = foreign_content unless foreign_content.empty?
139
+ end
140
+ }
141
+ end
142
+
143
+ def table_to_xml(table, rows, builder)
144
+ builder.tag!(table) {
145
+ rows.each { |row|
146
+ row_to_xml('row', row, builder)
147
+ }
148
+ }
149
+ end
150
+
151
+ def row_to_xml(name, row, builder)
152
+ builder.tag!(name) {
153
+ row.sort.each { |field, content|
154
+ field_to_xml(field, content, builder)
155
+ }
156
+ }
157
+ end
158
+
159
+ def field_to_xml(field, content, builder)
160
+ case content
161
+ when String
162
+ builder.tag!(column_to_element(field), content)
163
+ when Array
164
+ content.each { |item|
165
+ field_to_xml(field, item, builder)
166
+ }
167
+ when Hash
168
+ row_to_xml(field, content, builder)
169
+ else
170
+ raise ArgumentError, "don't know how to handle content of type '#{content.class}'"
171
+ end
172
+ end
173
+
174
+ end
175
+
176
+ end
@@ -0,0 +1,55 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of flattendb, the relational database flattener. #
5
+ # #
6
+ # Copyright (C) 2007 University of Cologne, #
7
+ # Albertus-Magnus-Platz, #
8
+ # 50932 Cologne, Germany #
9
+ # #
10
+ # Authors: #
11
+ # Jens Wille <jens.wille@uni-koeln.de> #
12
+ # #
13
+ # flattendb is free software; you can redistribute it and/or modify it under #
14
+ # the terms of the GNU General Public License as published by the Free #
15
+ # Software Foundation; either version 3 of the License, or (at your option) #
16
+ # any later version. #
17
+ # #
18
+ # flattendb is distributed in the hope that it will be useful, but WITHOUT #
19
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
20
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
21
+ # more details. #
22
+ # #
23
+ # You should have received a copy of the GNU General Public License along #
24
+ # with flattendb. If not, see <http://www.gnu.org/licenses/>. #
25
+ # #
26
+ ###############################################################################
27
+ #++
28
+
29
+ module FlattenDB
30
+
31
+ module Version
32
+
33
+ MAJOR = 0
34
+ MINOR = 0
35
+ TINY = 4
36
+
37
+ class << self
38
+
39
+ # Returns array representation.
40
+ def to_a
41
+ [MAJOR, MINOR, TINY]
42
+ end
43
+
44
+ # Short-cut for version string.
45
+ def to_s
46
+ to_a.join('.')
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+
53
+ VERSION = Version.to_s
54
+
55
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blackwinter-flattendb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Jens Wille
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-19 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: highline
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: libxml-ruby
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "0"
32
+ version:
33
+ - !ruby/object:Gem::Dependency
34
+ name: builder
35
+ version_requirement:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ - !ruby/object:Gem::Dependency
43
+ name: ruby-nuggets
44
+ version_requirement:
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ description: Flatten relational databases.
52
+ email: jens.wille@uni-koeln.de
53
+ executables:
54
+ - flattendb.mysql
55
+ - flattendb
56
+ - flattendb.mdb
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - COPYING
61
+ - ChangeLog
62
+ - README
63
+ files:
64
+ - lib/flattendb/types/mdb.rb
65
+ - lib/flattendb/types/mysql.rb
66
+ - lib/flattendb/cli.rb
67
+ - lib/flattendb/version.rb
68
+ - lib/flattendb/base.rb
69
+ - lib/flattendb.rb
70
+ - bin/flattendb.mysql
71
+ - bin/flattendb
72
+ - bin/flattendb.mdb
73
+ - Rakefile
74
+ - COPYING
75
+ - ChangeLog
76
+ - README
77
+ - example/mysql-sample.xml
78
+ - example/mysql-sample2flat.yaml
79
+ - example/mysql-sample.flat.xml
80
+ - example/mysql-sample.sql
81
+ has_rdoc: true
82
+ homepage: http://prometheus.rubyforge.org/flattendb
83
+ post_install_message:
84
+ rdoc_options:
85
+ - --line-numbers
86
+ - --inline-source
87
+ - --title
88
+ - flattendb Application documentation
89
+ - --main
90
+ - README
91
+ - --charset
92
+ - UTF-8
93
+ - --all
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ version:
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ version:
108
+ requirements: []
109
+
110
+ rubyforge_project: prometheus
111
+ rubygems_version: 1.2.0
112
+ signing_key:
113
+ specification_version: 2
114
+ summary: Flatten relational databases.
115
+ test_files: []
116
+