simple-sql 0.4.21 → 0.4.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 333ad2e3a74fd8e625a0704efd36e444f5f7798d2dd9c47a5a71dbf0db67acb9
4
- data.tar.gz: 12b24d2fc0a760189dfc47a45de4a4a1ce1ed84419301eb5162938ef3db7126d
3
+ metadata.gz: 58caff3c654cc1f068e15d93f2d356fde5598e159ddd2589aeb5e5588a1e8c16
4
+ data.tar.gz: d9d06f994b53824b937872c548601c025a93060c917602a73e99d8a8ab24dbdd
5
5
  SHA512:
6
- metadata.gz: f30c396c7b81e5e754e9f19335f6083a515a0892f1aea65f528c7e075d2714164fc7c325ae5c478d04449b28e8fce3cbf96bc000d895da29dd3c304779c38a4f
7
- data.tar.gz: 30448b8eea1a67ed03725d25e0b242e18cb866a82031414a94d4f9751a3f9875d6928eb2ed9f728f52dd65f3268f988661710e6cc204afdeca89c303ae492d23
6
+ metadata.gz: af6166f4600345b8151092e3f2198f89defb2d64365a3e0d616b2da86ef87a73ba7b6ed360e3c4dd4371569afa70e210ef72bdbbabd25e5b87f685cc0d7c99ef
7
+ data.tar.gz: e04ef18bd368c7e2e404daf84767204335de2f85ee22594d56bd37415b085e6dc78417cd46d5e085e134493d2cb0d09318da8665a8695a1333a6e89a7c24af7c
data/.gitignore CHANGED
@@ -6,4 +6,4 @@ log/test.log
6
6
  Gemfile.lock
7
7
  .rake_t_cache
8
8
  .rspec.status
9
- simple-sql-0.4.10.gem
9
+ .DS_Store
@@ -4,12 +4,6 @@ require "time"
4
4
  module Simple::SQL::Helpers::Decoder
5
5
  extend self
6
6
 
7
- def parse_timestamp(s)
8
- r = ::Time.parse(s)
9
- return r if r.utc_offset == 0
10
- Time.gm(r.year, r.mon, r.day, r.hour, r.min, r.sec, r.usec)
11
- end
12
-
13
7
  # rubocop:disable Metrics/AbcSize
14
8
  # rubocop:disable Metrics/CyclomaticComplexity
15
9
  def decode_value(type, s)
@@ -23,7 +17,8 @@ module Simple::SQL::Helpers::Decoder
23
17
  when :'integer[]' then s.scan(/-?\d+/).map { |part| Integer(part) }
24
18
  when :"character varying[]" then parse_pg_array(s)
25
19
  when :"text[]" then parse_pg_array(s)
26
- when :"timestamp without time zone" then parse_timestamp(s)
20
+ when :"timestamp without time zone" then ::Time.parse(s)
21
+ when :"timestamp with time zone" then ::Time.parse(s)
27
22
  when :hstore then HStore.parse(s)
28
23
  when :json then ::JSON.parse(s)
29
24
  when :jsonb then ::JSON.parse(s)
@@ -7,20 +7,20 @@ module Simple
7
7
  # - records - a single hash of attributes or an array of hashes of attributes
8
8
  # - on_conflict - uses a postgres ON CONFLICT clause to ignore insert conflicts if true
9
9
  #
10
- def insert(table, records, on_conflict: nil)
10
+ def insert(table, records, on_conflict: nil, into: nil)
11
11
  if records.is_a?(Hash)
12
- insert_many(table, [records], on_conflict).first
12
+ insert_many(table, [records], on_conflict, into).first
13
13
  else
14
- insert_many(table, records, on_conflict)
14
+ insert_many(table, records, on_conflict, into)
15
15
  end
16
16
  end
17
17
 
18
18
  private
19
19
 
20
- def insert_many(table, records, on_conflict)
20
+ def insert_many(table, records, on_conflict, into)
21
21
  return [] if records.empty?
22
22
 
23
- inserter = Inserter.create(table_name: table.to_s, columns: records.first.keys, on_conflict: on_conflict)
23
+ inserter = Inserter.create(table_name: table.to_s, columns: records.first.keys, on_conflict: on_conflict, into: into)
24
24
  inserter.insert(records: records)
25
25
  end
26
26
 
@@ -29,16 +29,19 @@ module Simple
29
29
 
30
30
  @@inserters = {}
31
31
 
32
- def self.create(table_name:, columns:, on_conflict:)
33
- @@inserters[[table_name, columns, on_conflict]] ||= new(table_name: table_name, columns: columns, on_conflict: on_conflict)
32
+ def self.create(table_name:, columns:, on_conflict:, into:)
33
+ expect! on_conflict => CONFICT_HANDLING.keys
34
+
35
+ @@inserters[[table_name, columns, on_conflict, into]] ||= new(table_name: table_name, columns: columns, on_conflict: on_conflict, into: into)
34
36
  end
35
37
 
36
38
  #
37
39
  # - table_name - the name of the table
38
40
  # - columns - name of columns, as Array[String] or Array[Symbol]
39
41
  #
40
- def initialize(table_name:, columns:, on_conflict:)
42
+ def initialize(table_name:, columns:, on_conflict:, into:)
41
43
  @columns = columns
44
+ @into = into
42
45
 
43
46
  cols = []
44
47
  vals = []
@@ -51,7 +54,9 @@ module Simple
51
54
  cols += timestamp_columns
52
55
  vals += timestamp_columns.map { "now()" }
53
56
 
54
- @sql = "INSERT INTO #{table_name} (#{cols.join(',')}) VALUES(#{vals.join(',')}) #{confict_handling(on_conflict)} RETURNING id"
57
+ returning = into ? '*' : "id"
58
+
59
+ @sql = "INSERT INTO #{table_name} (#{cols.join(',')}) VALUES(#{vals.join(',')}) #{confict_handling(on_conflict)} RETURNING #{returning}"
55
60
  end
56
61
 
57
62
  CONFICT_HANDLING = {
@@ -69,7 +74,7 @@ module Simple
69
74
  def insert(records:)
70
75
  SQL.transaction do
71
76
  records.map do |record|
72
- SQL.ask @sql, *record.values_at(*@columns)
77
+ SQL.ask @sql, *record.values_at(*@columns), into: @into
73
78
  end
74
79
  end
75
80
  end
@@ -3,6 +3,8 @@
3
3
  # rubocop:disable Metrics/ParameterLists
4
4
  # rubocop:disable Style/GuardClause
5
5
 
6
+ require "active_support/core_ext/string/inflections"
7
+
6
8
  #
7
9
  # This module implements a pretty generic AssociationLoader.
8
10
  #
@@ -52,7 +54,7 @@ module ::Simple::SQL::Result::AssociationLoader # :nodoc:
52
54
 
53
55
  sql = <<~SQL
54
56
  WITH foreign_keys AS(
55
- SELECT
57
+ SELECT DISTINCT
56
58
  tc.table_schema || '.' || tc.table_name AS belonging_table,
57
59
  kcu.column_name AS belonging_column,
58
60
  ccu.table_schema || '.' || ccu.table_name AS having_table,
@@ -1,5 +1,5 @@
1
1
  module Simple
2
2
  module SQL
3
- VERSION = "0.4.21"
3
+ VERSION = "0.4.22"
4
4
  end
5
5
  end
data/scripts/watch CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/bin/bash
2
- watchr lib,spec 'rspec --only-failures ; [[ $? == 2 ]] && rspec'
2
+ watchr lib,spec rspec $@
File without changes
@@ -3,18 +3,32 @@ require "spec_helper"
3
3
  describe "Simple::SQL.insert" do
4
4
  let!(:users) { 1.upto(USER_COUNT).map { create(:user) } }
5
5
 
6
- it "inserts a single user" do
7
- initial_ids = SQL.all("SELECT id FROM users")
8
-
9
- id = SQL.insert :users, first_name: "foo", last_name: "bar"
10
- expect(id).to be_a(Integer)
11
- expect(initial_ids).not_to include(id)
12
- expect(SQL.ask("SELECT count(*) FROM users")).to eq(USER_COUNT+1)
13
-
14
- user = SQL.ask("SELECT * FROM users WHERE id=$1", id, into: OpenStruct)
15
- expect(user.first_name).to eq("foo")
16
- expect(user.last_name).to eq("bar")
17
- expect(user.created_at).to be_a(Time)
6
+ context "when inserting a user" do
7
+ let!(:initial_ids) { SQL.all("SELECT id FROM users") }
8
+
9
+ it "inserts a single user" do
10
+ id = SQL.insert :users, first_name: "foo", last_name: "bar"
11
+ expect(id).to be_a(Integer)
12
+ expect(initial_ids).not_to include(id)
13
+ expect(SQL.ask("SELECT count(*) FROM users")).to eq(USER_COUNT+1)
14
+
15
+ user = SQL.ask("SELECT * FROM users WHERE id=$1", id, into: OpenStruct)
16
+ expect(user.first_name).to eq("foo")
17
+ expect(user.last_name).to eq("bar")
18
+ expect(user.created_at).to be_a(Time)
19
+ end
20
+
21
+ it "returns the id" do
22
+ id = SQL.insert :users, first_name: "foo", last_name: "bar"
23
+ expect(id).to be_a(Integer)
24
+ expect(initial_ids).not_to include(id)
25
+ end
26
+
27
+ it "returns the record as a Hash" do
28
+ rec = SQL.insert :users, { first_name: "foo", last_name: "bar" }, into: Hash
29
+ expect(rec).to be_a(Hash)
30
+ expect(rec).to include(first_name: "foo", last_name: "bar")
31
+ end
18
32
  end
19
33
 
20
34
  describe 'confict handling' do
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.21
4
+ version: 0.4.22
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-10-07 00:00:00.000000000 Z
12
+ date: 2018-11-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pg_array_parser
@@ -206,17 +206,17 @@ files:
206
206
  - scripts/watch
207
207
  - simple-sql.gemspec
208
208
  - spec/manual/threadtest.rb
209
+ - spec/simple/sql/all_into_spec.rb
210
+ - spec/simple/sql/ask_spec.rb
211
+ - spec/simple/sql/associations_spec.rb
212
+ - spec/simple/sql/config_spec.rb
213
+ - spec/simple/sql/conversion_spec.rb
214
+ - spec/simple/sql/duplicate_spec.rb
215
+ - spec/simple/sql/duplicate_unique_spec.rb
216
+ - spec/simple/sql/insert_spec.rb
217
+ - spec/simple/sql/reflection_spec.rb
218
+ - spec/simple/sql/scope_spec.rb
209
219
  - spec/simple/sql/version_spec.rb
210
- - spec/simple/sql_all_into_spec.rb
211
- - spec/simple/sql_ask_spec.rb
212
- - spec/simple/sql_associations_spec.rb
213
- - spec/simple/sql_config_spec.rb
214
- - spec/simple/sql_conversion_spec.rb
215
- - spec/simple/sql_duplicate_spec.rb
216
- - spec/simple/sql_duplicate_unique_spec.rb
217
- - spec/simple/sql_insert_spec.rb
218
- - spec/simple/sql_reflection_spec.rb
219
- - spec/simple/sql_scope_spec.rb
220
220
  - spec/spec_helper.rb
221
221
  - spec/support/001_database.rb
222
222
  - spec/support/002_database_cleaner.rb
@@ -249,17 +249,17 @@ specification_version: 4
249
249
  summary: SQL with a simple interface
250
250
  test_files:
251
251
  - spec/manual/threadtest.rb
252
+ - spec/simple/sql/all_into_spec.rb
253
+ - spec/simple/sql/ask_spec.rb
254
+ - spec/simple/sql/associations_spec.rb
255
+ - spec/simple/sql/config_spec.rb
256
+ - spec/simple/sql/conversion_spec.rb
257
+ - spec/simple/sql/duplicate_spec.rb
258
+ - spec/simple/sql/duplicate_unique_spec.rb
259
+ - spec/simple/sql/insert_spec.rb
260
+ - spec/simple/sql/reflection_spec.rb
261
+ - spec/simple/sql/scope_spec.rb
252
262
  - spec/simple/sql/version_spec.rb
253
- - spec/simple/sql_all_into_spec.rb
254
- - spec/simple/sql_ask_spec.rb
255
- - spec/simple/sql_associations_spec.rb
256
- - spec/simple/sql_config_spec.rb
257
- - spec/simple/sql_conversion_spec.rb
258
- - spec/simple/sql_duplicate_spec.rb
259
- - spec/simple/sql_duplicate_unique_spec.rb
260
- - spec/simple/sql_insert_spec.rb
261
- - spec/simple/sql_reflection_spec.rb
262
- - spec/simple/sql_scope_spec.rb
263
263
  - spec/spec_helper.rb
264
264
  - spec/support/001_database.rb
265
265
  - spec/support/002_database_cleaner.rb