sequel-jdbc-crate 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +202 -0
- data/README.md +38 -0
- data/examples.rb +412 -0
- data/lib/sequel/adapters/jdbc/crate.rb +112 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7a93cd4c356053adaba3ae20e45c19c5aa8dc630
|
4
|
+
data.tar.gz: 3ec6287574f9524eddafa4d0f33dfd67723f0902
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8559b8df8cc84c152755abd7b1a4a11929467bbe3480221143941c0106ba7fd43842fbc5f9fe57e8dd79c7a937bdc5ba1bfce3d63e4f79361869a525b01f0866
|
7
|
+
data.tar.gz: 56e6640658beecfbbd3a31238cc13ca87797635bc6b53df994abcf01b41b5cb9198336d96084a9ce18472aa1b2916b1880ab30201fdd204f2dffe95af874ffd7
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# sequel-jdbc-crate
|
2
|
+
|
3
|
+
Adapter for Sequel using the JDBC driver for connecting to Crate.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Example how to connect
|
8
|
+
|
9
|
+
require 'sequel'
|
10
|
+
require 'logger'
|
11
|
+
DB = Sequel.connect('jdbc:crate://localhost:4300')
|
12
|
+
DB.loggers << ::Logger.new($stdout)
|
13
|
+
|
14
|
+
## Examples
|
15
|
+
|
16
|
+
Please take a look at the [examples.rb](examples.rb) file for all the examples.
|
17
|
+
If you get `Java::IoCrateActionSql::SQLActionException: org.elasticsearch.index.shard.IllegalIndexShardStateException`
|
18
|
+
then just run the command again.
|
19
|
+
|
20
|
+
To run the examples with RVM
|
21
|
+
|
22
|
+
$ rvm jruby@sequel-jdbc-crate --create
|
23
|
+
$ bundle install --path vendor --binstubs
|
24
|
+
$ bundle exec ruby -Ilib examples.rb
|
25
|
+
|
26
|
+
## Exceptions
|
27
|
+
|
28
|
+
Currently, raised exception class is Java::IoCrateActionSql::SQLActionException
|
29
|
+
|
30
|
+
def safe_drop(table)
|
31
|
+
DB.execute(%Q(DROP TABLE "#{table}"))
|
32
|
+
rescue Java::IoCrateActionSql::SQLActionException
|
33
|
+
#no problem,the table didnt exist
|
34
|
+
end
|
35
|
+
|
36
|
+
## License
|
37
|
+
|
38
|
+
Crate JDBC is distributed under the Apache License 2.0, see *LICENSE.txt*.
|
data/examples.rb
ADDED
@@ -0,0 +1,412 @@
|
|
1
|
+
|
2
|
+
# Examples taken mostly from
|
3
|
+
# http://sequel.jeremyevans.net/rdoc/files/README_rdoc.html
|
4
|
+
# to see how much features the driver can do compared to mysql and postgresql
|
5
|
+
|
6
|
+
##############
|
7
|
+
# Connecting #
|
8
|
+
##############
|
9
|
+
require 'securerandom'
|
10
|
+
require 'logger'
|
11
|
+
require 'sequel'
|
12
|
+
require 'sequel/adapters/jdbc/crate'
|
13
|
+
|
14
|
+
DB = Sequel.connect('jdbc:crate://localhost:4300')
|
15
|
+
DB.loggers << Logger.new($stdout)
|
16
|
+
|
17
|
+
###########
|
18
|
+
# Helpers #
|
19
|
+
###########
|
20
|
+
def safe_drop(table)
|
21
|
+
DB.execute(%Q(DROP TABLE "#{table}"))
|
22
|
+
rescue Sequel::DatabaseError
|
23
|
+
#no problem, the table didnt exist
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
#################
|
28
|
+
# Does it work? #
|
29
|
+
#################
|
30
|
+
|
31
|
+
DB["SELECT * FROM sys.nodes"].each { |node| p node[:name] }
|
32
|
+
|
33
|
+
###################
|
34
|
+
# A short example #
|
35
|
+
###################
|
36
|
+
|
37
|
+
safe_drop(:items)
|
38
|
+
DB.create_table :items do
|
39
|
+
primary_key :id
|
40
|
+
String :name
|
41
|
+
Float :price
|
42
|
+
end
|
43
|
+
|
44
|
+
items = DB[:items] # Create a dataset
|
45
|
+
|
46
|
+
# Populate the table
|
47
|
+
items.insert(:id => SecureRandom.uuid, :name => 'abc', :price => rand * 100)
|
48
|
+
items.insert(:id => SecureRandom.uuid, :name => 'def', :price => rand * 100)
|
49
|
+
items.insert(:id => SecureRandom.uuid, :name => 'ghi', :price => rand * 100)
|
50
|
+
DB.run('REFRESH TABLE items')
|
51
|
+
|
52
|
+
# Print out the number of records
|
53
|
+
puts "Item count: #{items.count}"
|
54
|
+
|
55
|
+
# Print out the average price
|
56
|
+
puts "The average price is: #{items.avg(:price)}"
|
57
|
+
|
58
|
+
#########################
|
59
|
+
# Arbitrary SQL queries #
|
60
|
+
#########################
|
61
|
+
|
62
|
+
#You can execute arbitrary SQL code using Database#run:
|
63
|
+
DB.run("create table t (a string, b string) with (number_of_replicas = '0-all')")
|
64
|
+
DB.run("insert into t values ('a', 'b')")
|
65
|
+
DB.run("drop table t")
|
66
|
+
|
67
|
+
## NOT SUPPORTED: ERROR -- : Java::IoCrateActionSql::SQLActionException: Unsupported statement.: SELECT count(*) AS "count" FROM (select id from items) AS "t1" LIMIT 1
|
68
|
+
##You can also create datasets based on raw SQL:
|
69
|
+
##dataset = DB['select id from items']
|
70
|
+
#dataset.count # will return the number of records in the result set
|
71
|
+
#dataset.map(:id) # will return an array containing all values of the id column in the result set
|
72
|
+
|
73
|
+
#You can also fetch records with raw SQL through the dataset:
|
74
|
+
DB['select * from items'].each do |row|
|
75
|
+
p row
|
76
|
+
end
|
77
|
+
|
78
|
+
#You can use placeholders in your SQL string as well:
|
79
|
+
name = 'abc'
|
80
|
+
DB['select * from items where name = ?', name].each do |row|
|
81
|
+
p row
|
82
|
+
end
|
83
|
+
|
84
|
+
#############################
|
85
|
+
# Getting Dataset Instances #
|
86
|
+
#############################
|
87
|
+
|
88
|
+
safe_drop(:posts)
|
89
|
+
DB.create_table :posts do
|
90
|
+
primary_key :id
|
91
|
+
String :name
|
92
|
+
String :title
|
93
|
+
String :category
|
94
|
+
String :author
|
95
|
+
String :updated_by
|
96
|
+
Date :date
|
97
|
+
String :state
|
98
|
+
Timestamp :stamp
|
99
|
+
Integer :backup_number
|
100
|
+
Integer :num_comments
|
101
|
+
Boolean :visible
|
102
|
+
Date :written_on
|
103
|
+
end
|
104
|
+
|
105
|
+
posts = DB.from(:posts)
|
106
|
+
posts = DB[:posts] # same
|
107
|
+
|
108
|
+
posts.insert(:id => SecureRandom.uuid,
|
109
|
+
:name => 'abc',
|
110
|
+
:date => Time.now.utc.to_date, # Dates need to be converted to utc first manually, if you care about it.
|
111
|
+
:category => 'linux',
|
112
|
+
:author => 'JKR',
|
113
|
+
:stamp => Time.now) # Times don't need to be in utc. crate handles the conversion internally.
|
114
|
+
DB.run('REFRESH TABLE posts')
|
115
|
+
|
116
|
+
######################
|
117
|
+
# Retrieving Records #
|
118
|
+
######################
|
119
|
+
|
120
|
+
#If the dataset is ordered, you can also ask for the last record:
|
121
|
+
p posts.order(:stamp).last
|
122
|
+
|
123
|
+
######################
|
124
|
+
# Filtering Records #
|
125
|
+
######################
|
126
|
+
|
127
|
+
posts.where(:stamp => (Date.today - 14)..(Date.today + 1)).each {|post| p post[:stamp] }
|
128
|
+
|
129
|
+
posts.where(:category => ['ruby', 'postgres', 'linux']).each {|post| p post[:category] }
|
130
|
+
|
131
|
+
posts.where{stamp > Date.today << 1}.each {|post| p post[:stamp] }
|
132
|
+
|
133
|
+
##this doesnt work yet but maybe it can be made to work
|
134
|
+
##fails with Sequel::InvalidOperation: Pattern matching via regular expressions is not supported on
|
135
|
+
## maybe will work with this https://crate.io/docs/stable/sql/analyzer.html?highlight=regular#pattern
|
136
|
+
#posts.where(:category => /linux/i).each {|post| p post[:stamp] }
|
137
|
+
|
138
|
+
|
139
|
+
posts.exclude(:category => ['ruby', 'postgres', 'linux']).each {|post| p post[:category] }
|
140
|
+
|
141
|
+
posts.where('stamp IS NOT NULL').first
|
142
|
+
|
143
|
+
author_name = 'JKR'
|
144
|
+
posts.where('(stamp < ?) AND (author != ?)', Date.today - 3, author_name).first
|
145
|
+
|
146
|
+
## NOT SUPPORTED ERROR -- : Java::IoCrateActionSql::SQLActionException: Unsupported statement.: SELECT * FROM "items" WHERE (price > (SELECT (avg("price") + 100) FROM "items")) LIMIT 1
|
147
|
+
# DB[:items].where('price > ?', DB[:items].select{avg(price) + 100}).first
|
148
|
+
|
149
|
+
|
150
|
+
#######################
|
151
|
+
# Summarizing Records #
|
152
|
+
#######################
|
153
|
+
|
154
|
+
posts.where(Sequel.like(:category,'%ruby%')).count
|
155
|
+
|
156
|
+
puts items.max(:price)
|
157
|
+
puts items.min(:price)
|
158
|
+
puts items.sum(:price)
|
159
|
+
puts items.avg(:price)
|
160
|
+
|
161
|
+
####################
|
162
|
+
# Ordering Records #
|
163
|
+
####################
|
164
|
+
|
165
|
+
posts.order(:stamp).all
|
166
|
+
posts.order(:stamp, :name).all
|
167
|
+
posts.order(:stamp).order_prepend(:name).all
|
168
|
+
posts.reverse_order(:stamp).all
|
169
|
+
posts.order(Sequel.desc(:stamp)).all
|
170
|
+
|
171
|
+
#####################
|
172
|
+
# Selecting Columns #
|
173
|
+
#####################
|
174
|
+
|
175
|
+
posts.select(:stamp).all
|
176
|
+
posts.select(:stamp, :name).all
|
177
|
+
posts.select(:stamp).select_append(:name)
|
178
|
+
|
179
|
+
####################
|
180
|
+
# Deleting Records #
|
181
|
+
####################
|
182
|
+
|
183
|
+
posts.where('stamp < ?', Date.today - 3).delete
|
184
|
+
|
185
|
+
#####################
|
186
|
+
# Inserting Records #
|
187
|
+
#####################
|
188
|
+
|
189
|
+
posts.insert(:id => SecureRandom.uuid, :category => 'ruby', :author => 'jeremy')
|
190
|
+
|
191
|
+
####################
|
192
|
+
# Updating Records #
|
193
|
+
####################
|
194
|
+
|
195
|
+
posts.where('stamp < ?', Date.today - 7).update(:state => 'archived')
|
196
|
+
|
197
|
+
# Didnt work. ERROR -- : Java::IoCrateActionSql::SQLActionException: Validation failed for backup_number: Invalid value of type 'FUNCTION': UPDATE "posts" SET "backup_number" = ("backup_number" + 1) WHERE ("stamp" < '2014-08-24')
|
198
|
+
#posts.where{|o| o.stamp < Date.today - 7}.update(:backup_number => Sequel.+(:backup_number, 1))
|
199
|
+
|
200
|
+
################
|
201
|
+
# Transactions #
|
202
|
+
################
|
203
|
+
|
204
|
+
#This works but doesn't actually use transactions. Because crate doesn't support them.
|
205
|
+
DB.transaction do
|
206
|
+
posts.insert(:id => SecureRandom.uuid, :category => 'ruby', :author => 'jeremy')
|
207
|
+
posts.where('stamp < ?', Date.today - 7).update(:state => 'archived')
|
208
|
+
end
|
209
|
+
|
210
|
+
##################
|
211
|
+
# Joining Tables #
|
212
|
+
##################
|
213
|
+
|
214
|
+
#Nope
|
215
|
+
|
216
|
+
###############################
|
217
|
+
# Column references in Sequel #
|
218
|
+
###############################
|
219
|
+
|
220
|
+
items.where(:x => 1).all
|
221
|
+
|
222
|
+
items.where(1 => :x).all
|
223
|
+
|
224
|
+
###############################################
|
225
|
+
# Qualifying identifiers (column/table names) #
|
226
|
+
###############################################
|
227
|
+
|
228
|
+
puts items.literal(:items__price)
|
229
|
+
|
230
|
+
puts items.literal(Sequel.qualify(:items, :price))
|
231
|
+
|
232
|
+
# This doesn't make sense here, crate doesn't have databases, only tables.
|
233
|
+
#DB[:some_schema__posts]
|
234
|
+
|
235
|
+
######################
|
236
|
+
# Identifier aliases #
|
237
|
+
######################
|
238
|
+
|
239
|
+
puts items.literal(:price___p)
|
240
|
+
puts items.literal(:items__price___p)
|
241
|
+
puts items.literal(Sequel.as(:price, :p))
|
242
|
+
puts items.literal(Sequel.as(DB[:posts].select{max(id)}, :p))
|
243
|
+
|
244
|
+
#################
|
245
|
+
# Sequel Models #
|
246
|
+
#################
|
247
|
+
|
248
|
+
class Post < Sequel::Model
|
249
|
+
def before_create
|
250
|
+
self.id = SecureRandom.uuid
|
251
|
+
super
|
252
|
+
end
|
253
|
+
|
254
|
+
def after_create
|
255
|
+
super
|
256
|
+
puts 'After create hook called'
|
257
|
+
end
|
258
|
+
|
259
|
+
def after_destroy
|
260
|
+
super
|
261
|
+
puts 'After destroy hook called'
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
puts Post.table_name
|
266
|
+
|
267
|
+
###################
|
268
|
+
# Model instances #
|
269
|
+
###################
|
270
|
+
|
271
|
+
uuid = SecureRandom.uuid
|
272
|
+
posts.insert(:id => uuid, :category => 'ruby', :title => 'hello world', :author => 'jeremy')
|
273
|
+
DB.run('REFRESH TABLE posts')
|
274
|
+
|
275
|
+
post = Post[uuid]
|
276
|
+
puts post.pk
|
277
|
+
|
278
|
+
|
279
|
+
class PostWithCompositeKey < Sequel::Model(:posts)
|
280
|
+
set_primary_key [:category, :title]
|
281
|
+
end
|
282
|
+
post_with_composite_key = PostWithCompositeKey['ruby', 'hello world']
|
283
|
+
puts post_with_composite_key.pk
|
284
|
+
|
285
|
+
|
286
|
+
puts Post[:title => 'hello world']
|
287
|
+
puts Post.first{num_comments < 10}
|
288
|
+
|
289
|
+
#######################
|
290
|
+
# Acts like a dataset #
|
291
|
+
#######################
|
292
|
+
|
293
|
+
Post.where(:category => 'ruby').each{|post| p post}
|
294
|
+
|
295
|
+
Post.where{num_comments < 7}.delete
|
296
|
+
Post.where(Sequel.like(:title, 'ruby')).update(:category => 'ruby')
|
297
|
+
|
298
|
+
###########################
|
299
|
+
# Accessing record values #
|
300
|
+
###########################
|
301
|
+
|
302
|
+
puts post.values
|
303
|
+
puts post.id
|
304
|
+
puts post.title
|
305
|
+
|
306
|
+
puts post[:id]
|
307
|
+
puts post[:title]
|
308
|
+
|
309
|
+
post.title = 'hey there'
|
310
|
+
post[:title] = 'hey there'
|
311
|
+
post.save
|
312
|
+
|
313
|
+
###################
|
314
|
+
# Mass assignment #
|
315
|
+
###################
|
316
|
+
|
317
|
+
post.set(:title=>'hey there', :updated_by=>'foo')
|
318
|
+
post.save
|
319
|
+
|
320
|
+
post.update(:title => 'hey there', :updated_by=>'foobar')
|
321
|
+
|
322
|
+
########################
|
323
|
+
# Creating new records #
|
324
|
+
########################
|
325
|
+
|
326
|
+
post = Post.create(:title => 'hello world')
|
327
|
+
|
328
|
+
post = Post.new
|
329
|
+
post.title = 'hello world'
|
330
|
+
post.save
|
331
|
+
|
332
|
+
post = Post.new do |p|
|
333
|
+
p.title = 'hello world'
|
334
|
+
end
|
335
|
+
|
336
|
+
post = Post.create{|p| p.title = 'hello world'}
|
337
|
+
|
338
|
+
#########
|
339
|
+
# Hooks #
|
340
|
+
#########
|
341
|
+
|
342
|
+
#example in the Post model
|
343
|
+
|
344
|
+
####################
|
345
|
+
# Deleting records #
|
346
|
+
####################
|
347
|
+
|
348
|
+
begin
|
349
|
+
post = Post.first
|
350
|
+
post.delete # => bypasses hooks
|
351
|
+
post = Post.last
|
352
|
+
post.destroy # => runs hooks
|
353
|
+
rescue Sequel::NoExistingObject
|
354
|
+
#No worries
|
355
|
+
end
|
356
|
+
|
357
|
+
Post.where(:category => 'mysql').delete # => bypasses hooks
|
358
|
+
Post.where(:category => 'ruby').destroy # => runs hooks
|
359
|
+
|
360
|
+
################
|
361
|
+
# Associations #
|
362
|
+
################
|
363
|
+
|
364
|
+
#Hmm
|
365
|
+
|
366
|
+
#################
|
367
|
+
# Eager Loading #
|
368
|
+
#################
|
369
|
+
|
370
|
+
# Double hmm
|
371
|
+
|
372
|
+
#############################
|
373
|
+
# Joining with Associations #
|
374
|
+
#############################
|
375
|
+
|
376
|
+
# not gonna happen
|
377
|
+
|
378
|
+
####################################
|
379
|
+
# Extending the underlying dataset #
|
380
|
+
####################################
|
381
|
+
|
382
|
+
class PostWithDatasetMethods < Sequel::Model(:posts)
|
383
|
+
dataset_module do
|
384
|
+
def clean_posts_with_few_comments
|
385
|
+
posts_with_few_comments.delete
|
386
|
+
end
|
387
|
+
end
|
388
|
+
|
389
|
+
subset(:posts_with_few_comments){num_comments < 30}
|
390
|
+
subset :invisible, Sequel.~(:visible)
|
391
|
+
end
|
392
|
+
|
393
|
+
PostWithDatasetMethods.where(:category => 'ruby').clean_posts_with_few_comments
|
394
|
+
PostWithDatasetMethods.where(:category => 'ruby').invisible.all
|
395
|
+
|
396
|
+
#####################
|
397
|
+
# Model Validations #
|
398
|
+
#####################
|
399
|
+
|
400
|
+
class PostWithValidation < Sequel::Model(:posts)
|
401
|
+
def validate
|
402
|
+
super
|
403
|
+
errors.add(:name, "can't be empty") if name.nil? || name.empty?
|
404
|
+
errors.add(:written_on, "should be in the past") if written_on.nil? || written_on >= Time.now
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
begin
|
409
|
+
PostWithValidation.create(:title => 'hello world')
|
410
|
+
rescue Sequel::ValidationFailed
|
411
|
+
puts 'Yay, validation worked'
|
412
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'sequel/adapters/jdbc'
|
2
|
+
|
3
|
+
require 'jdbc/crate'
|
4
|
+
Jdbc::Crate.load_driver
|
5
|
+
|
6
|
+
module Sequel
|
7
|
+
module JDBC
|
8
|
+
Sequel.synchronize do
|
9
|
+
DATABASE_SETUP[:crate] = proc do |db|
|
10
|
+
db.extend(Sequel::JDBC::Crate::DatabaseMethods)
|
11
|
+
db.extend_datasets(Sequel::JDBC::Crate::DatasetMethods)
|
12
|
+
|
13
|
+
#table names in crate are always downcased unless you double-quote them. so Table gets table, but "Table" remains Table
|
14
|
+
#sequel will quote table names so dont do anything to it
|
15
|
+
db.identifier_input_method = nil
|
16
|
+
|
17
|
+
Jdbc::Crate::Driver::CrateDriver
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Database and Dataset instance methods for Crate specific
|
22
|
+
# support via JDBC.
|
23
|
+
module Crate
|
24
|
+
# Database instance methods for Crate databases accessed via JDBC.
|
25
|
+
module DatabaseMethods
|
26
|
+
|
27
|
+
#this is for when fetching rows and want to convert
|
28
|
+
# def setup_type_convertor_map
|
29
|
+
# @type_convertor_map = TypeConvertor::MAP.merge(Java::JavaSQL::Types::TIMESTAMP=>timestamp_convertor)
|
30
|
+
# @basic_type_convertor_map = TypeConvertor::BASIC_MAP
|
31
|
+
# end
|
32
|
+
|
33
|
+
# default behaviour will mangle timezones
|
34
|
+
def to_application_timestamp(v)
|
35
|
+
#TODO: triple check this
|
36
|
+
Time.local(*v)
|
37
|
+
end
|
38
|
+
|
39
|
+
# # there is no AUTOINCREMENT
|
40
|
+
def serial_primary_key_options
|
41
|
+
{:primary_key => true, :type=>:String}
|
42
|
+
end
|
43
|
+
|
44
|
+
#only one string type called 'string'
|
45
|
+
def type_literal_generic_string(column)
|
46
|
+
:string
|
47
|
+
end
|
48
|
+
|
49
|
+
#crate supports integer, long, short, double, float and byte
|
50
|
+
def type_literal_generic_numeric(column)
|
51
|
+
:double
|
52
|
+
end
|
53
|
+
|
54
|
+
def type_literal_generic_float(column)
|
55
|
+
:double
|
56
|
+
end
|
57
|
+
|
58
|
+
def type_literal_generic_bignum(column)
|
59
|
+
:integer
|
60
|
+
end
|
61
|
+
|
62
|
+
def type_literal_generic_integer(column)
|
63
|
+
:integer
|
64
|
+
end
|
65
|
+
|
66
|
+
#timestamp is only time related type
|
67
|
+
def type_literal_generic_date(column)
|
68
|
+
:timestamp
|
69
|
+
end
|
70
|
+
def type_literal_generic_datetime(column)
|
71
|
+
:timestamp
|
72
|
+
end
|
73
|
+
def type_literal_generic_timestamp(column)
|
74
|
+
:timestamp
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
#Sequel error wrapper will swallow exceptions without this
|
79
|
+
def database_error_classes
|
80
|
+
[Java::IoCrateActionSql::SQLActionException]
|
81
|
+
end
|
82
|
+
|
83
|
+
#this isn't called but needed so that sequel will do schema parsing
|
84
|
+
#sequel asks: respond_to?(:schema_parse_table, true)
|
85
|
+
def schema_parse_table(table_name, opts)
|
86
|
+
end
|
87
|
+
|
88
|
+
#crate doesn't support transactions
|
89
|
+
def begin_transaction(*args); end
|
90
|
+
def commit_transaction(*args); end
|
91
|
+
def rollback_transaction(*args); end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
module DatasetMethods
|
96
|
+
# crate needs times in iso8601 format
|
97
|
+
def literal_time_append(sql, t)
|
98
|
+
literal_string_append(sql, t.iso8601)
|
99
|
+
end
|
100
|
+
|
101
|
+
#From: SELECT count(*) AS "count" FROM "posts" WHERE ("category" LIKE '%ruby%' ESCAPE '\') LIMIT 1
|
102
|
+
#To: SELECT count(*) AS "count" FROM "posts" WHERE ("category" LIKE '%ruby%') LIMIT 1
|
103
|
+
def complex_expression_sql_append(sql, op, args)
|
104
|
+
sql = super
|
105
|
+
sql.gsub!(" ESCAPE '\\'",'')
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel-jdbc-crate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ahto Jussila
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sequel
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.14.0
|
20
|
+
requirement: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 4.14.0
|
25
|
+
prerelease: false
|
26
|
+
type: :runtime
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: jdbc-crate
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.1.0
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ~>
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 1.1.1.0
|
39
|
+
prerelease: false
|
40
|
+
type: :runtime
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 4.0.4
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ~>
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 4.0.4
|
53
|
+
prerelease: false
|
54
|
+
type: :development
|
55
|
+
description: Sequel JDBC adapter for Crate
|
56
|
+
email:
|
57
|
+
- ahto@jussila.org
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- README.md
|
63
|
+
- LICENSE.txt
|
64
|
+
- examples.rb
|
65
|
+
- lib/sequel/adapters/jdbc/crate.rb
|
66
|
+
homepage: http://github.com/ahto/sequel-jdbc-crate
|
67
|
+
licenses:
|
68
|
+
- Apache-2
|
69
|
+
metadata: {}
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options:
|
72
|
+
- --main
|
73
|
+
- README.md
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.1.9
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: Crate jdbc for Sequel.
|
92
|
+
test_files: []
|