Class: M4DBI::Statement

Inherits:
Object show all
Defined in:
lib/m4dbi/statement.rb

Instance Method Summary collapse

Constructor Details

#initialize(rdbi_statement, m4dbi_dbh = nil) ⇒ Statement

Returns a new instance of Statement



5
6
7
8
# File 'lib/m4dbi/statement.rb', line 5

def initialize( rdbi_statement, m4dbi_dbh = nil )
  @st = rdbi_statement
  @synchronizer = m4dbi_dbh || Mutex.new
end

Instance Method Details

#execute(*args) ⇒ Object Also known as: update, u, insert, i, delete, d



16
17
18
19
20
# File 'lib/m4dbi/statement.rb', line 16

def execute( *args )
  self.synchronize do
    @st.execute *args
  end
end

#finishObject



22
23
24
25
26
# File 'lib/m4dbi/statement.rb', line 22

def finish
  self.synchronize do
    @st.finish
  end
end

#select(*bindvars) ⇒ Object Also known as: select_all, s



28
29
30
31
32
# File 'lib/m4dbi/statement.rb', line 28

def select( *bindvars )
  self.synchronize do
    @st.execute( *bindvars ).fetch( :all, RDBI::Result::Driver::Struct )
  end
end

#select_column(*bindvars) ⇒ Object Also known as: sc



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/m4dbi/statement.rb', line 38

def select_column( *bindvars )
  rows = nil
  self.synchronize do
    rows = @st.execute( *bindvars ).fetch( 1, RDBI::Result::Driver::Array )
  end
  if rows.any?
    rows[0][0]
  else
    raise RDBI::Error.new( "Query returned no rows." )
  end
end

#select_one(*bindvars) ⇒ Object Also known as: s1



34
35
36
# File 'lib/m4dbi/statement.rb', line 34

def select_one( *bindvars )
  select( *bindvars )[0]
end

#synchronizeObject



10
11
12
13
14
# File 'lib/m4dbi/statement.rb', line 10

def synchronize
  @synchronizer.synchronize do
    yield
  end
end