Class: MonetDB

Inherits:
Object
  • Object
show all
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

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.

Returns:

  • (Boolean)


249
250
251
# File 'lib/MonetDB.rb', line 249

def auto_commit?
  @connection.auto_commit?
end

#closeObject

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

#initalizeObject



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

Returns:

  • (Boolean)


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

#reconnectObject

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

#releaseObject

Release a savepoint ID



264
265
266
# File 'lib/MonetDB.rb', line 264

def release
  @connection.transactions.release
end

#saveObject

Create a new savepoint ID



259
260
261
# File 'lib/MonetDB.rb', line 259

def save
  @connection.transactions.save
end

#transactionsObject

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