Skip to content

Commit 64a073c

Browse files
committed
Add basic example program for connecting to CrateDB with Ruby
1 parent 78c9471 commit 64a073c

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

by-language/ruby/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Gemfile.lock

by-language/ruby/Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
gem 'crate_ruby', '<1'
6+
gem 'pg', '<2'

by-language/ruby/README.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
##################################################
2+
Basic examples for connecting to CrateDB with Ruby
3+
##################################################
4+
5+
6+
*****
7+
About
8+
*****
9+
10+
This is a basic example program for connecting to CrateDB with Ruby.
11+
It uses the `crate_ruby`_ driver to connect to CrateDB using HTTP.
12+
It also demonstrates CrateDB's `PostgreSQL wire protocol`_ compatibility by
13+
exercising the same example using Ruby's canonical `pg`_ driver.
14+
15+
For further information, please also visit the `CrateDB clients and tools`_ page.
16+
Because CrateDB only supports (implicitly created) `table schemas`_ instead of databases,
17+
it makes sense to also have a look at the `PostgreSQL documentation about schemas`_.
18+
19+
20+
*****
21+
Usage
22+
*****
23+
24+
Run CrateDB::
25+
26+
docker run -it --rm --publish=4200:4200 --publish=5432:5432 crate:5.1.1
27+
28+
Install example program::
29+
30+
git clone https://github.com/crate/cratedb-examples
31+
cd cratedb-examples/by-language/ruby
32+
33+
gem install bundler
34+
bundle install
35+
36+
Invoke example program::
37+
38+
# Using `crate_ruby` driver.
39+
ruby ruby_example.rb crate_ruby
40+
41+
# Using `pg` driver.
42+
ruby ruby_example.rb pg
43+
44+
45+
.. _CrateDB clients and tools: https://crate.io/docs/crate/clients-tools/
46+
.. _crate_ruby: https://github.com/crate/crate_ruby
47+
.. _pg: https://rubygems.org/gems/pg
48+
.. _PostgreSQL documentation about schemas: https://www.postgresql.org/docs/current/ddl-schemas.html
49+
.. _PostgreSQL wire protocol: https://crate.io/docs/reference/en/latest/protocols/postgres.html
50+
.. _table schemas: https://crate.io/docs/crate/reference/en/latest/general/ddl/create-table.html#schemas

by-language/ruby/ruby_example.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#
2+
# Basic examples for connecting to CrateDB with Ruby, using both HTTP and PostgreSQL protocols.
3+
#
4+
# https://github.com/crate/cratedb-examples/tree/main/spikes/ruby-basic
5+
#
6+
7+
def demo_crate_ruby
8+
9+
puts "=" * 42
10+
puts "Demo using `crate_ruby` library"
11+
puts "=" * 42
12+
13+
# Use `crate_ruby` library.
14+
require 'crate_ruby'
15+
client = CrateRuby::Client.new(servers = ["localhost:4200"], opts = {username: "crate"})
16+
17+
# Insert data.
18+
client.execute("CREATE TABLE IF NOT EXISTS testdrive (id INT PRIMARY KEY, data TEXT);")
19+
client.execute("DELETE FROM testdrive;")
20+
client.execute("INSERT INTO testdrive VALUES (0, 'zero'), (1, 'one'), (2, 'two')")
21+
client.execute("REFRESH TABLE testdrive;")
22+
23+
# Query data.
24+
result = client.execute("SELECT * FROM testdrive ORDER BY id")
25+
26+
puts "Columns and values:"
27+
p Columns: result.cols, Results: result.values
28+
puts
29+
30+
puts "Results by row:"
31+
result.each do |row|
32+
puts row.inspect
33+
end
34+
35+
end
36+
37+
38+
def demo_pg
39+
40+
puts "=" * 42
41+
puts "Demo using `pg` library"
42+
puts "=" * 42
43+
44+
# Use `pg` library.
45+
require 'pg'
46+
client = PG.connect("postgresql://crate@localhost:5432/doc")
47+
48+
# Insert data.
49+
client.exec("CREATE TABLE IF NOT EXISTS testdrive (id INT PRIMARY KEY, data TEXT);")
50+
client.exec("DELETE FROM testdrive;")
51+
client.exec("INSERT INTO testdrive VALUES (0, 'zero'), (1, 'one'), (2, 'two')")
52+
client.exec("REFRESH TABLE testdrive;")
53+
54+
# Query data.
55+
result = client.exec("SELECT * FROM testdrive ORDER BY id")
56+
57+
puts "Columns and values:"
58+
p Columns: result.fields, Results: result.values
59+
puts
60+
61+
puts "Results by row:"
62+
result.each do |row|
63+
puts row.inspect
64+
end
65+
66+
end
67+
68+
69+
if __FILE__ == $0
70+
driver = ARGV[0]
71+
if driver == "crate_ruby"
72+
demo_crate_ruby()
73+
elsif driver == "pg"
74+
demo_pg()
75+
else
76+
raise "Unknown driver: #{driver}. Please use 'crate_ruby' or 'pg'."
77+
end
78+
end

0 commit comments

Comments
 (0)