lib/niceql.rb in niceql-0.1.14 vs lib/niceql.rb in niceql-0.1.15
- old
+ new
@@ -32,11 +32,11 @@
end
module Prettifier
INLINE_VERBS = %w(WITH ASC IN COALESCE AS WHEN THEN ELSE END AND UNION ALL WITH ON DISTINCT INTERSECT EXCEPT EXISTS NOT COUNT ROUND CAST).join('| ')
- NEW_LINE_VERBS = 'SELECT|FROM|WHERE|CASE|ORDER BY|LIMIT|GROUP BY|LEFT JOIN|RIGHT JOIN|JOIN|HAVING|OFFSET|UPDATE'
+ NEW_LINE_VERBS = 'SELECT|FROM|WHERE|CASE|ORDER BY|LIMIT|GROUP BY|(RIGHT |LEFT )*(INNER |OUTER )*JOIN|HAVING|OFFSET|UPDATE'
POSSIBLE_INLINER = /(ORDER BY|CASE)/
VERBS = "#{INLINE_VERBS}|#{NEW_LINE_VERBS}"
STRINGS = /("[^"]+")|('[^']+')/
BRACKETS = '[\(\)]'
@@ -93,11 +93,11 @@
parentness = []
#it's better to remove all new lines because it will break formatting
sql = sql.gsub("\n", ' ')
# remove any additional formatting
- sql = sql.gsub(/[ ]+/, ' ')
+ sql = sql.gsub(/[\s]+/, ' ')
sql = sql.gsub(STRINGS){ |str| StringColorize.colorize_str(str) } if colorize
first_verb = true
sql.gsub( /(#{VERBS}|#{BRACKETS})/).with_index do |verb, index|
@@ -135,10 +135,19 @@
# replacing sql with prettified sql, thats all
super( Prettifier.prettify_sql(sql, false), name, binds, prepare: prepare )
end
end
+ module AbstractAdapterLogPrettifier
+ def log( sql, *args, &block )
+ # \n need to be placed because AR log will start with action description + time info.
+ # rescue sql - just to be sure Prettifier didn't break production
+ formatted_sql = "\n" + Prettifier.prettify_sql(sql, false) rescue sql
+ super( formatted_sql, *args, &block )
+ end
+ end
+
module ErrorExt
def to_s
if ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
Prettifier.prettify_err( super )
else
@@ -146,28 +155,30 @@
end
end
end
class NiceQLConfig
- attr_accessor :pg_adapter_with_nicesql
+ attr_accessor :pg_adapter_with_nicesql,
+ :indentation_base,
+ :open_bracket_is_newliner,
+ :prettify_active_record_log_output
- attr_accessor :indentation_base
-
- attr_accessor :open_bracket_is_newliner
-
def initialize
self.pg_adapter_with_nicesql = false
self.indentation_base = 2
self.open_bracket_is_newliner = false
+ self.prettify_active_record_log_output = false
end
end
def self.configure
yield( config )
if config.pg_adapter_with_nicesql
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(PostgresAdapterNiceQL)
+ elsif config.prettify_active_record_log_output
+ ::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend( AbstractAdapterLogPrettifier )
end
end
def self.config
@config ||= NiceQLConfig.new