simple-sql 0.4.9 → 0.4.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/Gemfile.lock +3 -1
- data/lib/simple/sql.rb +15 -13
- data/lib/simple/sql/config.rb +2 -5
- data/lib/simple/sql/connection_adapter.rb +35 -32
- data/lib/simple/sql/helpers.rb +32 -0
- data/lib/simple/sql/{decoder.rb → helpers/decoder.rb} +35 -63
- data/lib/simple/sql/{encoder.rb → helpers/encoder.rb} +1 -1
- data/lib/simple/sql/helpers/row_converter.rb +34 -0
- data/lib/simple/sql/reflection.rb +0 -2
- data/lib/simple/sql/result.rb +27 -0
- data/lib/simple/sql/result/association_loader.rb +161 -0
- data/lib/simple/sql/result/records.rb +72 -0
- data/lib/simple/sql/result/rows.rb +30 -0
- data/lib/simple/sql/scope.rb +41 -18
- data/lib/simple/sql/scope/order.rb +15 -3
- data/lib/simple/sql/simple_transactions.rb +0 -1
- data/lib/simple/sql/version.rb +1 -1
- data/simple-sql.gemspec +1 -0
- data/spec/simple/sql_all_into_spec.rb +68 -9
- data/spec/simple/sql_associations_spec.rb +142 -0
- data/spec/simple/sql_scope_spec.rb +15 -0
- data/spec/spec_helper.rb +2 -2
- data/spec/support/001_database.rb +25 -19
- data/spec/support/003_factories.rb +4 -0
- metadata +26 -8
- data/spec/simple/sql_ask_into_spec.rb +0 -27
- data/spec/simple/sql_ask_into_struct_spec.rb +0 -17
@@ -203,6 +203,21 @@ describe "Simple::SQL::Scope" do
|
|
203
203
|
end
|
204
204
|
end
|
205
205
|
|
206
|
+
context "Building with Hash" do
|
207
|
+
it "runs with SQL.ask" do
|
208
|
+
scope = SQL::Scope.new table: "users", select: "1, 2", where: "id >= 0"
|
209
|
+
expect(SQL.all(scope)).to eq([[1,2], [1,2]])
|
210
|
+
|
211
|
+
scope = SQL::Scope.new table: "users", select: [1,3,4], where: "id >= 0"
|
212
|
+
expect(SQL.all(scope)).to eq([[1,3,4], [1,3,4]])
|
213
|
+
end
|
214
|
+
|
215
|
+
it "raises an error with missing or invalid attributes" do
|
216
|
+
expect { SQL::Scope.new table: "users", limit: 1 }.to raise_error(ArgumentError)
|
217
|
+
expect { SQL::Scope.new select: "*" }.to raise_error(ArgumentError)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
206
221
|
context "describe pagination" do
|
207
222
|
let(:scope) do
|
208
223
|
scope = SQL::Scope.new "SELECT 1, 2 FROM users"
|
data/spec/spec_helper.rb
CHANGED
@@ -6,6 +6,7 @@ end
|
|
6
6
|
ENV["RACK_ENV"] = "test"
|
7
7
|
ENV["RAILS_ENV"] = "test"
|
8
8
|
|
9
|
+
require "byebug"
|
9
10
|
require "rspec"
|
10
11
|
require "awesome_print"
|
11
12
|
Dir.glob("./spec/support/**/*.rb").sort.each { |path| load path }
|
@@ -30,8 +31,7 @@ RSpec.configure do |config|
|
|
30
31
|
config.example_status_persistence_file_path = ".rspec.data"
|
31
32
|
|
32
33
|
config.around(:each) do |example|
|
33
|
-
Simple::SQL.ask "
|
34
|
-
Simple::SQL.ask "DELETE FROM unique_users"
|
34
|
+
Simple::SQL.ask "TRUNCATE TABLE users, unique_users, organizations RESTART IDENTITY CASCADE"
|
35
35
|
example.run
|
36
36
|
end
|
37
37
|
end
|
@@ -12,11 +12,11 @@ ActiveRecord::Base.logger = Logger.new("log/test.log")
|
|
12
12
|
ActiveRecord::Schema.define do
|
13
13
|
self.verbose = false
|
14
14
|
|
15
|
-
execute
|
16
|
-
|
17
|
-
|
15
|
+
execute <<~SQL
|
16
|
+
DROP SCHEMA public CASCADE;
|
17
|
+
CREATE SCHEMA public;
|
18
|
+
CREATE EXTENSION IF NOT EXISTS hstore;
|
18
19
|
|
19
|
-
execute <<-SQL
|
20
20
|
DROP TYPE IF EXISTS access_level;
|
21
21
|
CREATE TYPE access_level AS ENUM (
|
22
22
|
'private',
|
@@ -24,24 +24,30 @@ ActiveRecord::Schema.define do
|
|
24
24
|
'viewable',
|
25
25
|
'accessible'
|
26
26
|
);
|
27
|
-
SQL
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
t.jsonb :metadata
|
34
|
-
t.column :access_level, :access_level
|
28
|
+
CREATE TABLE organizations (
|
29
|
+
id SERIAL PRIMARY KEY,
|
30
|
+
name VARCHAR
|
31
|
+
);
|
35
32
|
|
36
|
-
|
37
|
-
|
33
|
+
CREATE TABLE users (
|
34
|
+
id SERIAL PRIMARY KEY,
|
35
|
+
organization_id INTEGER REFERENCES organizations (id),
|
36
|
+
role_id INTEGER,
|
37
|
+
first_name VARCHAR,
|
38
|
+
last_name VARCHAR,
|
39
|
+
metadata JSONB,
|
40
|
+
access_level access_level,
|
41
|
+
created_at timestamp,
|
42
|
+
updated_at timestamp
|
43
|
+
);
|
38
44
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
45
|
+
CREATE TABLE unique_users (
|
46
|
+
id SERIAL PRIMARY KEY,
|
47
|
+
first_name VARCHAR,
|
48
|
+
last_name VARCHAR
|
49
|
+
);
|
43
50
|
|
44
|
-
|
45
|
-
CREATE UNIQUE INDEX unique_users_ix1 ON unique_users(first_name, last_name)
|
51
|
+
CREATE UNIQUE INDEX unique_users_ix1 ON unique_users(first_name, last_name)
|
46
52
|
SQL
|
47
53
|
end
|
@@ -21,6 +21,10 @@ def table_attrs(table)
|
|
21
21
|
first_name: sequence("First {{sequence}}"),
|
22
22
|
last_name: sequence("Last {{sequence}}")
|
23
23
|
}.freeze
|
24
|
+
when :organization
|
25
|
+
{
|
26
|
+
name: sequence("organization {{sequence}}")
|
27
|
+
}.freeze
|
24
28
|
else
|
25
29
|
raise ArgumentError, "Invalid table for factory: #{table.inspect}"
|
26
30
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-sql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- radiospiel
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-08-
|
12
|
+
date: 2018-08-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pg_array_parser
|
@@ -39,6 +39,20 @@ dependencies:
|
|
39
39
|
- - "~>"
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '0.20'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: expectation
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '1'
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '1'
|
42
56
|
- !ruby/object:Gem::Dependency
|
43
57
|
name: activerecord
|
44
58
|
requirement: !ruby/object:Gem::Requirement
|
@@ -171,12 +185,18 @@ files:
|
|
171
185
|
- lib/simple/sql/config.rb
|
172
186
|
- lib/simple/sql/connection.rb
|
173
187
|
- lib/simple/sql/connection_adapter.rb
|
174
|
-
- lib/simple/sql/decoder.rb
|
175
188
|
- lib/simple/sql/duplicate.rb
|
176
|
-
- lib/simple/sql/
|
189
|
+
- lib/simple/sql/helpers.rb
|
190
|
+
- lib/simple/sql/helpers/decoder.rb
|
191
|
+
- lib/simple/sql/helpers/encoder.rb
|
192
|
+
- lib/simple/sql/helpers/row_converter.rb
|
177
193
|
- lib/simple/sql/insert.rb
|
178
194
|
- lib/simple/sql/logging.rb
|
179
195
|
- lib/simple/sql/reflection.rb
|
196
|
+
- lib/simple/sql/result.rb
|
197
|
+
- lib/simple/sql/result/association_loader.rb
|
198
|
+
- lib/simple/sql/result/records.rb
|
199
|
+
- lib/simple/sql/result/rows.rb
|
180
200
|
- lib/simple/sql/scope.rb
|
181
201
|
- lib/simple/sql/scope/filters.rb
|
182
202
|
- lib/simple/sql/scope/order.rb
|
@@ -189,9 +209,8 @@ files:
|
|
189
209
|
- spec/manual/threadtest.rb
|
190
210
|
- spec/simple/sql/version_spec.rb
|
191
211
|
- spec/simple/sql_all_into_spec.rb
|
192
|
-
- spec/simple/sql_ask_into_spec.rb
|
193
|
-
- spec/simple/sql_ask_into_struct_spec.rb
|
194
212
|
- spec/simple/sql_ask_spec.rb
|
213
|
+
- spec/simple/sql_associations_spec.rb
|
195
214
|
- spec/simple/sql_config_spec.rb
|
196
215
|
- spec/simple/sql_conversion_spec.rb
|
197
216
|
- spec/simple/sql_duplicate_spec.rb
|
@@ -233,9 +252,8 @@ test_files:
|
|
233
252
|
- spec/manual/threadtest.rb
|
234
253
|
- spec/simple/sql/version_spec.rb
|
235
254
|
- spec/simple/sql_all_into_spec.rb
|
236
|
-
- spec/simple/sql_ask_into_spec.rb
|
237
|
-
- spec/simple/sql_ask_into_struct_spec.rb
|
238
255
|
- spec/simple/sql_ask_spec.rb
|
256
|
+
- spec/simple/sql_associations_spec.rb
|
239
257
|
- spec/simple/sql_config_spec.rb
|
240
258
|
- spec/simple/sql_conversion_spec.rb
|
241
259
|
- spec/simple/sql_duplicate_spec.rb
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe "Simple::SQL.ask into: argument" do
|
4
|
-
let!(:users) { 1.upto(USER_COUNT).map { create(:user) } }
|
5
|
-
|
6
|
-
it "calls the database" do
|
7
|
-
r = SQL.ask("SELECT COUNT(*) AS count FROM users", into: Hash)
|
8
|
-
expect(r).to eq({count: 2})
|
9
|
-
end
|
10
|
-
|
11
|
-
it "returns nil when there is no match" do
|
12
|
-
r = SQL.ask("SELECT * FROM users WHERE FALSE", into: Hash)
|
13
|
-
expect(r).to be_nil
|
14
|
-
end
|
15
|
-
|
16
|
-
it "returns a OpenStruct with into: OpenStruct" do
|
17
|
-
r = SQL.ask("SELECT COUNT(*) AS count FROM users", into: OpenStruct)
|
18
|
-
expect(r).to be_a(OpenStruct)
|
19
|
-
expect(r).to eq(OpenStruct.new(count: 2))
|
20
|
-
end
|
21
|
-
|
22
|
-
it "supports the into: option even with parameters" do
|
23
|
-
r = SQL.ask("SELECT $1::integer AS count FROM users", 2, into: OpenStruct)
|
24
|
-
expect(r).to be_a(OpenStruct)
|
25
|
-
expect(r).to eq(OpenStruct.new(count: 2))
|
26
|
-
end
|
27
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe "Simple::SQL.ask into: :struct" do
|
4
|
-
let!(:users) { 1.upto(USER_COUNT).map { create(:user) } }
|
5
|
-
|
6
|
-
it "calls the database" do
|
7
|
-
r = SQL.ask("SELECT COUNT(*) AS count FROM users", into: :struct)
|
8
|
-
expect(r.count).to eq(2)
|
9
|
-
expect(r.class.members).to eq([:count])
|
10
|
-
end
|
11
|
-
|
12
|
-
it "reuses the struct" do
|
13
|
-
r1 = SQL.ask("SELECT COUNT(*) AS count FROM users", into: :struct)
|
14
|
-
r2 = SQL.ask("SELECT COUNT(*) AS count FROM users", into: :struct)
|
15
|
-
expect(r1.class.object_id).to eq(r2.class.object_id)
|
16
|
-
end
|
17
|
-
end
|