simple-sql 0.5.36 → 0.9.0
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/.gitignore +1 -0
- data/.rubocop.yml +6 -1
- data/.ruby-version +1 -1
- data/Gemfile +5 -1
- data/Makefile +4 -12
- data/README.md +188 -86
- data/VERSION +1 -1
- data/bin/db_restore +7 -1
- data/bin/pg +7 -1
- data/config/database.yml +2 -2
- data/lib/simple/sql/config.rb +7 -2
- data/lib/simple/sql/connection/base.rb +1 -0
- data/lib/simple/sql/connection/insert.rb +3 -2
- data/lib/simple/sql/connection/reflection.rb +2 -0
- data/lib/simple/sql/connection/scope/count_by_groups.rb +91 -33
- data/lib/simple/sql/connection/scope/search.rb +2 -1
- data/lib/simple/sql/connection/scope/where.rb +3 -3
- data/lib/simple/sql/connection.rb +1 -0
- data/lib/simple/sql/helpers/decoder.rb +10 -2
- data/lib/simple/sql/helpers/row_converter.rb +3 -3
- data/lib/simple/sql/monkey_patches.rb +4 -0
- data/lib/simple/sql/result/association_loader.rb +3 -3
- data/lib/simple/sql/result.rb +1 -0
- data/lib/simple/sql/version.rb +2 -0
- data/scripts/integration_tests +49 -0
- data/simple-sql.gemspec +6 -16
- data/spec/simple/sql/config_spec.rb +1 -1
- data/spec/simple/sql/count_by_groups_spec.rb +37 -18
- data/spec/simple/sql/insert_spec.rb +2 -2
- data/spec/simple/sql/scope_spec.rb +8 -8
- data/spec/simple/sql/version_spec.rb +1 -1
- data/spec/support/001_database.rb +11 -2
- metadata +15 -84
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 75d235ac454cec6f58662af61d97c7fddf980da80c8bb6f9369db3f0e28e5c68
|
4
|
+
data.tar.gz: 6f047d1c86fdc3308d0b4f32616640d9c083c9bcab2360539ecf16d940bbf1da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7e366a2dd129d99b3ca48fb9b1bd49e37f7bb403f97b7c2fcc87c7f305417fc23ba333e76bbfa457fe57e27bf2df707c2214adf75382ae280cdde9f9c155b34
|
7
|
+
data.tar.gz: 07ed66bd39a8c7db53807ad0f54bab797d6a72d6ee63f38a011e5da753ba170ff865fb00705eee82835d53b8fbdd5c97cc494926f2944ec5089972db4897d309
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
AllCops:
|
2
|
-
TargetRubyVersion: 2
|
2
|
+
TargetRubyVersion: 3.2
|
3
|
+
NewCops: disable
|
3
4
|
Exclude:
|
4
5
|
- 'spec/**/*'
|
5
6
|
- 'test/**/*'
|
@@ -79,3 +80,7 @@ Style/TrailingUnderscoreVariable:
|
|
79
80
|
|
80
81
|
Style/StderrPuts:
|
81
82
|
Enabled: false
|
83
|
+
|
84
|
+
# https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/HashSyntax
|
85
|
+
Style/HashSyntax:
|
86
|
+
EnforcedShorthandSyntax: never
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
3.3.8
|
data/Gemfile
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
gem 'pry-byebug'
|
4
|
-
gem 'rubocop', '~>
|
4
|
+
gem 'rubocop', '~> 1.54.1'
|
5
|
+
|
6
|
+
gem 'rake', '>= 12.3.3'
|
7
|
+
gem 'rspec', '~> 3.7'
|
8
|
+
gem 'simplecov', '~> 0'
|
5
9
|
|
6
10
|
# Specify your gem's dependencies in {gemname}.gemspec
|
7
11
|
gemspec
|
data/Makefile
CHANGED
@@ -1,16 +1,8 @@
|
|
1
|
-
|
1
|
+
default:
|
2
|
+
bundle exec rspec
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
rspec
|
6
|
-
|
7
|
-
test5:
|
8
|
-
SIMPLE_SQL_ACTIVERECORD_SPECS="> 5,< 6" bundle update activerecord
|
9
|
-
rspec
|
10
|
-
|
11
|
-
test6:
|
12
|
-
SIMPLE_SQL_ACTIVERECORD_SPECS="> 6,< 7" bundle update activerecord
|
13
|
-
rspec
|
4
|
+
tests:
|
5
|
+
./scripts/integration_tests
|
14
6
|
|
15
7
|
stats:
|
16
8
|
@scripts/stats lib/simple/sql
|
data/README.md
CHANGED
@@ -1,85 +1,112 @@
|
|
1
1
|
# simple-sql
|
2
2
|
|
3
|
-
The simple-sql gem defines a module `Simple::SQL`, which you can use to execute
|
4
|
-
SQL statements on a Postgresql database. Care has been taken to provide the
|
5
|
-
simplest interface we could come up with.
|
3
|
+
The simple-sql gem defines a module `Simple::SQL`, which you can use to execute SQL statements on a Postgresql database. Care has been taken to provide the simplest interface we could come up with.
|
6
4
|
|
7
|
-
|
8
|
-
plans to do so. If you deem that necessary, feel free to fork this code into a
|
9
|
-
`simple-sql-<yourdatabase>` gem to provide the same or a similar interface.
|
5
|
+
However, apart from providing a simple interface `simple-sql` also provides a huge performance boon over `ActiveRecord` when interacting with your database, by cutting out all the `ActiveRecord` layers between the `pg` gem and you ruby code. However, `simple-sql` is still using ActiveRecord to manage connections.
|
10
6
|
|
11
|
-
|
7
|
+
**Note:** Databases other than Postgresql are not supported, and there are no plans to do so.
|
8
|
+
|
9
|
+
<!-- TOC -->
|
12
10
|
|
13
|
-
|
14
|
-
line to your `Gemfile` and bundle us usual:
|
11
|
+
## Installation
|
15
12
|
|
16
|
-
|
13
|
+
The gem is available on rubygems as `simple-sql`.
|
17
14
|
|
18
|
-
|
15
|
+
Also make sure to add the `pg` gem to your Gemfile. `simple-sql` should be pretty flexible about the `pg` version: the earliest supported version is `0.21`.
|
19
16
|
|
20
|
-
|
17
|
+
To use it add the following lines to your `Gemfile` and bundle up usual:
|
21
18
|
|
22
|
-
|
23
|
-
|
19
|
+
```ruby
|
20
|
+
gem 'simple-sql' # + version
|
21
|
+
gem 'pg' # + version
|
22
|
+
```
|
24
23
|
|
25
|
-
|
26
|
-
you typically have a connection to Postgresql configured already. In that case you
|
27
|
-
don't need to do anything; `simple-sql` will just use the current database connection.
|
28
|
-
|
29
|
-
This is usually the right thing, especially since `simple-sql`, when called from inside
|
30
|
-
a controller action, is using the connection valid in the current context.
|
24
|
+
## Connecting to a database
|
31
25
|
|
32
|
-
|
33
|
-
server. **Note that in this case there are no pooled connections!** simple-sql is not
|
34
|
-
thread-/fiber-safe in this mode.
|
26
|
+
Before you can send SQL commands to a database you need to connect first. `simple-sql` gives you the following options:
|
35
27
|
|
36
|
-
|
28
|
+
### Explicitly connect to the database
|
37
29
|
|
38
|
-
|
30
|
+
In a standalone applications you need to connect to a Postgresql server. `simple-sql` uses ActiveRecord's connection handling.
|
39
31
|
|
40
|
-
|
32
|
+
You can explicitly connect to a server by calling
|
41
33
|
|
42
|
-
|
34
|
+
```ruby
|
35
|
+
db = ::Simple::SQL.connect! "postgres://user:password@server[:port]/database"
|
36
|
+
```
|
43
37
|
|
44
|
-
|
45
|
-
|
46
|
-
- the `DATABASE_URL` environment value
|
47
|
-
- the `config/database.yml` file from the current directory, taking `RAILS_ENV`/`RACK_ENV` into account.
|
38
|
+
You can then use `db.ask`, `db.all`, etc. (See below)
|
48
39
|
|
49
|
-
###
|
40
|
+
### Automatically determine connection details
|
50
41
|
|
51
|
-
|
42
|
+
Alternatively, you can have `simple-sql` figure out the details automatically:
|
52
43
|
|
53
44
|
```ruby
|
54
|
-
Simple::SQL.
|
45
|
+
db = ::Simple::SQL.connect!
|
55
46
|
```
|
56
47
|
|
57
|
-
|
48
|
+
In that case we try to find connection parameters in the following places:
|
49
|
+
|
50
|
+
- the `DATABASE_URL` environment value
|
51
|
+
- the `config/database.yml` file from the current directory, taking `RAILS_ENV`/`RACK_ENV` into account.
|
52
|
+
|
53
|
+
### Using the default connection
|
54
|
+
|
55
|
+
You don't need to call `::Simple::SQL.connect!` inside a Rails application. `simple-sql` automatically uses the default connection when using `Simple::SQL` instead of an explicit database handle; i.e. typically you can use
|
58
56
|
|
59
57
|
```ruby
|
60
|
-
Simple::SQL.
|
58
|
+
Simple::SQL.ask "SELECT ..."
|
61
59
|
```
|
62
60
|
|
61
|
+
and `simple-sql` will be doing the right thing.
|
62
|
+
|
63
|
+
This is especially true in a Rails' applications controller action, because it allows you to mix Simple::SQL and ActiveRecord interactions with the database.
|
64
|
+
|
65
|
+
## Simple queries
|
66
|
+
|
63
67
|
### Simple::SQL.all: Fetching all results of a query
|
64
68
|
|
65
69
|
`Simple::SQL.all` runs a query, with optional arguments, and returns the result. Usage example:
|
66
70
|
|
67
|
-
|
71
|
+
```ruby
|
72
|
+
# returns an array of arrays `[ <id>, <email> ]`.
|
73
|
+
Simple::SQL.all "SELECT id, email FROM users WHERE id = ANY($1)", [1,2,3]
|
74
|
+
```
|
75
|
+
|
76
|
+
If a block is passed to SQL.all, each row is yielded into the block:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
Simple::SQL.all "SELECT id, email FROM users" do |id, email|
|
80
|
+
# do something
|
81
|
+
end
|
82
|
+
```
|
68
83
|
|
69
|
-
|
70
|
-
Otherwise it returns an array of arrays.
|
84
|
+
Typically the SQL query returns an array of arrays (1 per row). If, however, the query is set up to only return a single column, this method returns an array of these values instead. This is especially useful with the block version:
|
71
85
|
|
72
86
|
Examples:
|
73
87
|
|
74
88
|
```ruby
|
75
89
|
Simple::SQL.all("SELECT id FROM users") # returns an array of id values, but
|
76
|
-
|
90
|
+
|
91
|
+
Simple::SQL.all "SELECT id FROM users" do |id|
|
92
|
+
# do something with the users' ids.
|
93
|
+
end
|
77
94
|
```
|
78
95
|
|
79
|
-
|
96
|
+
### Simple::SQL.each: run a block with each result of a query
|
97
|
+
|
98
|
+
This is identical to `.all`. It might offer beneficial optimisations, but this is theoretical at this point.
|
80
99
|
|
81
100
|
```ruby
|
82
|
-
Simple::SQL.
|
101
|
+
Simple::SQL.each "SELECT id, email FROM users" do |id, email|
|
102
|
+
# do something
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
### Simple::SQL.print: Printing results of a query
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
Simple::SQL.print "SELECT id, email FROM users" do |id, email|
|
83
110
|
# do something
|
84
111
|
end
|
85
112
|
```
|
@@ -88,9 +115,13 @@ end
|
|
88
115
|
|
89
116
|
`Simple::SQL.ask` runs a query, with optional arguments, and returns the first result row or nil, if there was no result.
|
90
117
|
|
91
|
-
|
118
|
+
```ruby
|
119
|
+
Simple::SQL.ask "SELECT id, email FROM users WHERE id = ANY($1) LIMIT 1", [1,2,3]
|
120
|
+
```
|
121
|
+
|
122
|
+
If the SQL query only returns a single column, this method returns the column value of the first row; otherwise it returns an array (or `nil` if there was no result).
|
92
123
|
|
93
|
-
|
124
|
+
Usually all attributes are converted to its corresponding ruby type.
|
94
125
|
|
95
126
|
Examples:
|
96
127
|
|
@@ -99,75 +130,146 @@ Simple::SQL.ask "SELECT id FROM users WHERE email=$1", "foo@local" # ret
|
|
99
130
|
Simple::SQL.ask "SELECT id, email FROM users WHERE email=$?", "foo@local" # returns an array `[ <id>, <email> ]` (or `nil`)
|
100
131
|
```
|
101
132
|
|
102
|
-
###
|
133
|
+
### Using placeholders
|
134
|
+
|
135
|
+
Note: whenever you run a query `simple-sql` takes care of sending query parameters over the wire properly. That means that you use placeholders `$1`, `$2`, etc. to use these inside your queries; the following is a correct example:
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
Simple::SQL.all "SELECT * FROM users WHERE email=$1", "foo@bar.local"
|
139
|
+
```
|
140
|
+
|
141
|
+
Usually arguments are converted correctly when sending over to the database. One notable exception is sending `jsonb` data - you must use JSON.encode on the argument:
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
Simple::SQL.ask "INSERT INTO table (column) VALUES($1)", JSON.encode(..)
|
145
|
+
```
|
146
|
+
|
147
|
+
It is probably worth pointing out that you cannotto use an array as the argument for the `IN(?)` SQL construct. Instead you want to use `ANY`, for example:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
Simple::SQL.all "SELECT * FROM users WHERE id = ANY($1)", [1,2,3]
|
151
|
+
```
|
152
|
+
|
153
|
+
### Determining the result type
|
154
|
+
|
155
|
+
By default `ask` and `all` convert each result row into an Array. Sometimes you might want to use Hashes or similar objects instead. To do so, you use the `into:` keyword argument:
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
# returns a single Hash (or nil)
|
159
|
+
Simple::SQL.ask("SELECT id FROM users", into: Hash)
|
160
|
+
```
|
161
|
+
|
162
|
+
If you want the returned record to be in a structure which is not a Hash, you can use the `into: <klass>` option. The following would return an array of up to two `OpenStruct` objects:
|
103
163
|
|
104
|
-
|
105
|
-
|
164
|
+
```ruby
|
165
|
+
sql = "SELECT id, email FROM users WHERE id = ANY($1) LIMIT 1",
|
166
|
+
Simple::SQL.all sql, [1,2,3], into: OpenStruct
|
167
|
+
```
|
106
168
|
|
107
|
-
|
108
|
-
Simple::SQL.ask("SELECT id FROM users", into: Hash)
|
169
|
+
This supports all target types that take a constructor accepting Hash arguments.
|
109
170
|
|
110
|
-
|
111
|
-
the `into: <klass>` option. The following would return an array of up to two `OpenStruct`
|
112
|
-
objects:
|
171
|
+
It also supports a `:struct` argument, in which case simple-sql creates uses a Struct-class. Struct classes are reused when possible, and are maintained by `Simple::SQL`. This is potentially the best performing option when you want to use dot-notation.
|
113
172
|
|
114
|
-
|
115
|
-
|
173
|
+
```ruby
|
174
|
+
sql = "SELECT id, email FROM users WHERE id = ANY($1) LIMIT 1",
|
175
|
+
Simple::SQL.all sql, [1,2,3], into: :struct
|
176
|
+
```
|
116
177
|
|
117
|
-
|
178
|
+
### Running non-`SELECT` queries
|
118
179
|
|
119
|
-
|
120
|
-
Struct classes are reused when possible, and are maintained by Simple::SQL.
|
180
|
+
You can use `all` and `ask` with other queries as well. However, both only support running a single query. If you want to run multiple queries (i.e. a SQL script) you would probably look into `Simple::SQL.exec` instead.
|
121
181
|
|
122
|
-
|
123
|
-
Simple::SQL.all sql, [1,2,3], into: :struct
|
182
|
+
Be aware that `Simple::SQL.exec` does not support placeholders: you cannot pass in arguments into `Simple::SQL.exec`.
|
124
183
|
|
125
184
|
### Transaction support
|
126
185
|
|
127
|
-
`simple-sql`
|
128
|
-
connection, we use ActiveRecord's transaction implementation (which uses savepoints for nested
|
129
|
-
transactions, so you might be able to rollback from inside a nested transaction).
|
186
|
+
`simple-sql` borrows transaction support from `ActiveRecord`.
|
130
187
|
|
131
|
-
|
188
|
+
## Using scopes
|
132
189
|
|
133
|
-
|
190
|
+
A scope lets you build a condition over time, like this:
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
scope = db.scope "SELECT * FROM users"
|
194
|
+
scope = scope.where(email: ["foo@foobar.test", "bar@foobar.test"])
|
195
|
+
scope = scope.where("deleted_at is NULL")
|
196
|
+
users = scope.all
|
197
|
+
```
|
134
198
|
|
135
|
-
|
136
|
-
created to write to STDERR; to get another logger use code like
|
199
|
+
This also works with the default connection, via `scope = Simple::SQL.scope ...`.
|
137
200
|
|
138
|
-
|
201
|
+
A scope supports the following methods to set up a scope:
|
139
202
|
|
140
|
-
|
203
|
+
| | |
|
204
|
+
|---------------------------|---------------------------------------------|
|
205
|
+
| `where(args...)` | add additional conditions. `where` also has additional support for using `?` instead of `$nn`, and supports JSONB query conditions. See [where.rb](lib/simple/sql/connection/scope/where.rb) for details.|
|
206
|
+
| `order_by(sql_fragment)` ||
|
207
|
+
| `limit(limit)` ||
|
208
|
+
| `offset(offset)` ||
|
209
|
+
| `paginate(per:, page:)` | adds pagination (calls `limit` and `offset`) |
|
141
210
|
|
142
|
-
### 1. Multiple connections
|
143
211
|
|
144
|
-
|
145
|
-
connected via ActiveRecord::Base.connection.
|
212
|
+
These methods can then be used to evaluate the scope:
|
146
213
|
|
147
|
-
|
214
|
+
| | |
|
215
|
+
|---------------------------|---------------------------------------------|
|
216
|
+
| `all(into: ...)` | returns all matching entries |
|
217
|
+
| `first(into: ...)` | returns the first matching entry|
|
218
|
+
| `count` | returns the exact count of matching records|
|
219
|
+
| `count_estimate` | returns a fast estimate of the count of matching records. Note that this needs suitable and up-to-date indices.|
|
220
|
+
| `enumerate_groups(sql)` | returns all groups |
|
221
|
+
| `count_by(sql)` | counts by groups |
|
222
|
+
| `print` | print all matching entries|
|
223
|
+
| `explain` | returns the query plan|
|
148
224
|
|
149
|
-
|
225
|
+
## Inserting objects
|
150
226
|
|
151
|
-
|
227
|
+
Inserting objects is much faster via `simple-sql`. You should be able to insert ~1000 or so records per second into a table with no trouble.
|
152
228
|
|
153
|
-
|
154
|
-
|
155
|
-
|
229
|
+
```ruby
|
230
|
+
Simple::SQL.insert :users, first_name: "foo", last_name: "bar"
|
231
|
+
```
|
232
|
+
|
233
|
+
```ruby
|
234
|
+
users = []
|
235
|
+
users.push first_name: "first", last_name: "user"
|
236
|
+
users.push first_name: "second", last_name: "user"
|
237
|
+
|
238
|
+
Simple::SQL.insert :users, users
|
239
|
+
```
|
240
|
+
|
241
|
+
The `.insert` method lets you set up conflict resolution, via
|
242
|
+
|
243
|
+
```ruby
|
244
|
+
Simple::SQL.insert :users, users, on_conflict: :ignore
|
245
|
+
```
|
246
|
+
|
247
|
+
## Advisory Locks
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
Simple::SQL.transaction do
|
251
|
+
Simple::SQL.lock!(4711)
|
252
|
+
|
253
|
+
# do something.
|
254
|
+
end
|
255
|
+
```
|
256
|
+
|
257
|
+
## Logging
|
156
258
|
|
157
|
-
|
158
|
-
pg to not decode data in any meaningful way, and provides some code to decode
|
159
|
-
the data returned from the database. Only a handful of types is currently
|
160
|
-
supported by the Decoder - it is fairly easy to add new types, though.
|
259
|
+
`simple-sql` builds a logger which logs all queries. The logger, by default, is created to write to STDERR; to get another logger use code like
|
161
260
|
|
162
|
-
|
261
|
+
```ruby
|
262
|
+
Simple::SQL.logger = Rails.logger
|
263
|
+
```
|
264
|
+
|
265
|
+
## Bugs and Limitations
|
266
|
+
|
267
|
+
**Limited support for types:** This gem does not use `pg`'s support for encoding and decoding types, since that might probably interfere with how ActiveRecord is setting up the `pg` gem.
|
163
268
|
|
164
|
-
|
165
|
-
strings containing the "`" character.
|
269
|
+
It therefore assumes ActiveRecord is used in the same project, which sets up pg to not decode data in any meaningful way, and provides some code to decode the data returned from the database. Only a handful of types is currently supported by the Decoder - it is fairly easy to add new types, though.
|
166
270
|
|
167
271
|
## Test
|
168
272
|
|
169
273
|
1. `createdb simple-sql-test`
|
170
274
|
2. `bundle install`
|
171
275
|
3. `bin/rspec`
|
172
|
-
|
173
|
-
## Test again
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.9.0
|
data/bin/db_restore
CHANGED
@@ -3,7 +3,13 @@ require 'yaml'
|
|
3
3
|
|
4
4
|
env = ENV["POSTJOB_ENV"] || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
|
5
5
|
|
6
|
-
|
6
|
+
path = "config/database.yml"
|
7
|
+
configs = if Psych::VERSION > '4.0'
|
8
|
+
YAML.safe_load(File.read(path), aliases: true)
|
9
|
+
else
|
10
|
+
YAML.safe_load(File.read(path), [], [], true)
|
11
|
+
end
|
12
|
+
|
7
13
|
config = configs.fetch(env) { configs.fetch("defaults") }
|
8
14
|
|
9
15
|
ENV["PGHOST"] = config["host"]
|
data/bin/pg
CHANGED
@@ -3,7 +3,13 @@ require 'yaml'
|
|
3
3
|
|
4
4
|
env = ENV["POSTJOB_ENV"] || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
|
5
5
|
|
6
|
-
|
6
|
+
path = "config/database.yml"
|
7
|
+
configs = if Psych::VERSION > '4.0'
|
8
|
+
YAML.safe_load(File.read(path), aliases: true)
|
9
|
+
else
|
10
|
+
YAML.safe_load(File.read(path), [], [], true)
|
11
|
+
end
|
12
|
+
|
7
13
|
config = configs.fetch(env) { configs.fetch("defaults") }
|
8
14
|
|
9
15
|
ENV["PGHOST"] = config["host"]
|
data/config/database.yml
CHANGED
data/lib/simple/sql/config.rb
CHANGED
@@ -16,7 +16,7 @@ module Simple::SQL::Config
|
|
16
16
|
|
17
17
|
config = {
|
18
18
|
dbname: uri.path.sub(%r{^/}, ""),
|
19
|
-
host:
|
19
|
+
host: uri.hostname
|
20
20
|
}
|
21
21
|
config[:port] = uri.port if uri.port
|
22
22
|
config[:user] = uri.user if uri.user
|
@@ -55,7 +55,12 @@ module Simple::SQL::Config
|
|
55
55
|
|
56
56
|
def load_activerecord_base_configuration(path:, env:)
|
57
57
|
require "yaml"
|
58
|
-
database_config =
|
58
|
+
database_config = if Psych::VERSION > '4.0'
|
59
|
+
YAML.safe_load(File.read(path), aliases: true)
|
60
|
+
else
|
61
|
+
YAML.safe_load(File.read(path), [], [], true)
|
62
|
+
end
|
63
|
+
|
59
64
|
env ||= ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
|
60
65
|
|
61
66
|
database_config[env] ||
|
@@ -81,6 +81,7 @@ class Simple::SQL::Connection
|
|
81
81
|
# - <tt>Simple::SQL.ask "SELECT id, email FROM users WHERE email=$?", "foo@local"</tt>
|
82
82
|
# returns an array <tt>[ <id>, <email> ]</tt> (or +nil+)
|
83
83
|
def ask(sql, *args, into: nil)
|
84
|
+
# rubocop:disable Lint/UnreachableLoop
|
84
85
|
catch(:ok) do
|
85
86
|
each(sql, *args, into: into) { |row| throw :ok, row }
|
86
87
|
nil
|
@@ -28,9 +28,9 @@ class Simple::SQL::Connection
|
|
28
28
|
|
29
29
|
class Inserter
|
30
30
|
CONFICT_HANDLING = {
|
31
|
-
nil
|
31
|
+
nil => "",
|
32
32
|
:nothing => "ON CONFLICT DO NOTHING",
|
33
|
-
:ignore
|
33
|
+
:ignore => "ON CONFLICT DO NOTHING"
|
34
34
|
}
|
35
35
|
|
36
36
|
#
|
@@ -89,6 +89,7 @@ class Simple::SQL::Connection
|
|
89
89
|
|
90
90
|
def json_encode(value)
|
91
91
|
return value unless value.is_a?(Hash) || value.is_a?(Array)
|
92
|
+
|
92
93
|
JSON.generate(value)
|
93
94
|
end
|
94
95
|
end
|