<commit_before># Niceql

**ATTENTION: Since ver 0.5.0 the ActiveRecord integration will be provided via standalone gem: [rails_sql_prettifier](https://github.com/alekseyl/rails_sql_prettifier)!**

This is a small, nice, simple and zero dependency solution for SQL prettifying for Ruby. 
It can be used in an irb console without any dependencies ( run bin/console and look for examples ).

Any reasonable suggestions are welcome. 

**Please pay attention: untill issue https://github.com/alekseyl/niceql/issues/16 is resolved any UPDATE or INSERT request might corrupt your data, don't use on production!**

 
## Before/After 
### SQL prettifier: 
![alt text](https://github.com/alekseyl/niceql/raw/master/to_niceql.png "To_niceql")

### PG errors prettifier 

before: 
![alt text](https://github.com/alekseyl/niceql/raw/master/err_was.png "To_niceql")

after:
![alt text](https://github.com/alekseyl/niceql/raw/master/err_now.png "To_niceql")


## Installation

Add this line to your application's Gemfile:

```ruby
gem 'niceql'
```

And then execute:

    $ bundle
    # if you are using rails, you may want to install niceql config:
    rails g niceql:install 

Or install it yourself as:

    $ gem install niceql

## Configuration

```ruby
Niceql.configure do |c|
  # Setting pg_adapter_with_nicesql to true will force formatting SQL queries
  # before execution. Formatted SQL will lead to much better SQL-query debugging and much more clearer error messages 
  # if you are using Postgresql as a data source.  
  # 
  # You can adjust pg_adapter in production but do it at your own risk!
  # 
  # If you need to debug SQL queries in production use exec_niceql
  # default: false
  # uncomment next string to enable in development
  # c.pg_adapter_with_nicesql = Rails.env.development?
  
  # uncomment next string if you want to log prettified SQL inside ActiveRecord logging. 
  # default: false
  # c.prettify_active_record_log_output = true
  
  # Error prettifying is also configurable
  # default: defined? ::ActiveRecord::Base && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
  # c.prettify_pg_errors = defined? ::ActiveRecord::Base && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
  
  # spaces count for one indentation
  c.indentation_base = 2
  
  # setting open_bracket_is_newliner to true will start opening brackets '(' with nested subqueries from new line 
  # i.e. SELECT * FROM ( SELECT * FROM tags ) tags; will transform to: 
  # SELECT * 
  # FROM 
  # ( 
 #    SELECT * FROM tags 
 #  ) tags; 
 # when open_bracket_is_newliner is false: 
  # SELECT * 
  # FROM ( 
 #   SELECT * FROM tags 
 # ) tags; 
 # default: false
  c.open_bracket_is_newliner = false
end
```

## Usage

### With ActiveRecord

```ruby
  # puts colorized and formatted corresponding SQL query
  Model.scope.niceql
  
  # only formatting without colorization, you can run output of to_niceql as a SQL query in connection.execute  
  Model.scope.to_niceql<commit_msg>readme update<commit_after># Niceql

**ATTENTION: After ver 0.5.0 the ActiveRecord integration is provided via standalone gem: [rails_sql_prettifier](https://github.com/alekseyl/rails_sql_prettifier)!**

This is a small, nice, simple and zero dependency solution for SQL prettifying for Ruby. 
It can be used in an irb console without any dependencies ( run bin/console and look for examples ).

Any reasonable suggestions are welcome. 

**Please pay attention: even though issue https://github.com/alekseyl/niceql/issues/16 is resolved 
 still potentially UPDATE or INSERT request might corrupt your data, please don't patch pg_adapter on production!**

 
## Before/After 
### SQL prettifier: 
![alt text](https://github.com/alekseyl/niceql/raw/master/to_niceql.png "To_niceql")

### PG errors prettifier 

before: 
![alt text](https://github.com/alekseyl/niceql/raw/master/err_was.png "To_niceql")

after:
![alt text](https://github.com/alekseyl/niceql/raw/master/err_now.png "To_niceql")


## Installation

Add this line to your application's Gemfile:

```ruby
gem 'niceql'
```

And then execute:

    $ bundle
    # if you are using rails, you may want to install niceql config:
    rails g niceql:install 

Or install it yourself as:

    $ gem install niceql

## Configuration

```ruby
Niceql.configure do |c|
  # Setting pg_adapter_with_nicesql to true will force formatting SQL queries
  # before execution. Formatted SQL will lead to much better SQL-query debugging and much more clearer error messages 
  # if you are using Postgresql as a data source.  
  # 
  # Adjusting pg_adapter in production is strongly discouraged! 
  # 
  # If you need to debug SQL queries in production use exec_niceql
  # default: false
  # uncomment next string to enable in development
  # c.pg_adapter_with_nicesql = Rails.env.development?
  
  # uncomment next string if you want to log prettified SQL inside ActiveRecord logging. 
  # default: false
  # c.prettify_active_record_log_output = true
  
  # Error prettifying is also configurable
  # default: defined? ::ActiveRecord::Base && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
  # c.prettify_pg_errors = defined? ::ActiveRecord::Base && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
  
  # spaces count for one indentation
  c.indentation_base = 2
  
  # setting open_bracket_is_newliner to true will start opening brackets '(' with nested subqueries from new line 
  # i.e. SELECT * FROM ( SELECT * FROM tags ) tags; will transform to: 
  # SELECT * 
  # FROM 
  # ( 
 #    SELECT * FROM tags 
 #  ) tags; 
 # when open_bracket_is_newliner is false: 
  # SELECT * 
  # FROM ( 
 #   SELECT * FROM tags 
 # ) tags; 
 # default: false
  c.open_bracket_is_newliner = false
end
```

## Usage

### With ActiveRecord ( you need rails_sql_prettifier for that! )

```ruby
  # puts colorized and formatted corresponding SQL query
  Model.scope.niceql
  
  # only formatting without colorization, you can run output of to_niceql as a SQL query in connection.execute  
  Model.scope.to_niceql
{'fraction_non_alphanumeric': 0.09004428407413483, 'fraction_numerical': 0.0019681810726586845, 'mean_word_length': 3.994266994266994, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1581246', 'n_tokens_mistral': 1968, 'n_tokens_neox': 1822, 'n_words': 636}