Skip to content
Browse files

unifying two branches of the parser

  • Loading branch information...
1 parent f0a9392 commit 85dfca9b91f1a31062919f4b158cb2ad46ede593 @german committed Apr 13, 2012
Showing with 21 additions and 42 deletions.
  1. +21 −42 joint_parser.rb
View
63 joint_parser.rb
@@ -44,11 +44,8 @@
puts 'condition - ' + condition.inspect
# match "i.id = l.io_id"
if cond = condition.match(/(?<f_table>[\w_]+)\.(?<f_table_column>[\w_]+)\s*=\s*(?<s_table>[\w_]+)\.(?<s_table_column>[\w_]+)/mi)
- puts 'cond - ' + cond.inspect
+
if (fta = table_aliases.detect{|talias, _| talias == cond[:f_table] }) && (sta = table_aliases.detect{|talias, _| talias == cond[:s_table]})
- puts 'fta - ' + fta.inspect
- puts 'sta - ' + sta.inspect
-
table_joins[fta[1]] ||= {}
table_joins[fta[1]][cond[:f_table_column]] = [cond[:s_table_column].to_s.strip, sta[1]]
@@ -66,38 +63,22 @@
tables << primary_table
table_aliases[primary_table_alias] = primary_table
- sql.scan(/(inner|left|right)*\s+join\s+([\w_]{2,}) ([\w_]+)\s+on\s+([\w_\.]+)\s*=\s*([\w_\.]+)/mi).each do |joins|
- puts 'joins - ' + joins.inspect
-
- join_table = joins[1].strip
- join_table_alias = joins[2].strip
-
- table_aliases[join_table_alias] = join_table
- tables << join_table
-
- first_in_join_equality = joins[3].strip
- second_in_join_equality = joins[4].strip
-
- if join_table && join_table_alias && first_in_join_equality && second_in_join_equality
- m_first = first_in_join_equality.split('.')
- m_second = second_in_join_equality.split('.')
-
- first_table_alias = m_first.first.strip
- second_table_alias = m_second.first.strip
+ #m = sql.match(/where\s+(.*)/mi)
- puts 'table aliases - ' + table_aliases.inspect
- puts 'first_table_alias - ' + first_table_alias.inspect
- puts 'second_table_alias - ' + second_table_alias.inspect
-
- if (fta = table_aliases.detect{|talias, _| talias == first_table_alias }) && (sta = table_aliases.detect{|talias, _| talias == second_table_alias})
- puts 'fta - ' + fta.inspect
- puts 'sta - ' + sta.inspect
+ if ! m.nil?
+ sql.scan(/(inner|left|right)*\s+join\s+(?<join_table>[\w_]{2,}) (?<join_table_alias>[\w_]+)\s+on\s+(?<f_table>[\w_]+)\.(?<f_table_column>[\w_]+)\s*=\s*(?<s_table>[\w_]+)\.(?<s_table_column>[\w_]+)/mi).each do |joins|
+ cond = {:f_table_column => joins[3], :f_table => joins[2], :s_table => joins[4], :s_table_column => joins[5], :join_table => joins[0], :join_table_alias => joins[1]}
+ table_aliases[cond[:join_table_alias]] = cond[:join_table]
+ tables << cond[:join_table]
- table_joins[fta[1]] ||= {}
- table_joins[fta[1]][m_first.last.strip] = [m_second.last.strip, sta[1]]
+ if cond[:f_table_column] && cond[:s_table_column] && cond[:f_table] && cond[:s_table]
+ if (fta = table_aliases.detect{|talias, _| talias == cond[:f_table] }) && (sta = table_aliases.detect{|talias, _| talias == cond[:s_table]})
+ table_joins[fta[1]] ||= {}
+ table_joins[fta[1]][cond[:f_table_column]] = [cond[:s_table_column], sta[1]]
- table_joins[sta[1]] ||= {}
- table_joins[sta[1]][m_second.last.strip] = [m_first.last.strip, fta[1]]
+ table_joins[sta[1]] ||= {}
+ table_joins[sta[1]][cond[:s_table_column]] = [cond[:f_table_column], fta[1]]
+ end
end
end
end
@@ -118,10 +99,7 @@
table_colors[table] = table_color
table_label = if table_joins[table]
- join_columns = table_joins[table].each_with_index.collect do |kv, i|
- puts 'kv - ' + kv.inspect
- "<f#{i+1}> #{kv[0]}"
- end.join(' | ')
+ join_columns = table_joins[table].each_with_index.collect{|kv, i| "<f#{i+1}> #{kv[0]}"}.join(' | ')
"<f0> #{table} | #{join_columns}"
else
@@ -139,7 +117,8 @@
joins.each do |foreign_key, foreigns|
foreign_table_key, foreign_table = foreigns
- tailport = 0
+ # starting headport/tailport on 1 because of <f0> is a name of the table in the node's label
+ tailport = 1
table_joins[table].each do |k, v|
if k == foreign_key
break
@@ -148,7 +127,7 @@
end
end
- headport = 0
+ headport = 1
table_joins[foreign_table].each do |k, v|
if k == foreign_table_key
break
@@ -157,10 +136,10 @@
end
end
- puts "connecting #{table} and #{foreign_table} [headport f#{headport+1}, tailport f#{tailport+1}]"
- g.add_edges( nodes.detect{|n| n.id == table}, nodes.detect{|n| n.id == foreign_table}, :headport => "f#{headport+1}", :tailport => "f#{tailport+1}", :color => table_colors[table] )
+ puts "connecting #{table} and #{foreign_table} [headport f#{headport}, tailport f#{tailport}]"
+ g.add_edges( nodes.detect{|n| n.id == table}, nodes.detect{|n| n.id == foreign_table}, :headport => "f#{headport}", :tailport => "f#{tailport}", :color => table_colors[table] )
end
end
-# Generate output image
+# Generate output image of the relations between tables
g.output( :png => "schema.png" )

0 comments on commit 85dfca9

Please sign in to comment.
Something went wrong with that request. Please try again.