jdbc 0.1.1-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 69bedbae4b303c6012ca4f3d78129a649c0c06c7
4
+ data.tar.gz: b37d77323e4f7bc6abb01dd784d660e7d0e5ff78
5
+ SHA512:
6
+ metadata.gz: 19fc18118b3de10225daa97d8278fef83357184cf627d9e3ebd0644aea9ae519576986e0c4c2f726dbf6d5a36e1e785dba25afb15338276822dd86c16dd6d2e3
7
+ data.tar.gz: 4eb87016ece80757b34e4d3e3066d96d4dc2b52ff24a77b67b5273fd35e4223d75cd129f9e9de6e005921db307dd6396fac40ba558b2418f2cbc5f19ef208735
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.irbrc ADDED
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift("lib")
2
+ require "jdbc"
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,48 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
4
+ Documentation:
5
+ Enabled: false
6
+
7
+ Metrics/BlockLength:
8
+ Max: 32
9
+ Exclude:
10
+ - "spec/**/*.rb"
11
+
12
+ Metrics/ClassLength:
13
+ Max: 120
14
+
15
+ Metrics/LineLength:
16
+ Max: 120
17
+
18
+ Metrics/MethodLength:
19
+ Max: 12
20
+
21
+ Style/AndOr:
22
+ EnforcedStyle: conditionals
23
+
24
+ Style/Encoding:
25
+ EnforcedStyle: when_needed
26
+
27
+ Style/FrozenStringLiteralComment:
28
+ Enabled: false
29
+
30
+ Style/IndentHash:
31
+ Enabled: false
32
+
33
+ Style/PercentLiteralDelimiters:
34
+ PreferredDelimiters:
35
+ "%i": "[]"
36
+ "%I": "[]"
37
+ "%w": "[]"
38
+ "%W": "[]"
39
+
40
+ Style/RedundantSelf:
41
+ Enabled: false
42
+
43
+ Style/SignalException:
44
+ Enabled: true
45
+ EnforcedStyle: semantic
46
+
47
+ Style/StringLiterals:
48
+ EnforcedStyle: double_quotes
@@ -0,0 +1,7 @@
1
+ ## 0.1.1
2
+
3
+ * fixed SQL parsing when parentheses present around bindings
4
+
5
+ ## 0.1.0
6
+
7
+ * initial release
@@ -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 ja AT jestem DOT tw. 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/
@@ -0,0 +1,13 @@
1
+ FROM jruby:9.1.7.0-jre-alpine
2
+
3
+ RUN apk --update --no-cache add git openssh-client && \
4
+ gem install bundler && \
5
+ mkdir /jdbc
6
+
7
+ WORKDIR /jdbc
8
+
9
+ COPY jdbc.gemspec Gemfile Gemfile.lock ./
10
+
11
+ RUN bundle
12
+
13
+ COPY . ./
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,103 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jdbc (0.1.1-java)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.3.0)
10
+ coderay (1.1.1)
11
+ concurrent-ruby (1.0.5-java)
12
+ diff-lcs (1.3)
13
+ docile (1.1.5)
14
+ dry-configurable (0.6.1)
15
+ concurrent-ruby (~> 1.0)
16
+ dry-container (0.6.0)
17
+ concurrent-ruby (~> 1.0)
18
+ dry-configurable (~> 0.1, >= 0.1.3)
19
+ dry-core (0.2.4)
20
+ concurrent-ruby (~> 1.0)
21
+ dry-equalizer (0.2.0)
22
+ dry-logic (0.4.1)
23
+ dry-container (~> 0.2, >= 0.2.6)
24
+ dry-core (~> 0.2)
25
+ dry-equalizer (~> 0.2)
26
+ dry-types (0.9.4)
27
+ concurrent-ruby (~> 1.0)
28
+ dry-configurable (~> 0.1)
29
+ dry-container (~> 0.3)
30
+ dry-core (~> 0.2, >= 0.2.1)
31
+ dry-equalizer (~> 0.2)
32
+ dry-logic (~> 0.4, >= 0.4.0)
33
+ inflecto (~> 0.0.0, >= 0.0.2)
34
+ dry-validation (0.10.5)
35
+ concurrent-ruby (~> 1.0)
36
+ dry-configurable (~> 0.1, >= 0.1.3)
37
+ dry-core (~> 0.2, >= 0.2.1)
38
+ dry-equalizer (~> 0.2)
39
+ dry-logic (~> 0.4, >= 0.4.0)
40
+ dry-types (~> 0.9, >= 0.9.0)
41
+ ffi (1.9.17-java)
42
+ hucpa (0.2.2-java)
43
+ dry-validation (~> 0.10)
44
+ inflecto (0.0.2)
45
+ jdbc-postgres (9.4.1206)
46
+ json (2.0.3-java)
47
+ method_source (0.8.2)
48
+ parser (2.4.0.0)
49
+ ast (~> 2.2)
50
+ powerpack (0.1.1)
51
+ pry (0.10.4-java)
52
+ coderay (~> 1.1.0)
53
+ method_source (~> 0.8.1)
54
+ slop (~> 3.4)
55
+ spoon (~> 0.0)
56
+ rainbow (2.2.1)
57
+ rake (12.0.0)
58
+ rspec (3.5.0)
59
+ rspec-core (~> 3.5.0)
60
+ rspec-expectations (~> 3.5.0)
61
+ rspec-mocks (~> 3.5.0)
62
+ rspec-core (3.5.4)
63
+ rspec-support (~> 3.5.0)
64
+ rspec-expectations (3.5.0)
65
+ diff-lcs (>= 1.2.0, < 2.0)
66
+ rspec-support (~> 3.5.0)
67
+ rspec-mocks (3.5.0)
68
+ diff-lcs (>= 1.2.0, < 2.0)
69
+ rspec-support (~> 3.5.0)
70
+ rspec-support (3.5.0)
71
+ rubocop (0.47.1)
72
+ parser (>= 2.3.3.1, < 3.0)
73
+ powerpack (~> 0.1)
74
+ rainbow (>= 1.99.1, < 3.0)
75
+ ruby-progressbar (~> 1.7)
76
+ unicode-display_width (~> 1.0, >= 1.0.1)
77
+ ruby-progressbar (1.8.1)
78
+ simplecov (0.13.0)
79
+ docile (~> 1.1.0)
80
+ json (>= 1.8, < 3)
81
+ simplecov-html (~> 0.10.0)
82
+ simplecov-html (0.10.0)
83
+ slop (3.6.0)
84
+ spoon (0.0.6)
85
+ ffi
86
+ unicode-display_width (1.1.3)
87
+
88
+ PLATFORMS
89
+ java
90
+
91
+ DEPENDENCIES
92
+ bundler (~> 1.14)
93
+ hucpa (~> 0)
94
+ jdbc!
95
+ jdbc-postgres (~> 9.4)
96
+ pry (~> 0)
97
+ rake (~> 12.0)
98
+ rspec (~> 3.5)
99
+ rubocop (~> 0)
100
+ simplecov (~> 0)
101
+
102
+ BUNDLED WITH
103
+ 1.14.5
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Tomek Wałkuski
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,246 @@
1
+ # JDBC
2
+
3
+ [![Code Climate](https://codeclimate.com/github/tomekw/jdbc/badges/gpa.svg)](https://codeclimate.com/github/tomekw/jdbc) [![Gem Version](https://badge.fury.io/rb/jdbc.svg)](https://badge.fury.io/rb/jdbc) [![CircleCI](https://circleci.com/gh/tomekw/jdbc.svg?style=svg)](https://circleci.com/gh/tomekw/jdbc)
4
+
5
+ JDBC meets JRuby.
6
+
7
+ Please note the project supports only JRuby (tested with 9.1.7.0+) on Java 8.
8
+
9
+ The public API is subject to change before version `1.0.0`.
10
+
11
+ The library is in the alpha state. It has been tested only with the `PostgreSQL` database, only basic queries and commands were tested.
12
+ Please raise issues if any of your use cases is not working and I will be very happy to help!
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem "jdbc"
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install jdbc
29
+
30
+ ## Public API
31
+
32
+ | Method | Description | Returned value |
33
+ | ----------------------------- | ------------------------------------------- | ----------------------------------------------------------- |
34
+ | `command(sql, bindings = {})` | `INSERT`s, `UPDATE`s, `DELETE`s, etc. calls | Depending on the driver, either full record or primary keys |
35
+ | `ddl(sql, bindings = {})` | `CREATE TABLES`s, `ADD INDEX`es, etc. calls | `true` or driver exception |
36
+ | `query(sql, bindings = {})` | `SELECT`s, etc. calls | Ruby `Array` of `Hashes` |
37
+
38
+ ## Usage
39
+
40
+ Install the database driver, for PostgreSQL:
41
+
42
+ ```ruby
43
+ gem "jdbc-postgres"
44
+ ```
45
+
46
+ Load the the database driver if needed, for PostgreSQL:
47
+
48
+ ```ruby
49
+ require "jdbc/postgres"
50
+ Jdbc::Postgres.load_driver
51
+ ```
52
+
53
+ Configure the connection pool:
54
+
55
+ ```ruby
56
+ gem "hucpa"
57
+ ```
58
+
59
+ ```ruby
60
+ require "hucpa"
61
+
62
+ # Using the adapter option
63
+ options = {
64
+ adapter: :postgresql,
65
+ database_name: "jdbc",
66
+ password: "jdbc",
67
+ server_name: "postgres",
68
+ username: "jdbc"
69
+ }
70
+
71
+ # Using the jdbc_url option
72
+ options = {
73
+ jdbc_url: "jdbc:postgresql://postgres/jdbc",
74
+ password: "jdbc",
75
+ username: "jdbc"
76
+ }
77
+
78
+ connection_pool = Hucpa::ConnectionPool.new(options)
79
+
80
+ gateway = JDBC::Gateway.new(connection_pool: connection_pool)
81
+ ```
82
+
83
+ Query for records:
84
+
85
+ ```ruby
86
+ gateway.query("SELECT * FROM things")
87
+ => [
88
+ {
89
+ id: 1,
90
+ name: "Foo",
91
+ created_at: Time.parse("2017-02-01 10:20:45")
92
+ },
93
+ {
94
+ id: 2,
95
+ name: "Bar",
96
+ created_at: Time.parse("2017-02-01 10:21:47")
97
+ }
98
+ ]
99
+ ```
100
+
101
+ Query bindings can be provided:
102
+
103
+ ```ruby
104
+ gateway.query("SELECT * FROM things WHERE name = :name", name: "Foo")
105
+ => [
106
+ {
107
+ id: 1,
108
+ name: "Foo",
109
+ created_at: Time.parse("2017-02-01 10:20:45")
110
+ }
111
+ ]
112
+ ```
113
+
114
+ Optionally, bindings can be annotated with a [JDBC type](#jdbc-types).
115
+ It is in fact required when value can be `nil`:
116
+
117
+ ```ruby
118
+ gateway.query("SELECT * FROM things WHERE name = :name:VARCHAR OR (name IS NULL AND :name:VARCHAR IS NULL)", name: nil)
119
+ => [
120
+ {
121
+ id: 3,
122
+ name: nil,
123
+ created_at: Time.parse("2017-02-02 10:20:45")
124
+ }
125
+ ]
126
+ ```
127
+
128
+ Pass commands:
129
+
130
+ ```ruby
131
+ gateway.command("INSERT INTO things (name, created_at) VALUES (:name, :created_at)", name: "Foo", created_at: Time.parse("2017-02-02 10:20:45"))
132
+ => [
133
+ {
134
+ id: 4,
135
+ name: "Foo",
136
+ created_at: Time.parse("2017-02-02 10:20:45")
137
+ }
138
+ ]
139
+ ```
140
+
141
+ ```ruby
142
+ gateway.command("UPDATE things SET name = :name WHERE id < :id", name: "Bar", id: 2)
143
+ => [
144
+ {
145
+ id: 1,
146
+ name: "Bar",
147
+ created_at: Time.parse("2017-02-02 10:20:45")
148
+ }
149
+ ]
150
+ ```
151
+
152
+ ```ruby
153
+ gateway.command("DELETE FROM things WHERE id = :id", id: 1)
154
+ => [
155
+ {
156
+ id: 1,
157
+ name: "Bar",
158
+ created_at: Time.parse("2017-02-02 10:20:45")
159
+ }
160
+ ]
161
+ ```
162
+
163
+ Invoke DDL calls:
164
+
165
+ ```ruby
166
+ gateway.ddl("CREATE INDEX name_idx ON things(name)")
167
+ => true
168
+ ```
169
+
170
+ Close the connection pool:
171
+
172
+ ```ruby
173
+ connection_pool.close
174
+ ```
175
+
176
+ ## (Known) things that won't work (yet)
177
+
178
+ * groupping SQL queries / commands in transactions
179
+
180
+ ## JDBC types
181
+
182
+ * `ARRAY`
183
+ * `BIGINT`
184
+ * `BINARY`
185
+ * `BIT`
186
+ * `BLOB`
187
+ * `BOOLEAN`
188
+ * `CHAR`
189
+ * `CLOB`
190
+ * `DATALINK`
191
+ * `DATE`
192
+ * `DECIMAL`
193
+ * `DISTINCT`
194
+ * `DOUBLE`
195
+ * `FLOAT`
196
+ * `INTEGER`
197
+ * `JAVA_OBJECT`
198
+ * `LONGNVARCHAR`
199
+ * `LONGVARBINARY`
200
+ * `LONGVARCHAR`
201
+ * `NCHAR`
202
+ * `NCLOB`
203
+ * `NULL`
204
+ * `NUMERIC`
205
+ * `NVARCHAR`
206
+ * `OTHER`
207
+ * `REAL`
208
+ * `REF`
209
+ * `REF_CURSOR`
210
+ * `ROWID`
211
+ * `SMALLINT`
212
+ * `SQLXML`
213
+ * `STRUCT`
214
+ * `TIME`
215
+ * `TIME_WITH_TIMEZONE`
216
+ * `TIMESTAMP`
217
+ * `TIMESTAMP_WITH_TIMEZONE`
218
+ * `TINYINT`
219
+ * `VARBINARY`
220
+ * `VARCHAR`
221
+
222
+ ## Development
223
+
224
+ Build the Docker image:
225
+
226
+ $ docker-compose build
227
+
228
+ Create services:
229
+
230
+ $ docker-compose create
231
+
232
+ Run specs:
233
+
234
+ $ docker-compose run --rm app rspec spec
235
+
236
+ Run console:
237
+
238
+ $ docker-compose run --rm app irb
239
+
240
+ ## Contributing
241
+
242
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tomekw/jdbc. 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.
243
+
244
+ ## License
245
+
246
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "rubocop/rake_task"
4
+
5
+ load "lib/tasks/ci.rake"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ RuboCop::RakeTask.new(:rubocop)
9
+
10
+ task default: :spec
@@ -0,0 +1,17 @@
1
+ machine:
2
+ services:
3
+ - docker
4
+
5
+ database:
6
+ override:
7
+ - psql -U ubuntu -c "CREATE USER jdbc WITH SUPERUSER PASSWORD 'jdbc'"
8
+ - psql -U ubuntu -c "CREATE DATABASE jdbc OWNER jdbc"
9
+
10
+ dependencies:
11
+ override:
12
+ - docker build --rm=false -t tomekw/jdbc .
13
+
14
+ test:
15
+ override:
16
+ - docker run --net=host --env DATABASE_HOST=localhost --rm tomekw/jdbc rubocop
17
+ - docker run --net=host --env DATABASE_HOST=localhost --rm tomekw/jdbc rspec spec
@@ -0,0 +1,22 @@
1
+ version: "2"
2
+ services:
3
+ app:
4
+ build: .
5
+ depends_on:
6
+ - postgres
7
+ environment:
8
+ JRUBY_OPTS: --debug
9
+ volumes:
10
+ - .:/jdbc
11
+ - ${HOME}/.ssh:/root/.ssh:ro
12
+ - ${HOME}/.gem:/root/.gem:rw
13
+ postgres:
14
+ environment:
15
+ POSTGRES_USER: jdbc
16
+ POSTGRES_PASSWORD: jdbc
17
+ image: postgres:9.6.2
18
+ volumes:
19
+ - postgres:/var/lib/postgresql/data
20
+ volumes:
21
+ postgres:
22
+ driver: local
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "jdbc"
6
+ spec.version = "0.1.1"
7
+ spec.authors = ["Tomek Wałkuski"]
8
+ spec.email = "ja@jestem.tw"
9
+
10
+ spec.summary = "JDBC meets JRuby"
11
+ spec.homepage = "https://github.com/tomekw/jdbc"
12
+ spec.license = "MIT"
13
+
14
+ spec.platform = "java"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.require_paths = %w[lib]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.14"
22
+ spec.add_development_dependency "hucpa", "~> 0"
23
+ spec.add_development_dependency "jdbc-postgres", "~> 9.4"
24
+ spec.add_development_dependency "pry", "~> 0"
25
+ spec.add_development_dependency "rake", "~> 12.0"
26
+ spec.add_development_dependency "rspec", "~> 3.5"
27
+ spec.add_development_dependency "rubocop", "~> 0"
28
+ spec.add_development_dependency "simplecov", "~> 0"
29
+ end
@@ -0,0 +1,15 @@
1
+ require "jdbc/action"
2
+ require "jdbc/coercions"
3
+ require "jdbc/column"
4
+ require "jdbc/command"
5
+ require "jdbc/ddl"
6
+ require "jdbc/gateway"
7
+ require "jdbc/prepared_statement_builder"
8
+ require "jdbc/query"
9
+ require "jdbc/result_set_meta_data"
10
+ require "jdbc/result_set_transformer"
11
+ require "jdbc/sql_parser"
12
+ require "jdbc/uuid"
13
+
14
+ module JDBC
15
+ end
@@ -0,0 +1,32 @@
1
+ module JDBC
2
+ class Action
3
+ def initialize(connection:, sql:, bindings:)
4
+ @connection = connection
5
+ @sql = sql
6
+ @bindings = bindings
7
+ end
8
+
9
+ def run
10
+ ResultSetTransformer.new(result_set: result_set).transform
11
+ ensure
12
+ result_set&.close
13
+ statement&.close
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :connection, :sql, :bindings
19
+
20
+ def parsed_sql
21
+ @parsed_sql ||= SqlParser.new(sql: sql, bindings: bindings).parse
22
+ end
23
+
24
+ def jdbc_sql
25
+ parsed_sql.first
26
+ end
27
+
28
+ def binding_values
29
+ parsed_sql.last
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ module JDBC
2
+ module Coercions
3
+ def identity
4
+ @identity ||= ->(value) { value }
5
+ end
6
+ module_function :identity
7
+
8
+ def to_s
9
+ @to_s ||= ->(value) { value.to_s }
10
+ end
11
+ module_function :to_s
12
+
13
+ def to_time
14
+ @to_time ||= ->(value) { Time.parse(value.to_s) }
15
+ end
16
+ module_function :to_time
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ module JDBC
2
+ class Column
3
+ attr_reader :index, :label, :jdbc_type
4
+
5
+ def initialize(index:, label:, jdbc_type:)
6
+ @index = index
7
+ @label = label
8
+ @jdbc_type = jdbc_type
9
+ end
10
+
11
+ def coerce_proc
12
+ Coercions.public_send(COERCIONS_MAP.fetch(jdbc_type, :identity))
13
+ end
14
+
15
+ COERCIONS_MAP = {
16
+ uuid: :to_s,
17
+ timestamp: :to_time
18
+ }.freeze
19
+ private_constant :COERCIONS_MAP
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module JDBC
2
+ class Command < Action
3
+ private
4
+
5
+ def result_set
6
+ @result_set ||=
7
+ begin
8
+ statement.execute_update
9
+ statement.get_generated_keys
10
+ end
11
+ end
12
+
13
+ def statement
14
+ @statement ||= PreparedStatementBuilder.for_command(
15
+ connection: connection,
16
+ jdbc_sql: jdbc_sql,
17
+ binding_values: binding_values
18
+ )
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module JDBC
2
+ class DDL < Action
3
+ def run
4
+ result_set.zero?
5
+ end
6
+
7
+ private
8
+
9
+ def result_set
10
+ @result_set ||= statement.execute_update
11
+ end
12
+
13
+ def statement
14
+ @statement ||= PreparedStatementBuilder.for_query(
15
+ connection: connection,
16
+ jdbc_sql: jdbc_sql,
17
+ binding_values: binding_values
18
+ )
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ module JDBC
2
+ class Gateway
3
+ def initialize(connection_pool:)
4
+ @connection_pool = connection_pool
5
+ end
6
+
7
+ %w[Command DDL Query].each do |action_name|
8
+ define_method(action_name.downcase) do |sql, bindings = {}|
9
+ connection_pool.with_connection do |connection|
10
+ JDBC
11
+ .const_get(action_name)
12
+ .new(connection: connection, sql: sql, bindings: bindings)
13
+ .run
14
+ end
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :connection_pool
21
+ end
22
+ end
@@ -0,0 +1,89 @@
1
+ module JDBC
2
+ class PreparedStatementBuilder
3
+ def initialize(statement:, binding_values:)
4
+ @statement = statement
5
+ @binding_values = binding_values
6
+ end
7
+
8
+ def self.for_command(connection:, jdbc_sql:, binding_values:)
9
+ new(
10
+ statement: connection.prepare_statement(jdbc_sql, java.sql.Statement::RETURN_GENERATED_KEYS),
11
+ binding_values: binding_values
12
+ ).build
13
+ end
14
+
15
+ def self.for_query(connection:, jdbc_sql:, binding_values:)
16
+ new(
17
+ statement: connection.prepare_statement(jdbc_sql),
18
+ binding_values: binding_values
19
+ ).build
20
+ end
21
+
22
+ def build
23
+ binding_values.each_with_index do |(value, type), index|
24
+ method_name, method_parameters = ParameterSetter.new(
25
+ index: index,
26
+ value: value,
27
+ type: type
28
+ ).build
29
+
30
+ statement.public_send(method_name, *method_parameters)
31
+ end
32
+
33
+ statement
34
+ end
35
+
36
+ private
37
+
38
+ attr_reader :statement, :binding_values
39
+
40
+ class ParameterSetter
41
+ def initialize(index:, value:, type:)
42
+ @index = index
43
+ @value = value
44
+ @type = type
45
+ end
46
+
47
+ def build
48
+ [method_name, method_parameters]
49
+ end
50
+
51
+ private
52
+
53
+ attr_reader :index, :value, :type
54
+
55
+ def method_name
56
+ value.nil? ? :set_null : :set_object
57
+ end
58
+
59
+ def method_parameters
60
+ if value.nil?
61
+ [parameter_index, jdbc_type || java.sql.Types::NULL]
62
+ else
63
+ [parameter_index, java_value, jdbc_type].compact
64
+ end
65
+ end
66
+
67
+ def parameter_index
68
+ index + 1
69
+ end
70
+
71
+ def jdbc_type
72
+ type ? java.sql.Types.const_get(type) : nil
73
+ end
74
+
75
+ def uuid_value
76
+ @uuid_value ||= UUID.new(value)
77
+ end
78
+
79
+ def java_value
80
+ if uuid_value.valid?
81
+ uuid_value
82
+ else
83
+ value
84
+ end.to_java
85
+ end
86
+ end
87
+ private_constant :ParameterSetter
88
+ end
89
+ end
@@ -0,0 +1,17 @@
1
+ module JDBC
2
+ class Query < Action
3
+ private
4
+
5
+ def result_set
6
+ @result_set ||= statement.execute_query
7
+ end
8
+
9
+ def statement
10
+ @statement ||= PreparedStatementBuilder.for_query(
11
+ connection: connection,
12
+ jdbc_sql: jdbc_sql,
13
+ binding_values: binding_values
14
+ )
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ module JDBC
2
+ class ResultSetMetaData
3
+ def initialize(result_set:)
4
+ @result_set = result_set
5
+ end
6
+
7
+ def parse
8
+ columns_range.map do |index|
9
+ Column.new(
10
+ index: index,
11
+ label: meta_data.get_column_label(index).to_sym,
12
+ jdbc_type: meta_data.get_column_type_name(index).to_sym
13
+ )
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :result_set
20
+
21
+ def meta_data
22
+ @meta_data ||= result_set.meta_data
23
+ end
24
+
25
+ def columns_range
26
+ 1..meta_data.column_count
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,26 @@
1
+ module JDBC
2
+ class ResultSetTransformer
3
+ def initialize(result_set:)
4
+ @result_set = result_set
5
+ end
6
+
7
+ def transform
8
+ [].tap do |results|
9
+ while result_set.next
10
+ results << meta_data.each_with_object({}) do |column, record|
11
+ record[column.label] =
12
+ column.coerce_proc.call(result_set.get_object(column.index))
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :result_set
21
+
22
+ def meta_data
23
+ @meta_data ||= ResultSetMetaData.new(result_set: result_set).parse
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JDBC
4
+ class SqlParser
5
+ def initialize(sql:, bindings:)
6
+ @sql = sql
7
+ @bindings = bindings
8
+ end
9
+
10
+ def parse
11
+ fail ArgumentError, sql_binding_mismatch_msg if sql_binding_mismatch?
12
+
13
+ [jdbc_sql, binding_values]
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :sql, :bindings
19
+
20
+ JAVA_SQL_TYPES = %w[
21
+ ARRAY
22
+ BIGINT
23
+ BINARY
24
+ BIT
25
+ BLOB
26
+ BOOLEAN
27
+ CHAR
28
+ CLOB
29
+ DATALINK
30
+ DATE
31
+ DECIMAL
32
+ DISTINCT
33
+ DOUBLE
34
+ FLOAT
35
+ INTEGER
36
+ JAVA_OBJECT
37
+ LONGNVARCHAR
38
+ LONGVARBINARY
39
+ LONGVARCHAR
40
+ NCHAR
41
+ NCLOB
42
+ NULL
43
+ NUMERIC
44
+ NVARCHAR
45
+ OTHER
46
+ REAL
47
+ REF
48
+ REF_CURSOR
49
+ ROWID
50
+ SMALLINT
51
+ SQLXML
52
+ STRUCT
53
+ TIME
54
+ TIME_WITH_TIMEZONE
55
+ TIMESTAMP
56
+ TIMESTAMP_WITH_TIMEZONE
57
+ TINYINT
58
+ VARBINARY
59
+ VARCHAR
60
+ ].freeze
61
+ private_constant :JAVA_SQL_TYPES
62
+
63
+ COLON = ":"
64
+ private_constant :COLON
65
+
66
+ EMPTY_STRING = ""
67
+ private_constant :EMPTY_STRING
68
+
69
+ NONE = "(none)"
70
+ private_constant :NONE
71
+
72
+ JOIN_COMMA = ", "
73
+ private_constant :JOIN_COMMA
74
+
75
+ LEFT_PAREN = "("
76
+ private_constant :LEFT_PAREN
77
+
78
+ RIGHT_PAREN = ")"
79
+ private_constant :RIGHT_PAREN
80
+
81
+ PIPE = "|"
82
+ private_constant :PIPE
83
+
84
+ QUESTION_MARK = "?"
85
+ private_constant :QUESTION_MARK
86
+
87
+ SQL_TAGS_REGEX = /(?:[\s\(]):(\w+)(?::(#{JAVA_SQL_TYPES.join(PIPE)}))?(?:[\s,;\)]|\z)/
88
+ private_constant :SQL_TAGS_REGEX
89
+
90
+ TAG_CLEANUP_REGEX = /#{COLON}(#{JAVA_SQL_TYPES.reverse.join(PIPE)})?/
91
+ private_constant :TAG_CLEANUP_REGEX
92
+
93
+ def binding_names
94
+ @binding_names ||= bindings.keys.map(&:to_sym)
95
+ end
96
+
97
+ def binding_values
98
+ sql_tags.each_with_object([]) do |(tag, type), values|
99
+ values << [bindings.fetch(tag)].flatten.map { |value| [value, type] }
100
+ end.flatten(1)
101
+ end
102
+
103
+ def sql_tag_names
104
+ @sql_tag_names ||= sql_tags.map(&:first)
105
+ end
106
+
107
+ def sql_tags
108
+ @sql_tags ||= sql.scan(SQL_TAGS_REGEX).map do |(tag, type)|
109
+ [tag.to_sym, type]
110
+ end
111
+ end
112
+
113
+ def replace_sql_tags_regex
114
+ @replace_sql_tags_regex ||= /:(#{sql_tags.map { |t| t.compact.join(COLON) }.join(PIPE) })/
115
+ end
116
+
117
+ def jdbc_sql
118
+ sql_tags.empty? ? sql : parsed_sql
119
+ end
120
+
121
+ def parsed_sql
122
+ sql.gsub(replace_sql_tags_regex) do |tag|
123
+ tag_name = tag.gsub(TAG_CLEANUP_REGEX, EMPTY_STRING).to_sym
124
+ tag_value = bindings.fetch(tag_name)
125
+
126
+ if tag_value.is_a?(Array)
127
+ LEFT_PAREN + tag_value.map { QUESTION_MARK }.join(JOIN_COMMA) + RIGHT_PAREN
128
+ else
129
+ QUESTION_MARK
130
+ end
131
+ end
132
+ end
133
+
134
+ def sql_binding_mismatch?
135
+ binding_names.sort != sql_tag_names.uniq.sort
136
+ end
137
+
138
+ def sql_binding_mismatch_msg
139
+ given = binding_names.join(", ")
140
+ expected = sql_tag_names.join(", ")
141
+
142
+ given_msg = given.empty? ? NONE : given
143
+ expected_msg = expected.empty? ? NONE : expected
144
+
145
+ "SQL query bindings mismatch, given: #{given_msg}, expected: #{expected_msg}"
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,22 @@
1
+ module JDBC
2
+ class UUID
3
+ def initialize(value)
4
+ @value = value.to_s
5
+ end
6
+
7
+ def to_java
8
+ java.util.UUID.from_string(value)
9
+ end
10
+
11
+ def valid?
12
+ !UUID_REGEX.match(value).nil?
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :value
18
+
19
+ UUID_REGEX = /^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
20
+ private_constant :UUID_REGEX
21
+ end
22
+ end
@@ -0,0 +1,8 @@
1
+ require "rubocop/rake_task"
2
+
3
+ namespace :ci do
4
+ RuboCop::RakeTask.new(:rubocop)
5
+
6
+ desc "Run all CI checks"
7
+ task run: %i[rubocop spec]
8
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jdbc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: java
6
+ authors:
7
+ - Tomek Wałkuski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.14'
19
+ name: bundler
20
+ prerelease: false
21
+ type: :development
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ name: hucpa
34
+ prerelease: false
35
+ type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '9.4'
47
+ name: jdbc-postgres
48
+ prerelease: false
49
+ type: :development
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '9.4'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ name: pry
62
+ prerelease: false
63
+ type: :development
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '12.0'
75
+ name: rake
76
+ prerelease: false
77
+ type: :development
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '12.0'
83
+ - !ruby/object:Gem::Dependency
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.5'
89
+ name: rspec
90
+ prerelease: false
91
+ type: :development
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.5'
97
+ - !ruby/object:Gem::Dependency
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ name: rubocop
104
+ prerelease: false
105
+ type: :development
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ requirement: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ name: simplecov
118
+ prerelease: false
119
+ type: :development
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email: ja@jestem.tw
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - ".gitignore"
132
+ - ".irbrc"
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - CHANGELOG.md
136
+ - CODE_OF_CONDUCT.md
137
+ - Dockerfile
138
+ - Gemfile
139
+ - Gemfile.lock
140
+ - LICENSE.txt
141
+ - README.md
142
+ - Rakefile
143
+ - circle.yml
144
+ - docker-compose.yml
145
+ - jdbc.gemspec
146
+ - lib/jdbc.rb
147
+ - lib/jdbc/action.rb
148
+ - lib/jdbc/coercions.rb
149
+ - lib/jdbc/column.rb
150
+ - lib/jdbc/command.rb
151
+ - lib/jdbc/ddl.rb
152
+ - lib/jdbc/gateway.rb
153
+ - lib/jdbc/prepared_statement_builder.rb
154
+ - lib/jdbc/query.rb
155
+ - lib/jdbc/result_set_meta_data.rb
156
+ - lib/jdbc/result_set_transformer.rb
157
+ - lib/jdbc/sql_parser.rb
158
+ - lib/jdbc/uuid.rb
159
+ - lib/tasks/ci.rake
160
+ homepage: https://github.com/tomekw/jdbc
161
+ licenses:
162
+ - MIT
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.6.8
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: JDBC meets JRuby
184
+ test_files: []