Class: MagicQuery::Schema::Parser
- Inherits:
-
Object
- Object
- MagicQuery::Schema::Parser
- Defined in:
- lib/magic_query/schema/parser.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_column(line, tables, current_table) ⇒ Object
- #add_constraint(line, tables, current_table) ⇒ Object
- #column_definition?(line) ⇒ Boolean
- #constraint?(line) ⇒ Boolean
- #create_table?(line) ⇒ Boolean
- #end_of_table?(line) ⇒ Boolean
- #handle_column(line, tables, current_table) ⇒ Object
- #handle_constraint(line, tables, current_table) ⇒ Object
- #handle_create_table(line, tables) ⇒ Object
-
#initialize(sql_schema) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
- #process_line(line, tables, current_table) ⇒ Object
- #skip_line?(line) ⇒ Boolean
Constructor Details
#initialize(sql_schema) ⇒ Parser
Returns a new instance of Parser.
10 11 12 |
# File 'lib/magic_query/schema/parser.rb', line 10 def initialize(sql_schema) @sql_schema = sql_schema.to_s end |
Class Method Details
.parse(sql_schema) ⇒ Object
6 7 8 |
# File 'lib/magic_query/schema/parser.rb', line 6 def self.parse(sql_schema) new(sql_schema).parse end |
Instance Method Details
#add_column(line, tables, current_table) ⇒ Object
73 74 75 76 |
# File 'lib/magic_query/schema/parser.rb', line 73 def add_column(line, tables, current_table) column_info = parse_column(line) tables[current_table][:columns] << column_info if column_info end |
#add_constraint(line, tables, current_table) ⇒ Object
78 79 80 |
# File 'lib/magic_query/schema/parser.rb', line 78 def add_constraint(line, tables, current_table) tables[current_table][:constraints] << line end |
#column_definition?(line) ⇒ Boolean
61 62 63 |
# File 'lib/magic_query/schema/parser.rb', line 61 def column_definition?(line) line.match?(/^\w+/) end |
#constraint?(line) ⇒ Boolean
65 66 67 |
# File 'lib/magic_query/schema/parser.rb', line 65 def constraint?(line) line.match?(/(PRIMARY KEY|FOREIGN KEY|UNIQUE|INDEX)/i) end |
#create_table?(line) ⇒ Boolean
57 58 59 |
# File 'lib/magic_query/schema/parser.rb', line 57 def create_table?(line) line.match?(/CREATE\s+TABLE/i) end |
#end_of_table?(line) ⇒ Boolean
69 70 71 |
# File 'lib/magic_query/schema/parser.rb', line 69 def end_of_table?(line) line.match?(/\);?$/) end |
#handle_column(line, tables, current_table) ⇒ Object
47 48 49 50 |
# File 'lib/magic_query/schema/parser.rb', line 47 def handle_column(line, tables, current_table) add_column(line, tables, current_table) current_table end |
#handle_constraint(line, tables, current_table) ⇒ Object
52 53 54 55 |
# File 'lib/magic_query/schema/parser.rb', line 52 def handle_constraint(line, tables, current_table) add_constraint(line, tables, current_table) current_table end |
#handle_create_table(line, tables) ⇒ Object
41 42 43 44 45 |
# File 'lib/magic_query/schema/parser.rb', line 41 def handle_create_table(line, tables) extract_table_name(line).tap do |table_name| tables[table_name] = { columns: [], constraints: [] } end end |
#parse ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/magic_query/schema/parser.rb', line 14 def parse tables = {} current_table = nil @sql_schema.lines.each do |line| line = line.strip next if skip_line?(line) current_table = process_line(line, tables, current_table) end tables end |
#process_line(line, tables, current_table) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/magic_query/schema/parser.rb', line 32 def process_line(line, tables, current_table) return handle_create_table(line, tables) if create_table?(line) return handle_column(line, tables, current_table) if current_table && column_definition?(line) return handle_constraint(line, tables, current_table) if current_table && constraint?(line) return nil if end_of_table?(line) current_table end |
#skip_line?(line) ⇒ Boolean
28 29 30 |
# File 'lib/magic_query/schema/parser.rb', line 28 def skip_line?(line) line.empty? || line.start_with?('--') end |