forked from tinco/ruby-monetdb-xquery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
155 lines (103 loc) · 3.96 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
Warning: this version has crude support for xquery added and has not been properly tested or completed, use with caution.
== Standalone driver ==
This directory contains the a ruby interface to monetdb5
written in pure ruby.
lib/MonetDB.rb
lib/MonetDBConnection.rb
lib/MonetDBStatements.rb
lib/MonetDBData.rb
lib/MonetDBExceptions.rb
lib/hasher.rb
lib/demo.rb: demo application how to interact with the database
ruby-monetdb-sql-0.1.gemspec: make file for rubygems
doc/: rubydoc in HTML format
== Installation ==
The standalone monetdb driver can be installed using the RubyGems Package Manager.
First build a gem file starting from the gemspec configuration:
$ gem build ruby-monetdb-sql-0.1.gemspec
Then install with the command:
$ gem install ruby-monetdb-sql-0.1.gem
== Usage ==
To use the standalone driver import the 'MonetDB' class and 'rubygems' (in case you installed it using gems).
A typical sequence of events is as follows:
Invoke query using the database handle to send the statement to the server and get back a result set object.
A result set object has methods for fetching rows, moving around in the result set, obtaining column metadata, and releasing the result set.
Use a row fetching method such as fetch_row or an iterator such as each to access the rows of the result set.
If you want a count of the number of rows in the result set: invoke 'num_rows' method.
Invoke 'free' to release the result set.
== Example ==
require 'MonetDB'
db = MonetDB.new
db.connect(user = "monetdb", passwd = "monetdb", lang = "sql", host="127.0.0.1", port = 50000, db_name = "demo", auth_type = "SHA1")
# set type_cast=true to enable MonetDB to Ruby type mapping
res = db.query("SELECT * from tables;", type_cast = false)
#puts res.debug_columns_type
puts "Number of rows returned: " + res.num_rows.to_s
puts "Number of fields: " + res.num_fields.to_s
# Get the columns' name
col_names = res.name_fields
# Iterate over the record set and retrieve on row at a time
puts res.fetch
while row = res.fetch do
printf "%s \n", row
end
# Release the result set.
res.free
# Disconnect from server
db.close
See lib/demo.rb and the MonetDBDatar class documentation for more examples.
== ActiveRecord connector adapter ==
Active Record connects business objects and database tables to create a persistable domain model where logic and data are presented in one wrapping. It‘s an implementation of the object-relational mapping (ORM) pattern.
Required files:
adapter/lib/active_record/monetdb_adapter.rb
Usage example follows:
require 'active_record'
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.colorize_logging = true
ActiveRecord::Base.establish_connection(
:adapter => "monetdb",
:host => "localhost",
:database => "demo"
)
# Create a new table
class AddTests < ActiveRecord::Migration
def self.up
create_table :tests do |table|
table.column :name, :string
table.column :surname, :string
end
end
def self.down
drop_table :tests
end
end
AddTests.up
# Migration: add a column name with a default value
class AddAge < ActiveRecord::Migration
def self.up
add_column :tests, :age, :smallint, :default => 18
end
def self.down
remove_column :tests, :age
end
end
class Test < ActiveRecord::Base
end
# Insert an entry in the table
Test.create(:name => 'X', :surname => 'Y')
# add a column
AddAge.up
# return the first result of the query SELECT * from tables
row = Test.find(:first)
printf "SELECT * from tests LIMIT 1:\n"
printf "Name: %s, Surname: %s, Age: %s\n", row.name, row.surname, row.age
# Drop the table
AddTests.down
== Rubygem ==
The standalone ruby driver can be distributed as a ruby gem.
A gem file is already available; however, it can be generated
starting from the ruby-monetdb-sql-0.1.gemspec file:
$ gem build ruby-monetdb-sql-0.1.gemspec
To install the file run the command:
$ gem install ruby-monetdb-sql-0.1.gem
Documentation in ri and html format will be generated and installed as well