qry 1.0.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: ec6ece4a43106f9c48a553a266958ddfedae6039bcb51956b506723fc5cc524e
4
+ data.tar.gz: f131840b1e0ce3b10abd2ee20a87401e3297580bb03d5243bd0d72939f627388
5
+ SHA512:
6
+ metadata.gz: e7dfefa4c9aff715012f55079989a6e88e74395dd2eac411b9ff8098e34d669a2dce4a6fc5c8b8be182431f50cf294f582f2d7d7173c348da45a485ae9c0268e
7
+ data.tar.gz: b6417fe6cd4c0f2de63914c6f38f30a53cd6458a42df815f54be31754682f5715a2a659b5484de683d0b207a9bea976311a09805e9c7570d5815e1737555d6a2
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at me@austinschneider.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ ruby "2.6.3"
2
+
3
+ source "https://rubygems.org" do
4
+ gemspec
5
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ qry (1.0.0)
5
+ icy (~> 0.3)
6
+ ivo (~> 0.4)
7
+ sequel (~> 5.20)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ diff-lcs (1.3)
13
+ icy (0.3.0)
14
+ ivo (0.5.0)
15
+ rspec (3.9.0)
16
+ rspec-core (~> 3.9.0)
17
+ rspec-expectations (~> 3.9.0)
18
+ rspec-mocks (~> 3.9.0)
19
+ rspec-core (3.9.1)
20
+ rspec-support (~> 3.9.1)
21
+ rspec-expectations (3.9.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.9.0)
24
+ rspec-mocks (3.9.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.9.0)
27
+ rspec-support (3.9.2)
28
+ sequel (5.29.0)
29
+ sqlite3 (1.4.2)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ bundler (~> 2.0)!
36
+ qry!
37
+ rspec (~> 3.9)!
38
+ sqlite3 (~> 1.3)!
39
+
40
+ RUBY VERSION
41
+ ruby 2.6.3p62
42
+
43
+ BUNDLED WITH
44
+ 2.0.2
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Qry
2
+
3
+ ## Usage
4
+
5
+ ### Connect
6
+
7
+ ```ruby
8
+ require 'qry'
9
+
10
+ qry = Qry.connect(...) # Same arguments as Sequel gem
11
+ ```
12
+
13
+ ### Run
14
+
15
+ ```ruby
16
+ qry.run(<<~SQL)
17
+ create table fruits (
18
+ id integer not null primary key autoincrement,
19
+ name varchar(255)
20
+ )
21
+ SQL
22
+ ```
23
+
24
+ ### Insert
25
+
26
+ ```ruby
27
+ qry.insert('insert into fruits (name) values (?), (?)', 'Strawberry', 'Orange')
28
+ ```
29
+
30
+ ### Fetch
31
+
32
+ ```ruby
33
+ fruits = qry.fetch('select * from fruits')
34
+ fruits.size # 2
35
+ fruits[0].name # Strawberry
36
+ fruits[1].name # Orange
37
+ ```
38
+
39
+ ### Update
40
+
41
+ ```ruby
42
+ qry.update('update fruits set name = ? where id = ?', 'Mango', fruits[0].id)
43
+
44
+ fruits = qry.fetch('select * from fruits')
45
+ fruits[0].name # Mango
46
+ fruits[1].name # Orange
47
+ ```
48
+
49
+ ### Delete
50
+
51
+ ```ruby
52
+ qry.delete('delete from fruits where id = ?', fruits[1].id)
53
+
54
+ fruits = qry.fetch('select * from fruits')
55
+ fruits.size # 1
56
+ fruits[0].name # Mango
57
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "qry"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/qry.rb ADDED
@@ -0,0 +1,45 @@
1
+ require 'forwardable'
2
+ require 'icy'
3
+ require 'ivo'
4
+ require 'sequel'
5
+
6
+ Sequel.default_timezone = :utc
7
+
8
+ module Qry
9
+ extend self
10
+
11
+ def connect(**args)
12
+ instrumenter = args.delete(:instrumenter) || NullInstrumenter.new
13
+
14
+ sequel_args = if args.key?(:url)
15
+ url = args.delete(:url)
16
+ [url, args]
17
+ else
18
+ [args]
19
+ end
20
+
21
+ if block_given?
22
+ Sequel.connect(*sequel_args) do |sequel_db|
23
+ yield Interface.with(
24
+ sequel_db: sequel_db,
25
+ instrumenter: instrumenter,
26
+ )
27
+ end
28
+ else
29
+ Interface.with(
30
+ sequel_db: Sequel.connect(*sequel_args),
31
+ instrumenter: instrumenter,
32
+ )
33
+ end
34
+ end
35
+ end
36
+
37
+ if defined? ::Rails
38
+ require 'qry/rails'
39
+ else
40
+ Icy.require_tree('qry', exclude: [
41
+ 'qry/rails',
42
+ 'qry/rails.rb',
43
+ 'qry/manager.rb',
44
+ ])
45
+ end
@@ -0,0 +1,126 @@
1
+ module Qry
2
+ Interface = Ivo.new(:sequel_db, :instrumenter) do
3
+ extend Forwardable
4
+
5
+ def_delegators(:sequel_db, :transaction, :disconnect, :url)
6
+
7
+ def adapter
8
+ sequel_db.opts[:adapter]
9
+ end
10
+
11
+ def user
12
+ sequel_db.opts[:user]
13
+ end
14
+
15
+ def password
16
+ sequel_db.opts[:password]
17
+ end
18
+
19
+ def host
20
+ sequel_db.opts[:host]
21
+ end
22
+
23
+ def name
24
+ sequel_db.opts[:database]
25
+ end
26
+
27
+ def fetch(sql, *binds)
28
+ instrumenter.instrument('qry.fetch', qry: self) do
29
+ sequel_db.fetch(sql, *binds).map { |row| Ivo.(row) }
30
+ end
31
+ end
32
+
33
+ def insert(sql, *binds)
34
+ instrumenter.instrument('qry.insert', qry: self) do
35
+ sequel_db.fetch(sql, *binds).insert
36
+ end
37
+ end
38
+
39
+ def update(sql, *binds)
40
+ instrumenter.instrument('qry.update', qry: self) do
41
+ sequel_db.fetch(sql, *binds).update
42
+ end
43
+ end
44
+
45
+ def delete(sql, *binds)
46
+ instrumenter.instrument('qry.delete', qry: self) do
47
+ sequel_db.fetch(sql, *binds).delete
48
+ end
49
+ end
50
+
51
+ def run(sql, *binds)
52
+ sql = sequel_db.fetch(sql, *binds).sql unless binds.empty?
53
+
54
+ instrumenter.instrument('qry.run', qry: self) do
55
+ sequel_db.run(sql)
56
+ end
57
+ end
58
+
59
+ def insert_row(table, data = nil, extra_sql = nil)
60
+ data ||= {}
61
+
62
+ columns = data.map { |column, _| Sequel.identifier(column.to_s) }
63
+ values = data.map { |_, value| value }
64
+ placeholders = data.map { '?' }.join(', ')
65
+
66
+ sql = <<~SQL % {columns: placeholders, values: placeholders, extra: extra_sql}
67
+ insert into ? (%{columns})
68
+ values (%{values})
69
+ %{extra}
70
+ SQL
71
+
72
+ if !table.is_a?(Sequel::SQL::Identifier) && !table.is_a?(Sequel::SQL::QualifiedIdentifier)
73
+ table = Sequel.identifier(table)
74
+ end
75
+
76
+ insert(sql, table, *columns, *values)
77
+ end
78
+
79
+ def insert_rows(table, columns, values_lists, extra_sql = nil)
80
+ return if values_lists.empty?
81
+
82
+ columns = columns.map { |column| Sequel.identifier(column.to_s) }
83
+ placeholders = columns.map { '?' }.join(', ')
84
+ values = values_lists.map { "(%{placeholders})" }.join(', ') % {placeholders: placeholders}
85
+
86
+ sql = <<~SQL % {columns: placeholders, values: values, extra: extra_sql}
87
+ insert into ? (%{columns})
88
+ values %{values}
89
+ %{extra}
90
+ SQL
91
+
92
+ if !table.is_a?(Sequel::SQL::Identifier) && !table.is_a?(Sequel::SQL::QualifiedIdentifier)
93
+ table = Sequel.identifier(table)
94
+ end
95
+
96
+ insert(sql, table, *columns, *values_lists.flatten)
97
+ end
98
+
99
+ def update_rows(table, updates, where)
100
+ return if updates.empty?
101
+
102
+ binds = [Sequel.identifier(table.to_s)]
103
+
104
+ updates.each do |column, value|
105
+ binds << Sequel.identifier(column.to_s) << value
106
+ end
107
+
108
+ where.each do |column, value|
109
+ binds << Sequel.identifier(column.to_s) << value
110
+ end
111
+
112
+ sql = <<~SQL
113
+ update ?
114
+ set %{updates}
115
+ where %{where}
116
+ SQL
117
+
118
+ sql = sql % {
119
+ updates: updates.map { "? = ?" }.join(", "),
120
+ where: where.map { "? = ?" }.join(" and "),
121
+ }
122
+
123
+ update(sql, *binds)
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,77 @@
1
+ Sequel.extension :migration
2
+
3
+ module Qry
4
+ Manager = Ivo.new(:qry, :migrations_path, :schema_path, :host_whitelist) do
5
+ def current_version?
6
+ Sequel::Migrator.is_current?(qry.sequel_db, migrations_path)
7
+ end
8
+
9
+ def load_schema
10
+ ensure_valid_host
11
+
12
+ File.read(schema_path).split(';').map(&:strip).reject(&:empty?).each do |sql|
13
+ qry.run(sql)
14
+ end
15
+ end
16
+
17
+ def migrate(version: nil)
18
+ version = Integer(version) if version
19
+
20
+ if version
21
+ puts "Migrating to version #{version}"
22
+ Sequel::Migrator.run(qry.sequel_db, migrations_path, target: version)
23
+ else
24
+ puts "Migrating to latest"
25
+ Sequel::Migrator.run(qry.sequel_db, migrations_path)
26
+ end
27
+
28
+ dump_schema
29
+ end
30
+
31
+ def drop_all_tables
32
+ ensure_valid_host
33
+
34
+ fetch_table_names.each do |table_name|
35
+ qry.run('drop table ?', Sequel.identifier(table_name))
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def ensure_valid_host
42
+ unless host_whitelist.include?(qry.host)
43
+ raise "Host must be one of: #{host_whitelist.join(', ')}"
44
+ end
45
+ end
46
+
47
+ def fetch_table_names
48
+ sql = <<~SQL
49
+ select t.table_name
50
+ from information_schema.tables t
51
+ where t.table_schema = ?
52
+ SQL
53
+
54
+ qry.fetch(sql, qry.name).map(&:table_name)
55
+ end
56
+
57
+ def dump_schema
58
+ sqls = fetch_table_names.map do |table_name|
59
+ create_table_sql = qry
60
+ .fetch('show create table ?', Sequel.identifier(table_name))
61
+ .map { |row| row.public_send('Create Table') }[0]
62
+ create_table_sql_without_auto_increment = create_table_sql.gsub(/AUTO_INCREMENT=\d+\s/, '')
63
+ "#{create_table_sql_without_auto_increment};"
64
+ end
65
+
66
+ schema_info = qry.fetch('select si.version from schema_info si limit 1')[0]
67
+
68
+ sqls << "insert into schema_info (version) values (#{schema_info.version});"
69
+
70
+ schema_sql = "#{sqls.join "\n\n"}\n"
71
+
72
+ File.open(schema_path, 'w') do |file|
73
+ file.write(schema_sql)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,7 @@
1
+ module Qry
2
+ class NullInstrumenter
3
+ def instrument(*)
4
+ yield
5
+ end
6
+ end
7
+ end
data/lib/qry/rails.rb ADDED
@@ -0,0 +1,6 @@
1
+ module Qry
2
+ module Rails
3
+ end
4
+ end
5
+
6
+ Icy.require_tree('rails')
@@ -0,0 +1,9 @@
1
+ module Qry
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace Qry
5
+
6
+ config.eager_load_paths += [config.root.join('lib')]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Qry
2
+ VERSION = "1.0.0"
3
+ end
data/qry.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'qry/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'qry'
7
+ spec.version = Qry::VERSION
8
+ spec.authors = ['Austin Schneider']
9
+ spec.email = ['me@austinschneider.com']
10
+
11
+ spec.summary = 'Qry'
12
+ spec.description = 'https://github.com/austinthecoder/qry'
13
+
14
+ # spec.metadata['allowed_push_host'] = 'TODO: Set to 'http://mygemserver.com''
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 2.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.9'
27
+ spec.add_development_dependency 'sqlite3', '~> 1.3'
28
+
29
+ spec.add_dependency 'icy', '~> 0.3'
30
+ spec.add_dependency 'ivo', '~> 0.4'
31
+ spec.add_dependency 'sequel', '~> 5.20'
32
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qry
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Austin Schneider
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.9'
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'
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'
55
+ - !ruby/object:Gem::Dependency
56
+ name: icy
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.3'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: ivo
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.4'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sequel
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.20'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.20'
97
+ description: https://github.com/austinthecoder/qry
98
+ email:
99
+ - me@austinschneider.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - CODE_OF_CONDUCT.md
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/qry.rb
114
+ - lib/qry/interface.rb
115
+ - lib/qry/manager.rb
116
+ - lib/qry/null_instrumenter.rb
117
+ - lib/qry/rails.rb
118
+ - lib/qry/rails/engine.rb
119
+ - lib/qry/version.rb
120
+ - qry.gemspec
121
+ homepage:
122
+ licenses: []
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.0.3
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Qry
143
+ test_files: []