Class: EachSQL
- Inherits:
-
Object
- Object
- EachSQL
- Includes:
- Enumerable
- Defined in:
- lib/each_sql/each_sql.rb,
lib/each_sql/parser.rb
Overview
Enumerable EachSQL object.
Defined Under Namespace
Modules: Parser
Instance Method Summary collapse
-
#<<(input) ⇒ Object
Appends the given String to the buffer.
-
#delimiter ⇒ Object
@return Current delimiter.
- #delimiter=(delim) ⇒ Object
-
#each {|String| ... } ⇒ NilClass
Parses the buffer and enumerates through the executable blocks.
-
#empty? ⇒ Boolean
Return is the buffer is empty.
-
#initialize(type) ⇒ EachSQL
constructor
@param type RDBMS type: :default|:mysql|:oracle|:postgres.
-
#shift ⇒ Hash
Parses current buffer and returns the result in Hash.
Constructor Details
Instance Method Details
#<<(input) ⇒ Object
Appends the given String to the buffer. @param input String to append
31 32 33 34 35 36 |
# File 'lib/each_sql/each_sql.rb', line 31 def << input if input @data << input.sub(/\A#{[65279].pack('U*')}/, '') # BOM (FIXME) end self end |
#delimiter ⇒ Object
@return Current delimiter.
25 26 27 |
# File 'lib/each_sql/each_sql.rb', line 25 def delimiter @delim end |
#delimiter=(delim) ⇒ Object
18 19 20 21 22 |
# File 'lib/each_sql/each_sql.rb', line 18 def delimiter= delim @delim = delim @parser = EachSQL::Parser.parser_for @type, delim self end |
#each {|String| ... } ⇒ NilClass
Parses the buffer and enumerates through the executable blocks.
62 63 64 65 66 67 68 69 70 |
# File 'lib/each_sql/each_sql.rb', line 62 def each result = shift sqls = (result[:sqls] + result[:leftover]). map { |sql| strip_sql(sql) }. reject(&:empty?) sqls.each do |sql| yield sql end end |
#empty? ⇒ Boolean
Return is the buffer is empty
55 56 57 |
# File 'lib/each_sql/each_sql.rb', line 55 def empty? @data.gsub(/\s/, '').empty? end |
#shift ⇒ Hash
Parses current buffer and returns the result in Hash. :sqls is an Array of processed executable SQL blocks, :leftover is the unparsed trailing data
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/each_sql/each_sql.rb', line 42 def shift result = @parser.parse @data @data = result.captures[:leftover].join leftover = strip_sql(@data) { :sqls => result.captures[:execution_block].map { |b| strip_sql b }, :leftover => leftover.empty? ? nil : leftover } end |