getto-repository-sequel 0.1.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 +7 -0
- data/.envrc +5 -0
- data/.git_release_request.rc.sh +7 -0
- data/.gitignore +9 -0
- data/.gitlab-ci.yml +12 -0
- data/.travis.yml +12 -0
- data/CHANGELOG.md +16 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +34 -0
- data/LICENSE +21 -0
- data/README.md +350 -0
- data/Rakefile +10 -0
- data/getto-repository-sequel.gemspec +38 -0
- data/lib/getto/repository/sequel.rb +24 -0
- data/lib/getto/repository/sequel/search.rb +145 -0
- data/lib/getto/repository/sequel/version.rb +7 -0
- metadata +149 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 061abf5cf2464713f76e2500d6544a05f3c68c6d2b6180df87d7e8c9f165e999
|
4
|
+
data.tar.gz: d1a884d98e52ed91e2affc997a24e347762dbcee98de063e7e3fc009a8199e1c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 826d26a8b8d0ea4dc89b13432a4191dc7d0cfa3090a228ff53d75c97f7d91578f4c5d00d9e0464de9887b77a07b5d0caa0d1179bf320e73e7d6c51056f7cf15f
|
7
|
+
data.tar.gz: 941630fb67ce7e077eaebf3ce328561eedd16e5b1f9aea5365a3438ccec77d11aa8ae6c5b814851ad22c0c18cc41984938193e18b9f7c30f5e29fe291011d466
|
data/.envrc
ADDED
data/.gitignore
ADDED
data/.gitlab-ci.yml
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
rvm:
|
4
|
+
- 2.5.1
|
5
|
+
before_install: gem install bundler -v 1.16.2
|
6
|
+
deploy:
|
7
|
+
provider: rubygems
|
8
|
+
gem: getto-repository-sequel
|
9
|
+
on:
|
10
|
+
tags: true
|
11
|
+
api_key:
|
12
|
+
secure: "XLSSBGoExWUtl13m7nUaUlLWosB4omJxNv1RxVm1pdQUpExaX/7jKH1SUcxuROxyU04kkvphotiouWHQS+w5HZL1ch0pof3J3DyN0a572iFOZwWM1bbdDL5lYVjkjbNiJAs5qXkSVFGqK23h7XkC6X7awmfDZAF84let7hvhUwjjhJObvYFYCTQ/SuWldCncP5HHNyOUQv8YNqcyApDY0uA+kTt+NPzk8fi9ntBvaBAoJYcQgE3Eo8DtWcowDjZBWgiQJB1BuEDuIAoranyVhtpbf9KiCGoesya6kYG3SQftKi05Gi3kulf8+1AdkyG8eNB9WrawCEMzJ79mT8GX1KsC+7D4rgSdjUnniisQgGxlyTlSd4t5/cEZ1wWQDB5tE/RnES0CqNomS+DX456KtJk8kXUvytqz7UWXbANhnyuRsxtX6xS1v3chW5HF4rzLa4bP+NWpUUvxkqRtEaUN6xMdHEbhJkV4sJe1wNc1ZjZJG50p3XCapj+8qQl87i14BUcR12+tgyyCz1y+mjvUEJTxTIFrB4wn4BBzde2HzPMMDkvGOu6r04agg+YjWkfznMs1IVTDFY8aPHADeQ+MMQlLtf1XtjGiuHQkRkvj8mMQXKEZd/YSy9mCGLmQNP1OKC0kxrbd3sluO+qkK28jtwdYJbqJC9t5iSmvITAPTdw="
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
getto-repository-sequel (0.1.0)
|
5
|
+
getto-repository (~> 1.0.0)
|
6
|
+
sequel (~> 5.13.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
docile (1.3.1)
|
12
|
+
getto-repository (1.0.0)
|
13
|
+
json (2.1.0)
|
14
|
+
minitest (5.11.3)
|
15
|
+
rake (10.5.0)
|
16
|
+
sequel (5.13.0)
|
17
|
+
simplecov (0.16.1)
|
18
|
+
docile (~> 1.1)
|
19
|
+
json (>= 1.8, < 3)
|
20
|
+
simplecov-html (~> 0.10.0)
|
21
|
+
simplecov-html (0.10.2)
|
22
|
+
|
23
|
+
PLATFORMS
|
24
|
+
ruby
|
25
|
+
|
26
|
+
DEPENDENCIES
|
27
|
+
bundler (~> 1.16)
|
28
|
+
getto-repository-sequel!
|
29
|
+
minitest (~> 5.0)
|
30
|
+
rake (~> 10.0)
|
31
|
+
simplecov (~> 0.16)
|
32
|
+
|
33
|
+
BUNDLED WITH
|
34
|
+
1.16.2
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 shun@getto.systems
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,350 @@
|
|
1
|
+
# getto-repository-sequel
|
2
|
+
|
3
|
+
[rubygems: getto-repository-sequel](https://rubygems.org/gems/getto-repository-sequel)
|
4
|
+
|
5
|
+
Repository helper for [Sequel](http://sequel.jeremyevans.net/)
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require "getto/repository/sequel"
|
9
|
+
|
10
|
+
class Repository < Getto::Repository::Sequel
|
11
|
+
def account_exists?(id)
|
12
|
+
not db[:accounts].where(id: id).empty?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
require "sequel"
|
17
|
+
|
18
|
+
db = Sequep.connect config
|
19
|
+
|
20
|
+
repository = Repository.new(db)
|
21
|
+
|
22
|
+
repository.transaction do
|
23
|
+
repository.account_exists?
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
- misc: search helper
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require "getto/repository/sequel/search"
|
31
|
+
|
32
|
+
|
33
|
+
search = Getto::Repository::Search::Sequel.new(
|
34
|
+
limit: 1000,
|
35
|
+
sort: {
|
36
|
+
column: :active,
|
37
|
+
order: true,
|
38
|
+
},
|
39
|
+
query: {
|
40
|
+
"id.eq": "1",
|
41
|
+
|
42
|
+
"name.cont": "cont",
|
43
|
+
|
44
|
+
"name.cont_as_kana": "かな",
|
45
|
+
"name.cont_as_hira": "カナ",
|
46
|
+
|
47
|
+
"name.cont_any": "カナ",
|
48
|
+
|
49
|
+
"active.in": ["True"],
|
50
|
+
},
|
51
|
+
)
|
52
|
+
|
53
|
+
where = search.where do |w|
|
54
|
+
w.search "id.eq", &w.eq(::Sequel[:accounts][:id])
|
55
|
+
|
56
|
+
w.search "name.cont", &w.cont(::Sequel[:accounts][:name])
|
57
|
+
|
58
|
+
w.search "name.cont_as_kana", &w.cont_as_kana(::Sequel[:accounts][:name])
|
59
|
+
w.search "name.cont_as_hira", &w.cont_as_hira(::Sequel[:accounts][:name])
|
60
|
+
|
61
|
+
w.search "name.cont_any", &w.or([
|
62
|
+
w.cont_hira_or_kana(::Sequel[:accounts][:name]),
|
63
|
+
w.cont_hira_or_kana(::Sequel[:accounts][:kana]),
|
64
|
+
])
|
65
|
+
|
66
|
+
w.search "active.in", &w.in(
|
67
|
+
&w.is_not_null(
|
68
|
+
::Sequel[:account_actives][:account_id],
|
69
|
+
"True" => true,
|
70
|
+
"False" => false,
|
71
|
+
)
|
72
|
+
)
|
73
|
+
end
|
74
|
+
|
75
|
+
order = search.order do |o|
|
76
|
+
o.order :active, o.is_not_null(::Sequel[:account_actives][:account_id], 0, 1)
|
77
|
+
|
78
|
+
o.order :id, ::Sequel[:accounts][:id]
|
79
|
+
|
80
|
+
o.force ::Sequel[:accounts][:id]
|
81
|
+
end
|
82
|
+
|
83
|
+
pages = search.pages(
|
84
|
+
db[:accounts]
|
85
|
+
.left_join(:account_actives, account_id: :id)
|
86
|
+
.where(where)
|
87
|
+
.count
|
88
|
+
)
|
89
|
+
# => current page: e.g. count = 120, limit = 100 => pages = 2
|
90
|
+
|
91
|
+
db[:accounts]
|
92
|
+
.left_join(:account_actives, account_id: :id)
|
93
|
+
.where(where)
|
94
|
+
.order(*order)
|
95
|
+
.select(
|
96
|
+
::Sequel[:accounts][:id],
|
97
|
+
::Sequel[:account_actives][:account_id].as(:active),
|
98
|
+
::Sequel[:accounts][:name],
|
99
|
+
::Sequel[:accounts][:kana],
|
100
|
+
)
|
101
|
+
.all
|
102
|
+
```
|
103
|
+
|
104
|
+
It generates sql like below
|
105
|
+
|
106
|
+
```sql
|
107
|
+
SELECT
|
108
|
+
`accounts`.`id`,
|
109
|
+
`account_actives`.`account_id` as `active`,
|
110
|
+
`accounts`.`name`,
|
111
|
+
`accounts`.`kana`
|
112
|
+
FROM
|
113
|
+
`accounts`
|
114
|
+
LEFT JOIN
|
115
|
+
`account_actives`
|
116
|
+
ON `account_actives`.`account_id` = `accounts`.`id`
|
117
|
+
WHERE
|
118
|
+
(`accounts`.`id` = 1) AND
|
119
|
+
(`accounts`.`name` LIKE '%cont%') AND
|
120
|
+
(`accounts`.`name` LIKE '%かな%') AND
|
121
|
+
(`accounts`.`name` LIKE '%カナ%') AND
|
122
|
+
(
|
123
|
+
(
|
124
|
+
(`accounts`.`name` LIKE '%カナ%') OR
|
125
|
+
(`accounts`.`name` LIKE '%かな%')
|
126
|
+
) OR
|
127
|
+
(
|
128
|
+
(`accounts`.`kana` LIKE '%カナ%') OR
|
129
|
+
(`accounts`.`kana` LIKE '%かな%')
|
130
|
+
)
|
131
|
+
) AND
|
132
|
+
(`account_actives`.`account_id` IS NOT NULL)
|
133
|
+
ORDER BY
|
134
|
+
if(`account_actives`.`account_id`, 0, 1) ASC,
|
135
|
+
`accounts`.`id` ASC
|
136
|
+
```
|
137
|
+
|
138
|
+
|
139
|
+
###### Table of Contents
|
140
|
+
|
141
|
+
- [Requirements](#Requirements)
|
142
|
+
- [Usage](#Usage)
|
143
|
+
- [License](#License)
|
144
|
+
|
145
|
+
<a id="Requirements"></a>
|
146
|
+
## Requirements
|
147
|
+
|
148
|
+
- developed on ruby: 2.5.1
|
149
|
+
- [Sequel](http://sequel.jeremyevans.net/)
|
150
|
+
|
151
|
+
|
152
|
+
<a id="Usage"></a>
|
153
|
+
## Usage
|
154
|
+
|
155
|
+
### where clause
|
156
|
+
|
157
|
+
- equals
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
where = search.where do |w|
|
161
|
+
w.search "id.eq", &w.eq(::Sequel[:accounts][:id])
|
162
|
+
end
|
163
|
+
```
|
164
|
+
|
165
|
+
```sql
|
166
|
+
-- { "id.eq": "1" }
|
167
|
+
WHERE
|
168
|
+
(`accounts`.`id` = 1)
|
169
|
+
```
|
170
|
+
|
171
|
+
- contains
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
where = search.where do |w|
|
175
|
+
w.search "name.cont", &w.cont(::Sequel[:accounts][:name])
|
176
|
+
end
|
177
|
+
```
|
178
|
+
|
179
|
+
```sql
|
180
|
+
-- { "name.cont": "cont" }
|
181
|
+
WHERE
|
182
|
+
(`accounts`.`name` LIKE '%cont%')
|
183
|
+
```
|
184
|
+
|
185
|
+
- contains as kana
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
where = search.where do |w|
|
189
|
+
w.search "name.cont", &w.cont_as_kana(::Sequel[:accounts][:name])
|
190
|
+
end
|
191
|
+
```
|
192
|
+
|
193
|
+
```sql
|
194
|
+
-- { "name.cont": "かな" }
|
195
|
+
WHERE
|
196
|
+
(`accounts`.`name` LIKE '%カナ%') -- convert 'かな' to 'カナ'
|
197
|
+
```
|
198
|
+
|
199
|
+
- contains as hira
|
200
|
+
|
201
|
+
```ruby
|
202
|
+
where = search.where do |w|
|
203
|
+
w.search "name.cont", &w.cont_as_hira(::Sequel[:accounts][:name])
|
204
|
+
end
|
205
|
+
```
|
206
|
+
|
207
|
+
```sql
|
208
|
+
-- { "name.cont": "カナ" }
|
209
|
+
WHERE
|
210
|
+
(`accounts`.`name` LIKE '%かな%') -- convert 'カナ' to 'かな'
|
211
|
+
```
|
212
|
+
|
213
|
+
- contains as hira or kana
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
where = search.where do |w|
|
217
|
+
w.search "name.cont", &w.cont_hira_or_kana(::Sequel[:accounts][:name])
|
218
|
+
end
|
219
|
+
```
|
220
|
+
|
221
|
+
```sql
|
222
|
+
-- { "name.cont": "カナ" }
|
223
|
+
WHERE
|
224
|
+
(
|
225
|
+
(`accounts`.`name` LIKE '%カナ%') OR
|
226
|
+
(`accounts`.`name` LIKE '%かな%')
|
227
|
+
)
|
228
|
+
|
229
|
+
-- { "name.cont": "かな" }
|
230
|
+
WHERE
|
231
|
+
(
|
232
|
+
(`accounts`.`name` LIKE '%カナ%') OR
|
233
|
+
(`accounts`.`name` LIKE '%かな%')
|
234
|
+
)
|
235
|
+
```
|
236
|
+
|
237
|
+
- is not null
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
where = search.where do |w|
|
241
|
+
w.search "active.is", &w.is_not_null(
|
242
|
+
::Sequel[:account_actives][:account_id],
|
243
|
+
"True" => true,
|
244
|
+
"False" => false,
|
245
|
+
)
|
246
|
+
end
|
247
|
+
```
|
248
|
+
|
249
|
+
```sql
|
250
|
+
-- { "active.is": "True" }
|
251
|
+
WHERE
|
252
|
+
(`account_actives`.`account_id` IS NOT NULL)
|
253
|
+
|
254
|
+
-- { "active.is": "False" }
|
255
|
+
WHERE
|
256
|
+
(`account_actives`.`account_id` IS NULL)
|
257
|
+
```
|
258
|
+
|
259
|
+
- or
|
260
|
+
|
261
|
+
```ruby
|
262
|
+
w.search "name.cont_any", &w.or([
|
263
|
+
w.cont(::Sequel[:accounts][:name]),
|
264
|
+
w.cont(::Sequel[:accounts][:kana]),
|
265
|
+
])
|
266
|
+
```
|
267
|
+
|
268
|
+
```sql
|
269
|
+
-- { "name.cont": "カナ" }
|
270
|
+
WHERE
|
271
|
+
(
|
272
|
+
(`accounts`.`name` LIKE '%カナ%') OR
|
273
|
+
(`accounts`.`kana` LIKE '%カナ%')
|
274
|
+
)
|
275
|
+
```
|
276
|
+
|
277
|
+
- in
|
278
|
+
|
279
|
+
```ruby
|
280
|
+
w.search "active.in", &w.in(
|
281
|
+
&w.is_not_null(
|
282
|
+
::Sequel[:account_actives][:account_id],
|
283
|
+
"True" => true,
|
284
|
+
"False" => false,
|
285
|
+
)
|
286
|
+
)
|
287
|
+
```
|
288
|
+
|
289
|
+
```sql
|
290
|
+
-- { "active.in": ["True"] }
|
291
|
+
WHERE
|
292
|
+
(
|
293
|
+
(`account_actives`.`account_id` IS NOT NULL)
|
294
|
+
)
|
295
|
+
|
296
|
+
-- { "active.in": ["True","False"] }
|
297
|
+
WHERE
|
298
|
+
(
|
299
|
+
(`account_actives`.`account_id` IS NOT NULL) OR
|
300
|
+
(`account_actives`.`account_id` IS NULL)
|
301
|
+
)
|
302
|
+
```
|
303
|
+
|
304
|
+
- create where-clause by block
|
305
|
+
|
306
|
+
```ruby
|
307
|
+
w.search("active.is"){|val|
|
308
|
+
if val == "True"
|
309
|
+
::Sequel.~( ::Sequel[:account_actives][:account_id] => nil )
|
310
|
+
end
|
311
|
+
}
|
312
|
+
```
|
313
|
+
|
314
|
+
```sql
|
315
|
+
-- { "active.is": "True" }
|
316
|
+
WHERE
|
317
|
+
(`account_actives`.`account_id` IS NOT NULL)
|
318
|
+
|
319
|
+
-- { "active.is": "False" }
|
320
|
+
WHERE
|
321
|
+
(1 = 1) -- if block returns nil, create no where-clauses
|
322
|
+
```
|
323
|
+
|
324
|
+
## Install
|
325
|
+
|
326
|
+
Add this line to your application's Gemfile:
|
327
|
+
|
328
|
+
```ruby
|
329
|
+
gem 'getto-repository-sequel'
|
330
|
+
```
|
331
|
+
|
332
|
+
And then execute:
|
333
|
+
|
334
|
+
```
|
335
|
+
$ bundle
|
336
|
+
```
|
337
|
+
|
338
|
+
Or install it yourself as:
|
339
|
+
|
340
|
+
```
|
341
|
+
$ gem install getto-repository-sequel
|
342
|
+
```
|
343
|
+
|
344
|
+
|
345
|
+
<a id="License"></a>
|
346
|
+
## License
|
347
|
+
|
348
|
+
getto/repository/sequel is licensed under the [MIT](LICENSE) license.
|
349
|
+
|
350
|
+
Copyright © since 2018 shun@getto.systems
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "getto/repository/sequel/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.required_ruby_version = ">= 2.5.1"
|
7
|
+
|
8
|
+
spec.name = "getto-repository-sequel"
|
9
|
+
spec.version = Getto::Repository::Sequel::VERSION
|
10
|
+
spec.authors = ["shun@getto.systems"]
|
11
|
+
spec.email = ["shun@getto.systems"]
|
12
|
+
|
13
|
+
spec.summary = %q{Repository helper for Sequel}
|
14
|
+
spec.description = %q{Repository helper for Sequel}
|
15
|
+
spec.homepage = "https://github.com/getto-systems/getto-repository-sequel"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
|
28
|
+
spec.extra_rdoc_files = ['README.md', 'LICENSE']
|
29
|
+
spec.rdoc_options = %w[--title Getto::Repository::Sequel --main README.md]
|
30
|
+
|
31
|
+
spec.add_runtime_dependency "getto-repository", "~> 1.0.0"
|
32
|
+
spec.add_runtime_dependency "sequel", "~> 5.13.0"
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
37
|
+
spec.add_development_dependency "simplecov", "~> 0.16"
|
38
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Getto
|
2
|
+
module Repository
|
3
|
+
# :nocov:
|
4
|
+
class Sequel
|
5
|
+
def initialize(db)
|
6
|
+
@db = db
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :db
|
10
|
+
|
11
|
+
def transaction
|
12
|
+
db.transaction do
|
13
|
+
yield
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def last_insert_id
|
18
|
+
db["select last_insert_id() as id"]
|
19
|
+
.map{|hash| hash[:id]}.first
|
20
|
+
end
|
21
|
+
end
|
22
|
+
# :nocov:
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
require "getto/initialize_with"
|
2
|
+
|
3
|
+
require "getto/repository/page"
|
4
|
+
|
5
|
+
require "nkf"
|
6
|
+
|
7
|
+
module Getto
|
8
|
+
module Repository
|
9
|
+
# :nocov:
|
10
|
+
class Sequel
|
11
|
+
class Search
|
12
|
+
include Getto::InitializeWith
|
13
|
+
|
14
|
+
initialize_with :limit, :sort, :query
|
15
|
+
|
16
|
+
def pages(count)
|
17
|
+
Getto::Repository::Page.new(count: count, limit: limit).pages
|
18
|
+
end
|
19
|
+
|
20
|
+
def where
|
21
|
+
where = []
|
22
|
+
yield Where.new(query: query, where: where)
|
23
|
+
|
24
|
+
where.compact!
|
25
|
+
|
26
|
+
if where.empty?
|
27
|
+
{1 => 1}
|
28
|
+
else
|
29
|
+
::Sequel.&(*where)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def order
|
34
|
+
order = []
|
35
|
+
yield Order.new(sort: sort, order: order)
|
36
|
+
order
|
37
|
+
end
|
38
|
+
|
39
|
+
class Where
|
40
|
+
def cont(column)
|
41
|
+
->(value){ ::Sequel.like(column, "%#{value}%") }
|
42
|
+
end
|
43
|
+
|
44
|
+
def cont_hira_or_kana(column)
|
45
|
+
self.or([
|
46
|
+
cont_as_kana(column),
|
47
|
+
cont_as_hira(column),
|
48
|
+
])
|
49
|
+
end
|
50
|
+
|
51
|
+
def cont_as_kana(column)
|
52
|
+
->(value){
|
53
|
+
cont(column).call NKF.nkf("--katakana -w", value)
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
def cont_as_hira(column)
|
58
|
+
->(value){
|
59
|
+
cont(column).call NKF.nkf("--hiragana -w", value)
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
def eq(column)
|
64
|
+
->(value){ { column => value } }
|
65
|
+
end
|
66
|
+
|
67
|
+
def is_not_null(column,map)
|
68
|
+
->(value){
|
69
|
+
if map.has_key?(value)
|
70
|
+
if map[value]
|
71
|
+
::Sequel.~(column => nil)
|
72
|
+
else
|
73
|
+
{column => nil}
|
74
|
+
end
|
75
|
+
end
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
def in(&query)
|
81
|
+
->(value){
|
82
|
+
where = value.map(&query).compact
|
83
|
+
unless where.empty?
|
84
|
+
::Sequel.|(*where)
|
85
|
+
end
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
def or(wheres)
|
91
|
+
wheres = wheres.compact
|
92
|
+
|
93
|
+
->(value){
|
94
|
+
unless wheres.empty?
|
95
|
+
::Sequel.|(*wheres.map{|w| w.call(value)})
|
96
|
+
end
|
97
|
+
}
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
def initialize(query:, where:)
|
102
|
+
@query = query
|
103
|
+
@where = where
|
104
|
+
end
|
105
|
+
|
106
|
+
def search(column,&block)
|
107
|
+
if @query.has_key?(column.to_sym)
|
108
|
+
@where << block.call(@query[column.to_sym])
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
class Order
|
114
|
+
def initialize(sort:, order:)
|
115
|
+
@sort = sort
|
116
|
+
@order = order
|
117
|
+
end
|
118
|
+
|
119
|
+
def order(key,column)
|
120
|
+
if @sort[:column] == key
|
121
|
+
force(column)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def force(column)
|
126
|
+
if @sort[:order]
|
127
|
+
@order << ::Sequel.asc(column)
|
128
|
+
else
|
129
|
+
@order << ::Sequel.desc(column)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def is_not_null(column, not_null_value, null_value)
|
134
|
+
::Sequel.function(
|
135
|
+
:if,
|
136
|
+
::Sequel.lit("? is not null", column),
|
137
|
+
not_null_value, null_value)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
end
|
143
|
+
# :nocov:
|
144
|
+
end
|
145
|
+
end
|
metadata
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: getto-repository-sequel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- shun@getto.systems
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: getto-repository
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sequel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 5.13.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 5.13.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.16'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.16'
|
97
|
+
description: Repository helper for Sequel
|
98
|
+
email:
|
99
|
+
- shun@getto.systems
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files:
|
103
|
+
- README.md
|
104
|
+
- LICENSE
|
105
|
+
files:
|
106
|
+
- ".envrc"
|
107
|
+
- ".git_release_request.rc.sh"
|
108
|
+
- ".gitignore"
|
109
|
+
- ".gitlab-ci.yml"
|
110
|
+
- ".travis.yml"
|
111
|
+
- CHANGELOG.md
|
112
|
+
- Gemfile
|
113
|
+
- Gemfile.lock
|
114
|
+
- LICENSE
|
115
|
+
- README.md
|
116
|
+
- Rakefile
|
117
|
+
- getto-repository-sequel.gemspec
|
118
|
+
- lib/getto/repository/sequel.rb
|
119
|
+
- lib/getto/repository/sequel/search.rb
|
120
|
+
- lib/getto/repository/sequel/version.rb
|
121
|
+
homepage: https://github.com/getto-systems/getto-repository-sequel
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
metadata: {}
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options:
|
127
|
+
- "--title"
|
128
|
+
- Getto::Repository::Sequel
|
129
|
+
- "--main"
|
130
|
+
- README.md
|
131
|
+
require_paths:
|
132
|
+
- lib
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: 2.5.1
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
requirements: []
|
144
|
+
rubyforge_project:
|
145
|
+
rubygems_version: 2.7.7
|
146
|
+
signing_key:
|
147
|
+
specification_version: 4
|
148
|
+
summary: Repository helper for Sequel
|
149
|
+
test_files: []
|