Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/igorkasyanchuk/execute_sql
Execute SQL inside Rails console, or app itself
https://github.com/igorkasyanchuk/execute_sql
Last synced: about 2 months ago
JSON representation
Execute SQL inside Rails console, or app itself
- Host: GitHub
- URL: https://github.com/igorkasyanchuk/execute_sql
- Owner: igorkasyanchuk
- License: mit
- Created: 2019-03-13T20:52:23.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-02-28T13:18:17.000Z (over 1 year ago)
- Last Synced: 2024-04-14T07:00:47.146Z (6 months ago)
- Language: Ruby
- Homepage: https://www.railsjazz.com/
- Size: 130 KB
- Stars: 42
- Watchers: 3
- Forks: 3
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: MIT-LICENSE
Awesome Lists containing this project
README
# Rails Execute SQL
[![RailsJazz](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/my_other.svg?raw=true)](https://www.railsjazz.com)
[![https://www.patreon.com/igorkasyanchuk](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/patron.svg?raw=true)](https://www.patreon.com/igorkasyanchuk)
[![Listed on OpenSource-Heroes.com](https://opensource-heroes.com/badge-v1.svg)](https://opensource-heroes.com/r/igorkasyanchuk/execute_sql)Execute SQL with `execute_sql` helper inside your Rails apps. Directly in `rails console`.
## Usage
Simply add this gem into your Gemfile.
And for example call directly in `rails console`:
```ruby
execute_sql "select count(*) from users where age > 50"# see below for additional options.
```OR in your models, controllers, or other parts of app:
```ruby
ExecuteSQL.run "select count(*) from users where age > 50"# or with different mode
# default mode: :print# return array of HashWithIndifferentAccess objects
# please remember that arrays can be manipulated with Enumerable methods, but this is *not* a chainable ARel relation
ExecuteSQL.run "select * from users where age > 50", mode: :array# return array of User objects
ExecuteSQL.run "select * from users where age > 50", mode: :array, klass: User# return single value
ExecuteSQL.run "select count(*) from users where age > 50", mode: :single# return array of results
ExecuteSQL.run "select * from users where age > 50", mode: :raw# just execute and return nil
ExecuteSQL.run "truncate table users", mode: :none
```Sample in controller:
```ruby
class HomeController < ApplicationController
def index
@users = ExecuteSql.run "select * from users", mode: :raw
end
end
```## Installation
Add this line to your application's Gemfile:```ruby
gem 'execute_sql'
```And then execute:
```bash
$ bundle
```## Options and modes
You can call:
In rails console use helper: `execute_sql "some SQL"`.
Or `ExecuteSql.run "some SQL"` or `ExecuteSQL.run "some SQL"`.
## TODO
- import SQL files
- travis CI
- verify with older rails
- more specs## Contributing
You are welcome to contribute.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
Some pieces of code I took from another my gem: https://github.com/igorkasyanchuk/rails_db.
## Contributors
- @pjforde1978
Big thank you.
[](https://www.railsjazz.com/?utm_source=github&utm_medium=bottom&utm_campaign=execute_sql)