Class: SQLtorial::SqlToExample
- Inherits:
-
Object
- Object
- SQLtorial::SqlToExample
- Defined in:
- lib/sqltorial/sql_to_example.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
- #formatted ⇒ Object
- #formatted_lines ⇒ Object
- #formatter ⇒ Object
-
#initialize(file, db, number) ⇒ SqlToExample
constructor
A new instance of SqlToExample.
- #make_prose_directives_and_query(query) ⇒ Object
- #number ⇒ Object
- #queries ⇒ Object
- #title ⇒ Object
- #title_line ⇒ Object
- #to_str(include_results = true) ⇒ Object
Constructor Details
#initialize(file, db, number) ⇒ SqlToExample
Returns a new instance of SqlToExample.
8 9 10 11 12 |
# File 'lib/sqltorial/sql_to_example.rb', line 8 def initialize(file, db, number) @file = file @db = db @number = number end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
7 8 9 |
# File 'lib/sqltorial/sql_to_example.rb', line 7 def file @file end |
Instance Method Details
#formatted ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/sqltorial/sql_to_example.rb', line 14 def formatted @formatted ||= `fsqlf -i #{file}` #@formatted ||= `pg_format #{file}` #@formatted ||= `cat #{file} | anbt-sql-formatter` #@formatted ||= `cat #{file} | py_format` #@formatted ||= formatter.format(file.read) #file.read end |
#formatted_lines ⇒ Object
27 28 29 30 31 32 |
# File 'lib/sqltorial/sql_to_example.rb', line 27 def formatted_lines if @formatted_lines.nil? @title_line, @formatted_lines = get_title_and_formatted_lines end @formatted_lines end |
#formatter ⇒ Object
23 24 25 |
# File 'lib/sqltorial/sql_to_example.rb', line 23 def formatter @formatter ||= Formatter.new end |
#make_prose_directives_and_query(query) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/sqltorial/sql_to_example.rb', line 49 def make_prose_directives_and_query(query) lines = query.dup prose_lines = [] lines.shift while lines.first.strip.empty? prose_lines << lines.shift.sub(/^\s*-+\s*/, ' ').chomp.sub(/^ $/, "\n\n") while lines.first =~ /^\s*(-+|$)/ directives, prose_lines = prose_lines.partition { |line| Directive.match(line) } [prose_lines.join(''), process_directives(directives), lines.join("\n")] end |
#number ⇒ Object
58 59 60 |
# File 'lib/sqltorial/sql_to_example.rb', line 58 def number @number ||= file.basename.to_s.to_i end |
#queries ⇒ Object
34 35 36 |
# File 'lib/sqltorial/sql_to_example.rb', line 34 def queries @queries ||= formatted_lines.slice_after { |l| l =~ /;$/ } end |
#title ⇒ Object
45 46 47 |
# File 'lib/sqltorial/sql_to_example.rb', line 45 def title @title ||= title_line.gsub(/^\s*-+\s*/, '') end |
#title_line ⇒ Object
38 39 40 41 42 43 |
# File 'lib/sqltorial/sql_to_example.rb', line 38 def title_line if @title_line.nil? @title_line, @formatted_lines = get_title_and_formatted_lines end @title_line end |
#to_str(include_results = true) ⇒ Object
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 |
# File 'lib/sqltorial/sql_to_example.rb', line 62 def to_str(include_results = true) hash = {} queries.each_with_index do |query, index| prose, directives, sql = make_prose_directives_and_query(query) begin if is_create(sql) execute(sql, include_results) hash[sql] = [prose, create_to_md(include_results, sql, directives)]; next elsif is_drop(sql) execute(sql, include_results) hash[sql] = [prose, nil]; next end hash[sql] = [prose, query_to_md(include_results, sql, directives)] rescue puts sql puts $!. puts $!.backtrace.join("\n") $stdin.gets end end parts = [] parts << "## Example #{number}: #{title}\n" part_num = 0 parts += hash.map do |key, value| arr = [value.first] if key && !key.empty? part_num += 1 arr << "**Query #{number}.#{part_num}**" arr << "```sql\n#{key}\n```" end arr << value.last arr.join("\n\n") end parts.join("\n") + "\n\n" end |