Class: MonetDB
- Inherits:
-
Object
- Object
- MonetDB
- Defined in:
- lib/MonetDB.rb
Overview
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 returneds 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 on row at a time. Each row is returned as an array. fetch_hash - iterates over columns (on cell at a time). fetch_all_hash - returns record set entries hashed by column name orderd by column position.
To return the record set as an array (with each tuple stored as array of fields) the following method can be used:
fetch_all - fetch all rows and store them
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
To release a record set MonetDBData#free can be used.
Type conversion
Invoking MonetDB#query with the flag type_conversion=true will result in a type cast of the record set fields from SQL types to ruby types
demo.rb contains usage example of the above mentioned methods.
Instance Method Summary collapse
-
#auto_commit(flag = true) ⇒ Object
Turn auto commit on/off.
-
#auto_commit? ⇒ Boolean
Returns the current auto commit (on/off) settings.
-
#close ⇒ Object
Close an active connection.
-
#connect(username = "monetdb", password = "monetdb", lang = "sql", host = "127.0.0.1", port = "50000", db_name = "test", auth_type = "SHA1") ⇒ Object
Establish a new connection.
- #initalize ⇒ Object
-
#is_connected? ⇒ Boolean
Return true if there exists a “connection” object.
-
#query(q = "") ⇒ Object
Send a user submitted query to the server and store the response.
-
#reconnect ⇒ Object
Reconnect to the server.
-
#release ⇒ Object
Release a savepoint ID.
-
#save ⇒ Object
Create a new savepoint ID.
-
#transactions ⇒ Object
Returns the name of the last savepoint in a transactions pool.
Instance Method Details
#auto_commit(flag = true) ⇒ Object
Turn auto commit on/off
244 245 246 |
# File 'lib/MonetDB.rb', line 244 def auto_commit(flag=true) @connection.set_auto_commit(flag) end |
#auto_commit? ⇒ Boolean
Returns the current auto commit (on/off) settings.
249 250 251 |
# File 'lib/MonetDB.rb', line 249 def auto_commit? @connection.auto_commit? end |
#close ⇒ Object
Close an active connection
269 270 271 272 |
# File 'lib/MonetDB.rb', line 269 def close() @connection.disconnect @connection = nil end |
#connect(username = "monetdb", password = "monetdb", lang = "sql", host = "127.0.0.1", port = "50000", db_name = "test", auth_type = "SHA1") ⇒ Object
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)
201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/MonetDB.rb', line 201 def connect(username = "monetdb", password = "monetdb", lang = "sql", host="127.0.0.1", port = "50000", db_name = "test", auth_type = "SHA1") # 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 |
#initalize ⇒ Object
189 190 191 |
# File 'lib/MonetDB.rb', line 189 def initalize() @connection = nil end |
#is_connected? ⇒ Boolean
Return true if there exists a “connection” object
225 226 227 228 229 230 231 |
# File 'lib/MonetDB.rb', line 225 def is_connected? if @connection == nil return false else return true end end |
#query(q = "") ⇒ Object
Send a user submitted query to the server and store the response. Returns and instance of MonetDBData.
216 217 218 219 220 221 222 |
# File 'lib/MonetDB.rb', line 216 def query(q = "") if @connection != nil @data = MonetDBData.new(@connection) @data.execute(q) end return @data end |
#reconnect ⇒ Object
Reconnect to the server
234 235 236 237 238 239 240 241 |
# File 'lib/MonetDB.rb', line 234 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 |
#release ⇒ Object
Release a savepoint ID
264 265 266 |
# File 'lib/MonetDB.rb', line 264 def release @connection.transactions.release end |
#save ⇒ Object
Create a new savepoint ID
259 260 261 |
# File 'lib/MonetDB.rb', line 259 def save @connection.transactions.save end |
#transactions ⇒ Object
Returns the name of the last savepoint in a transactions pool
254 255 256 |
# File 'lib/MonetDB.rb', line 254 def transactions @connection.savepoint end |