First commit; add MonetDB ruby client

parents
Pipeline #4638 skipped
* Wed Mar 2 2016 Sjoerd Mullender <sjoerd@acm.org>
- The Ruby interface to MonetDB is now a separate package.
- The Ruby interface was updated to Ruby 2, and the activerecord
integration was removed.
VERSION = 1.0
monetdb-sql-$(VERSION).gem: monetdb-sql.gemspec \
lib/MonetDB.rb lib/MonetDBConnection.rb \
lib/MonetDBData.rb lib/MonetDBExceptions.rb lib/hasher.rb
gem build $<
.PHONY: clean
clean:
rm -f *.gem
rm -f *.rpm
.PHONY: srpm
srpm: rubygem-monetdb-sql.spec monetdb-sql-$(VERSION).gem
mkdir -p rpmbuild/RPMS
mkdir -p rpmbuild/SRPMS
mkdir -p rpmbuild/BUILD
mkdir -p rpmbuild/SOURCES
cp monetdb-sql-$(VERSION).gem rpmbuild/SOURCES
rpmbuild --define="_topdir $$PWD/rpmbuild" --define='_tmppath /tmp' --define='tmpdir %{_tmppath}' --define='dist %{?dummymacro}' -bs $<
mv rpmbuild/SRPMS/*.src.rpm .
rm -rf rpmbuild
.PHONY: rpm
rpm: rubygem-monetdb-sql.spec monetdb-sql-$(VERSION).gem
mkdir -p rpmbuild/RPMS
mkdir -p rpmbuild/SRPMS
mkdir -p rpmbuild/BUILD
mkdir -p rpmbuild/SOURCES
cp monetdb-sql-$(VERSION).gem rpmbuild/SOURCES
rpmbuild --define="_topdir $$PWD/rpmbuild" --define='_tmppath /tmp' --define='tmpdir %{_tmppath}' --define='dist %{?dummymacro}' -ba $<
mv rpmbuild/RPMS/noarch/*.noarch.rpm .
mv rpmbuild/SRPMS/*.src.rpm .
rm -rf rpmbuild
.PHONY: deb
deb: monetdb-sql-$(VERSION).gem debian/changelog debian/control debian/rules \
debian/copyright debian/source/format debian/compat
pdebuild --use-pdebuild-internal
* test and improve utf8 and type conversion
* documentation cleanup
* OSM on rails demo (slowed down due to third party plugins requirements)
ruby-monetdb-sql (1.0) unstable; urgency=low
* The Ruby interface to MonetDB is now a separate package.
* The Ruby interface was updated to Ruby 2, and the activerecord
integration was removed.
-- Sjoerd Mullender <sjoerd@acm.org> Wed, 02 Mar 2016 12:27:21 +0100
Source: ruby-monetdb-sql
Section: ruby
Priority: optional
Maintainer: MonetDB BV <info@monetdb.org>
Build-Depends: debhelper (>= 9~),
gem2deb
Standards-Version: 3.9.6
Vcs-Hg: http://dev.monetdb.org/hg/monetdb-ruby/
Vcs-Browser: http://dev.monetdb.org/hg/monetdb-ruby/
Homepage: https://www.monetdb.org/
Testsuite: autopkgtest-pkg-ruby
XS-Ruby-Versions: all
Package: ruby-monetdb-sql
Architecture: all
XB-Ruby-Versions: ${ruby:Versions}
Depends: ruby | ruby-interpreter,
${misc:Depends},
${shlibs:Depends}
Recommends: monetdb5-sql
Conflicts: ruby-monetdb-client
Replaces: ruby-monetdb-client
Description: Pure Ruby database driver for MonetDB/SQL
MonetDB is a database management system that is developed from a
main-memory perspective with use of a fully decomposed storage model,
automatic index management, extensibility of data types and search
accelerators. It also has an SQL frontend.
.
This package contains a pure Ruby database driver for MonetDB/SQL.
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: monetdb-sql
Upstream-Contact: info@monetdb.org
Source: http://dev.monetdb.org/downloads/
Files: *
Copyright: 2016 MonetDB B.V.
License: MPL-2.0
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
.
Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
#!/usr/bin/make -f
#export DH_VERBOSE=1
#
# Uncomment to ignore all test failures (but the tests will run anyway)
#export DH_RUBY_IGNORE_TESTS=all
#
# Uncomment to ignore some test failures (but the tests will run anyway).
# Valid values:
#export DH_RUBY_IGNORE_TESTS=ruby2.1 ruby2.2
#
# If you need to specify the .gemspec (eg there is more than one)
#export DH_RUBY_GEMSPEC=gem.gemspec
#
# Uncomment to check dependencies during build:
# export GEM2DEB_TEST_RUNNER = --check-dependencies
%:
dh $@ --buildsystem=ruby --with ruby
=== Install the standalone driver ===
First build a gem file starting from the given gemspec:
$ gem build ruby-monetdb-sql-0.2.gemspec
and install the resulting gem with the command:
$ gem install --local ruby-monetdb-sql-0.2.gem
=== Tutorial ===
A short example on how to use Ruby with MonetDB can be found in the lib/example.rb file. Make sure you create a database named "testdatabase2" and have a MonetDB server running on your system prior to trying the script. Instructions on how to run the server and create the database can be found here:
https://www.monetdb.org/Documentation/UserGuide/Tutorial
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
# = Introduction
#
# A typical sequence of events is as follows:
# Create a database instance (handle), 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.
# A result set object is an instance of the MonetDBData class.
#
# Records can be returned as arrays and iterators over the set.
#
# A database handler (dbh) is and instance of the MonetDB class.
#
# = Connection management
#
# connect - establish a new connection
# * user: username (default is monetdb)
# * passwd: password (default is monetdb)
# * lang: language (default is sql)
# * host: server hostanme or ip (default is localhost)
# * port: server port (default is 50000)
# * db_name: name of the database to connect to
# * auth_type: hashing function to use during authentication (default is SHA1)
#
# is_connected? - returns true if there is an active connection to a server, false otherwise
# reconnect - recconnect to a server
# close - terminate a connection
# auto_commit? - returns ture if the session is running in auto commit mode, false otherwise
# auto_commit - enable/disable auto commit mode.
#
# query - fire a query
#
# Currently MAPI protocols 8 and 9 are supported.
#
# = Managing record sets
#
#
# A record set is represented as an instance of the MonetDBData class; the class provides methods to manage retrieved data.
#
#
# The following methods allow to iterate over data:
#
# fetch - iterates over the record set and retrieves one row at a time. Each row is returned as an array.
# each_record - works as ruby each method. The method takes a block as parameter and yields each record to this block.
# fetch_hash - iterates over the record set and retrieves one row at a time. Each row is returned as a hash.
# each_record_as_hash - works as ruby each method. The method takes a block as parameter and yields each record, as hash, to this block
# fetch_all - returns all rows as a two dimensional array
# fetch_all_as_column_hash - returns all records as a hash with the column name as the keys and an array with all column values as values
#
# Information about the retrieved record set can be obtained via the following methods:
#
# num_rows - returns the number of rows present in the record set
# num_fields - returns the number of fields (columns) that compose the schema
# name_fields - returns the (ordered) name of the schema's columns
# type_fields - returns the (ordered) types list of the schema's columns
#
# To release a record set MonetDBData#free can be used.
#
# = Type conversion
#
# All values from the database are converted to the closest ruby type, i.e: INTEGER to int, TIME to time, CLOB to string
# Some of the more complex datatypes are not recognized, such as INTERVAL, these are converted to strings
#
# = Transactions
#
# By default monetdb works in auto_commit mode. To turn this feature off MonetDB#auto_commit(flag=false) can be used.
#
# Once auto_commit has been disable it is possible to start transactions, create/delete savepoints, rollback and commit with
# the usual SQL statements.
#
# Savepoints IDs can be generated using the MonetDB#save method. To release a savepoint ID use MonetDB#release.
#
# Savepoints can be accessed (as a stack) with the MonetDB#transactions method.
#
# demo.rb contains usage example of the above mentioned methods.
require_relative 'MonetDBConnection'
require_relative 'MonetDBData'
require_relative 'MonetDBExceptions'
class MonetDB
DEFAULT_USERNAME = "monetdb"
DEFAULT_PASSWORD = "monetdb"
DEFAULT_LANG = MonetDBConnection::LANG_SQL
DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 50000
DEFAULT_DATABASE = "test"
DEFAULT_AUTHTYPE = "SHA1"
def initalize()
@connection = nil
end
# Establish a new connection.
# * username: username (default is monetdb)
# * password: password (default is monetdb)
# * lang: language (default is sql)
# * host: server hostanme or ip (default is localhost)
# * port: server port (default is 50000)
# * db_name: name of the database to connect to
# * auth_type: hashing function to use during authentication (default is SHA1)
def connect(username=DEFAULT_USERNAME, password=DEFAULT_PASSWORD, lang=DEFAULT_LANG, host=DEFAULT_HOST, port=DEFAULT_PORT, db_name=DEFAULT_DATABASE, auth_type=DEFAULT_AUTHTYPE)
# TODO: handle pools of connections
@username = username
@password = password
@lang = lang
@host = host
@port = port
@db_name = db_name
@auth_type = auth_type
@connection = MonetDBConnection.new(user = @username, passwd = @password, lang = @lang, host = @host, port = @port)
@connection.connect(@db_name, @auth_type)
end
# Establish a new connection using named parameters.
# * user: username (default is monetdb)
# * passwd: password (default is monetdb)
# * language: lang (default is sql)
# * host: host to connect to (default is localhost)
# * port: port to connect to (default is 50000)
# * database: name of the database to connect to
# * auth_type: hashing function to use during authentication (default is SHA1)
#
# Conventionally named parameters are passed as an hash.
#
# Ruby 1.8:
# MonetDB::conn({ :user => "username", :passwd => "password", :database => "database"})
#
# Ruby 1.9:
# MonetDB::conn(user: "username", passwd: "password", database: "database")
def conn(options)
user = options[:user] || DEFAULT_USERNAME
passwd = options[:passwd] || DEFAULT_PASSWORD
language = options[:language] || DEFAULT_LANG
host = options[:host] || DEFAULT_HOST
port = options[:port] || DEFAULT_PORT
database = options[:database] || DEFAULT_DATABASE
auth_type = options[:auth_type] || DEFAULT_AUTHTYPE
connect(user, passwd, language, host, port, database, auth_type)
end
# Send a <b> user submitted </b> query to the server and store the response.
# Returns and instance of MonetDBData.
def query(q="")
if @connection != nil
@data = MonetDBData.new(@connection)
@data.execute(q)
end
return @data
end
# Return true if there exists a "connection" object
def is_connected?
if @connection == nil
return false
else
return true
end
end
# Reconnect to the server
def reconnect
if @connection != nil
self.close
@connection = MonetDBConnection.new(user = @username, passwd = @password, lang = @lang, host = @host, port = @port)
@connection.connect(db_name = @db_name, auth_type = @auth_type)
end
end
# Turn auto commit on/off
def auto_commit(flag=true)
@connection.set_auto_commit(flag)
end
# Returns the current auto commit (on/off) settings.
def auto_commit?
@connection.auto_commit?
end
# Returns the name of the last savepoint in a transactions pool
def transactions
@connection.savepoint
end
# Create a new savepoint ID
def save
@connection.transactions.save
end
# Release a savepoint ID
def release
@connection.transactions.release
end
# Close an active connection
def close()
@connection.disconnect
@connection = nil
end
end
This diff is collapsed.
This diff is collapsed.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
# Exception classes for the ruby-monetdb driver
class MonetDBQueryError < StandardError
def initialize(e)
$stderr.puts e
end
end
class MonetDBDataError < StandardError
def initialize(e)
$stderr.puts e
end
end
class MonetDBCommandError < StandardError
def initialize(e)
$stderr.puts e
end
end
class MonetDBConnectionError < StandardError
def initialize(e)
$stderr.puts e
end
end
class MonetDBSocketError < StandardError
def initialize(e)
$stderr.puts e
end
end
class MonetDBProtocolError < StandardError
def initialize(e)
$stderr.puts e
end
end
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
require_relative 'MonetDB'
def print_result(result, message="")
puts message
puts "========"
result.each_record do |record|
puts record
end
puts "========"
puts
end
def get_database_connection
database_connection = MonetDB.new
database_connection.connect(user = "monetdb", passwd = "monetdb", lang = "sql", host="127.0.0.1", port = 50000, database_connection_name = "testdatabase2", auth_type = "SHA1")
return database_connection
end
# Connect to the database. This assumes you have a MonetDB server running with a database called testdatabase2
db = get_database_connection
# Add a table and some data to the database
db.query("CREATE TABLE testtable (testnumber INTEGER)")
db.query("INSERT INTO testtable(testnumber) VALUES (1)")
db.query("INSERT INTO testtable(testnumber) VALUES (2)")
# Get some data from the database
result = db.query("SELECT * FROM testtable")
print_result(result, "Showing all data in table")
# Use of autocommit
db.auto_commit(false)
db.query("INSERT INTO testtable(testnumber) VALUES (100)")
db.query("INSERT INTO testtable(testnumber) VALUES (101)")
result = db.query("SELECT * FROM testtable")
print_result(result, "First connection: 100 and 101 have not yet been saved, but are shown because they were added by the same connection")
# Show that the data is not really in the database yet
db2 = get_database_connection
result = db2.query("SELECT * FROM testtable")
print_result(result, "Second connection: 100 and 101 are not shown, because autocommit is off and it is a different connection")
# Save the database data
db.query("COMMIT")
result = db2.query("SELECT * FROM testtable")
print_result(result, "Second connection: 100 and 101 are shown now, because they were committed in db connection 1")
db.auto_commit(true)
# Get the data with other public interface methods
puts "number of rows: #{result.num_rows}"
puts "Fetch all: #{result.fetch_all}"
puts "As column hash: #{result.fetch_all_as_column_hash}"
puts "Iterate one at a time: #{result.fetch}"
result.reset_index # restart the iterator used in the single fetch methods
puts "Iterate one at a time as hash: #{result.fetch_hash}"
# close the connection
db.query("DROP TABLE testtable")
db.close
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
require 'digest/md5'
require 'digest/sha1'
require 'digest/sha2'
class Hasher
# Constructor
# method = "SHA1" or "MD5"
# pwd = Password
def initialize(method, pwd)
if (method.upcase == "SHA1")
@hashfunc = Digest::SHA1.new
@hashname = method.upcase
elsif (method.upcase == "SHA256")
@hashfunc = Digest::SHA256.new
@hashname = method.upcase
elsif (method.upcase == "SHA384")
@hashfunc = Digest::SHA384.new
@hashname = method.upcase
elsif (method.upcase == "SHA512")
@hashfunc = Digest::SHA512.new
@hashname = method.upcase
else
# default to MD5
@hashfunc = Digest::MD5.new
@hashname = "MD5"
end
@pwd = pwd
end
def hashname
@hashname
end
# Compute hash code
def hashsum
return @hashfunc.hexdigest(@pwd)
end
end
Gem::Specification.new do |s|
s.required_ruby_version = '>= 1.8.0'
s.name = %q{monetdb-sql}
s.version = "1.0"
s.date = %q{2016-03-02}
s.authors = ["G Modena", "R Koopmanschap"]
s.email = "info@monetdb.org"
s.license = "MPL-2.0"
s.summary = %q{Pure Ruby database driver for MonetDB/SQL}
s.homepage = %q{http://www.monetdb.org/}
s.description = %q{Pure Ruby database driver for the MonetDB/SQL columnar database management system}
s.files = ["lib/MonetDB.rb", "lib/MonetDBConnection.rb", "lib/MonetDBData.rb", "lib/MonetDBExceptions.rb", "lib/hasher.rb"]
s.has_rdoc = true
s.require_path = './lib'
# placeholder project to avoid warning about not having a rubyforge_project
s.rubyforge_project = "nowarning"
end
%global gem_name monetdb-sql
Name: rubygem-%{gem_name}
Epoch: 1
Version: 1.0
Release: 1%{?dist}
Summary: Pure Ruby database driver for MonetDB/SQL
Group: Applications/Databases
License: MPLv2.0
URL: http://www.monetdb.org/
Source0: http://dev.monetdb.org/downloads/ruby/gems/%{gem_name}-%{version}.gem
BuildRequires: ruby(release)
BuildRequires: rubygems-devel
BuildRequires: ruby >= 1.8.0
BuildArch: noarch
Requires: ruby(release)
Requires: rubygem-bigdecimal
Recommends: MonetDB-SQL-server5
Suggests: %{name}-doc = %{version}-%{release}
%description
MonetDB is a database management system that is developed from a
main-memory perspective with use of a fully decomposed storage model,
automatic index management, extensibility of data types and search
accelerators. It also has an SQL frontend.
This package contains a pure Ruby database driver for MonetDB/SQL.
%package doc
Summary: Documentation for %{name}
Group: Documentation
Requires: %{name} = %{version}-%{release}
BuildArch: noarch
%description doc
This package contains documentation for %{name}.
%prep
%setup -q -c -T
cp %{SOURCE0} .
%build
%gem_install
%install
mkdir -p %{buildroot}%{gem_dir}
cp -a .%{gem_dir}/* %{buildroot}/%{gem_dir}
find %{buildroot}%{gem_instdir} -name \*.rb -exec chmod 0644 '{}' +
%files
%dir %{gem_instdir}
%{gem_libdir}
%exclude %{gem_cache}
%{gem_spec}
%files doc
%doc %{gem_docdir}
%changelog
* Wed Mar 2 2016 Sjoerd Mullender <sjoerd@acm.org> - 1.0-1
- The Ruby interface to MonetDB is now a separate package.
- The Ruby interface was updated to Ruby 2, and the activerecord
integration was removed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment