mini_sql 0.2.2-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2fa76e26bf57648741cebb8a9ddbc55dddb48b51b10ac4a5ecf0f8ac7cf58373
4
+ data.tar.gz: 673660673dbf917c3eaa48c13a73ca2e0fa1fd58972705ef10778b826c09fcd6
5
+ SHA512:
6
+ metadata.gz: f7de03a4f91d31b466c3b3f7c7fc25b7cdc2b35229bb36e921a8258088fc1035dfd116c29db0cae1e2aa887eb31ae411c19a17ed8d0b2e27e301b80752e88599
7
+ data.tar.gz: 7bf2cde4021d60268608cfb4b144af908a0993a044af06e43ea1400c6dba34cb9c392b28cdd8cde4758a88bcab2d83f59df46ffbd5e52aad94db76b3d3ed1377
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+ /.idea/
@@ -0,0 +1,21 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.5.1
4
+ - ruby-head
5
+
6
+ before_install:
7
+ - gem install bundler
8
+
9
+ cache: bundler
10
+ sudo: false
11
+
12
+ addons:
13
+ postgresql: 9.6
14
+
15
+ install:
16
+ - createdb test_mini_sql
17
+ - bundle install
18
+
19
+ matrix:
20
+ allow_failures:
21
+ - rvm: ruby-head
@@ -0,0 +1,7 @@
1
+ 2019-11-04 - 0.2.2
2
+
3
+ - Added adapters for JRuby postgres support thanks to @enebo
4
+
5
+ 2019-02-25 - 0.2.1
6
+
7
+ - Handle `BigDecimal.new` deprecation by using `BigDecimal()` instead
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at sam.saffron@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mini_sql.gemspec
6
+ gemspec
@@ -0,0 +1,6 @@
1
+ guard :minitest do
2
+ watch(%r{^test/(.*)_test\.rb$})
3
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
4
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
5
+ watch("test/mini_sql/builder_tests.rb") { ['test/mini_sql/postgres/builder_test.rb', 'test/mini_sql/sqlite/builder_test.rb'] }
6
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 TODO: Write your name
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.
@@ -0,0 +1,120 @@
1
+ # MiniSql
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'mini_sql'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mini_sql
18
+
19
+ ## Usage
20
+
21
+ MiniSql is a very simple, safe and fast SQL executor and mapper for PG and Sqlite.
22
+
23
+ ```ruby
24
+ pg_conn = PG.connect(db_name: 'my_db')
25
+ conn = MiniSql::Connection.get(pg_conn)
26
+
27
+ puts conn.exec('update table set column = 1 where id in (1,2)')
28
+ # returns 2 if 2 rows changed
29
+
30
+ conn.query("select 1 id, 'bob' name").each do |user|
31
+ puts user.name # bob
32
+ puts user.id # 1
33
+ end
34
+
35
+ p conn.query_single('select 1 union select 2')
36
+ # [1,2]
37
+
38
+ p conn.query_hash('select 1 as a, 2 as b union select 3, 4')
39
+ # [{"a" => 1, "b"=> 1},{"a" => 3, "b" => 4}
40
+ ```
41
+
42
+ ## The query builder
43
+
44
+ You can use the simple query builder interface to compose queries.
45
+
46
+ ```ruby
47
+ builder = conn.build("select * from topics /*where*/ /*limit*/")
48
+
49
+ builder.where('created_at > ?', Time.now - 400)
50
+
51
+ if look_for_something
52
+ builder.where("title = :title", title: 'something')
53
+ end
54
+
55
+ builder.limit(20)
56
+
57
+ builder.query.each do |t|
58
+ puts t.id
59
+ puts t.title
60
+ end
61
+ ```
62
+
63
+ The builder allows for `order_by`, `where`, `select`, `set`, `limit`, `join`, `left_join` and `offset`.
64
+
65
+ ## Is it fast?
66
+
67
+ Yes, it is very fast. See benchmarks in [the bench directory](https://github.com/discourse/mini_sql/tree/master/bench).
68
+
69
+ As a rule it will outperform similar naive PG code while remaining safe.
70
+
71
+ ```ruby
72
+ pg_conn = PG.connect(db_name: 'my_db')
73
+
74
+ # this is slower, and less safe
75
+ result = pg_conn.async_exec('select * from table')
76
+ result.each do |r|
77
+ name = r['name']
78
+ end
79
+ # ideally you want to remember to run r.clear here
80
+
81
+ # this is faster and safer
82
+ conn = MiniSql::Connection.get(pg_conn)
83
+ r = conn.query('select * from table')
84
+
85
+ r.each do |row|
86
+ name = row.name
87
+ end
88
+ ```
89
+
90
+ ## Safety
91
+
92
+ In PG gem version 1.0 and below you should be careful to clear results. If you do not you risk memory bloat.
93
+ See: [Sam's blog post](https://samsaffron.com/archive/2018/06/13/ruby-x27-s-external-malloc-problem).
94
+
95
+ MiniSql is careful to always clear results as soon as possible.
96
+
97
+ ## Timestamp decoding
98
+
99
+ MiniSql's default type mapper prefers treating `timestamp without time zone` columns as utc. This is done to ensure widest amount of compatability and is a departure from the default in the PG 1.0 gem. If you wish to amend behavior feel free to pass in a custom type_map.
100
+
101
+
102
+ ## I want more features!
103
+
104
+ MiniSql is designed to be very minimal. Even though the query builder and type materializer give you a lot of mileage, it is not intended to be a fully fledged ORM. If you are looking for an ORM I recommend investigating ActiveRecord or Sequel which provide significantly more features.
105
+
106
+ ## Development
107
+
108
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
109
+
110
+ ## Contributing
111
+
112
+ Bug reports and pull requests are welcome on GitHub at https://github.com/SamSaffron/mini_sql. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
113
+
114
+ ## License
115
+
116
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
117
+
118
+ ## Code of Conduct
119
+
120
+ Everyone interacting in the MiniSql project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/discourse/mini_sql/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,16 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ if RUBY_ENGINE == 'jruby' # Excluding sqlite3 tests
5
+ test_glob = "test/**/{inline_param_encoder_test.rb,postgres/*_test.rb}"
6
+ else
7
+ test_glob = "test/**/*_test.rb"
8
+ end
9
+
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << "test"
12
+ t.libs << "lib"
13
+ t.test_files = FileList[test_glob]
14
+ end
15
+
16
+ task :default => :test
@@ -0,0 +1,286 @@
1
+ require 'bundler/inline'
2
+
3
+ gemfile do
4
+ source 'https://rubygems.org'
5
+ gem 'pg', github: 'ged/ruby-pg'
6
+ gem 'mini_sql', path: '../'
7
+ gem 'activesupport'
8
+ gem 'activerecord'
9
+ gem 'activemodel'
10
+ gem 'memory_profiler'
11
+ gem 'benchmark-ips'
12
+ gem 'sequel', github: 'jeremyevans/sequel'
13
+ gem 'sequel_pg', github: 'jeremyevans/sequel_pg', require: 'sequel'
14
+ # gem 'swift-db-postgres', github: 'deepfryed/swift-db-postgres'
15
+ end
16
+
17
+ require 'sequel'
18
+ require 'active_record'
19
+ require 'memory_profiler'
20
+ require 'benchmark/ips'
21
+ require 'mini_sql'
22
+
23
+ ActiveRecord::Base.establish_connection(
24
+ :adapter => "postgresql",
25
+ :database => "test_db"
26
+ )
27
+
28
+ Sequel.default_timezone = :utc
29
+ DB = Sequel.postgres('test_db')
30
+
31
+ pg = ActiveRecord::Base.connection.raw_connection
32
+
33
+ pg.async_exec <<SQL
34
+ drop table if exists timestamps
35
+ SQL
36
+
37
+ pg.async_exec <<SQL
38
+ CREATE TABLE timestamps (
39
+ id int primary key,
40
+ time1 timestamp without time zone NOT NULL,
41
+ time2 timestamp without time zone NOT NULL,
42
+ time3 timestamp without time zone NOT NULL,
43
+ time4 timestamp without time zone NOT NULL
44
+ )
45
+ SQL
46
+
47
+ class Timestamp < ActiveRecord::Base
48
+ end
49
+
50
+ class TimestampSequel< Sequel::Model(:timestamps)
51
+ end
52
+
53
+
54
+ Timestamp.transaction do
55
+ stamps = {
56
+ }
57
+ Timestamp.columns.each do |c|
58
+ stamps[c.name.to_sym] = case c.type
59
+ when :integer then 1
60
+ when :datetime then Time.now
61
+ when :boolean then false
62
+ else "HELLO WORLD" * 2
63
+ end
64
+ end
65
+
66
+ 1000.times do |id|
67
+ stamps[:id] = id
68
+ Timestamp.create!(stamps)
69
+ end
70
+ end
71
+
72
+ $conn = ActiveRecord::Base.connection.raw_connection
73
+
74
+ def ar_pluck_times(l=1000)
75
+ s = +""
76
+ Timestamp.limit(l).order(:id).pluck(:time1, :time2).each do |time1, time2|
77
+ s << time1.to_f.to_s
78
+ s << time2.to_f.to_s
79
+ end
80
+ s
81
+ end
82
+
83
+ def ar_select_times(l=1000)
84
+ s = +""
85
+ Timestamp.limit(l).order(:id).select(:time1, :time2).each do |t|
86
+ s << t.time1.to_f.to_s
87
+ s << t.time2.to_f.to_s
88
+ end
89
+ s
90
+ end
91
+
92
+ $mini_sql = MiniSql::Connection.new($conn)
93
+
94
+ def pg_times_params(l=1000)
95
+ s = +""
96
+ # use the safe pattern here
97
+ r = $conn.async_exec_params(-"select time1, time2 from timestamps order by id limit $1", [l])
98
+ r.type_map = $mini_sql.type_map
99
+
100
+ i = 0
101
+ a = r.values
102
+ n = a.length
103
+
104
+ while i < n
105
+ s << a[i][0].to_f.to_s
106
+ s << a[i][1].to_f.to_s
107
+ i += 1
108
+ end
109
+ r.clear
110
+ s
111
+ end
112
+
113
+ def pg_times(l=1000)
114
+ s = +""
115
+ # use the safe pattern here
116
+ r = $conn.async_exec("select time1, time2 from timestamps order by id limit #{l}")
117
+ r.type_map = $mini_sql.type_map
118
+
119
+ i = 0
120
+ a = r.values
121
+ n = a.length
122
+
123
+ while i < n
124
+ s << a[i][0].to_f.to_s
125
+ s << a[i][1].to_f.to_s
126
+ i += 1
127
+ end
128
+ r.clear
129
+ s
130
+ end
131
+
132
+ def mini_sql_times(l=1000)
133
+ s = +""
134
+ $mini_sql.query(-"select time1, time2 from timestamps order by id limit ?", l).each do |t|
135
+ s << t.time1.to_f.to_s
136
+ s << t.time2.to_f.to_s
137
+ end
138
+ s
139
+ end
140
+
141
+ def sequel_times(l=1000)
142
+ s = +""
143
+ TimestampSequel.limit(l).order(:id).select(:time1, :time2).each do |t|
144
+ s << t.time1.to_f.to_s
145
+ s << t.time2.to_f.to_s
146
+ end
147
+ s
148
+ end
149
+
150
+ def sequel_pluck_times(l=1000)
151
+ s = +""
152
+ TimestampSequel.limit(l).order(:id).select_map([:time1, :time2]).each do |t|
153
+ s << t[0].to_f.to_s
154
+ s << t[1].to_f.to_s
155
+ end
156
+ s
157
+ end
158
+
159
+ def sequel_raw_times(l=1000)
160
+ s = +""
161
+ DB[-"select time1, time2 from timestamps order by id limit ?", l].map([:time1, :time2]).each do |time1, time2|
162
+ s << time1.to_f.to_s
163
+ s << time2.to_f.to_s
164
+ end
165
+ s
166
+ end
167
+
168
+ # usage is not really recommended but just to compare to pluck lets have it
169
+ def mini_sql_times_single(l=1000)
170
+ s = +""
171
+ i = 0
172
+ r = $mini_sql.query_single(-"select time1, time2 from timestamps order by id limit ?", l)
173
+ while i < r.length
174
+ s << r[i].to_f.to_s
175
+ s << r[i+1].to_f.to_s
176
+ i += 2
177
+ end
178
+ s
179
+ end
180
+
181
+ # $swift = Swift::DB::Postgres.new(db: "test_db", user: 'sam', password: 'password')
182
+ #
183
+ # def swift_select_times(l=1000)
184
+ # s = ""
185
+ # r = $swift.execute("select time1, time2 from timestamps order by id limit $1", l)
186
+ # r.each do |row|
187
+ # s << row[:time1].to_f.to_s
188
+ # s << row[:time2].to_f.to_s
189
+ # end
190
+ # s
191
+ # end
192
+
193
+
194
+ results = [
195
+ ar_select_times,
196
+ ar_pluck_times,
197
+ pg_times,
198
+ pg_times_params,
199
+ mini_sql_times,
200
+ mini_sql_times_single,
201
+ sequel_times,
202
+ sequel_pluck_times,
203
+ sequel_raw_times,
204
+ # can not compare correctly as it is returning DateTime not Time
205
+ # swift_select_times.gsub("+00:00", "Z")
206
+ ]
207
+
208
+ exit(-1) unless results.uniq.length == 1
209
+
210
+ Benchmark.ips do |r|
211
+ r.warmup = 10
212
+ r.time = 5
213
+
214
+ r.report("mini_sql query_single times") do |n|
215
+ while n > 0
216
+ mini_sql_times_single
217
+ n -= 1
218
+ end
219
+ end
220
+ r.report("sequel times") do |n|
221
+ while n > 0
222
+ sequel_times
223
+ n -= 1
224
+ end
225
+ end
226
+ r.report("pg times async_exec values") do |n|
227
+ while n > 0
228
+ pg_times_params
229
+ n -= 1
230
+ end
231
+ end
232
+ r.report("pg times async_exec_params values") do |n|
233
+ while n > 0
234
+ pg_times
235
+ n -= 1
236
+ end
237
+ end
238
+ r.report("mini sql times") do |n|
239
+ while n > 0
240
+ mini_sql_times
241
+ n -= 1
242
+ end
243
+ end
244
+ r.report("sequel pluck times") do |n|
245
+ while n > 0
246
+ sequel_pluck_times
247
+ n -= 1
248
+ end
249
+ end
250
+ r.report("sequel raw times") do |n|
251
+ while n > 0
252
+ sequel_raw_times
253
+ n -= 1
254
+ end
255
+ end
256
+ r.report("ar select times") do |n|
257
+ while n > 0
258
+ ar_select_times
259
+ n -= 1
260
+ end
261
+ end
262
+ r.report("ar pluck times") do |n|
263
+ while n > 0
264
+ ar_pluck_times
265
+ n -= 1
266
+ end
267
+ end
268
+ r.compare!
269
+ end
270
+
271
+ # Linux x64 numbers
272
+ #
273
+ #
274
+ # pg times async_exec values: 441.6 i/s
275
+ # pg times async_exec_params values: 438.5 i/s - same-ish: difference falls within error
276
+ # mini_sql query_single times: 436.3 i/s - same-ish: difference falls within error
277
+ # sequel raw times: 426.1 i/s - same-ish: difference falls within error
278
+ # mini sql times: 417.1 i/s - 1.06x slower
279
+ # sequel pluck times: 414.0 i/s - 1.07x slower
280
+ # sequel times: 378.5 i/s - 1.17x slower
281
+ # ar pluck times: 30.8 i/s - 14.35x slower
282
+ # ar select times: 22.7 i/s - 19.44x slower
283
+ #
284
+
285
+ # NOTE PG version 1.0.0 has a much slower time materializer
286
+ # NOTE 2: on Mac numbers are far closer Time parsing on mac is slow