Class: M4DBI::Database

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

Instance Method Summary collapse

Constructor Details

#initialize(rdbi_dbh) ⇒ Database



7
8
9
10
# File 'lib/m4dbi/database.rb', line 7

def initialize( rdbi_dbh )
  @dbh = rdbi_dbh
  @mutex = Mutex.new
end

Instance Method Details

#connected?Boolean



80
81
82
# File 'lib/m4dbi/database.rb', line 80

def connected?
  @dbh.connected?
end

#database_nameObject



92
93
94
# File 'lib/m4dbi/database.rb', line 92

def database_name
  @dbh.database_name
end

#disconnectObject



84
85
86
# File 'lib/m4dbi/database.rb', line 84

def disconnect
  @dbh.disconnect
end

#driverObject



104
105
106
# File 'lib/m4dbi/database.rb', line 104

def driver
  @dbh.driver
end

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



24
25
26
27
28
29
30
31
32
33
# File 'lib/m4dbi/database.rb', line 24

def execute( *args )
  result = nil
  self.synchronize do
    result = @dbh.execute(*args)
  end
  if defined?( RDBI::Driver::PostgreSQL ) && RDBI::Driver::PostgreSQL === @dbh.driver
    result.finish
  end
  result
end

#last_queryObject



100
101
102
# File 'lib/m4dbi/database.rb', line 100

def last_query
  @dbh.last_query
end

#prepare(*args) ⇒ Object



18
19
20
21
22
# File 'lib/m4dbi/database.rb', line 18

def prepare( *args )
  self.synchronize do
    Statement.new( @dbh.prepare(*args), self )
  end
end

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



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/m4dbi/database.rb', line 35

def select( sql, *bindvars )
  result = nil
  rows = nil
  self.synchronize do
    result = @dbh.execute( sql, *bindvars )
    rows = result.fetch( :all, RDBI::Result::Driver::Struct )
  end
  if defined?( RDBI::Driver::PostgreSQL ) && RDBI::Driver::PostgreSQL === @dbh.driver
    result.finish
  end
  rows
end

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/m4dbi/database.rb', line 52

def select_column( sql, *bindvars )
  result = nil
  rows = nil
  self.synchronize do
    result = @dbh.execute( sql, *bindvars )
    rows = result.fetch( 1, RDBI::Result::Driver::Array )
  end
  if defined?( RDBI::Driver::PostgreSQL ) && RDBI::Driver::PostgreSQL === @dbh.driver
    result.finish
  end
  if rows.any?
    rows[0][0]
  else
    raise RDBI::Error.new( "Query returned no rows.  SQL: #{@dbh.last_query}" )
  end
end

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



48
49
50
# File 'lib/m4dbi/database.rb', line 48

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

#synchronizeObject



12
13
14
15
16
# File 'lib/m4dbi/database.rb', line 12

def synchronize
  @mutex.synchronize do
    yield
  end
end

#table_schema(*args) ⇒ Object



88
89
90
# File 'lib/m4dbi/database.rb', line 88

def table_schema( *args )
  @dbh.table_schema( *args )
end

#transaction(&block) ⇒ Object



96
97
98
# File 'lib/m4dbi/database.rb', line 96

def transaction( &block )
  @dbh.transaction &block
end