execute_sql 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9d044ff6ed461e770b7cbdab32ae3631f63b67770d8bfac0db8c9b262d2921bb
4
+ data.tar.gz: daceaad6d0633c7567783e213a73c23911798010609ebb4e71ba539530f7daff
5
+ SHA512:
6
+ metadata.gz: acb0057884f0562a44c564e4bdf68b2db53510c0daab1368fe83fd8fbf6908d3f62424c7273e06c531d821cf60927b4808d3e0255675e22f77b53e9fbcdb7094
7
+ data.tar.gz: 5bec6886e112aa42fcbd6c5727e563717b93ccda3651903cac3fa339b2b2f7adb1f066f9beb681db069ddb85067d72ec2553e0a8d09a8b09750b68316755f53b
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Igor Kasyanchuk
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Rails Execute SQL
2
+
3
+ Execute SQL with `execute_sql` helper inside your Rails apps. Directly in `rails console`.
4
+
5
+ <img src="https://github.com/igorkasyanchuk/execute_sql/blob/master/docs/sample_execute_sql.png?raw=true" width="60%" />
6
+
7
+ ## Usage
8
+
9
+ Simply add this gem into your Gemfile.
10
+
11
+ And for example call directly in `rails console`:
12
+
13
+ ```ruby
14
+ execute_sql "select count(*) from users where age > 50"
15
+
16
+ # see below for additional options.
17
+ ```
18
+
19
+ OR in your models, controllers, or other parts of app:
20
+
21
+ ```ruby
22
+ ExecuteSQL.run "select count(*) from users where age > 50"
23
+
24
+ # or with different mode
25
+ # default mode: :print
26
+
27
+ # return single value
28
+ ExecuteSQL.run "select count(*) from users where age > 50", mode: :single
29
+
30
+ # return array of results
31
+ ExecuteSQL.run "select * from users where age > 50", mode: :raw
32
+
33
+ # just execute and return nil
34
+ ExecuteSQL.run "truncate table users", mode: :none
35
+ ```
36
+
37
+ Sample in controller:
38
+
39
+ ```ruby
40
+ class HomeController < ApplicationController
41
+ def index
42
+ @users = ExecuteSql.run "select * from users", mode: :raw
43
+ end
44
+ end
45
+ ```
46
+
47
+ ## Installation
48
+ Add this line to your application's Gemfile:
49
+
50
+ ```ruby
51
+ gem 'execute_sql'
52
+ ```
53
+
54
+ And then execute:
55
+ ```bash
56
+ $ bundle
57
+ ```
58
+
59
+ ## Options and modes
60
+
61
+ You can call:
62
+
63
+ In rails console use helper: `execute_sql "some SQL"`.
64
+
65
+ Or `ExecuteSql.run "some SQL"` or `ExecuteSQL.run "some SQL"`.
66
+
67
+ ## Contributing
68
+
69
+ You are welcome to contribute.
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ExecuteSql'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,49 @@
1
+ module ExecuteSql
2
+ module Adapters
3
+
4
+ class BaseAdapter
5
+ extend ::ExecuteSql::Connection
6
+
7
+ MULTI_STATEMENT_HELP_TEXT = "EXPERIMENTAL: You can import only file with SQL statements separated by ';'. Each new statement must start from new line."
8
+
9
+ def self.execute(sql)
10
+ t0 = Time.now
11
+ connection.execute(sql)
12
+ Time.now - t0
13
+ end
14
+
15
+ def self.exec_query(sql)
16
+ t0 = Time.now
17
+ results = connection.exec_query(sql)
18
+ execution_time = Time.now - t0
19
+ [results, execution_time]
20
+ end
21
+
22
+ def self.select(sql)
23
+ BaseAdapter.exec_query(sql)
24
+ end
25
+
26
+ def self.explain(sql)
27
+ BaseAdapter.exec_query(sql)
28
+ end
29
+
30
+ def self.adapter_name
31
+ 'base'
32
+ end
33
+
34
+ def self.mime
35
+ 'text/x-sql'
36
+ end
37
+
38
+ private
39
+
40
+ def self.multiple_execute(sql, divider = ";\n")
41
+ sql.split(divider).each do |statement|
42
+ connection.execute(statement)
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,31 @@
1
+ module ExecuteSql
2
+ module Adapters
3
+
4
+ class Mysql < BaseAdapter
5
+
6
+ def self.execute(sql)
7
+ t0 = Time.now
8
+ multiple_execute(sql)
9
+ Time.now - t0
10
+ end
11
+
12
+ def self.exec_query(sql, log = true)
13
+ t0 = Time.now
14
+ results = connection.execute(sql, 'SQL') # used from RoR mysql adapter source
15
+ result = MysqlResult.new(results)
16
+ execution_time = Time.now - t0
17
+ [result, execution_time]
18
+ end
19
+
20
+ def self.adapter_name
21
+ 'mysql'
22
+ end
23
+
24
+ def self.mime
25
+ 'text/x-mysql'
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ module ExecuteSql
2
+ module Adapters
3
+
4
+ class Postgres < BaseAdapter
5
+
6
+ def self.adapter_name
7
+ 'postgres'
8
+ end
9
+
10
+ def self.mime
11
+ 'text/x-plsql'
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module ExecuteSql
2
+ module Adapters
3
+
4
+ class Sqlite < BaseAdapter
5
+
6
+ def self.execute(sql)
7
+ t0 = Time.now
8
+ multiple_execute(sql)
9
+ Time.now - t0
10
+ end
11
+
12
+ def self.adapter_name
13
+ 'sqlite'
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ module ExecuteSql
2
+ class BlankResults
3
+
4
+ def rows; [] end
5
+ def columns; [] end
6
+
7
+ end
8
+ end
@@ -0,0 +1,25 @@
1
+ module ExecuteSql
2
+ module Connection
3
+
4
+ def connection
5
+ ActiveRecord::Base.connection
6
+ end
7
+
8
+ def columns
9
+ connection.columns(name)
10
+ end
11
+
12
+ def column_properties
13
+ %w(name sql_type null limit precision scale type default)
14
+ end
15
+
16
+ def to_param
17
+ name
18
+ end
19
+
20
+ def column_names
21
+ columns.collect(&:name)
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ module ExecuteSql
2
+ class Database
3
+ extend Connection
4
+
5
+ class << self
6
+ delegate :execute, to: :adapter
7
+ delegate :select, to: :adapter
8
+ delegate :explain, to: :adapter
9
+ delegate :exec_query, to: :adapter
10
+ end
11
+
12
+ def self.adapter
13
+ case connection.class.to_s
14
+ when /Mysql/
15
+ ExecuteSql::Adapters::Mysql
16
+ when /Postgre/
17
+ ExecuteSql::Adapters::Postgres
18
+ when /SQLite/
19
+ ExecuteSql::Adapters::Sqlite
20
+ else
21
+ ExecuteSql::Adapters::BaseAdapter
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,4 @@
1
+ module ExecuteSql
2
+ class ExecuteSqlError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,18 @@
1
+ module ExecuteSql
2
+ class MysqlResult
3
+
4
+ attr_reader :columns, :rows
5
+
6
+ delegate :each, to: :rows
7
+
8
+ def initialize(result)
9
+ @columns = []
10
+ @rows = []
11
+ if result
12
+ @columns = result.fields
13
+ @rows = result.entries
14
+ end
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module ExecuteSql
2
+ class Railtie < ::Rails::Railtie
3
+
4
+ console do
5
+ TOPLEVEL_BINDING.eval('self').extend ExecuteSql::ConsoleMethods
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ module ExecuteSql
2
+ class Result
3
+
4
+ attr_reader :error
5
+
6
+ def initialize(error)
7
+ @error = error
8
+ end
9
+
10
+ def self.ok
11
+ Result.new(nil)
12
+ end
13
+
14
+ def ok?
15
+ error.nil?
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module ExecuteSql
2
+ class SqlExplain < SqlQueryData
3
+
4
+ def sql
5
+ "EXPLAIN #{super}"
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ module ExecuteSql
2
+ class SqlImport
3
+ include Connection
4
+
5
+ attr_reader :time, :result
6
+
7
+ def initialize(file)
8
+ @file = file
9
+ end
10
+
11
+ def valid?
12
+ if @file.nil?
13
+ raise ExecuteSqlError.new('Please select valid SQL file and continue')
14
+ end
15
+ end
16
+
17
+ def import
18
+ valid?
19
+ @time = Database.adapter.execute(@file.read)
20
+ @result = Result.ok
21
+ rescue ExecuteSqlError, ActiveRecord::StatementInvalid => e
22
+ @result = Result.new(e)
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,42 @@
1
+ module ExecuteSql
2
+ class SqlQuery
3
+ include Connection
4
+
5
+ attr_reader :query, :data, :explain, :sql_explain
6
+
7
+ def initialize(query, sql_explain = false)
8
+ @query = query
9
+ @sql_explain = sql_explain
10
+ end
11
+
12
+ def valid?
13
+ query.present?
14
+ end
15
+
16
+ def load_explain
17
+ @explain ||= SqlExplain.new(self).load_data
18
+ end
19
+
20
+ def load_data
21
+ @data ||= SqlQueryData.new(self).load_data
22
+ end
23
+
24
+ def execute
25
+ if valid?
26
+ load_data
27
+ load_explain if sql_explain
28
+ end
29
+ self
30
+ end
31
+
32
+ def to_csv
33
+ CSV.generate do |csv|
34
+ csv << data.columns
35
+ data.rows.each do |row|
36
+ csv << row
37
+ end
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,36 @@
1
+ module ExecuteSql
2
+ class SqlQueryData
3
+
4
+ attr_reader :sql_query, :time, :columns, :rows, :error
5
+
6
+ def initialize(sql_query)
7
+ @rows = []
8
+ @columns = []
9
+ @sql_query = sql_query
10
+ end
11
+
12
+ def load_data
13
+ result, @time = Database.adapter.exec_query(sql)
14
+ @columns = result.columns
15
+ @rows = result.rows
16
+ self
17
+ rescue ActiveRecord::StatementInvalid => e
18
+ @columns, @rows = [], []
19
+ @error = e
20
+ self
21
+ end
22
+
23
+ def count
24
+ rows.count
25
+ end
26
+
27
+ def is_error?
28
+ error.present?
29
+ end
30
+
31
+ def sql
32
+ sql_query.query
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module ExecuteSql
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,51 @@
1
+ require 'terminal-table'
2
+
3
+ require_relative 'execute_sql/connection'
4
+ require_relative 'execute_sql/database'
5
+ require_relative 'execute_sql/blank_results'
6
+ require_relative 'execute_sql/mysql_result'
7
+ require_relative 'execute_sql/result'
8
+ require_relative 'execute_sql/execute_sql_error'
9
+
10
+ require_relative 'execute_sql/sql_query_data'
11
+ require_relative 'execute_sql/sql_explain'
12
+ require_relative 'execute_sql/sql_import'
13
+ require_relative 'execute_sql/sql_query'
14
+
15
+ require_relative 'execute_sql/adapters/base_adapter'
16
+ require_relative 'execute_sql/adapters/mysql'
17
+ require_relative 'execute_sql/adapters/postgres'
18
+ require_relative 'execute_sql/adapters/sqlite'
19
+
20
+ require "execute_sql/railtie"
21
+
22
+ module ExecuteSql
23
+ module ConsoleMethods
24
+
25
+ def execute_sql(sql, mode: :print)
26
+ sql_query = ExecuteSql::SqlQuery.new("#{sql}".strip).execute
27
+ rows = sql_query.data.rows
28
+ cols = sql_query.data.columns
29
+ case mode.to_s
30
+ when 'print'
31
+ puts Terminal::Table.new(rows: rows, headings: cols)
32
+ when 'raw'
33
+ rows
34
+ when 'single'
35
+ rows.flatten[0]
36
+ when 'none'
37
+ nil
38
+ else
39
+ []
40
+ end
41
+ end
42
+
43
+ def run(sql, *args)
44
+ execute_sql(sql, *args)
45
+ end
46
+ end
47
+
48
+ extend ConsoleMethods
49
+ end
50
+
51
+ ExecuteSQL = ExecuteSql
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :execute_sql do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: execute_sql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Kasyanchuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: terminal-table
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.3.13
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.3.13
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Execute SQL directly in rails console or in the code.
70
+ email:
71
+ - igorkasyanchuk@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - lib/execute_sql.rb
80
+ - lib/execute_sql/adapters/base_adapter.rb
81
+ - lib/execute_sql/adapters/mysql.rb
82
+ - lib/execute_sql/adapters/postgres.rb
83
+ - lib/execute_sql/adapters/sqlite.rb
84
+ - lib/execute_sql/blank_results.rb
85
+ - lib/execute_sql/connection.rb
86
+ - lib/execute_sql/database.rb
87
+ - lib/execute_sql/execute_sql_error.rb
88
+ - lib/execute_sql/mysql_result.rb
89
+ - lib/execute_sql/railtie.rb
90
+ - lib/execute_sql/result.rb
91
+ - lib/execute_sql/sql_explain.rb
92
+ - lib/execute_sql/sql_import.rb
93
+ - lib/execute_sql/sql_query.rb
94
+ - lib/execute_sql/sql_query_data.rb
95
+ - lib/execute_sql/version.rb
96
+ - lib/tasks/execute_sql_tasks.rake
97
+ homepage: https://github.com/igorkasyanchuk/execute_sql
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.0.2
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Execute SQL directly in rails console or in the code.
120
+ test_files: []