lib/niceql.rb in niceql-0.1.0 vs lib/niceql.rb in niceql-0.1.1
- old
+ new
@@ -17,17 +17,21 @@
end
module ArExtentions
def explain_err
begin
- connection.execute( "EXPLAIN #{Prettifier.prettify_sql(to_sql, false)}" )
+ connection.execute( "EXPLAIN #{to_nicesql}" )
rescue StandardError => e
puts Prettifier.prettify_err(e )
end
end
- def to_niceql( colorize = true )
+ def to_nicesql
+ Prettifier.prettify_sql(to_sql, false)
+ end
+
+ def puts_niceql( colorize = true )
puts Prettifier.prettify_sql( to_sql, colorize )
end
end
module Prettifier
@@ -64,17 +68,19 @@
def self.prettify_sql( sql, colorize = true )
indent = 0
parentness = []
sql = sql.gsub(STRINGS){ |str| StringColorize.colorize_str(str) } if colorize
+ first_verb = true
sql.gsub( /(#{VERBS}|#{BRACKETS})/) do |verb|
add_new_line = false
if 'SELECT' == verb
indent += 1
parentness.last[:nested] = true if parentness.last
- add_new_line = true
+ add_new_line = !first_verb
+ first_verb = false
elsif verb == '('
parentness << { nested: false }
indent += 1
elsif verb == ')'
# this also covers case when right bracket is used without corresponding left one
@@ -88,10 +94,9 @@
add_new_line = parentness.last.nil? || parentness.last[:nested]
else
add_new_line = verb[/(#{INLINE_VERBS})/].nil?
end
verb = StringColorize.colorize_verb(verb) if !['(', ')'].include?(verb) && colorize
-
add_new_line ? "\n#{' ' * indent}" + verb : verb
end
end
end