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 +4 -4
- data/.rubocop.yml +24 -0
- data/Gemfile.lock +2 -2
- data/lib/simple/sql/config.rb +10 -5
- data/lib/simple/sql/connection.rb +8 -5
- data/lib/simple/sql/decoder.rb +1 -1
- data/lib/simple/sql/insert.rb +14 -6
- data/lib/simple/sql/logging.rb +7 -5
- data/lib/simple/sql/reflection.rb +14 -15
- data/lib/simple/sql/version.rb +1 -1
- data/lib/simple/sql.rb +22 -5
- data/lib/simple-sql.rb +2 -0
- data/spec/simple/sql_ask_spec.rb +0 -2
- data/spec/simple/sql_insert_spec.rb +0 -3
- data/spec/simple/sql_record_spec.rb +0 -2
- data/spec/spec_helper.rb +3 -0
- data/spec/support/001_database.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cad4b0118fd0c649c4f6050f6f641245292e0b4d
|
4
|
+
data.tar.gz: 8e12a2a3b15218a02f3a2e7464aaec7ea84e39ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
data/lib/simple/sql/config.rb
CHANGED
@@ -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
|
41
|
+
username, password, host, port, database = abc.values_at "username", "password", "host", "port", "database"
|
37
42
|
|
38
|
-
username_and_password = [
|
39
|
-
host_and_port = [
|
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
|
-
|
50
|
-
|
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(&
|
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(&
|
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
|
data/lib/simple/sql/decoder.rb
CHANGED
data/lib/simple/sql/insert.rb
CHANGED
@@ -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
|
-
|
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(
|
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:
|
64
|
+
def insert(records:)
|
57
65
|
SQL.transaction do
|
58
66
|
records.map do |record|
|
59
67
|
SQL.ask @sql, *record.values_at(*@columns)
|
data/lib/simple/sql/logging.rb
CHANGED
@@ -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, &
|
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" % [
|
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" % [
|
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
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
[
|
44
|
+
[p1, p2]
|
46
45
|
else
|
47
|
-
[
|
46
|
+
["public", p1]
|
48
47
|
end
|
49
48
|
end
|
50
49
|
|
data/lib/simple/sql/version.rb
CHANGED
data/lib/simple/sql.rb
CHANGED
@@ -16,7 +16,24 @@ module Simple
|
|
16
16
|
extend self
|
17
17
|
|
18
18
|
attr_accessor :logger
|
19
|
-
|
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
|
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
data/spec/simple/sql_ask_spec.rb
CHANGED
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
|