simple-sql 0.2.6 → 0.2.7

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
  SHA1:
3
- metadata.gz: 2fea0396d5b98a9ec21ea6910bf6b68bc35aab90
4
- data.tar.gz: bdfd6c5e2a4abbbc82cc86b3564de1c5649d4333
3
+ metadata.gz: cad4b0118fd0c649c4f6050f6f641245292e0b4d
4
+ data.tar.gz: 8e12a2a3b15218a02f3a2e7464aaec7ea84e39ae
5
5
  SHA512:
6
- metadata.gz: 50cd39402330417cbf4e6f81ff9ad49cc15b1bf96f3a11772e57e677803dac734e898d1b44036e299bda343a54fc9d6b606351377589ceb8908bd3d69280a046
7
- data.tar.gz: bb6482542f96b64c8b9a75c19421dbfe01953fa6cdc76e2bdee5aaabcbdea931d44a8c2cf184819b6630559d7bc30757dbec5f0c5002b00df92a7ed93943eeb0
6
+ metadata.gz: acf9fe4aacd51c3a34a34a92d45b339daa673f703d8e933f31e319057c571a3c1cae8b1d181ac8700d1953b18a8f5fd83ed57262e82415adedc662fd5701418c
7
+ data.tar.gz: 2078e081ab1d2110a1c582a1861d617a201d37ab76896ff05d8a9f5b9f01cc3630bd430a0ae070a48d8fbff473587b3aa775e52486f5f99fd9a2097a54afe11e
data/.rubocop.yml CHANGED
@@ -33,3 +33,27 @@ Style/Documentation:
33
33
 
34
34
  Style/MutableConstant:
35
35
  Enabled: false
36
+
37
+ Style/FormatStringToken:
38
+ Enabled: false
39
+
40
+ Style/Lambda:
41
+ Enabled: false
42
+
43
+ Style/SymbolArray:
44
+ Enabled: false
45
+
46
+ Style/FormatString:
47
+ Enabled: false
48
+
49
+ Style/PercentLiteralDelimiters:
50
+ Enabled: false
51
+
52
+ Lint/MissingCopEnableDirective:
53
+ Enabled: false
54
+
55
+ Style/NumericPredicate:
56
+ Enabled: false
57
+
58
+ Style/RegexpLiteral:
59
+ Enabled: false
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple-sql (0.2.6)
4
+ simple-sql (0.2.7)
5
5
  pg (~> 0.20)
6
6
  pg_array_parser (~> 0)
7
7
 
@@ -90,4 +90,4 @@ DEPENDENCIES
90
90
  simplecov (~> 0)
91
91
 
92
92
  BUNDLED WITH
93
- 1.13.6
93
+ 1.16.1
@@ -1,3 +1,8 @@
1
+ # rubocop:disable Metrics/AbcSize
2
+ # rubocop:disable Metrics/CyclomaticComplexity
3
+ # rubocop:disable Metrics/MethodLength
4
+ # rubocop:disable Metrics/PerceivedComplexity
5
+
1
6
  # private
2
7
  module Simple::SQL::Config
3
8
  extend self
@@ -33,10 +38,10 @@ module Simple::SQL::Config
33
38
 
34
39
  def database_url_from_database_yml
35
40
  abc = read_database_yml
36
- username, password, host, port, database, password = abc.values_at "username", "password", "host", "port", "database", "password"
41
+ username, password, host, port, database = abc.values_at "username", "password", "host", "port", "database"
37
42
 
38
- username_and_password = [ username, password ].compact.join(":")
39
- host_and_port = [ host, port ].compact.join(":")
43
+ username_and_password = [username, password].compact.join(":")
44
+ host_and_port = [host, port].compact.join(":")
40
45
  "postgres://#{username_and_password}@#{host_and_port}/#{database}"
41
46
  end
42
47
 
@@ -46,7 +51,7 @@ module Simple::SQL::Config
46
51
  env = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
47
52
 
48
53
  database_config[env] ||
49
- database_config["defaults"] ||
50
- raise("Invalid or missing database configuration in config/database.yml for #{env.inspect} environment")
54
+ database_config["defaults"] ||
55
+ raise("Invalid or missing database configuration in config/database.yml for #{env.inspect} environment")
51
56
  end
52
57
  end
@@ -1,3 +1,6 @@
1
+ # rubocop:disable Metrics/MethodLength
2
+ # rubocop:disable Style/IfUnlessModifier
3
+
1
4
  # private
2
5
  module Simple::SQL::Connection
3
6
  def self.new(connection)
@@ -18,7 +21,7 @@ module Simple::SQL::Connection
18
21
  extend Forwardable
19
22
  delegate %w(exec_params exec escape wait_for_notify) => :@raw_connection
20
23
 
21
- def transaction(&block)
24
+ def transaction(&_block)
22
25
  raise ArgumentError, "Implementation missing for #transaction"
23
26
  end
24
27
  end
@@ -31,9 +34,9 @@ module Simple::SQL::Connection
31
34
 
32
35
  private
33
36
 
34
- def transaction(&block)
35
- # Notes: by using "ensure" (as opposed to rescue) we are rolling back
36
- # both when an exception was raised and when a value was thrown. This
37
+ def transaction(&_block)
38
+ # Notes: by using "ensure" (as opposed to rescue) we are rolling back
39
+ # both when an exception was raised and when a value was thrown. This
37
40
  # also means we have to track whether or not to rollback. i.e. do roll
38
41
  # back when we yielded to &block but not otherwise.
39
42
  #
@@ -71,6 +74,6 @@ module Simple::SQL::Connection
71
74
  @connection = connection
72
75
  end
73
76
 
74
- delegate :transaction => :@connection
77
+ delegate [:transaction] => :@connection
75
78
  end
76
79
  end
@@ -37,7 +37,7 @@ module Simple::SQL::Decoder
37
37
  else
38
38
  # unknown value, we just return the string here.
39
39
  # STDERR.puts "unknown type: #{type.inspect}"
40
- s
40
+ s
41
41
  end
42
42
  end
43
43
  # rubocop:enable Metrics/AbcSize
@@ -1,11 +1,19 @@
1
+ # rubocop:disable Style/ClassVars
2
+ # rubocop:disable Metrics/AbcSize
3
+
1
4
  module Simple
2
5
  module SQL
3
- # Inse
4
6
  def insert(table, records)
5
7
  if records.is_a?(Hash)
6
- return insert(table, [records]).first
8
+ insert_many(table, [records]).first
9
+ else
10
+ insert_many table, records
7
11
  end
12
+ end
13
+
14
+ private
8
15
 
16
+ def insert_many(table, records)
9
17
  return [] if records.empty?
10
18
 
11
19
  inserter = Inserter.create(table_name: table.to_s, columns: records.first.keys)
@@ -32,20 +40,20 @@ module Simple
32
40
  vals = []
33
41
 
34
42
  cols += columns
35
- vals += columns.each_with_index.map { |_, idx| "$#{idx+1}" }
43
+ vals += columns.each_with_index.map { |_, idx| "$#{idx + 1}" }
36
44
 
37
45
  timestamp_columns = timestamp_columns_in_table(table_name) - columns.map(&:to_s)
38
46
 
39
47
  cols += timestamp_columns
40
48
  vals += timestamp_columns.map { "now()" }
41
49
 
42
- @sql = "INSERT INTO #{table_name} (#{cols.join(",")}) VALUES(#{vals.join(",")}) RETURNING id"
50
+ @sql = "INSERT INTO #{table_name} (#{cols.join(',')}) VALUES(#{vals.join(',')}) RETURNING id"
43
51
  end
44
52
 
45
53
  # timestamp_columns are columns that will be set to the current time when
46
54
  # inserting a record. This includes:
47
55
  #
48
- # - inserted_at (for Ecto)
56
+ # - inserted_at (for Ecto)
49
57
  # - created_at (for ActiveRecord)
50
58
  # - updated_at (for Ecto and ActiveRecord)
51
59
  def timestamp_columns_in_table(table_name)
@@ -53,7 +61,7 @@ module Simple
53
61
  columns_for_table & %w(inserted_at created_at updated_at)
54
62
  end
55
63
 
56
- def insert(records: records)
64
+ def insert(records:)
57
65
  SQL.transaction do
58
66
  records.map do |record|
59
67
  SQL.ask @sql, *record.values_at(*@columns)
@@ -1,17 +1,20 @@
1
+ # rubocop:disable Metrics/AbcSize
2
+ # rubocop:disable Metrics/LineLength
3
+
1
4
  module Simple
2
5
  module SQL
3
6
  module Logging
4
7
  extend self
5
8
 
6
- def yield_logged(sql, *args, &block)
9
+ def yield_logged(sql, *args, &_block)
7
10
  r0 = Time.now
8
11
  rv = yield
9
12
  realtime = Time.now - r0
10
- ::Simple::SQL.logger.debug "[sql] %.3f secs: %s" % [ realtime, format_query(sql, *args) ]
13
+ ::Simple::SQL.logger.debug "[sql] %.3f secs: %s" % [realtime, format_query(sql, *args)]
11
14
  rv
12
- rescue => e
15
+ rescue StandardError => e
13
16
  realtime = Time.now - r0
14
- ::Simple::SQL.logger.warn "[sql] %.3f secs: %s:\n\tfailed with error %s" % [ realtime, format_query(sql, *args), e.message ]
17
+ ::Simple::SQL.logger.warn "[sql] %.3f secs: %s:\n\tfailed with error %s" % [realtime, format_query(sql, *args), e.message]
15
18
  raise
16
19
  end
17
20
 
@@ -26,4 +29,3 @@ module Simple
26
29
  end
27
30
  end
28
31
  end
29
-
@@ -1,3 +1,5 @@
1
+ # rubocop:disable Metrics/MethodLength
2
+
1
3
  module Simple
2
4
  module SQL
3
5
  module Reflection
@@ -7,20 +9,17 @@ module Simple
7
9
  delegate [:ask, :all, :records, :record] => ::Simple::SQL
8
10
 
9
11
  def tables(schema: "public")
10
- if schema == "public"
11
- sql = <<~SQL
12
- SELECT table_name AS name, *
13
- FROM information_schema.tables
14
- WHERE table_schema=$1
15
- SQL
16
- else
17
- sql = <<~SQL
18
- SELECT table_schema || '.' || table_name AS name, *
19
- FROM information_schema.tables
20
- WHERE table_schema=$1
12
+ select = if schema == "public"
13
+ "table_name AS name, *"
14
+ else
15
+ "table_schema || '.' || table_name AS name, *"
16
+ end
17
+
18
+ records = ::Simple::SQL.records <<~SQL, schema
19
+ SELECT #{select}
20
+ FROM information_schema.tables
21
+ WHERE table_schema=$1
21
22
  SQL
22
- end
23
- records = ::Simple::SQL.records sql, schema
24
23
  records_by_attr(records, :name)
25
24
  end
26
25
 
@@ -42,9 +41,9 @@ module Simple
42
41
  def parse_table_name(table_name)
43
42
  p1, p2 = table_name.split(".", 2)
44
43
  if p2
45
- [ p1, p2 ]
44
+ [p1, p2]
46
45
  else
47
- [ "public", p1 ]
46
+ ["public", p1]
48
47
  end
49
48
  end
50
49
 
@@ -1,5 +1,5 @@
1
1
  module Simple
2
2
  module SQL
3
- VERSION = "0.2.6"
3
+ VERSION = "0.2.7"
4
4
  end
5
5
  end
data/lib/simple/sql.rb CHANGED
@@ -16,7 +16,24 @@ module Simple
16
16
  extend self
17
17
 
18
18
  attr_accessor :logger
19
- self.logger = Logger.new(STDERR)
19
+
20
+ def logger
21
+ @logger ||= default_logger
22
+ end
23
+
24
+ def logger=(logger)
25
+ @logger = logger
26
+ end
27
+
28
+ def default_logger
29
+ if defined?(ActiveRecord)
30
+ ActiveRecord::Base.logger
31
+ else
32
+ logger = Logger.new(STDERR)
33
+ logger.level = Logger::INFO
34
+ logger
35
+ end
36
+ end
20
37
 
21
38
  # execute one or more sql statements. This method does not allow to pass in
22
39
  # arguments - since the pg client does not support this - but it allows to
@@ -127,16 +144,16 @@ module Simple
127
144
  @connector = connector
128
145
  end
129
146
 
130
- self.connector = lambda {
147
+ self.connector = lambda {
131
148
  connect_to_active_record
132
149
  }
133
150
 
134
151
  def connect_to_active_record
135
152
  return Connection.new(ActiveRecord::Base.connection) if defined?(ActiveRecord)
136
153
 
137
- STDERR.puts <<-SQL
138
- Simple::SQL works out of the box with ActiveRecord-based postgres connections, reusing the current connection.
139
- To use without ActiveRecord you must connect to a database via Simple::SQL.connect!.
154
+ STDERR.puts <<~SQL
155
+ Simple::SQL works out of the box with ActiveRecord-based postgres connections, reusing the current connection.
156
+ To use without ActiveRecord you must connect to a database via Simple::SQL.connect!.
140
157
  SQL
141
158
 
142
159
  raise ArgumentError, "simple-sql: missing connection"
data/lib/simple-sql.rb CHANGED
@@ -1 +1,3 @@
1
+ # rubocop:disable Naming/FileName
2
+
1
3
  require "simple/sql"
@@ -1,8 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe "Simple::SQL.ask" do
4
- USER_COUNT = 2
5
-
6
4
  def expects(expected_result, sql, *args)
7
5
  expect(SQL.ask(sql, *args)).to eq(expected_result)
8
6
  end
@@ -1,9 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe "Simple::SQL.insert" do
4
- USER_COUNT = 2
5
- SQL = SQL
6
-
7
4
  def expects(expected_result, sql, *args)
8
5
  expect(SQL.record(sql, *args)).to eq(expected_result)
9
6
  end
@@ -1,8 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe "Simple::SQL.record" do
4
- USER_COUNT = 2
5
-
6
4
  def expects(expected_result, sql, *args)
7
5
  expect(SQL.record(sql, *args)).to eq(expected_result)
8
6
  end
data/spec/spec_helper.rb CHANGED
@@ -13,6 +13,9 @@ Dir.glob("./spec/support/**/*.rb").sort.each { |path| load path }
13
13
  require "simple/sql"
14
14
 
15
15
  SQL = Simple::SQL
16
+ USER_COUNT = 2
17
+
18
+ ActiveRecord::Base.logger.level = Logger::INFO
16
19
 
17
20
  RSpec.configure do |config|
18
21
  config.run_all_when_everything_filtered = true
@@ -33,6 +33,6 @@ ActiveRecord::Schema.define do
33
33
  t.hstore :meta_data
34
34
  t.column :access_level, :access_level
35
35
 
36
- t.timestamps
36
+ t.timestamps null: true
37
37
  end
38
38
  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.2.6
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - radiospiel