Giter VIP home page Giter VIP logo

execute_sql's Introduction

Rails Execute SQL

RailsJazz https://www.patreon.com/igorkasyanchuk Listed on OpenSource-Heroes.com

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:

execute_sql "select count(*) from users where age > 50"

# see below for additional options.

OR in your models, controllers, or other parts of app:

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:

class HomeController < ApplicationController
  def index
    @users = ExecuteSql.run "select * from users", mode: :raw
  end
end

Installation

Add this line to your application's Gemfile:

gem 'execute_sql'

And then execute:

$ 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.

Some pieces of code I took from another my gem: https://github.com/igorkasyanchuk/rails_db.

Contributors

  • @pjforde1978

Big thank you.

execute_sql's People

Contributors

igorkasyanchuk avatar leastbad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.