Class: Seasar::DBI::DBIInterceptor

Inherits:
Object
  • Object
show all
Defined in:
lib/seasar/dbi/dbi-interceptor.rb

Defined Under Namespace

Classes: ConnectError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDBIInterceptor

  • args

    • none



27
28
29
30
31
# File 'lib/seasar/dbi/dbi-interceptor.rb', line 27

def initialize
  @dbh = :di, ::DBI::DatabaseHandle
  @statements = {}
  @auto_finish = true
end

Instance Attribute Details

#auto_finishObject

Returns the value of attribute auto_finish.



32
33
34
# File 'lib/seasar/dbi/dbi-interceptor.rb', line 32

def auto_finish
  @auto_finish
end

Instance Method Details

#call(invocation) ⇒ Object

  • args

    1. Seasar::Aop::MethodInvocation invocation

  • return

    • Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/seasar/dbi/dbi-interceptor.rb', line 41

def call(invocation)
  sql, context = invocation.proceed
  sql.strip!
  context = invocation.args if context.nil?
  if !context.is_a?(Array) && !context.is_a?(Hash)
    context = [context]
  end
    
  if not @statements.key?(sql) or @statements[sql].finished?
    s2logger.debug(self.class.name){"#{sql} : (#{context.inspect})"}
    @statements[sql] = @dbh.prepare(sql)
  end
  begin
    if context.is_a?(Array)
      @statements[sql].execute(*context)
    else
      @statements[sql].execute(context)
    end
  rescue => e
    @statements[sql].finish
    raise e
  end

  if @statements[sql].column_names.size == 0
    result = @statements[sql].rows
  else
    result = []
    @statements[sql].fetch_hash {|row| result << row}
  end

  @statements[sql].finish if @auto_finish
  return result
end

#finish_allObject

  • args

    • none

  • return

    • none



81
82
83
# File 'lib/seasar/dbi/dbi-interceptor.rb', line 81

def finish_all
  @statements.each {|key, val| val.finish if not val.finished? }
end