Class: DBrb

Inherits:
Object
  • Object
show all
Defined in:
lib/DB.rb

Instance Method Summary collapse

Constructor Details

#initialize(driver, usr = nil, pwd = nil, params = nil) ⇒ DBrb

Returns a new instance of DBrb.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/DB.rb', line 46

def initialize driver, usr=nil, pwd=nil, params=nil
	@driver=driver
	@usr=usr
	@pwd=pwd
	@params=params
	

	@pstatements = {}

	if block_given?
		yield self
		self.close
	end

end

Instance Method Details

#closeObject

Closes all resources used by this object instance.



118
119
120
121
122
123
124
125
# File 'lib/DB.rb', line 118

def close 
	@pstatements.each_value {|pstmt|
		pstmt[0].finish
	}
	@pstatements = {}
	@db.disconnect
	@db = nil
end

#dumpStmtStatsObject

Debug method. This method dumps statistics about how often each sql statement was executed by this instance. Statistics are reset after calls to ‘close`



130
131
132
133
134
# File 'lib/DB.rb', line 130

def dumpStmtStats 
	@pstatements.each_pair { |stmt, arr|
		puts "#{stmt} : #{arr[1]}"
	} if @pstatements
end

#sql(stmt, *args, &block) ⇒ Object

sql is the method that does all the work. The only required parameter is stmt, which passes a string with the SQL statement to be executed. The optional parameters in args get bound to ‘?’-placeholders in the statement.

This method can be called in several ways.

  • Statements not returning results:

db.sql “UPDATE sometable SET somevalue=somevalue+1”

  • If the statement returns a single value, that value is returned by the method call:

value = db.sql “SELECT value FROM sometable LIMIT 1”

  • Statements returning several values in a single row:

first_name, last_name = db.sql “SELECT first, last FROM sometable LIMIT 1”

(It should go without saying: if a single row result is expected like in the examples above but the statement returns several rows, the first one is used.)

  • If a block is passed to the method, it can be used to iterate over the results.

db.sql(“SELECT first, last FROM sometable WHERE last=?”, “Smith”) do |row| puts “Name: #DBrb.rowrow.first #DBrb.rowrow.last end

  • or, in case the resulting rows only contain a single column :

db.sql “SELECT first FROM sometable” do |firstname| puts “Hello #firstname!” end



98
99
100
101
# File 'lib/DB.rb', line 98

def sql(stmt, *args, &block) #:yields: row if block_given?
			#optional args and an optional block
	_sql_internal(stmt,false,*args, &block)
end

#sql_count(stmt, *args, &block) ⇒ Object

Similar to the sql method. This method is meant for ‘INSERT` and `UPDATE` statements and returns the RPC (row processed count), i.e. the number of rows affected by the statement.

It’s possible to pass a SELECT statement to this method, (optionally passing in a block) and hoping that the returned value indicated the number of selected rows, but that doesn’t always work. (Mysql work, Postgres doesn’t, others: don’t know.)



113
114
115
# File 'lib/DB.rb', line 113

def sql_count (stmt, *args, &block) #:yields: row if block_given?
	_sql_internal(stmt,true,*args,&block)	
end

#to_sObject



136
137
138
# File 'lib/DB.rb', line 136

def to_s
	"DBrb: #{@driver}"
end